authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-07-04 07:19:08+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-07-04 07:33:00+02:00
log1cfc3647853123efc0fda991180e337d8a72eac5
treefc5b5b25177d1af56e33a521897d82135110cf97
parent768b17755e7735b328b92212de2dd7018f78fb4b

tsan: build dynamic library on Apple platforms


2 files changed, 34 insertions(+), 6 deletions(-)

src/Compilation.zig+3
......@@ -188,6 +188,9 @@ libunwind_static_lib: ?CRTFile = null,
188188/// Populated when we build the TSAN static library. A Job to build this is placed in the queue
189189/// and resolved before calling linker.flush().
190190tsan_static_lib: ?CRTFile = null,
191/// Populated when we build the TSAN dynamic library. A Job to build this is placed in the queue
192/// and resolved before calling linker.flush().
193tsan_dynamic_lib: ?CRTFile = null,
191194/// Populated when we build the libc static library. A Job to build this is placed in the queue
192195/// and resolved before calling linker.flush().
193196libc_static_lib: ?CRTFile = null,
src/libtsan.zig+31-6
......@@ -25,10 +25,20 @@ pub fn buildTsan(comp: *Compilation, prog_node: std.Progress.Node) BuildError!vo
2525 defer arena_allocator.deinit();
2626 const arena = arena_allocator.allocator();
2727
28 const root_name = "tsan";
29 const output_mode = .Lib;
30 const link_mode = .static;
3128 const target = comp.getTarget();
29 const root_name = switch (target.os.tag) {
30 // On Apple platforms, we use the same name as LLVM and Apple so that we correctly
31 // mark the images as instrumented when traversing them when TSAN dylib is
32 // initialized.
33 .macos => "clang_rt.tsan_osx_dynamic",
34 .ios => switch (target.abi) {
35 .simulator => "clang_rt.tsan_iossim_dynamic",
36 else => "clang_rt.tsan_ios_dynamic",
37 },
38 else => "tsan",
39 };
40 const link_mode: std.builtin.LinkMode = if (target.isDarwin()) .dynamic else .static;
41 const output_mode = .Lib;
3242 const basename = try std.zig.binNameAlloc(arena, .{
3343 .root_name = root_name,
3444 .target = target,
......@@ -43,6 +53,7 @@ pub fn buildTsan(comp: *Compilation, prog_node: std.Progress.Node) BuildError!vo
4353
4454 const optimize_mode = comp.compilerRtOptMode();
4555 const strip = comp.compilerRtStrip();
56 const link_libcpp = target.isDarwin();
4657
4758 const config = Compilation.Config.resolve(.{
4859 .output_mode = output_mode,
......@@ -54,6 +65,7 @@ pub fn buildTsan(comp: *Compilation, prog_node: std.Progress.Node) BuildError!vo
5465 .root_optimize_mode = optimize_mode,
5566 .root_strip = strip,
5667 .link_libc = true,
68 .link_libcpp = link_libcpp,
5769 }) catch |err| {
5870 comp.setMiscFailure(
5971 .libtsan,
......@@ -272,6 +284,12 @@ pub fn buildTsan(comp: *Compilation, prog_node: std.Progress.Node) BuildError!vo
272284 });
273285 }
274286
287 const skip_linker_dependencies = !target.isDarwin();
288 const linker_allow_shlib_undefined = target.isDarwin();
289 const install_name = if (target.isDarwin())
290 try std.fmt.allocPrintZ(arena, "@rpath/{s}", .{basename})
291 else
292 null;
275293 const sub_compilation = Compilation.create(comp.gpa, arena, .{
276294 .local_cache_directory = comp.global_cache_directory,
277295 .global_cache_directory = comp.global_cache_directory,
......@@ -294,7 +312,9 @@ pub fn buildTsan(comp: *Compilation, prog_node: std.Progress.Node) BuildError!vo
294312 .verbose_cimport = comp.verbose_cimport,
295313 .verbose_llvm_cpu_features = comp.verbose_llvm_cpu_features,
296314 .clang_passthrough_mode = comp.clang_passthrough_mode,
297 .skip_linker_dependencies = true,
315 .skip_linker_dependencies = skip_linker_dependencies,
316 .linker_allow_shlib_undefined = linker_allow_shlib_undefined,
317 .install_name = install_name,
298318 }) catch |err| {
299319 comp.setMiscFailure(
300320 .libtsan,
......@@ -317,8 +337,13 @@ pub fn buildTsan(comp: *Compilation, prog_node: std.Progress.Node) BuildError!vo
317337 },
318338 };
319339
320 assert(comp.tsan_static_lib == null);
321 comp.tsan_static_lib = try sub_compilation.toCrtFile();
340 assert(comp.tsan_static_lib == null and comp.tsan_dynamic_lib == null);
341
342 if (target.isDarwin()) {
343 comp.tsan_dynamic_lib = try sub_compilation.toCrtFile();
344 } else {
345 comp.tsan_static_lib = try sub_compilation.toCrtFile();
346 }
322347}
323348
324349const tsan_sources = [_][]const u8{