From b25eb6e0bd1e3b91c44dc4892d529b95c8677889 Mon Sep 17 00:00:00 2001 From: Alexandru Croitor Date: Tue, 13 Apr 2021 18:57:07 +0200 Subject: CMake: Introduce zlib find script to work around hardcoded iOS SDK Xcode allows building a project targeting either the device or simulator sysroot in one single build dir, but for the sysroot switching to work there should be no linker or compiler flags referencing absolute paths of a specific sysroot. During CMake configuration of a project targeting iOS, all found system libraries will be within one single sysroot, either the device one or the simulator one, whichever one was passed to CMAKE_OSX_SYSROOT. CMake will then generate the Xcode project and pass those absolute paths, which makes sysroot switching within Xcode not work. To avoid that, the CMake documentation recommends passing linker and framework flags of the form '-lfoo' and '-framework bar' instead of absolute paths. Xcode then takes care of setting the correct framework search path. Zlib is one of the libraries found in the iOS sysroot and thus passed as absolute path. To avoid that, create a new FindWrapZLIB find script. The target it creates will pass the absolute path to the library on non Apple platforms and an -lz linker flag on Apple platforms (macOS and iOS). To avoid issues with target global promotion when system PNG package is found, ensure that a found ZLIB::ZLIB target is promoted to global manually in src/gui/configure.cmake. Pick-to: 6.1 Change-Id: I8bd8649be4f680a331ad51925f27cb9d13ac5e5f Reviewed-by: Cristian Adam --- cmake/FindWrapZLIB.cmake | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 cmake/FindWrapZLIB.cmake (limited to 'cmake') diff --git a/cmake/FindWrapZLIB.cmake b/cmake/FindWrapZLIB.cmake new file mode 100644 index 0000000000..585dc5e95e --- /dev/null +++ b/cmake/FindWrapZLIB.cmake @@ -0,0 +1,29 @@ +# We can't create the same interface imported target multiple times, CMake will complain if we do +# that. This can happen if the find_package call is done in multiple different subdirectories. +if(TARGET WrapZLIB::WrapZLIB) + set(WrapZLIB_FOUND ON) + return() +endif() + +set(WrapZLIB_FOUND OFF) + +find_package(ZLIB ${WrapZLIB_FIND_VERSION}) + +if(ZLIB_FOUND) + set(WrapZLIB_FOUND ON) + + add_library(WrapZLIB::WrapZLIB INTERFACE IMPORTED) + if(APPLE) + # On Darwin platforms FindZLIB sets IMPORTED_LOCATION to the absolute path of the library + # within the framework. This ends up as an absolute path link flag, which we don't want, + # because that makes our .prl files un-relocatable and also breaks iOS simulator_and_device + # SDK switching in Xcode. + # Just pass a linker flag instead. + target_link_libraries(WrapZLIB::WrapZLIB INTERFACE "-lz") + else() + target_link_libraries(WrapZLIB::WrapZLIB INTERFACE ZLIB::ZLIB) + endif() +endif() + +include(FindPackageHandleStandardArgs) +find_package_handle_standard_args(WrapZLIB DEFAULT_MSG WrapZLIB_FOUND) -- cgit v1.2.3