2023-06-29 19:46:52 +08:00
|
|
|
#
|
2023-10-07 13:13:08 +08:00
|
|
|
# The minimal ios toolchain file: https://github.com/yasio/yasio/blob/dev/cmake/ios.cmake
|
2024-01-11 01:42:58 +08:00
|
|
|
# version: 4.1.2
|
2023-06-29 19:46:52 +08:00
|
|
|
#
|
|
|
|
# The supported params:
|
|
|
|
# PLAT: iOS, tvOS, default: iOS
|
|
|
|
# ARCHS: arm64, x86_64, default: arm64
|
2023-10-07 13:13:08 +08:00
|
|
|
# DEPLOYMENT_TARGET: default: iOS=11.0/12.0, tvOS=15.0, watchOS=8.0
|
2023-06-29 19:46:52 +08:00
|
|
|
# SIMULATOR: TRUE, FALSE, UNDEFINED(auto determine by archs)
|
|
|
|
# ENABLE_BITCODE: FALSE(default)
|
2023-10-07 12:36:33 +08:00
|
|
|
#
|
|
|
|
# !!!Note: iOS simulator, there is no xcode General tab, and we must set
|
|
|
|
# CMAKE_OSX_SYSROOT properly for simulator, otherwise will lead cmake based
|
|
|
|
# project detect C/C++ header from device sysroot which is not present in
|
|
|
|
# simulator sysroot, then cause compiling errors
|
|
|
|
#
|
2023-06-29 19:46:52 +08:00
|
|
|
#
|
|
|
|
|
|
|
|
# PLAT
|
|
|
|
if(NOT DEFINED PLAT)
|
|
|
|
set(PLAT iOS CACHE STRING "" FORCE)
|
|
|
|
endif()
|
|
|
|
|
|
|
|
# ARCHS
|
|
|
|
if(NOT DEFINED ARCHS)
|
|
|
|
set(ARCHS "arm64" CACHE STRING "" FORCE)
|
|
|
|
endif()
|
|
|
|
|
|
|
|
# DEPLOYMENT_TARGET
|
|
|
|
if(NOT DEFINED DEPLOYMENT_TARGET)
|
|
|
|
if (PLAT STREQUAL "iOS")
|
|
|
|
if("${ARCHS}" MATCHES ".*armv7.*")
|
|
|
|
set(DEPLOYMENT_TARGET "10.0" CACHE STRING "" FORCE)
|
|
|
|
else()
|
2023-10-07 13:13:08 +08:00
|
|
|
if (XCODE_VERSION LESS "14.3.0")
|
|
|
|
set(DEPLOYMENT_TARGET "11.0" CACHE STRING "" FORCE)
|
|
|
|
else() # xcode 14.3+ require 12.0 for c++ std::get
|
|
|
|
set(DEPLOYMENT_TARGET "12.0" CACHE STRING "" FORCE)
|
|
|
|
endif()
|
2023-06-29 19:46:52 +08:00
|
|
|
endif()
|
|
|
|
elseif (PLAT STREQUAL "tvOS")
|
|
|
|
set(DEPLOYMENT_TARGET "15.0" CACHE STRING "" FORCE)
|
|
|
|
elseif (PLAT STREQUAL "watchOS")
|
|
|
|
set(DEPLOYMENT_TARGET "8.0" CACHE STRING "" FORCE)
|
|
|
|
else()
|
|
|
|
message(FATAL_ERROR "PLAT=${PLAT} unsupported!")
|
|
|
|
endif()
|
|
|
|
endif()
|
|
|
|
|
|
|
|
# SIMULATOR, regards x86_64 as simulator if SIMULATOR not defined
|
|
|
|
if((NOT DEFINED SIMULATOR) AND ("${ARCHS}" STREQUAL "x86_64"))
|
|
|
|
set(SIMULATOR TRUE CACHE BOOL "" FORCE)
|
|
|
|
endif()
|
|
|
|
|
|
|
|
# ENABLE_BITCODE, default OFF, xcode14: Building with bitcode is deprecated. Please update your project and/or target settings to disable bitcode
|
|
|
|
if(NOT DEFINED ENABLE_BITCODE)
|
|
|
|
set(ENABLE_BITCODE FALSE CACHE BOOL "" FORCE)
|
|
|
|
endif()
|
|
|
|
|
|
|
|
# apply params
|
|
|
|
set(CMAKE_SYSTEM_NAME ${PLAT} CACHE STRING "")
|
|
|
|
set(CMAKE_OSX_ARCHITECTURES ${ARCHS} CACHE STRING "")
|
|
|
|
set(CMAKE_OSX_DEPLOYMENT_TARGET ${DEPLOYMENT_TARGET} CACHE STRING "")
|
|
|
|
|
|
|
|
# The best solution for fix try_compile failed with code sign currently
|
|
|
|
# since cmake-3.18.2, not required
|
|
|
|
# everyting for cmake toolchain config before project(xxx) is better
|
|
|
|
set(CMAKE_TRY_COMPILE_PLATFORM_VARIABLES
|
|
|
|
${CMAKE_TRY_COMPILE_PLATFORM_VARIABLES}
|
|
|
|
"CMAKE_XCODE_ATTRIBUTE_CODE_SIGNING_REQUIRED"
|
|
|
|
"CMAKE_XCODE_ATTRIBUTE_CODE_SIGNING_ALLOWED")
|
|
|
|
set(CMAKE_XCODE_ATTRIBUTE_CODE_SIGNING_REQUIRED NO)
|
|
|
|
set(CMAKE_XCODE_ATTRIBUTE_CODE_SIGNING_ALLOWED NO)
|
|
|
|
|
|
|
|
# set(CMAKE_BUILD_WITH_INSTALL_RPATH YES)
|
|
|
|
# set(CMAKE_INSTALL_RPATH "@executable_path/Frameworks")
|
|
|
|
# set(CMAKE_XCODE_ATTRIBUTE_LD_RUNPATH_SEARCH_PATHS "@executable_path/Frameworks" ${CMAKE_XCODE_ATTRIBUTE_LD_RUNPATH_SEARCH_PATHS})
|
|
|
|
|
|
|
|
# Fix compile failed with armv7 deployment target >= 11.0, xcode clang will report follow error
|
|
|
|
# clang: error: invalid iOS deployment version '--target=armv7-apple-ios13.6',
|
|
|
|
# iOS 10 is the maximum deployment target for 32-bit targets
|
|
|
|
# If not defined CMAKE_OSX_DEPLOYMENT_TARGET, cmake will choose latest deployment target
|
|
|
|
if(NOT DEFINED CMAKE_XCODE_ATTRIBUTE_IPHONEOS_DEPLOYMENT_TARGET)
|
|
|
|
set(CMAKE_XCODE_ATTRIBUTE_IPHONEOS_DEPLOYMENT_TARGET ${CMAKE_OSX_DEPLOYMENT_TARGET} CACHE STRING "")
|
|
|
|
endif()
|
|
|
|
|
2024-01-11 01:42:58 +08:00
|
|
|
if (NOT SIMULATOR)
|
|
|
|
if(PLAT STREQUAL "iOS")
|
|
|
|
set (CMAKE_XCODE_EFFECTIVE_PLATFORMS "-iphoneos")
|
|
|
|
elseif(PLAT STREQUAL "tvOS")
|
|
|
|
set (CMAKE_XCODE_EFFECTIVE_PLATFORMS "-appletvos")
|
|
|
|
elseif(PLAT STREQUAL "watchOS")
|
|
|
|
set (CMAKE_XCODE_EFFECTIVE_PLATFORMS "-watchos")
|
|
|
|
endif()
|
|
|
|
else()
|
2023-10-07 12:36:33 +08:00
|
|
|
if (PLAT STREQUAL "iOS")
|
|
|
|
set(_SDK_NAME "iphonesimulator")
|
2024-01-11 01:42:58 +08:00
|
|
|
set (CMAKE_XCODE_EFFECTIVE_PLATFORMS "-iphonesimulator")
|
2023-10-07 12:36:33 +08:00
|
|
|
elseif(PLAT STREQUAL "tvOS")
|
|
|
|
set(_SDK_NAME "appletvsimulator")
|
2024-01-11 01:42:58 +08:00
|
|
|
set (CMAKE_XCODE_EFFECTIVE_PLATFORMS "-tvsimulator")
|
|
|
|
|
2023-10-07 12:36:33 +08:00
|
|
|
elseif(PLAT STREQUAL "watchOS")
|
|
|
|
set(_SDK_NAME "watchsimulator")
|
2024-01-11 01:42:58 +08:00
|
|
|
set (CMAKE_XCODE_EFFECTIVE_PLATFORMS "-watchsimulator")
|
2023-10-07 12:36:33 +08:00
|
|
|
else()
|
|
|
|
message(FATAL_ERROR "PLAT=${PLAT} unsupported!")
|
|
|
|
endif()
|
|
|
|
|
|
|
|
find_program(XCODEBUILD_PROG xcodebuild)
|
|
|
|
if(NOT XCODEBUILD_PROG)
|
|
|
|
message(FATAL_ERROR "xcodebuild not found. Please install either the standalone commandline tools or Xcode.")
|
|
|
|
endif()
|
|
|
|
execute_process(COMMAND ${XCODEBUILD_PROG} -version -sdk ${_SDK_NAME} SDKVersion
|
|
|
|
OUTPUT_VARIABLE _SDK_VER
|
|
|
|
ERROR_QUIET
|
|
|
|
OUTPUT_STRIP_TRAILING_WHITESPACE)
|
|
|
|
set(CMAKE_OSX_SYSROOT "${_SDK_NAME}${_SDK_VER}" CACHE STRING "")
|
|
|
|
endif()
|
|
|
|
|
2023-06-29 19:46:52 +08:00
|
|
|
# Since xcode14, the bitcode was marked deprecated, so we disable by default
|
|
|
|
if(ENABLE_BITCODE)
|
|
|
|
set(CMAKE_XCODE_ATTRIBUTE_BITCODE_GENERATION_MODE "bitcode")
|
|
|
|
set(CMAKE_XCODE_ATTRIBUTE_ENABLE_BITCODE "YES")
|
|
|
|
else()
|
|
|
|
set(CMAKE_XCODE_ATTRIBUTE_ENABLE_BITCODE "NO")
|
|
|
|
endif()
|
|
|
|
|
|
|
|
# Set find path mode properly for cross-compiling
|
|
|
|
# refer to: https://discourse.cmake.org/t/find-package-stops-working-when-cmake-system-name-ios/4609/6
|
2024-01-06 01:09:31 +08:00
|
|
|
# BUT: CMAKE_FIND_ROOT_PATH is preferred for additional search directories when cross-compiling
|
|
|
|
# set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE BOTH CACHE STRING "")
|
|
|
|
# set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE BOTH CACHE STRING "")
|
|
|
|
# set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY BOTH CACHE STRING "")
|
|
|
|
|
2023-06-29 19:46:52 +08:00
|
|
|
# by default, we want find host program only when cross-compiling
|
|
|
|
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER CACHE STRING "")
|
|
|
|
|
|
|
|
# Sets CMAKE_SYSTEM_PROCESSOR for device and simulator properly
|
|
|
|
string(TOLOWER "${CMAKE_OSX_SYSROOT}" lowercase_CMAKE_OSX_SYSROOT)
|
|
|
|
if("${lowercase_CMAKE_OSX_SYSROOT}" MATCHES ".*simulator")
|
|
|
|
if("${CMAKE_OSX_ARCHITECTURES}" MATCHES "i386")
|
|
|
|
set(CMAKE_SYSTEM_PROCESSOR i386)
|
|
|
|
elseif("${CMAKE_OSX_ARCHITECTURES}" MATCHES "x86_64")
|
|
|
|
set(CMAKE_SYSTEM_PROCESSOR x86_64)
|
|
|
|
else() # Since xcode12, default arch for simulator is arm64
|
|
|
|
if(${XCODE_VERSION} LESS "12.0.0")
|
|
|
|
set(CMAKE_SYSTEM_PROCESSOR x86_64)
|
|
|
|
else()
|
|
|
|
set(CMAKE_SYSTEM_PROCESSOR arm64)
|
|
|
|
endif()
|
|
|
|
endif()
|
|
|
|
else()
|
|
|
|
set(CMAKE_SYSTEM_PROCESSOR arm64)
|
|
|
|
endif()
|
|
|
|
|
|
|
|
# This little function lets you set any XCode specific property, refer to: ios.toolchain.cmake
|
|
|
|
function(set_xcode_property TARGET XCODE_PROPERTY XCODE_VALUE)
|
|
|
|
if (ARGC LESS 4 OR ARGV3 STREQUAL "All")
|
|
|
|
set_property(TARGET ${TARGET} PROPERTY XCODE_ATTRIBUTE_${XCODE_PROPERTY} ${XCODE_VALUE})
|
|
|
|
else()
|
|
|
|
set(XCODE_RELVERSION ${ARGV3})
|
|
|
|
set_property(TARGET ${TARGET} PROPERTY XCODE_ATTRIBUTE_${XCODE_PROPERTY}[variant=${XCODE_RELVERSION}] "${XCODE_VALUE}")
|
|
|
|
endif()
|
|
|
|
endfunction()
|