-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
ToolChains.cpp
1553 lines (1342 loc) · 60.5 KB
/
ToolChains.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
//===--- ToolChains.cpp - Job invocations (general and per-platform) ------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#include "ToolChains.h"
#include "swift/AST/DiagnosticsDriver.h"
#include "swift/Basic/Dwarf.h"
#include "swift/Basic/LLVM.h"
#include "swift/Basic/Platform.h"
#include "swift/Basic/Range.h"
#include "swift/Basic/STLExtras.h"
#include "swift/Basic/TaskQueue.h"
#include "swift/Config.h"
#include "swift/Driver/Compilation.h"
#include "swift/Driver/Driver.h"
#include "swift/Driver/Job.h"
#include "swift/Frontend/Frontend.h"
#include "swift/Option/Options.h"
#include "clang/Basic/Version.h"
#include "clang/Driver/Util.h"
#include "llvm/ADT/DenseSet.h"
#include "llvm/ADT/StringSwitch.h"
#include "llvm/Option/Arg.h"
#include "llvm/Option/ArgList.h"
#include "llvm/ProfileData/InstrProf.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/Process.h"
#include "llvm/Support/Program.h"
using namespace swift;
using namespace swift::driver;
using namespace llvm::opt;
bool ToolChain::JobContext::shouldUseInputFileList() const {
return getTopLevelInputFiles().size() > C.getFilelistThreshold();
}
bool ToolChain::JobContext::shouldUsePrimaryInputFileListInFrontendInvocation()
const {
return InputActions.size() > C.getFilelistThreshold();
}
bool ToolChain::JobContext::shouldUseMainOutputFileListInFrontendInvocation()
const {
return Output.getPrimaryOutputFilenames().size() > C.getFilelistThreshold();
}
bool ToolChain::JobContext::
shouldUseSupplementaryOutputFileMapInFrontendInvocation() const {
static const unsigned UpperBoundOnSupplementaryOutputFileTypes =
file_types::TY_INVALID;
return InputActions.size() * UpperBoundOnSupplementaryOutputFileTypes >
C.getFilelistThreshold();
}
bool ToolChain::JobContext::shouldFilterFrontendInputsByType() const {
// FIXME: SingleCompile has not filtered its inputs in the past and now people
// rely upon that. But we would like the compilation modes to be consistent.
return OI.CompilerMode != OutputInfo::Mode::SingleCompile;
}
void ToolChain::addInputsOfType(ArgStringList &Arguments,
ArrayRef<const Action *> Inputs,
file_types::ID InputType,
const char *PrefixArgument) const {
for (auto &Input : Inputs) {
if (Input->getType() != InputType)
continue;
if (PrefixArgument)
Arguments.push_back(PrefixArgument);
Arguments.push_back(cast<InputAction>(Input)->getInputArg().getValue());
}
}
void ToolChain::addInputsOfType(ArgStringList &Arguments,
ArrayRef<const Job *> Jobs,
const llvm::opt::ArgList &Args,
file_types::ID InputType,
const char *PrefixArgument) const {
for (const Job *Cmd : Jobs) {
auto output = Cmd->getOutput().getAnyOutputForType(InputType);
if (!output.empty()) {
if (PrefixArgument)
Arguments.push_back(PrefixArgument);
Arguments.push_back(Args.MakeArgString(output));
}
}
}
void ToolChain::addPrimaryInputsOfType(ArgStringList &Arguments,
ArrayRef<const Job *> Jobs,
const llvm::opt::ArgList &Args,
file_types::ID InputType,
const char *PrefixArgument) const {
for (const Job *Cmd : Jobs) {
auto &outputInfo = Cmd->getOutput();
if (outputInfo.getPrimaryOutputType() == InputType) {
for (auto Output : outputInfo.getPrimaryOutputFilenames()) {
if (PrefixArgument)
Arguments.push_back(PrefixArgument);
Arguments.push_back(Args.MakeArgString(Output));
}
}
}
}
static bool addOutputsOfType(ArgStringList &Arguments,
CommandOutput const &Output,
const llvm::opt::ArgList &Args,
file_types::ID OutputType,
const char *PrefixArgument = nullptr) {
bool Added = false;
for (auto Output : Output.getAdditionalOutputsForType(OutputType)) {
assert(!Output.empty());
if (PrefixArgument)
Arguments.push_back(PrefixArgument);
Arguments.push_back(Args.MakeArgString(Output));
Added = true;
}
return Added;
}
static void addLTOArgs(const OutputInfo &OI, ArgStringList &arguments) {
switch (OI.LTOVariant) {
case OutputInfo::LTOKind::None:
break;
case OutputInfo::LTOKind::LLVMThin:
arguments.push_back("-lto=llvm-thin");
break;
case OutputInfo::LTOKind::LLVMFull:
arguments.push_back("-lto=llvm-full");
break;
}
}
void ToolChain::addCommonFrontendArgs(const OutputInfo &OI,
const CommandOutput &output,
const ArgList &inputArgs,
ArgStringList &arguments) const {
// Only pass -target to the REPL or immediate modes if it was explicitly
// specified on the command line.
switch (OI.CompilerMode) {
case OutputInfo::Mode::REPL:
case OutputInfo::Mode::Immediate:
if (!inputArgs.hasArg(options::OPT_target))
break;
LLVM_FALLTHROUGH;
case OutputInfo::Mode::StandardCompile:
case OutputInfo::Mode::SingleCompile:
case OutputInfo::Mode::BatchModeCompile:
arguments.push_back("-target");
arguments.push_back(inputArgs.MakeArgString(Triple.str()));
break;
}
if (const Arg *variant = inputArgs.getLastArg(options::OPT_target_variant)) {
arguments.push_back("-target-variant");
std::string normalized = llvm::Triple::normalize(variant->getValue());
arguments.push_back(inputArgs.MakeArgString(normalized));
}
// Enable address top-byte ignored in the ARM64 backend.
if (Triple.getArch() == llvm::Triple::aarch64 ||
Triple.getArch() == llvm::Triple::aarch64_32) {
arguments.push_back("-Xllvm");
arguments.push_back("-aarch64-use-tbi");
}
if (output.getPrimaryOutputType() == file_types::TY_SwiftModuleFile) {
arguments.push_back("-warn-on-potentially-unavailable-enum-case");
}
// Enable or disable ObjC interop appropriately for the platform
if (Triple.isOSDarwin()) {
arguments.push_back("-enable-objc-interop");
} else {
arguments.push_back("-disable-objc-interop");
}
// Add flags for C++ interop.
if (const Arg *arg =
inputArgs.getLastArg(options::OPT_experimental_cxx_stdlib)) {
arguments.push_back("-Xcc");
arguments.push_back(
inputArgs.MakeArgString(Twine("-stdlib=") + arg->getValue()));
}
if (inputArgs.hasArg(options::OPT_experimental_hermetic_seal_at_link)) {
arguments.push_back("-enable-llvm-vfe");
arguments.push_back("-enable-llvm-wme");
arguments.push_back("-conditional-runtime-records");
arguments.push_back("-internalize-at-link");
}
// Handle the CPU and its preferences.
inputArgs.AddLastArg(arguments, options::OPT_target_cpu);
if (!OI.SDKPath.empty()) {
arguments.push_back("-sdk");
arguments.push_back(inputArgs.MakeArgString(OI.SDKPath));
}
if (const Arg *A = inputArgs.getLastArg(options::OPT_windows_sdk_root)) {
arguments.push_back("-windows-sdk-root");
arguments.push_back(inputArgs.MakeArgString(A->getValue()));
}
if (const Arg *A = inputArgs.getLastArg(options::OPT_windows_sdk_version)) {
arguments.push_back("-windows-sdk-version");
arguments.push_back(inputArgs.MakeArgString(A->getValue()));
}
if (const Arg *A = inputArgs.getLastArg(options::OPT_visualc_tools_root)) {
arguments.push_back("-visualc-tools-root");
arguments.push_back(inputArgs.MakeArgString(A->getValue()));
}
if (const Arg *A = inputArgs.getLastArg(options::OPT_visualc_tools_version)) {
arguments.push_back("-visualc-tools-version");
arguments.push_back(inputArgs.MakeArgString(A->getValue()));
}
if (llvm::sys::Process::StandardErrHasColors()) {
arguments.push_back("-color-diagnostics");
}
inputArgs.AddAllArgs(arguments, options::OPT_I);
inputArgs.AddAllArgs(arguments, options::OPT_F, options::OPT_Fsystem);
inputArgs.AddAllArgs(arguments, options::OPT_vfsoverlay);
inputArgs.AddAllArgs(arguments, options::OPT_plugin_path);
inputArgs.AddAllArgs(arguments, options::OPT_external_plugin_path);
inputArgs.AddLastArg(arguments, options::OPT_AssertConfig);
inputArgs.AddLastArg(arguments, options::OPT_autolink_force_load);
inputArgs.AddLastArg(arguments,
options::OPT_color_diagnostics,
options::OPT_no_color_diagnostics);
inputArgs.AddLastArg(arguments, options::OPT_fixit_all);
inputArgs.AddLastArg(arguments,
options::OPT_warn_swift3_objc_inference_minimal,
options::OPT_warn_swift3_objc_inference_complete);
inputArgs.AddLastArg(arguments,
options::OPT_enable_actor_data_race_checks,
options::OPT_disable_actor_data_race_checks);
inputArgs.AddLastArg(arguments, options::OPT_warn_concurrency);
inputArgs.AddLastArg(arguments, options::OPT_strict_concurrency);
inputArgs.AddAllArgs(arguments, options::OPT_enable_experimental_feature);
inputArgs.AddAllArgs(arguments, options::OPT_enable_upcoming_feature);
inputArgs.AddLastArg(arguments, options::OPT_warn_implicit_overrides);
inputArgs.AddLastArg(arguments, options::OPT_typo_correction_limit);
inputArgs.AddLastArg(arguments, options::OPT_enable_app_extension);
inputArgs.AddLastArg(arguments, options::OPT_enable_library_evolution);
inputArgs.AddLastArg(arguments, options::OPT_require_explicit_availability);
inputArgs.AddLastArg(arguments, options::OPT_require_explicit_availability_target);
inputArgs.AddLastArg(arguments, options::OPT_require_explicit_availability_EQ);
inputArgs.AddLastArg(arguments, options::OPT_require_explicit_sendable);
inputArgs.AddLastArg(arguments, options::OPT_check_api_availability_only);
inputArgs.AddLastArg(arguments, options::OPT_enable_testing);
inputArgs.AddLastArg(arguments, options::OPT_enable_private_imports);
inputArgs.AddLastArg(arguments, options::OPT_g_Group);
inputArgs.AddLastArg(arguments, options::OPT_debug_info_format);
inputArgs.AddLastArg(arguments, options::OPT_import_underlying_module);
inputArgs.AddLastArg(arguments, options::OPT_module_cache_path);
inputArgs.AddLastArg(arguments, options::OPT_module_link_name);
inputArgs.AddLastArg(arguments, options::OPT_module_abi_name);
inputArgs.AddLastArg(arguments, options::OPT_package_name);
inputArgs.AddLastArg(arguments, options::OPT_export_as);
inputArgs.AddLastArg(arguments, options::OPT_nostdimport);
inputArgs.AddLastArg(arguments, options::OPT_parse_stdlib);
inputArgs.AddLastArg(arguments, options::OPT_resource_dir);
inputArgs.AddLastArg(arguments, options::OPT_solver_memory_threshold);
inputArgs.AddLastArg(arguments, options::OPT_value_recursion_threshold);
inputArgs.AddLastArg(arguments, options::OPT_warn_swift3_objc_inference);
inputArgs.AddLastArg(arguments, options::OPT_Rpass_EQ);
inputArgs.AddLastArg(arguments, options::OPT_Rpass_missed_EQ);
inputArgs.AddLastArg(arguments, options::OPT_suppress_warnings);
inputArgs.AddLastArg(arguments, options::OPT_suppress_remarks);
inputArgs.AddLastArg(arguments, options::OPT_profile_generate);
inputArgs.AddLastArg(arguments, options::OPT_profile_use);
inputArgs.AddLastArg(arguments, options::OPT_profile_coverage_mapping);
inputArgs.AddAllArgs(arguments, options::OPT_warnings_as_errors,
options::OPT_no_warnings_as_errors);
inputArgs.AddLastArg(arguments, options::OPT_sanitize_EQ);
inputArgs.AddLastArg(arguments, options::OPT_sanitize_recover_EQ);
inputArgs.AddLastArg(arguments,
options::OPT_sanitize_address_use_odr_indicator);
inputArgs.AddLastArg(arguments, options::OPT_sanitize_coverage_EQ);
inputArgs.AddLastArg(arguments, options::OPT_static);
inputArgs.AddLastArg(arguments, options::OPT_swift_version);
inputArgs.AddLastArg(arguments, options::OPT_enforce_exclusivity_EQ);
inputArgs.AddLastArg(arguments, options::OPT_stats_output_dir);
inputArgs.AddLastArg(arguments, options::OPT_tools_directory);
inputArgs.AddLastArg(arguments, options::OPT_trace_stats_events);
inputArgs.AddLastArg(arguments, options::OPT_profile_stats_events);
inputArgs.AddLastArg(arguments, options::OPT_profile_stats_entities);
inputArgs.AddLastArg(arguments,
options::OPT_solver_shrink_unsolved_threshold);
inputArgs.AddLastArg(arguments, options::OPT_O_Group);
inputArgs.AddLastArg(arguments, options::OPT_RemoveRuntimeAsserts);
inputArgs.AddLastArg(arguments, options::OPT_AssumeSingleThreaded);
inputArgs.AddLastArg(arguments,
options::OPT_emit_fine_grained_dependency_sourcefile_dot_files);
inputArgs.AddLastArg(arguments, options::OPT_package_description_version);
inputArgs.AddLastArg(arguments, options::OPT_locale);
inputArgs.AddLastArg(arguments, options::OPT_localization_path);
inputArgs.AddLastArg(arguments, options::OPT_serialize_diagnostics_path);
inputArgs.AddLastArg(arguments, options::OPT_debug_diagnostic_names);
inputArgs.AddLastArg(arguments, options::OPT_print_educational_notes);
inputArgs.AddLastArg(arguments, options::OPT_diagnostic_style);
inputArgs.AddLastArg(arguments,
options::OPT_enable_experimental_concise_pound_file);
inputArgs.AddLastArg(arguments,
options::OPT_verify_incremental_dependencies);
inputArgs.AddLastArg(arguments, options::OPT_access_notes_path);
inputArgs.AddLastArg(arguments, options::OPT_library_level);
inputArgs.AddLastArg(arguments, options::OPT_enable_bare_slash_regex);
inputArgs.AddLastArg(arguments, options::OPT_enable_experimental_cxx_interop);
inputArgs.AddLastArg(arguments, options::OPT_cxx_interoperability_mode);
inputArgs.AddLastArg(arguments, options::OPT_load_plugin_library);
inputArgs.AddLastArg(arguments, options::OPT_load_plugin_executable);
inputArgs.AddLastArg(arguments, options::OPT_enable_builtin_module);
// Pass on any build config options
inputArgs.AddAllArgs(arguments, options::OPT_D);
// Pass on file paths that should be remapped in debug info.
inputArgs.AddAllArgs(arguments, options::OPT_debug_prefix_map,
options::OPT_coverage_prefix_map,
options::OPT_file_prefix_map);
std::string globalRemapping = getGlobalDebugPathRemapping();
if (!globalRemapping.empty()) {
arguments.push_back("-debug-prefix-map");
arguments.push_back(inputArgs.MakeArgString(globalRemapping));
}
// Pass through the values passed to -Xfrontend.
inputArgs.AddAllArgValues(arguments, options::OPT_Xfrontend);
// Pass on module names whose symbols should be embedded in tbd.
inputArgs.AddAllArgs(arguments, options::OPT_embed_tbd_for_module);
if (auto *A = inputArgs.getLastArg(options::OPT_working_directory)) {
// Add -Xcc -working-directory before any other -Xcc options to ensure it is
// overridden by an explicit -Xcc -working-directory, although having a
// different working directory is probably incorrect.
SmallString<128> workingDirectory(A->getValue());
llvm::sys::fs::make_absolute(workingDirectory);
arguments.push_back("-Xcc");
arguments.push_back("-working-directory");
arguments.push_back("-Xcc");
arguments.push_back(inputArgs.MakeArgString(workingDirectory));
}
addLTOArgs(OI, arguments);
// -g implies -enable-anonymous-context-mangled-names, because the extra
// metadata aids debugging.
if (inputArgs.hasArg(options::OPT_g)) {
// But don't add the option in optimized builds: it would prevent dead code
// stripping of unused metadata.
auto OptArg = inputArgs.getLastArgNoClaim(options::OPT_O_Group);
if (!OptArg || OptArg->getOption().matches(options::OPT_Onone))
arguments.push_back("-enable-anonymous-context-mangled-names");
// TODO: Should we support -fcoverage-compilation-dir?
inputArgs.AddAllArgs(arguments, options::OPT_file_compilation_dir);
}
// Add plugin path options.
inputArgs.AddAllArgs(arguments, options::OPT_plugin_path);
{
SmallString<64> pluginPath;
auto programPath = getDriver().getSwiftProgramPath();
CompilerInvocation::computeRuntimeResourcePathFromExecutablePath(
programPath, /*shared=*/true, pluginPath);
auto defaultPluginPath = pluginPath;
llvm::sys::path::append(defaultPluginPath, "host", "plugins");
// Default plugin path.
arguments.push_back("-plugin-path");
arguments.push_back(inputArgs.MakeArgString(defaultPluginPath));
// Local plugin path.
llvm::sys::path::remove_filename(pluginPath); // Remove "swift"
llvm::sys::path::remove_filename(pluginPath); // Remove "lib"
llvm::sys::path::append(pluginPath, "local", "lib");
llvm::sys::path::append(pluginPath, "swift");
llvm::sys::path::append(pluginPath, "host", "plugins");
arguments.push_back("-plugin-path");
arguments.push_back(inputArgs.MakeArgString(pluginPath));
}
// Pass through any subsystem flags.
inputArgs.AddAllArgs(arguments, options::OPT_Xllvm);
inputArgs.AddAllArgs(arguments, options::OPT_Xcc);
}
static void addRuntimeLibraryFlags(const OutputInfo &OI,
ArgStringList &Arguments) {
if (!OI.RuntimeVariant)
return;
const OutputInfo::MSVCRuntime RT = OI.RuntimeVariant.value();
Arguments.push_back("-autolink-library");
Arguments.push_back("oldnames");
Arguments.push_back("-autolink-library");
switch (RT) {
case OutputInfo::MSVCRuntime::MultiThreaded:
Arguments.push_back("libcmt");
break;
case OutputInfo::MSVCRuntime::MultiThreadedDebug:
Arguments.push_back("libcmtd");
break;
case OutputInfo::MSVCRuntime::MultiThreadedDLL:
Arguments.push_back("msvcrt");
break;
case OutputInfo::MSVCRuntime::MultiThreadedDebugDLL:
Arguments.push_back("msvcrtd");
break;
}
// NOTE(compnerd) we do not support /ML and /MLd
Arguments.push_back("-Xcc");
Arguments.push_back("-D_MT");
if (RT == OutputInfo::MSVCRuntime::MultiThreadedDLL ||
RT == OutputInfo::MSVCRuntime::MultiThreadedDebugDLL) {
Arguments.push_back("-Xcc");
Arguments.push_back("-D_DLL");
}
}
ToolChain::InvocationInfo
ToolChain::constructInvocation(const CompileJobAction &job,
const JobContext &context) const {
InvocationInfo II{SWIFT_EXECUTABLE_NAME};
ArgStringList &Arguments = II.Arguments;
II.allowsResponseFiles = true;
for (auto &s : getDriver().getSwiftProgramArgs())
Arguments.push_back(s.c_str());
Arguments.push_back("-frontend");
{
// Determine the frontend mode option.
const char *FrontendModeOption = context.computeFrontendModeForCompile();
assert(FrontendModeOption != nullptr &&
"No frontend mode option specified!");
Arguments.push_back(FrontendModeOption);
}
context.addFrontendInputAndOutputArguments(Arguments, II.FilelistInfos);
// Forward migrator flags.
if (auto DataPath =
context.Args.getLastArg(options::OPT_api_diff_data_file)) {
Arguments.push_back("-api-diff-data-file");
Arguments.push_back(DataPath->getValue());
}
if (auto DataDir = context.Args.getLastArg(options::OPT_api_diff_data_dir)) {
Arguments.push_back("-api-diff-data-dir");
Arguments.push_back(DataDir->getValue());
}
if (context.Args.hasArg(options::OPT_dump_usr)) {
Arguments.push_back("-dump-usr");
}
if (context.Args.hasArg(options::OPT_parse_stdlib))
Arguments.push_back("-disable-objc-attr-requires-foundation-module");
addCommonFrontendArgs(context.OI, context.Output, context.Args, Arguments);
addRuntimeLibraryFlags(context.OI, Arguments);
// Pass along an -import-objc-header arg, replacing the argument with the name
// of any input PCH to the current action if one is present.
if (context.Args.hasArgNoClaim(options::OPT_import_objc_header)) {
bool ForwardAsIs = true;
bool bridgingPCHIsEnabled =
context.Args.hasFlag(options::OPT_enable_bridging_pch,
options::OPT_disable_bridging_pch, true);
bool usePersistentPCH = bridgingPCHIsEnabled &&
context.Args.hasArg(options::OPT_pch_output_dir);
if (!usePersistentPCH) {
for (auto *IJ : context.Inputs) {
if (!IJ->getOutput().getAnyOutputForType(file_types::TY_PCH).empty()) {
Arguments.push_back("-import-objc-header");
addInputsOfType(Arguments, context.Inputs, context.Args,
file_types::TY_PCH);
ForwardAsIs = false;
break;
}
}
}
if (ForwardAsIs) {
context.Args.AddLastArg(Arguments, options::OPT_import_objc_header);
}
if (usePersistentPCH) {
context.Args.AddLastArg(Arguments, options::OPT_pch_output_dir);
switch (context.OI.CompilerMode) {
case OutputInfo::Mode::StandardCompile:
case OutputInfo::Mode::BatchModeCompile:
// In the 'multiple invocations for each file' mode we don't need to
// validate the PCH every time, it has been validated with the initial
// -emit-pch invocation.
Arguments.push_back("-pch-disable-validation");
break;
case OutputInfo::Mode::Immediate:
case OutputInfo::Mode::REPL:
case OutputInfo::Mode::SingleCompile:
break;
}
}
}
if (context.Args.hasArg(options::OPT_parse_as_library) ||
context.Args.hasArg(options::OPT_emit_library))
Arguments.push_back("-parse-as-library");
context.Args.AddLastArg(Arguments, options::OPT_parse_sil);
Arguments.push_back("-module-name");
Arguments.push_back(context.Args.MakeArgString(context.OI.ModuleName));
if (context.Args.hasArg(options::OPT_CrossModuleOptimization)) {
Arguments.push_back("-cross-module-optimization");
}
if (context.Args.hasArg(options::OPT_ExperimentalPerformanceAnnotations)) {
Arguments.push_back("-experimental-performance-annotations");
}
file_types::ID remarksFileType = file_types::TY_YAMLOptRecord;
// If a specific format is specified for the remarks, forward that as is.
if (auto remarksFormat =
context.Args.getLastArg(options::OPT_save_optimization_record_EQ)) {
Arguments.push_back(context.Args.MakeArgString(
Twine("-save-optimization-record=") + remarksFormat->getValue()));
// If that's the case, add the proper output file for the type.
if (llvm::Expected<file_types::ID> fileType =
remarkFileTypeFromArgs(context.Args))
remarksFileType = *fileType;
else
consumeError(fileType.takeError()); // Don't report errors here. This will
// be reported later anyway.
}
addOutputsOfType(Arguments, context.Output, context.Args, remarksFileType,
"-save-optimization-record-path");
if (auto remarksFilter = context.Args.getLastArg(
options::OPT_save_optimization_record_passes)) {
Arguments.push_back("-save-optimization-record-passes");
Arguments.push_back(remarksFilter->getValue());
}
if (context.Args.hasArg(options::OPT_migrate_keep_objc_visibility)) {
Arguments.push_back("-migrate-keep-objc-visibility");
}
addOutputsOfType(Arguments, context.Output, context.Args,
file_types::TY_Remapping, "-emit-remap-file-path");
if (context.OI.numThreads > 0) {
Arguments.push_back("-num-threads");
Arguments.push_back(
context.Args.MakeArgString(Twine(context.OI.numThreads)));
}
// Add the output file argument if necessary.
if (context.Output.getPrimaryOutputType() != file_types::TY_Nothing) {
auto IndexUnitOutputs = context.Output.getIndexUnitOutputFilenames();
if (context.shouldUseMainOutputFileListInFrontendInvocation()) {
Arguments.push_back("-output-filelist");
Arguments.push_back(context.getTemporaryFilePath("outputs", ""));
II.FilelistInfos.push_back({Arguments.back(),
context.Output.getPrimaryOutputType(),
FilelistInfo::WhichFiles::Output});
if (!IndexUnitOutputs.empty()) {
Arguments.push_back("-index-unit-output-path-filelist");
Arguments.push_back(context.getTemporaryFilePath("index-unit-outputs",
""));
II.FilelistInfos.push_back({
Arguments.back(), file_types::TY_IndexUnitOutputPath,
FilelistInfo::WhichFiles::IndexUnitOutputPaths});
}
} else {
for (auto FileName : context.Output.getPrimaryOutputFilenames()) {
Arguments.push_back("-o");
Arguments.push_back(context.Args.MakeArgString(FileName));
}
for (auto FileName : IndexUnitOutputs) {
Arguments.push_back("-index-unit-output-path");
Arguments.push_back(context.Args.MakeArgString(FileName));
}
}
}
if (context.Args.hasArg(options::OPT_embed_bitcode_marker))
Arguments.push_back("-embed-bitcode-marker");
// For `-index-file` mode add `-disable-typo-correction`, since the errors
// will be ignored and it can be expensive to do typo-correction.
if (job.getType() == file_types::TY_IndexData) {
Arguments.push_back("-disable-typo-correction");
}
if (context.Args.hasArg(options::OPT_index_store_path)) {
context.Args.AddLastArg(Arguments, options::OPT_index_store_path);
if (!context.Args.hasArg(options::OPT_index_ignore_system_modules))
Arguments.push_back("-index-system-modules");
context.Args.AddLastArg(Arguments, options::OPT_index_ignore_clang_modules);
context.Args.AddLastArg(Arguments, options::OPT_index_include_locals);
}
if (context.Args.hasArg(options::OPT_debug_info_store_invocation) ||
shouldStoreInvocationInDebugInfo()) {
Arguments.push_back("-debug-info-store-invocation");
}
if (context.Args.hasArg(
options::OPT_disable_autolinking_runtime_compatibility)) {
Arguments.push_back("-disable-autolinking-runtime-compatibility");
}
if (auto arg = context.Args.getLastArg(
options::OPT_runtime_compatibility_version)) {
Arguments.push_back("-runtime-compatibility-version");
Arguments.push_back(arg->getValue());
}
if (context.Args.hasArg(options::OPT_track_system_dependencies)) {
Arguments.push_back("-track-system-dependencies");
}
if (context.Args.hasFlag(options::OPT_static_executable,
options::OPT_no_static_executable, false) ||
context.Args.hasFlag(options::OPT_static_stdlib,
options::OPT_no_static_stdlib, false)) {
Arguments.push_back("-use-static-resource-dir");
}
context.Args.AddLastArg(
Arguments,
options::
OPT_disable_autolinking_runtime_compatibility_dynamic_replacements);
context.Args.AddLastArg(
Arguments,
options::OPT_disable_autolinking_runtime_compatibility_concurrency);
if (context.OI.CompilerMode == OutputInfo::Mode::SingleCompile) {
context.Args.AddLastArg(Arguments, options::OPT_emit_symbol_graph);
context.Args.AddLastArg(Arguments, options::OPT_emit_symbol_graph_dir);
}
context.Args.AddLastArg(Arguments, options::OPT_include_spi_symbols);
context.Args.AddLastArg(Arguments, options::OPT_emit_extension_block_symbols,
options::OPT_omit_extension_block_symbols);
context.Args.AddLastArg(Arguments, options::OPT_symbol_graph_minimum_access_level);
return II;
}
const char *ToolChain::JobContext::computeFrontendModeForCompile() const {
switch (OI.CompilerMode) {
case OutputInfo::Mode::StandardCompile:
case OutputInfo::Mode::SingleCompile:
case OutputInfo::Mode::BatchModeCompile:
break;
case OutputInfo::Mode::Immediate:
case OutputInfo::Mode::REPL:
llvm_unreachable("REPL and immediate modes handled elsewhere");
}
switch (Output.getPrimaryOutputType()) {
case file_types::TY_Object:
return "-c";
case file_types::TY_PCH:
return "-emit-pch";
case file_types::TY_ASTDump:
return "-dump-ast";
case file_types::TY_RawSIL:
return "-emit-silgen";
case file_types::TY_SIL:
return "-emit-sil";
case file_types::TY_RawSIB:
return "-emit-sibgen";
case file_types::TY_SIB:
return "-emit-sib";
case file_types::TY_LLVM_IR:
return "-emit-ir";
case file_types::TY_LLVM_BC:
return "-emit-bc";
case file_types::TY_ClangModuleFile:
return "-emit-pcm";
case file_types::TY_Assembly:
return "-S";
case file_types::TY_SwiftModuleFile:
// Since this is our primary output, we need to specify the option here.
return "-emit-module";
case file_types::TY_ImportedModules:
return "-emit-imported-modules";
case file_types::TY_JSONDependencies:
return "-scan-dependencies";
case file_types::TY_JSONFeatures:
return "-emit-supported-features";
case file_types::TY_IndexData:
return "-typecheck";
case file_types::TY_Remapping:
return "-update-code";
case file_types::TY_Nothing:
// We were told to output nothing, so get the last mode option and use that.
if (const Arg *A = Args.getLastArg(options::OPT_modes_Group))
return A->getSpelling().data();
else
llvm_unreachable("We were told to perform a standard compile, "
"but no mode option was passed to the driver.");
case file_types::TY_Swift:
case file_types::TY_dSYM:
case file_types::TY_AutolinkFile:
case file_types::TY_Dependencies:
case file_types::TY_SwiftModuleDocFile:
case file_types::TY_SerializedDiagnostics:
case file_types::TY_ClangHeader:
case file_types::TY_Image:
case file_types::TY_SwiftDeps:
case file_types::TY_ExternalSwiftDeps:
case file_types::TY_ModuleTrace:
case file_types::TY_TBD:
case file_types::TY_YAMLOptRecord:
case file_types::TY_BitstreamOptRecord:
case file_types::TY_SwiftModuleInterfaceFile:
case file_types::TY_PrivateSwiftModuleInterfaceFile:
case file_types::TY_SwiftModuleSummaryFile:
case file_types::TY_SwiftSourceInfoFile:
case file_types::TY_SwiftCrossImportDir:
case file_types::TY_SwiftOverlayFile:
case file_types::TY_IndexUnitOutputPath:
case file_types::TY_SwiftABIDescriptor:
case file_types::TY_ConstValues:
llvm_unreachable("Output type can never be primary output.");
case file_types::TY_INVALID:
llvm_unreachable("Invalid type ID");
}
llvm_unreachable("unhandled output type");
}
void ToolChain::JobContext::addFrontendInputAndOutputArguments(
ArgStringList &Arguments, std::vector<FilelistInfo> &FilelistInfos) const {
switch (OI.CompilerMode) {
case OutputInfo::Mode::StandardCompile:
assert(InputActions.size() == 1 &&
"Standard-compile mode takes exactly one input (the primary file)");
break;
case OutputInfo::Mode::BatchModeCompile:
case OutputInfo::Mode::SingleCompile:
break;
case OutputInfo::Mode::Immediate:
case OutputInfo::Mode::REPL:
llvm_unreachable("REPL and immediate modes handled elsewhere");
}
const bool UseFileList = shouldUseInputFileList();
const bool MayHavePrimaryInputs = OI.mightHaveExplicitPrimaryInputs(Output);
const bool UsePrimaryFileList =
MayHavePrimaryInputs &&
shouldUsePrimaryInputFileListInFrontendInvocation();
const bool FilterInputsByType = shouldFilterFrontendInputsByType();
const bool UseSupplementaryOutputFileList =
shouldUseSupplementaryOutputFileMapInFrontendInvocation();
assert((C.getFilelistThreshold() != Compilation::NEVER_USE_FILELIST ||
!UseFileList && !UsePrimaryFileList &&
!UseSupplementaryOutputFileList) &&
"No filelists are used if FilelistThreshold=NEVER_USE_FILELIST");
if (UseFileList) {
Arguments.push_back("-filelist");
Arguments.push_back(getAllSourcesPath());
}
if (UsePrimaryFileList) {
Arguments.push_back("-primary-filelist");
Arguments.push_back(getTemporaryFilePath("primaryInputs", ""));
FilelistInfos.push_back({Arguments.back(), file_types::TY_Swift,
FilelistInfo::WhichFiles::SourceInputActions});
}
if (!UseFileList || !UsePrimaryFileList) {
addFrontendCommandLineInputArguments(MayHavePrimaryInputs, UseFileList,
UsePrimaryFileList, FilterInputsByType,
Arguments);
}
if (UseSupplementaryOutputFileList) {
Arguments.push_back("-supplementary-output-file-map");
Arguments.push_back(getTemporaryFilePath("supplementaryOutputs", ""));
FilelistInfos.push_back({Arguments.back(), file_types::TY_INVALID,
FilelistInfo::WhichFiles::SupplementaryOutput});
} else {
addFrontendSupplementaryOutputArguments(Arguments);
}
}
void ToolChain::JobContext::addFrontendCommandLineInputArguments(
const bool mayHavePrimaryInputs, const bool useFileList,
const bool usePrimaryFileList, const bool filterByType,
ArgStringList &arguments) const {
llvm::DenseSet<StringRef> primaries;
if (mayHavePrimaryInputs) {
for (const Action *A : InputActions) {
const auto *IA = cast<InputAction>(A);
const llvm::opt::Arg &InArg = IA->getInputArg();
primaries.insert(InArg.getValue());
}
}
// -index-file compilations are weird. They are processed as SingleCompiles
// (WMO), but must indicate that there is one primary file, designated by
// -index-file-path.
if (Arg *A = Args.getLastArg(options::OPT_index_file_path)) {
assert(primaries.empty() &&
"index file jobs should be treated as single (WMO) compiles");
primaries.insert(A->getValue());
}
for (auto inputPair : getTopLevelInputFiles()) {
if (filterByType && !file_types::isPartOfSwiftCompilation(inputPair.first))
continue;
const char *inputName = inputPair.second->getValue();
const bool isPrimary = primaries.count(inputName);
if (isPrimary && !usePrimaryFileList) {
arguments.push_back("-primary-file");
arguments.push_back(inputName);
}
if ((!isPrimary || usePrimaryFileList) && !useFileList)
arguments.push_back(inputName);
}
}
void ToolChain::JobContext::addFrontendSupplementaryOutputArguments(
ArgStringList &arguments) const {
// FIXME: Get these and other argument strings from the same place for both
// driver and frontend.
addOutputsOfType(arguments, Output, Args, file_types::ID::TY_SwiftModuleFile,
"-emit-module-path");
addOutputsOfType(arguments, Output, Args, file_types::TY_SwiftModuleDocFile,
"-emit-module-doc-path");
addOutputsOfType(arguments, Output, Args, file_types::TY_SwiftSourceInfoFile,
"-emit-module-source-info-path");
addOutputsOfType(arguments, Output, Args,
file_types::ID::TY_SwiftModuleInterfaceFile,
"-emit-module-interface-path");
addOutputsOfType(arguments, Output, Args,
file_types::ID::TY_PrivateSwiftModuleInterfaceFile,
"-emit-private-module-interface-path");
addOutputsOfType(arguments, Output, Args,
file_types::TY_SerializedDiagnostics,
"-serialize-diagnostics-path");
if (addOutputsOfType(arguments, Output, Args, file_types::ID::TY_ClangHeader,
"-emit-objc-header-path")) {
assert(OI.CompilerMode == OutputInfo::Mode::SingleCompile &&
"The Swift tool should only emit an Obj-C header in single compile"
"mode!");
}
addOutputsOfType(arguments, Output, Args, file_types::TY_Dependencies,
"-emit-dependencies-path");
addOutputsOfType(arguments, Output, Args, file_types::TY_SwiftDeps,
"-emit-reference-dependencies-path");
addOutputsOfType(arguments, Output, Args, file_types::TY_ModuleTrace,
"-emit-loaded-module-trace-path");
addOutputsOfType(arguments, Output, Args, file_types::TY_TBD,
"-emit-tbd-path");
addOutputsOfType(arguments, Output, Args,
file_types::TY_SwiftModuleSummaryFile,
"-emit-module-summary-path");
}
ToolChain::InvocationInfo
ToolChain::constructInvocation(const InterpretJobAction &job,
const JobContext &context) const {
assert(context.OI.CompilerMode == OutputInfo::Mode::Immediate);
InvocationInfo II{SWIFT_EXECUTABLE_NAME};
ArgStringList &Arguments = II.Arguments;
II.allowsResponseFiles = true;
for (auto &s : getDriver().getSwiftProgramArgs())
Arguments.push_back(s.c_str());
Arguments.push_back("-frontend");
Arguments.push_back("-interpret");
assert(context.Inputs.empty() &&
"The Swift frontend does not expect to be fed any input Jobs!");
for (const Action *A : context.InputActions) {
cast<InputAction>(A)->getInputArg().render(context.Args, Arguments);
}
if (context.Args.hasArg(options::OPT_parse_stdlib))
Arguments.push_back("-disable-objc-attr-requires-foundation-module");
addCommonFrontendArgs(context.OI, context.Output, context.Args, Arguments);
addRuntimeLibraryFlags(context.OI, Arguments);
context.Args.AddLastArg(Arguments, options::OPT_import_objc_header);
context.Args.AddLastArg(Arguments, options::OPT_parse_sil);
Arguments.push_back("-module-name");
Arguments.push_back(context.Args.MakeArgString(context.OI.ModuleName));
context.Args.AddAllArgs(Arguments, options::OPT_framework);
ToolChain::addLinkedLibArgs(context.Args, Arguments);
// The immediate arguments must be last.
context.Args.AddLastArg(Arguments, options::OPT__DASH_DASH);
return II;
}
ToolChain::InvocationInfo
ToolChain::constructInvocation(const BackendJobAction &job,
const JobContext &context) const {
assert(context.Args.hasArg(options::OPT_embed_bitcode));
ArgStringList Arguments;
for (auto &s : getDriver().getSwiftProgramArgs())
Arguments.push_back(s.c_str());
Arguments.push_back("-frontend");
// Determine the frontend mode option.
const char *FrontendModeOption = nullptr;
switch (context.OI.CompilerMode) {
case OutputInfo::Mode::StandardCompile:
case OutputInfo::Mode::SingleCompile: {
switch (context.Output.getPrimaryOutputType()) {
case file_types::TY_Object:
FrontendModeOption = "-c";
break;
case file_types::TY_LLVM_IR:
FrontendModeOption = "-emit-ir";
break;
case file_types::TY_LLVM_BC:
FrontendModeOption = "-emit-bc";
break;
case file_types::TY_Assembly:
FrontendModeOption = "-S";
break;
case file_types::TY_Nothing:
// We were told to output nothing, so get the last mode option and use
// that.
if (const Arg *A = context.Args.getLastArg(options::OPT_modes_Group))
FrontendModeOption = A->getSpelling().data();
else
llvm_unreachable("We were told to perform a standard compile, "
"but no mode option was passed to the driver.");
break;
case file_types::TY_ImportedModules:
case file_types::TY_TBD:
case file_types::TY_SwiftModuleFile:
case file_types::TY_ASTDump:
case file_types::TY_RawSIL:
case file_types::TY_RawSIB:
case file_types::TY_SIL:
case file_types::TY_SIB:
case file_types::TY_PCH:
case file_types::TY_ClangModuleFile:
case file_types::TY_IndexData:
case file_types::TY_JSONDependencies:
case file_types::TY_JSONFeatures:
llvm_unreachable("Cannot be output from backend job");
case file_types::TY_Swift:
case file_types::TY_dSYM:
case file_types::TY_AutolinkFile:
case file_types::TY_Dependencies:
case file_types::TY_SwiftModuleDocFile:
case file_types::TY_SerializedDiagnostics:
case file_types::TY_ClangHeader: