Skip to content

Commit ba364a1

Browse files
committed
libswift: rename cmake targets and functions
libswift -> swiftCompilerModules or swiftCompilerSources
1 parent 4beb94c commit ba364a1

File tree

15 files changed

+182
-183
lines changed

15 files changed

+182
-183
lines changed

SwiftCompilerSources/CMakeLists.txt

Lines changed: 159 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,178 @@
66
# See http://swift.org/LICENSE.txt for license information
77
# See http://swift.org/CONTRIBUTORS.txt for Swift project authors
88

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)
11162

12163
if (NOT LIBSWIFT_BUILD_MODE)
13164

14-
add_library(libswift ALIAS libswiftStub)
165+
add_library(swiftCompilerModules ALIAS swiftCompilerStub)
15166

16167
else()
17168
# Note: "Swift" is not added intentinally here, because it would break
18169
# 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)
20171

21172
add_subdirectory(Sources)
22173

23174
if(${LIBSWIFT_BUILD_MODE} MATCHES "HOSTTOOLS|CROSSCOMPILE")
24175

25176
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")
27178
endif()
28179

29-
add_libswift("libswift"
180+
add_swift_compiler_modules_library(swiftCompilerModules
30181
SWIFT_EXEC "${SWIFT_EXEC_FOR_LIBSWIFT}")
31182

32183
elseif(${LIBSWIFT_BUILD_MODE} MATCHES "BOOTSTRAPPING.*")
@@ -59,14 +210,14 @@ else()
59210

60211
# Bootstrapping - stage 1, using the compiler from level 0
61212

62-
add_libswift(libswift-bootstrapping1
213+
add_swift_compiler_modules_library(swiftCompilerModules-bootstrapping1
63214
SWIFT_EXEC $<TARGET_FILE_DIR:swift-frontend-bootstrapping0>/swiftc${CMAKE_EXECUTABLE_SUFFIX}
64215
DEPENDS ${b0_deps}
65216
BOOTSTRAPPING 1)
66217

67218
# The final build, using the compiler from stage 1
68219

69-
add_libswift(libswift
220+
add_swift_compiler_modules_library(swiftCompilerModules
70221
SWIFT_EXEC $<TARGET_FILE_DIR:swift-frontend-bootstrapping1>/swiftc${CMAKE_EXECUTABLE_SUFFIX}
71222
DEPENDS ${b1_deps})
72223

SwiftCompilerSources/Sources/ExperimentalRegex/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ foreach(source ${_LIBSWIFT_EXPERIMENTAL_REGEX_SOURCES})
1515
endforeach()
1616
message(STATUS "Using Experimental String Processing library for libswift ExperimentalRegex (${EXPERIMENTAL_STRING_PROCESSING_SOURCE_DIR}).")
1717

18-
add_libswift_module(ExperimentalRegex
18+
add_swift_compiler_module(ExperimentalRegex
1919
"${LIBSWIFT_EXPERIMENTAL_REGEX_SOURCES}"
2020
Regex.swift)
2121

SwiftCompilerSources/Sources/Optimizer/Analysis/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
# See http://swift.org/LICENSE.txt for license information
77
# See http://swift.org/CONTRIBUTORS.txt for Swift project authors
88

9-
libswift_sources(Optimizer
9+
swift_compiler_sources(Optimizer
1010
AliasAnalysis.swift
1111
CalleeAnalysis.swift)

SwiftCompilerSources/Sources/Optimizer/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ if(SWIFT_ENABLE_EXPERIMENTAL_STRING_PROCESSING)
1212
list(APPEND dependencies ExperimentalRegex)
1313
endif()
1414

15-
add_libswift_module(Optimizer DEPENDS ${dependencies})
15+
add_swift_compiler_module(Optimizer DEPENDS ${dependencies})
1616

1717
add_subdirectory(Analysis)
1818
add_subdirectory(InstructionPasses)

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
# See http://swift.org/LICENSE.txt for license information
77
# See http://swift.org/CONTRIBUTORS.txt for Swift project authors
88

9-
libswift_sources(Optimizer
9+
swift_compiler_sources(Optimizer
1010
SILPrinter.swift
1111
MergeCondFails.swift
1212
)

SwiftCompilerSources/Sources/Optimizer/InstructionPasses/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
# See http://swift.org/LICENSE.txt for license information
77
# See http://swift.org/CONTRIBUTORS.txt for Swift project authors
88

9-
libswift_sources(Optimizer
9+
swift_compiler_sources(Optimizer
1010
SimplifyBeginCOWMutation.swift
1111
SimplifyGlobalValue.swift
1212
SimplifyStrongRetainRelease.swift)

SwiftCompilerSources/Sources/Optimizer/PassManager/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
# See http://swift.org/LICENSE.txt for license information
77
# See http://swift.org/CONTRIBUTORS.txt for Swift project authors
88

9-
libswift_sources(Optimizer
9+
swift_compiler_sources(Optimizer
1010
PassUtils.swift
1111
PassRegistration.swift)
1212

SwiftCompilerSources/Sources/Optimizer/Utilities/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@
66
# See http://swift.org/LICENSE.txt for license information
77
# See http://swift.org/CONTRIBUTORS.txt for Swift project authors
88

9-
libswift_sources(Optimizer
9+
swift_compiler_sources(Optimizer
1010
OptUtils.swift)

SwiftCompilerSources/Sources/SIL/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
# See http://swift.org/LICENSE.txt for license information
77
# See http://swift.org/CONTRIBUTORS.txt for Swift project authors
88

9-
add_libswift_module(SIL
9+
add_swift_compiler_module(SIL
1010
ApplySite.swift
1111
Argument.swift
1212
BasicBlock.swift

0 commit comments

Comments
 (0)