|
| 1 | +# This source file is part of the Swift.org open source project |
| 2 | +# |
| 3 | +# Copyright (c) 2014 - 2021 Apple Inc. and the Swift project authors |
| 4 | +# Licensed under Apache License v2.0 with Runtime Library Exception |
| 5 | +# |
| 6 | +# See http://swift.org/LICENSE.txt for license information |
| 7 | +# See http://swift.org/CONTRIBUTORS.txt for Swift project authors |
| 8 | + |
| 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(${BOOTSTRAPPING_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(${BOOTSTRAPPING_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 | + set(all_obj_files) |
| 111 | + set(all_module_targets) |
| 112 | + get_property(modules GLOBAL PROPERTY "swift_compiler_modules") |
| 113 | + foreach(module ${modules}) |
| 114 | + |
| 115 | + set(module_target "SwiftModule${module}") |
| 116 | + get_target_property(module ${module_target} "module_name") |
| 117 | + get_target_property(sources ${module_target} SOURCES) |
| 118 | + get_target_property(dependencies ${module_target} "module_depends") |
| 119 | + set(deps, "") |
| 120 | + if (dependencies) |
| 121 | + foreach(dep_module ${dependencies}) |
| 122 | + if (DEFINED "${dep_module}_dep_target") |
| 123 | + list(APPEND deps "${${dep_module}_dep_target}") |
| 124 | + else() |
| 125 | + message(FATAL_ERROR "module dependency ${module} -> ${dep_module} not found. Make sure to add modules in dependency order") |
| 126 | + endif() |
| 127 | + endforeach() |
| 128 | + endif() |
| 129 | + |
| 130 | + set(module_obj_file "${build_dir}/${module}.o") |
| 131 | + set(module_file "${build_dir}/${module}.swiftmodule") |
| 132 | + set_property(TARGET ${module_target} PROPERTY "module_file" "${module_file}") |
| 133 | + |
| 134 | + set(all_obj_files ${all_obj_files} ${module_obj_file}) |
| 135 | + |
| 136 | + # Compile the module into an object file |
| 137 | + add_custom_command_target(dep_target OUTPUT ${module_obj_file} |
| 138 | + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} |
| 139 | + DEPENDS ${sources} ${deps} ${ALS_DEPENDS} |
| 140 | + COMMAND ${ALS_SWIFT_EXEC} "-c" "-o" ${module_obj_file} |
| 141 | + ${sdk_option} |
| 142 | + "-target" ${target} |
| 143 | + "-module-name" ${module} "-emit-module" |
| 144 | + "-emit-module-path" "${build_dir}/${module}.swiftmodule" |
| 145 | + "-parse-as-library" ${sources} |
| 146 | + "-wmo" ${swift_compile_options} |
| 147 | + "-I" "${SWIFT_SOURCE_DIR}/include/swift" |
| 148 | + "-I" "${SWIFT_SOURCE_DIR}/include" |
| 149 | + "-I" "${build_dir}" |
| 150 | + COMMENT "Building swift module ${module}") |
| 151 | + |
| 152 | + set("${module}_dep_target" ${dep_target}) |
| 153 | + set(all_module_targets ${all_module_targets} ${dep_target}) |
| 154 | + endforeach() |
| 155 | + |
| 156 | + # Create a static library containing all module object files. |
| 157 | + add_library(${name} STATIC ${all_obj_files}) |
| 158 | + add_dependencies(${name} ${all_module_targets}) |
| 159 | + set_target_properties(${name} PROPERTIES LINKER_LANGUAGE CXX) |
| 160 | + set_property(GLOBAL APPEND PROPERTY SWIFT_BUILDTREE_EXPORTS ${name}) |
| 161 | +endfunction() |
| 162 | + |
| 163 | + |
| 164 | +# A dummy library if swift in the compiler is disabled |
| 165 | +add_swift_host_library(swiftCompilerStub OBJECT stubs.cpp) |
| 166 | + |
| 167 | +if (NOT BOOTSTRAPPING_MODE) |
| 168 | + |
| 169 | + add_library(swiftCompilerModules ALIAS swiftCompilerStub) |
| 170 | + |
| 171 | +else() |
| 172 | + # Note: "Swift" is not added intentinally here, because it would break |
| 173 | + # the bootstrapping build in case no swift toolchain is installed on the host. |
| 174 | + project(SwiftInTheCompiler LANGUAGES C CXX) |
| 175 | + |
| 176 | + add_subdirectory(Sources) |
| 177 | + |
| 178 | + if(${BOOTSTRAPPING_MODE} MATCHES "HOSTTOOLS|CROSSCOMPILE") |
| 179 | + |
| 180 | + if (NOT SWIFT_EXEC_FOR_SWIFT_MODULES) |
| 181 | + message(FATAL_ERROR "Need a swift toolchain building swift compiler sources") |
| 182 | + endif() |
| 183 | + |
| 184 | + add_swift_compiler_modules_library(swiftCompilerModules |
| 185 | + SWIFT_EXEC "${SWIFT_EXEC_FOR_SWIFT_MODULES}") |
| 186 | + |
| 187 | + elseif(${BOOTSTRAPPING_MODE} MATCHES "BOOTSTRAPPING.*") |
| 188 | + |
| 189 | + set(b0_deps swift-frontend-bootstrapping0 symlink-headers-bootstrapping0) |
| 190 | + set(b1_deps swift-frontend-bootstrapping1 symlink-headers-bootstrapping1) |
| 191 | + if(${BOOTSTRAPPING_MODE} STREQUAL "BOOTSTRAPPING") |
| 192 | + list(APPEND b0_deps swiftCore-bootstrapping0) |
| 193 | + list(APPEND b1_deps swiftCore-bootstrapping1) |
| 194 | + if(CMAKE_BUILD_TYPE STREQUAL "Debug") |
| 195 | + list(APPEND b0_deps swiftSwiftOnoneSupport-bootstrapping0) |
| 196 | + list(APPEND b1_deps swiftSwiftOnoneSupport-bootstrapping1) |
| 197 | + endif() |
| 198 | + if(SWIFT_HOST_VARIANT_SDK IN_LIST SWIFT_DARWIN_PLATFORMS) |
| 199 | + list(APPEND b0_deps swiftDarwin-bootstrapping0) |
| 200 | + list(APPEND b1_deps swiftDarwin-bootstrapping1) |
| 201 | + endif() |
| 202 | + endif() |
| 203 | + if(SWIFT_HOST_VARIANT_SDK IN_LIST SWIFT_DARWIN_PLATFORMS) |
| 204 | + set(platform ${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_LIB_SUBDIR}) |
| 205 | + set(compatibility_libs |
| 206 | + "swiftCompatibility50-${platform}" |
| 207 | + "swiftCompatibility51-${platform}" |
| 208 | + "swiftCompatibilityDynamicReplacements-${platform}") |
| 209 | + |
| 210 | + list(APPEND b0_deps ${compatibility_libs}) |
| 211 | + list(APPEND b1_deps ${compatibility_libs}) |
| 212 | + endif() |
| 213 | + |
| 214 | + |
| 215 | + # Bootstrapping - stage 1, using the compiler from level 0 |
| 216 | + |
| 217 | + add_swift_compiler_modules_library(swiftCompilerModules-bootstrapping1 |
| 218 | + SWIFT_EXEC $<TARGET_FILE_DIR:swift-frontend-bootstrapping0>/swiftc${CMAKE_EXECUTABLE_SUFFIX} |
| 219 | + DEPENDS ${b0_deps} |
| 220 | + BOOTSTRAPPING 1) |
| 221 | + |
| 222 | + # The final build, using the compiler from stage 1 |
| 223 | + |
| 224 | + add_swift_compiler_modules_library(swiftCompilerModules |
| 225 | + SWIFT_EXEC $<TARGET_FILE_DIR:swift-frontend-bootstrapping1>/swiftc${CMAKE_EXECUTABLE_SUFFIX} |
| 226 | + DEPENDS ${b1_deps}) |
| 227 | + |
| 228 | + if(BOOTSTRAPPING_MODE STREQUAL "BOOTSTRAPPING-WITH-HOSTLIBS") |
| 229 | + file(GLOB module_dirs "${CMAKE_BINARY_DIR}/bootstrapping*/lib/swift/macosx/*.swiftmodule") |
| 230 | + foreach(module_dir ${module_dirs}) |
| 231 | + message(WARNING "${module_dir} found from a previous 'bootstrapping' build: removing") |
| 232 | + file(REMOVE_RECURSE "${module_dir}") |
| 233 | + endforeach() |
| 234 | + endif() |
| 235 | + else() |
| 236 | + message(FATAL_ERROR "Unknown BOOTSTRAPPING_MODE '${BOOTSTRAPPING_MODE}'") |
| 237 | + endif() |
| 238 | + |
| 239 | +endif() |
| 240 | + |
0 commit comments