authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-09-29 18:32:41-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-09-29 18:32:41-07:00
log1572cd4d76dfa35e6a05bbb1405982d7fef6f833
tree9fcbeff7b1cbc3e0ca3450feec5aad5e21138a7d
parentaf64fd2f424401ff66638696b35b1bf385c4b039

different strategy to fix compiler_rt.zig and c.zig

with respect to std.builtin.link_libc. The commit 27e008eb292038c5a6b9a13b64c7b69d1525f690 did not solve the problem because although it got std.builtin.link_libc to be true for compiler_rt.zig and c.zig, it had other unintentional side effects which broke the build for -lc -target foo-linux-musl. This commit introduces a new flag to Compilation to allow setting this comptime flag to true without introducing other side effects to compilation and linking.

3 files changed, 19 insertions(+), 14 deletions(-)

src/Compilation.zig+18-11
......@@ -374,6 +374,7 @@ pub const InitOptions = struct {
374374 is_test: bool = false,
375375 test_evented_io: bool = false,
376376 is_compiler_rt_or_libc: bool = false,
377 parent_compilation_link_libc: bool = false,
377378 stack_size_override: ?u64 = null,
378379 self_exe_path: ?[]const u8 = null,
379380 version: ?std.builtin.Version = null,
......@@ -614,6 +615,8 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
614615 hash.add(options.target.os.getVersionRange());
615616 hash.add(dll_export_fns);
616617 hash.add(options.is_test);
618 hash.add(options.is_compiler_rt_or_libc);
619 hash.add(options.parent_compilation_link_libc);
617620
618621 const digest = hash.final();
619622 const artifact_sub_dir = try std.fs.path.join(arena, &[_][]const u8{ "o", &digest });
......@@ -781,6 +784,7 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
781784 .error_return_tracing = error_return_tracing,
782785 .llvm_cpu_features = llvm_cpu_features,
783786 .is_compiler_rt_or_libc = options.is_compiler_rt_or_libc,
787 .parent_compilation_link_libc = options.parent_compilation_link_libc,
784788 .each_lib_rpath = options.each_lib_rpath orelse false,
785789 .disable_lld_caching = options.disable_lld_caching,
786790 .subsystem = options.subsystem,
......@@ -848,7 +852,7 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
848852 comp.c_object_table.putAssumeCapacityNoClobber(c_object, {});
849853 }
850854
851 if (comp.bin_file.options.emit != null) {
855 if (comp.bin_file.options.emit != null and !comp.bin_file.options.is_compiler_rt_or_libc) {
852856 // If we need to build glibc for the target, add work items for it.
853857 // We go through the work queue so that building can be done in parallel.
854858 if (comp.wantBuildGLibCFromSource()) {
......@@ -903,9 +907,7 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
903907 try comp.work_queue.writeItem(.libcxx);
904908 try comp.work_queue.writeItem(.libcxxabi);
905909 }
906 if (is_exe_or_dyn_lib and !comp.bin_file.options.is_compiler_rt_or_libc and
907 build_options.is_stage1)
908 {
910 if (is_exe_or_dyn_lib and build_options.is_stage1) {
909911 try comp.work_queue.writeItem(.{ .libcompiler_rt = {} });
910912 if (!comp.bin_file.options.link_libc) {
911913 try comp.work_queue.writeItem(.{ .zig_libc = {} });
......@@ -2345,6 +2347,15 @@ pub fn generateBuiltinZigSource(comp: *Compilation, allocator: *Allocator) ![]u8
23452347 ),
23462348 }
23472349 try buffer.appendSlice("};\n");
2350
2351 // This is so that compiler_rt and libc.zig libraries know whether they
2352 // will eventually be linked with libc. They make different decisions
2353 // about what to export depending on whether another libc will be linked
2354 // in. For example, compiler_rt will not export the __chkstk symbol if it
2355 // knows libc will provide it, and likewise c.zig will not export memcpy.
2356 const link_libc = comp.bin_file.options.link_libc or
2357 (comp.bin_file.options.is_compiler_rt_or_libc and comp.bin_file.options.parent_compilation_link_libc);
2358
23482359 try buffer.writer().print(
23492360 \\pub const object_format = ObjectFormat.{};
23502361 \\pub const mode = Mode.{};
......@@ -2359,7 +2370,7 @@ pub fn generateBuiltinZigSource(comp: *Compilation, allocator: *Allocator) ![]u8
23592370 , .{
23602371 @tagName(comp.bin_file.options.object_format),
23612372 @tagName(comp.bin_file.options.optimize_mode),
2362 comp.bin_file.options.link_libc,
2373 link_libc,
23632374 comp.bin_file.options.link_libcpp,
23642375 comp.bin_file.options.error_return_tracing,
23652376 comp.bin_file.options.valgrind,
......@@ -2481,6 +2492,7 @@ fn buildStaticLibFromZig(comp: *Compilation, src_basename: []const u8, out: *?CR
24812492 .verbose_llvm_cpu_features = comp.verbose_llvm_cpu_features,
24822493 .clang_passthrough_mode = comp.clang_passthrough_mode,
24832494 .is_compiler_rt_or_libc = true,
2495 .parent_compilation_link_libc = comp.bin_file.options.link_libc,
24842496 });
24852497 defer sub_compilation.destroy();
24862498
......@@ -2842,12 +2854,7 @@ pub fn build_crt_file(
28422854 .verbose_llvm_cpu_features = comp.verbose_llvm_cpu_features,
28432855 .clang_passthrough_mode = comp.clang_passthrough_mode,
28442856 .is_compiler_rt_or_libc = true,
2845 // This is so that compiler_rt and libc.zig libraries know whether they
2846 // will eventually be linked with libc. They make different decisions
2847 // about what to export depending on whether another libc will be linked
2848 // in. For example, compiler_rt will not export the __chkstk symbol if it
2849 // knows libc will provide it, and likewise c.zig will not export memcpy.
2850 .link_libc = comp.bin_file.options.link_libc,
2857 .parent_compilation_link_libc = comp.bin_file.options.link_libc,
28512858 });
28522859 defer sub_compilation.destroy();
28532860
src/link.zig+1
......@@ -73,6 +73,7 @@ pub const Options = struct {
7373 dll_export_fns: bool,
7474 error_return_tracing: bool,
7575 is_compiler_rt_or_libc: bool,
76 parent_compilation_link_libc: bool,
7677 each_lib_rpath: bool,
7778 disable_lld_caching: bool,
7879 is_test: bool,
src/link/MachO.zig-3
......@@ -323,9 +323,6 @@ fn linkWithLLD(self: *MachO, comp: *Compilation) !void {
323323 const is_lib = self.base.options.output_mode == .Lib;
324324 const is_dyn_lib = self.base.options.link_mode == .Dynamic and is_lib;
325325 const is_exe_or_dyn_lib = is_dyn_lib or self.base.options.output_mode == .Exe;
326 const have_dynamic_linker = self.base.options.link_libc and
327 self.base.options.link_mode == .Dynamic and is_exe_or_dyn_lib;
328 const link_in_crt = self.base.options.link_libc and self.base.options.output_mode == .Exe;
329326 const target = self.base.options.target;
330327 const stack_size = self.base.options.stack_size_override orelse 16777216;
331328 const allow_shlib_undefined = self.base.options.allow_shlib_undefined orelse !self.base.options.is_native_os;