RunCMakeTest.cmake 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. include(RunCMake)
  2. function(create_library type platform system_name archs sysroot)
  3. set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/create-${type}-${platform}-build)
  4. run_cmake_with_options(create-${type}-${platform} -DCMAKE_SYSTEM_NAME=${system_name} -DCMAKE_OSX_ARCHITECTURES=${archs} -DCMAKE_OSX_SYSROOT=${sysroot} -DCMAKE_INSTALL_PREFIX=${RunCMake_TEST_BINARY_DIR}/install)
  5. set(RunCMake_TEST_NO_CLEAN 1)
  6. run_cmake_command(create-${type}-${platform}-build ${CMAKE_COMMAND} --build . --config Release)
  7. run_cmake_command(create-${type}-${platform}-install ${CMAKE_COMMAND} --install . --config Release)
  8. endfunction()
  9. function(create_libraries type)
  10. create_library(${type} macos Darwin "${macos_archs_2}" macosx)
  11. create_library(${type} ios iOS "arm64" iphoneos)
  12. create_library(${type} tvos tvOS "arm64" appletvos)
  13. create_library(${type} watchos watchOS "armv7k\\\\;arm64_32" watchos)
  14. #FIXME(#25266): Xcode 15.0 does not have visionOS. Improve this condition.
  15. #if(CMake_TEST_XCODE_VERSION VERSION_GREATER_EQUAL 15)
  16. # create_library(${type} visionos visionOS "arm64" xros)
  17. #endif()
  18. create_library(${type} ios-simulator iOS "${macos_archs_2}" iphonesimulator)
  19. create_library(${type} tvos-simulator tvOS "${macos_archs_2}" appletvsimulator)
  20. create_library(${type} watchos-simulator watchOS "${watch_sim_archs_2}" watchsimulator)
  21. #FIXME(#25266): Xcode 15.0 does not have visionOS. Improve this condition.
  22. #if(CMake_TEST_XCODE_VERSION VERSION_GREATER_EQUAL 15)
  23. # create_library(${type} visionos-simulator visionOS "${macos_archs_2}" xrsimulator)
  24. #endif()
  25. endfunction()
  26. function(create_xcframework name type platforms)
  27. set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/create-xcframework-${name}-build)
  28. if(CMake_TEST_XCODE_VERSION VERSION_GREATER_EQUAL 15)
  29. # 'xcodebuild -create-xcframework' fails on symlinked paths.
  30. file(REAL_PATH "${RunCMake_SOURCE_DIR}" src_dir)
  31. file(REAL_PATH "${RunCMake_BINARY_DIR}" bld_dir)
  32. else()
  33. set(src_dir "${RunCMake_SOURCE_DIR}")
  34. set(bld_dir "${RunCMake_BINARY_DIR}")
  35. endif()
  36. set(args)
  37. foreach(platform IN LISTS platforms)
  38. set(lib_dir "${bld_dir}/create-${type}-${platform}-build/install/lib")
  39. if(type STREQUAL "framework")
  40. list(APPEND args -framework ${lib_dir}/mylib.framework)
  41. else()
  42. list(APPEND args -library ${lib_dir}/libmylib.a -headers ${src_dir}/mylib/include)
  43. endif()
  44. endforeach()
  45. run_cmake_command(create-xcframework-${name} xcodebuild -create-xcframework ${args} -output ${RunCMake_TEST_BINARY_DIR}/mylib.xcframework)
  46. endfunction()
  47. function(create_executable name xcfname system_name archs sysroot)
  48. set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/create-executable-${name}-build)
  49. run_cmake_with_options(create-executable-${name} -DCMAKE_SYSTEM_NAME=${system_name} -DCMAKE_OSX_ARCHITECTURES=${archs} -DCMAKE_OSX_SYSROOT=${sysroot} -DMYLIB_LIBRARY=${RunCMake_BINARY_DIR}/create-xcframework-${xcfname}-build/mylib.xcframework)
  50. set(RunCMake_TEST_NO_CLEAN 1)
  51. run_cmake_command(create-executable-${name}-build ${CMAKE_COMMAND} --build . --config Release)
  52. endfunction()
  53. function(create_executables name type)
  54. create_executable(${name}-macos ${type} Darwin "${macos_archs_2}" macosx)
  55. create_executable(${name}-ios ${type} iOS "arm64" iphoneos)
  56. create_executable(${name}-tvos ${type} tvOS "arm64" appletvos)
  57. create_executable(${name}-watchos ${type} watchOS "armv7k\\\\;arm64_32" watchos)
  58. #FIXME(#25266): Xcode 15.0 does not have visionOS. Improve this condition.
  59. #if(CMake_TEST_XCODE_VERSION VERSION_GREATER_EQUAL 15)
  60. # create_executable(${name}-visionos ${type} visionOS "arm64" xros)
  61. #endif()
  62. create_executable(${name}-ios-simulator ${type} iOS "${macos_archs_2}" iphonesimulator)
  63. create_executable(${name}-tvos-simulator ${type} tvOS "${macos_archs_2}" appletvsimulator)
  64. create_executable(${name}-watchos-simulator ${type} watchOS "${watch_sim_archs_2}" watchsimulator)
  65. #FIXME(#25266): Xcode 15.0 does not have visionOS. Improve this condition.
  66. #if(CMake_TEST_XCODE_VERSION VERSION_GREATER_EQUAL 15)
  67. # create_executable(${name}-visionos-simulator ${type} visionOS "${macos_archs_2}" xrsimulator)
  68. #endif()
  69. endfunction()
  70. set(xcframework_platforms macos ios tvos watchos ios-simulator tvos-simulator watchos-simulator)
  71. #FIXME(#25266): Xcode 15.0 does not have visionOS. Improve this condition.
  72. #if(CMake_TEST_XCODE_VERSION VERSION_GREATER_EQUAL 15)
  73. # list(APPEND xcframework_platforms visionos visionos-simulator)
  74. #endif()
  75. if(CMake_TEST_XCODE_VERSION VERSION_GREATER_EQUAL 12)
  76. set(macos_archs_1 "x86_64\\;arm64")
  77. set(macos_archs_2 "x86_64\\\\;arm64")
  78. set(watch_sim_archs_2 "x86_64")
  79. else()
  80. set(macos_archs_1 "x86_64")
  81. set(macos_archs_2 "x86_64")
  82. set(watch_sim_archs_2 "i386")
  83. endif()
  84. create_libraries(library)
  85. create_libraries(framework)
  86. create_xcframework(library library "${xcframework_platforms}")
  87. create_xcframework(framework framework "${xcframework_platforms}")
  88. create_xcframework(incomplete framework "tvos;watchos")
  89. create_executables(library library)
  90. create_executables(framework framework)
  91. run_cmake_with_options(create-executable-incomplete -DCMAKE_SYSTEM_NAME=Darwin "-DCMAKE_OSX_ARCHITECTURES=${macos_archs_1}" -DMYLIB_LIBRARY=${RunCMake_BINARY_DIR}/create-xcframework-incomplete-build/mylib.xcframework)
  92. create_executables(target-library library)
  93. create_executables(target-framework framework)
  94. run_cmake_with_options(create-executable-target-incomplete -DCMAKE_SYSTEM_NAME=Darwin "-DCMAKE_OSX_ARCHITECTURES=${macos_archs_1}" -DMYLIB_LIBRARY=${RunCMake_BINARY_DIR}/create-xcframework-incomplete-build/mylib.xcframework)
  95. if(RunCMake_GENERATOR STREQUAL "Xcode" AND CMake_TEST_XCODE_VERSION VERSION_GREATER_EQUAL 12)
  96. create_executables(library-link-phase library)
  97. create_executables(framework-link-phase framework)
  98. run_cmake_with_options(create-executable-incomplete-link-phase -DCMAKE_SYSTEM_NAME=Darwin "-DCMAKE_OSX_ARCHITECTURES=${macos_archs_1}" -DMYLIB_LIBRARY=${RunCMake_BINARY_DIR}/create-xcframework-incomplete-build/mylib.xcframework)
  99. create_executables(target-library-link-phase library)
  100. create_executables(target-framework-link-phase framework)
  101. run_cmake_with_options(create-executable-target-incomplete-link-phase -DCMAKE_SYSTEM_NAME=Darwin "-DCMAKE_OSX_ARCHITECTURES=${macos_archs_1}" -DMYLIB_LIBRARY=${RunCMake_BINARY_DIR}/create-xcframework-incomplete-build/mylib.xcframework)
  102. endif()
  103. # Ensure that .xcframework is found before .framework
  104. set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/create-xcframework-framework-build)
  105. set(RunCMake_TEST_NO_CLEAN 1)
  106. run_cmake_command(copy-framework ${CMAKE_COMMAND} -E copy_directory ${RunCMake_BINARY_DIR}/create-framework-macos-build/install/lib/mylib.framework ${RunCMake_TEST_BINARY_DIR}/mylib.framework)
  107. unset(RunCMake_TEST_NO_CLEAN)
  108. unset(RunCMake_TEST_BINARY_DIR)
  109. run_cmake(find-library)
  110. run_cmake_command(find-library-script ${CMAKE_COMMAND} -P ${RunCMake_SOURCE_DIR}/find-library.cmake)
  111. file(REMOVE_RECURSE ${RunCMake_BINARY_DIR}/export-install)
  112. set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/export-macos-build)
  113. run_cmake_with_options(export-macos -DCMAKE_SYSTEM_NAME=Darwin -DCMAKE_INSTALL_PREFIX=${RunCMake_BINARY_DIR}/export-install)
  114. set(RunCMake_TEST_NO_CLEAN 1)
  115. set(_config_arg)
  116. if(RunCMake_GENERATOR_IS_MULTI_CONFIG)
  117. set(_config_arg --config Release)
  118. endif()
  119. run_cmake_command(export-macos-build ${CMAKE_COMMAND} --build . ${_config_arg})
  120. run_cmake_command(export-macos-install ${CMAKE_COMMAND} --install . ${_config_arg})
  121. unset(RunCMake_TEST_NO_CLEAN)
  122. unset(RunCMake_TEST_BINARY_DIR)
  123. set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/import-macos-install-specific-no-xcframework-build)
  124. run_cmake_with_options(import-macos-install-specific-no-xcframework -DCMAKE_SYSTEM_NAME=Darwin -Dmylib_DIR=${RunCMake_BINARY_DIR}/export-install/lib/macos/cmake/mylib)
  125. set(RunCMake_TEST_NO_CLEAN 1)
  126. set(_config_arg)
  127. set(_config_dir)
  128. if(RunCMake_GENERATOR_IS_MULTI_CONFIG)
  129. set(_config_arg --config Release)
  130. set(_config_dir /Release)
  131. endif()
  132. run_cmake_command(import-macos-install-specific-no-xcframework-build ${CMAKE_COMMAND} --build . ${_config_arg})
  133. unset(RunCMake_TEST_NO_CLEAN)
  134. unset(RunCMake_TEST_BINARY_DIR)
  135. set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/export-macos-build)
  136. set(RunCMake_TEST_NO_CLEAN 1)
  137. if(CMake_TEST_XCODE_VERSION VERSION_GREATER_EQUAL 15)
  138. # 'xcodebuild -create-xcframework' fails on symlinked paths.
  139. file(REAL_PATH "${RunCMake_SOURCE_DIR}" src_dir)
  140. file(REAL_PATH "${RunCMake_BINARY_DIR}" bld_dir)
  141. else()
  142. set(src_dir "${RunCMake_SOURCE_DIR}")
  143. set(bld_dir "${RunCMake_BINARY_DIR}")
  144. endif()
  145. run_cmake_command(export-install-xcframework xcodebuild -create-xcframework
  146. -output ${bld_dir}/export-install/lib/mylib.xcframework
  147. -library ${bld_dir}/export-install/lib/macos/libmylib.a
  148. -headers ${src_dir}/mylib/include
  149. )
  150. run_cmake_command(export-build-xcframework xcodebuild -create-xcframework
  151. -output ${bld_dir}/export-macos-build/lib/mylib.xcframework
  152. -library ${bld_dir}/export-macos-build/lib/macos${_config_dir}/libmylib.a
  153. -headers ${src_dir}/mylib/include
  154. )
  155. run_cmake_command(export-install-xcframework-genex xcodebuild -create-xcframework
  156. -output ${bld_dir}/export-install/lib2/mylib-genex.xcframework
  157. -library ${bld_dir}/export-install/lib/macos/libmylib-genex.a
  158. -headers ${src_dir}/mylib/include
  159. )
  160. run_cmake_command(export-build-xcframework-genex xcodebuild -create-xcframework
  161. -output ${bld_dir}/export-macos-build/lib/mylib-genex.xcframework
  162. -library ${bld_dir}/export-macos-build/lib/macos${_config_dir}/libmylib-genex.a
  163. -headers ${src_dir}/mylib/include
  164. )
  165. unset(RunCMake_TEST_NO_CLEAN)
  166. unset(RunCMake_TEST_BINARY_DIR)
  167. set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/import-macos-install-specific-build)
  168. run_cmake_with_options(import-macos-install-specific -DCMAKE_SYSTEM_NAME=Darwin -Dmylib_DIR=${RunCMake_BINARY_DIR}/export-install/lib/macos/cmake/mylib)
  169. set(RunCMake_TEST_NO_CLEAN 1)
  170. set(_config_arg)
  171. if(RunCMake_GENERATOR_IS_MULTI_CONFIG)
  172. set(_config_arg --config Release)
  173. endif()
  174. run_cmake_command(import-macos-install-specific-build ${CMAKE_COMMAND} --build . ${_config_arg})
  175. unset(RunCMake_TEST_NO_CLEAN)
  176. unset(RunCMake_TEST_BINARY_DIR)
  177. set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/import-macos-build-specific-build)
  178. run_cmake_with_options(import-macos-build-specific -DCMAKE_SYSTEM_NAME=Darwin -Dmylib_DIR=${RunCMake_BINARY_DIR}/export-macos-build/lib/macos/cmake/mylib)
  179. set(RunCMake_TEST_NO_CLEAN 1)
  180. set(_config_arg)
  181. if(RunCMake_GENERATOR_IS_MULTI_CONFIG)
  182. set(_config_arg --config Release)
  183. endif()
  184. run_cmake_command(import-macos-build-specific-build ${CMAKE_COMMAND} --build . ${_config_arg})
  185. unset(RunCMake_TEST_NO_CLEAN)
  186. unset(RunCMake_TEST_BINARY_DIR)
  187. set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/import-macos-install-specific-genex-build)
  188. run_cmake_with_options(import-macos-install-specific-genex -DCMAKE_SYSTEM_NAME=Darwin -Dmylib_DIR=${RunCMake_BINARY_DIR}/export-install/lib/macos/cmake/mylib)
  189. set(RunCMake_TEST_NO_CLEAN 1)
  190. set(_config_arg)
  191. if(RunCMake_GENERATOR_IS_MULTI_CONFIG)
  192. set(_config_arg --config Release)
  193. endif()
  194. run_cmake_command(import-macos-install-specific-genex-build ${CMAKE_COMMAND} --build . ${_config_arg})
  195. unset(RunCMake_TEST_NO_CLEAN)
  196. unset(RunCMake_TEST_BINARY_DIR)
  197. set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/import-macos-build-specific-genex-build)
  198. run_cmake_with_options(import-macos-build-specific-genex -DCMAKE_SYSTEM_NAME=Darwin -Dmylib_DIR=${RunCMake_BINARY_DIR}/export-macos-build/lib/macos/cmake/mylib)
  199. set(RunCMake_TEST_NO_CLEAN 1)
  200. set(_config_arg)
  201. if(RunCMake_GENERATOR_IS_MULTI_CONFIG)
  202. set(_config_arg --config Release)
  203. endif()
  204. run_cmake_command(import-macos-build-specific-genex-build ${CMAKE_COMMAND} --build . ${_config_arg})
  205. unset(RunCMake_TEST_NO_CLEAN)
  206. unset(RunCMake_TEST_BINARY_DIR)