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()
124124
125125set(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
127134find_package(llvm 16)
128135find_package(clang 16)
129136find_package(lld 16)
build.zig+14-10
......@@ -632,16 +632,14 @@ fn addCmakeCfgOptionsToExe(
632632 const lib_suffix = if (static) exe.target.staticLibSuffix()[1..] else exe.target.dynamicLibSuffix()[1..];
633633 switch (exe.target.getOsTag()) {
634634 .linux => {
635 // First we try to link against system libstdc++ or libc++.
636 // If that doesn't work, we fall to -lc++ and cross our fingers.
637 const found = for ([_][]const u8{ "stdc++", "c++" }) |name| {
638 addCxxKnownPath(b, cfg, exe, b.fmt("lib{s}.{s}", .{ name, lib_suffix }), "", need_cpp_includes) catch |err| switch (err) {
639 error.RequiredLibraryNotFound => continue,
640 else => |e| return e,
641 };
642 break true;
643 } else false;
644 if (!found) exe.linkLibCpp();
635 // First we try to link against the detected libcxx name. If that doesn't work, we fall
636 // back to -lc++ and cross our fingers.
637 addCxxKnownPath(b, cfg, exe, b.fmt("lib{s}.{s}", .{ cfg.system_libcxx, lib_suffix }), "", need_cpp_includes) catch |err| switch (err) {
638 error.RequiredLibraryNotFound => {
639 exe.linkLibCpp();
640 },
641 else => |e| return e,
642 };
645643 exe.linkSystemLibrary("unwind");
646644 },
647645 .ios, .macos, .watchos, .tvos => {
......@@ -775,6 +773,7 @@ const CMakeConfig = struct {
775773 llvm_include_dir: []const u8,
776774 llvm_libraries: []const u8,
777775 dia_guids_lib: []const u8,
776 system_libcxx: []const u8,
778777};
779778
780779const max_config_h_bytes = 1 * 1024 * 1024;
......@@ -840,6 +839,7 @@ fn parseConfigH(b: *std.Build, config_h_text: []const u8) ?CMakeConfig {
840839 .llvm_include_dir = undefined,
841840 .llvm_libraries = undefined,
842841 .dia_guids_lib = undefined,
842 .system_libcxx = undefined,
843843 };
844844
845845 const mappings = [_]struct { prefix: []const u8, field: []const u8 }{
......@@ -891,6 +891,10 @@ fn parseConfigH(b: *std.Build, config_h_text: []const u8) ?CMakeConfig {
891891 .prefix = "#define ZIG_LLVM_LIB_PATH ",
892892 .field = "llvm_lib_dir",
893893 },
894 .{
895 .prefix = "#define ZIG_SYSTEM_LIBCXX",
896 .field = "system_libcxx",
897 },
894898 // .prefix = ZIG_LLVM_LINK_MODE parsed manually below
895899 };
896900
stage1/config.h.in+1
......@@ -28,5 +28,6 @@
2828#define ZIG_LLVM_LIBRARIES "@LLVM_LIBRARIES@"
2929#define ZIG_LLVM_LIB_PATH "@LLVM_LIBDIRS@"
3030#define ZIG_LLVM_LINK_MODE "@LLVM_LINK_MODE@"
31#define ZIG_SYSTEM_LIBCXX "@ZIG_SYSTEM_LIBCXX@"
3132
3233#endif