authorgravatar for michael.dusan@gmail.comMichael Dusan <michael.dusan@gmail.com> 2023-09-01 10:04:26-04:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-09-03 08:38:03+02:00
log3cf71580c460a30bb52da6e2cbc4482a67bd2930
tree18d4a840df75cfd0f1296e987ecbedf1c438d171
parent1816bb4ab050f46504a2f1e827e9d74f8d253101

build stage3: detect system libcxx

Detect system libcxx name with the CMake build system and convey that information to build.zig via config.h so that the same system libcxx is used for stage3. This undoes the hacky search 901457d173467c71b681a8c69f4b77c94d516da7 . closes #17018

3 files changed, 22 insertions(+), 10 deletions(-)

CMakeLists.txt+7
...@@ -124,6 +124,13 @@ endif()...@@ -124,6 +124,13 @@ endif()
124124
125set(ZIG_PIE off CACHE BOOL "produce a position independent zig executable")125set(ZIG_PIE off CACHE BOOL "produce a position independent zig executable")
126126
127# Detect system libcxx name.
128if ("c++" IN_LIST CMAKE_CXX_IMPLICIT_LINK_LIBRARIES)
129 set(ZIG_SYSTEM_LIBCXX "c++" CACHE STRING "system libcxx name for build.zig")
130else()
131 set(ZIG_SYSTEM_LIBCXX "stdc++" CACHE STRING "system libcxx name for build.zig")
132endif()
133
127find_package(llvm 16)134find_package(llvm 16)
128find_package(clang 16)135find_package(clang 16)
129find_package(lld 16)136find_package(lld 16)
build.zig+14-10
...@@ -632,16 +632,14 @@ fn addCmakeCfgOptionsToExe(...@@ -632,16 +632,14 @@ fn addCmakeCfgOptionsToExe(
632 const lib_suffix = if (static) exe.target.staticLibSuffix()[1..] else exe.target.dynamicLibSuffix()[1..];632 const lib_suffix = if (static) exe.target.staticLibSuffix()[1..] else exe.target.dynamicLibSuffix()[1..];
633 switch (exe.target.getOsTag()) {633 switch (exe.target.getOsTag()) {
634 .linux => {634 .linux => {
635 // First we try to link against system libstdc++ or libc++.635 // First we try to link against the detected libcxx name. If that doesn't work, we fall
636 // If that doesn't work, we fall to -lc++ and cross our fingers.636 // back to -lc++ and cross our fingers.
637 const found = for ([_][]const u8{ "stdc++", "c++" }) |name| {637 addCxxKnownPath(b, cfg, exe, b.fmt("lib{s}.{s}", .{ cfg.system_libcxx, lib_suffix }), "", need_cpp_includes) catch |err| switch (err) {
638 addCxxKnownPath(b, cfg, exe, b.fmt("lib{s}.{s}", .{ name, lib_suffix }), "", need_cpp_includes) catch |err| switch (err) {638 error.RequiredLibraryNotFound => {
639 error.RequiredLibraryNotFound => continue,639 exe.linkLibCpp();
640 else => |e| return e,640 },
641 };641 else => |e| return e,
642 break true;642 };
643 } else false;
644 if (!found) exe.linkLibCpp();
645 exe.linkSystemLibrary("unwind");643 exe.linkSystemLibrary("unwind");
646 },644 },
647 .ios, .macos, .watchos, .tvos => {645 .ios, .macos, .watchos, .tvos => {
...@@ -775,6 +773,7 @@ const CMakeConfig = struct {...@@ -775,6 +773,7 @@ const CMakeConfig = struct {
775 llvm_include_dir: []const u8,773 llvm_include_dir: []const u8,
776 llvm_libraries: []const u8,774 llvm_libraries: []const u8,
777 dia_guids_lib: []const u8,775 dia_guids_lib: []const u8,
776 system_libcxx: []const u8,
778};777};
779778
780const max_config_h_bytes = 1 * 1024 * 1024;779const max_config_h_bytes = 1 * 1024 * 1024;
...@@ -840,6 +839,7 @@ fn parseConfigH(b: *std.Build, config_h_text: []const u8) ?CMakeConfig {...@@ -840,6 +839,7 @@ fn parseConfigH(b: *std.Build, config_h_text: []const u8) ?CMakeConfig {
840 .llvm_include_dir = undefined,839 .llvm_include_dir = undefined,
841 .llvm_libraries = undefined,840 .llvm_libraries = undefined,
842 .dia_guids_lib = undefined,841 .dia_guids_lib = undefined,
842 .system_libcxx = undefined,
843 };843 };
844844
845 const mappings = [_]struct { prefix: []const u8, field: []const u8 }{845 const mappings = [_]struct { prefix: []const u8, field: []const u8 }{
...@@ -891,6 +891,10 @@ fn parseConfigH(b: *std.Build, config_h_text: []const u8) ?CMakeConfig {...@@ -891,6 +891,10 @@ fn parseConfigH(b: *std.Build, config_h_text: []const u8) ?CMakeConfig {
891 .prefix = "#define ZIG_LLVM_LIB_PATH ",891 .prefix = "#define ZIG_LLVM_LIB_PATH ",
892 .field = "llvm_lib_dir",892 .field = "llvm_lib_dir",
893 },893 },
894 .{
895 .prefix = "#define ZIG_SYSTEM_LIBCXX",
896 .field = "system_libcxx",
897 },
894 // .prefix = ZIG_LLVM_LINK_MODE parsed manually below898 // .prefix = ZIG_LLVM_LINK_MODE parsed manually below
895 };899 };
896900
stage1/config.h.in+1
...@@ -28,5 +28,6 @@...@@ -28,5 +28,6 @@
28#define ZIG_LLVM_LIBRARIES "@LLVM_LIBRARIES@"28#define ZIG_LLVM_LIBRARIES "@LLVM_LIBRARIES@"
29#define ZIG_LLVM_LIB_PATH "@LLVM_LIBDIRS@"29#define ZIG_LLVM_LIB_PATH "@LLVM_LIBDIRS@"
30#define ZIG_LLVM_LINK_MODE "@LLVM_LINK_MODE@"30#define ZIG_LLVM_LINK_MODE "@LLVM_LINK_MODE@"
31#define ZIG_SYSTEM_LIBCXX "@ZIG_SYSTEM_LIBCXX@"
3132
32#endif33#endif