From a1236b32f9a4114231312b1493dcbac308a2c055 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Fri, 15 Dec 2023 14:22:24 -0700 Subject: [PATCH] libcxx: update to new Compilation API --- src/Compilation/Config.zig | 2 + src/Module.zig | 6 +- src/Package/Module.zig | 5 ++ src/libcxx.zig | 173 ++++++++++++++++++++++++------------- src/musl.zig | 3 - 5 files changed, 122 insertions(+), 67 deletions(-) diff --git a/src/Compilation/Config.zig b/src/Compilation/Config.zig index efe314095c..44ace55ae4 100644 --- a/src/Compilation/Config.zig +++ b/src/Compilation/Config.zig @@ -13,6 +13,7 @@ any_non_single_threaded: bool, /// and function calling convention depend on this global value, however, other /// kinds of error tracing are omitted depending on the per-Module setting. any_error_tracing: bool, +any_sanitize_thread: bool, pie: bool, /// If this is true then linker code is responsible for making an LLVM IR /// Module, outputting it to an object file, and then linking that together @@ -427,6 +428,7 @@ pub fn resolve(options: Options) !Config { .any_c_source_files = options.any_c_source_files, .any_non_single_threaded = options.any_non_single_threaded, .any_error_tracing = any_error_tracing, + .any_sanitize_thread = options.any_sanitize_thread, .root_error_tracing = root_error_tracing, .pie = pie, .lto = lto, diff --git a/src/Module.zig b/src/Module.zig index 0645b6f04f..571301c8e6 100644 --- a/src/Module.zig +++ b/src/Module.zig @@ -4566,7 +4566,7 @@ pub fn analyzeFnBody(mod: *Module, func_index: InternPool.Index, arena: Allocato // If we don't get an error return trace from a caller, create our own. if (func.analysis(ip).calls_or_awaits_errorable_fn and - mod.comp.config.error_tracing and + mod.comp.config.any_error_tracing and !sema.fn_ret_ty.isError(mod)) { sema.setupErrorReturnTrace(&inner_block, last_arg_index) catch |err| switch (err) { @@ -5567,8 +5567,8 @@ pub const Feature = enum { }; pub fn backendSupportsFeature(zcu: Module, feature: Feature) bool { - const cpu_arch = zcu.root_mod.resolved_target.cpu.arch; - const ofmt = zcu.root_mod.resolved_target.ofmt; + const cpu_arch = zcu.root_mod.resolved_target.result.cpu.arch; + const ofmt = zcu.root_mod.resolved_target.result.ofmt; const use_llvm = zcu.comp.config.use_llvm; return switch (feature) { .panic_fn => ofmt == .c or use_llvm or cpu_arch == .x86_64, diff --git a/src/Package/Module.zig b/src/Package/Module.zig index 5e786a6fa2..4ce0f0a856 100644 --- a/src/Package/Module.zig +++ b/src/Package/Module.zig @@ -102,6 +102,11 @@ pub const ResolvedTarget = struct { /// At least one of `parent` and `resolved_target` must be non-null. pub fn create(arena: Allocator, options: CreateOptions) !*Package.Module { + if (options.inherited.sanitize_thread == true) assert(options.global.any_sanitize_thread); + if (options.inherited.single_threaded == false) assert(options.global.any_non_single_threaded); + if (options.inherited.unwind_tables == true) assert(options.global.any_unwind_tables); + if (options.inherited.error_tracing == true) assert(options.global.any_error_tracing); + const resolved_target = options.inherited.resolved_target orelse options.parent.?.resolved_target; const target = resolved_target.result; diff --git a/src/libcxx.zig b/src/libcxx.zig index 8ae7db80f1..1f78b867a3 100644 --- a/src/libcxx.zig +++ b/src/libcxx.zig @@ -6,6 +6,7 @@ const target_util = @import("target.zig"); const Compilation = @import("Compilation.zig"); const build_options = @import("build_options"); const trace = @import("tracy.zig").trace; +const Module = @import("Package/Module.zig"); pub const AbiVersion = enum(u2) { @"1" = 1, @@ -115,7 +116,7 @@ pub fn buildLibCXX(comp: *Compilation, prog_node: *std.Progress.Node) !void { const root_name = "c++"; const output_mode = .Lib; const link_mode = .Static; - const target = comp.getTarget(); + const target = comp.root_mod.resolved_target.result; const basename = try std.zig.binNameAlloc(arena, .{ .root_name = root_name, .target = target, @@ -226,46 +227,70 @@ pub fn buildLibCXX(comp: *Compilation, prog_node: *std.Progress.Node) !void { }); } + const optimize_mode = comp.compilerRtOptMode(); + const strip = comp.compilerRtStrip(); + + const config = try Compilation.Config.resolve(.{ + .output_mode = output_mode, + .link_mode = link_mode, + .resolved_target = comp.root_mod.resolved_target, + .is_test = false, + .have_zcu = false, + .emit_bin = true, + .root_optimize_mode = optimize_mode, + .root_strip = strip, + .link_libc = true, + .lto = comp.config.lto, + }); + + const root_mod = try Module.create(arena, .{ + .global_cache_directory = comp.global_cache_directory, + .paths = .{ + .root = .{ .root_dir = comp.zig_lib_directory }, + .root_src_path = "", + }, + .fully_qualified_name = "root", + .inherited = .{ + .strip = strip, + .stack_check = false, + .stack_protector = 0, + .sanitize_c = false, + .sanitize_thread = comp.config.any_sanitize_thread, + .red_zone = comp.root_mod.red_zone, + .omit_frame_pointer = comp.root_mod.omit_frame_pointer, + .valgrind = false, + .optimize_mode = optimize_mode, + .structured_cfg = comp.root_mod.structured_cfg, + .pic = comp.root_mod.pic, + }, + .global = config, + .cc_argv = &.{}, + .parent = null, + .builtin_mod = null, + }); + const sub_compilation = try Compilation.create(comp.gpa, .{ .local_cache_directory = comp.global_cache_directory, .global_cache_directory = comp.global_cache_directory, .zig_lib_directory = comp.zig_lib_directory, - .cache_mode = .whole, - .target = target, - .root_name = root_name, - .main_mod = null, - .output_mode = output_mode, - .thread_pool = comp.thread_pool, - .libc_installation = comp.bin_file.options.libc_installation, - .emit_bin = emit_bin, - .optimize_mode = comp.compilerRtOptMode(), - .link_mode = link_mode, - .want_sanitize_c = false, - .want_stack_check = false, - .want_stack_protector = 0, - .want_red_zone = comp.bin_file.options.red_zone, - .omit_frame_pointer = comp.bin_file.options.omit_frame_pointer, - .want_valgrind = false, - .want_tsan = comp.bin_file.options.tsan, - .want_pic = comp.bin_file.options.pic, - .want_pie = null, - .want_lto = comp.bin_file.options.lto, - .function_sections = comp.bin_file.options.function_sections, - .emit_h = null, - .strip = comp.compilerRtStrip(), - .is_native_os = comp.bin_file.options.is_native_os, - .is_native_abi = comp.bin_file.options.is_native_abi, .self_exe_path = comp.self_exe_path, + .cache_mode = .whole, + .config = config, + .root_mod = root_mod, + .root_name = root_name, + .thread_pool = comp.thread_pool, + .libc_installation = comp.libc_installation, + .emit_bin = emit_bin, + .emit_h = null, .c_source_files = c_source_files.items, .verbose_cc = comp.verbose_cc, - .verbose_link = comp.bin_file.options.verbose_link, + .verbose_link = comp.verbose_link, .verbose_air = comp.verbose_air, .verbose_llvm_ir = comp.verbose_llvm_ir, .verbose_llvm_bc = comp.verbose_llvm_bc, .verbose_cimport = comp.verbose_cimport, .verbose_llvm_cpu_features = comp.verbose_llvm_cpu_features, .clang_passthrough_mode = comp.clang_passthrough_mode, - .link_libc = true, .skip_linker_dependencies = true, }); defer sub_compilation.destroy(); @@ -274,8 +299,8 @@ pub fn buildLibCXX(comp: *Compilation, prog_node: *std.Progress.Node) !void { assert(comp.libcxx_static_lib == null); comp.libcxx_static_lib = Compilation.CRTFile{ - .full_object_path = try sub_compilation.bin_file.options.emit.?.directory.join(comp.gpa, &[_][]const u8{ - sub_compilation.bin_file.options.emit.?.sub_path, + .full_object_path = try sub_compilation.bin_file.?.emit.directory.join(comp.gpa, &.{ + sub_compilation.bin_file.?.emit.sub_path, }), .lock = sub_compilation.bin_file.toOwnedLock(), }; @@ -296,7 +321,7 @@ pub fn buildLibCXXABI(comp: *Compilation, prog_node: *std.Progress.Node) !void { const root_name = "c++abi"; const output_mode = .Lib; const link_mode = .Static; - const target = comp.getTarget(); + const target = comp.root_mod.resolved_target.result; const basename = try std.zig.binNameAlloc(arena, .{ .root_name = root_name, .target = target, @@ -365,7 +390,6 @@ pub fn buildLibCXXABI(comp: *Compilation, prog_node: *std.Progress.Node) !void { } try cflags.append("-nostdinc++"); try cflags.append("-fstrict-aliasing"); - try cflags.append("-funwind-tables"); try cflags.append("-std=c++20"); // 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 { }); } + const optimize_mode = comp.compilerRtOptMode(); + const strip = comp.compilerRtStrip(); + const unwind_tables = true; + + const config = try Compilation.Config.resolve(.{ + .output_mode = output_mode, + .link_mode = link_mode, + .resolved_target = comp.root_mod.resolved_target, + .is_test = false, + .have_zcu = false, + .emit_bin = true, + .root_optimize_mode = optimize_mode, + .root_strip = strip, + .link_libc = true, + .any_unwind_tables = unwind_tables, + .lto = comp.config.lto, + }); + + const root_mod = try Module.create(arena, .{ + .global_cache_directory = comp.global_cache_directory, + .paths = .{ + .root = .{ .root_dir = comp.zig_lib_directory }, + .root_src_path = "", + }, + .fully_qualified_name = "root", + .inherited = .{ + .strip = strip, + .stack_check = false, + .stack_protector = 0, + .sanitize_c = false, + .sanitize_thread = comp.config.any_sanitize_thread, + .red_zone = comp.root_mod.red_zone, + .omit_frame_pointer = comp.root_mod.omit_frame_pointer, + .valgrind = false, + .optimize_mode = optimize_mode, + .structured_cfg = comp.root_mod.structured_cfg, + .unwind_tables = unwind_tables, + .pic = comp.root_mod.pic, + }, + .global = config, + .cc_argv = &.{}, + .parent = null, + .builtin_mod = null, + }); + const sub_compilation = try Compilation.create(comp.gpa, .{ .local_cache_directory = comp.global_cache_directory, .global_cache_directory = comp.global_cache_directory, .zig_lib_directory = comp.zig_lib_directory, - .cache_mode = .whole, - .target = target, - .root_name = root_name, - .main_mod = null, - .output_mode = output_mode, - .thread_pool = comp.thread_pool, - .libc_installation = comp.bin_file.options.libc_installation, - .emit_bin = emit_bin, - .optimize_mode = comp.compilerRtOptMode(), - .link_mode = link_mode, - .want_sanitize_c = false, - .want_stack_check = false, - .want_stack_protector = 0, - .want_red_zone = comp.bin_file.options.red_zone, - .omit_frame_pointer = comp.bin_file.options.omit_frame_pointer, - .want_valgrind = false, - .want_tsan = comp.bin_file.options.tsan, - .want_pic = comp.bin_file.options.pic, - .want_pie = null, - .want_lto = comp.bin_file.options.lto, - .function_sections = comp.bin_file.options.function_sections, - .emit_h = null, - .strip = comp.compilerRtStrip(), - .is_native_os = comp.bin_file.options.is_native_os, - .is_native_abi = comp.bin_file.options.is_native_abi, .self_exe_path = comp.self_exe_path, + .cache_mode = .whole, + .config = config, + .root_mod = root_mod, + .root_name = root_name, + .thread_pool = comp.thread_pool, + .libc_installation = comp.libc_installation, + .emit_bin = emit_bin, + .emit_h = null, .c_source_files = c_source_files.items, .verbose_cc = comp.verbose_cc, - .verbose_link = comp.bin_file.options.verbose_link, + .verbose_link = comp.verbose_link, .verbose_air = comp.verbose_air, .verbose_llvm_ir = comp.verbose_llvm_ir, .verbose_llvm_bc = comp.verbose_llvm_bc, .verbose_cimport = comp.verbose_cimport, .verbose_llvm_cpu_features = comp.verbose_llvm_cpu_features, .clang_passthrough_mode = comp.clang_passthrough_mode, - .link_libc = true, .skip_linker_dependencies = true, }); defer sub_compilation.destroy(); @@ -437,8 +488,8 @@ pub fn buildLibCXXABI(comp: *Compilation, prog_node: *std.Progress.Node) !void { assert(comp.libcxxabi_static_lib == null); comp.libcxxabi_static_lib = Compilation.CRTFile{ - .full_object_path = try sub_compilation.bin_file.options.emit.?.directory.join(comp.gpa, &[_][]const u8{ - sub_compilation.bin_file.options.emit.?.sub_path, + .full_object_path = try sub_compilation.bin_file.?.emit.directory.join(comp.gpa, &[_][]const u8{ + sub_compilation.bin_file.?.emit.sub_path, }), .lock = sub_compilation.bin_file.toOwnedLock(), }; diff --git a/src/musl.zig b/src/musl.zig index e29d348d1f..249753d415 100644 --- a/src/musl.zig +++ b/src/musl.zig @@ -191,7 +191,6 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile, prog_node: *std.Progr return comp.build_crt_file("c", .Lib, .@"musl libc.a", prog_node, c_source_files.items); }, .libc_so => { - const unwind_tables = false; const optimize_mode = comp.compilerRtOptMode(); const strip = comp.compilerRtStrip(); const config = try Compilation.Config.resolve(.{ @@ -204,7 +203,6 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile, prog_node: *std.Progr .root_optimize_mode = optimize_mode, .root_strip = strip, .link_libc = false, - .any_unwind_tables = unwind_tables, }); const target = comp.root_mod.resolved_target.result; @@ -232,7 +230,6 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile, prog_node: *std.Progr .red_zone = comp.root_mod.red_zone, .omit_frame_pointer = comp.root_mod.omit_frame_pointer, .valgrind = false, - .unwind_tables = unwind_tables, .optimize_mode = optimize_mode, .structured_cfg = comp.root_mod.structured_cfg, },