common-ios-toolchain.cmake 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. # load settings in case of "try compile"
  2. set(TOOLCHAIN_CONFIG_FILE "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/toolchain.config.cmake")
  3. get_property(__IN_TRY_COMPILE GLOBAL PROPERTY IN_TRY_COMPILE)
  4. if(__IN_TRY_COMPILE)
  5. set(TOOLCHAIN_CONFIG_FILE "${CMAKE_CURRENT_SOURCE_DIR}/../toolchain.config.cmake")
  6. if(NOT EXISTS "${TOOLCHAIN_CONFIG_FILE}")
  7. # Hack for "try_compile" commands with other binary directory
  8. set(TOOLCHAIN_CONFIG_FILE "${CMAKE_PLATFORM_INFO_DIR}/../toolchain.config.cmake")
  9. if(NOT EXISTS "${TOOLCHAIN_CONFIG_FILE}")
  10. message(FATAL_ERROR "Current CMake version (${CMAKE_VERSION}) is not supported")
  11. endif()
  12. endif()
  13. include("${TOOLCHAIN_CONFIG_FILE}")
  14. macro(toolchain_save_config)
  15. # nothing
  16. endmacro()
  17. else()
  18. macro(toolchain_save_config)
  19. set(__config "#message(\"Load TOOLCHAIN config...\")\n")
  20. get_cmake_property(__variableNames VARIABLES)
  21. set(__vars_list ${ARGN})
  22. list(APPEND __vars_list
  23. ${TOOLCHAIN_CONFIG_VARS}
  24. CMAKE_SYSTEM_NAME
  25. CMAKE_SYSTEM_VERSION
  26. CMAKE_SYSTEM_PROCESSOR
  27. CMAKE_C_COMPILER
  28. CMAKE_CXX_COMPILER
  29. CMAKE_C_FLAGS
  30. CMAKE_CXX_FLAGS
  31. CMAKE_SHARED_LINKER_FLAGS
  32. CMAKE_MODULE_LINKER_FLAGS
  33. CMAKE_EXE_LINKER_FLAGS
  34. CMAKE_SKIP_RPATH
  35. CMAKE_FIND_ROOT_PATH
  36. )
  37. foreach(__var ${__variableNames})
  38. foreach(_v ${__vars_list})
  39. if("x${__var}" STREQUAL "x${_v}")
  40. if(${__var} MATCHES " ")
  41. set(__config "${__config}set(${__var} \"${${__var}}\")\n")
  42. else()
  43. set(__config "${__config}set(${__var} ${${__var}})\n")
  44. endif()
  45. endif()
  46. endforeach()
  47. endforeach()
  48. if(EXISTS "${TOOLCHAIN_CONFIG_FILE}")
  49. file(READ "${TOOLCHAIN_CONFIG_FILE}" __config_old)
  50. endif()
  51. if("${__config_old}" STREQUAL "${__config}")
  52. # nothing
  53. else()
  54. #message("Update TOOLCHAIN config: ${__config}")
  55. file(WRITE "${TOOLCHAIN_CONFIG_FILE}" "${__config}")
  56. endif()
  57. unset(__config)
  58. unset(__config_old)
  59. unset(__vars_list)
  60. unset(__variableNames)
  61. endmacro()
  62. endif() # IN_TRY_COMPILE
  63. if(NOT DEFINED IOS_ARCH)
  64. message(FATAL_ERROR "iOS toolchain requires ARCH option for proper configuration of compiler flags")
  65. endif()
  66. if(IOS_ARCH MATCHES "^arm64")
  67. set(AARCH64 1)
  68. elseif(IOS_ARCH MATCHES "^armv")
  69. set(ARM 1)
  70. elseif(IOS_ARCH MATCHES "^x86_64")
  71. set(X86_64 1)
  72. elseif(IOS_ARCH MATCHES "^i386")
  73. set(X86 1)
  74. else()
  75. message(FATAL_ERROR "iOS toolchain doesn't recognize ARCH='${IOS_ARCH}' value")
  76. endif()
  77. if(NOT DEFINED CMAKE_OSX_SYSROOT)
  78. if(IPHONEOS)
  79. set(CMAKE_OSX_SYSROOT "iphoneos")
  80. elseif(IPHONESIMULATOR)
  81. set(CMAKE_OSX_SYSROOT "iphonesimulator")
  82. elseif(MAC_CATALYST)
  83. # Use MacOS SDK for Catalyst builds
  84. set(CMAKE_OSX_SYSROOT "macosx")
  85. endif()
  86. endif()
  87. set(CMAKE_MACOSX_BUNDLE YES)
  88. set(CMAKE_XCODE_ATTRIBUTE_CODE_SIGNING_REQUIRED "NO")
  89. if(APPLE_FRAMEWORK AND NOT BUILD_SHARED_LIBS)
  90. set(CMAKE_OSX_ARCHITECTURES "${IOS_ARCH}" CACHE INTERNAL "Build architecture for iOS" FORCE)
  91. endif()
  92. if(NOT DEFINED IPHONEOS_DEPLOYMENT_TARGET AND NOT MAC_CATALYST)
  93. if(NOT DEFINED ENV{IPHONEOS_DEPLOYMENT_TARGET})
  94. message(FATAL_ERROR "IPHONEOS_DEPLOYMENT_TARGET is not specified")
  95. endif()
  96. set(IPHONEOS_DEPLOYMENT_TARGET "$ENV{IPHONEOS_DEPLOYMENT_TARGET}")
  97. endif()
  98. if(NOT __IN_TRY_COMPILE)
  99. set(_xcodebuild_wrapper "${CMAKE_BINARY_DIR}/xcodebuild_wrapper")
  100. if(NOT EXISTS "${_xcodebuild_wrapper}")
  101. set(_xcodebuild_wrapper_tmp "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/xcodebuild_wrapper")
  102. if(NOT DEFINED CMAKE_MAKE_PROGRAM) # empty since CMake 3.10
  103. find_program(XCODEBUILD_PATH "xcodebuild")
  104. if(NOT XCODEBUILD_PATH)
  105. message(FATAL_ERROR "Specify CMAKE_MAKE_PROGRAM variable ('xcodebuild' absolute path)")
  106. endif()
  107. set(CMAKE_MAKE_PROGRAM "${XCODEBUILD_PATH}")
  108. endif()
  109. if(CMAKE_MAKE_PROGRAM STREQUAL _xcodebuild_wrapper)
  110. message(FATAL_ERROR "Can't prepare xcodebuild_wrapper")
  111. endif()
  112. if(APPLE_FRAMEWORK AND BUILD_SHARED_LIBS)
  113. set(XCODEBUILD_EXTRA_ARGS "${XCODEBUILD_EXTRA_ARGS} IPHONEOS_DEPLOYMENT_TARGET=${IPHONEOS_DEPLOYMENT_TARGET} CODE_SIGN_IDENTITY='' CODE_SIGNING_REQUIRED=NO -sdk ${CMAKE_OSX_SYSROOT}")
  114. else()
  115. 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}")
  116. endif()
  117. configure_file("${CMAKE_CURRENT_LIST_DIR}/xcodebuild_wrapper.in" "${_xcodebuild_wrapper_tmp}" @ONLY)
  118. 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)
  119. endif()
  120. set(CMAKE_MAKE_PROGRAM "${_xcodebuild_wrapper}" CACHE INTERNAL "" FORCE)
  121. endif()
  122. # Standard settings
  123. set(CMAKE_SYSTEM_NAME iOS)
  124. # Apple Framework settings
  125. if(APPLE_FRAMEWORK AND BUILD_SHARED_LIBS)
  126. set(CMAKE_SYSTEM_VERSION "${IPHONEOS_DEPLOYMENT_TARGET}")
  127. set(CMAKE_C_SIZEOF_DATA_PTR 4)
  128. set(CMAKE_CXX_SIZEOF_DATA_PTR 4)
  129. else()
  130. set(CMAKE_SYSTEM_VERSION "${IPHONEOS_DEPLOYMENT_TARGET}")
  131. set(CMAKE_SYSTEM_PROCESSOR "${IOS_ARCH}")
  132. if(AARCH64 OR X86_64)
  133. set(CMAKE_C_SIZEOF_DATA_PTR 8)
  134. set(CMAKE_CXX_SIZEOF_DATA_PTR 8)
  135. else()
  136. set(CMAKE_C_SIZEOF_DATA_PTR 4)
  137. set(CMAKE_CXX_SIZEOF_DATA_PTR 4)
  138. endif()
  139. endif()
  140. # Include extra modules for the iOS platform files
  141. set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/platforms/ios/cmake/Modules")
  142. # Force the compilers to clang for iOS
  143. include(CMakeForceCompiler)
  144. #CMAKE_FORCE_C_COMPILER (clang GNU)
  145. #CMAKE_FORCE_CXX_COMPILER (clang++ GNU)
  146. set(CMAKE_C_HAS_ISYSROOT 1)
  147. set(CMAKE_CXX_HAS_ISYSROOT 1)
  148. set(CMAKE_C_COMPILER_ABI ELF)
  149. set(CMAKE_CXX_COMPILER_ABI ELF)
  150. # Skip the platform compiler checks for cross compiling
  151. set(CMAKE_CXX_COMPILER_WORKS TRUE)
  152. set(CMAKE_C_COMPILER_WORKS TRUE)
  153. if(NOT CMAKE_FIND_ROOT_PATH_MODE_LIBRARY)
  154. set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
  155. endif()
  156. if(NOT CMAKE_FIND_ROOT_PATH_MODE_INCLUDE)
  157. set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
  158. endif()
  159. if(NOT CMAKE_FIND_ROOT_PATH_MODE_PACKAGE)
  160. set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
  161. endif()
  162. if(NOT CMAKE_FIND_ROOT_PATH_MODE_PROGRAM)
  163. set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
  164. endif()
  165. toolchain_save_config(IOS_ARCH IPHONEOS_DEPLOYMENT_TARGET)