From 0949c41dab340ff6bc3b18c3090238bd1f4f46b7 Mon Sep 17 00:00:00 2001 From: Sebastian Ehlert <2684096-awvwgk@users.noreply.gitlab.com> Date: Sun, 11 Jul 2021 15:17:25 +0000 Subject: [PATCH] Use CMake's built-in zlib finder --- CMakeLists.txt | 5 +++-- cmake/FindStaticZLIB.cmake | 12 ++++++++++++ cmake/FindZLIB.cmake | 14 -------------- src/lfortran/CMakeLists.txt | 2 +- 4 files changed, 16 insertions(+), 17 deletions(-) create mode 100644 cmake/FindStaticZLIB.cmake delete mode 100644 cmake/FindZLIB.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 44b001e11e..9aac58a9bf 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -77,8 +77,9 @@ set(LFORTRAN_STATIC_BIN no CACHE BOOL "Build LFortran as a static binary") set(WITH_LFORTRAN_BINARY_MODFILES YES CACHE BOOL "Use binary modfiles") -#ZLIB -find_package(ZLIB REQUIRED) +# Find ZLIB with our custom finder before including LLVM since the finder for LLVM +# might search for ZLIB again and find the shared libraries instead of the static ones +find_package(StaticZLIB REQUIRED) # LLVM set(WITH_LLVM no CACHE BOOL "Build with LLVM support") diff --git a/cmake/FindStaticZLIB.cmake b/cmake/FindStaticZLIB.cmake new file mode 100644 index 0000000000..1467599800 --- /dev/null +++ b/cmake/FindStaticZLIB.cmake @@ -0,0 +1,12 @@ +# Backup the original value of the requested library suffixes +set(_CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES}) +# Static libraries end with .a on Unix and .lib on Windows +set(CMAKE_FIND_LIBRARY_SUFFIXES .a .lib) + +# Now use CMake's built-in ZLIB finder +find_package(ZLIB REQUIRED) + +# Reset the library suffixes to the original value +set(CMAKE_FIND_LIBRARY_SUFFIXES ${_CMAKE_FIND_LIBRARY_SUFFIXES}) +# Unset the temporary to not pollute the global namespace +unset(_CMAKE_FIND_LIBRARY_SUFFIXES) diff --git a/cmake/FindZLIB.cmake b/cmake/FindZLIB.cmake deleted file mode 100644 index fb6741a1b6..0000000000 --- a/cmake/FindZLIB.cmake +++ /dev/null @@ -1,14 +0,0 @@ -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) - -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}) - -message("ZLIB_INCLUDE_DIR: ${ZLIB_INCLUDE_DIR}") -message("ZLIB_LIBRARY: ${ZLIB_LIBRARY}") diff --git a/src/lfortran/CMakeLists.txt b/src/lfortran/CMakeLists.txt index 79a99b9097..dc7b929655 100644 --- a/src/lfortran/CMakeLists.txt +++ b/src/lfortran/CMakeLists.txt @@ -72,7 +72,7 @@ if (WITH_LLVM) endif() endif() add_library(lfortran_lib ${SRC}) -target_link_libraries(lfortran_lib lfortran_runtime_static p::zlib) +target_link_libraries(lfortran_lib lfortran_runtime_static ZLIB::ZLIB) target_include_directories(lfortran_lib BEFORE PUBLIC ${lfortran_SOURCE_DIR}/src) target_include_directories(lfortran_lib BEFORE PUBLIC ${lfortran_BINARY_DIR}/src) if (WITH_XEUS)