authorgravatar for michael.dusan@gmail.comMichael Dusan <michael.dusan@gmail.com> 2024-06-07 17:50:10-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-06-08 15:34:19-04:00
log2cd536d7e8ae9c095eace969561a5e23c332fdad
treeb1fee457a9f593c1230ffd8bfc2123c79d99794b
parent6327a3994af8667d8553910d0175603c165d494d

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

7 files changed, 82 insertions(+), 18 deletions(-)

src/libcxx.zig+20-18
......@@ -37,17 +37,13 @@ const libcxxabi_files = [_][]const u8{
3737 "src/stdlib_typeinfo.cpp",
3838};
3939
40const libcxx_files = [_][]const u8{
40const libcxx_base_files = [_][]const u8{
4141 "src/algorithm.cpp",
4242 "src/any.cpp",
43 "src/atomic.cpp",
44 "src/barrier.cpp",
4543 "src/bind.cpp",
4644 "src/call_once.cpp",
4745 "src/charconv.cpp",
4846 "src/chrono.cpp",
49 "src/condition_variable.cpp",
50 "src/condition_variable_destructor.cpp",
5147 "src/error_category.cpp",
5248 "src/exception.cpp",
5349 "src/experimental/keep.cpp",
......@@ -62,7 +58,6 @@ const libcxx_files = [_][]const u8{
6258 "src/filesystem/path.cpp",
6359 "src/fstream.cpp",
6460 "src/functional.cpp",
65 "src/future.cpp",
6661 "src/hash.cpp",
6762 "src/ios.cpp",
6863 "src/ios.instantiations.cpp",
......@@ -71,8 +66,6 @@ const libcxx_files = [_][]const u8{
7166 "src/locale.cpp",
7267 "src/memory.cpp",
7368 "src/memory_resource.cpp",
74 "src/mutex.cpp",
75 "src/mutex_destructor.cpp",
7669 "src/new.cpp",
7770 "src/new_handler.cpp",
7871 "src/new_helpers.cpp",
......@@ -86,7 +79,6 @@ const libcxx_files = [_][]const u8{
8679 "src/ryu/d2fixed.cpp",
8780 "src/ryu/d2s.cpp",
8881 "src/ryu/f2s.cpp",
89 "src/shared_mutex.cpp",
9082 "src/stdexcept.cpp",
9183 "src/string.cpp",
9284 "src/strstream.cpp",
......@@ -95,9 +87,7 @@ const libcxx_files = [_][]const u8{
9587 "src/support/ibm/xlocale_zos.cpp",
9688 "src/support/win32/locale_win32.cpp",
9789 "src/support/win32/support.cpp",
98 "src/support/win32/thread_win32.cpp",
9990 "src/system_error.cpp",
100 "src/thread.cpp",
10191 "src/typeinfo.cpp",
10292 "src/tz.cpp",
10393 "src/tzdb_list.cpp",
......@@ -107,6 +97,19 @@ const libcxx_files = [_][]const u8{
10797 "src/verbose_abort.cpp",
10898};
10999
100const libcxx_thread_files = [_][]const u8{
101 "src/atomic.cpp",
102 "src/barrier.cpp",
103 "src/condition_variable.cpp",
104 "src/condition_variable_destructor.cpp",
105 "src/future.cpp",
106 "src/mutex.cpp",
107 "src/mutex_destructor.cpp",
108 "src/shared_mutex.cpp",
109 "src/support/win32/thread_win32.cpp",
110 "src/thread.cpp",
111};
112
110113pub const BuildError = error{
111114 OutOfMemory,
112115 SubCompilationFailed,
......@@ -210,6 +213,11 @@ pub fn buildLibCXX(comp: *Compilation, prog_node: std.Progress.Node) BuildError!
210213 return error.SubCompilationFailed;
211214 };
212215
216 const libcxx_files = if (comp.config.any_non_single_threaded)
217 &(libcxx_base_files ++ libcxx_thread_files)
218 else
219 &libcxx_base_files;
220
213221 var c_source_files = try std.ArrayList(Compilation.CSourceFile).initCapacity(arena, libcxx_files.len);
214222
215223 for (libcxx_files) |cxx_src| {
......@@ -223,16 +231,10 @@ pub fn buildLibCXX(comp: *Compilation, prog_node: std.Progress.Node) BuildError!
223231
224232 if (std.mem.startsWith(u8, cxx_src, "src/support/win32/") and target.os.tag != .windows)
225233 continue;
226 if (std.mem.startsWith(u8, cxx_src, "src/support/solaris/") and !target.os.tag.isSolarish())
227 continue;
228234 if (std.mem.startsWith(u8, cxx_src, "src/support/ibm/") and target.os.tag != .zos)
229235 continue;
230 if (!comp.config.any_non_single_threaded) {
231 if (std.mem.startsWith(u8, cxx_src, "src/support/win32/thread_win32.cpp")) {
232 continue;
233 }
236 if (!comp.config.any_non_single_threaded)
234237 try cflags.append("-D_LIBCPP_HAS_NO_THREADS");
235 }
236238
237239 try cflags.append("-DNDEBUG");
238240 try cflags.append(hardeningModeFlag(optimize_mode));
test/standalone/build.zig.zon+3
......@@ -92,6 +92,9 @@
9292 .issue_11595 = .{
9393 .path = "issue_11595",
9494 },
95 .libcxx = .{
96 .path = "libcxx",
97 },
9598 .load_dynamic_library = .{
9699 .path = "load_dynamic_library",
97100 },
test/standalone/libcxx/build.zig created+38
......@@ -0,0 +1,38 @@
1const std = @import("std");
2
3pub fn build(b: *std.Build) void {
4 const target = b.standardTargetOptions(.{});
5 const optimize = b.standardOptimizeOption(.{});
6
7 const link_step = b.step("link", "Link with libcxx");
8 const run_step = b.step("run", "Run executables");
9 b.default_step = link_step;
10
11 {
12 const exe = b.addExecutable(.{
13 .name = "mt",
14 .root_source_file = b.path("mt.zig"),
15 .target = target,
16 .optimize = optimize,
17 });
18 exe.linkLibCpp();
19 exe.addCSourceFile(.{ .file = b.path("mt_doit.cpp") });
20 link_step.dependOn(&exe.step);
21 b.installArtifact(exe);
22 run_step.dependOn(&b.addRunArtifact(exe).step);
23 }
24 {
25 const exe = b.addExecutable(.{
26 .name = "st",
27 .root_source_file = b.path("st.zig"),
28 .target = target,
29 .optimize = optimize,
30 .single_threaded = true,
31 });
32 exe.linkLibCpp();
33 exe.addCSourceFile(.{ .file = b.path("st_doit.cpp") });
34 link_step.dependOn(&exe.step);
35 b.installArtifact(exe);
36 run_step.dependOn(&b.addRunArtifact(exe).step);
37 }
38}
test/standalone/libcxx/mt.zig created+5
......@@ -0,0 +1,5 @@
1extern fn doit() void;
2
3pub fn main() void {
4 doit();
5}
test/standalone/libcxx/mt_doit.cpp created+6
......@@ -0,0 +1,6 @@
1#include <iostream>
2#include <thread>
3
4extern "C" void doit() {
5 std::cout << "mt: thread=" << std::this_thread::get_id() << std::endl;
6}
test/standalone/libcxx/st.zig created+5
......@@ -0,0 +1,5 @@
1extern fn doit() void;
2
3pub fn main() void {
4 doit();
5}
test/standalone/libcxx/st_doit.cpp created+5
......@@ -0,0 +1,5 @@
1#include <iostream>
2
3extern "C" void doit() {
4 std::cout << "st: hello" << std::endl;
5}