CMakeIOSInstallCombined.cmake 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file Copyright.txt or https://cmake.org/licensing for details.
  3. cmake_policy(PUSH)
  4. cmake_policy(SET CMP0057 NEW) # if IN_LIST
  5. # Function to print messages of this module
  6. function(_ios_install_combined_message)
  7. message("[iOS combined] " ${ARGN})
  8. endfunction()
  9. # Get build settings for the current target/config/SDK by running
  10. # `xcodebuild -sdk ... -showBuildSettings` and parsing it's output
  11. function(_ios_install_combined_get_build_setting sdk variable resultvar)
  12. if("${sdk}" STREQUAL "")
  13. message(FATAL_ERROR "`sdk` is empty")
  14. endif()
  15. if("${variable}" STREQUAL "")
  16. message(FATAL_ERROR "`variable` is empty")
  17. endif()
  18. if("${resultvar}" STREQUAL "")
  19. message(FATAL_ERROR "`resultvar` is empty")
  20. endif()
  21. set(
  22. cmd
  23. xcodebuild -showBuildSettings
  24. -sdk "${sdk}"
  25. -target "${CURRENT_TARGET}"
  26. -config "${CURRENT_CONFIG}"
  27. )
  28. execute_process(
  29. COMMAND ${cmd}
  30. WORKING_DIRECTORY "${CMAKE_BINARY_DIR}"
  31. RESULT_VARIABLE result
  32. OUTPUT_VARIABLE output
  33. )
  34. if(NOT result EQUAL 0)
  35. message(FATAL_ERROR "Command failed (${result}): ${cmd}")
  36. endif()
  37. if(NOT output MATCHES " ${variable} = ([^\n]*)")
  38. if("${variable}" STREQUAL "VALID_ARCHS")
  39. # VALID_ARCHS may be unset by user for given SDK
  40. # (e.g. for build without simulator).
  41. set("${resultvar}" "" PARENT_SCOPE)
  42. return()
  43. else()
  44. message(FATAL_ERROR "${variable} not found.")
  45. endif()
  46. endif()
  47. set("${resultvar}" "${CMAKE_MATCH_1}" PARENT_SCOPE)
  48. endfunction()
  49. # Get architectures of given SDK (iphonesimulator/iphoneos)
  50. function(_ios_install_combined_get_valid_archs sdk resultvar)
  51. cmake_policy(PUSH)
  52. cmake_policy(SET CMP0007 NEW)
  53. if("${resultvar}" STREQUAL "")
  54. message(FATAL_ERROR "`resultvar` is empty")
  55. endif()
  56. _ios_install_combined_get_build_setting("${sdk}" "VALID_ARCHS" valid_archs)
  57. separate_arguments(valid_archs)
  58. list(REMOVE_ITEM valid_archs "") # remove empty elements
  59. list(REMOVE_DUPLICATES valid_archs)
  60. string(REPLACE ";" " " printable "${valid_archs}")
  61. _ios_install_combined_message("Architectures (${sdk}): ${printable}")
  62. set("${resultvar}" "${valid_archs}" PARENT_SCOPE)
  63. cmake_policy(POP)
  64. endfunction()
  65. # Final target can contain more architectures that specified by SDK. This
  66. # function will run 'lipo -info' and parse output. Result will be returned
  67. # as a CMake list.
  68. function(_ios_install_combined_get_real_archs filename resultvar)
  69. set(cmd "${_lipo_path}" -info "${filename}")
  70. execute_process(
  71. COMMAND ${cmd}
  72. RESULT_VARIABLE result
  73. OUTPUT_VARIABLE output
  74. ERROR_VARIABLE output
  75. OUTPUT_STRIP_TRAILING_WHITESPACE
  76. ERROR_STRIP_TRAILING_WHITESPACE
  77. )
  78. if(NOT result EQUAL 0)
  79. message(
  80. FATAL_ERROR "Command failed (${result}): ${cmd}\n\nOutput:\n${output}"
  81. )
  82. endif()
  83. if(NOT output MATCHES "(Architectures in the fat file: [^\n]+ are|Non-fat file: [^\n]+ is architecture): ([^\n]*)")
  84. message(FATAL_ERROR "Could not detect architecture from: ${output}")
  85. endif()
  86. separate_arguments(CMAKE_MATCH_2)
  87. set(${resultvar} ${CMAKE_MATCH_2} PARENT_SCOPE)
  88. endfunction()
  89. # Run build command for the given SDK
  90. function(_ios_install_combined_build sdk)
  91. if("${sdk}" STREQUAL "")
  92. message(FATAL_ERROR "`sdk` is empty")
  93. endif()
  94. _ios_install_combined_message("Build `${CURRENT_TARGET}` for `${sdk}`")
  95. execute_process(
  96. COMMAND
  97. "${CMAKE_COMMAND}"
  98. --build
  99. .
  100. --target "${CURRENT_TARGET}"
  101. --config ${CURRENT_CONFIG}
  102. --
  103. -sdk "${sdk}"
  104. WORKING_DIRECTORY "${CMAKE_BINARY_DIR}"
  105. RESULT_VARIABLE result
  106. )
  107. if(NOT result EQUAL 0)
  108. message(FATAL_ERROR "Build failed")
  109. endif()
  110. endfunction()
  111. # Remove given architecture from file. This step needed only in rare cases
  112. # when target was built in "unusual" way. Emit warning message.
  113. function(_ios_install_combined_remove_arch lib arch)
  114. _ios_install_combined_message(
  115. "Warning! Unexpected architecture `${arch}` detected and will be removed "
  116. "from file `${lib}`")
  117. set(cmd "${_lipo_path}" -remove ${arch} -output ${lib} ${lib})
  118. execute_process(
  119. COMMAND ${cmd}
  120. RESULT_VARIABLE result
  121. OUTPUT_VARIABLE output
  122. ERROR_VARIABLE output
  123. OUTPUT_STRIP_TRAILING_WHITESPACE
  124. ERROR_STRIP_TRAILING_WHITESPACE
  125. )
  126. if(NOT result EQUAL 0)
  127. message(
  128. FATAL_ERROR "Command failed (${result}): ${cmd}\n\nOutput:\n${output}"
  129. )
  130. endif()
  131. endfunction()
  132. # Check that 'lib' contains only 'archs' architectures (remove others).
  133. function(_ios_install_combined_keep_archs lib archs)
  134. _ios_install_combined_get_real_archs("${lib}" real_archs)
  135. set(archs_to_remove ${real_archs})
  136. list(REMOVE_ITEM archs_to_remove ${archs})
  137. foreach(x ${archs_to_remove})
  138. _ios_install_combined_remove_arch("${lib}" "${x}")
  139. endforeach()
  140. endfunction()
  141. function(_ios_install_combined_detect_sdks this_sdk_var corr_sdk_var)
  142. set(this_sdk "$ENV{PLATFORM_NAME}")
  143. if("${this_sdk}" STREQUAL "")
  144. message(FATAL_ERROR "Environment variable PLATFORM_NAME is empty")
  145. endif()
  146. set(all_platforms "$ENV{SUPPORTED_PLATFORMS}")
  147. if("${all_platforms}" STREQUAL "")
  148. message(FATAL_ERROR "Environment variable SUPPORTED_PLATFORMS is empty")
  149. endif()
  150. separate_arguments(all_platforms)
  151. if(NOT this_sdk IN_LIST all_platforms)
  152. message(FATAL_ERROR "`${this_sdk}` not found in `${all_platforms}`")
  153. endif()
  154. list(REMOVE_ITEM all_platforms "" "${this_sdk}")
  155. list(LENGTH all_platforms all_platforms_length)
  156. if(NOT all_platforms_length EQUAL 1)
  157. message(FATAL_ERROR "Expected one element: ${all_platforms}")
  158. endif()
  159. set(${this_sdk_var} "${this_sdk}" PARENT_SCOPE)
  160. set(${corr_sdk_var} "${all_platforms}" PARENT_SCOPE)
  161. endfunction()
  162. # Create combined binary for the given target.
  163. #
  164. # Preconditions:
  165. # * Target already installed at ${destination}
  166. # for the ${PLATFORM_NAME} platform
  167. #
  168. # This function will:
  169. # * Run build for the lacking platform, i.e. opposite to the ${PLATFORM_NAME}
  170. # * Fuse both libraries by running lipo
  171. function(ios_install_combined target destination)
  172. if("${target}" STREQUAL "")
  173. message(FATAL_ERROR "`target` is empty")
  174. endif()
  175. if("${destination}" STREQUAL "")
  176. message(FATAL_ERROR "`destination` is empty")
  177. endif()
  178. if(NOT IS_ABSOLUTE "${destination}")
  179. message(FATAL_ERROR "`destination` is not absolute: ${destination}")
  180. endif()
  181. if(IS_DIRECTORY "${destination}" OR IS_SYMLINK "${destination}")
  182. message(FATAL_ERROR "`destination` is no regular file: ${destination}")
  183. endif()
  184. if("${CMAKE_BINARY_DIR}" STREQUAL "")
  185. message(FATAL_ERROR "`CMAKE_BINARY_DIR` is empty")
  186. endif()
  187. if(NOT IS_DIRECTORY "${CMAKE_BINARY_DIR}")
  188. message(FATAL_ERROR "Is not a directory: ${CMAKE_BINARY_DIR}")
  189. endif()
  190. if("${CMAKE_INSTALL_CONFIG_NAME}" STREQUAL "")
  191. message(FATAL_ERROR "CMAKE_INSTALL_CONFIG_NAME is empty")
  192. endif()
  193. set(cmd xcrun -f lipo)
  194. # Do not merge OUTPUT_VARIABLE and ERROR_VARIABLE since latter may contain
  195. # some diagnostic information even for the successful run.
  196. execute_process(
  197. COMMAND ${cmd}
  198. RESULT_VARIABLE result
  199. OUTPUT_VARIABLE output
  200. ERROR_VARIABLE error_output
  201. OUTPUT_STRIP_TRAILING_WHITESPACE
  202. ERROR_STRIP_TRAILING_WHITESPACE
  203. )
  204. if(NOT result EQUAL 0)
  205. message(
  206. FATAL_ERROR "Command failed (${result}): ${cmd}\n\nOutput:\n${output}\nOutput(error):\n${error_output}"
  207. )
  208. endif()
  209. set(_lipo_path ${output})
  210. list(LENGTH _lipo_path len)
  211. if(NOT len EQUAL 1)
  212. message(FATAL_ERROR "Unexpected xcrun output: ${_lipo_path}")
  213. endif()
  214. if(NOT EXISTS "${_lipo_path}")
  215. message(FATAL_ERROR "File not found: ${_lipo_path}")
  216. endif()
  217. set(CURRENT_CONFIG "${CMAKE_INSTALL_CONFIG_NAME}")
  218. set(CURRENT_TARGET "${target}")
  219. _ios_install_combined_message("Target: ${CURRENT_TARGET}")
  220. _ios_install_combined_message("Config: ${CURRENT_CONFIG}")
  221. _ios_install_combined_message("Destination: ${destination}")
  222. # Get SDKs
  223. _ios_install_combined_detect_sdks(this_sdk corr_sdk)
  224. # Get architectures of the target
  225. _ios_install_combined_get_valid_archs("${corr_sdk}" corr_valid_archs)
  226. _ios_install_combined_get_valid_archs("${this_sdk}" this_valid_archs)
  227. # Return if there are no valid architectures for the SDK.
  228. # (note that library already installed)
  229. if("${corr_valid_archs}" STREQUAL "")
  230. _ios_install_combined_message(
  231. "No architectures detected for `${corr_sdk}` (skip)"
  232. )
  233. return()
  234. endif()
  235. # Trigger build of corresponding target
  236. _ios_install_combined_build("${corr_sdk}")
  237. # Get location of the library in build directory
  238. _ios_install_combined_get_build_setting(
  239. "${corr_sdk}" "CONFIGURATION_BUILD_DIR" corr_build_dir)
  240. _ios_install_combined_get_build_setting(
  241. "${corr_sdk}" "EXECUTABLE_PATH" corr_executable_path)
  242. set(corr "${corr_build_dir}/${corr_executable_path}")
  243. _ios_install_combined_keep_archs("${corr}" "${corr_valid_archs}")
  244. _ios_install_combined_keep_archs("${destination}" "${this_valid_archs}")
  245. _ios_install_combined_message("Current: ${destination}")
  246. _ios_install_combined_message("Corresponding: ${corr}")
  247. set(cmd "${_lipo_path}" -create ${corr} ${destination} -output ${destination})
  248. execute_process(
  249. COMMAND ${cmd}
  250. WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
  251. RESULT_VARIABLE result
  252. )
  253. if(NOT result EQUAL 0)
  254. message(FATAL_ERROR "Command failed: ${cmd}")
  255. endif()
  256. _ios_install_combined_message("Install done: ${destination}")
  257. endfunction()
  258. cmake_policy(POP)