cmake: handle llvm system libraries separately from the llvm libraries themselves, to fix path issues on windows

This commit is contained in:
kcbanner 2022-11-01 23:11:52 -04:00
parent ed23615638
commit 4ffc2bbb5e
3 changed files with 33 additions and 3 deletions

View File

@ -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",

View File

@ -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)

View File

@ -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