master
1const std = @import("std");
2const path = std.fs.path;
3const assert = std.debug.assert;
4
5const target_util = @import("../target.zig");
6const Compilation = @import("../Compilation.zig");
7const Module = @import("../Package/Module.zig");
8const build_options = @import("build_options");
9const trace = @import("../tracy.zig").trace;
10
11pub const BuildError = error{
12 OutOfMemory,
13 AlreadyReported,
14 ZigCompilerNotBuiltWithLLVMExtensions,
15};
16
17pub fn buildStaticLib(comp: *Compilation, prog_node: std.Progress.Node) BuildError!void {
18 if (!build_options.have_llvm) {
19 return error.ZigCompilerNotBuiltWithLLVMExtensions;
20 }
21
22 const tracy = trace(@src());
23 defer tracy.end();
24
25 var arena_allocator = std.heap.ArenaAllocator.init(comp.gpa);
26 defer arena_allocator.deinit();
27 const arena = arena_allocator.allocator();
28
29 const io = comp.io;
30 const output_mode = .Lib;
31 const target = &comp.root_mod.resolved_target.result;
32 const unwind_tables: std.builtin.UnwindTables =
33 if (target.cpu.arch == .x86 and target.os.tag == .windows) .none else .async;
34 const config = Compilation.Config.resolve(.{
35 .output_mode = output_mode,
36 .resolved_target = comp.root_mod.resolved_target,
37 .is_test = false,
38 .have_zcu = false,
39 .emit_bin = true,
40 .root_optimize_mode = comp.compilerRtOptMode(),
41 .root_strip = comp.compilerRtStrip(),
42 .link_libc = true,
43 .any_unwind_tables = unwind_tables != .none,
44 .lto = comp.config.lto,
45 }) catch |err| {
46 comp.lockAndSetMiscFailure(
47 .libunwind,
48 "unable to build libunwind: resolving configuration failed: {s}",
49 .{@errorName(err)},
50 );
51 return error.AlreadyReported;
52 };
53 const root_mod = Module.create(arena, .{
54 .paths = .{
55 .root = .zig_lib_root,
56 .root_src_path = "",
57 },
58 .fully_qualified_name = "root",
59 .inherited = .{
60 .resolved_target = comp.root_mod.resolved_target,
61 .strip = comp.compilerRtStrip(),
62 .stack_check = false,
63 .stack_protector = 0,
64 .red_zone = comp.root_mod.red_zone,
65 .omit_frame_pointer = comp.root_mod.omit_frame_pointer,
66 .valgrind = false,
67 .sanitize_c = .off,
68 .sanitize_thread = false,
69 // necessary so that libunwind can unwind through its own stack frames
70 // The old 32-bit x86 variant of SEH doesn't use tables.
71 .unwind_tables = unwind_tables,
72 .pic = if (target_util.supports_fpic(target)) true else null,
73 .optimize_mode = comp.compilerRtOptMode(),
74 .code_model = comp.root_mod.code_model,
75 },
76 .global = config,
77 .cc_argv = &.{},
78 .parent = null,
79 }) catch |err| {
80 comp.lockAndSetMiscFailure(
81 .libunwind,
82 "unable to build libunwind: creating module failed: {s}",
83 .{@errorName(err)},
84 );
85 return error.AlreadyReported;
86 };
87
88 const root_name = "unwind";
89 var c_source_files: [unwind_src_list.len]Compilation.CSourceFile = undefined;
90 for (unwind_src_list, 0..) |unwind_src, i| {
91 var cflags = std.array_list.Managed([]const u8).init(arena);
92
93 switch (Compilation.classifyFileExt(unwind_src)) {
94 .c => {
95 try cflags.appendSlice(&.{
96 "-std=c99",
97 "-fexceptions",
98 });
99 },
100 .cpp => {
101 try cflags.append("-fno-exceptions");
102 try cflags.append("-fno-rtti");
103 },
104 .assembly_with_cpp => {},
105 else => unreachable, // See `unwind_src_list`.
106 }
107 try cflags.append("-I");
108 try cflags.append(try comp.dirs.zig_lib.join(arena, &.{ "libunwind", "include" }));
109 try cflags.append("-D_LIBUNWIND_HIDE_SYMBOLS");
110 try cflags.append("-Wa,--noexecstack");
111 try cflags.append("-fvisibility=hidden");
112 try cflags.append("-fvisibility-inlines-hidden");
113 try cflags.append("-fvisibility-global-new-delete=force-hidden");
114
115 // This is intentionally always defined because the macro definition means, should it only
116 // build for the target specified by compiler defines. Since we pass -target the compiler
117 // defines will be correct.
118 try cflags.append("-D_LIBUNWIND_IS_NATIVE_ONLY");
119
120 if (comp.root_mod.optimize_mode == .Debug) {
121 try cflags.append("-D_DEBUG");
122 }
123 if (!comp.config.any_non_single_threaded) {
124 try cflags.append("-D_LIBUNWIND_HAS_NO_THREADS");
125 }
126 if (target.cpu.arch.isArm() and target.abi.float() == .hard) {
127 try cflags.append("-DCOMPILER_RT_ARMHF_TARGET");
128 }
129 try cflags.append("-Wno-bitwise-conditional-parentheses");
130 try cflags.append("-Wno-visibility");
131 try cflags.append("-Wno-incompatible-pointer-types");
132
133 if (target.os.tag == .windows) {
134 try cflags.append("-Wno-dll-attribute-on-redeclaration");
135 }
136
137 c_source_files[i] = .{
138 .src_path = try comp.dirs.zig_lib.join(arena, &.{unwind_src}),
139 .extra_flags = cflags.items,
140 .owner = root_mod,
141 };
142 }
143
144 const misc_task: Compilation.MiscTask = .libunwind;
145
146 var sub_create_diag: Compilation.CreateDiagnostic = undefined;
147 const sub_compilation = Compilation.create(comp.gpa, arena, io, &sub_create_diag, .{
148 .dirs = comp.dirs.withoutLocalCache(),
149 .self_exe_path = comp.self_exe_path,
150 .config = config,
151 .root_mod = root_mod,
152 .cache_mode = .whole,
153 .root_name = root_name,
154 .main_mod = null,
155 .thread_pool = comp.thread_pool,
156 .libc_installation = comp.libc_installation,
157 .emit_bin = .yes_cache,
158 .function_sections = comp.function_sections,
159 .c_source_files = &c_source_files,
160 .verbose_cc = comp.verbose_cc,
161 .verbose_link = comp.verbose_link,
162 .verbose_air = comp.verbose_air,
163 .verbose_llvm_ir = comp.verbose_llvm_ir,
164 .verbose_llvm_bc = comp.verbose_llvm_bc,
165 .verbose_cimport = comp.verbose_cimport,
166 .verbose_llvm_cpu_features = comp.verbose_llvm_cpu_features,
167 .clang_passthrough_mode = comp.clang_passthrough_mode,
168 .skip_linker_dependencies = true,
169 }) catch |err| {
170 switch (err) {
171 else => comp.lockAndSetMiscFailure(misc_task, "unable to build {t}: create compilation failed: {t}", .{ misc_task, err }),
172 error.CreateFail => comp.lockAndSetMiscFailure(misc_task, "unable to build {t}: create compilation failed: {f}", .{ misc_task, sub_create_diag }),
173 }
174 return error.AlreadyReported;
175 };
176 defer sub_compilation.destroy();
177
178 comp.updateSubCompilation(sub_compilation, misc_task, prog_node) catch |err| switch (err) {
179 error.AlreadyReported => return error.AlreadyReported,
180 else => |e| {
181 comp.lockAndSetMiscFailure(misc_task, "unable to build {t}: compilation failed: {s}", .{ misc_task, @errorName(e) });
182 return error.AlreadyReported;
183 },
184 };
185
186 const crt_file = try sub_compilation.toCrtFile();
187 comp.queuePrelinkTaskMode(crt_file.full_object_path, &config);
188 assert(comp.libunwind_static_lib == null);
189 comp.libunwind_static_lib = crt_file;
190}
191
192const unwind_src_list = [_][]const u8{
193 "libunwind" ++ path.sep_str ++ "src" ++ path.sep_str ++ "libunwind.cpp",
194 "libunwind" ++ path.sep_str ++ "src" ++ path.sep_str ++ "Unwind-EHABI.cpp",
195 "libunwind" ++ path.sep_str ++ "src" ++ path.sep_str ++ "Unwind-seh.cpp",
196 "libunwind" ++ path.sep_str ++ "src" ++ path.sep_str ++ "UnwindLevel1.c",
197 "libunwind" ++ path.sep_str ++ "src" ++ path.sep_str ++ "UnwindLevel1-gcc-ext.c",
198 "libunwind" ++ path.sep_str ++ "src" ++ path.sep_str ++ "Unwind-sjlj.c",
199 "libunwind" ++ path.sep_str ++ "src" ++ path.sep_str ++ "Unwind-wasm.c",
200 "libunwind" ++ path.sep_str ++ "src" ++ path.sep_str ++ "UnwindRegistersRestore.S",
201 "libunwind" ++ path.sep_str ++ "src" ++ path.sep_str ++ "UnwindRegistersSave.S",
202 "libunwind" ++ path.sep_str ++ "src" ++ path.sep_str ++ "gcc_personality_v0.c",
203};