-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
1856 lines (1715 loc) · 67 KB
/
Copy pathmain.cpp
File metadata and controls
1856 lines (1715 loc) · 67 KB
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 <filesystem>
#include <fstream>
#include <iostream>
#include <memory>
#include <optional>
#include <sstream>
#include <string>
#include <string_view>
#include <vector>
#include <llvm/ADT/SmallString.h>
#include <llvm/Support/FileSystem.h>
#include <llvm/Support/Program.h>
#include "air/passes.h"
#include "air/print.h"
#include "air/verifier.h"
#include "assembler/assembler.h"
#include "backend/backend.h"
#include "cir/examples.h"
#include "cir2air/cir2air.h"
#include "cir2llvm/cir2llvm.h"
#include "driver/options.h"
#include "libaburi/frontend.h"
#include "libaburi/modules/scan.h"
#include "libaburi/modules/session.h"
#include "libaburi/serialize/bmi.h"
#include "perf_stats.h"
#include "source_mgnt.h"
namespace {
using aburi::driver::DriverOptions;
bool is_stdin_input(const std::string& path) {
return path == "-";
}
std::string read_file(const std::string& path) {
if (is_stdin_input(path)) {
std::ostringstream ss;
ss << std::cin.rdbuf();
return ss.str();
}
std::ifstream in(path, std::ios::binary);
if (!in) {
throw std::runtime_error("could not open input file '" + path + "'");
}
std::ostringstream ss;
ss << in.rdbuf();
return ss.str();
}
bool has_cpp_extension(const std::string& path) {
auto ends_with = [&](std::string_view suffix) {
return path.size() >= suffix.size() &&
path.compare(path.size() - suffix.size(), suffix.size(), suffix) == 0;
};
return ends_with(".cc") || ends_with(".cpp") || ends_with(".cxx") ||
ends_with(".c++") || ends_with(".C") || ends_with(".hpp") ||
ends_with(".cppm") || ends_with(".ixx") || ends_with(".mpp");
}
bool has_cpp_module_extension(const std::string& path) {
auto ends_with = [&](std::string_view suffix) {
return path.size() >= suffix.size() &&
path.compare(path.size() - suffix.size(), suffix.size(), suffix) == 0;
};
return ends_with(".cppm") || ends_with(".ixx") || ends_with(".mpp");
}
bool has_objc_extension(const std::string& path) {
auto ends_with = [&](std::string_view suffix) {
return path.size() >= suffix.size() &&
path.compare(path.size() - suffix.size(), suffix.size(), suffix) == 0;
};
return ends_with(".m") || ends_with(".mm") || ends_with(".M");
}
LangOptions options_for_file(LangOptions opts, const std::string& path) {
if (opts.language_mode == LanguageMode::Auto) {
auto ends_with = [&](std::string_view suffix) {
return path.size() >= suffix.size() &&
path.compare(path.size() - suffix.size(), suffix.size(), suffix) == 0;
};
if (has_objc_extension(path)) {
opts.language_mode = ends_with(".m") ? LanguageMode::C
: LanguageMode::CXX;
opts.objc = true;
} else {
opts.language_mode = has_cpp_extension(path) ? LanguageMode::CXX
: LanguageMode::C;
}
}
if (opts.standard.empty() && opts.language_mode == LanguageMode::CXX &&
has_cpp_module_extension(path)) {
opts.set_standard("c++20");
}
return opts;
}
std::string to_lower_ascii(std::string value) {
for (char& c : value) {
if (c >= 'A' && c <= 'Z') {
c = static_cast<char>(c - 'A' + 'a');
}
}
return value;
}
std::string input_extension(const std::filesystem::path& input_path) {
return to_lower_ascii(input_path.extension().string());
}
bool is_cpp_source_extension(const std::string& ext) {
return ext == ".cc" || ext == ".cpp" || ext == ".cxx" ||
ext == ".c++" || ext == ".cp" || ext == ".cppm" ||
ext == ".ixx" || ext == ".mpp";
}
bool is_linker_input_extension(const std::string& ext) {
return ext == ".o" || ext == ".a" || ext == ".so" ||
ext == ".dylib" || ext == ".tbd" || ext == ".lo";
}
bool is_passthrough_source_extension(const DriverOptions& opts, const std::string& ext) {
if (ext == ".m" || ext == ".mm" || opts.lang_opts.is_objc()) {
return opts.objc_passthrough;
}
return ext == ".s" || ext == ".asm" || ext == ".cu";
}
bool is_frontend_source(const DriverOptions& opts, const std::filesystem::path& path) {
if (opts.assembler_language) {
return false;
}
const std::string ext = input_extension(path);
if (is_passthrough_source_extension(opts, ext) ||
is_linker_input_extension(ext)) {
return false;
}
if (opts.preprocess_only) {
return true;
}
if (opts.lang_opts.language_mode != LanguageMode::Auto) {
return true;
}
if (is_stdin_input(path.string())) {
return true;
}
return ext == ".c" || is_cpp_source_extension(ext) ||
ext == ".m" || ext == ".mm" ||
path.extension().string() == ".C";
}
bool file_selects_cxx_linker(const DriverOptions& opts, const std::filesystem::path& path) {
LangOptions lang_opts = options_for_file(opts.lang_opts, path.string());
if (lang_opts.is_cxx_mode()) {
return true;
}
const std::string ext = input_extension(path);
return ext == ".mm" || ext == ".cu";
}
std::ostream& open_output_if_requested(const DriverOptions& opts,
std::ofstream& owned,
bool multi_input) {
if (opts.output_path.empty()) {
return std::cout;
}
if (multi_input) {
throw std::runtime_error("-o cannot be used with multiple inputs in this driver");
}
owned.open(opts.output_path, std::ios::binary);
if (!owned) {
throw std::runtime_error("could not open output file '" + opts.output_path + "'");
}
return owned;
}
int output_mode_count(const DriverOptions& opts) {
int count = 0;
if (opts.emit_llvm) ++count;
if (opts.emit_assembly) ++count;
if (opts.compile_only) ++count;
if (opts.jit) ++count;
return count;
}
bool needs_text_output_stream(const DriverOptions& opts) {
return opts.preprocess_only || opts.dump_syntax || opts.dump_cir ||
opts.dump_air || opts.emit_llvm;
}
std::string derived_target_output_path(const DriverOptions& opts,
const std::string& input_path,
std::string_view extension) {
if (!opts.output_path.empty()) {
return opts.output_path;
}
std::filesystem::path path(input_path);
std::string stem = path.stem().string();
if (stem.empty()) {
stem = "a";
}
return stem + std::string(extension);
}
std::string depfile_escape(const std::string& path) {
std::string escaped;
escaped.reserve(path.size());
for (char c : path) {
if (c == ' ') {
escaped += "\\ ";
} else if (c == '#') {
escaped += "\\#";
} else if (c == '$') {
escaped += "$$";
} else {
escaped.push_back(c);
}
}
return escaped;
}
std::string default_depfile_target(const DriverOptions& opts,
const std::string& input_path) {
if (!opts.output_path.empty()) {
return opts.output_path;
}
std::filesystem::path path(input_path);
std::string stem = path.stem().string();
if (stem.empty()) {
stem = "a";
}
return stem + ".o";
}
std::string default_depfile_path(const DriverOptions& opts,
const std::string& input_path) {
std::filesystem::path base(opts.output_path.empty() ? input_path
: opts.output_path);
std::string stem = base.stem().string();
if (stem.empty()) {
stem = "a";
}
std::filesystem::path dir = base.parent_path();
return (dir / (stem + ".d")).string();
}
bool write_depfile(const DriverOptions& opts,
const std::string& input_path,
const SourceManager& sm) {
const bool to_stdout =
opts.depfile.generate_only && opts.depfile.output_path.empty();
const std::string depfile_path = opts.depfile.output_path.empty()
? default_depfile_path(opts, input_path)
: opts.depfile.output_path;
std::vector<std::string> deps;
deps.push_back(input_path == "-" ? "<stdin>" : input_path);
for (const auto& file : sm.files) {
if (file == nullptr || file->resolved_path.empty()) {
continue;
}
if (file->is_system_header && !opts.depfile.include_system_headers) {
continue;
}
if (file->resolved_path == deps.front()) {
continue;
}
deps.push_back(file->resolved_path);
}
std::ofstream owned;
if (!to_stdout) {
owned.open(depfile_path, std::ios::binary);
if (!owned) {
std::cerr << "aburi: error: cannot write dependency file '"
<< depfile_path << "'\n";
return false;
}
}
std::ostream& out = to_stdout ? std::cout : owned;
std::string target_list;
if (opts.depfile.targets.empty()) {
target_list = depfile_escape(default_depfile_target(opts, input_path));
} else {
for (size_t i = 0; i < opts.depfile.targets.size(); ++i) {
if (i != 0) {
target_list += ' ';
}
target_list += opts.depfile.targets[i];
}
}
out << target_list << ':';
for (const std::string& dep : deps) {
out << " \\\n " << depfile_escape(dep);
}
out << '\n';
if (opts.depfile.phony_targets) {
for (size_t i = 1; i < deps.size(); ++i) {
out << '\n' << depfile_escape(deps[i]) << ":\n";
}
}
return out.good();
}
bool is_final_link_mode(const DriverOptions& opts) {
return !opts.preprocess_only &&
!opts.precompile_only &&
!opts.syntax_only &&
!opts.dump_syntax &&
!opts.dump_cir &&
!opts.dump_air &&
!opts.emit_llvm &&
!opts.emit_assembly &&
!opts.compile_only &&
!opts.jit;
}
struct LinkState {
std::vector<std::string> object_files;
std::vector<std::string> linker_input_files;
std::optional<std::filesystem::path> temp_object_dir;
size_t temp_object_counter = 0;
bool link_as_cxx = false;
bool link_objc_runtime = false;
};
std::filesystem::path ensure_temp_object_dir(LinkState& state) {
if (state.temp_object_dir.has_value()) {
return *state.temp_object_dir;
}
llvm::SmallString<256> temp_dir_storage;
std::error_code ec =
llvm::sys::fs::createUniqueDirectory("aburi-link-%%%%%%", temp_dir_storage);
if (ec) {
throw std::runtime_error(
"unable to create temporary link object directory: " + ec.message());
}
state.temp_object_dir = std::filesystem::path(std::string(temp_dir_storage.str()));
return *state.temp_object_dir;
}
std::string temporary_object_path(LinkState& state, const std::filesystem::path& input_path) {
std::string stem = input_path.stem().string();
if (stem.empty()) {
stem = "input";
}
std::filesystem::path temp_path =
ensure_temp_object_dir(state) /
(stem + "." + std::to_string(state.temp_object_counter++) + ".o");
return temp_path.string();
}
void cleanup_temp_objects(LinkState& state) {
if (!state.temp_object_dir.has_value()) {
return;
}
std::error_code ec;
std::filesystem::remove_all(*state.temp_object_dir, ec);
}
int run_command(const std::string& command, const std::vector<std::string>& args) {
std::vector<llvm::StringRef> arg_refs;
arg_refs.push_back(command);
for (const std::string& arg : args) {
arg_refs.push_back(arg);
}
std::string error_message;
int result = llvm::sys::ExecuteAndWait(command,
llvm::ArrayRef<llvm::StringRef>(arg_refs),
std::nullopt,
{},
0,
0,
&error_message);
if (result != 0 && !error_message.empty()) {
std::cerr << "aburi: error: " << error_message << '\n';
}
return result;
}
bool target_is_linux(const DriverOptions& opts) {
return !opts.target_triple.empty() &&
target_os_from_triple(opts.target_triple) == TargetOS::LINUX;
}
bool target_is_cross_elf(const DriverOptions& opts) {
if (opts.target_triple.empty()) {
return false;
}
TargetOS os = target_os_from_triple(opts.target_triple);
return os == TargetOS::LINUX || os == TargetOS::FREEBSD ||
os == TargetOS::NETBSD;
}
std::shared_ptr<TargetInfo> effective_target_info(const DriverOptions& opts) {
std::shared_ptr<TargetInfo> info;
if (!opts.target_triple.empty()) {
info = TargetInfo::create_for_triple(opts.target_triple);
}
if (!info) {
info = TargetInfo::create_host();
}
if (info && opts.short_wchar) {
info->wchar_width = 16;
}
return info;
}
bool integrated_assembler_supports(
const std::shared_ptr<TargetInfo>& target) {
if (!target || target->os != TargetOS::MACOS) {
return false;
}
return target->arch == TargetArch::AARCH64 ||
target->arch == TargetArch::X86_64;
}
std::string codegen_feature_string(const DriverOptions& opts) {
std::shared_ptr<TargetInfo> target = effective_target_info(opts);
bool aarch64 = target && target->arch == TargetArch::AARCH64;
std::vector<std::string> features;
if (opts.general_regs_only && aarch64) {
features.insert(features.end(),
{"+v8a", "-fp-armv8", "-neon", "-sve"});
} else if (aarch64 && (opts.aarch64_aes || opts.aarch64_sha2)) {
features.push_back("+v8a");
if (opts.aarch64_aes) {
features.push_back("+aes");
}
if (opts.aarch64_sha2) {
features.push_back("+sha2");
}
}
if (aarch64) {
for (int reg : opts.reserved_aarch64_gprs) {
features.push_back("+reserve-x" + std::to_string(reg));
}
}
std::string joined;
for (const std::string& feature : features) {
if (!joined.empty()) {
joined += ",";
}
joined += feature;
}
return joined;
}
std::string resolve_installed_dir(const char* argv0) {
if (argv0 == nullptr || *argv0 == '\0') {
return ".";
}
std::error_code ec;
auto absolute_path = std::filesystem::absolute(std::filesystem::path(argv0), ec);
if (ec) {
absolute_path = std::filesystem::path(argv0);
}
auto resolved = std::filesystem::weakly_canonical(absolute_path, ec);
auto final_path = ec ? absolute_path : resolved;
auto parent = final_path.parent_path();
if (parent.empty()) {
return ".";
}
return parent.string();
}
std::string clang_persona_version(const TargetInfo& target) {
return std::to_string(target.clang_major) + "." +
std::to_string(target.clang_minor) + "." +
std::to_string(target.clang_patch);
}
std::string gcc_persona_version() {
return std::to_string(aburi::driver::kGccPersonaMajor) + "." +
std::to_string(aburi::driver::kGccPersonaMinor) + "." +
std::to_string(aburi::driver::kGccPersonaPatch);
}
void print_gcc_version_banner(std::ostream& os) {
os << "gcc (" << aburi::driver::kGccPersonaBrand << ") "
<< gcc_persona_version()
<< " (" << aburi::driver::kAburiProductName << ' '
<< aburi::driver::kAburiVersionString
<< "; compatible with GCC from the Free Software Foundation)\n";
}
void print_gcc_verbose_banner(std::ostream& os,
const DriverOptions& opts) {
std::shared_ptr<TargetInfo> target = effective_target_info(opts);
const std::string triple = target ? target->triple : opts.target_triple;
os << "Using built-in specs.\n";
os << "Target: " << triple << '\n';
os << "Thread model: posix\n";
os << "gcc version " << gcc_persona_version()
<< " (" << aburi::driver::kGccPersonaBrand
<< "; " << aburi::driver::kAburiProductName << ' '
<< aburi::driver::kAburiVersionString
<< "; compatible with GCC from the Free Software Foundation)\n";
}
void print_version_banner(std::ostream& os,
const DriverOptions& opts,
const char* argv0) {
std::shared_ptr<TargetInfo> target = effective_target_info(opts);
const std::string triple =
target ? target->triple : opts.target_triple;
if (opts.persona == aburi::driver::DriverPersona::ClangCompat) {
os << "clang version "
<< (target ? clang_persona_version(*target) : "0.0.0")
<< " (" << aburi::driver::kAburiProductName << ' '
<< aburi::driver::kAburiVersionString << ")\n";
os << "Target: " << triple << '\n';
os << "Thread model: posix\n";
os << "InstalledDir: " << resolve_installed_dir(argv0) << '\n';
return;
}
if (opts.persona == aburi::driver::DriverPersona::GccCompat) {
print_gcc_version_banner(os);
return;
}
os << aburi::driver::kAburiProductName << " compiler "
<< aburi::driver::kAburiVersionString << '\n';
os << "Target: " << triple << '\n';
os << "InstalledDir: " << resolve_installed_dir(argv0) << '\n';
}
int handle_early_driver_query(const DriverOptions& opts, const char* argv0) {
using aburi::driver::EarlyDriverQuery;
switch (opts.early_query) {
case EarlyDriverQuery::Version:
print_version_banner(std::cout, opts, argv0);
return 0;
case EarlyDriverQuery::VerboseVersion:
if (opts.persona == aburi::driver::DriverPersona::GccCompat) {
print_gcc_verbose_banner(std::cerr, opts);
} else {
print_version_banner(std::cout, opts, argv0);
}
return 0;
case EarlyDriverQuery::DumpMachine: {
std::shared_ptr<TargetInfo> target = effective_target_info(opts);
std::cout << (target ? target->triple : opts.target_triple) << '\n';
return 0;
}
case EarlyDriverQuery::PrintFileName:
std::cout << opts.print_file_name << '\n';
return 0;
case EarlyDriverQuery::DumpVersion: {
if (opts.persona == aburi::driver::DriverPersona::ClangCompat) {
std::shared_ptr<TargetInfo> target = effective_target_info(opts);
std::cout << (target ? clang_persona_version(*target) : "0.0.0")
<< '\n';
} else if (opts.persona ==
aburi::driver::DriverPersona::GccCompat) {
std::cout << aburi::driver::kGccPersonaMajor << '\n';
} else {
std::cout << aburi::driver::kAburiVersionString << '\n';
}
return 0;
}
case EarlyDriverQuery::DumpFullVersion:
std::cout << gcc_persona_version() << '\n';
return 0;
case EarlyDriverQuery::None:
break;
}
return -1;
}
bool target_is_x86_32(const DriverOptions& opts) {
const std::string& t = opts.target_triple;
return (t.find("i386") != std::string::npos ||
t.find("i486") != std::string::npos ||
t.find("i586") != std::string::npos ||
t.find("i686") != std::string::npos);
}
bool target_is_or1k(const DriverOptions& opts) {
const std::string& t = opts.target_triple;
return t.find("or1k") != std::string::npos ||
t.find("openrisc") != std::string::npos;
}
std::string primary_target_sysroot(const DriverOptions& opts) {
if (!opts.sysroots.empty()) {
return opts.sysroots.front();
}
if (const char* env = std::getenv("ABURI_LINUX_SYSROOT")) {
return env;
}
return {};
}
std::string cross_compiler_path(const DriverOptions& opts, bool as_cxx) {
std::string base = opts.cross_cc;
if (base.empty()) {
if (const char* env = std::getenv("ABURI_CROSS_CC")) {
base = env;
}
}
if (base.empty()) {
base = "/opt/homebrew/opt/llvm/bin/clang";
}
if (as_cxx) {
if (base.size() >= 5 && base.rfind("clang") == base.size() - 5) {
base += "++";
} else if (base.size() >= 3 && base.rfind("gcc") == base.size() - 3) {
base.replace(base.size() - 3, 3, "g++");
}
}
return base;
}
bool cross_compiler_is_clang(const std::string& path) {
return path.find("clang") != std::string::npos;
}
bool has_explicit_target_sysroot(const DriverOptions& opts) {
if (!opts.sysroots.empty()) {
return true;
}
return target_is_linux(opts) && std::getenv("ABURI_LINUX_SYSROOT") != nullptr;
}
StdLibKind effective_stdlib(const DriverOptions& opts) {
return resolve_effective_stdlib(opts.target_triple,
opts.requested_stdlib,
has_explicit_target_sysroot(opts));
}
// Everything the final link needs in order to put Adinkra on the command line.
// Inactive whenever the user is supplying the C++ standard library themselves.
struct AdinkraLinkPlan {
bool active = false;
std::string library;
std::string rpath_dir;
std::string error;
};
AdinkraLinkPlan plan_adinkra_link(const DriverOptions& opts,
bool link_as_cxx,
const char* argv0) {
AdinkraLinkPlan plan;
if (!link_as_cxx || effective_stdlib(opts) != StdLibKind::Adinkra) {
return plan;
}
if (opts.nostdlib || opts.nostdlibxx) {
return plan;
}
if (!opts.adinkra_library.empty()) {
plan.active = true;
plan.library = opts.adinkra_library;
return plan;
}
if (!target_is_native_host(opts.target_triple) && opts.adinkra_root.empty()) {
plan.error = "aburi: error: '-stdlib=adinkra' for target '" +
opts.target_triple +
"' needs a target-built Adinkra runtime; pass --adinkra-lib=PATH "
"or --adinkra-root=DIR";
return plan;
}
const AdinkraLayout& layout = discover_adinkra_layout(argv0, opts.adinkra_root);
const bool want_shared = opts.adinkra_runtime == AdinkraRuntimeKind::Shared;
plan.library = want_shared ? layout.shared_library : layout.static_library;
if (plan.library.empty()) {
return plan;
}
plan.active = true;
if (want_shared && layout.from_build_tree) {
plan.rpath_dir = layout.shared_library_dir;
}
return plan;
}
bool require_adinkra_when_requested(const DriverOptions& opts, const char* argv0) {
if (opts.requested_stdlib != StdLibKind::Adinkra) {
return true;
}
if (!opts.adinkra_library.empty()) {
return true;
}
const AdinkraLayout& layout = discover_adinkra_layout(argv0, opts.adinkra_root);
if (layout.found) {
return true;
}
std::cerr << "aburi: error: '-stdlib=adinkra' selected but the Adinkra "
<< "headers were not found\n";
for (const std::string& root : layout.attempted_roots) {
std::cerr << "note: searched " << root << '\n';
}
std::cerr << "note: set ABURI_ADINKRA_ROOT or pass --adinkra-root=DIR\n";
return false;
}
std::vector<std::string> passthrough_compile_flags(const DriverOptions& opts,
const char* argv0) {
std::vector<std::string> flags;
for (const std::string& path : opts.include_paths) {
flags.push_back("-I" + path);
}
for (const std::string& path : opts.system_include_paths) {
flags.push_back("-isystem");
flags.push_back(path);
}
for (const std::string& path : opts.quote_include_paths) {
flags.push_back("-iquote");
flags.push_back(path);
}
for (const std::string& path : opts.framework_include_paths) {
flags.push_back("-F" + path);
}
for (const std::string& path : opts.system_framework_include_paths) {
flags.push_back("-iframework");
flags.push_back(path);
}
for (const auto& [name, value] : opts.defines) {
flags.push_back("-D" + name + "=" + value);
}
for (const std::string& name : opts.undefines) {
flags.push_back("-U" + name);
}
if (!opts.target_triple.empty()) {
flags.push_back("-target");
flags.push_back(opts.target_triple);
}
if (target_is_linux(opts)) {
const std::string sysroot = primary_target_sysroot(opts);
if (!sysroot.empty()) {
flags.push_back("--sysroot=" + sysroot);
}
} else {
for (const std::string& sysroot : opts.sysroots) {
flags.push_back("-isysroot");
flags.push_back(sysroot);
}
}
if (opts.assembler_language) {
flags.push_back("-x");
flags.push_back("assembler-with-cpp");
} else if (opts.lang_opts.language_mode == LanguageMode::C) {
flags.push_back("-x");
flags.push_back(opts.lang_opts.objc ? "objective-c" : "c");
} else if (opts.lang_opts.language_mode == LanguageMode::CXX) {
flags.push_back("-x");
flags.push_back(opts.lang_opts.objc ? "objective-c++" : "c++");
}
if (opts.lang_opts.objc_arc) {
flags.push_back("-fobjc-arc");
}
// ObjC++ and CUDA go to the system compiler. It has never heard of
// '-stdlib=adinkra', and leaving those units on libc++ while sibling C++
// units use Adinkra would produce two incompatible std:: namespaces that
// link cleanly and then fail to interoperate.
if (effective_stdlib(opts) == StdLibKind::Adinkra) {
const AdinkraLayout& layout =
discover_adinkra_layout(argv0, opts.adinkra_root);
if (layout.found) {
flags.push_back("-nostdinc++");
flags.push_back("-isystem");
flags.push_back(layout.include_dir);
}
} else if (opts.requested_stdlib != StdLibKind::Auto) {
flags.push_back("-stdlib=" + stdlib_kind_name(opts.requested_stdlib));
}
if (opts.nostdinc) {
flags.push_back("-nostdinc");
}
if (opts.nostdincxx) {
flags.push_back("-nostdinc++");
}
for (const std::string& file : opts.pre_includes) {
flags.push_back("-include");
flags.push_back(file);
}
if (opts.keep_comments) {
flags.push_back("-C");
}
if (opts.preprocess_no_linemarkers) {
flags.push_back("-P");
}
for (const std::string& wa : opts.assembler_flags) {
flags.push_back(wa);
}
if (opts.depfile.enabled) {
if (opts.depfile.generate_only) {
flags.push_back(opts.depfile.include_system_headers ? "-M" : "-MM");
} else {
flags.push_back(opts.depfile.include_system_headers ? "-MD" : "-MMD");
}
if (!opts.depfile.output_path.empty()) {
flags.push_back("-MF");
flags.push_back(opts.depfile.output_path);
}
for (const std::string& target : opts.depfile.targets) {
flags.push_back("-MT");
flags.push_back(target);
}
if (opts.depfile.phony_targets) {
flags.push_back("-MP");
}
}
return flags;
}
std::vector<std::string> linker_driver_flags(const DriverOptions& opts,
bool link_as_cxx,
const AdinkraLinkPlan& adinkra) {
std::vector<std::string> flags;
if (!opts.target_triple.empty()) {
flags.push_back("-target");
flags.push_back(opts.target_triple);
}
if (target_is_cross_elf(opts)) {
const std::string sysroot = primary_target_sysroot(opts);
if (!sysroot.empty()) {
flags.push_back("--sysroot=" + sysroot);
}
} else {
for (const std::string& sysroot : opts.sysroots) {
flags.push_back("-isysroot");
flags.push_back(sysroot);
}
}
if (adinkra.active) {
// Adinkra replaces the host driver's C++ standard library entirely.
flags.push_back("-nostdlib++");
} else if (link_as_cxx && opts.requested_stdlib != StdLibKind::Auto &&
opts.requested_stdlib != StdLibKind::Adinkra) {
// '-stdlib=adinkra' is meaningless to the host driver, and a C-only
// link has no C++ standard library to select.
flags.push_back("-stdlib=" + stdlib_kind_name(opts.requested_stdlib));
}
if (target_is_x86_32(opts) || target_is_or1k(opts)) {
flags.push_back("-no-pie");
}
return flags;
}
bool validate_driver_options(const DriverOptions& opts) {
if (output_mode_count(opts) > 1) {
std::cerr
<< "aburi: error: choose only one of --emit-llvm, -S, -c, or --jit\n";
return false;
}
if (opts.syntax_only &&
(opts.emit_llvm || opts.emit_assembly || opts.compile_only || opts.jit)) {
std::cerr << "aburi: error: cannot combine '-fsyntax-only' with a "
<< "backend output stage\n";
return false;
}
if (opts.lang_opts.blocks_mode == BlocksMode::Enabled) {
std::shared_ptr<TargetInfo> target = opts.target_triple.empty()
? TargetInfo::create_host()
: TargetInfo::create_for_triple(opts.target_triple);
if (!target || target->os != TargetOS::MACOS) {
std::cerr << "aburi: error: '-fblocks' is only supported on Darwin "
<< "targets\n";
return false;
}
}
if ((opts.emit_llvm || opts.emit_assembly || opts.compile_only) &&
(opts.dump_syntax || opts.dump_cir || opts.dump_air)) {
std::cerr
<< "aburi: error: output modes cannot be combined with "
<< "--dump-syntax, --dump-cir, or --dump-air\n";
return false;
}
if (opts.backend == "air") {
if (opts.jit) {
std::cerr << "aburi: error: --jit is not supported with "
<< "--backend=air yet\n";
return false;
}
if (opts.emit_llvm) {
std::cerr << "aburi: error: --emit-llvm requires --backend=llvm\n";
return false;
}
}
if (!opts.output_path.empty() && opts.input_files.size() > 1 &&
(opts.preprocess_only || opts.dump_syntax || opts.dump_cir ||
opts.dump_air || opts.emit_llvm || opts.emit_assembly ||
opts.compile_only)) {
std::cerr
<< "aburi: error: -o cannot be used with multiple inputs in this mode\n";
return false;
}
if (opts.jit) {
if (opts.preprocess_only || opts.dump_macros || opts.syntax_only ||
opts.dump_syntax || opts.dump_cir || opts.dump_air ||
!opts.dump_cir_example.empty()) {
std::cerr
<< "aburi: error: --jit cannot be combined with preprocessing, "
<< "syntax-only, or dump modes\n";
return false;
}
if (!opts.output_path.empty()) {
std::cerr << "aburi: error: --jit does not write an output file\n";
return false;
}
if (opts.input_files.size() > 1) {
std::cerr << "aburi: error: --jit accepts exactly one input file\n";
return false;
}
}
return true;
}
aburi::frontend::FrontendInvocation make_invocation(const DriverOptions& base_opts,
const std::string& path,
std::string source,
const char* argv0) {
aburi::frontend::FrontendInvocation invocation;
invocation.filename = path;
invocation.source = std::move(source);
invocation.lang_options = options_for_file(base_opts.lang_opts, path);
invocation.target_triple = base_opts.target_triple;
invocation.target = effective_target_info(base_opts);
invocation.include_paths = base_opts.include_paths;
invocation.system_include_paths = base_opts.system_include_paths;
invocation.quote_include_paths = base_opts.quote_include_paths;
invocation.framework_include_paths = base_opts.framework_include_paths;
invocation.system_framework_include_paths =
base_opts.system_framework_include_paths;
invocation.sysroots = base_opts.sysroots;
invocation.pre_includes = base_opts.pre_includes;
if (base_opts.persona == aburi::driver::DriverPersona::GccCompat) {
invocation.defines.emplace_back(
"__GNUC__", std::to_string(aburi::driver::kGccPersonaMajor));
invocation.defines.emplace_back(
"__GNUC_MINOR__", std::to_string(aburi::driver::kGccPersonaMinor));
invocation.defines.emplace_back(
"__GNUC_PATCHLEVEL__", std::to_string(aburi::driver::kGccPersonaPatch));
invocation.defines.emplace_back(
"__VERSION__", "\"" + gcc_persona_version() + " (" +
aburi::driver::kGccPersonaBrand + ")\"");
if (invocation.lang_options.is_cxx_mode()) {
invocation.defines.emplace_back(
"__GNUG__", std::to_string(aburi::driver::kGccPersonaMajor));
}
invocation.undefines = {
"__clang__",
"__clang_major__",
"__clang_minor__",
"__clang_patchlevel__",
"__clang_version__",
};
}
invocation.defines.insert(invocation.defines.end(),
base_opts.defines.begin(), base_opts.defines.end());
invocation.undefines.insert(invocation.undefines.end(),
base_opts.undefines.begin(), base_opts.undefines.end());
invocation.requested_stdlib = base_opts.requested_stdlib;
invocation.adinkra_root = base_opts.adinkra_root;
invocation.nostdinc = base_opts.nostdinc;
invocation.nostdincxx = base_opts.nostdincxx;
invocation.inhibit_warnings = base_opts.inhibit_warnings;
invocation.argv0_for_header_discovery = argv0 ? argv0 : "";
return invocation;
}
void print_frontend_failures(const aburi::frontend::FrontendResult& result) {
if (!result.exception_text.empty()) {
std::cerr << "aburi: error: " << result.exception_text << '\n';
}
for (const std::string& diagnostic : result.diagnostics) {
std::cerr << diagnostic << '\n';
}
if (!result.tree_verified && !result.tree_verify_output.empty()) {
std::cerr << result.tree_verify_output;