CMakeLists.txt 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. cmake_minimum_required(VERSION 3.3)
  2. if(POLICY CMP0126)
  3. cmake_policy(SET CMP0126 NEW)
  4. endif()
  5. if(POLICY CMP0157)
  6. cmake_policy(SET CMP0157 NEW)
  7. endif()
  8. # NOTE: Force the Release mode configuration as there are some issues with the
  9. # debug information handling on macOS on certain Xcode builds.
  10. if(NOT CMAKE_CONFIGURATION_TYPES)
  11. set(CMAKE_BUILD_TYPE Release CACHE STRING "Choose the type of build" FORCE)
  12. endif()
  13. # NOTE: enable shared libraries by default. Older Xcode releases do not play
  14. # well with static libraries, and Windows does not currently support static
  15. # libraries in Swift.
  16. set(BUILD_SHARED_LIBS YES)
  17. project(SwiftOnly Swift)
  18. if(NOT XCODE_VERSION VERSION_LESS 10.2)
  19. set(CMAKE_Swift_LANGUAGE_VERSION 5.0)
  20. elseif(NOT XCODE_VERSION VERSION_LESS 8.0)
  21. set(CMAKE_Swift_LANGUAGE_VERSION 3.0)
  22. endif()
  23. add_subdirectory(SubA)
  24. add_subdirectory(SubB)
  25. set(CMAKE_Swift_MODULE_DIRECTORY ${CMAKE_BINARY_DIR}/swift)
  26. add_executable(SwiftOnly main.swift)
  27. target_compile_definitions(SwiftOnly PRIVATE SWIFTONLY)
  28. add_library(L L.swift)
  29. add_library(M M.swift)
  30. target_link_libraries(M PUBLIC
  31. L)
  32. add_library(N N.swift)
  33. target_link_libraries(N PUBLIC
  34. M)
  35. # Dummy to make sure generation works with such targets.
  36. add_library(SwiftIface INTERFACE)
  37. target_link_libraries(SwiftOnly PRIVATE SwiftIface)
  38. # @_alwaysEmitIntoClient ensures that the function body is inserted into the
  39. # swiftmodule instead of as a symbol in the binary itself. I'm doing this to
  40. # avoid having to link the executable. There are some flags required in order to
  41. # link an executable into a library that I didn't see CMake emitting for Swift
  42. # on macOS. AEIC is the easiest workaround that still tests this functionality.
  43. # Unfortunately, AEIC was only added recently (~Swift 5.2), so we need to check
  44. # that it is available before using it.
  45. if(CMAKE_Swift_COMPILER_VERSION VERSION_GREATER_EQUAL 5.2)
  46. add_subdirectory("SwiftPlugin")
  47. endif()
  48. function(test_cmp0157_default mode)
  49. cmake_policy(GET CMP0157 cmp0157_wmo)
  50. if(cmp0157_wmo STREQUAL "NEW")
  51. set(CMAKE_Swift_COMPILATION_MODE "${mode}")
  52. add_executable(hi_${mode} main.swift)
  53. get_target_property(${mode}_swift_comp_mode hi_${mode} "Swift_COMPILATION_MODE")
  54. if(NOT ${mode}_swift_comp_mode STREQUAL ${mode})
  55. message(SEND_ERROR "expected ${mode} -- found ${${mode}_swift_comp_mode}")
  56. endif()
  57. endif()
  58. endfunction()
  59. test_cmp0157_default("wholemodule")
  60. test_cmp0157_default("incremental")
  61. test_cmp0157_default("singlefile")