`find_library()` in CMake 3.21 doesn't work under a specific combination of C/CXX compiler paths.
Background
This is originally found when compiling OpenMP in LLVM 15. It is specific to CMake 3.21. CMake 3.22/3.23/3.25 seem fine but I'm not sure about the root cause and if it is actually "fixed", because if it is a known bug I would expect a 3.21 backport at this moment.
Description
On Ubuntu 22.04, if we create a simple project of find_library():
cmake_minimum_required(VERSION 3.21)
project(X C CXX)
find_library(FOO NAMES z)
The results will somehow depend on the C/C++ compiler provided.
If we symlink distro-stock GCC as /usr/local/bin/gcc -> /usr/bin/gcc
, use it as C compiler, and does not specify C++ compiler (it'll still default to gcc), find_library()
will failed with NOTFOUND.
However, if we set any C++ compiler, or use /usr/bin/gcc (without local) for C, find_library() works.
Repro
Here's a repro of 3x3 combinations of nothing/non-local/local as C/C++ compiler.
for i in '' -DCMAKE_C_COMPILER=/usr/{,local/}bin/gcc; do \
for j in '' -DCMAKE_CXX_COMPILER=/usr/{,local/}bin/gcc; do \
rm -rf build \
&& mkdir build \
&& pushd build >/dev/null \
&& printf 'cmake_minimum_required(VERSION 3.21)\n' > CMakeLists.txt \
&& printf 'project(X C CXX)\n' >> CMakeLists.txt \
&& printf 'find_library(FOO NAMES z)\n' >> CMakeLists.txt \
&& ln -sf /usr/{,local/}bin/gcc \
&& cmake $i $j . >/dev/null \
&& rm -f /usr/local/bin/gcc \
&& grep FOO CMakeCache.txt \
&& popd >/dev/null; \
done; \
done
Only line 7 of output, with /usr/local/bin/gcc for C and nothing for C++, failed.
FOO:FILEPATH=/usr/lib/x86_64-linux-gnu/libz.so
FOO:FILEPATH=/usr/lib/x86_64-linux-gnu/libz.so
FOO:FILEPATH=/usr/lib/x86_64-linux-gnu/libz.so
FOO:FILEPATH=/usr/lib/x86_64-linux-gnu/libz.so
FOO:FILEPATH=/usr/lib/x86_64-linux-gnu/libz.so
FOO:FILEPATH=/usr/lib/x86_64-linux-gnu/libz.so
FOO:FILEPATH=FOO-NOTFOUND
FOO:FILEPATH=/usr/lib/x86_64-linux-gnu/libz.so
FOO:FILEPATH=/usr/lib/x86_64-linux-gnu/libz.so
But /usr/local/bin/gcc
and /usr/bin/gcc
are symlinked.
They are identical and should not affect results.