authorgravatar for topolarity@tapscott.meCody Tapscott <topolarity@tapscott.me> 2022-07-13 10:36:25-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-07-14 22:18:10-04:00
logdd70336f3ace541586210bd7bd061cc09a8c0e03
treebadb6ba2ced8240adfdae419f175dd2dc1b09267
parenta4559271501670b39305fc3575ae8d87fa03d35e

CMake: Use NAMES_PER_DIR in all lookups

CMake has a surprising default behavior where looking up a library by multiple names gives the name order higher priority than the directory search order. For example, if your system provides "llvm-config-14" and CMAKE_PREFIX_PATH includes "llvm-config", CMake will always end up choosing the system-provided llvm-config-14. This change add NAMES_PER_DIR to request the more sensible behavior: directory search order has higher priority than name order, so CMAKE_PREFIX_PATH always wins over system-provided tools/libraries.

4 files changed, 12 insertions(+), 10 deletions(-)

CMakeLists.txt+3-3
......@@ -31,7 +31,7 @@ set(ZIG_VERSION "" CACHE STRING "Override Zig version string. Default is to find
3131
3232if("${ZIG_VERSION}" STREQUAL "")
3333 set(ZIG_VERSION "${ZIG_VERSION_MAJOR}.${ZIG_VERSION_MINOR}.${ZIG_VERSION_PATCH}")
34 find_program(GIT_EXE NAMES git)
34 find_program(GIT_EXE NAMES git NAMES_PER_DIR)
3535 if(GIT_EXE)
3636 execute_process(
3737 COMMAND ${GIT_EXE} -C ${CMAKE_SOURCE_DIR} describe --match *.*.* --tags
......@@ -110,13 +110,13 @@ find_package(lld 14)
110110
111111if(ZIG_STATIC_ZLIB)
112112 list(REMOVE_ITEM LLVM_LIBRARIES "-lz")
113 find_library(ZLIB NAMES libz.a libzlibstatic.a z zlib libz)
113 find_library(ZLIB NAMES libz.a libzlibstatic.a z zlib libz NAMES_PER_DIR)
114114 list(APPEND LLVM_LIBRARIES "${ZLIB}")
115115endif()
116116
117117if(APPLE AND ZIG_STATIC)
118118 list(REMOVE_ITEM LLVM_LIBRARIES "-lcurses")
119 find_library(CURSES NAMES libcurses.a libncurses.a
119 find_library(CURSES NAMES libcurses.a libncurses.a NAMES_PER_DIR
120120 PATHS
121121 /usr/local/opt/ncurses/lib
122122 /opt/homebrew/opt/ncurses/lib)
cmake/Findclang.cmake+2-1
......@@ -25,6 +25,7 @@ if(ZIG_PREFER_CLANG_CPP_DYLIB)
2525 clang-cpp-14.0
2626 clang-cpp140
2727 clang-cpp
28 NAMES_PER_DIR
2829 PATHS
2930 ${CLANG_LIBDIRS}
3031 /usr/lib/llvm/14/lib
......@@ -40,7 +41,7 @@ endif()
4041if(NOT CLANG_LIBRARIES)
4142 macro(FIND_AND_ADD_CLANG_LIB _libname_)
4243 string(TOUPPER ${_libname_} _prettylibname_)
43 find_library(CLANG_${_prettylibname_}_LIB NAMES ${_libname_}
44 find_library(CLANG_${_prettylibname_}_LIB NAMES ${_libname_} NAMES_PER_DIR
4445 PATHS
4546 ${CLANG_LIBDIRS}
4647 /usr/lib/llvm/14/lib
cmake/Findlld.cmake+2-2
......@@ -15,7 +15,7 @@ find_path(LLD_INCLUDE_DIRS NAMES lld/Common/Driver.h
1515 /opt/homebrew/opt/llvm@14/include
1616 /mingw64/include)
1717
18find_library(LLD_LIBRARY NAMES lld-14.0 lld140 lld
18find_library(LLD_LIBRARY NAMES lld-14.0 lld140 lld NAMES_PER_DIR
1919 PATHS
2020 /usr/lib/llvm-14/lib
2121 /usr/local/llvm140/lib
......@@ -28,7 +28,7 @@ if(EXISTS ${LLD_LIBRARY})
2828else()
2929 macro(FIND_AND_ADD_LLD_LIB _libname_)
3030 string(TOUPPER ${_libname_} _prettylibname_)
31 find_library(LLD_${_prettylibname_}_LIB NAMES ${_libname_}
31 find_library(LLD_${_prettylibname_}_LIB NAMES ${_libname_} NAMES_PER_DIR
3232 PATHS
3333 ${LLD_LIBDIRS}
3434 /usr/lib/llvm-14/lib
cmake/Findllvm.cmake+5-4
......@@ -27,6 +27,7 @@ if(ZIG_PREFER_CLANG_CPP_DYLIB)
2727 LLVM-14
2828 LLVM-140
2929 LLVM
30 NAMES_PER_DIR
3031 PATHS
3132 ${LLVM_LIBDIRS}
3233 /usr/lib/llvm/14/lib
......@@ -40,7 +41,7 @@ if(ZIG_PREFER_CLANG_CPP_DYLIB)
4041 )
4142
4243 find_program(LLVM_CONFIG_EXE
43 NAMES llvm-config-14 llvm-config-14.0 llvm-config140 llvm-config14 llvm-config
44 NAMES llvm-config-14 llvm-config-14.0 llvm-config140 llvm-config14 llvm-config NAMES_PER_DIR
4445 PATHS
4546 "/mingw64/bin"
4647 "/c/msys64/mingw64/bin"
......@@ -71,7 +72,7 @@ if(ZIG_PREFER_CLANG_CPP_DYLIB)
7172 endif()
7273elseif(ZIG_USE_LLVM_CONFIG)
7374 find_program(LLVM_CONFIG_EXE
74 NAMES llvm-config-14 llvm-config-14.0 llvm-config140 llvm-config14 llvm-config
75 NAMES llvm-config-14 llvm-config-14.0 llvm-config140 llvm-config14 llvm-config NAMES_PER_DIR
7576 PATHS
7677 "/mingw64/bin"
7778 "/c/msys64/mingw64/bin"
......@@ -173,7 +174,7 @@ elseif(ZIG_USE_LLVM_CONFIG)
173174 set(LLVM_LIBRARIES ${LLVM_LIBRARIES} ${LLVM_SYSTEM_LIBS})
174175
175176 if(NOT LLVM_LIBRARIES)
176 find_library(LLVM_LIBRARIES NAMES LLVM LLVM-14 LLVM-14.0)
177 find_library(LLVM_LIBRARIES NAMES LLVM LLVM-14 LLVM-14.0 NAMES_PER_DIR)
177178 endif()
178179
179180 link_directories("${CMAKE_PREFIX_PATH}/lib")
......@@ -184,7 +185,7 @@ else()
184185
185186 macro(FIND_AND_ADD_LLVM_LIB _libname_)
186187 string(TOUPPER ${_libname_} _prettylibname_)
187 find_library(LLVM_${_prettylibname_}_LIB NAMES ${_libname_} PATHS ${LLVM_LIBDIRS})
188 find_library(LLVM_${_prettylibname_}_LIB NAMES ${_libname_} NAMES_PER_DIR PATHS ${LLVM_LIBDIRS})
188189 set(LLVM_LIBRARIES ${LLVM_LIBRARIES} ${LLVM_${_prettylibname_}_LIB})
189190 endmacro(FIND_AND_ADD_LLVM_LIB)
190191