From 4ffc2bbb5ec6993802a59f358c4316375615a27c Mon Sep 17 00:00:00 2001 From: kcbanner Date: Tue, 1 Nov 2022 23:11:52 -0400 Subject: [PATCH] cmake: handle llvm system libraries separately from the llvm libraries themselves, to fix path issues on windows --- build.zig | 24 ++++++++++++++++++++++++ cmake/Findllvm.cmake | 11 ++++++++--- stage1/config.h.in | 1 + 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/build.zig b/build.zig index c11329dbf3..61b2a08b09 100644 --- a/build.zig +++ b/build.zig @@ -552,6 +552,7 @@ fn addCmakeCfgOptionsToExe( addCMakeLibraryList(exe, cfg.clang_libraries); addCMakeLibraryList(exe, cfg.lld_libraries); addCMakeLibraryList(exe, cfg.llvm_libraries); + addCMakeSystemLibraryList(exe, cfg.llvm_system_libraries); if (use_zig_libcxx) { exe.linkLibCpp(); @@ -679,6 +680,23 @@ fn addCMakeLibraryList(exe: *std.build.LibExeObjStep, list: []const u8) void { } } +fn addCMakeSystemLibraryList(exe: *std.build.LibExeObjStep, list: []const u8) void { + var it = mem.tokenize(u8, list, ";"); + while (it.next()) |lib| { + var start_offset: usize = 0; + var end_offset: usize = 0; + if (mem.startsWith(u8, lib, "-l")) { + start_offset = "-l".len; + } + + if (exe.target.isWindows() and mem.endsWith(u8, lib, ".lib")) { + end_offset = ".lib".len; + } + + exe.linkSystemLibrary(lib[start_offset..lib.len - end_offset]); + } +} + const CMakeConfig = struct { llvm_linkage: std.build.LibExeObjStep.Linkage, cmake_binary_dir: []const u8, @@ -692,6 +710,7 @@ const CMakeConfig = struct { llvm_lib_dir: []const u8, llvm_include_dir: []const u8, llvm_libraries: []const u8, + llvm_system_libraries: []const u8, dia_guids_lib: []const u8, }; @@ -757,6 +776,7 @@ fn parseConfigH(b: *Builder, config_h_text: []const u8) ?CMakeConfig { .llvm_lib_dir = undefined, .llvm_include_dir = undefined, .llvm_libraries = undefined, + .llvm_system_libraries = undefined, .dia_guids_lib = undefined, }; @@ -797,6 +817,10 @@ fn parseConfigH(b: *Builder, config_h_text: []const u8) ?CMakeConfig { .prefix = "#define ZIG_LLVM_LIBRARIES ", .field = "llvm_libraries", }, + .{ + .prefix = "#define ZIG_LLVM_SYSTEM_LIBRARIES ", + .field = "llvm_system_libraries", + }, .{ .prefix = "#define ZIG_DIA_GUIDS_LIB ", .field = "dia_guids_lib", diff --git a/cmake/Findllvm.cmake b/cmake/Findllvm.cmake index 60a52056d8..87059f9f1d 100644 --- a/cmake/Findllvm.cmake +++ b/cmake/Findllvm.cmake @@ -5,6 +5,7 @@ # LLVM_FOUND # LLVM_INCLUDE_DIRS # LLVM_LIBRARIES +# LLVM_SYSTEM_LIBRARIES # LLVM_LIBDIRS # LLVM_LINK_MODE @@ -172,9 +173,9 @@ if(ZIG_USE_LLVM_CONFIG) OUTPUT_STRIP_TRAILING_WHITESPACE) string(REPLACE " " ";" LLVM_STATIC_SYSTEM_LIBS "${LLVM_STATIC_SYSTEM_LIBS_SPACES}") - set(LLVM_LIBRARIES ${LLVM_LIBRARIES} ${LLVM_SYSTEM_LIBS} ${LLVM_STATIC_SYSTEM_LIBS}) + set(LLVM_SYSTEM_LIBRARIES ${LLVM_SYSTEM_LIBS} ${LLVM_STATIC_SYSTEM_LIBS}) else() - set(LLVM_LIBRARIES ${LLVM_LIBRARIES} ${LLVM_SYSTEM_LIBS}) + set(LLVM_SYSTEM_LIBRARIES ${LLVM_SYSTEM_LIBS}) endif() execute_process( @@ -369,7 +370,11 @@ else() find_path(LLVM_INCLUDE_DIRS NAMES llvm/IR/IRBuilder.h) endif() +if(NOT LLVM_SYSTEM_LIBRARIES) + set(LLVM_SYSTEM_LIBRARIES "") +endif() + include(FindPackageHandleStandardArgs) find_package_handle_standard_args(llvm DEFAULT_MSG LLVM_LIBRARIES LLVM_INCLUDE_DIRS) -mark_as_advanced(LLVM_INCLUDE_DIRS LLVM_LIBRARIES LLVM_LIBDIRS) +mark_as_advanced(LLVM_INCLUDE_DIRS LLVM_LIBRARIES LLVM_SYSTEM_LIBRARIES LLVM_LIBDIRS) diff --git a/stage1/config.h.in b/stage1/config.h.in index 0f1d902ef9..4ec1aa1fa6 100644 --- a/stage1/config.h.in +++ b/stage1/config.h.in @@ -28,5 +28,6 @@ #define ZIG_LLVM_LIBRARIES "@LLVM_LIBRARIES@" #define ZIG_LLVM_LIB_PATH "@LLVM_LIBDIRS@" #define ZIG_LLVM_LINK_MODE "@LLVM_LINK_MODE@" +#define ZIG_LLVM_SYSTEM_LIBRARIES "@LLVM_SYSTEM_LIBRARIES@" #endif