forked from microsoft/vcpkg-tool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommands.set-installed.cpp
352 lines (314 loc) · 14.8 KB
/
commands.set-installed.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#include <vcpkg/base/contractual-constants.h>
#include <vcpkg/base/downloads.h>
#include <vcpkg/base/json.h>
#include <vcpkg/base/system.debug.h>
#include <vcpkg/binarycaching.h>
#include <vcpkg/cmakevars.h>
#include <vcpkg/commands.install.h>
#include <vcpkg/commands.set-installed.h>
#include <vcpkg/input.h>
#include <vcpkg/metrics.h>
#include <vcpkg/portfileprovider.h>
#include <vcpkg/registries.h>
#include <vcpkg/vcpkgcmdarguments.h>
#include <vcpkg/vcpkglib.h>
#include <vcpkg/vcpkgpaths.h>
using namespace vcpkg;
namespace
{
constexpr CommandSwitch INSTALL_SWITCHES[] = {
{SwitchDryRun, msgCmdSetInstalledOptDryRun},
{SwitchNoPrintUsage, msgCmdSetInstalledOptNoUsage},
{SwitchOnlyDownloads, msgHelpTxtOptOnlyDownloads},
{SwitchKeepGoing, msgHelpTxtOptKeepGoing},
{SwitchEnforcePortChecks, msgHelpTxtOptEnforcePortChecks},
{SwitchAllowUnsupported, msgHelpTxtOptAllowUnsupportedPort},
};
constexpr CommandSetting INSTALL_SETTINGS[] = {
{SwitchXWriteNuGetPackagesConfig, msgCmdSetInstalledOptWritePkgConfig},
};
} // unnamed namespace
namespace vcpkg
{
constexpr CommandMetadata CommandSetInstalledMetadata = {
"x-set-installed",
msgCmdSetInstalledSynopsis,
{msgCmdSetInstalledExample1, "vcpkg x-set-installed zlib:x64-windows boost"},
Undocumented,
AutocompletePriority::Public,
0,
SIZE_MAX,
{INSTALL_SWITCHES, INSTALL_SETTINGS},
nullptr,
};
Optional<Json::Object> create_dependency_graph_snapshot(const VcpkgCmdArguments& args,
const ActionPlan& action_plan)
{
const auto github_ref = args.github_ref.get();
const auto github_sha = args.github_sha.get();
const auto github_job = args.github_job.get();
const auto github_workflow = args.github_workflow.get();
const auto github_run_id = args.github_run_id.get();
if (github_ref && github_sha && github_job && github_workflow && github_run_id)
{
Json::Object detector;
detector.insert(JsonIdName, Json::Value::string("vcpkg"));
detector.insert(JsonIdUrl, Json::Value::string("https://github.com/microsoft/vcpkg"));
detector.insert(JsonIdVersion, Json::Value::string("1.0.0"));
Json::Object job;
job.insert(JsonIdId, Json::Value::string(*github_run_id));
job.insert(JsonIdCorrelator, Json::Value::string(fmt::format("{}-{}", *github_workflow, *github_run_id)));
Json::Object snapshot;
snapshot.insert(JsonIdJob, job);
snapshot.insert(JsonIdVersion, Json::Value::integer(0));
snapshot.insert(JsonIdSha, Json::Value::string(*github_sha));
snapshot.insert(JsonIdRef, Json::Value::string(*github_ref));
snapshot.insert(JsonIdScanned, Json::Value::string(CTime::now_string()));
snapshot.insert(JsonIdDetector, detector);
Json::Object manifest;
manifest.insert(JsonIdName, FileVcpkgDotJson);
std::unordered_map<std::string, std::string> map;
for (auto&& action : action_plan.install_actions)
{
const auto scfl = action.source_control_file_and_location.get();
if (!scfl)
{
return nullopt;
}
auto spec = action.spec.to_string();
map.insert(
{spec, fmt::format("pkg:github/vcpkg/{}@{}", spec, scfl->source_control_file->to_version())});
}
Json::Object resolved;
for (auto&& action : action_plan.install_actions)
{
Json::Object resolved_item;
auto spec = action.spec.to_string();
const auto found = map.find(spec);
if (found != map.end())
{
const auto& pkg_url = found->second;
resolved_item.insert(JsonIdPackageUnderscoreUrl, pkg_url);
resolved_item.insert(JsonIdRelationship, Json::Value::string(JsonIdDirect));
Json::Array deps_list;
for (auto&& dep : action.package_dependencies)
{
const auto found_dep = map.find(dep.to_string());
if (found_dep != map.end())
{
deps_list.push_back(found_dep->second);
}
}
resolved_item.insert(JsonIdDependencies, deps_list);
resolved.insert(pkg_url, resolved_item);
}
}
manifest.insert(JsonIdResolved, resolved);
Json::Object manifests;
manifests.insert(JsonIdVcpkgDotJson, manifest);
snapshot.insert(JsonIdManifests, manifests);
Debug::print(Json::stringify(snapshot));
return snapshot;
}
return nullopt;
}
std::set<PackageSpec> adjust_action_plan_to_status_db(ActionPlan& action_plan, const StatusParagraphs& status_db)
{
std::set<std::string> all_abis;
for (const auto& action : action_plan.install_actions)
{
all_abis.insert(action.abi_info.value_or_exit(VCPKG_LINE_INFO).package_abi);
}
std::vector<PackageSpec> specs_to_remove;
std::set<PackageSpec> specs_installed;
for (auto&& status_pgh : status_db)
{
if (!status_pgh->is_installed()) continue;
if (status_pgh->package.is_feature()) continue;
const auto& abi = status_pgh->package.abi;
if (abi.empty() || !Util::Sets::contains(all_abis, abi))
{
specs_to_remove.push_back(status_pgh->package.spec);
}
else
{
specs_installed.emplace(status_pgh->package.spec);
}
}
action_plan.remove_actions = create_remove_plan(specs_to_remove, status_db).remove;
for (const auto& action : action_plan.remove_actions)
{
// This should not technically be needed, however ensuring that all specs to be removed are not included in
// `specs_installed` acts as a sanity check
specs_installed.erase(action.spec);
}
Util::erase_remove_if(action_plan.install_actions, [&](const InstallPlanAction& ipa) {
return Util::Sets::contains(specs_installed, ipa.spec);
});
return specs_installed;
}
void command_set_installed_and_exit_ex(const VcpkgCmdArguments& args,
const VcpkgPaths& paths,
Triplet host_triplet,
const BuildPackageOptions& build_options,
const CMakeVars::CMakeVarProvider& cmake_vars,
ActionPlan action_plan,
DryRun dry_run,
PrintUsage print_usage,
const Optional<Path>& maybe_pkgconfig,
bool include_manifest_in_github_issue)
{
auto& fs = paths.get_filesystem();
cmake_vars.load_tag_vars(action_plan, host_triplet);
compute_all_abis(paths, action_plan, cmake_vars, {});
std::vector<PackageSpec> user_requested_specs;
for (const auto& action : action_plan.install_actions)
{
if (action.request_type == RequestType::USER_REQUESTED)
{
// save for reporting usage later
user_requested_specs.push_back(action.spec);
}
}
if (paths.manifest_mode_enabled() && paths.get_feature_flags().dependency_graph)
{
msg::println(msgDependencyGraphCalculation);
auto snapshot = create_dependency_graph_snapshot(args, action_plan);
bool s = false;
if (snapshot.has_value() && args.github_token.has_value() && args.github_repository.has_value())
{
s = send_snapshot_to_api(*args.github_token.get(), *args.github_repository.get(), *snapshot.get());
if (s)
{
msg::println(msgDependencyGraphSuccess);
}
else
{
msg::println(msgDependencyGraphFailure);
}
}
get_global_metrics_collector().track_bool(BoolMetric::DependencyGraphSuccess, s);
}
// currently (or once) installed specifications
auto status_db = database_load_check(fs, paths.installed());
adjust_action_plan_to_status_db(action_plan, status_db);
print_plan(action_plan, paths.builtin_ports_directory());
if (auto p_pkgsconfig = maybe_pkgconfig.get())
{
auto pkgsconfig_path = paths.original_cwd / *p_pkgsconfig;
auto pkgsconfig_contents = generate_nuget_packages_config(action_plan, args.nuget_id_prefix.value_or(""));
fs.write_contents(pkgsconfig_path, pkgsconfig_contents, VCPKG_LINE_INFO);
msg::println(msgWroteNuGetPkgConfInfo, msg::path = pkgsconfig_path);
}
if (dry_run == DryRun::Yes)
{
Checks::exit_success(VCPKG_LINE_INFO);
}
paths.flush_lockfile();
track_install_plan(action_plan);
install_preclear_packages(paths, action_plan);
auto binary_cache = build_options.only_downloads == OnlyDownloads::Yes
? BinaryCache(paths.get_filesystem())
: BinaryCache::make(args, paths, out_sink).value_or_exit(VCPKG_LINE_INFO);
binary_cache.fetch(action_plan.install_actions);
const auto summary = install_execute_plan(args,
paths,
host_triplet,
build_options,
action_plan,
status_db,
binary_cache,
null_build_logs_recorder(),
include_manifest_in_github_issue);
if (build_options.keep_going == KeepGoing::Yes && summary.failed())
{
summary.print_failed();
if (build_options.only_downloads == OnlyDownloads::No)
{
Checks::exit_fail(VCPKG_LINE_INFO);
}
}
if (print_usage == PrintUsage::Yes)
{
// Note that this differs from the behavior of `vcpkg install` in that it will print usage information for
// packages named but not installed here
std::set<std::string> printed_usages;
for (auto&& ur_spec : user_requested_specs)
{
auto it = status_db.find_installed(ur_spec);
if (it != status_db.end())
{
install_print_usage_information(it->get()->package, printed_usages, fs, paths.installed());
}
}
}
Checks::exit_success(VCPKG_LINE_INFO);
}
void command_set_installed_and_exit(const VcpkgCmdArguments& args,
const VcpkgPaths& paths,
Triplet default_triplet,
Triplet host_triplet)
{
// input sanitization
const ParsedArguments options = args.parse_arguments(CommandSetInstalledMetadata);
const std::vector<FullPackageSpec> specs = Util::fmap(options.command_arguments, [&](const std::string& arg) {
return check_and_get_full_package_spec(arg, default_triplet, paths.get_triplet_db())
.value_or_exit(VCPKG_LINE_INFO);
});
const auto only_downloads =
Util::Sets::contains(options.switches, SwitchOnlyDownloads) ? OnlyDownloads::Yes : OnlyDownloads::No;
const auto keep_going =
Util::Sets::contains(options.switches, SwitchKeepGoing) || only_downloads == OnlyDownloads::Yes
? KeepGoing::Yes
: KeepGoing::No;
const auto unsupported_port_action = Util::Sets::contains(options.switches, SwitchAllowUnsupported)
? UnsupportedPortAction::Warn
: UnsupportedPortAction::Error;
const auto prohibit_backcompat_features = Util::Sets::contains(options.switches, SwitchEnforcePortChecks)
? BackcompatFeatures::Prohibit
: BackcompatFeatures::Allow;
const BuildPackageOptions build_options{
BuildMissing::Yes,
AllowDownloads::Yes,
only_downloads,
CleanBuildtrees::Yes,
CleanPackages::Yes,
CleanDownloads::No,
DownloadTool::Builtin,
prohibit_backcompat_features,
keep_going,
};
auto& fs = paths.get_filesystem();
auto registry_set = paths.make_registry_set();
PathsPortFileProvider provider(*registry_set,
make_overlay_provider(fs, paths.original_cwd, paths.overlay_ports));
auto cmake_vars = CMakeVars::make_triplet_cmake_var_provider(paths);
Optional<Path> pkgsconfig;
auto it_pkgsconfig = options.settings.find(SwitchXWriteNuGetPackagesConfig);
if (it_pkgsconfig != options.settings.end())
{
get_global_metrics_collector().track_define(DefineMetric::X_WriteNugetPackagesConfig);
pkgsconfig = it_pkgsconfig->second;
}
// We have a set of user-requested specs.
// We need to know all the specs which are required to fulfill dependencies for those specs.
// Therefore, we see what we would install into an empty installed tree, so we can use the existing code.
auto action_plan = create_feature_install_plan(
provider,
*cmake_vars,
specs,
{},
{nullptr, host_triplet, paths.packages(), unsupported_port_action, UseHeadVersion::No, Editable::No});
command_set_installed_and_exit_ex(
args,
paths,
host_triplet,
build_options,
*cmake_vars,
std::move(action_plan),
Util::Sets::contains(options.switches, SwitchDryRun) ? DryRun::Yes : DryRun::No,
Util::Sets::contains(options.switches, SwitchNoPrintUsage) ? PrintUsage::No : PrintUsage::Yes,
pkgsconfig,
false);
}
} // namespace vcpkg