authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-11-03 11:24:39-07:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-11-04 09:12:54+01:00
loga333ac846dd2ac8ed67f84a15bef272ccd65df37
tree7723b706eb1338535a593e5dd72deab784159b3c
parent65adbec918d7ccc1dd32a48dc010dca87d48411d

Compilation: refactor logic for library-building capabilities

And mark the stage2_x86_64 backend as being capable of building compiler-rt and zig libc.

1 files changed, 63 insertions(+), 11 deletions(-)

src/Compilation.zig+63-11
...@@ -810,16 +810,6 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {...@@ -810,16 +810,6 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
810 return error.ExportTableAndImportTableConflict;810 return error.ExportTableAndImportTableConflict;
811 }811 }
812812
813 // The `have_llvm` condition is here only because native backends cannot yet build compiler-rt.
814 // Once they are capable this condition could be removed. When removing this condition,
815 // also test the use case of `build-obj -fcompiler-rt` with the native backends
816 // and make sure the compiler-rt symbols are emitted.
817 const is_p9 = options.target.os.tag == .plan9;
818 const is_spv = options.target.cpu.arch.isSpirV();
819 const capable_of_building_compiler_rt = build_options.have_llvm and !is_p9 and !is_spv;
820 const capable_of_building_zig_libc = build_options.have_llvm and !is_p9 and !is_spv;
821 const capable_of_building_ssp = build_options.have_llvm and !is_p9 and !is_spv;
822
823 const comp: *Compilation = comp: {813 const comp: *Compilation = comp: {
824 // For allocations that have the same lifetime as Compilation. This arena is used only during this814 // For allocations that have the same lifetime as Compilation. This arena is used only during this
825 // initialization and then is freed in deinit().815 // initialization and then is freed in deinit().
...@@ -1094,6 +1084,8 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {...@@ -1094,6 +1084,8 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
1094 if (stack_check and !target_util.supportsStackProbing(options.target))1084 if (stack_check and !target_util.supportsStackProbing(options.target))
1095 return error.StackCheckUnsupportedByTarget;1085 return error.StackCheckUnsupportedByTarget;
10961086
1087 const capable_of_building_ssp = canBuildLibSsp(options.target, use_llvm);
1088
1097 const stack_protector: u32 = options.want_stack_protector orelse b: {1089 const stack_protector: u32 = options.want_stack_protector orelse b: {
1098 if (!target_util.supportsStackProtector(options.target)) break :b @as(u32, 0);1090 if (!target_util.supportsStackProtector(options.target)) break :b @as(u32, 0);
10991091
...@@ -1752,6 +1744,9 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {...@@ -1752,6 +1744,9 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
17521744
1753 const target = comp.getTarget();1745 const target = comp.getTarget();
17541746
1747 const capable_of_building_compiler_rt = canBuildLibCompilerRt(target, comp.bin_file.options.use_llvm);
1748 const capable_of_building_zig_libc = canBuildZigLibC(target, comp.bin_file.options.use_llvm);
1749
1755 // Add a `CObject` for each `c_source_files`.1750 // Add a `CObject` for each `c_source_files`.
1756 try comp.c_object_table.ensureTotalCapacity(gpa, options.c_source_files.len);1751 try comp.c_object_table.ensureTotalCapacity(gpa, options.c_source_files.len);
1757 for (options.c_source_files) |c_source_file| {1752 for (options.c_source_files) |c_source_file| {
...@@ -6238,9 +6233,66 @@ pub fn dump_argv(argv: []const []const u8) void {...@@ -6238,9 +6233,66 @@ pub fn dump_argv(argv: []const []const u8) void {
6238 nosuspend stderr.print("{s}\n", .{argv[argv.len - 1]}) catch {};6233 nosuspend stderr.print("{s}\n", .{argv[argv.len - 1]}) catch {};
6239}6234}
62406235
6236fn canBuildLibCompilerRt(target: std.Target, use_llvm: bool) bool {
6237 switch (target.os.tag) {
6238 .plan9 => return false,
6239 else => {},
6240 }
6241 switch (target.cpu.arch) {
6242 .spirv32, .spirv64 => return false,
6243 else => {},
6244 }
6245 return switch (zigBackend(target, use_llvm)) {
6246 .stage2_llvm,
6247 .stage2_x86_64,
6248 => true,
6249
6250 else => false,
6251 };
6252}
6253
6254fn canBuildLibSsp(target: std.Target, use_llvm: bool) bool {
6255 switch (target.os.tag) {
6256 .plan9 => return false,
6257 else => {},
6258 }
6259 switch (target.cpu.arch) {
6260 .spirv32, .spirv64 => return false,
6261 else => {},
6262 }
6263 return switch (zigBackend(target, use_llvm)) {
6264 .stage2_llvm => true,
6265 else => false,
6266 };
6267}
6268
6269/// Not to be confused with canBuildLibC, which builds musl, glibc, and similar.
6270/// This one builds lib/c.zig.
6271fn canBuildZigLibC(target: std.Target, use_llvm: bool) bool {
6272 switch (target.os.tag) {
6273 .plan9 => return false,
6274 else => {},
6275 }
6276 switch (target.cpu.arch) {
6277 .spirv32, .spirv64 => return false,
6278 else => {},
6279 }
6280 return switch (zigBackend(target, use_llvm)) {
6281 .stage2_llvm,
6282 .stage2_x86_64,
6283 => true,
6284
6285 else => false,
6286 };
6287}
6288
6241pub fn getZigBackend(comp: Compilation) std.builtin.CompilerBackend {6289pub fn getZigBackend(comp: Compilation) std.builtin.CompilerBackend {
6242 if (comp.bin_file.options.use_llvm) return .stage2_llvm;
6243 const target = comp.bin_file.options.target;6290 const target = comp.bin_file.options.target;
6291 return zigBackend(target, comp.bin_file.options.use_llvm);
6292}
6293
6294fn zigBackend(target: std.Target, use_llvm: bool) std.builtin.CompilerBackend {
6295 if (use_llvm) return .stage2_llvm;
6244 if (target.ofmt == .c) return .stage2_c;6296 if (target.ofmt == .c) return .stage2_c;
6245 return switch (target.cpu.arch) {6297 return switch (target.cpu.arch) {
6246 .wasm32, .wasm64 => std.builtin.CompilerBackend.stage2_wasm,6298 .wasm32, .wasm64 => std.builtin.CompilerBackend.stage2_wasm,