Skip to content

Commit 48399af

Browse files
authored
Merge branch 'main' into unicodectomy
2 parents f1a5e27 + d1bb98b commit 48399af

File tree

255 files changed

+5148
-2476
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

255 files changed

+5148
-2476
lines changed

CMakeLists.txt

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -192,17 +192,17 @@ set(SWIFT_TOOLS_ENABLE_LTO OFF CACHE STRING "Build Swift tools with LTO. One
192192
option only affects the tools that run on the host (the compiler), and has
193193
no effect on the target libraries (the standard library and the runtime).")
194194

195-
# NOTE: We do not currently support building libswift with the Xcode generator.
196-
cmake_dependent_option(LIBSWIFT_BUILD_MODE [=[
197-
How to build libswift. Possible values are
198-
OFF: the compiler is built without libswift
199-
HOSTTOOLS: libswift is built with a pre-installed toolchain
200-
BOOTSTRAPPING: libswift is built with a 2-stage bootstrapping process
201-
BOOTSTRAPPING-WITH-HOSTLIBS: libswift is built with a 2-stage bootstrapping process,
195+
# NOTE: We do not currently support building the swift compiler modules with the Xcode generator.
196+
cmake_dependent_option(BOOTSTRAPPING_MODE [=[
197+
How to build the swift compiler modules. Possible values are
198+
OFF: build without swift modules
199+
HOSTTOOLS: build with a pre-installed toolchain
200+
BOOTSTRAPPING: build with a 2-stage bootstrapping process
201+
BOOTSTRAPPING-WITH-HOSTLIBS: build with a 2-stage bootstrapping process,
202202
but the compiler links against the host system swift libs (macOS only)
203-
CROSSCOMPILE: libswift is cross-compiled with a native host compiler, provided in
203+
CROSSCOMPILE: cross-compiledwith a native host compiler, provided in
204204
`SWIFT_NATIVE_SWIFT_TOOLS_PATH` (non-Darwin only)
205-
CROSSCOMPILE-WITH-HOSTLIBS: libswift is built with a bootstrapping-with-hostlibs compiled
205+
CROSSCOMPILE-WITH-HOSTLIBS: build with a bootstrapping-with-hostlibs compiled
206206
compiler, provided in `SWIFT_NATIVE_SWIFT_TOOLS_PATH`
207207
]=] OFF "NOT CMAKE_GENERATOR STREQUAL \"Xcode\"" OFF)
208208

@@ -620,24 +620,24 @@ set(SWIFT_RUNTIME_OUTPUT_INTDIR "${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/bin")
620620
set(SWIFT_LIBRARY_OUTPUT_INTDIR "${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/lib")
621621
if("${SWIFT_NATIVE_SWIFT_TOOLS_PATH}" STREQUAL "")
622622
set(SWIFT_NATIVE_SWIFT_TOOLS_PATH "${SWIFT_RUNTIME_OUTPUT_INTDIR}")
623-
set(SWIFT_EXEC_FOR_LIBSWIFT "${CMAKE_Swift_COMPILER}")
624-
elseif(LIBSWIFT_BUILD_MODE MATCHES "BOOTSTRAPPING.*")
625-
# If cross-compiling we don't have to bootstrap. We can just use the previously
626-
# built native swiftc to build libswift.
627-
message(STATUS "Building libswift with previously built tools instead of bootstrapping")
628-
set(SWIFT_EXEC_FOR_LIBSWIFT "${SWIFT_NATIVE_SWIFT_TOOLS_PATH}/swiftc")
629-
if(LIBSWIFT_BUILD_MODE STREQUAL "BOOTSTRAPPING-WITH-HOSTLIBS")
630-
set(LIBSWIFT_BUILD_MODE "CROSSCOMPILE-WITH-HOSTLIBS")
631-
elseif(LIBSWIFT_BUILD_MODE STREQUAL "BOOTSTRAPPING")
632-
set(LIBSWIFT_BUILD_MODE "CROSSCOMPILE")
623+
set(SWIFT_EXEC_FOR_SWIFT_MODULES "${CMAKE_Swift_COMPILER}")
624+
elseif(BOOTSTRAPPING_MODE MATCHES "BOOTSTRAPPING.*")
625+
# If cross-compiling, we don't have to bootstrap. We can just use the previously
626+
# built native swiftc to build the swift compiler modules.
627+
message(STATUS "Building swift modules with previously built tools instead of bootstrapping")
628+
set(SWIFT_EXEC_FOR_SWIFT_MODULES "${SWIFT_NATIVE_SWIFT_TOOLS_PATH}/swiftc")
629+
if(BOOTSTRAPPING_MODE STREQUAL "BOOTSTRAPPING-WITH-HOSTLIBS")
630+
set(BOOTSTRAPPING_MODE "CROSSCOMPILE-WITH-HOSTLIBS")
631+
elseif(BOOTSTRAPPING_MODE STREQUAL "BOOTSTRAPPING")
632+
set(BOOTSTRAPPING_MODE "CROSSCOMPILE")
633633
else()
634-
set(LIBSWIFT_BUILD_MODE "HOSTTOOLS")
634+
set(BOOTSTRAPPING_MODE "HOSTTOOLS")
635635
endif()
636636
endif()
637637

638-
if(LIBSWIFT_BUILD_MODE MATCHES "HOSTTOOLS|.*-WITH-HOSTLIBS")
638+
if(BOOTSTRAPPING_MODE MATCHES "HOSTTOOLS|.*-WITH-HOSTLIBS")
639639
if(SWIFT_ENABLE_ARRAY_COW_CHECKS)
640-
message(STATUS "array COW checks disabled when building libswift with host libraries")
640+
message(STATUS "array COW checks disabled when building the swift modules with host libraries")
641641
set(SWIFT_ENABLE_ARRAY_COW_CHECKS FALSE)
642642
endif()
643643
endif()
@@ -1004,7 +1004,7 @@ if(SWIFT_INCLUDE_TOOLS)
10041004
message(STATUS " Build type: ${CMAKE_BUILD_TYPE}")
10051005
message(STATUS " Assertions: ${LLVM_ENABLE_ASSERTIONS}")
10061006
message(STATUS " LTO: ${SWIFT_TOOLS_ENABLE_LTO}")
1007-
message(STATUS " libswift: ${LIBSWIFT_BUILD_MODE}")
1007+
message(STATUS " Bootstrapping: ${BOOTSTRAPPING_MODE}")
10081008
message(STATUS "")
10091009
else()
10101010
message(STATUS "Not building host Swift tools")
@@ -1124,12 +1124,12 @@ add_subdirectory(include)
11241124
if(SWIFT_INCLUDE_TOOLS)
11251125
add_subdirectory(lib)
11261126

1127-
# "libswift" must come before "tools".
1128-
# It adds libswift module names to the global property "libswift_modules"
1127+
# SwiftCompilerSources must come before "tools".
1128+
# It adds swift module names to the global property "swift_compiler_modules"
11291129
# which is used in add_swift_host_tool for the lldb workaround.
11301130
#
1131-
# NOTE: We do not currently support libswift with the Xcode generator.
1132-
add_subdirectory(libswift)
1131+
# NOTE: We do not currently support SwiftCompilerSources with the Xcode generator.
1132+
add_subdirectory(SwiftCompilerSources)
11331133

11341134
# Always include this after including stdlib/!
11351135
# Refer to the large comment above the add_subdirectory(stdlib) call.

SwiftCompilerSources/CMakeLists.txt

Lines changed: 240 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,240 @@
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+

libswift/Package.swift renamed to SwiftCompilerSources/Package.swift

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
import PackageDescription
44

55
let package = Package(
6-
name: "libswift",
6+
name: "SwiftCompilerSources",
77
platforms: [
88
.macOS("10.9"),
99
],
1010
products: [
1111
.library(
1212
name: "Swift",
1313
type: .static,
14-
targets: ["SIL", "Optimizer"]),
14+
targets: ["SIL", "Optimizer", "ExperimentalRegex"]),
1515
],
1616
dependencies: [
1717
],
@@ -25,9 +25,16 @@ let package = Package(
2525
"-I", "../include/swift",
2626
"-cross-module-optimization"
2727
])]),
28+
.target(
29+
name: "ExperimentalRegex",
30+
dependencies: [],
31+
swiftSettings: [SwiftSetting.unsafeFlags([
32+
"-I", "../include/swift",
33+
"-cross-module-optimization"
34+
])]),
2835
.target(
2936
name: "Optimizer",
30-
dependencies: ["SIL"],
37+
dependencies: ["SIL", "ExperimentalRegex"],
3138
swiftSettings: [SwiftSetting.unsafeFlags([
3239
"-I", "../include/swift",
3340
"-cross-module-optimization"

0 commit comments

Comments
 (0)