From c5f01e1dee0d41b8014ac6f1eeafda974917ba17 Mon Sep 17 00:00:00 2001 From: alcroito Date: Wed, 15 Apr 2020 22:06:55 +0200 Subject: [PATCH] Add initial iOS support (#6275) * Add iOS community triplets and toolchain support Added an iOS toolchain to enable building packages for iOS. The toolchain is used when a triplet's VCPKG_CMAKE_SYSTEM_NAME is set to iOS. To configure which architecture should be built, as well as other iOS specifics, the following triplet variables can be set: - VCPKG_TARGET_ARCHITECTURE - VCPKG_OSX_SYSROOT - VCPKG_OSX_DEPLOYMENT_TARGET - VCPKG_OSX_ARCHITECTURES The following VCPKG_TARGET_ARCHITECTURE values are currently supported: - arm, arm64, x64, x86. The following VCPKG_OSX_SYSROOT values are currently supported: - iphoneos, iphonesimulator, or an absolute path to the device or simulator Xcode SDK. VCPKG_OSX_DEPLOYMENT_TARGET can be set to control the minimum iOS delopyment target for the built libraries. CMAKE_OSX_ARCHITECTURES is derived from VCPKG_TARGET_ARCHITECTURE, so generally it should not be set. In case if someone needs to target a more specific architecture (like armv7k or arm64e), it can be set in the triplet via VCPKG_OSX_ARCHITECTURES. Note that only certain combinations of the architecture and sysroot will work: simulator SDKs only provide x86-based libraries, etc. The toolchain also sets CMAKE_SYSTEM_PROCESSOR for certain configurations, because certain packages (like libpng) depend on the processor type. Added 4 community iOS triplets that build static libraries: - arm-ios, arm64-ios, x86-ios, x64-ios. The non-arm triplets target the iOS simulator. The triplets build static libraries because they are easiest to integrate into an iOS project. Dynamic libraries or frameworks require code signing on iOS, which complicates integration. Added heuristics to try and automatically detect what iOS triplet to use when building your own CMake project (so when a CMake project sets CMAKE_TOOLCHAIN_FILE to buildsystems/vcpkg.cmake), if no explicit triplet is provided (VCPKG_TARGET_TRIPLET is undefined). The heuristic checks for the values of CMAKE_SYSTEM_NAME and CMAKE_OSX_ARCHITECTURES. Note that for this to work, CMAKE_OSX_ARCHITECTURES needs to be set before the first project() call in your CMake project. Added workaround so find_package finds vcpkg installed packages when targeting iOS. This is done by saving / restoring the value of CMAKE_FIND_ROOT_PATH while also adding the vcpkg package root in the find_package override macro. The workaround can be removed once vcpkg upgrades to CMake 3.15.0 or higher where the issue is fixed. Fixes: #6003 * Fix building libpng and pcre2 targetting iOS Fixes: #6003 --- .gitignore | 4 ++ docs/users/triplets.md | 4 ++ ports/libpng/CONTROL | 2 +- ports/libpng/portfile.cmake | 6 +++ ports/pcre2/CONTROL | 2 +- ports/pcre2/portfile.cmake | 2 +- scripts/buildsystems/vcpkg.cmake | 50 ++++++++++++++++++++- scripts/cmake/vcpkg_configure_cmake.cmake | 4 +- scripts/toolchains/ios.cmake | 54 +++++++++++++++++++++++ triplets/community/arm-ios.cmake | 4 ++ triplets/community/arm64-ios.cmake | 4 ++ triplets/community/x64-ios.cmake | 4 ++ triplets/community/x86-ios.cmake | 4 ++ 13 files changed, 139 insertions(+), 5 deletions(-) create mode 100644 scripts/toolchains/ios.cmake create mode 100644 triplets/community/arm-ios.cmake create mode 100644 triplets/community/arm64-ios.cmake create mode 100644 triplets/community/x64-ios.cmake create mode 100644 triplets/community/x86-ios.cmake diff --git a/.gitignore b/.gitignore index eba62b6f6..43f63c0df 100644 --- a/.gitignore +++ b/.gitignore @@ -308,6 +308,10 @@ __pycache__/ !triplets/community/x86-android.cmake !triplets/community/arm-android.cmake !triplets/community/arm64-android.cmake +!triplets/community/arm64-ios.cmake +!triplets/community/arm-ios.cmake +!triplets/community/x64-ios.cmake +!triplets/community/x86-ios.cmake !triplets/arm-uwp.cmake !triplets/x64-uwp.cmake !triplets/x64-windows.cmake diff --git a/docs/users/triplets.md b/docs/users/triplets.md index 080475651..30fd66eea 100644 --- a/docs/users/triplets.md +++ b/docs/users/triplets.md @@ -140,6 +140,10 @@ Sets the minimum macOS version for compiled binaries. This also changes what ver ### VCPKG_OSX_SYSROOT Set the name or path of the macOS platform SDK that will be used by CMake. See the CMake documentation for [CMAKE_OSX_SYSROOT](https://cmake.org/cmake/help/latest/variable/CMAKE_OSX_SYSROOT.html) for more information. + +### VCPKG_OSX_ARCHITECTURES +Set the macOS / iOS target architecture which will be used by CMake. See the CMake documentation for [CMAKE_OSX_ARCHITECTURES](https://cmake.org/cmake/help/latest/variable/CMAKE_OSX_ARCHITECTURES.html) for more information. + ## Per-port customization The CMake Macro `PORT` will be set when interpreting the triplet file and can be used to change settings (such as `VCPKG_LIBRARY_LINKAGE`) on a per-port basis. diff --git a/ports/libpng/CONTROL b/ports/libpng/CONTROL index d03709b00..173c3a0d3 100644 --- a/ports/libpng/CONTROL +++ b/ports/libpng/CONTROL @@ -1,5 +1,5 @@ Source: libpng -Version: 1.6.37-6 +Version: 1.6.37-7 Build-Depends: zlib Homepage: https://github.com/glennrp/libpng Description: libpng is a library implementing an interface for reading and writing PNG (Portable Network Graphics) format files. diff --git a/ports/libpng/portfile.cmake b/ports/libpng/portfile.cmake index e9a200455..cc3ee459c 100644 --- a/ports/libpng/portfile.cmake +++ b/ports/libpng/portfile.cmake @@ -47,11 +47,17 @@ else() set(PNG_SHARED_LIBS OFF) endif() +set(LIBPNG_HARDWARE_OPTIMIZATIONS_OPTION ) +if(VCPKG_CMAKE_SYSTEM_NAME STREQUAL iOS) + set(LIBPNG_HARDWARE_OPTIMIZATIONS_OPTION "-DPNG_HARDWARE_OPTIMIZATIONS=OFF") +endif() + vcpkg_configure_cmake( SOURCE_PATH ${SOURCE_PATH} PREFER_NINJA OPTIONS ${LIBPNG_APNG_OPTION} + ${LIBPNG_HARDWARE_OPTIMIZATIONS_OPTION} -DPNG_STATIC=${PNG_STATIC_LIBS} -DPNG_SHARED=${PNG_SHARED_LIBS} -DPNG_TESTS=OFF diff --git a/ports/pcre2/CONTROL b/ports/pcre2/CONTROL index be71b866e..044bb81d3 100644 --- a/ports/pcre2/CONTROL +++ b/ports/pcre2/CONTROL @@ -1,4 +1,4 @@ Source: pcre2 -Version: 10.30-6 +Version: 10.30-7 Homepage: https://pcre.org/ Description: PCRE2 is a re-working of the original Perl Compatible Regular Expressions library diff --git a/ports/pcre2/portfile.cmake b/ports/pcre2/portfile.cmake index 199f72a59..9057ede47 100644 --- a/ports/pcre2/portfile.cmake +++ b/ports/pcre2/portfile.cmake @@ -14,7 +14,7 @@ vcpkg_extract_source_archive_ex( fix-uwp.patch ) -if(VCPKG_CMAKE_SYSTEM_NAME STREQUAL "Emscripten") +if(VCPKG_CMAKE_SYSTEM_NAME STREQUAL "Emscripten" OR VCPKG_CMAKE_SYSTEM_NAME STREQUAL "iOS") set(JIT OFF) else() set(JIT ON) diff --git a/scripts/buildsystems/vcpkg.cmake b/scripts/buildsystems/vcpkg.cmake index 1eebc3b8d..963546e98 100644 --- a/scripts/buildsystems/vcpkg.cmake +++ b/scripts/buildsystems/vcpkg.cmake @@ -76,6 +76,37 @@ else() set(_VCPKG_TARGET_TRIPLET_ARCH arm64) elseif(_VCPKG_CL MATCHES "bin/cl.exe$" OR _VCPKG_CL MATCHES "x86/cl.exe$") set(_VCPKG_TARGET_TRIPLET_ARCH x86) + elseif(CMAKE_HOST_SYSTEM_NAME STREQUAL "Darwin" AND DEFINED CMAKE_SYSTEM_NAME AND NOT CMAKE_SYSTEM_NAME STREQUAL "Darwin") + list(LENGTH CMAKE_OSX_ARCHITECTURES arch_count) + if(arch_count EQUAL 0) + message(WARNING "Unable to determine target architecture. " + "Consider providing a value for the CMAKE_OSX_ARCHITECTURES cache variable. " + "Continuing without vcpkg.") + set(VCPKG_TOOLCHAIN ON) + return() + else() + if(arch_count GREATER 1) + message(WARNING "Detected more than one target architecture. Using the first one.") + endif() + list(GET CMAKE_OSX_ARCHITECTURES 0 target_arch) + if(target_arch STREQUAL arm64) + set(_VCPKG_TARGET_TRIPLET_ARCH arm64) + elseif(target_arch STREQUAL arm64s) + set(_VCPKG_TARGET_TRIPLET_ARCH arm64s) + elseif(target_arch STREQUAL armv7s) + set(_VCPKG_TARGET_TRIPLET_ARCH armv7s) + elseif(target_arch STREQUAL armv7) + set(_VCPKG_TARGET_TRIPLET_ARCH arm) + elseif(target_arch STREQUAL x86_64) + set(_VCPKG_TARGET_TRIPLET_ARCH x64) + elseif(target_arch STREQUAL i386) + set(_VCPKG_TARGET_TRIPLET_ARCH x86) + else() + message(WARNING "Unable to determine target architecture, continuing without vcpkg.") + set(VCPKG_TOOLCHAIN ON) + return() + endif() + endif() elseif(CMAKE_HOST_SYSTEM_PROCESSOR STREQUAL "x86_64") set(_VCPKG_TARGET_TRIPLET_ARCH x64) else() @@ -96,6 +127,8 @@ elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux" OR (NOT CMAKE_SYSTEM_NAME AND CMAKE_HO set(_VCPKG_TARGET_TRIPLET_PLAT linux) elseif(CMAKE_SYSTEM_NAME STREQUAL "Darwin" OR (NOT CMAKE_SYSTEM_NAME AND CMAKE_HOST_SYSTEM_NAME STREQUAL "Darwin")) set(_VCPKG_TARGET_TRIPLET_PLAT osx) +elseif(CMAKE_SYSTEM_NAME STREQUAL "iOS") + set(_VCPKG_TARGET_TRIPLET_PLAT ios) elseif(CMAKE_SYSTEM_NAME STREQUAL "Windows" OR (NOT CMAKE_SYSTEM_NAME AND CMAKE_HOST_SYSTEM_NAME STREQUAL "Windows")) set(_VCPKG_TARGET_TRIPLET_PLAT windows) elseif(CMAKE_SYSTEM_NAME STREQUAL "FreeBSD" OR (NOT CMAKE_SYSTEM_NAME AND CMAKE_HOST_SYSTEM_NAME STREQUAL "FreeBSD")) @@ -146,6 +179,8 @@ else() #Release build: Put Release paths before Debug paths. Debug Paths are req ) endif() +set(VCPKG_CMAKE_FIND_ROOT_PATH ${CMAKE_FIND_ROOT_PATH}) + file(TO_CMAKE_PATH "$ENV{PROGRAMFILES}" _programfiles) set(_PROGRAMFILESX86 "PROGRAMFILES(x86)") file(TO_CMAKE_PATH "$ENV{${_PROGRAMFILESX86}}" _programfiles_x86) @@ -235,7 +270,15 @@ if(NOT DEFINED VCPKG_OVERRIDE_FIND_PACKAGE_NAME) set(VCPKG_OVERRIDE_FIND_PACKAGE_NAME find_package) endif() macro(${VCPKG_OVERRIDE_FIND_PACKAGE_NAME} name) + # Workaround to set the ROOT_PATH until upstream CMake stops overriding + # the ROOT_PATH at apple OS initialization phase. + # See https://gitlab.kitware.com/cmake/cmake/merge_requests/3273 + if(CMAKE_SYSTEM_NAME STREQUAL iOS) + set(BACKUP_CMAKE_FIND_ROOT_PATH ${CMAKE_FIND_ROOT_PATH}) + list(APPEND CMAKE_FIND_ROOT_PATH ${VCPKG_CMAKE_FIND_ROOT_PATH}) + endif() string(TOLOWER "${name}" _vcpkg_lowercase_name) + if(EXISTS "${_VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/share/${_vcpkg_lowercase_name}/vcpkg-cmake-wrapper.cmake") set(ARGS "${ARGV}") include(${_VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/share/${_vcpkg_lowercase_name}/vcpkg-cmake-wrapper.cmake) @@ -285,6 +328,9 @@ macro(${VCPKG_OVERRIDE_FIND_PACKAGE_NAME} name) else() _find_package(${ARGV}) endif() + if(CMAKE_SYSTEM_NAME STREQUAL iOS) + set(CMAKE_FIND_ROOT_PATH "${BACKUP_CMAKE_FIND_ROOT_PATH}") + endif() endmacro() set(VCPKG_TOOLCHAIN ON) @@ -301,13 +347,15 @@ if(NOT _CMAKE_IN_TRY_COMPILE) file(TO_CMAKE_PATH "${_VCPKG_ROOT_DIR}" _root_dir) file(WRITE "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/vcpkg.config.cmake" "set(VCPKG_TARGET_TRIPLET \"${VCPKG_TARGET_TRIPLET}\" CACHE STRING \"\")\n" + "set(VCPKG_TARGET_ARCHITECTURE \"${VCPKG_TARGET_ARCHITECTURE}\" CACHE STRING \"\")\n" "set(VCPKG_APPLOCAL_DEPS \"${VCPKG_APPLOCAL_DEPS}\" CACHE STRING \"\")\n" "set(VCPKG_CHAINLOAD_TOOLCHAIN_FILE \"${_chainload_file}\" CACHE STRING \"\")\n" "set(_VCPKG_ROOT_DIR \"${_root_dir}\" CACHE STRING \"\")\n" ) else() - list(APPEND CMAKE_TRY_COMPILE_PLATFORM_VARIABLES + list(APPEND CMAKE_TRY_COMPILE_PLATFORM_VARIABLES VCPKG_TARGET_TRIPLET + VCPKG_TARGET_ARCHITECTURE VCPKG_APPLOCAL_DEPS VCPKG_CHAINLOAD_TOOLCHAIN_FILE _VCPKG_ROOT_DIR diff --git a/scripts/cmake/vcpkg_configure_cmake.cmake b/scripts/cmake/vcpkg_configure_cmake.cmake index 9766bd267..ab65a5683 100644 --- a/scripts/cmake/vcpkg_configure_cmake.cmake +++ b/scripts/cmake/vcpkg_configure_cmake.cmake @@ -202,6 +202,8 @@ function(vcpkg_configure_cmake) set(VCPKG_CHAINLOAD_TOOLCHAIN_FILE "${SCRIPTS}/toolchains/android.cmake") elseif(VCPKG_CMAKE_SYSTEM_NAME STREQUAL "Darwin") set(VCPKG_CHAINLOAD_TOOLCHAIN_FILE "${SCRIPTS}/toolchains/osx.cmake") + elseif(VCPKG_CMAKE_SYSTEM_NAME STREQUAL "iOS") + set(VCPKG_CHAINLOAD_TOOLCHAIN_FILE "${SCRIPTS}/toolchains/ios.cmake") elseif(VCPKG_CMAKE_SYSTEM_NAME STREQUAL "FreeBSD") set(VCPKG_CHAINLOAD_TOOLCHAIN_FILE "${SCRIPTS}/toolchains/freebsd.cmake") elseif(VCPKG_CMAKE_SYSTEM_NAME STREQUAL "MinGW") @@ -243,7 +245,7 @@ function(vcpkg_configure_cmake) endif() # Sets configuration variables for macOS builds - foreach(config_var INSTALL_NAME_DIR OSX_DEPLOYMENT_TARGET OSX_SYSROOT) + foreach(config_var INSTALL_NAME_DIR OSX_DEPLOYMENT_TARGET OSX_SYSROOT OSX_ARCHITECTURES) if(DEFINED VCPKG_${config_var}) list(APPEND _csc_OPTIONS "-DCMAKE_${config_var}=${VCPKG_${config_var}}") endif() diff --git a/scripts/toolchains/ios.cmake b/scripts/toolchains/ios.cmake new file mode 100644 index 000000000..5497117a3 --- /dev/null +++ b/scripts/toolchains/ios.cmake @@ -0,0 +1,54 @@ +if(NOT _VCPKG_IOS_TOOLCHAIN) + set(_VCPKG_IOS_TOOLCHAIN 1) + + # Set the CMAKE_SYSTEM_NAME for try_compile calls. + set(CMAKE_SYSTEM_NAME iOS CACHE STRING "") + + macro(_vcpkg_setup_ios_arch arch) + unset(_vcpkg_ios_system_processor) + unset(_vcpkg_ios_sysroot) + unset(_vcpkg_ios_target_architecture) + + if ("${arch}" STREQUAL "arm64") + set(_vcpkg_ios_system_processor "aarch64") + set(_vcpkg_ios_target_architecture "arm64") + elseif("${arch}" STREQUAL "arm") + set(_vcpkg_ios_system_processor "arm") + set(_vcpkg_ios_target_architecture "armv7") + elseif("${arch}" STREQUAL "x64") + set(_vcpkg_ios_sysroot "iphonesimulator") + set(_vcpkg_ios_target_architecture "x86_64") + elseif("${arch}" STREQUAL "x86") + set(_vcpkg_ios_sysroot "iphonesimulator") + set(_vcpkg_ios_target_architecture "i386") + else() + message(FATAL_ERROR + "Unknown VCPKG_TARGET_ARCHITECTURE value provided for triplet ${VCPKG_TARGET_TRIPLET}: ${arch}") + endif() + endmacro() + + _vcpkg_setup_ios_arch("${VCPKG_TARGET_ARCHITECTURE}") + if(_vcpkg_ios_system_processor AND NOT CMAKE_SYSTEM_PROCESSOR) + set(CMAKE_SYSTEM_PROCESSOR ${_vcpkg_ios_system_processor}) + endif() + + # If VCPKG_OSX_ARCHITECTURES or VCPKG_OSX_SYSROOT is set in the triplet, they will take priority, + # so the following will be no-ops. + set(CMAKE_OSX_ARCHITECTURES "${_vcpkg_ios_target_architecture}" CACHE STRING "Build architectures for iOS") + if(_vcpkg_ios_sysroot) + set(CMAKE_OSX_SYSROOT ${_vcpkg_ios_sysroot} CACHE STRING "iOS sysroot") + endif() + + get_property( _CMAKE_IN_TRY_COMPILE GLOBAL PROPERTY IN_TRY_COMPILE ) + if(NOT _CMAKE_IN_TRY_COMPILE) + string(APPEND CMAKE_C_FLAGS_INIT " -fPIC ${VCPKG_C_FLAGS} ") + string(APPEND CMAKE_CXX_FLAGS_INIT " -fPIC ${VCPKG_CXX_FLAGS} ") + string(APPEND CMAKE_C_FLAGS_DEBUG_INIT " ${VCPKG_C_FLAGS_DEBUG} ") + string(APPEND CMAKE_CXX_FLAGS_DEBUG_INIT " ${VCPKG_CXX_FLAGS_DEBUG} ") + string(APPEND CMAKE_C_FLAGS_RELEASE_INIT " ${VCPKG_C_FLAGS_RELEASE} ") + string(APPEND CMAKE_CXX_FLAGS_RELEASE_INIT " ${VCPKG_CXX_FLAGS_RELEASE} ") + + string(APPEND CMAKE_SHARED_LINKER_FLAGS_INIT " ${VCPKG_LINKER_FLAGS} ") + string(APPEND CMAKE_EXE_LINKER_FLAGS_INIT " ${VCPKG_LINKER_FLAGS} ") + endif() +endif() diff --git a/triplets/community/arm-ios.cmake b/triplets/community/arm-ios.cmake new file mode 100644 index 000000000..7fee3751d --- /dev/null +++ b/triplets/community/arm-ios.cmake @@ -0,0 +1,4 @@ +set(VCPKG_TARGET_ARCHITECTURE arm) +set(VCPKG_CRT_LINKAGE dynamic) +set(VCPKG_LIBRARY_LINKAGE static) +set(VCPKG_CMAKE_SYSTEM_NAME iOS) diff --git a/triplets/community/arm64-ios.cmake b/triplets/community/arm64-ios.cmake new file mode 100644 index 000000000..c632e9381 --- /dev/null +++ b/triplets/community/arm64-ios.cmake @@ -0,0 +1,4 @@ +set(VCPKG_TARGET_ARCHITECTURE arm64) +set(VCPKG_CRT_LINKAGE dynamic) +set(VCPKG_LIBRARY_LINKAGE static) +set(VCPKG_CMAKE_SYSTEM_NAME iOS) diff --git a/triplets/community/x64-ios.cmake b/triplets/community/x64-ios.cmake new file mode 100644 index 000000000..0c93d6c68 --- /dev/null +++ b/triplets/community/x64-ios.cmake @@ -0,0 +1,4 @@ +set(VCPKG_TARGET_ARCHITECTURE x64) +set(VCPKG_CRT_LINKAGE dynamic) +set(VCPKG_LIBRARY_LINKAGE static) +set(VCPKG_CMAKE_SYSTEM_NAME iOS) diff --git a/triplets/community/x86-ios.cmake b/triplets/community/x86-ios.cmake new file mode 100644 index 000000000..143b373b9 --- /dev/null +++ b/triplets/community/x86-ios.cmake @@ -0,0 +1,4 @@ +set(VCPKG_TARGET_ARCHITECTURE x86) +set(VCPKG_CRT_LINKAGE dynamic) +set(VCPKG_LIBRARY_LINKAGE static) +set(VCPKG_CMAKE_SYSTEM_NAME iOS)