find_package(ZLIB) is not idempotent
cmake version 3.16.3 (on Debian Linux) cmake version 3.15.19101501-MSVC_2 (on Windows 10)
It seems that find_package(ZLIB)
is not idemponent. If find_package(ZLIB)
is called twice, the second call fails.
Steps to reproduce:
This bug can be reproduced on a computer without an installed zlib dev package. For example, on a Debian or Ubuntu Linux computer, uninstall the zlib1g-dev package:
sudo apt-get remove zlib1g-dev
and verify that /usr/include/zlib.h
and /usr/include/zconf.h
don't exist.
Change into a work directory and follow these steps.
- Create a
CMakeLists.txt
file with these contents:
cmake_minimum_required(VERSION 3.5)
project(ProjectName)
set(ZLIB_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/zlib")
set(ZLIB_LIBRARY zlibstatic)
find_package(ZLIB REQUIRED)
find_package(ZLIB REQUIRED)
Note that find_package(ZLIB)
is called twice.
- Create a
zlib-patch.txt
file with these contents:
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 0fe939d..c66583f 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -63,7 +63,6 @@ if(MSVC)
set(CMAKE_DEBUG_POSTFIX "d")
add_definitions(-D_CRT_SECURE_NO_DEPRECATE)
add_definitions(-D_CRT_NONSTDC_NO_DEPRECATE)
- include_directories(${CMAKE_CURRENT_SOURCE_DIR})
endif()
if(NOT CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_CURRENT_BINARY_DIR)
@@ -83,7 +82,7 @@ configure_file( ${CMAKE_CURRENT_SOURCE_DIR}/zlib.pc.cmakein
${ZLIB_PC} @ONLY)
configure_file( ${CMAKE_CURRENT_SOURCE_DIR}/zconf.h.cmakein
${CMAKE_CURRENT_BINARY_DIR}/zconf.h @ONLY)
-include_directories(${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_SOURCE_DIR})
+include_directories(${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR})
#============================================================================
This patch fixes a bug in zlib/CMakeLists.txt
when zlib is built as a sub-project. Note that it is not necessary to apply this patch to this minimal test case, but I included it for completeness.
- Run the following commands:
$ git clone -b v1.2.11 --depth 1 https://github.com/madler/zlib.git
$ cd zlib
$ patch -p1 < ../zlib-patch.txt
$ cd ..
$ mkdir build
$ cd build
$ cmake ..
You will see output like this:
$ cmake ..
-- The C compiler identification is GNU 9.3.0
-- The CXX compiler identification is GNU 9.3.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found ZLIB: zlibstatic (found version "1.2.11")
CMake Error at /usr/share/cmake-3.16/Modules/FindPackageHandleStandardArgs.cmake:146 (message):
Could NOT find ZLIB (missing: ZLIB_INCLUDE_DIR) (found version "1.2.11")
Call Stack (most recent call first):
/usr/share/cmake-3.16/Modules/FindPackageHandleStandardArgs.cmake:393 (_FPHSA_FAILURE_MESSAGE)
/usr/share/cmake-3.16/Modules/FindZLIB.cmake:115 (FIND_PACKAGE_HANDLE_STANDARD_ARGS)
CMakeLists.txt:7 (find_package)
-- Configuring incomplete, errors occurred!
See also "/home/wtc/tmp/work/build/CMakeFiles/CMakeOutput.log".
$ echo $?
1
Is the second find_package(ZLIB) call expected to fail? If not, could you recommend a workaround for this bug?
Thank you very much for your help!