|
6 | 6 | # See http://swift.org/LICENSE.txt for license information
|
7 | 7 | # See http://swift.org/CONTRIBUTORS.txt for Swift project authors
|
8 | 8 |
|
9 |
| -# A dummy libswift if libswift is disabled |
10 |
| -add_swift_host_library(libswiftStub OBJECT stubs.cpp) |
| 9 | + |
| 10 | +# Following function are needed as a workaround until it's possible to compile |
| 11 | +# swift code with cmake's builtin swift support. |
| 12 | + |
| 13 | +# Add a swift compiler module |
| 14 | +# |
| 15 | +# Creates a target to compile a swift module. |
| 16 | +# Adds the module name to the global property "swift_compiler_modules". |
| 17 | +# |
| 18 | +function(add_swift_compiler_module module) |
| 19 | + cmake_parse_arguments(ALSM |
| 20 | + "" |
| 21 | + "" |
| 22 | + "DEPENDS" |
| 23 | + ${ARGN}) |
| 24 | + set(raw_sources ${ALSM_UNPARSED_ARGUMENTS}) |
| 25 | + set(sources) |
| 26 | + foreach(raw_source ${raw_sources}) |
| 27 | + get_filename_component( |
| 28 | + raw_source "${raw_source}" REALPATH BASE_DIR "${CMAKE_CURRENT_SOURCE_DIR}") |
| 29 | + list(APPEND sources "${raw_source}") |
| 30 | + endforeach() |
| 31 | + |
| 32 | + set(target_name "SwiftModule${module}") |
| 33 | + |
| 34 | + # Add a target which depends on the actual compilation target, which |
| 35 | + # will be created in add_swift_compiler_modules_library. |
| 36 | + # This target is mainly used to add properties, like the list of source files. |
| 37 | + add_custom_target( |
| 38 | + ${target_name} |
| 39 | + SOURCES ${sources} |
| 40 | + COMMENT "swift compiler module ${module}") |
| 41 | + |
| 42 | + set_property(TARGET ${target_name} PROPERTY module_name ${module}) |
| 43 | + set_property(TARGET ${target_name} PROPERTY module_depends ${ALSM_DEPENDS}) |
| 44 | + |
| 45 | + get_property(modules GLOBAL PROPERTY swift_compiler_modules) |
| 46 | + set_property(GLOBAL PROPERTY swift_compiler_modules ${modules} ${module}) |
| 47 | +endfunction() |
| 48 | + |
| 49 | +# Add source files to a swift compiler module. |
| 50 | +# |
| 51 | +function(swift_compiler_sources module) |
| 52 | + cmake_parse_arguments(LSS |
| 53 | + "" |
| 54 | + "" |
| 55 | + "" |
| 56 | + ${ARGN}) |
| 57 | + set(sources ${LSS_UNPARSED_ARGUMENTS}) |
| 58 | + list(TRANSFORM sources PREPEND "${CMAKE_CURRENT_SOURCE_DIR}/") |
| 59 | + |
| 60 | + set(target_name "SwiftModule${module}") |
| 61 | + set_property(TARGET "SwiftModule${module}" APPEND PROPERTY SOURCES ${sources}) |
| 62 | +endfunction() |
| 63 | + |
| 64 | +# Add a library target for the swift compiler modules. |
| 65 | +# |
| 66 | +# Adds targets to compile all swift compiler modules and a target for the |
| 67 | +# library itself. |
| 68 | +# |
| 69 | +function(add_swift_compiler_modules_library name) |
| 70 | + cmake_parse_arguments(ALS |
| 71 | + "" |
| 72 | + "BOOTSTRAPPING;SWIFT_EXEC" |
| 73 | + "DEPENDS" |
| 74 | + ${ARGN}) |
| 75 | + |
| 76 | + set(swift_compile_options |
| 77 | + "-Xfrontend" "-validate-tbd-against-ir=none" |
| 78 | + "-Xfrontend" "-enable-cxx-interop" |
| 79 | + "-Xcc" "-UIBOutlet" "-Xcc" "-UIBAction" "-Xcc" "-UIBInspectable") |
| 80 | + |
| 81 | + if(CMAKE_BUILD_TYPE STREQUAL Debug) |
| 82 | + list(APPEND swift_compile_options "-g") |
| 83 | + else() |
| 84 | + list(APPEND swift_compile_options "-O" "-cross-module-optimization") |
| 85 | + endif() |
| 86 | + |
| 87 | + get_bootstrapping_path(build_dir ${CMAKE_CURRENT_BINARY_DIR} "${ALS_BOOTSTRAPPING}") |
| 88 | + |
| 89 | + set(sdk_option "") |
| 90 | + |
| 91 | + if(SWIFT_HOST_VARIANT_SDK IN_LIST SWIFT_DARWIN_PLATFORMS) |
| 92 | + set(deployment_version "10.15") # TODO: once #38675 lands, replace this with |
| 93 | +# set(deployment_version "${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_DEPLOYMENT_VERSION}") |
| 94 | + set(sdk_option "-sdk" "${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_ARCH_${SWIFT_HOST_VARIANT_ARCH}_PATH}") |
| 95 | + if(${LIBSWIFT_BUILD_MODE} STREQUAL "CROSSCOMPILE-WITH-HOSTLIBS") |
| 96 | + # Let the cross-compiled compile don't pick up the compiled stdlib by providing |
| 97 | + # an (almost) empty resource dir. |
| 98 | + # The compiler will instead pick up the stdlib from the SDK. |
| 99 | + get_filename_component(swift_exec_bin_dir ${ALS_SWIFT_EXEC} DIRECTORY) |
| 100 | + set(sdk_option ${sdk_option} "-resource-dir" "${swift_exec_bin_dir}/../bootstrapping0/lib/swift") |
| 101 | + endif() |
| 102 | + elseif(${LIBSWIFT_BUILD_MODE} STREQUAL "CROSSCOMPILE") |
| 103 | + set(sdk_option "-sdk" "${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_ARCH_${SWIFT_HOST_VARIANT_ARCH}_PATH}") |
| 104 | + get_filename_component(swift_exec_bin_dir ${ALS_SWIFT_EXEC} DIRECTORY) |
| 105 | + set(sdk_option ${sdk_option} "-resource-dir" "${swift_exec_bin_dir}/../lib/swift") |
| 106 | + endif() |
| 107 | + get_versioned_target_triple(target ${SWIFT_HOST_VARIANT_SDK} |
| 108 | + ${SWIFT_HOST_VARIANT_ARCH} "${deployment_version}") |
| 109 | + |
| 110 | + get_property(modules GLOBAL PROPERTY "swift_compiler_modules") |
| 111 | + foreach(module ${modules}) |
| 112 | + |
| 113 | + set(module_target "SwiftModule${module}") |
| 114 | + get_target_property(module ${module_target} "module_name") |
| 115 | + get_target_property(sources ${module_target} SOURCES) |
| 116 | + get_target_property(dependencies ${module_target} "module_depends") |
| 117 | + set(deps, "") |
| 118 | + if (dependencies) |
| 119 | + foreach(dep_module ${dependencies}) |
| 120 | + if (DEFINED "${dep_module}_dep_target") |
| 121 | + list(APPEND deps "${${dep_module}_dep_target}") |
| 122 | + else() |
| 123 | + message(FATAL_ERROR "module dependency ${module} -> ${dep_module} not found. Make sure to add modules in dependency order") |
| 124 | + endif() |
| 125 | + endforeach() |
| 126 | + endif() |
| 127 | + |
| 128 | + set(module_obj_file "${build_dir}/${module}.o") |
| 129 | + set(module_file "${build_dir}/${module}.swiftmodule") |
| 130 | + set_property(TARGET ${module_target} PROPERTY "module_file" "${module_file}") |
| 131 | + |
| 132 | + set(all_obj_files ${all_obj_files} ${module_obj_file}) |
| 133 | + |
| 134 | + # Compile the module into an object file |
| 135 | + add_custom_command_target(dep_target OUTPUT ${module_obj_file} |
| 136 | + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} |
| 137 | + DEPENDS ${sources} ${deps} ${ALS_DEPENDS} |
| 138 | + COMMAND ${ALS_SWIFT_EXEC} "-c" "-o" ${module_obj_file} |
| 139 | + ${sdk_option} |
| 140 | + "-target" ${target} |
| 141 | + "-module-name" ${module} "-emit-module" |
| 142 | + "-emit-module-path" "${build_dir}/${module}.swiftmodule" |
| 143 | + "-parse-as-library" ${sources} |
| 144 | + "-wmo" ${swift_compile_options} |
| 145 | + "-I" "${SWIFT_SOURCE_DIR}/include/swift" |
| 146 | + "-I" "${SWIFT_SOURCE_DIR}/include" |
| 147 | + "-I" "${build_dir}" |
| 148 | + COMMENT "Building swift module ${module}") |
| 149 | + |
| 150 | + set("${module}_dep_target" ${dep_target}) |
| 151 | + endforeach() |
| 152 | + |
| 153 | + # Create a static library containing all module object files. |
| 154 | + add_library(${name} STATIC ${all_obj_files}) |
| 155 | + set_target_properties(${name} PROPERTIES LINKER_LANGUAGE CXX) |
| 156 | + set_property(GLOBAL APPEND PROPERTY SWIFT_BUILDTREE_EXPORTS ${name}) |
| 157 | +endfunction() |
| 158 | + |
| 159 | + |
| 160 | +# A dummy library if swift in the compiler is disabled |
| 161 | +add_swift_host_library(swiftCompilerStub OBJECT stubs.cpp) |
11 | 162 |
|
12 | 163 | if (NOT LIBSWIFT_BUILD_MODE)
|
13 | 164 |
|
14 |
| - add_library(libswift ALIAS libswiftStub) |
| 165 | + add_library(swiftCompilerModules ALIAS swiftCompilerStub) |
15 | 166 |
|
16 | 167 | else()
|
17 | 168 | # Note: "Swift" is not added intentinally here, because it would break
|
18 | 169 | # the bootstrapping build in case no swift toolchain is installed on the host.
|
19 |
| - project(LibSwift LANGUAGES C CXX) |
| 170 | + project(SwiftInTheCompiler LANGUAGES C CXX) |
20 | 171 |
|
21 | 172 | add_subdirectory(Sources)
|
22 | 173 |
|
23 | 174 | if(${LIBSWIFT_BUILD_MODE} MATCHES "HOSTTOOLS|CROSSCOMPILE")
|
24 | 175 |
|
25 | 176 | if (NOT SWIFT_EXEC_FOR_LIBSWIFT)
|
26 |
| - message(FATAL_ERROR "Need a swift toolchain for building libswift") |
| 177 | + message(FATAL_ERROR "Need a swift toolchain building swift compiler sources") |
27 | 178 | endif()
|
28 | 179 |
|
29 |
| - add_libswift("libswift" |
| 180 | + add_swift_compiler_modules_library(swiftCompilerModules |
30 | 181 | SWIFT_EXEC "${SWIFT_EXEC_FOR_LIBSWIFT}")
|
31 | 182 |
|
32 | 183 | elseif(${LIBSWIFT_BUILD_MODE} MATCHES "BOOTSTRAPPING.*")
|
@@ -59,14 +210,14 @@ else()
|
59 | 210 |
|
60 | 211 | # Bootstrapping - stage 1, using the compiler from level 0
|
61 | 212 |
|
62 |
| - add_libswift(libswift-bootstrapping1 |
| 213 | + add_swift_compiler_modules_library(swiftCompilerModules-bootstrapping1 |
63 | 214 | SWIFT_EXEC $<TARGET_FILE_DIR:swift-frontend-bootstrapping0>/swiftc${CMAKE_EXECUTABLE_SUFFIX}
|
64 | 215 | DEPENDS ${b0_deps}
|
65 | 216 | BOOTSTRAPPING 1)
|
66 | 217 |
|
67 | 218 | # The final build, using the compiler from stage 1
|
68 | 219 |
|
69 |
| - add_libswift(libswift |
| 220 | + add_swift_compiler_modules_library(swiftCompilerModules |
70 | 221 | SWIFT_EXEC $<TARGET_FILE_DIR:swift-frontend-bootstrapping1>/swiftc${CMAKE_EXECUTABLE_SUFFIX}
|
71 | 222 | DEPENDS ${b1_deps})
|
72 | 223 |
|
|
0 commit comments