-
Notifications
You must be signed in to change notification settings - Fork 302
/
Copy pathcommands.build.cpp
1932 lines (1722 loc) · 87.9 KB
/
commands.build.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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <vcpkg/base/cache.h>
#include <vcpkg/base/checks.h>
#include <vcpkg/base/chrono.h>
#include <vcpkg/base/contractual-constants.h>
#include <vcpkg/base/file_sink.h>
#include <vcpkg/base/hash.h>
#include <vcpkg/base/message_sinks.h>
#include <vcpkg/base/messages.h>
#include <vcpkg/base/optional.h>
#include <vcpkg/base/stringview.h>
#include <vcpkg/base/system.debug.h>
#include <vcpkg/base/system.h>
#include <vcpkg/base/system.process.h>
#include <vcpkg/base/system.proxy.h>
#include <vcpkg/base/util.h>
#include <vcpkg/base/uuid.h>
#include <vcpkg/binarycaching.h>
#include <vcpkg/buildenvironment.h>
#include <vcpkg/cmakevars.h>
#include <vcpkg/commands.build.h>
#include <vcpkg/commands.version.h>
#include <vcpkg/dependencies.h>
#include <vcpkg/documentation.h>
#include <vcpkg/input.h>
#include <vcpkg/installedpaths.h>
#include <vcpkg/metrics.h>
#include <vcpkg/paragraphs.h>
#include <vcpkg/portfileprovider.h>
#include <vcpkg/postbuildlint.h>
#include <vcpkg/registries.h>
#include <vcpkg/spdx.h>
#include <vcpkg/statusparagraphs.h>
#include <vcpkg/tools.h>
#include <vcpkg/vcpkgcmdarguments.h>
#include <vcpkg/vcpkglib.h>
#include <vcpkg/vcpkgpaths.h>
#include <numeric>
using namespace vcpkg;
namespace
{
struct NullBuildLogsRecorder final : IBuildLogsRecorder
{
void record_build_result(const VcpkgPaths& paths, const PackageSpec& spec, BuildResult result) const override
{
(void)paths;
(void)spec;
(void)result;
}
};
static const NullBuildLogsRecorder null_build_logs_recorder_instance;
}
namespace vcpkg
{
void command_build_and_exit_ex(const VcpkgCmdArguments& args,
const VcpkgPaths& paths,
Triplet host_triplet,
const BuildPackageOptions& build_options,
const FullPackageSpec& full_spec,
const PathsPortFileProvider& provider,
const IBuildLogsRecorder& build_logs_recorder)
{
Checks::exit_with_code(
VCPKG_LINE_INFO,
command_build_ex(args, paths, host_triplet, build_options, full_spec, provider, build_logs_recorder));
}
constexpr CommandMetadata CommandBuildMetadata{
"build",
msgCmdBuildSynopsis,
{msgCmdBuildExample1, "vcpkg build zlib:x64-windows"},
Undocumented,
AutocompletePriority::Internal,
1,
1,
{},
nullptr,
};
void command_build_and_exit(const VcpkgCmdArguments& args,
const VcpkgPaths& paths,
Triplet default_triplet,
Triplet host_triplet)
{
// Build only takes a single package and all dependencies must already be installed
const ParsedArguments options = args.parse_arguments(CommandBuildMetadata);
static constexpr BuildPackageOptions build_command_build_package_options{
BuildMissing::Yes,
AllowDownloads::Yes,
OnlyDownloads::No,
CleanBuildtrees::No,
CleanPackages::No,
CleanDownloads::No,
DownloadTool::Builtin,
BackcompatFeatures::Allow,
PrintUsage::Yes,
};
const FullPackageSpec spec =
check_and_get_full_package_spec(options.command_arguments[0], default_triplet, paths.get_triplet_db())
.value_or_exit(VCPKG_LINE_INFO);
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));
Checks::exit_with_code(VCPKG_LINE_INFO,
command_build_ex(args,
paths,
host_triplet,
build_command_build_package_options,
spec,
provider,
null_build_logs_recorder()));
}
int command_build_ex(const VcpkgCmdArguments& args,
const VcpkgPaths& paths,
Triplet host_triplet,
const BuildPackageOptions& build_options,
const FullPackageSpec& full_spec,
const PathsPortFileProvider& provider,
const IBuildLogsRecorder& build_logs_recorder)
{
const PackageSpec& spec = full_spec.package_spec;
auto var_provider_storage = CMakeVars::make_triplet_cmake_var_provider(paths);
auto& var_provider = *var_provider_storage;
var_provider.load_dep_info_vars({{spec}}, host_triplet);
StatusParagraphs status_db = database_load_check(paths.get_filesystem(), paths.installed());
auto action_plan = create_feature_install_plan(
provider,
var_provider,
{&full_spec, 1},
status_db,
{nullptr, host_triplet, paths.packages(), UnsupportedPortAction::Error, UseHeadVersion::No, Editable::Yes});
var_provider.load_tag_vars(action_plan, host_triplet);
compute_all_abis(paths, action_plan, var_provider, status_db);
InstallPlanAction* action = nullptr;
for (auto& install_action : action_plan.already_installed)
{
if (install_action.spec == full_spec.package_spec)
{
Checks::msg_exit_with_error(VCPKG_LINE_INFO, msgBuildAlreadyInstalled, msg::spec = spec);
}
}
for (auto& install_action : action_plan.install_actions)
{
if (install_action.spec == full_spec.package_spec)
{
action = &install_action;
}
}
Checks::check_exit(VCPKG_LINE_INFO, action != nullptr);
ASSUME(action != nullptr);
auto& scf = *action->source_control_file_and_location.value_or_exit(VCPKG_LINE_INFO).source_control_file;
const auto& spec_name = spec.name();
const auto& core_paragraph_name = scf.to_name();
if (spec_name != core_paragraph_name)
{
Checks::msg_exit_with_error(VCPKG_LINE_INFO,
msgSourceFieldPortNameMismatch,
msg::package_name = core_paragraph_name,
msg::path = spec_name);
}
auto binary_cache = BinaryCache::make(args, paths, out_sink).value_or_exit(VCPKG_LINE_INFO);
const ElapsedTimer build_timer;
const auto result =
build_package(args, paths, host_triplet, build_options, *action, build_logs_recorder, status_db);
msg::print(msgElapsedForPackage, msg::spec = spec, msg::elapsed = build_timer);
switch (result.code)
{
case BuildResult::Succeeded: binary_cache.push_success(build_options.clean_packages, *action); return 0;
case BuildResult::CascadedDueToMissingDependencies:
{
LocalizedString errorMsg = msg::format_error(msgBuildDependenciesMissing);
for (const auto& p : result.unmet_dependencies)
{
errorMsg.append_raw('\n').append_indent().append_raw(p.to_string());
}
Checks::msg_exit_with_message(VCPKG_LINE_INFO, errorMsg);
}
case BuildResult::BuildFailed:
case BuildResult::PostBuildChecksFailed:
case BuildResult::FileConflicts:
case BuildResult::CacheMissing:
case BuildResult::Downloaded:
case BuildResult::Removed:
{
LocalizedString warnings;
for (auto&& msg : action->build_failure_messages)
{
warnings.append(msg).append_raw('\n');
}
if (!warnings.data().empty())
{
msg::print(Color::warning, warnings);
}
msg::println_error(create_error_message(result, spec));
msg::print(create_user_troubleshooting_message(*action, paths, nullopt));
return 1;
}
case BuildResult::Excluded:
default: Checks::unreachable(VCPKG_LINE_INFO);
}
}
StringLiteral to_string_view(BuildPolicy policy)
{
switch (policy)
{
case BuildPolicy::EMPTY_PACKAGE: return PolicyEmptyPackage;
case BuildPolicy::DLLS_WITHOUT_LIBS: return PolicyDllsWithoutLibs;
case BuildPolicy::DLLS_WITHOUT_EXPORTS: return PolicyDllsWithoutExports;
case BuildPolicy::DLLS_IN_STATIC_LIBRARY: return PolicyDllsInStaticLibrary;
case BuildPolicy::MISMATCHED_NUMBER_OF_BINARIES: return PolicyMismatchedNumberOfBinaries;
case BuildPolicy::ONLY_RELEASE_CRT: return PolicyOnlyReleaseCrt;
case BuildPolicy::EMPTY_INCLUDE_FOLDER: return PolicyEmptyIncludeFolder;
case BuildPolicy::ALLOW_OBSOLETE_MSVCRT: return PolicyAllowObsoleteMsvcrt;
case BuildPolicy::ALLOW_RESTRICTED_HEADERS: return PolicyAllowRestrictedHeaders;
case BuildPolicy::SKIP_DUMPBIN_CHECKS: return PolicySkipDumpbinChecks;
case BuildPolicy::SKIP_ARCHITECTURE_CHECK: return PolicySkipArchitectureCheck;
case BuildPolicy::CMAKE_HELPER_PORT: return PolicyCMakeHelperPort;
case BuildPolicy::SKIP_ABSOLUTE_PATHS_CHECK: return PolicySkipAbsolutePathsCheck;
case BuildPolicy::SKIP_ALL_POST_BUILD_CHECKS: return PolicySkipAllPostBuildChecks;
case BuildPolicy::SKIP_APPCONTAINER_CHECK: return PolicySkipAppcontainerCheck;
case BuildPolicy::SKIP_CRT_LINKAGE_CHECK: return PolicySkipCrtLinkageCheck;
case BuildPolicy::SKIP_MISPLACED_CMAKE_FILES_CHECK: return PolicySkipMisplacedCMakeFilesCheck;
case BuildPolicy::SKIP_LIB_CMAKE_MERGE_CHECK: return PolicySkipLibCMakeMergeCheck;
case BuildPolicy::ALLOW_DLLS_IN_LIB: return PolicyAllowDllsInLib;
case BuildPolicy::SKIP_MISPLACED_REGULAR_FILES_CHECK: return PolicySkipMisplacedRegularFilesCheck;
case BuildPolicy::SKIP_COPYRIGHT_CHECK: return PolicySkipCopyrightCheck;
case BuildPolicy::ALLOW_KERNEL32_FROM_XBOX: return PolicyAllowKernel32FromXBox;
case BuildPolicy::ALLOW_EXES_IN_BIN: return PolicyAllowExesInBin;
case BuildPolicy::SKIP_USAGE_INSTALL_CHECK: return PolicySkipUsageInstallCheck;
case BuildPolicy::ALLOW_EMPTY_FOLDERS: return PolicyAllowEmptyFolders;
case BuildPolicy::ALLOW_DEBUG_INCLUDE: return PolicyAllowDebugInclude;
case BuildPolicy::ALLOW_DEBUG_SHARE: return PolicyAllowDebugShare;
case BuildPolicy::SKIP_PKGCONFIG_CHECK: return PolicySkipPkgConfigCheck;
default: Checks::unreachable(VCPKG_LINE_INFO);
}
}
std::string to_string(BuildPolicy policy) { return to_string_view(policy).to_string(); }
StringLiteral to_cmake_variable(BuildPolicy policy)
{
switch (policy)
{
case BuildPolicy::EMPTY_PACKAGE: return CMakeVariablePolicyEmptyPackage;
case BuildPolicy::DLLS_WITHOUT_LIBS: return CMakeVariablePolicyDllsWithoutLibs;
case BuildPolicy::DLLS_WITHOUT_EXPORTS: return CMakeVariablePolicyDllsWithoutExports;
case BuildPolicy::DLLS_IN_STATIC_LIBRARY: return CMakeVariablePolicyDllsInStaticLibrary;
case BuildPolicy::MISMATCHED_NUMBER_OF_BINARIES: return CMakeVariablePolicyMismatchedNumberOfBinaries;
case BuildPolicy::ONLY_RELEASE_CRT: return CMakeVariablePolicyOnlyReleaseCrt;
case BuildPolicy::EMPTY_INCLUDE_FOLDER: return CMakeVariablePolicyEmptyIncludeFolder;
case BuildPolicy::ALLOW_OBSOLETE_MSVCRT: return CMakeVariablePolicyAllowObsoleteMsvcrt;
case BuildPolicy::ALLOW_RESTRICTED_HEADERS: return CMakeVariablePolicyAllowRestrictedHeaders;
case BuildPolicy::SKIP_DUMPBIN_CHECKS: return CMakeVariablePolicySkipDumpbinChecks;
case BuildPolicy::SKIP_ARCHITECTURE_CHECK: return CMakeVariablePolicySkipArchitectureCheck;
case BuildPolicy::CMAKE_HELPER_PORT: return CMakeVariablePolicyCMakeHelperPort;
case BuildPolicy::SKIP_ABSOLUTE_PATHS_CHECK: return CMakeVariablePolicySkipAbsolutePathsCheck;
case BuildPolicy::SKIP_ALL_POST_BUILD_CHECKS: return CMakeVariablePolicySkipAllPostBuildChecks;
case BuildPolicy::SKIP_APPCONTAINER_CHECK: return CMakeVariablePolicySkipAppcontainerCheck;
case BuildPolicy::SKIP_CRT_LINKAGE_CHECK: return CMakeVariablePolicySkipCrtLinkageCheck;
case BuildPolicy::SKIP_MISPLACED_CMAKE_FILES_CHECK: return CMakeVariablePolicySkipMisplacedCMakeFilesCheck;
case BuildPolicy::SKIP_LIB_CMAKE_MERGE_CHECK: return CMakeVariablePolicySkipLibCMakeMergeCheck;
case BuildPolicy::ALLOW_DLLS_IN_LIB: return CMakeVariablePolicyAllowDllsInLib;
case BuildPolicy::SKIP_MISPLACED_REGULAR_FILES_CHECK:
return CMakeVariablePolicySkipMisplacedRegularFilesCheck;
case BuildPolicy::SKIP_COPYRIGHT_CHECK: return CMakeVariablePolicySkipCopyrightCheck;
case BuildPolicy::ALLOW_KERNEL32_FROM_XBOX: return CMakeVariablePolicyAllowKernel32FromXBox;
case BuildPolicy::ALLOW_EXES_IN_BIN: return CMakeVariablePolicyAllowExesInBin;
case BuildPolicy::SKIP_USAGE_INSTALL_CHECK: return CMakeVariablePolicySkipUsageInstallCheck;
case BuildPolicy::ALLOW_EMPTY_FOLDERS: return CMakeVariablePolicyAllowEmptyFolders;
case BuildPolicy::ALLOW_DEBUG_INCLUDE: return CMakeVariablePolicyAllowDebugInclude;
case BuildPolicy::ALLOW_DEBUG_SHARE: return CMakeVariablePolicyAllowDebugShare;
case BuildPolicy::SKIP_PKGCONFIG_CHECK: return CMakeVariablePolicySkipPkgConfigCheck;
default: Checks::unreachable(VCPKG_LINE_INFO);
}
}
StringLiteral to_string_view(DownloadTool tool)
{
switch (tool)
{
case DownloadTool::Builtin: return "BUILT_IN";
case DownloadTool::Aria2: return "ARIA2";
default: Checks::unreachable(VCPKG_LINE_INFO);
}
}
std::string to_string(DownloadTool tool) { return to_string_view(tool).to_string(); }
Optional<LinkageType> to_linkage_type(StringView str)
{
if (str == "dynamic") return LinkageType::Dynamic;
if (str == "static") return LinkageType::Static;
return nullopt;
}
#if defined(_WIN32)
static ZStringView to_vcvarsall_target(StringView cmake_system_name)
{
if (cmake_system_name.empty()) return "";
if (cmake_system_name == "Windows") return "";
if (cmake_system_name == "WindowsStore") return "store";
Checks::msg_exit_with_error(VCPKG_LINE_INFO, msgUnsupportedSystemName, msg::system_name = cmake_system_name);
}
static ZStringView to_vcvarsall_toolchain(StringView target_architecture, const Toolset& toolset, Triplet triplet)
{
auto maybe_target_arch = to_cpu_architecture(target_architecture);
if (!maybe_target_arch.has_value())
{
msg::println_error(msgInvalidArchitecture, msg::value = target_architecture);
Checks::exit_maybe_upgrade(VCPKG_LINE_INFO);
}
auto target_arch = maybe_target_arch.value_or_exit(VCPKG_LINE_INFO);
// Ask for an arm64 compiler when targeting arm64ec; arm64ec is selected with a different flag on the compiler
// command line.
if (target_arch == CPUArchitecture::ARM64EC)
{
target_arch = CPUArchitecture::ARM64;
}
auto host_architectures = get_supported_host_architectures();
for (auto&& host : host_architectures)
{
const auto it = Util::find_if(toolset.supported_architectures, [&](const ToolsetArchOption& opt) {
return host == opt.host_arch && target_arch == opt.target_arch;
});
if (it != toolset.supported_architectures.end()) return it->name;
}
const auto toolset_list = Strings::join(
", ", toolset.supported_architectures, [](const ToolsetArchOption& t) { return t.name.c_str(); });
msg::println_error(msgUnsupportedToolchain,
msg::triplet = triplet,
msg::arch = target_architecture,
msg::path = toolset.visual_studio_root_path,
msg::list = toolset_list);
msg::println(msgSeeURL, msg::url = docs::vcpkg_visual_studio_path_url);
Checks::exit_maybe_upgrade(VCPKG_LINE_INFO);
}
#endif
#if defined(_WIN32)
const Environment& EnvCache::get_action_env(const VcpkgPaths& paths,
const PreBuildInfo& pre_build_info,
const Toolset& toolset)
{
auto build_env_cmd = make_build_env_cmd(pre_build_info, toolset);
const auto& base_env = envs.get_lazy(pre_build_info.passthrough_env_vars, [&]() -> EnvMapEntry {
std::unordered_map<std::string, std::string> env;
for (auto&& env_var : pre_build_info.passthrough_env_vars)
{
auto maybe_env_val = get_environment_variable(env_var);
if (auto env_val = maybe_env_val.get())
{
env[env_var] = std::move(*env_val);
}
}
static constexpr StringLiteral s_extra_vars[] = {
EnvironmentVariableVcpkgCommand,
EnvironmentVariableVcpkgForceSystemBinaries,
EnvironmentVariableXVcpkgRecursiveData,
};
for (const auto& var : s_extra_vars)
{
auto val = get_environment_variable(var);
if (auto p_val = val.get()) env.emplace(var, *p_val);
}
/*
* On Windows 10 (>= 8.1) it is a user-friendly way to automatically set HTTP_PROXY and HTTPS_PROXY
* environment variables by reading proxy settings via WinHttpGetIEProxyConfigForCurrentUser, preventing
* users set and unset these variables manually (which is not a decent way). It is common in China or
* any other regions that needs an proxy software (v2ray, shadowsocks, etc.), which sets the IE Proxy
* Settings, but not setting environment variables. This will make vcpkg easier to use, specially when
* use vcpkg in Visual Studio, we even cannot set HTTP(S)_PROXY in CLI, if we want to open or close
* Proxy we need to restart VS.
*/
// 2021-05-09 Fix: Detect If there's already HTTP(S)_PROXY presented in the environment variables.
// If so, we no longer overwrite them.
bool proxy_from_env = (get_environment_variable(EnvironmentVariableHttpProxy).has_value() ||
get_environment_variable(EnvironmentVariableHttpsProxy).has_value());
if (proxy_from_env)
{
msg::println(msgUseEnvVar, msg::env_var = format_environment_variable("HTTP(S)_PROXY"));
}
else
{
auto ieProxy = get_windows_ie_proxy_server();
if (ieProxy.has_value() && !proxy_from_env)
{
std::string server = Strings::to_utf8(ieProxy.get()->server);
// Separate settings in IE Proxy Settings, which is rare?
// Python implementation:
// https://github.com/python/cpython/blob/7215d1ae25525c92b026166f9d5cac85fb1defe1/Lib/urllib/request.py#L2655
if (Strings::contains(server, "="))
{
auto proxy_settings = Strings::split(server, ';');
for (auto& s : proxy_settings)
{
auto kvp = Strings::split(s, '=');
if (kvp.size() == 2)
{
auto& protocol = kvp[0];
auto& address = kvp[1];
/* Unlike Python's urllib implementation about this type of proxy configuration
* (http=addr:port;https=addr:port)
* https://github.com/python/cpython/blob/7215d1ae25525c92b026166f9d5cac85fb1defe1/Lib/urllib/request.py#L2682
* we do not intentionally append protocol prefix to address. Because HTTPS_PROXY's
* address is not always an HTTPS proxy, an HTTP proxy can also proxy HTTPS requests
* without end-to-end security (As an HTTP Proxy can see your cleartext while an
* HTTPS proxy can't).
*
* If the prefix (http=http://addr:port;https=https://addr:port) already exists in
* the address, we should consider this address points to an HTTPS proxy, and assign
* to HTTPS_PROXY directly. However, if it doesn't exist, then we should NOT append
* an `https://` prefix to an `addr:port` as it could be an HTTP proxy, and the
* connection request will fail.
*/
protocol = Strings::concat(Strings::ascii_to_uppercase(protocol), "_PROXY");
env.emplace(protocol, address);
msg::println(msgSettingEnvVar,
msg::env_var = format_environment_variable(protocol),
msg::url = address);
}
}
}
// Specified http:// prefix
else if (Strings::starts_with(server, "http://"))
{
msg::println(msgSettingEnvVar,
msg::env_var = format_environment_variable(EnvironmentVariableHttpProxy),
msg::url = server);
env.emplace(EnvironmentVariableHttpProxy, server);
}
// Specified https:// prefix
else if (Strings::starts_with(server, "https://"))
{
msg::println(msgSettingEnvVar,
msg::env_var = format_environment_variable(EnvironmentVariableHttpsProxy),
msg::url = server);
env.emplace(EnvironmentVariableHttpsProxy, server);
}
// Most common case: "ip:port" style, apply to HTTP and HTTPS proxies.
// An HTTP(S)_PROXY means https requests go through that, it can be:
// http:// prefixed: the request go through an HTTP proxy without end-to-end security.
// https:// prefixed: the request go through an HTTPS proxy with end-to-end security.
// Nothing prefixed: don't know the default behaviour, seems considering HTTP proxy as default.
// We simply set "ip:port" to HTTP(S)_PROXY variables because it works on most common cases.
else
{
msg::println(msgAutoSettingEnvVar,
msg::env_var = format_environment_variable("HTTP(S)_PROXY"),
msg::url = server);
env.emplace(EnvironmentVariableHttpProxy, server.c_str());
env.emplace(EnvironmentVariableHttpsProxy, server.c_str());
}
}
}
return {env};
});
return base_env.cmd_cache.get_lazy(build_env_cmd, [&]() {
const Path& powershell_exe_path = paths.get_tool_exe("powershell-core", out_sink);
auto clean_env = get_modified_clean_environment(base_env.env_map, powershell_exe_path.parent_path());
if (build_env_cmd.empty())
return clean_env;
else
return cmd_execute_and_capture_environment(build_env_cmd, clean_env);
});
}
#else
const Environment& EnvCache::get_action_env(const VcpkgPaths&, const PreBuildInfo&, const Toolset&)
{
return get_clean_environment();
}
#endif
static CompilerInfo load_compiler_info(const VcpkgPaths& paths,
const PreBuildInfo& pre_build_info,
const Toolset& toolset);
static const std::string& get_toolchain_cache(Cache<Path, std::string>& cache,
const Path& tcfile,
const ReadOnlyFilesystem& fs)
{
return cache.get_lazy(tcfile, [&]() {
return Hash::get_file_hash(fs, tcfile, Hash::Algorithm::Sha256).value_or_exit(VCPKG_LINE_INFO);
});
}
const EnvCache::TripletMapEntry& EnvCache::get_triplet_cache(const ReadOnlyFilesystem& fs, const Path& p) const
{
return m_triplet_cache.get_lazy(p, [&]() -> TripletMapEntry {
return TripletMapEntry{Hash::get_file_hash(fs, p, Hash::Algorithm::Sha256).value_or_exit(VCPKG_LINE_INFO)};
});
}
const CompilerInfo& EnvCache::get_compiler_info(const VcpkgPaths& paths,
const PreBuildInfo& pre_build_info,
const Toolset& toolset)
{
if (!m_compiler_tracking || pre_build_info.disable_compiler_tracking)
{
static CompilerInfo empty_ci;
return empty_ci;
}
const auto& fs = paths.get_filesystem();
const auto& triplet_file_path = paths.get_triplet_db().get_triplet_file_path(pre_build_info.triplet);
auto&& toolchain_hash = get_toolchain_cache(m_toolchain_cache, pre_build_info.toolchain_file(), fs);
auto&& triplet_entry = get_triplet_cache(fs, triplet_file_path);
return triplet_entry.compiler_info.get_lazy(toolchain_hash, [&]() -> CompilerInfo {
if (m_compiler_tracking)
{
return load_compiler_info(paths, pre_build_info, toolset);
}
else
{
return CompilerInfo{};
}
});
}
const std::string& EnvCache::get_triplet_info(const VcpkgPaths& paths,
const PreBuildInfo& pre_build_info,
const Toolset& toolset)
{
const auto& fs = paths.get_filesystem();
const auto& triplet_file_path = paths.get_triplet_db().get_triplet_file_path(pre_build_info.triplet);
auto&& toolchain_hash = get_toolchain_cache(m_toolchain_cache, pre_build_info.toolchain_file(), fs);
auto&& triplet_entry = get_triplet_cache(fs, triplet_file_path);
if (m_compiler_tracking && !pre_build_info.disable_compiler_tracking)
{
return triplet_entry.triplet_infos.get_lazy(toolchain_hash, [&]() -> std::string {
auto& compiler_info = get_compiler_info(paths, pre_build_info, toolset);
return Strings::concat(triplet_entry.hash, '-', toolchain_hash, '-', compiler_info.hash);
});
}
else
{
return triplet_entry.triplet_infos_without_compiler.get_lazy(toolchain_hash, [&]() -> std::string {
return Strings::concat(triplet_entry.hash, '-', toolchain_hash);
});
}
}
vcpkg::Command make_build_env_cmd(const PreBuildInfo& pre_build_info, const Toolset& toolset)
{
if (!pre_build_info.using_vcvars()) return {};
#if !defined(WIN32)
// pre_build_info.using_vcvars() should always be false on non-Win32 hosts.
// If it was true, we should have failed earlier while selecting a Toolset
(void)toolset;
Checks::unreachable(VCPKG_LINE_INFO);
#else
const char* tonull = " >nul";
if (Debug::g_debugging)
{
tonull = "";
}
const auto arch = to_vcvarsall_toolchain(pre_build_info.target_architecture, toolset, pre_build_info.triplet);
const auto target = to_vcvarsall_target(pre_build_info.cmake_system_name);
return vcpkg::Command{"cmd"}.string_arg("/d").string_arg("/c").raw_arg(
fmt::format(R"("{}" {} {} {} {} 2>&1 <NUL)",
toolset.vcvarsall,
Strings::join(" ", toolset.vcvarsall_options),
arch,
target,
tonull));
#endif
}
static std::vector<PackageSpec> fspecs_to_pspecs(View<FeatureSpec> fspecs)
{
std::set<PackageSpec> set;
for (auto&& f : fspecs)
set.insert(f.spec());
std::vector<PackageSpec> ret{set.begin(), set.end()};
return ret;
}
static std::unique_ptr<BinaryControlFile> create_binary_control_file(const InstallPlanAction& action,
const BuildInfo& build_info)
{
const auto& scfl = action.source_control_file_and_location.value_or_exit(VCPKG_LINE_INFO);
auto bcf = std::make_unique<BinaryControlFile>();
auto find_itr = action.feature_dependencies.find(FeatureNameCore.to_string());
Checks::check_exit(VCPKG_LINE_INFO, find_itr != action.feature_dependencies.end());
BinaryParagraph bpgh(*scfl.source_control_file->core_paragraph,
action.default_features.value_or_exit(VCPKG_LINE_INFO),
action.spec.triplet(),
action.public_abi(),
fspecs_to_pspecs(find_itr->second));
if (const auto p_ver = build_info.detected_head_version.get())
{
bpgh.version = *p_ver;
}
bcf->core_paragraph = std::move(bpgh);
bcf->features.reserve(action.feature_list.size());
for (auto&& feature : action.feature_list)
{
find_itr = action.feature_dependencies.find(feature);
Checks::check_exit(VCPKG_LINE_INFO, find_itr != action.feature_dependencies.end());
auto maybe_fpgh = scfl.source_control_file->find_feature(feature);
if (auto fpgh = maybe_fpgh.get())
{
bcf->features.emplace_back(action.spec, *fpgh, fspecs_to_pspecs(find_itr->second));
}
}
return bcf;
}
static void write_binary_control_file(const Filesystem& fs, const Path& package_dir, const BinaryControlFile& bcf)
{
std::string start = Strings::serialize(bcf.core_paragraph);
for (auto&& feature : bcf.features)
{
start.push_back('\n');
start += Strings::serialize(feature);
}
const auto binary_control_file = package_dir / FileControl;
fs.write_contents(binary_control_file, start, VCPKG_LINE_INFO);
}
static void get_generic_cmake_build_args(const VcpkgPaths& paths,
Triplet triplet,
const Toolset& toolset,
std::vector<CMakeVariable>& out_vars)
{
out_vars.emplace_back(CMakeVariableCmd, "BUILD");
out_vars.emplace_back(CMakeVariableDownloads, paths.downloads);
out_vars.emplace_back(CMakeVariableTargetTriplet, triplet.canonical_name());
out_vars.emplace_back(CMakeVariableTargetTripletFile, paths.get_triplet_db().get_triplet_file_path(triplet));
out_vars.emplace_back(CMakeVariableBaseVersion, VCPKG_BASE_VERSION_AS_STRING);
out_vars.emplace_back(CMakeVariableConcurrency, std::to_string(get_concurrency()));
out_vars.emplace_back(CMakeVariablePlatformToolset, toolset.version);
// Make sure GIT could be found
out_vars.emplace_back(CMakeVariableGit, paths.get_tool_exe(Tools::GIT, out_sink));
}
static CompilerInfo load_compiler_info(const VcpkgPaths& paths,
const PreBuildInfo& pre_build_info,
const Toolset& toolset)
{
auto& triplet = pre_build_info.triplet;
msg::println(msgDetectCompilerHash, msg::triplet = triplet);
auto buildpath = paths.buildtrees() / FileDetectCompiler;
std::vector<CMakeVariable> cmake_args{
{CMakeVariableCurrentPortDir, paths.scripts / FileDetectCompiler},
{CMakeVariableCurrentBuildtreesDir, buildpath},
{CMakeVariableCurrentPackagesDir,
paths.packages() / fmt::format("{}_{}", FileDetectCompiler, triplet.canonical_name())},
// The detect_compiler "port" doesn't depend on the host triplet, so always natively compile
{CMakeVariableHostTriplet, triplet.canonical_name()},
};
get_generic_cmake_build_args(paths, triplet, toolset, cmake_args);
auto cmd = vcpkg::make_cmake_cmd(paths, paths.ports_cmake, std::move(cmake_args));
RedirectedProcessLaunchSettings settings;
settings.environment.emplace(paths.get_action_env(pre_build_info, toolset));
auto& fs = paths.get_filesystem();
fs.create_directory(buildpath, VCPKG_LINE_INFO);
auto stdoutlog = buildpath / ("stdout-" + triplet.canonical_name() + ".log");
CompilerInfo compiler_info;
std::string buf;
ExpectedL<int> rc = LocalizedString();
{
const auto out_file = fs.open_for_write(stdoutlog, VCPKG_LINE_INFO);
rc = cmd_execute_and_stream_lines(cmd, settings, [&](StringView s) {
if (Strings::starts_with(s, MarkerCompilerHash))
{
compiler_info.hash = s.substr(MarkerCompilerHash.size()).to_string();
}
if (Strings::starts_with(s, MarkerCompilerCxxVersion))
{
compiler_info.version = s.substr(MarkerCompilerCxxVersion.size()).to_string();
}
if (Strings::starts_with(s, MarkerCompilerCxxId))
{
compiler_info.id = s.substr(MarkerCompilerCxxId.size()).to_string();
}
static constexpr StringLiteral s_path_marker = "#COMPILER_CXX_PATH#";
if (Strings::starts_with(s, s_path_marker))
{
const auto compiler_cxx_path = s.substr(s_path_marker.size());
compiler_info.path.assign(compiler_cxx_path.data(), compiler_cxx_path.size());
}
Debug::println(s);
const auto old_buf_size = buf.size();
Strings::append(buf, s, '\n');
const auto write_size = buf.size() - old_buf_size;
Checks::msg_check_exit(VCPKG_LINE_INFO,
out_file.write(buf.c_str() + old_buf_size, 1, write_size) == write_size,
msgErrorWhileWriting,
msg::path = stdoutlog);
});
} // close out_file
if (compiler_info.hash.empty() || !succeeded(rc))
{
Debug::println("Compiler information tracking can be disabled by passing --",
SwitchFeatureFlags,
"=-",
FeatureFlagCompilertracking);
msg::println_error(msgErrorDetectingCompilerInfo, msg::path = stdoutlog);
msg::write_unlocalized_text(Color::none, buf);
Checks::msg_exit_with_error(VCPKG_LINE_INFO, msgErrorUnableToDetectCompilerInfo);
}
Debug::println("Detected compiler hash for triplet ", triplet, ": ", compiler_info.hash);
if (!compiler_info.path.empty())
{
msg::println(msgCompilerPath, msg::path = compiler_info.path);
}
return compiler_info;
}
static std::vector<CMakeVariable> get_cmake_build_args(const VcpkgCmdArguments& args,
const VcpkgPaths& paths,
Triplet host_triplet,
const BuildPackageOptions& build_options,
const InstallPlanAction& action)
{
auto& scfl = action.source_control_file_and_location.value_or_exit(VCPKG_LINE_INFO);
auto& scf = *scfl.source_control_file;
auto& port_name = scf.to_name();
std::string all_features;
for (auto& feature : scf.feature_paragraphs)
{
all_features.append(feature->name + ";");
}
auto& post_portfile_includes = action.pre_build_info(VCPKG_LINE_INFO).post_portfile_includes;
std::string all_post_portfile_includes =
Strings::join(";", Util::fmap(post_portfile_includes, [](const Path& p) { return p.generic_u8string(); }));
std::vector<CMakeVariable> variables{
{CMakeVariableAllFeatures, all_features},
{CMakeVariableCurrentPortDir, scfl.port_directory()},
{CMakeVariableHostTriplet, host_triplet.canonical_name()},
{CMakeVariableFeatures, Strings::join(";", action.feature_list)},
{CMakeVariablePort, port_name},
{CMakeVariableVersion, scf.to_version().text},
{CMakeVariableUseHeadVersion, Util::Enum::to_bool(action.use_head_version) ? "1" : "0"},
{CMakeVariableDownloadTool, to_string_view(build_options.download_tool)},
{CMakeVariableEditable, Util::Enum::to_bool(action.editable) ? "1" : "0"},
{CMakeVariableNoDownloads, !Util::Enum::to_bool(build_options.allow_downloads) ? "1" : "0"},
{CMakeVariableZChainloadToolchainFile, action.pre_build_info(VCPKG_LINE_INFO).toolchain_file()},
{CMakeVariableZPostPortfileIncludes, all_post_portfile_includes},
};
if (build_options.download_tool == DownloadTool::Aria2)
{
variables.emplace_back("ARIA2", paths.get_tool_exe(Tools::ARIA2, out_sink));
}
if (auto cmake_debug = args.cmake_debug.get())
{
if (cmake_debug->is_port_affected(port_name))
{
variables.emplace_back("--debugger");
variables.emplace_back(fmt::format("--debugger-pipe={}", cmake_debug->value));
}
}
if (auto cmake_configure_debug = args.cmake_configure_debug.get())
{
if (cmake_configure_debug->is_port_affected(port_name))
{
variables.emplace_back(fmt::format("-DVCPKG_CMAKE_CONFIGURE_OPTIONS=--debugger;--debugger-pipe={}",
cmake_configure_debug->value));
}
}
for (const auto& cmake_arg : args.cmake_args)
{
variables.emplace_back(cmake_arg);
}
if (build_options.backcompat_features == BackcompatFeatures::Prohibit)
{
variables.emplace_back(CMakeVariableProhibitBackcompatFeatures, "1");
}
get_generic_cmake_build_args(
paths,
action.spec.triplet(),
action.abi_info.value_or_exit(VCPKG_LINE_INFO).toolset.value_or_exit(VCPKG_LINE_INFO),
variables);
if (Util::Enum::to_bool(build_options.only_downloads))
{
variables.emplace_back(CMakeVariableDownloadMode, "true");
}
const ReadOnlyFilesystem& fs = paths.get_filesystem();
std::vector<std::string> port_configs;
for (const PackageSpec& dependency : action.package_dependencies)
{
Path port_config_path = paths.installed().vcpkg_port_config_cmake(dependency);
if (fs.is_regular_file(port_config_path))
{
port_configs.emplace_back(std::move(port_config_path).native());
}
}
if (!port_configs.empty())
{
variables.emplace_back(CMakeVariablePortConfigs, Strings::join(";", port_configs));
}
return variables;
}
bool PreBuildInfo::using_vcvars() const
{
return (!external_toolchain_file.has_value() || load_vcvars_env) &&
(cmake_system_name.empty() || cmake_system_name == "WindowsStore");
}
Path PreBuildInfo::toolchain_file() const
{
if (auto p = external_toolchain_file.get())
{
return *p;
}
else if (cmake_system_name == "Linux")
{
return m_paths.scripts / "toolchains/linux.cmake";
}
else if (cmake_system_name == "Darwin")
{
return m_paths.scripts / "toolchains/osx.cmake";
}
else if (cmake_system_name == "FreeBSD")
{
return m_paths.scripts / "toolchains/freebsd.cmake";
}
else if (cmake_system_name == "OpenBSD")
{
return m_paths.scripts / "toolchains/openbsd.cmake";
}
else if (cmake_system_name == "Android")
{
return m_paths.scripts / "toolchains/android.cmake";
}
else if (cmake_system_name == "iOS")
{
return m_paths.scripts / "toolchains/ios.cmake";
}
else if (cmake_system_name == "MinGW")
{
return m_paths.scripts / "toolchains/mingw.cmake";
}
else if (cmake_system_name == "WindowsStore")
{
return m_paths.scripts / "toolchains/uwp.cmake";
}
else if (target_is_xbox)
{
return m_paths.scripts / "toolchains/xbox.cmake";
}
else if (cmake_system_name.empty() || cmake_system_name == "Windows")
{
return m_paths.scripts / "toolchains/windows.cmake";
}
else
{
Checks::msg_exit_maybe_upgrade(VCPKG_LINE_INFO,
msgUndeterminedToolChainForTriplet,
msg::triplet = triplet,
msg::system_name = cmake_system_name);
}
}
static void write_sbom(const VcpkgPaths& paths,
const InstallPlanAction& action,
std::vector<Json::Value> heuristic_resources)
{
auto& fs = paths.get_filesystem();
const auto& scfl = action.source_control_file_and_location.value_or_exit(VCPKG_LINE_INFO);
const auto& scf = *scfl.source_control_file;
auto doc_ns = Strings::concat("https://spdx.org/spdxdocs/",
scf.to_name(),
'-',
action.spec.triplet(),
'-',
scf.to_version(),
'-',
generate_random_UUID());
const auto now = CTime::now_string();
const auto& abi = action.abi_info.value_or_exit(VCPKG_LINE_INFO);
const auto json_path =
action.package_dir.value_or_exit(VCPKG_LINE_INFO) / "share" / action.spec.name() / "vcpkg.spdx.json";
fs.write_contents_and_dirs(
json_path,
create_spdx_sbom(
action, abi.relative_port_files, abi.relative_port_hashes, now, doc_ns, std::move(heuristic_resources)),
VCPKG_LINE_INFO);
}
static ExtendedBuildResult do_build_package(const VcpkgCmdArguments& args,
const VcpkgPaths& paths,
Triplet host_triplet,
const BuildPackageOptions& build_options,
const InstallPlanAction& action,
bool all_dependencies_satisfied)
{
const auto& pre_build_info = action.pre_build_info(VCPKG_LINE_INFO);
auto& fs = paths.get_filesystem();
auto&& scfl = action.source_control_file_and_location.value_or_exit(VCPKG_LINE_INFO);
Triplet triplet = action.spec.triplet();
const auto& triplet_db = paths.get_triplet_db();
const auto& triplet_file_path = triplet_db.get_triplet_file_path(triplet);
if (Strings::starts_with(triplet_file_path, triplet_db.community_triplet_directory))
{
msg::print(LocalizedString::from_raw(triplet_file_path)
.append_raw(": ")
.append_raw(InfoPrefix)
.append(msgLoadedCommunityTriplet)
.append_raw('\n'));
}
else if (!Strings::starts_with(triplet_file_path, triplet_db.default_triplet_directory))
{
msg::print(LocalizedString::from_raw(triplet_file_path)
.append_raw(": ")
.append_raw(InfoPrefix)
.append(msgLoadedOverlayTriplet)
.append_raw('\n'));
}
if (!Strings::starts_with(scfl.control_path, paths.builtin_ports_directory()))
{
msg::print(LocalizedString::from_raw(scfl.port_directory())
.append_raw(": ")
.append_raw(InfoPrefix)
.append(msgInstallingOverlayPort)
.append_raw('\n'));
}
const auto& abi_info = action.abi_info.value_or_exit(VCPKG_LINE_INFO);
const ElapsedTimer timer;
auto cmd = vcpkg::make_cmake_cmd(
paths, paths.ports_cmake, get_cmake_build_args(args, paths, host_triplet, build_options, action));