CMakeSwiftInformation.cmake 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file Copyright.txt or https://cmake.org/licensing for details.
  3. if(UNIX)
  4. set(CMAKE_Swift_OUTPUT_EXTENSION .o)
  5. else()
  6. set(CMAKE_Swift_OUTPUT_EXTENSION .obj)
  7. endif()
  8. # Load compiler-specific information.
  9. if(CMAKE_Swift_COMPILER_ID)
  10. include(Compiler/${CMAKE_Swift_COMPILER_ID}-Swift OPTIONAL)
  11. if(CMAKE_SYSTEM_PROCESSOR)
  12. include(Platform/${CMAKE_EFFECTIVE_SYSTEM_NAME}-${CMAKE_Swift_COMPILER_ID}-Swift-${CMAKE_SYSTEM_PROCESSOR} OPTIONAL)
  13. endif()
  14. include(Platform/${CMAKE_EFFECTIVE_SYSTEM_NAME}-${CMAKE_Swift_COMPILER_ID}-Swift OPTIONAL)
  15. endif()
  16. set(CMAKE_INCLUDE_FLAG_Swift "-I ")
  17. # FIXME: Move compiler- and platform-specific flags to the above-included modules.
  18. if(CMAKE_SYSTEM_NAME STREQUAL "Darwin" OR CMAKE_SYSTEM_NAME STREQUAL "iOS"
  19. OR CMAKE_SYSTEM_NAME STREQUAL "tvOS" OR CMAKE_SYSTEM_NAME STREQUAL "visionOS"
  20. OR CMAKE_SYSTEM_NAME STREQUAL "watchOS")
  21. set(CMAKE_SHARED_LIBRARY_SONAME_Swift_FLAG "-Xlinker -install_name -Xlinker ")
  22. elseif(NOT CMAKE_SYSTEM_NAME STREQUAL Windows)
  23. set(CMAKE_SHARED_LIBRARY_SONAME_Swift_FLAG "-Xlinker -soname -Xlinker ")
  24. endif()
  25. if(NOT CMAKE_SYSTEM_NAME STREQUAL Windows)
  26. set(CMAKE_EXECUTABLE_RUNTIME_Swift_FLAG "-Xlinker -rpath -Xlinker ")
  27. set(CMAKE_SHARED_LIBRARY_RUNTIME_Swift_FLAG "-Xlinker -rpath -Xlinker ")
  28. if(CMAKE_SYSTEM_NAME STREQUAL "Darwin" OR CMAKE_SYSTEM_NAME STREQUAL "iOS"
  29. OR CMAKE_SYSTEM_NAME STREQUAL "tvOS" OR CMAKE_SYSTEM_NAME STREQUAL "visionOS"
  30. OR CMAKE_SYSTEM_NAME STREQUAL "watchOS")
  31. set(CMAKE_EXECUTABLE_RUNTIME_Swift_FLAG_SEP "")
  32. set(CMAKE_SHARED_LIBRARY_RUNTIME_Swift_FLAG_SEP "")
  33. else()
  34. set(CMAKE_EXECUTABLE_RUNTIME_Swift_FLAG_SEP ":")
  35. set(CMAKE_SHARED_LIBRARY_RUNTIME_Swift_FLAG_SEP ":")
  36. endif()
  37. endif()
  38. set(CMAKE_Swift_COMPILE_OPTIONS_TARGET "-target ")
  39. set(CMAKE_Swift_COMPILE_OPTIONS_EXTERNAL_TOOLCHAIN "-tools-directory ")
  40. # NOTE(compnerd) the `-sdk` support is not yet ready in the compiler; when that
  41. # is fully working, we should be able to enable this.
  42. # set(CMAKE_Swift_COMPILE_OPTIONS_SYSROOT "-sdk ")
  43. # NOTE(compnerd) do not setup `-frontend` as we use the compiler as the driver
  44. # during the link phase and use that to drive the compilation
  45. set(CMAKE_Swift_COMPILER_ARG1 "")
  46. set(CMAKE_Swift_DEFINE_FLAG -D)
  47. set(CMAKE_Swift_FRAMEWORK_SEARCH_FLAG "-F ")
  48. set(CMAKE_Swift_LIBRARY_PATH_FLAG "-L ")
  49. set(CMAKE_Swift_LIBRARY_PATH_TERMINATOR "")
  50. set(CMAKE_Swift_LINK_LIBRARY_FLAG "-l")
  51. set(CMAKE_Swift_LINKER_WRAPPER_FLAG "-Xlinker" " ")
  52. set(CMAKE_Swift_RESPONSE_FILE_LINK_FLAG @)
  53. set(CMAKE_Swift_RESPONSE_FILE_FLAG @)
  54. set(CMAKE_Swift_COMPILE_OPTIONS_COLOR_DIAGNOSTICS -color-diagnostics)
  55. set(CMAKE_Swift_COMPILE_OPTIONS_COLOR_DIAGNOSTICS_OFF -no-color-diagnostics)
  56. set(CMAKE_Swift_LINKER_PREFERENCE 50)
  57. set(CMAKE_Swift_LINKER_PREFERENCE_PROPAGATES 1)
  58. # NOTE(compnerd) use the short form for convenience and ease of search. They
  59. # are treated equivalent to their long form names as well as custom Swift
  60. # specific names.
  61. set(CMAKE_Swift_COMPILE_OPTIONS_MSVC_RUNTIME_LIBRARY_MultiThreaded -libc MT)
  62. set(CMAKE_Swift_COMPILE_OPTIONS_MSVC_RUNTIME_LIBRARY_MultiThreadedDLL -libc MD)
  63. set(CMAKE_Swift_COMPILE_OPTIONS_MSVC_RUNTIME_LIBRARY_MultiThreadedDebug -libc MTd)
  64. set(CMAKE_Swift_COMPILE_OPTIONS_MSVC_RUNTIME_LIBRARY_MultiThreadedDebugDLL -libc MDd)
  65. set(CMAKE_Swift_FLAGS_DEBUG_INIT "-Onone -g")
  66. set(CMAKE_Swift_FLAGS_RELEASE_INIT "-O")
  67. set(CMAKE_Swift_FLAGS_RELWITHDEBINFO_INIT "-O -g")
  68. set(CMAKE_Swift_FLAGS_MINSIZEREL_INIT "-Osize")
  69. if(CMAKE_GENERATOR STREQUAL "Xcode")
  70. string(APPEND CMAKE_Swift_FLAGS_DEBUG_INIT " ${CMAKE_Swift_FLAGS_DEBUG_LINKER_FLAGS}")
  71. string(APPEND CMAKE_Swift_FLAGS_RELWITHDEBINFO_INIT " ${CMAKE_Swift_FLAGS_RELWITHDEBINFO_LINKER_FLAGS}")
  72. endif()
  73. # Warns if unset and uses old policy.
  74. # Old policy flag-smashes the wmo and incremental flags onto the compiler flags.
  75. # New policy respects the Swift_COMPILATION_MODE target property to add
  76. # incremental and wholemodule optimization flags as appropriate.
  77. cmake_policy(GET CMP0157 __SWIFT_COMP_MODE_CMP0157)
  78. if(__SWIFT_COMP_MODE_CMP0157 STREQUAL "NEW")
  79. set(CMAKE_Swift_COMPILATION_MODE_DEFAULT "incremental")
  80. else()
  81. # Xcode has a separate Xcode project option (SWIFT_COMPILATION_MODE) used to set
  82. # whether compiling with whole-module optimizations or incrementally. Setting
  83. # these options here will have no effect when compiling with the built-in driver,
  84. # and will explode violently, leaving build products in the source directory, when
  85. # using the old swift driver. Don't append `-incremental` or `-wmo` to the
  86. # flags in the Xcode generator.
  87. if(NOT CMAKE_GENERATOR STREQUAL "Xcode")
  88. # Enable Whole Module Optimization by default unless the old
  89. # C++ driver is being used, which behaves differently under WMO.
  90. if(NOT CMAKE_Swift_COMPILER_USE_OLD_DRIVER)
  91. string(APPEND CMAKE_Swift_FLAGS_RELEASE_INIT " -wmo")
  92. string(APPEND CMAKE_Swift_FLAGS_RELWITHDEBINFO_INIT " -wmo")
  93. string(APPEND CMAKE_Swift_FLAGS_MINSIZEREL_INIT " -wmo")
  94. endif()
  95. string(APPEND CMAKE_Swift_FLAGS_DEBUG_INIT " -incremental")
  96. endif()
  97. endif()
  98. unset(__SWIFT_COMP_MODE_CMP0157)
  99. cmake_initialize_per_config_variable(CMAKE_Swift_FLAGS "Swift Compiler Flags")
  100. if(NOT CMAKE_Swift_NUM_THREADS MATCHES "^[0-9]+$")
  101. cmake_host_system_information(RESULT CMAKE_Swift_NUM_THREADS QUERY NUMBER_OF_LOGICAL_CORES)
  102. endif()
  103. # Swift split-compilation requires CMP0157 NEW policy
  104. if(CMAKE_Swift_COMPILATION_MODE_DEFAULT)
  105. set(CMAKE_Swift_PARALLEL_FLAGS "-j ${CMAKE_Swift_NUM_THREADS} -num-threads ${CMAKE_Swift_NUM_THREADS}")
  106. if(NOT CMAKE_Swift_COMPILE_OBJECT)
  107. # Omit the object output. The output is controlled by the output-file-map
  108. # for normal builds. For wholemodule builds, CMake appends the appropriate
  109. # flags.
  110. set(CMAKE_Swift_COMPILE_OBJECT "<CMAKE_Swift_COMPILER> ${CMAKE_Swift_PARALLEL_FLAGS} -c <DEFINES> <FLAGS> <INCLUDES> <SOURCE>")
  111. endif()
  112. if(NOT CMAKE_Swift_CREATE_SHARED_LIBRARY)
  113. set(CMAKE_Swift_CREATE_SHARED_LIBRARY "<CMAKE_Swift_COMPILER> ${CMAKE_Swift_PARALLEL_FLAGS} -emit-library <CMAKE_SHARED_LIBRARY_Swift_FLAGS> <LANGUAGE_COMPILE_FLAGS> <LINK_FLAGS> ${CMAKE_Swift_IMPLIB_LINKER_FLAGS} <SONAME_FLAG> <TARGET_INSTALLNAME_DIR><TARGET_SONAME> -o <TARGET> <OBJECTS> <LINK_LIBRARIES>")
  114. endif()
  115. if(NOT CMAKE_Swift_CREATE_SHARED_MODULE)
  116. set(CMAKE_Swift_CREATE_SHARED_MODULE "${CMAKE_Swift_CREATE_SHARED_LIBRARY} <CMAKE_SHARED_MODULE_CREATE_Swift_FLAGS>")
  117. endif()
  118. if(NOT CMAKE_Swift_LINK_EXECUTABLE)
  119. set(CMAKE_Swift_LINK_EXECUTABLE "<CMAKE_Swift_COMPILER> ${CMAKE_Swift_PARALLEL_FLAGS} -emit-executable -o <TARGET> <FLAGS> <OBJECTS> <LINK_FLAGS> <LINK_LIBRARIES>")
  120. endif()
  121. if(NOT CMAKE_Swift_CREATE_STATIC_LIBRARY)
  122. set(CMAKE_Swift_CREATE_STATIC_LIBRARY "<CMAKE_Swift_COMPILER> -emit-library -static -o <TARGET> <OBJECTS> <LINK_FLAGS>")
  123. set(CMAKE_Swift_ARCHIVE_CREATE "<CMAKE_AR> crs <TARGET> <OBJECTS>")
  124. set(CMAKE_Swift_ARCHIVE_FINISH "")
  125. endif()
  126. unset(CMAKE_Swift_PARALLEL_FLAGS)
  127. else()
  128. # NOTE(compnerd) we do not have an object compile rule since we build the objects as part of the link step
  129. if(NOT CMAKE_Swift_COMPILE_OBJECT)
  130. set(CMAKE_Swift_COMPILE_OBJECT ":")
  131. endif()
  132. if(NOT CMAKE_Swift_CREATE_SHARED_LIBRARY)
  133. set(CMAKE_Swift_CREATE_SHARED_LIBRARY "<CMAKE_Swift_COMPILER> -j ${CMAKE_Swift_NUM_THREADS} -num-threads ${CMAKE_Swift_NUM_THREADS} -emit-library -o <TARGET> -module-name <SWIFT_MODULE_NAME> -module-link-name <SWIFT_LIBRARY_NAME> -emit-module -emit-module-path <SWIFT_MODULE> -emit-dependencies <DEFINES> <FLAGS> <INCLUDES> <SWIFT_SOURCES> <LINK_FLAGS> <SONAME_FLAG> <TARGET_INSTALLNAME_DIR><TARGET_SONAME> ${CMAKE_Swift_IMPLIB_LINKER_FLAGS} <LINK_LIBRARIES>")
  134. endif()
  135. if(NOT CMAKE_Swift_CREATE_SHARED_MODULE)
  136. set(CMAKE_Swift_CREATE_SHARED_MODULE "${CMAKE_Swift_CREATE_SHARED_LIBRARY} <CMAKE_SHARED_MODULE_CREATE_Swift_FLAGS>")
  137. endif()
  138. if(NOT CMAKE_Swift_LINK_EXECUTABLE)
  139. set(CMAKE_Swift_LINK_EXECUTABLE "<CMAKE_Swift_COMPILER> -j ${CMAKE_Swift_NUM_THREADS} -num-threads ${CMAKE_Swift_NUM_THREADS} -emit-executable -o <TARGET> -emit-dependencies <DEFINES> <FLAGS> <INCLUDES> <SWIFT_SOURCES> <LINK_FLAGS> <LINK_LIBRARIES>")
  140. endif()
  141. if(NOT CMAKE_Swift_LINK_EXECUTABLE_WITH_EXPORTS)
  142. set(CMAKE_Swift_LINK_EXECUTABLE_WITH_EXPORTS "${CMAKE_Swift_LINK_EXECUTABLE} -emit-module -emit-module-path <SWIFT_MODULE> ${CMAKE_Swift_IMPLIB_LINKER_FLAGS}")
  143. endif()
  144. if(NOT CMAKE_Swift_CREATE_STATIC_LIBRARY)
  145. set(CMAKE_Swift_CREATE_STATIC_LIBRARY "<CMAKE_Swift_COMPILER> -j ${CMAKE_Swift_NUM_THREADS} -num-threads ${CMAKE_Swift_NUM_THREADS} -emit-library -static -o <TARGET> -module-name <SWIFT_MODULE_NAME> -module-link-name <SWIFT_LIBRARY_NAME> -emit-module -emit-module-path <SWIFT_MODULE> -emit-dependencies <DEFINES> <FLAGS> <INCLUDES> <SWIFT_SOURCES> <LINK_FLAGS> <LINK_LIBRARIES>")
  146. set(CMAKE_Swift_ARCHIVE_CREATE "<CMAKE_AR> crs <TARGET> <OBJECTS>")
  147. set(CMAKE_Swift_ARCHIVE_FINISH "")
  148. endif()
  149. endif()
  150. set(CMAKE_Swift_USE_LINKER_INFORMATION TRUE)
  151. set(CMAKE_Swift_INFORMATION_LOADED 1)