authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-04-29 15:42:23-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-05-08 19:37:29-07:00
log2c317b2cbafe4f91fca66ee20f45197bbbd26353
tree6bece787765a43382efc03849234ef0e9ec9670c
parentb018f2bae189cbcf2476b4147b108b815af149bc

libcxx, libtsan, libunwind: fix error reporting

use a consistent error set to avoid failure when bootstrapping from zig1

3 files changed, 139 insertions(+), 31 deletions(-)

src/libcxx.zig+84-16
......@@ -107,7 +107,13 @@ const libcxx_files = [_][]const u8{
107107 "src/verbose_abort.cpp",
108108};
109109
110pub fn buildLibCXX(comp: *Compilation, prog_node: *std.Progress.Node) !void {
110pub const BuildError = error{
111 OutOfMemory,
112 SubCompilationFailed,
113 ZigCompilerNotBuiltWithLLVMExtensions,
114};
115
116pub fn buildLibCXX(comp: *Compilation, prog_node: *std.Progress.Node) BuildError!void {
111117 if (!build_options.have_llvm) {
112118 return error.ZigCompilerNotBuiltWithLLVMExtensions;
113119 }
......@@ -148,7 +154,7 @@ pub fn buildLibCXX(comp: *Compilation, prog_node: *std.Progress.Node) !void {
148154 const optimize_mode = comp.compilerRtOptMode();
149155 const strip = comp.compilerRtStrip();
150156
151 const config = try Compilation.Config.resolve(.{
157 const config = Compilation.Config.resolve(.{
152158 .output_mode = output_mode,
153159 .link_mode = link_mode,
154160 .resolved_target = comp.root_mod.resolved_target,
......@@ -160,9 +166,16 @@ pub fn buildLibCXX(comp: *Compilation, prog_node: *std.Progress.Node) !void {
160166 .link_libc = true,
161167 .lto = comp.config.lto,
162168 .any_sanitize_thread = comp.config.any_sanitize_thread,
163 });
169 }) catch |err| {
170 comp.setMiscFailure(
171 .libcxx,
172 "unable to build libc++: resolving configuration failed: {s}",
173 .{@errorName(err)},
174 );
175 return error.SubCompilationFailed;
176 };
164177
165 const root_mod = try Module.create(arena, .{
178 const root_mod = Module.create(arena, .{
166179 .global_cache_directory = comp.global_cache_directory,
167180 .paths = .{
168181 .root = .{ .root_dir = comp.zig_lib_directory },
......@@ -188,7 +201,14 @@ pub fn buildLibCXX(comp: *Compilation, prog_node: *std.Progress.Node) !void {
188201 .parent = null,
189202 .builtin_mod = null,
190203 .builtin_modules = null, // there is only one module in this compilation
191 });
204 }) catch |err| {
205 comp.setMiscFailure(
206 .libcxx,
207 "unable to build libc++: creating module failed: {s}",
208 .{@errorName(err)},
209 );
210 return error.SubCompilationFailed;
211 };
192212
193213 var c_source_files = try std.ArrayList(Compilation.CSourceFile).initCapacity(arena, libcxx_files.len);
194214
......@@ -288,7 +308,7 @@ pub fn buildLibCXX(comp: *Compilation, prog_node: *std.Progress.Node) !void {
288308 });
289309 }
290310
291 const sub_compilation = try Compilation.create(comp.gpa, arena, .{
311 const sub_compilation = Compilation.create(comp.gpa, arena, .{
292312 .local_cache_directory = comp.global_cache_directory,
293313 .global_cache_directory = comp.global_cache_directory,
294314 .zig_lib_directory = comp.zig_lib_directory,
......@@ -311,16 +331,33 @@ pub fn buildLibCXX(comp: *Compilation, prog_node: *std.Progress.Node) !void {
311331 .verbose_llvm_cpu_features = comp.verbose_llvm_cpu_features,
312332 .clang_passthrough_mode = comp.clang_passthrough_mode,
313333 .skip_linker_dependencies = true,
314 });
334 }) catch |err| {
335 comp.setMiscFailure(
336 .libcxx,
337 "unable to build libc++: create compilation failed: {s}",
338 .{@errorName(err)},
339 );
340 return error.SubCompilationFailed;
341 };
315342 defer sub_compilation.destroy();
316343
317 try comp.updateSubCompilation(sub_compilation, .libcxx, prog_node);
344 comp.updateSubCompilation(sub_compilation, .libcxx, prog_node) catch |err| switch (err) {
345 error.SubCompilationFailed => return error.SubCompilationFailed,
346 else => |e| {
347 comp.setMiscFailure(
348 .libcxx,
349 "unable to build libc++: compilation failed: {s}",
350 .{@errorName(e)},
351 );
352 return error.SubCompilationFailed;
353 },
354 };
318355
319356 assert(comp.libcxx_static_lib == null);
320357 comp.libcxx_static_lib = try sub_compilation.toCrtFile();
321358}
322359
323pub fn buildLibCXXABI(comp: *Compilation, prog_node: *std.Progress.Node) !void {
360pub fn buildLibCXXABI(comp: *Compilation, prog_node: *std.Progress.Node) BuildError!void {
324361 if (!build_options.have_llvm) {
325362 return error.ZigCompilerNotBuiltWithLLVMExtensions;
326363 }
......@@ -362,7 +399,7 @@ pub fn buildLibCXXABI(comp: *Compilation, prog_node: *std.Progress.Node) !void {
362399 const strip = comp.compilerRtStrip();
363400 const unwind_tables = true;
364401
365 const config = try Compilation.Config.resolve(.{
402 const config = Compilation.Config.resolve(.{
366403 .output_mode = output_mode,
367404 .link_mode = link_mode,
368405 .resolved_target = comp.root_mod.resolved_target,
......@@ -375,9 +412,16 @@ pub fn buildLibCXXABI(comp: *Compilation, prog_node: *std.Progress.Node) !void {
375412 .any_unwind_tables = unwind_tables,
376413 .lto = comp.config.lto,
377414 .any_sanitize_thread = comp.config.any_sanitize_thread,
378 });
415 }) catch |err| {
416 comp.setMiscFailure(
417 .libcxxabi,
418 "unable to build libc++abi: resolving configuration failed: {s}",
419 .{@errorName(err)},
420 );
421 return error.SubCompilationFailed;
422 };
379423
380 const root_mod = try Module.create(arena, .{
424 const root_mod = Module.create(arena, .{
381425 .global_cache_directory = comp.global_cache_directory,
382426 .paths = .{
383427 .root = .{ .root_dir = comp.zig_lib_directory },
......@@ -404,7 +448,14 @@ pub fn buildLibCXXABI(comp: *Compilation, prog_node: *std.Progress.Node) !void {
404448 .parent = null,
405449 .builtin_mod = null,
406450 .builtin_modules = null, // there is only one module in this compilation
407 });
451 }) catch |err| {
452 comp.setMiscFailure(
453 .libcxxabi,
454 "unable to build libc++abi: creating module failed: {s}",
455 .{@errorName(err)},
456 );
457 return error.SubCompilationFailed;
458 };
408459
409460 var c_source_files = try std.ArrayList(Compilation.CSourceFile).initCapacity(arena, libcxxabi_files.len);
410461
......@@ -487,7 +538,7 @@ pub fn buildLibCXXABI(comp: *Compilation, prog_node: *std.Progress.Node) !void {
487538 });
488539 }
489540
490 const sub_compilation = try Compilation.create(comp.gpa, arena, .{
541 const sub_compilation = Compilation.create(comp.gpa, arena, .{
491542 .local_cache_directory = comp.global_cache_directory,
492543 .global_cache_directory = comp.global_cache_directory,
493544 .zig_lib_directory = comp.zig_lib_directory,
......@@ -510,10 +561,27 @@ pub fn buildLibCXXABI(comp: *Compilation, prog_node: *std.Progress.Node) !void {
510561 .verbose_llvm_cpu_features = comp.verbose_llvm_cpu_features,
511562 .clang_passthrough_mode = comp.clang_passthrough_mode,
512563 .skip_linker_dependencies = true,
513 });
564 }) catch |err| {
565 comp.setMiscFailure(
566 .libcxxabi,
567 "unable to build libc++abi: create compilation failed: {s}",
568 .{@errorName(err)},
569 );
570 return error.SubCompilationFailed;
571 };
514572 defer sub_compilation.destroy();
515573
516 try comp.updateSubCompilation(sub_compilation, .libcxxabi, prog_node);
574 comp.updateSubCompilation(sub_compilation, .libcxxabi, prog_node) catch |err| switch (err) {
575 error.SubCompilationFailed => return error.SubCompilationFailed,
576 else => |e| {
577 comp.setMiscFailure(
578 .libcxxabi,
579 "unable to build libc++abi: compilation failed: {s}",
580 .{@errorName(e)},
581 );
582 return error.SubCompilationFailed;
583 },
584 };
517585
518586 assert(comp.libcxxabi_static_lib == null);
519587 comp.libcxxabi_static_lib = try sub_compilation.toCrtFile();
src/libtsan.zig+10-7
......@@ -305,13 +305,16 @@ pub fn buildTsan(comp: *Compilation, prog_node: *std.Progress.Node) BuildError!v
305305 };
306306 defer sub_compilation.destroy();
307307
308 comp.updateSubCompilation(sub_compilation, .libtsan, prog_node) catch |err| {
309 comp.setMiscFailure(
310 .libtsan,
311 "unable to build thread sanitizer runtime: compilation failed: {s}",
312 .{@errorName(err)},
313 );
314 return error.SubCompilationFailed;
308 comp.updateSubCompilation(sub_compilation, .libtsan, prog_node) catch |err| switch (err) {
309 error.SubCompilationFailed => return error.SubCompilationFailed,
310 else => |e| {
311 comp.setMiscFailure(
312 .libtsan,
313 "unable to build thread sanitizer runtime: compilation failed: {s}",
314 .{@errorName(e)},
315 );
316 return error.SubCompilationFailed;
317 },
315318 };
316319
317320 assert(comp.tsan_static_lib == null);
src/libunwind.zig+45-8
......@@ -8,7 +8,13 @@ const Module = @import("Package/Module.zig");
88const build_options = @import("build_options");
99const trace = @import("tracy.zig").trace;
1010
11pub fn buildStaticLib(comp: *Compilation, prog_node: *std.Progress.Node) !void {
11pub const BuildError = error{
12 OutOfMemory,
13 SubCompilationFailed,
14 ZigCompilerNotBuiltWithLLVMExtensions,
15};
16
17pub fn buildStaticLib(comp: *Compilation, prog_node: *std.Progress.Node) BuildError!void {
1218 if (!build_options.have_llvm) {
1319 return error.ZigCompilerNotBuiltWithLLVMExtensions;
1420 }
......@@ -21,7 +27,7 @@ pub fn buildStaticLib(comp: *Compilation, prog_node: *std.Progress.Node) !void {
2127 const arena = arena_allocator.allocator();
2228
2329 const output_mode = .Lib;
24 const config = try Compilation.Config.resolve(.{
30 const config = Compilation.Config.resolve(.{
2531 .output_mode = .Lib,
2632 .resolved_target = comp.root_mod.resolved_target,
2733 .is_test = false,
......@@ -32,8 +38,15 @@ pub fn buildStaticLib(comp: *Compilation, prog_node: *std.Progress.Node) !void {
3238 .link_libc = true,
3339 // Disable LTO to avoid https://github.com/llvm/llvm-project/issues/56825
3440 .lto = false,
35 });
36 const root_mod = try Module.create(arena, .{
41 }) catch |err| {
42 comp.setMiscFailure(
43 .libunwind,
44 "unable to build libunwind: resolving configuration failed: {s}",
45 .{@errorName(err)},
46 );
47 return error.SubCompilationFailed;
48 };
49 const root_mod = Module.create(arena, .{
3750 .global_cache_directory = comp.global_cache_directory,
3851 .paths = .{
3952 .root = .{ .root_dir = comp.zig_lib_directory },
......@@ -59,7 +72,14 @@ pub fn buildStaticLib(comp: *Compilation, prog_node: *std.Progress.Node) !void {
5972 .parent = null,
6073 .builtin_mod = null,
6174 .builtin_modules = null, // there is only one module in this compilation
62 });
75 }) catch |err| {
76 comp.setMiscFailure(
77 .libunwind,
78 "unable to build libunwind: creating module failed: {s}",
79 .{@errorName(err)},
80 );
81 return error.SubCompilationFailed;
82 };
6383
6484 const root_name = "unwind";
6585 const link_mode = .static;
......@@ -124,7 +144,7 @@ pub fn buildStaticLib(comp: *Compilation, prog_node: *std.Progress.Node) !void {
124144 .owner = root_mod,
125145 };
126146 }
127 const sub_compilation = try Compilation.create(comp.gpa, arena, .{
147 const sub_compilation = Compilation.create(comp.gpa, arena, .{
128148 .self_exe_path = comp.self_exe_path,
129149 .local_cache_directory = comp.global_cache_directory,
130150 .global_cache_directory = comp.global_cache_directory,
......@@ -148,10 +168,27 @@ pub fn buildStaticLib(comp: *Compilation, prog_node: *std.Progress.Node) !void {
148168 .verbose_llvm_cpu_features = comp.verbose_llvm_cpu_features,
149169 .clang_passthrough_mode = comp.clang_passthrough_mode,
150170 .skip_linker_dependencies = true,
151 });
171 }) catch |err| {
172 comp.setMiscFailure(
173 .libunwind,
174 "unable to build libunwind: create compilation failed: {s}",
175 .{@errorName(err)},
176 );
177 return error.SubCompilationFailed;
178 };
152179 defer sub_compilation.destroy();
153180
154 try comp.updateSubCompilation(sub_compilation, .libunwind, prog_node);
181 comp.updateSubCompilation(sub_compilation, .libunwind, prog_node) catch |err| switch (err) {
182 error.SubCompilationFailed => return error.SubCompilationFailed,
183 else => |e| {
184 comp.setMiscFailure(
185 .libunwind,
186 "unable to build libunwind: compilation failed: {s}",
187 .{@errorName(e)},
188 );
189 return error.SubCompilationFailed;
190 },
191 };
155192
156193 assert(comp.libunwind_static_lib == null);
157194 comp.libunwind_static_lib = try sub_compilation.toCrtFile();