opencv/platforms/ios/cmake/Toolchains/common-ios-toolchain.cmake
Jonathan Cole 85b0fb2a9c
Merge pull request #18826 from Rightpoint:feature/colejd/build-catalyst-xcframework
Support XCFramework builds, Catalyst

* Early work on xcframework support

* Improve legibility

* Somehow this works

* Specify ABIs in a place where they won't get erased

If you pass in the C/CXX flags from the Python script, they won't be respected. By doing it in the actual toolchain, the options are respected and Catalyst successfully links.

* Clean up and push updates

* Actually use Catalyst ABI

Needed to specify EXE linker flags to get compiler tests to link to the Catalyst ABIs.

* Clean up

* Revert changes to common toolchain that don't matter

* Try some things

* Support Catalyst build in OSX scripts

* Remove unnecessary iOS reference to AssetsLibrary framework

* Getting closer

* Try some things, port to Python 3

* Some additional fixes

* Point Cmake Plist gen to osx directory for Catalyst targets

* Remove dynamic lib references for Catalyst, copy iOS instead of macos

* Add flag for building only specified archs, remove iOS catalyst refs

* Add build-xcframework.sh

* Update build-xcframework.sh

* Add presumptive Apple Silicon support

* Add arm64 iphonesimulator target

* Fix xcframework build

* Working on arm64 iOS simulator

* Support 2.7 (replace run with check_output)

* Correctly check output of uname_m against arch

* Clean up

* Use lipo for intermediate frameworks, add python script

Remove unneeded __init__.py

* Simplify python xcframework build script

* Add --only-64-bit flag

* Add --framework-name flag

* Document

* Commit to f-strings, improve console output

* Add i386 to iphonesimulator platform in xcframework generator

* Enable objc for non-Catalyst frameworks

* Fix xcframework builder for paths with spaces

* Use arch when specifying Catalyst build platform in build command

* Fix incorrect settings for framework_name argparse configuration

* Prefer underscores instead of hyphens in new flags

* Move Catalyst flags to where they'll actually get used

* Use --without=objc on Catalyst target for now

* Remove get_or_create_folder and simplify logic

* Remove unused import

* Tighten up help text

* Document

* Move common functions into cv_build_utils

* Improve documentation

* Remove old build script

* Add readme

* Check for required CMake and Xcode versions

* Clean up TODOs and re-enable `copy_samples()`

Remove TODO

Fixup

* Add missing print_function import

* Clarify CMake dependency documentation

* Revert python2 change in gen_objc

* Remove unnecessary builtins imports

* Remove trailing whitespace

* Avoid building Catalyst unless specified

This makes Catalyst support a non-breaking change, though defaults should be specified when a breaking change is possible.

* Prevent lipoing for the same archs on different platforms before build

* Rename build-xcframework.py to build_xcframework.py

* Check for duplicate archs more carefully

* Prevent sample copying error when directory already exists

This can happen when building multiple architectures for the same platform.

* Simplify code for checking for default archs

* Improve build_xcframework.py header text

* Correctly resolve Python script paths

* Parse only known args in ios/osx build_framework.py

* Pass through uncaptured args in build_xcframework to osx/ios build

* Fix typo

* Fix typo

* Fix unparameterized build path for intermediate frameworks

* Fix dyanmic info.plist path for catalyst

* Fix utf-8 Python 3 issue

* Add dynamic flag to osx script

* Rename platform to platforms, remove armv7s and i386

* Fix creation of dynamic framework on maccatalyst and macos

* Update platforms/apple/readme.md

* Add `macos_archs` flag and deprecate `archs` flag

* Allow specification of archs when generating xcframework from terminal

* Change xcframework platform argument names to match archs flag names

* Remove platforms as a concept and shadow archs flags from ios/osx .py

* Improve documentation

* Fix building of objc module on Catalyst, excluding Swift

* Clean up build folder logic a bit

* Fix framework_name flag

* Drop passthrough_args, use unknown_args instead

* minor: coding style changes

Co-authored-by: Chris Ballinger <cballinger@rightpoint.com>
2020-11-24 21:54:54 +00:00

173 lines
6.1 KiB
CMake

# load settings in case of "try compile"
set(TOOLCHAIN_CONFIG_FILE "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/toolchain.config.cmake")
get_property(__IN_TRY_COMPILE GLOBAL PROPERTY IN_TRY_COMPILE)
if(__IN_TRY_COMPILE)
set(TOOLCHAIN_CONFIG_FILE "${CMAKE_CURRENT_SOURCE_DIR}/../toolchain.config.cmake")
if(NOT EXISTS "${TOOLCHAIN_CONFIG_FILE}")
# Hack for "try_compile" commands with other binary directory
set(TOOLCHAIN_CONFIG_FILE "${CMAKE_PLATFORM_INFO_DIR}/../toolchain.config.cmake")
if(NOT EXISTS "${TOOLCHAIN_CONFIG_FILE}")
message(FATAL_ERROR "Current CMake version (${CMAKE_VERSION}) is not supported")
endif()
endif()
include("${TOOLCHAIN_CONFIG_FILE}")
macro(toolchain_save_config)
# nothing
endmacro()
else()
macro(toolchain_save_config)
set(__config "#message(\"Load TOOLCHAIN config...\")\n")
get_cmake_property(__variableNames VARIABLES)
set(__vars_list ${ARGN})
list(APPEND __vars_list
${TOOLCHAIN_CONFIG_VARS}
CMAKE_SYSTEM_NAME
CMAKE_SYSTEM_VERSION
CMAKE_SYSTEM_PROCESSOR
CMAKE_C_COMPILER
CMAKE_CXX_COMPILER
CMAKE_C_FLAGS
CMAKE_CXX_FLAGS
CMAKE_SHARED_LINKER_FLAGS
CMAKE_MODULE_LINKER_FLAGS
CMAKE_EXE_LINKER_FLAGS
CMAKE_SKIP_RPATH
CMAKE_FIND_ROOT_PATH
)
foreach(__var ${__variableNames})
foreach(_v ${__vars_list})
if("x${__var}" STREQUAL "x${_v}")
if(${__var} MATCHES " ")
set(__config "${__config}set(${__var} \"${${__var}}\")\n")
else()
set(__config "${__config}set(${__var} ${${__var}})\n")
endif()
endif()
endforeach()
endforeach()
if(EXISTS "${TOOLCHAIN_CONFIG_FILE}")
file(READ "${TOOLCHAIN_CONFIG_FILE}" __config_old)
endif()
if("${__config_old}" STREQUAL "${__config}")
# nothing
else()
#message("Update TOOLCHAIN config: ${__config}")
file(WRITE "${TOOLCHAIN_CONFIG_FILE}" "${__config}")
endif()
unset(__config)
unset(__config_old)
unset(__vars_list)
unset(__variableNames)
endmacro()
endif() # IN_TRY_COMPILE
if(NOT DEFINED IOS_ARCH)
message(FATAL_ERROR "iOS toolchain requires ARCH option for proper configuration of compiler flags")
endif()
if(IOS_ARCH MATCHES "^arm64")
set(AARCH64 1)
elseif(IOS_ARCH MATCHES "^armv")
set(ARM 1)
elseif(IOS_ARCH MATCHES "^x86_64")
set(X86_64 1)
elseif(IOS_ARCH MATCHES "^i386")
set(X86 1)
else()
message(FATAL_ERROR "iOS toolchain doesn't recognize ARCH='${IOS_ARCH}' value")
endif()
if(NOT DEFINED CMAKE_OSX_SYSROOT)
if(IPHONEOS)
set(CMAKE_OSX_SYSROOT "iphoneos")
elseif(IPHONESIMULATOR)
set(CMAKE_OSX_SYSROOT "iphonesimulator")
elseif(MAC_CATALYST)
# Use MacOS SDK for Catalyst builds
set(CMAKE_OSX_SYSROOT "macosx")
endif()
endif()
set(CMAKE_MACOSX_BUNDLE YES)
set(CMAKE_XCODE_ATTRIBUTE_CODE_SIGNING_REQUIRED "NO")
if(APPLE_FRAMEWORK AND NOT BUILD_SHARED_LIBS)
set(CMAKE_OSX_ARCHITECTURES "${IOS_ARCH}" CACHE INTERNAL "Build architecture for iOS" FORCE)
endif()
if(NOT DEFINED IPHONEOS_DEPLOYMENT_TARGET AND NOT MAC_CATALYST)
if(NOT DEFINED ENV{IPHONEOS_DEPLOYMENT_TARGET})
message(FATAL_ERROR "IPHONEOS_DEPLOYMENT_TARGET is not specified")
endif()
set(IPHONEOS_DEPLOYMENT_TARGET "$ENV{IPHONEOS_DEPLOYMENT_TARGET}")
endif()
if(NOT __IN_TRY_COMPILE)
set(_xcodebuild_wrapper "${CMAKE_BINARY_DIR}/xcodebuild_wrapper")
if(NOT EXISTS "${_xcodebuild_wrapper}")
set(_xcodebuild_wrapper_tmp "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/xcodebuild_wrapper")
if(NOT DEFINED CMAKE_MAKE_PROGRAM) # empty since CMake 3.10
find_program(XCODEBUILD_PATH "xcodebuild")
if(NOT XCODEBUILD_PATH)
message(FATAL_ERROR "Specify CMAKE_MAKE_PROGRAM variable ('xcodebuild' absolute path)")
endif()
set(CMAKE_MAKE_PROGRAM "${XCODEBUILD_PATH}")
endif()
if(CMAKE_MAKE_PROGRAM STREQUAL _xcodebuild_wrapper)
message(FATAL_ERROR "Can't prepare xcodebuild_wrapper")
endif()
if(APPLE_FRAMEWORK AND BUILD_SHARED_LIBS)
set(XCODEBUILD_EXTRA_ARGS "${XCODEBUILD_EXTRA_ARGS} IPHONEOS_DEPLOYMENT_TARGET=${IPHONEOS_DEPLOYMENT_TARGET} CODE_SIGN_IDENTITY='' CODE_SIGNING_REQUIRED=NO -sdk ${CMAKE_OSX_SYSROOT}")
else()
set(XCODEBUILD_EXTRA_ARGS "${XCODEBUILD_EXTRA_ARGS} IPHONEOS_DEPLOYMENT_TARGET=${IPHONEOS_DEPLOYMENT_TARGET} CODE_SIGN_IDENTITY='' CODE_SIGNING_REQUIRED=NO ARCHS=${IOS_ARCH} -sdk ${CMAKE_OSX_SYSROOT}")
endif()
configure_file("${CMAKE_CURRENT_LIST_DIR}/xcodebuild_wrapper.in" "${_xcodebuild_wrapper_tmp}" @ONLY)
file(COPY "${_xcodebuild_wrapper_tmp}" DESTINATION ${CMAKE_BINARY_DIR} FILE_PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE)
endif()
set(CMAKE_MAKE_PROGRAM "${_xcodebuild_wrapper}" CACHE INTERNAL "" FORCE)
endif()
# Standard settings
set(CMAKE_SYSTEM_NAME iOS)
# Apple Framework settings
if(APPLE_FRAMEWORK AND BUILD_SHARED_LIBS)
set(CMAKE_SYSTEM_VERSION "${IPHONEOS_DEPLOYMENT_TARGET}")
set(CMAKE_C_SIZEOF_DATA_PTR 4)
set(CMAKE_CXX_SIZEOF_DATA_PTR 4)
else()
set(CMAKE_SYSTEM_VERSION "${IPHONEOS_DEPLOYMENT_TARGET}")
set(CMAKE_SYSTEM_PROCESSOR "${IOS_ARCH}")
if(AARCH64 OR X86_64)
set(CMAKE_C_SIZEOF_DATA_PTR 8)
set(CMAKE_CXX_SIZEOF_DATA_PTR 8)
else()
set(CMAKE_C_SIZEOF_DATA_PTR 4)
set(CMAKE_CXX_SIZEOF_DATA_PTR 4)
endif()
endif()
# Include extra modules for the iOS platform files
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/platforms/ios/cmake/Modules")
# Force the compilers to clang for iOS
include(CMakeForceCompiler)
#CMAKE_FORCE_C_COMPILER (clang GNU)
#CMAKE_FORCE_CXX_COMPILER (clang++ GNU)
set(CMAKE_C_HAS_ISYSROOT 1)
set(CMAKE_CXX_HAS_ISYSROOT 1)
set(CMAKE_C_COMPILER_ABI ELF)
set(CMAKE_CXX_COMPILER_ABI ELF)
# Skip the platform compiler checks for cross compiling
set(CMAKE_CXX_COMPILER_WORKS TRUE)
set(CMAKE_C_COMPILER_WORKS TRUE)
# Search for programs in the build host directories
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM ONLY)
# for libraries and headers in the target directories
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
toolchain_save_config(IOS_ARCH IPHONEOS_DEPLOYMENT_TARGET)