Commit f79824f946

Andrew Kelley <andrew@ziglang.org>
2022-09-13 00:56:31
libcxx: define _LIBCPP_ABI_VERSION and _LIBCPP_ABI_NAMESPACE
The changes from https://reviews.llvm.org/D119173 mean that __config no longer defaults the libc++ ABI to 1, relying on external configuration. This means Zig must provide the external configuration. This fixes static libraries built with zig with -lc++ to have the standard __1 namespace prefix, which had previously regressed in the llvm15 branch.
1 parent 6e3bbba
Changed files (2)
src/Compilation.zig
@@ -154,6 +154,8 @@ owned_link_dir: ?std.fs.Dir,
 /// Don't use this for anything other than stage1 compatibility.
 color: Color = .auto,
 
+libcxx_abi_version: libcxx.AbiVersion = libcxx.AbiVersion.default,
+
 /// This mutex guards all `Compilation` mutable state.
 mutex: std.Thread.Mutex = .{},
 
@@ -950,6 +952,7 @@ pub const InitOptions = struct {
     headerpad_max_install_names: bool = false,
     /// (Darwin) remove dylibs that are unreachable by the entry point or exported symbols
     dead_strip_dylibs: bool = false,
+    libcxx_abi_version: libcxx.AbiVersion = libcxx.AbiVersion.default,
 };
 
 fn addPackageTableToCacheHash(
@@ -1843,6 +1846,7 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
             .test_evented_io = options.test_evented_io,
             .debug_compiler_runtime_libs = options.debug_compiler_runtime_libs,
             .debug_compile_errors = options.debug_compile_errors,
+            .libcxx_abi_version = options.libcxx_abi_version,
         };
         break :comp comp;
     };
@@ -4026,6 +4030,13 @@ pub fn addCCArgs(
         if (comp.bin_file.options.single_threaded) {
             try argv.append("-D_LIBCPP_HAS_NO_THREADS");
         }
+
+        try argv.append(try std.fmt.allocPrint(arena, "-D_LIBCPP_ABI_VERSION={d}", .{
+            @enumToInt(comp.libcxx_abi_version),
+        }));
+        try argv.append(try std.fmt.allocPrint(arena, "-D_LIBCPP_ABI_NAMESPACE=__{d}", .{
+            @enumToInt(comp.libcxx_abi_version),
+        }));
     }
 
     if (comp.bin_file.options.link_libunwind) {
src/libcxx.zig
@@ -7,6 +7,13 @@ const Compilation = @import("Compilation.zig");
 const build_options = @import("build_options");
 const trace = @import("tracy.zig").trace;
 
+pub const AbiVersion = enum(u2) {
+    @"1" = 1,
+    @"2" = 2,
+
+    pub const default: AbiVersion = .@"1";
+};
+
 const libcxxabi_files = [_][]const u8{
     "src/abort_message.cpp",
     "src/cxa_aux_runtime.cpp",
@@ -120,6 +127,12 @@ pub fn buildLibCXX(comp: *Compilation) !void {
     const cxxabi_include_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{ "libcxxabi", "include" });
     const cxx_include_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{ "libcxx", "include" });
     const cxx_src_include_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{ "libcxx", "src" });
+    const abi_version_arg = try std.fmt.allocPrint(arena, "-D_LIBCPP_ABI_VERSION={d}", .{
+        @enumToInt(comp.libcxx_abi_version),
+    });
+    const abi_namespace_arg = try std.fmt.allocPrint(arena, "-D_LIBCPP_ABI_NAMESPACE=__{d}", .{
+        @enumToInt(comp.libcxx_abi_version),
+    });
     var c_source_files = try std.ArrayList(Compilation.CSourceFile).initCapacity(arena, libcxx_files.len);
 
     for (libcxx_files) |cxx_src| {
@@ -152,6 +165,10 @@ pub fn buildLibCXX(comp: *Compilation) !void {
         try cflags.append("-D_LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS");
         try cflags.append("-D_LIBCPP_DISABLE_NEW_DELETE_DEFINITIONS");
         try cflags.append("-D_LIBCPP_HAS_NO_VENDOR_AVAILABILITY_ANNOTATIONS");
+
+        try cflags.append(abi_version_arg);
+        try cflags.append(abi_namespace_arg);
+
         try cflags.append("-fvisibility=hidden");
         try cflags.append("-fvisibility-inlines-hidden");
 
@@ -277,6 +294,12 @@ pub fn buildLibCXXABI(comp: *Compilation) !void {
     const cxxabi_include_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{ "libcxxabi", "include" });
     const cxx_include_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{ "libcxx", "include" });
     const cxx_src_include_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{ "libcxx", "src" });
+    const abi_version_arg = try std.fmt.allocPrint(arena, "-D_LIBCPP_ABI_VERSION={d}", .{
+        @enumToInt(comp.libcxx_abi_version),
+    });
+    const abi_namespace_arg = try std.fmt.allocPrint(arena, "-D_LIBCPP_ABI_NAMESPACE=__{d}", .{
+        @enumToInt(comp.libcxx_abi_version),
+    });
     var c_source_files = try std.ArrayList(Compilation.CSourceFile).initCapacity(arena, libcxxabi_files.len);
 
     for (libcxxabi_files) |cxxabi_src| {
@@ -306,6 +329,10 @@ pub fn buildLibCXXABI(comp: *Compilation) !void {
         try cflags.append("-D_LIBCXXABI_BUILDING_LIBRARY");
         try cflags.append("-D_LIBCXXABI_DISABLE_VISIBILITY_ANNOTATIONS");
         try cflags.append("-D_LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS");
+
+        try cflags.append(abi_version_arg);
+        try cflags.append(abi_namespace_arg);
+
         try cflags.append("-fvisibility=hidden");
         try cflags.append("-fvisibility-inlines-hidden");