Commit 2cd536d7e8

Michael Dusan <michael.dusan@gmail.com>
2024-06-07 23:50:10
libcxx: fix building when -fsingle-threaded
* Skip building libcxx mt-only source files when single-threaded. * This change is required for llvm18 libcxx. * Add standalone test to link a trivial: - mt-executable with libcxx - st-executable with libcxx
1 parent 6327a39
Changed files (7)
src/libcxx.zig
@@ -37,17 +37,13 @@ const libcxxabi_files = [_][]const u8{
     "src/stdlib_typeinfo.cpp",
 };
 
-const libcxx_files = [_][]const u8{
+const libcxx_base_files = [_][]const u8{
     "src/algorithm.cpp",
     "src/any.cpp",
-    "src/atomic.cpp",
-    "src/barrier.cpp",
     "src/bind.cpp",
     "src/call_once.cpp",
     "src/charconv.cpp",
     "src/chrono.cpp",
-    "src/condition_variable.cpp",
-    "src/condition_variable_destructor.cpp",
     "src/error_category.cpp",
     "src/exception.cpp",
     "src/experimental/keep.cpp",
@@ -62,7 +58,6 @@ const libcxx_files = [_][]const u8{
     "src/filesystem/path.cpp",
     "src/fstream.cpp",
     "src/functional.cpp",
-    "src/future.cpp",
     "src/hash.cpp",
     "src/ios.cpp",
     "src/ios.instantiations.cpp",
@@ -71,8 +66,6 @@ const libcxx_files = [_][]const u8{
     "src/locale.cpp",
     "src/memory.cpp",
     "src/memory_resource.cpp",
-    "src/mutex.cpp",
-    "src/mutex_destructor.cpp",
     "src/new.cpp",
     "src/new_handler.cpp",
     "src/new_helpers.cpp",
@@ -86,7 +79,6 @@ const libcxx_files = [_][]const u8{
     "src/ryu/d2fixed.cpp",
     "src/ryu/d2s.cpp",
     "src/ryu/f2s.cpp",
-    "src/shared_mutex.cpp",
     "src/stdexcept.cpp",
     "src/string.cpp",
     "src/strstream.cpp",
@@ -95,9 +87,7 @@ const libcxx_files = [_][]const u8{
     "src/support/ibm/xlocale_zos.cpp",
     "src/support/win32/locale_win32.cpp",
     "src/support/win32/support.cpp",
-    "src/support/win32/thread_win32.cpp",
     "src/system_error.cpp",
-    "src/thread.cpp",
     "src/typeinfo.cpp",
     "src/tz.cpp",
     "src/tzdb_list.cpp",
@@ -107,6 +97,19 @@ const libcxx_files = [_][]const u8{
     "src/verbose_abort.cpp",
 };
 
+const libcxx_thread_files = [_][]const u8{
+    "src/atomic.cpp",
+    "src/barrier.cpp",
+    "src/condition_variable.cpp",
+    "src/condition_variable_destructor.cpp",
+    "src/future.cpp",
+    "src/mutex.cpp",
+    "src/mutex_destructor.cpp",
+    "src/shared_mutex.cpp",
+    "src/support/win32/thread_win32.cpp",
+    "src/thread.cpp",
+};
+
 pub const BuildError = error{
     OutOfMemory,
     SubCompilationFailed,
@@ -210,6 +213,11 @@ pub fn buildLibCXX(comp: *Compilation, prog_node: std.Progress.Node) BuildError!
         return error.SubCompilationFailed;
     };
 
+    const libcxx_files = if (comp.config.any_non_single_threaded)
+        &(libcxx_base_files ++ libcxx_thread_files)
+    else
+        &libcxx_base_files;
+
     var c_source_files = try std.ArrayList(Compilation.CSourceFile).initCapacity(arena, libcxx_files.len);
 
     for (libcxx_files) |cxx_src| {
@@ -223,16 +231,10 @@ pub fn buildLibCXX(comp: *Compilation, prog_node: std.Progress.Node) BuildError!
 
         if (std.mem.startsWith(u8, cxx_src, "src/support/win32/") and target.os.tag != .windows)
             continue;
-        if (std.mem.startsWith(u8, cxx_src, "src/support/solaris/") and !target.os.tag.isSolarish())
-            continue;
         if (std.mem.startsWith(u8, cxx_src, "src/support/ibm/") and target.os.tag != .zos)
             continue;
-        if (!comp.config.any_non_single_threaded) {
-            if (std.mem.startsWith(u8, cxx_src, "src/support/win32/thread_win32.cpp")) {
-                continue;
-            }
+        if (!comp.config.any_non_single_threaded)
             try cflags.append("-D_LIBCPP_HAS_NO_THREADS");
-        }
 
         try cflags.append("-DNDEBUG");
         try cflags.append(hardeningModeFlag(optimize_mode));
test/standalone/libcxx/build.zig
@@ -0,0 +1,38 @@
+const std = @import("std");
+
+pub fn build(b: *std.Build) void {
+    const target = b.standardTargetOptions(.{});
+    const optimize = b.standardOptimizeOption(.{});
+
+    const link_step = b.step("link", "Link with libcxx");
+    const run_step = b.step("run", "Run executables");
+    b.default_step = link_step;
+
+    {
+        const exe = b.addExecutable(.{
+            .name = "mt",
+            .root_source_file = b.path("mt.zig"),
+            .target = target,
+            .optimize = optimize,
+        });
+        exe.linkLibCpp();
+        exe.addCSourceFile(.{ .file = b.path("mt_doit.cpp") });
+        link_step.dependOn(&exe.step);
+        b.installArtifact(exe);
+        run_step.dependOn(&b.addRunArtifact(exe).step);
+    }
+    {
+        const exe = b.addExecutable(.{
+            .name = "st",
+            .root_source_file = b.path("st.zig"),
+            .target = target,
+            .optimize = optimize,
+            .single_threaded = true,
+        });
+        exe.linkLibCpp();
+        exe.addCSourceFile(.{ .file = b.path("st_doit.cpp") });
+        link_step.dependOn(&exe.step);
+        b.installArtifact(exe);
+        run_step.dependOn(&b.addRunArtifact(exe).step);
+    }
+}
test/standalone/libcxx/mt.zig
@@ -0,0 +1,5 @@
+extern fn doit() void;
+
+pub fn main() void {
+    doit();
+}
test/standalone/libcxx/mt_doit.cpp
@@ -0,0 +1,6 @@
+#include <iostream>
+#include <thread>
+
+extern "C" void doit() {
+    std::cout << "mt: thread=" << std::this_thread::get_id() << std::endl;
+}
test/standalone/libcxx/st.zig
@@ -0,0 +1,5 @@
+extern fn doit() void;
+
+pub fn main() void {
+    doit();
+}
test/standalone/libcxx/st_doit.cpp
@@ -0,0 +1,5 @@
+#include <iostream>
+
+extern "C" void doit() {
+    std::cout << "st: hello" << std::endl;
+}
test/standalone/build.zig.zon
@@ -92,6 +92,9 @@
         .issue_11595 = .{
             .path = "issue_11595",
         },
+        .libcxx = .{
+            .path = "libcxx",
+        },
         .load_dynamic_library = .{
             .path = "load_dynamic_library",
         },