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