Skip to content

Commit

Permalink
Add iOS triplets and iOS toolchain
Browse files Browse the repository at this point in the history
The toolchain is automatically picked up when CMAKE_SYSTEM_NAME
equals iOS. The toolchain sets the architectures for which to build
packages, as well as the processor type (because certain packages like
libpng depend on their value).

Automatic detection of triplets for iOS / tvOS / watchOS is added when
using the vcpkg.cmake toolchain while building your own application.
It uses the values of CMAKE_SYSTEM_NAME and CMAKE_OSX_ARCHITECTURES
to detect the triplet.

Fix (in a hacky way) find_package to find vcpkg installed packages.
Reset the CMAKE_FIND_ROOT_PATH value, until upstream CMake removes its
hardcoding of CMAKE_FIND_ROOT_PATH to Apple's SDK sysroot.

Fixes: microsoft#6003
  • Loading branch information
alcroito committed May 1, 2019
1 parent e03ee9c commit 167a9f5
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,10 @@ __pycache__/
!triplets/arm64-windows.cmake
!triplets/x64-linux.cmake
!triplets/x64-osx.cmake
!triplets/arm64-ios.cmake
!triplets/arm-ios.cmake
!triplets/x64-ios.cmake
!triplets/x86-ios.cmake
*.exe
*.zip

Expand Down
49 changes: 49 additions & 0 deletions scripts/buildsystems/vcpkg.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,36 @@ 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 "vcpkg could not determine which architecture to build for. Please set a value for the CMAKE_OSX_ARCHITECTURES cache variable.")
set(VCPKG_TOOLCHAIN ON)
return()
elseif(NOT arch_count EQUAL 1)
message(WARNING "vcpkg does not support building for more than 1 architecture at a time.")
set(VCPKG_TOOLCHAIN ON)
return()
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 triplet arch from given arch: '${target_arch}'.")
set(VCPKG_TOOLCHAIN ON)
return()
endif()

elseif(CMAKE_HOST_SYSTEM_PROCESSOR STREQUAL "x86_64")
set(_VCPKG_TARGET_TRIPLET_ARCH x64)
else()
Expand All @@ -79,6 +109,12 @@ 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 "tvOS")
set(_VCPKG_TARGET_TRIPLET_PLAT tvos)
elseif(CMAKE_SYSTEM_NAME STREQUAL "watchOS")
set(_VCPKG_TARGET_TRIPLET_PLAT watchos)
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"))
Expand Down Expand Up @@ -129,6 +165,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(CMAKE_SYSTEM_IGNORE_PATH
"${_programfiles}/OpenSSL"
Expand Down Expand Up @@ -195,7 +233,15 @@ function(add_library name)
endfunction()

macro(find_package 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)
Expand Down Expand Up @@ -245,6 +291,9 @@ macro(find_package 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)
Expand Down
2 changes: 2 additions & 0 deletions scripts/cmake/vcpkg_configure_cmake.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@ function(vcpkg_configure_cmake)
list(APPEND _csc_OPTIONS "-DVCPKG_CHAINLOAD_TOOLCHAIN_FILE=${VCPKG_ROOT_DIR}/scripts/toolchains/android.cmake")
elseif(VCPKG_CMAKE_SYSTEM_NAME STREQUAL "Darwin")
list(APPEND _csc_OPTIONS "-DVCPKG_CHAINLOAD_TOOLCHAIN_FILE=${VCPKG_ROOT_DIR}/scripts/toolchains/osx.cmake")
elseif(VCPKG_CMAKE_SYSTEM_NAME STREQUAL "iOS")
list(APPEND _csc_OPTIONS "-DVCPKG_CHAINLOAD_TOOLCHAIN_FILE=${VCPKG_ROOT_DIR}/scripts/toolchains/ios.cmake")
elseif(VCPKG_CMAKE_SYSTEM_NAME STREQUAL "FreeBSD")
list(APPEND _csc_OPTIONS "-DVCPKG_CHAINLOAD_TOOLCHAIN_FILE=${VCPKG_ROOT_DIR}/scripts/toolchains/freebsd.cmake")
endif()
Expand Down
32 changes: 32 additions & 0 deletions scripts/toolchains/ios.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
if(NOT _VCPKG_IOS_TOOLCHAIN)
set(_VCPKG_IOS_TOOLCHAIN 1)

if (VCPKG_TARGET_TRIPLET STREQUAL "arm64-ios")
set(CMAKE_OSX_ARCHITECTURES arm64 CACHE STRING "")
set(CMAKE_SYSTEM_PROCESSOR aarch64)
elseif(VCPKG_TARGET_TRIPLET STREQUAL "arm-ios")
set(CMAKE_OSX_ARCHITECTURES arm CACHE STRING "")
set(CMAKE_SYSTEM_PROCESSOR arm)
elseif(VCPKG_TARGET_TRIPLET STREQUAL "x64-ios")
set(CMAKE_OSX_ARCHITECTURES x86_64 CACHE STRING "")
elseif(VCPKG_TARGET_TRIPLET STREQUAL "x86-ios")
set(CMAKE_OSX_ARCHITECTURES i386 CACHE STRING "")
else()
message(FATAL_ERROR "Unknown ABI for target triplet ${VCPKG_TARGET_TRIPLET}")
endif()

set(CMAKE_SYSTEM_NAME iOS CACHE STRING "")

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()
4 changes: 4 additions & 0 deletions triplets/arm-ios.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
set(VCPKG_TARGET_ARCHITECTURE arm)
set(VCPKG_CRT_LINKAGE dynamic)
set(VCPKG_LIBRARY_LINKAGE dynamic)
set(VCPKG_CMAKE_SYSTEM_NAME iOS)
4 changes: 4 additions & 0 deletions triplets/arm64-ios.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
set(VCPKG_TARGET_ARCHITECTURE arm64)
set(VCPKG_CRT_LINKAGE dynamic)
set(VCPKG_LIBRARY_LINKAGE dynamic)
set(VCPKG_CMAKE_SYSTEM_NAME iOS)
4 changes: 4 additions & 0 deletions triplets/x64-ios.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
set(VCPKG_TARGET_ARCHITECTURE x64)
set(VCPKG_CRT_LINKAGE dynamic)
set(VCPKG_LIBRARY_LINKAGE dynamic)
set(VCPKG_CMAKE_SYSTEM_NAME iOS)
4 changes: 4 additions & 0 deletions triplets/x86-ios.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
set(VCPKG_TARGET_ARCHITECTURE x86)
set(VCPKG_CRT_LINKAGE dynamic)
set(VCPKG_LIBRARY_LINKAGE dynamic)
set(VCPKG_CMAKE_SYSTEM_NAME iOS)

0 comments on commit 167a9f5

Please sign in to comment.