Skip to content

Commit 76c18c8

Browse files
authored
Merge pull request #64336 from rintaro/cmake-add_pure_swift
[CMake] Move add_pure_swift_host_library to a file
2 parents dbf6f47 + f9fba6c commit 76c18c8

File tree

6 files changed

+318
-215
lines changed

6 files changed

+318
-215
lines changed

CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ include(CheckSymbolExists)
9393
include(CMakeDependentOption)
9494
include(CheckLanguage)
9595
include(GNUInstallDirs)
96+
include(SwiftImplicitImport)
9697

9798
# Enable Swift for the host compiler build if we have the language. It is
9899
# optional until we have a bootstrap story.
@@ -732,6 +733,16 @@ message(STATUS "C++ Compiler (${CMAKE_CXX_COMPILER}) Version: ${CMAKE_CXX_COMPIL
732733
message(STATUS "Assembler (${CMAKE_${SWIFT_ASM_DIALECT}_COMPILER}) Version: ${CMAKE_${SWIFT_ASM_DIALECT}_COMPILER_VERSION}")
733734
if (CMAKE_Swift_COMPILER)
734735
message(STATUS "Swift Compiler (${CMAKE_Swift_COMPILER}) Version: ${CMAKE_Swift_COMPILER_VERSION}")
736+
737+
# Check if the current Swift compiler has implicit _StringProcessing module.
738+
swift_supports_implicit_module("string-processing"
739+
SWIFT_SUPPORTS_DISABLE_IMPLICIT_STRING_PROCESSING_MODULE_IMPORT)
740+
message(STATUS " Implicit 'string-processing' import: ${SWIFT_SUPPORTS_DISABLE_IMPLICIT_STRING_PROCESSING_MODULE_IMPORT}")
741+
742+
# Same for _Backtracing.
743+
swift_supports_implicit_module("backtracing"
744+
SWIFT_SUPPORTS_DISABLE_IMPLICIT_BACKTRACING_MODULE_IMPORT)
745+
message(STATUS " Implicit 'backtracing' import: ${SWIFT_SUPPORTS_DISABLE_IMPLICIT_BACKTRACING_MODULE_IMPORT}")
735746
else()
736747
message(STATUS "Swift Compiler (None).")
737748
endif()
@@ -777,6 +788,7 @@ include(AddSwift)
777788
include(SwiftConfigureSDK)
778789
include(SwiftComponents)
779790
include(SwiftList)
791+
include(AddPureSwift)
780792

781793
# Configure swift include, install, build components.
782794
swift_configure_components()

cmake/modules/AddPureSwift.cmake

Lines changed: 286 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,286 @@
1+
include(macCatalystUtils)
2+
3+
# Workaround a cmake bug, see the corresponding function in swift-syntax
4+
function(force_target_link_libraries TARGET)
5+
cmake_parse_arguments(ARGS "" "" "PUBLIC" ${ARGN})
6+
7+
foreach(DEPENDENCY ${ARGS_PUBLIC})
8+
target_link_libraries(${TARGET} PRIVATE
9+
${DEPENDENCY}
10+
)
11+
add_dependencies(${TARGET} ${DEPENDENCY})
12+
13+
add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/forced-${DEPENDENCY}-dep.swift
14+
COMMAND ${CMAKE_COMMAND} -E touch ${CMAKE_CURRENT_BINARY_DIR}/forced-${DEPENDENCY}-dep.swift
15+
DEPENDS ${DEPENDENCY}
16+
)
17+
target_sources(${TARGET} PRIVATE
18+
${CMAKE_CURRENT_BINARY_DIR}/forced-${DEPENDENCY}-dep.swift
19+
)
20+
endforeach()
21+
endfunction()
22+
23+
# Add compile options shared between libraries and executables.
24+
function(_add_host_swift_compile_options name)
25+
# Avoid introducing an implicit dependency on the string-processing library.
26+
if(SWIFT_SUPPORTS_DISABLE_IMPLICIT_STRING_PROCESSING_MODULE_IMPORT)
27+
target_compile_options(${name} PRIVATE
28+
"$<$<COMPILE_LANGUAGE:Swift>:SHELL:-Xfrontend -disable-implicit-string-processing-module-import>")
29+
endif()
30+
31+
# Same for backtracing
32+
if (SWIFT_SUPPORTS_DISABLE_IMPLICIT_BACKTRACING_MODULE_IMPORT)
33+
target_compile_options(${name} PRIVATE
34+
"$<$<COMPILE_LANGUAGE:Swift>:SHELL:-Xfrontend -disable-implicit-backtracing-module-import>")
35+
endif()
36+
37+
# The compat56 library is not available in current toolchains. The stage-0
38+
# compiler will build fine since the builder compiler is not aware of the 56
39+
# compat library, but the stage-1 and subsequent stage compilers will fail as
40+
# the stage-0 compiler is aware and will attempt to include the appropriate
41+
# compatibility library. We should turn this back on once we are building the
42+
# compiler correctly.
43+
# Note: This is safe at the moment because the 5.6 compat library only
44+
# contains concurrency runtime fixes, and the compiler frontend does not
45+
# use concurrency at the moment.
46+
target_compile_options(${name} PRIVATE
47+
$<$<COMPILE_LANGUAGE:Swift>:-runtime-compatibility-version>
48+
$<$<COMPILE_LANGUAGE:Swift>:none>)
49+
50+
# Set the appropriate target triple.
51+
# FIXME: This should be set by CMake.
52+
if(SWIFT_HOST_VARIANT_SDK IN_LIST SWIFT_DARWIN_PLATFORMS)
53+
set(DEPLOYMENT_VERSION "${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_DEPLOYMENT_VERSION}")
54+
endif()
55+
56+
if(SWIFT_HOST_VARIANT_SDK STREQUAL ANDROID)
57+
set(DEPLOYMENT_VERSION ${SWIFT_ANDROID_API_LEVEL})
58+
endif()
59+
60+
get_target_triple(target target_variant "${SWIFT_HOST_VARIANT_SDK}" "${SWIFT_HOST_VARIANT_ARCH}"
61+
MACCATALYST_BUILD_FLAVOR ""
62+
DEPLOYMENT_VERSION "${DEPLOYMENT_VERSION}")
63+
64+
target_compile_options(${name} PRIVATE $<$<COMPILE_LANGUAGE:Swift>:-target;${target}>)
65+
endfunction()
66+
67+
# Add a new "pure" Swift host library.
68+
#
69+
# "Pure" Swift host libraries can only contain Swift code, and will be built
70+
# with the host compiler. They are always expected to be part of the built
71+
# compiler, without bootstrapping.
72+
#
73+
# All of these libraries depend on the swift-syntax stack, since they are
74+
# meant to be part of the compiler.
75+
#
76+
# Usage:
77+
# add_pure_swift_host_library(name
78+
# [SHARED]
79+
# [STATIC]
80+
# [LLVM_LINK_COMPONENTS comp1 ...]
81+
# source1 [source2 source3 ...])
82+
#
83+
# name
84+
# Name of the library (e.g., swiftParse).
85+
#
86+
# SHARED
87+
# Build a shared library.
88+
#
89+
# STATIC
90+
# Build a static library.
91+
#
92+
# EMIT_MODULE
93+
# Emit '.swiftmodule' to
94+
#
95+
# DEPENDENCIES
96+
# Target names to pass target_link_library
97+
#
98+
# SWIFT_DEPENDENCIES
99+
# Target names to pass force_target_link_library.
100+
# TODO: Remove this and use DEPENDENCIES when CMake is fixed
101+
#
102+
# source1 ...
103+
# Sources to add into this library.
104+
function(add_pure_swift_host_library name)
105+
if (NOT SWIFT_SWIFT_PARSER)
106+
message(STATUS "Not building ${name} because swift-syntax is not available")
107+
return()
108+
endif()
109+
110+
# Option handling
111+
set(options
112+
SHARED
113+
STATIC
114+
EMIT_MODULE)
115+
set(single_parameter_options)
116+
set(multiple_parameter_options
117+
DEPENDENCIES
118+
SWIFT_DEPENDENCIES)
119+
120+
cmake_parse_arguments(APSHL
121+
"${options}"
122+
"${single_parameter_options}"
123+
"${multiple_parameter_options}"
124+
${ARGN})
125+
set(APSHL_SOURCES ${APSHL_UNPARSED_ARGUMENTS})
126+
127+
translate_flags(APSHL "${options}")
128+
129+
# Determine what kind of library we're building.
130+
if(APSHL_SHARED)
131+
set(libkind SHARED)
132+
elseif(APSHL_STATIC)
133+
set(libkind STATIC)
134+
endif()
135+
136+
# Create the library.
137+
add_library(${name} ${libkind} ${APSHL_SOURCES})
138+
_add_host_swift_compile_options(${name})
139+
140+
# Respect LLVM_COMMON_DEPENDS if it is set.
141+
#
142+
# LLVM_COMMON_DEPENDS if a global variable set in ./lib that provides targets
143+
# such as swift-syntax or tblgen that all LLVM/Swift based tools depend on. If
144+
# we don't have it defined, then do not add the dependency since some parts of
145+
# swift host tools do not interact with LLVM/Swift tools and do not define
146+
# LLVM_COMMON_DEPENDS.
147+
if (LLVM_COMMON_DEPENDS)
148+
add_dependencies(${name} ${LLVM_COMMON_DEPENDS})
149+
endif()
150+
151+
# Workaround to touch the library and its objects so that we don't
152+
# continually rebuild (again, see corresponding change in swift-syntax).
153+
add_custom_command(
154+
TARGET ${name}
155+
POST_BUILD
156+
COMMAND "${CMAKE_COMMAND}" -E touch_nocreate $<TARGET_FILE:${name}> $<TARGET_OBJECTS:${name}>
157+
COMMAND_EXPAND_LISTS
158+
COMMENT "Update mtime of library outputs workaround")
159+
160+
# Link against dependencies.
161+
target_link_libraries(${name} PUBLIC
162+
${APSHL_DEPENDENCIES}
163+
)
164+
# TODO: Change to target_link_libraries when cmake is fixed
165+
force_target_link_libraries(${name} PUBLIC
166+
${APSHL_SWIFT_DEPENDENCIES}
167+
)
168+
169+
# Make sure we can use the host libraries.
170+
target_include_directories(${name} PUBLIC
171+
${SWIFT_HOST_LIBRARIES_DEST_DIR})
172+
173+
if(APSHL_EMIT_MODULE)
174+
# Determine where Swift modules will be built and installed.
175+
176+
set(module_triple ${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_ARCH_${SWIFT_HOST_VARIANT_ARCH}_MODULE})
177+
set(module_dir ${SWIFT_HOST_LIBRARIES_DEST_DIR})
178+
set(module_base "${module_dir}/${name}.swiftmodule")
179+
set(module_file "${module_base}/${module_triple}.swiftmodule")
180+
set(module_interface_file "${module_base}/${module_triple}.swiftinterface")
181+
set(module_sourceinfo_file "${module_base}/${module_triple}.swiftsourceinfo")
182+
183+
set_target_properties(${name} PROPERTIES
184+
# Set the default module name to the target name.
185+
Swift_MODULE_NAME ${name}
186+
# Install the Swift module into the appropriate location.
187+
Swift_MODULE_DIRECTORY ${module_dir}
188+
# NOTE: workaround for CMake not setting up include flags.
189+
INTERFACE_INCLUDE_DIRECTORIES ${module_dir})
190+
191+
# Create the module directory.
192+
add_custom_command(
193+
TARGET ${name}
194+
PRE_BUILD
195+
COMMAND "${CMAKE_COMMAND}" -E make_directory ${module_base}
196+
COMMENT "Generating module directory for ${name}")
197+
198+
# Configure the emission of the Swift module files.
199+
target_compile_options("${name}" PRIVATE
200+
$<$<COMPILE_LANGUAGE:Swift>:
201+
-module-name;$<TARGET_PROPERTY:${name},Swift_MODULE_NAME>;
202+
-enable-library-evolution;
203+
-emit-module-path;${module_file};
204+
-emit-module-source-info-path;${module_sourceinfo_file};
205+
-emit-module-interface-path;${module_interface_file}
206+
>)
207+
endif()
208+
209+
# Export this target.
210+
set_property(GLOBAL APPEND PROPERTY SWIFT_EXPORTS ${name})
211+
endfunction()
212+
213+
# Add a new "pure" Swift host tool.
214+
#
215+
# "Pure" Swift host tools can only contain Swift code, and will be built
216+
# with the host compiler.
217+
#
218+
# Usage:
219+
# add_pure_swift_host_tool(name
220+
# [DEPENDENCIES dep1 ...]
221+
# [SWIFT_DEPENDENCIES swiftdep1 ...]
222+
# source1 [source2 source3 ...])
223+
#
224+
# name
225+
# Name of the tool (e.g., swift-frontend).
226+
#
227+
# DEPENDENCIES
228+
# Target names to pass target_link_library
229+
#
230+
# SWIFT_DEPENDENCIES
231+
# Target names to pass force_target_link_library.
232+
# TODO: Remove this and use DEPENDENCIES when CMake is fixed
233+
#
234+
# source1 ...
235+
# Sources to add into this tool.
236+
function(add_pure_swift_host_tool name)
237+
if (NOT SWIFT_SWIFT_PARSER)
238+
message(STATUS "Not building ${name} because swift-syntax is not available")
239+
return()
240+
endif()
241+
242+
# Option handling
243+
set(options)
244+
set(single_parameter_options)
245+
set(multiple_parameter_options
246+
DEPENDENCIES
247+
SWIFT_DEPENDENCIES)
248+
249+
cmake_parse_arguments(APSHT
250+
"${options}"
251+
"${single_parameter_options}"
252+
"${multiple_parameter_options}"
253+
${ARGN})
254+
set(APSHT_SOURCES ${APSHT_UNPARSED_ARGUMENTS})
255+
256+
# Create the library.
257+
add_executable(${name} ${APSHT_SOURCES})
258+
_add_host_swift_compile_options(${name})
259+
260+
# Respect LLVM_COMMON_DEPENDS if it is set.
261+
#
262+
# LLVM_COMMON_DEPENDS if a global variable set in ./lib that provides targets
263+
# such as swift-syntax or tblgen that all LLVM/Swift based tools depend on. If
264+
# we don't have it defined, then do not add the dependency since some parts of
265+
# swift host tools do not interact with LLVM/Swift tools and do not define
266+
# LLVM_COMMON_DEPENDS.
267+
if (LLVM_COMMON_DEPENDS)
268+
add_dependencies(${name} ${LLVM_COMMON_DEPENDS})
269+
endif()
270+
271+
# Link against dependencies.
272+
target_link_libraries(${name} PUBLIC
273+
${APSHT_DEPENDENCIES}
274+
)
275+
# TODO: Change to target_link_libraries when cmake is fixed
276+
force_target_link_libraries(${name} PUBLIC
277+
${APSHT_SWIFT_DEPENDENCIES}
278+
)
279+
280+
# Make sure we can use the host libraries.
281+
target_include_directories(${name} PUBLIC
282+
${SWIFT_HOST_LIBRARIES_DEST_DIR})
283+
284+
# Export this target.
285+
set_property(GLOBAL APPEND PROPERTY SWIFT_EXPORTS ${name})
286+
endfunction()

lib/ASTGen/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,11 @@ add_pure_swift_host_library(swiftASTGen STATIC
2525
DEPENDENCIES
2626
swiftAST
2727
SWIFT_DEPENDENCIES
28+
SwiftSyntax::SwiftDiagnostics
29+
SwiftSyntax::SwiftOperators
30+
SwiftSyntax::SwiftParser
31+
SwiftSyntax::SwiftParserDiagnostics
32+
SwiftSyntax::SwiftSyntax
33+
SwiftSyntax::SwiftSyntaxMacros
2834
swiftLLVMJSON
2935
)

0 commit comments

Comments
 (0)