CMake detection for zlib fails
The zlib detection is in cmake/FindZLIB.cmake
is faulty with LLVM 12 on Arch Linux. The CMake module of LLVM will also try to find zlib and use LFortran's finder instead of CMake's own finder and therefore fail with:
❯ cmake -B _build -DWITH_LLVM=yes -DCMAKE_INSTALL_PREFIX=$PWD/_dist -G Ninja
-- The C compiler identification is GNU 11.1.0
-- The CXX compiler identification is GNU 11.1.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found ZLIB: /usr/include
ZLIB_INCLUDE_DIR: /usr/include
ZLIB_LIBRARY: /usr/lib/libz.a
CMake Error at cmake/FindZLIB.cmake:7 (add_library):
add_library cannot create imported target "p::zlib" because another target
with the same name already exists.
Call Stack (most recent call first):
/usr/lib64/cmake/llvm/LLVMConfig.cmake:220 (find_package)
CMakeLists.txt:87 (find_package)
ZLIB_INCLUDE_DIR: /usr/include
ZLIB_LIBRARY: /usr/lib/libz.a
-- Found LibXml2: /usr/lib/libxml2.so (found version "2.9.10")
-- Found LLVM 12.0.0
-- Using LLVMConfig.cmake in: /usr/lib64/cmake/llvm
Configuration results
---------------------
LFORTRAN_VERSION: 0.10.0-1612-gc9b2152f
CPACK_PACKAGE_FILE_NAME: lfortran-0.10.0-1612-gc9b2152f-Linux
C compiler : /usr/bin/cc
C++ compiler : /usr/bin/c++
Build type: Release
C compiler flags : -O3 -DNDEBUG
C++ compiler flags : -Wall -Wextra -O3 -march=native -funroll-loops -DNDEBUG
Installation prefix: /home/awvwgk/projects/src/git/lfortran/lfortran-0.10.0-1612-gc9b2152f/_dist
WITH_LFORTRAN_ASSERT: no
LFORTRAN_STATIC_BIN: no
WITH_STACKTRACE: no
WITH_UNWIND: no
WITH_BFD: no
WITH_DWARFDUMP: no
WITH_LINKH: no
WITH_MACHO: no
HAVE_LFORTRAN_DEMANGLE: yes
WITH_LLVM: yes
WITH_XEUS: no
WITH_JSON: no
WITH_FMT: no
WITH_LFORTRAN_BINARY_MODFILES: YES
-- Configuring incomplete, errors occurred!
See also "/home/awvwgk/projects/src/git/lfortran/lfortran-0.10.0-1612-gc9b2152f/_build/CMakeFiles/CMakeOutput.log".
A hacky fix in LFortran's zlib finder which solved the problem for me was to check for the existence of the respective targets:
find_path(ZLIB_INCLUDE_DIR zlib.h)
find_library(ZLIB_LIBRARY NAMES libz.a z)
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(ZLIB DEFAULT_MSG ZLIB_INCLUDE_DIR ZLIB_LIBRARY)
if(NOT TARGET p::zlib)
add_library(p::zlib INTERFACE IMPORTED)
set_property(TARGET p::zlib PROPERTY INTERFACE_INCLUDE_DIRECTORIES
${ZLIB_INCLUDE_DIR})
set_property(TARGET p::zlib PROPERTY INTERFACE_LINK_LIBRARIES
${ZLIB_LIBRARY})
endif()
if(NOT TARGET ZLIB::ZLIB)
add_library(ZLIB::ZLIB INTERFACE IMPORTED)
set_property(TARGET ZLIB::ZLIB PROPERTY INTERFACE_INCLUDE_DIRECTORIES
${ZLIB_INCLUDE_DIR})
set_property(TARGET ZLIB::ZLIB PROPERTY INTERFACE_LINK_LIBRARIES
${ZLIB_LIBRARY})
endif()
message("ZLIB_INCLUDE_DIR: ${ZLIB_INCLUDE_DIR}")
message("ZLIB_LIBRARY: ${ZLIB_LIBRARY}")
Easier would be to either rely on CMake's built-in zlib finder or to properly export the ZLIB::ZLIB
target from the overwritten zlib finder in LFortran.
Details:
- LLVM 12.0.1
- CMake 3.20.3
- zlib 1.2.11