Skip to content

Commit 1fbe4ed

Browse files
committed
CMake: Improve usage of llvm-config, esp. for consistent linking
This commit reworks the LLVM/Clang/LLD discovery process for CMake. The biggest changes are that: 1. We search for LLVM from most preferred directory to least, skipping any `llvm-config` that is the wrong version, or that doesn't support the requested link mode ("static" or "shared"). 2. `ZIG_PREFER_CLANG_CPP_DYLIB` has been renamed to `ZIG_SHARED_LLVM`, to better align with `ZIG_STATIC_LLVM`. 3. We only search for LLVM and LLD in the same directory alongside LLVM. 4. LLVM's link mode is forward to Clang, so that we can look for the appropriate shared/static libraries. 5. We use `--static-libs` when querying `--system-libs` from llvm-config, so that this will include libz and other dependencies for statically linking LLD
1 parent 67c4256 commit 1fbe4ed

File tree

4 files changed

+150
-190
lines changed

4 files changed

+150
-190
lines changed

CMakeLists.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,15 @@ endif()
6666
message(STATUS "Configuring zig version ${ZIG_VERSION}")
6767

6868
set(ZIG_STATIC off CACHE BOOL "Attempt to build a static zig executable (not compatible with glibc)")
69+
set(ZIG_SHARED_LLVM off CACHE BOOL "Prefer linking against shared LLVM libraries")
6970
set(ZIG_STATIC_LLVM off CACHE BOOL "Prefer linking against static LLVM libraries")
7071
set(ZIG_STATIC_ZLIB off CACHE BOOL "Prefer linking against static zlib")
71-
set(ZIG_PREFER_CLANG_CPP_DYLIB off CACHE BOOL "Try to link against -lclang-cpp")
7272
set(ZIG_USE_CCACHE off CACHE BOOL "Use ccache if available")
7373

74+
if (ZIG_SHARED_LLVM AND ZIG_STATIC_LLVM)
75+
message(SEND_ERROR "-DZIG_SHARED_LLVM and -DZIG_STATIC_LLVM cannot both be enabled simultaneously")
76+
endif()
77+
7478
if(CCACHE_PROGRAM AND ZIG_USE_CCACHE)
7579
SET_PROPERTY(GLOBAL PROPERTY RULE_LAUNCH_COMPILE "${CCACHE_PROGRAM}")
7680
endif()

cmake/Findclang.cmake

Lines changed: 10 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -7,53 +7,29 @@
77
# CLANG_LIBRARIES
88
# CLANG_LIBDIRS
99

10+
#TODO: FIXME
1011
find_path(CLANG_INCLUDE_DIRS NAMES clang/Frontend/ASTUnit.h
11-
PATHS
12-
/usr/lib/llvm/14/include
13-
/usr/lib/llvm-14/include
14-
/usr/lib/llvm-14.0/include
15-
/usr/local/llvm140/include
16-
/usr/local/llvm14/include
17-
/usr/local/opt/llvm@14/include
18-
/opt/homebrew/opt/llvm@14/include
19-
/mingw64/include
12+
HINTS ${LLVM_INCLUDE_DIRS}
13+
NO_DEFAULT_PATH # Only look for clang next to LLVM
2014
)
2115

22-
if(ZIG_PREFER_CLANG_CPP_DYLIB)
16+
if(${LLVM_LINK_MODE} STREQUAL "shared")
2317
find_library(CLANG_LIBRARIES
2418
NAMES
19+
libclang-cpp.so.14
2520
clang-cpp-14.0
2621
clang-cpp140
2722
clang-cpp
2823
NAMES_PER_DIR
29-
PATHS
30-
${CLANG_LIBDIRS}
31-
/usr/lib/llvm/14/lib
32-
/usr/lib/llvm/14/lib64
33-
/usr/lib/llvm-14/lib
34-
/usr/local/llvm140/lib
35-
/usr/local/llvm14/lib
36-
/usr/local/opt/llvm@14/lib
37-
/opt/homebrew/opt/llvm@14/lib
24+
HINTS "${LLVM_LIBDIRS}"
25+
NO_DEFAULT_PATH # Only look for Clang next to LLVM
3826
)
39-
endif()
40-
41-
if(NOT CLANG_LIBRARIES)
27+
else()
4228
macro(FIND_AND_ADD_CLANG_LIB _libname_)
4329
string(TOUPPER ${_libname_} _prettylibname_)
4430
find_library(CLANG_${_prettylibname_}_LIB NAMES ${_libname_} NAMES_PER_DIR
45-
PATHS
46-
${CLANG_LIBDIRS}
47-
/usr/lib/llvm/14/lib
48-
/usr/lib/llvm-14/lib
49-
/usr/lib/llvm-14.0/lib
50-
/usr/local/llvm140/lib
51-
/usr/local/llvm14/lib
52-
/usr/local/opt/llvm@14/lib
53-
/opt/homebrew/opt/llvm@14/lib
54-
/mingw64/lib
55-
/c/msys64/mingw64/lib
56-
c:\\msys64\\mingw64\\lib
31+
HINTS "${LLVM_LIBDIRS}"
32+
NO_DEFAULT_PATH # Only look for Clang next to LLVM
5733
)
5834
if(CLANG_${_prettylibname_}_LIB)
5935
set(CLANG_LIBRARIES ${CLANG_LIBRARIES} ${CLANG_${_prettylibname_}_LIB})

cmake/Findlld.cmake

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,40 +7,25 @@
77
# LLD_LIBRARIES
88

99
find_path(LLD_INCLUDE_DIRS NAMES lld/Common/Driver.h
10-
PATHS
11-
/usr/lib/llvm-14/include
12-
/usr/local/llvm140/include
13-
/usr/local/llvm14/include
14-
/usr/local/opt/llvm@14/include
15-
/opt/homebrew/opt/llvm@14/include
16-
/mingw64/include)
10+
HINTS ${LLVM_INCLUDE_DIRS}
11+
NO_DEFAULT_PATH # Only look for LLD next to LLVM
12+
)
1713

1814
find_library(LLD_LIBRARY NAMES lld-14.0 lld140 lld NAMES_PER_DIR
19-
PATHS
20-
/usr/lib/llvm-14/lib
21-
/usr/local/llvm140/lib
22-
/usr/local/llvm14/lib
23-
/usr/local/opt/llvm@14/lib
24-
/opt/homebrew/opt/llvm@14/lib
15+
HINTS ${LLVM_LIBDIRS}
16+
NO_DEFAULT_PATH # Only look for LLD next to LLVM
2517
)
2618
if(EXISTS ${LLD_LIBRARY})
2719
set(LLD_LIBRARIES ${LLD_LIBRARY})
2820
else()
2921
macro(FIND_AND_ADD_LLD_LIB _libname_)
3022
string(TOUPPER ${_libname_} _prettylibname_)
3123
find_library(LLD_${_prettylibname_}_LIB NAMES ${_libname_} NAMES_PER_DIR
32-
PATHS
33-
${LLD_LIBDIRS}
34-
/usr/lib/llvm-14/lib
35-
/usr/local/llvm140/lib
36-
/usr/local/llvm14/lib
37-
/usr/local/opt/llvm@14/lib
38-
/opt/homebrew/opt/llvm@14/lib
39-
/mingw64/lib
40-
/c/msys64/mingw64/lib
41-
c:/msys64/mingw64/lib)
42-
if(LLD_${_prettylibname_}_LIB)
43-
set(LLD_LIBRARIES ${LLD_LIBRARIES} ${LLD_${_prettylibname_}_LIB})
24+
HINTS ${LLVM_LIBDIRS}
25+
NO_DEFAULT_PATH # Only look for LLD next to LLVM
26+
)
27+
if(LLD_${_prettylibname_}_LIB)
28+
set(LLD_LIBRARIES ${LLD_LIBRARIES} ${LLD_${_prettylibname_}_LIB})
4429
endif()
4530
endmacro(FIND_AND_ADD_LLD_LIB)
4631

0 commit comments

Comments
 (0)