authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-12-15 14:22:24-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-01-01 17:51:19-07:00
loga1236b32f9a4114231312b1493dcbac308a2c055
tree6c5760b75396a392765f6c08755110c0df1065a5
parent2596f5d92593a5f7dad21d9a54f2d08f33639432

libcxx: update to new Compilation API


5 files changed, 114 insertions(+), 59 deletions(-)

src/Compilation/Config.zig+2
......@@ -13,6 +13,7 @@ any_non_single_threaded: bool,
1313/// and function calling convention depend on this global value, however, other
1414/// kinds of error tracing are omitted depending on the per-Module setting.
1515any_error_tracing: bool,
16any_sanitize_thread: bool,
1617pie: bool,
1718/// If this is true then linker code is responsible for making an LLVM IR
1819/// Module, outputting it to an object file, and then linking that together
......@@ -427,6 +428,7 @@ pub fn resolve(options: Options) !Config {
427428 .any_c_source_files = options.any_c_source_files,
428429 .any_non_single_threaded = options.any_non_single_threaded,
429430 .any_error_tracing = any_error_tracing,
431 .any_sanitize_thread = options.any_sanitize_thread,
430432 .root_error_tracing = root_error_tracing,
431433 .pie = pie,
432434 .lto = lto,
src/Module.zig+3-3
......@@ -4566,7 +4566,7 @@ pub fn analyzeFnBody(mod: *Module, func_index: InternPool.Index, arena: Allocato
45664566
45674567 // If we don't get an error return trace from a caller, create our own.
45684568 if (func.analysis(ip).calls_or_awaits_errorable_fn and
4569 mod.comp.config.error_tracing and
4569 mod.comp.config.any_error_tracing and
45704570 !sema.fn_ret_ty.isError(mod))
45714571 {
45724572 sema.setupErrorReturnTrace(&inner_block, last_arg_index) catch |err| switch (err) {
......@@ -5567,8 +5567,8 @@ pub const Feature = enum {
55675567};
55685568
55695569pub fn backendSupportsFeature(zcu: Module, feature: Feature) bool {
5570 const cpu_arch = zcu.root_mod.resolved_target.cpu.arch;
5571 const ofmt = zcu.root_mod.resolved_target.ofmt;
5570 const cpu_arch = zcu.root_mod.resolved_target.result.cpu.arch;
5571 const ofmt = zcu.root_mod.resolved_target.result.ofmt;
55725572 const use_llvm = zcu.comp.config.use_llvm;
55735573 return switch (feature) {
55745574 .panic_fn => ofmt == .c or use_llvm or cpu_arch == .x86_64,
src/Package/Module.zig+5
......@@ -102,6 +102,11 @@ pub const ResolvedTarget = struct {
102102
103103/// At least one of `parent` and `resolved_target` must be non-null.
104104pub fn create(arena: Allocator, options: CreateOptions) !*Package.Module {
105 if (options.inherited.sanitize_thread == true) assert(options.global.any_sanitize_thread);
106 if (options.inherited.single_threaded == false) assert(options.global.any_non_single_threaded);
107 if (options.inherited.unwind_tables == true) assert(options.global.any_unwind_tables);
108 if (options.inherited.error_tracing == true) assert(options.global.any_error_tracing);
109
105110 const resolved_target = options.inherited.resolved_target orelse options.parent.?.resolved_target;
106111 const target = resolved_target.result;
107112
src/libcxx.zig+104-53
......@@ -6,6 +6,7 @@ const target_util = @import("target.zig");
66const Compilation = @import("Compilation.zig");
77const build_options = @import("build_options");
88const trace = @import("tracy.zig").trace;
9const Module = @import("Package/Module.zig");
910
1011pub const AbiVersion = enum(u2) {
1112 @"1" = 1,
......@@ -115,7 +116,7 @@ pub fn buildLibCXX(comp: *Compilation, prog_node: *std.Progress.Node) !void {
115116 const root_name = "c++";
116117 const output_mode = .Lib;
117118 const link_mode = .Static;
118 const target = comp.getTarget();
119 const target = comp.root_mod.resolved_target.result;
119120 const basename = try std.zig.binNameAlloc(arena, .{
120121 .root_name = root_name,
121122 .target = target,
......@@ -226,46 +227,70 @@ pub fn buildLibCXX(comp: *Compilation, prog_node: *std.Progress.Node) !void {
226227 });
227228 }
228229
230 const optimize_mode = comp.compilerRtOptMode();
231 const strip = comp.compilerRtStrip();
232
233 const config = try Compilation.Config.resolve(.{
234 .output_mode = output_mode,
235 .link_mode = link_mode,
236 .resolved_target = comp.root_mod.resolved_target,
237 .is_test = false,
238 .have_zcu = false,
239 .emit_bin = true,
240 .root_optimize_mode = optimize_mode,
241 .root_strip = strip,
242 .link_libc = true,
243 .lto = comp.config.lto,
244 });
245
246 const root_mod = try Module.create(arena, .{
247 .global_cache_directory = comp.global_cache_directory,
248 .paths = .{
249 .root = .{ .root_dir = comp.zig_lib_directory },
250 .root_src_path = "",
251 },
252 .fully_qualified_name = "root",
253 .inherited = .{
254 .strip = strip,
255 .stack_check = false,
256 .stack_protector = 0,
257 .sanitize_c = false,
258 .sanitize_thread = comp.config.any_sanitize_thread,
259 .red_zone = comp.root_mod.red_zone,
260 .omit_frame_pointer = comp.root_mod.omit_frame_pointer,
261 .valgrind = false,
262 .optimize_mode = optimize_mode,
263 .structured_cfg = comp.root_mod.structured_cfg,
264 .pic = comp.root_mod.pic,
265 },
266 .global = config,
267 .cc_argv = &.{},
268 .parent = null,
269 .builtin_mod = null,
270 });
271
229272 const sub_compilation = try Compilation.create(comp.gpa, .{
230273 .local_cache_directory = comp.global_cache_directory,
231274 .global_cache_directory = comp.global_cache_directory,
232275 .zig_lib_directory = comp.zig_lib_directory,
276 .self_exe_path = comp.self_exe_path,
233277 .cache_mode = .whole,
234 .target = target,
278 .config = config,
279 .root_mod = root_mod,
235280 .root_name = root_name,
236 .main_mod = null,
237 .output_mode = output_mode,
238281 .thread_pool = comp.thread_pool,
239 .libc_installation = comp.bin_file.options.libc_installation,
282 .libc_installation = comp.libc_installation,
240283 .emit_bin = emit_bin,
241 .optimize_mode = comp.compilerRtOptMode(),
242 .link_mode = link_mode,
243 .want_sanitize_c = false,
244 .want_stack_check = false,
245 .want_stack_protector = 0,
246 .want_red_zone = comp.bin_file.options.red_zone,
247 .omit_frame_pointer = comp.bin_file.options.omit_frame_pointer,
248 .want_valgrind = false,
249 .want_tsan = comp.bin_file.options.tsan,
250 .want_pic = comp.bin_file.options.pic,
251 .want_pie = null,
252 .want_lto = comp.bin_file.options.lto,
253 .function_sections = comp.bin_file.options.function_sections,
254284 .emit_h = null,
255 .strip = comp.compilerRtStrip(),
256 .is_native_os = comp.bin_file.options.is_native_os,
257 .is_native_abi = comp.bin_file.options.is_native_abi,
258 .self_exe_path = comp.self_exe_path,
259285 .c_source_files = c_source_files.items,
260286 .verbose_cc = comp.verbose_cc,
261 .verbose_link = comp.bin_file.options.verbose_link,
287 .verbose_link = comp.verbose_link,
262288 .verbose_air = comp.verbose_air,
263289 .verbose_llvm_ir = comp.verbose_llvm_ir,
264290 .verbose_llvm_bc = comp.verbose_llvm_bc,
265291 .verbose_cimport = comp.verbose_cimport,
266292 .verbose_llvm_cpu_features = comp.verbose_llvm_cpu_features,
267293 .clang_passthrough_mode = comp.clang_passthrough_mode,
268 .link_libc = true,
269294 .skip_linker_dependencies = true,
270295 });
271296 defer sub_compilation.destroy();
......@@ -274,8 +299,8 @@ pub fn buildLibCXX(comp: *Compilation, prog_node: *std.Progress.Node) !void {
274299
275300 assert(comp.libcxx_static_lib == null);
276301 comp.libcxx_static_lib = Compilation.CRTFile{
277 .full_object_path = try sub_compilation.bin_file.options.emit.?.directory.join(comp.gpa, &[_][]const u8{
278 sub_compilation.bin_file.options.emit.?.sub_path,
302 .full_object_path = try sub_compilation.bin_file.?.emit.directory.join(comp.gpa, &.{
303 sub_compilation.bin_file.?.emit.sub_path,
279304 }),
280305 .lock = sub_compilation.bin_file.toOwnedLock(),
281306 };
......@@ -296,7 +321,7 @@ pub fn buildLibCXXABI(comp: *Compilation, prog_node: *std.Progress.Node) !void {
296321 const root_name = "c++abi";
297322 const output_mode = .Lib;
298323 const link_mode = .Static;
299 const target = comp.getTarget();
324 const target = comp.root_mod.resolved_target.result;
300325 const basename = try std.zig.binNameAlloc(arena, .{
301326 .root_name = root_name,
302327 .target = target,
......@@ -365,7 +390,6 @@ pub fn buildLibCXXABI(comp: *Compilation, prog_node: *std.Progress.Node) !void {
365390 }
366391 try cflags.append("-nostdinc++");
367392 try cflags.append("-fstrict-aliasing");
368 try cflags.append("-funwind-tables");
369393 try cflags.append("-std=c++20");
370394
371395 // These depend on only the zig lib directory file path, which is
......@@ -389,46 +413,73 @@ pub fn buildLibCXXABI(comp: *Compilation, prog_node: *std.Progress.Node) !void {
389413 });
390414 }
391415
416 const optimize_mode = comp.compilerRtOptMode();
417 const strip = comp.compilerRtStrip();
418 const unwind_tables = true;
419
420 const config = try Compilation.Config.resolve(.{
421 .output_mode = output_mode,
422 .link_mode = link_mode,
423 .resolved_target = comp.root_mod.resolved_target,
424 .is_test = false,
425 .have_zcu = false,
426 .emit_bin = true,
427 .root_optimize_mode = optimize_mode,
428 .root_strip = strip,
429 .link_libc = true,
430 .any_unwind_tables = unwind_tables,
431 .lto = comp.config.lto,
432 });
433
434 const root_mod = try Module.create(arena, .{
435 .global_cache_directory = comp.global_cache_directory,
436 .paths = .{
437 .root = .{ .root_dir = comp.zig_lib_directory },
438 .root_src_path = "",
439 },
440 .fully_qualified_name = "root",
441 .inherited = .{
442 .strip = strip,
443 .stack_check = false,
444 .stack_protector = 0,
445 .sanitize_c = false,
446 .sanitize_thread = comp.config.any_sanitize_thread,
447 .red_zone = comp.root_mod.red_zone,
448 .omit_frame_pointer = comp.root_mod.omit_frame_pointer,
449 .valgrind = false,
450 .optimize_mode = optimize_mode,
451 .structured_cfg = comp.root_mod.structured_cfg,
452 .unwind_tables = unwind_tables,
453 .pic = comp.root_mod.pic,
454 },
455 .global = config,
456 .cc_argv = &.{},
457 .parent = null,
458 .builtin_mod = null,
459 });
460
392461 const sub_compilation = try Compilation.create(comp.gpa, .{
393462 .local_cache_directory = comp.global_cache_directory,
394463 .global_cache_directory = comp.global_cache_directory,
395464 .zig_lib_directory = comp.zig_lib_directory,
465 .self_exe_path = comp.self_exe_path,
396466 .cache_mode = .whole,
397 .target = target,
467 .config = config,
468 .root_mod = root_mod,
398469 .root_name = root_name,
399 .main_mod = null,
400 .output_mode = output_mode,
401470 .thread_pool = comp.thread_pool,
402 .libc_installation = comp.bin_file.options.libc_installation,
471 .libc_installation = comp.libc_installation,
403472 .emit_bin = emit_bin,
404 .optimize_mode = comp.compilerRtOptMode(),
405 .link_mode = link_mode,
406 .want_sanitize_c = false,
407 .want_stack_check = false,
408 .want_stack_protector = 0,
409 .want_red_zone = comp.bin_file.options.red_zone,
410 .omit_frame_pointer = comp.bin_file.options.omit_frame_pointer,
411 .want_valgrind = false,
412 .want_tsan = comp.bin_file.options.tsan,
413 .want_pic = comp.bin_file.options.pic,
414 .want_pie = null,
415 .want_lto = comp.bin_file.options.lto,
416 .function_sections = comp.bin_file.options.function_sections,
417473 .emit_h = null,
418 .strip = comp.compilerRtStrip(),
419 .is_native_os = comp.bin_file.options.is_native_os,
420 .is_native_abi = comp.bin_file.options.is_native_abi,
421 .self_exe_path = comp.self_exe_path,
422474 .c_source_files = c_source_files.items,
423475 .verbose_cc = comp.verbose_cc,
424 .verbose_link = comp.bin_file.options.verbose_link,
476 .verbose_link = comp.verbose_link,
425477 .verbose_air = comp.verbose_air,
426478 .verbose_llvm_ir = comp.verbose_llvm_ir,
427479 .verbose_llvm_bc = comp.verbose_llvm_bc,
428480 .verbose_cimport = comp.verbose_cimport,
429481 .verbose_llvm_cpu_features = comp.verbose_llvm_cpu_features,
430482 .clang_passthrough_mode = comp.clang_passthrough_mode,
431 .link_libc = true,
432483 .skip_linker_dependencies = true,
433484 });
434485 defer sub_compilation.destroy();
......@@ -437,8 +488,8 @@ pub fn buildLibCXXABI(comp: *Compilation, prog_node: *std.Progress.Node) !void {
437488
438489 assert(comp.libcxxabi_static_lib == null);
439490 comp.libcxxabi_static_lib = Compilation.CRTFile{
440 .full_object_path = try sub_compilation.bin_file.options.emit.?.directory.join(comp.gpa, &[_][]const u8{
441 sub_compilation.bin_file.options.emit.?.sub_path,
491 .full_object_path = try sub_compilation.bin_file.?.emit.directory.join(comp.gpa, &[_][]const u8{
492 sub_compilation.bin_file.?.emit.sub_path,
442493 }),
443494 .lock = sub_compilation.bin_file.toOwnedLock(),
444495 };
src/musl.zig-3
......@@ -191,7 +191,6 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile, prog_node: *std.Progr
191191 return comp.build_crt_file("c", .Lib, .@"musl libc.a", prog_node, c_source_files.items);
192192 },
193193 .libc_so => {
194 const unwind_tables = false;
195194 const optimize_mode = comp.compilerRtOptMode();
196195 const strip = comp.compilerRtStrip();
197196 const config = try Compilation.Config.resolve(.{
......@@ -204,7 +203,6 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile, prog_node: *std.Progr
204203 .root_optimize_mode = optimize_mode,
205204 .root_strip = strip,
206205 .link_libc = false,
207 .any_unwind_tables = unwind_tables,
208206 });
209207
210208 const target = comp.root_mod.resolved_target.result;
......@@ -232,7 +230,6 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile, prog_node: *std.Progr
232230 .red_zone = comp.root_mod.red_zone,
233231 .omit_frame_pointer = comp.root_mod.omit_frame_pointer,
234232 .valgrind = false,
235 .unwind_tables = unwind_tables,
236233 .optimize_mode = optimize_mode,
237234 .structured_cfg = comp.root_mod.structured_cfg,
238235 },