← All tasks
cpppocoproject/poco #5330Not a task: already works

POCO_UNBUNDLED=ON build fails when ZLIB is a transitive dependency of another found package

envgap__pocoproject__poco-5330

01 / FAILURE SIGNATURE

As reported upstream

Attempt to promote imported target "ZLIB::ZLIB" to global scope (by setting
Not a benchmark task.
  • The project already builds and runs before the fix, so there is nothing to repair.

02 / ENVIRONMENT RECIPE

Base commit
c8dd0172f026fb1fe146a9c8c3736baac823b19a
Manifest
CMakeLists.txt
Reproduce
Awaiting issue-specific recipe
Run under trace
Awaiting a meaningful runtime command

03 / ORIGINAL ISSUE TEXT

pocoproject/poco #5330 · read the original issue
Greetings Poco maintainers!

We are working on packaging Poco 1.15.2 in Conan via PR https://github.com/conan-io/conan-center-index/pull/29722. It's great to see the reformulation in 1.15.0 with unbundled dependencies 👏 

**Describe the bug**

During the same build as done for 1.14.z, but using both 1.15.0 and 1.15.2, we found the following error during the cmake setup step:

```
poco/1.15.2: RUN: cmake -G "Ninja" -DCMAKE_TOOLCHAIN_FILE="generators/conan_toolchain.cmake" -DCMAKE_INSTALL_PREFIX="/home/uilian/.conan2/p/b/poco5c5b819697edd/p" -DPOCO_UNBUNDLED="ON" -DPOCO_MINIMAL_BUILD="ON" -DCMAKE_DISABLE_FIND_PACKAGE_APR="ON" -DCMAKE_DISABLE_FIND_PACKAGE_APRUTIL="ON" -DCMAKE_DISABLE_FIND_PACKAGE_Apache2="ON" -DMYSQL_FOUND="ON" -DPOSTGRESQL_FOUND="ON" -DCMAKE_POLICY_DEFAULT_CMP0091="NEW" -DCMAKE_BUILD_TYPE="Release" "/home/uilian/.conan2/p/b/poco5c5b819697edd/b/src" --loglevel=VERBOSE
-- Using Conan toolchain: /home/uilian/.conan2/p/b/poco5c5b819697edd/b/build/Release/generators/conan_toolchain.cmake
-- Conan toolchain: Setting CMAKE_POSITION_INDEPENDENT_CODE=ON (options.fPIC)
-- Conan toolchain: Defining architecture flag: -m64
-- Conan toolchain: C++ Standard 20 with extensions ON
-- Conan toolchain: Setting BUILD_SHARED_LIBS = OFF
-- The C compiler identification is GNU 13.1.0
-- The CXX compiler identification is GNU 13.1.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/gcc-13 - 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/g++-13 - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Checking for C++20 compiler
-- Checking for C++20 compiler - available
-- Building Poco with C++20 standard
-- Building without unit tests
-- Building without samples
-- Using external zlib, pcre2, utf8proc, expat, sqlite
...
-- Conan: Found: /home/uilian/.conan2/p/zlib76e00a316e585/p/lib/libz.a
CMake Error at dependencies/zlib/CMakeLists.txt:12 (set_target_properties):
  Attempt to promote imported target "ZLIB::ZLIB" to global scope (by setting
  IMPORTED_GLOBAL) which is not built in this directory.
```

The error is related to a sum of factors, related to `POCO_UNBUNDLED=ON`.
In `CMakeLists.txt`, the `find_package(OpenSSL ...)` is called **before** `add_subdirectory(dependencies)`. OpenSSL has ZLIB as a transitive `find_dependency`, so `ZLIB::ZLIB` gets created at the top-level `CMakeLists.txt` scope.
When `dependencies/zlib/CMakeLists.txt` subsequently calls `find_package(ZLIB REQUIRED)` (ZLIB_FOUND is already TRUE) and then tries to promote the target as `GLOBAL`, but is rejected by CMake as a hard-rule documented in [IMPORTED_GLOBAL](https://cmake.org/cmake/help/latest/prop_tgt/IMPORTED_GLOBAL.html) - IMPORTED_GLOBAL TRUE can only be set on a target in the same directory where it was created.

**How To Reproduce**
I used Conan to build (See the build log), but it can be achieved using external dependencies.

* It will need all static.
* OpenSSL should require Zlib: https://gitlab.kitware.com/cmake/cmake/blob/master/Modules/FindOpenSSL.cmake#L225
* Poco should require Zlib.
* Using CMake >=3.26

**Expected behavior**
Using `POCO_UNBUNDLED=ON` with OpenSSL + Zlib should be an acceptable scenario.

**Logs**
Built using Conan: [poco-1.15.2-linux-amd64-gcc13-relase-static.log](https://github.com/user-attachments/files/26937530/poco-1.15.2-linux-amd64-gcc13-relase-static.log)

**Screenshots**
If applicable, add screenshots to help explain your problem.

**Please add relevant environment information:**
 - OS Type and Version: Linux, amd64, GCC-13, C++20
 - POCO Version: 1.15.0 and 1.15.2
 - Third-party product (eg. database or library) type and version: Error occurs with both CMake 3.26.3 and 4.3.1

**Additional context**

I suggest replacing the current usage with `IMPORTED_GLOBAL`: https://github.com/pocoproject/poco/blob/main/dependencies/zlib/CMakeLists.txt#L12

By the simplification supported since CMake 3.24 (Poco requires CMake 3.26):

```cmake
find_package(ZLIB REQUIRED GLOBAL)
``` 

Regards! 
Continue on GitHub ↗

04 / LABELS

Labels from the report text only; not yet run

No supported category has been assigned.

Label rules and the text that matched
[]