Skip to content

Commit a3dbb20

Browse files
committed
[CMake] Replace early swift-syntax with FetchContent
Use FetchContent to include swift-syntax directly in swift. This can be thought of as an `add_subdirectory` for a directory outside the root. The default build directory will be `_deps/swiftsyntax-subbuild/`, though the modules and shared libraries will be built in `lib/swift/host` by passing down `SWIFT_HOST_LIBRARIES_DEST_DIR` to avoid copying them as we were doing previously.
1 parent 4581eb7 commit a3dbb20

31 files changed

+140
-321
lines changed

CMakeLists.txt

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ include(CMakeDependentOption)
9494
include(CheckLanguage)
9595
include(GNUInstallDirs)
9696
include(SwiftImplicitImport)
97+
include(FetchContent)
9798

9899
# Enable Swift for the host compiler build if we have the language. It is
99100
# optional until we have a bootstrap story.
@@ -691,10 +692,9 @@ if(CMAKE_C_COMPILER_ID MATCHES Clang)
691692
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-Werror=c++98-compat-extra-semi>)
692693
endif()
693694

694-
# Make sure we know where swift-syntax is because we need it to build the parser.
695-
if(NOT EXISTS "${SWIFT_PATH_TO_SWIFT_SYNTAX_SOURCE}")
696-
message(SEND_ERROR "swift-syntax is required to build the Swift compiler. Please run update-checkout or specify SWIFT_PATH_TO_SWIFT_SYNTAX_SOURCE")
697-
endif()
695+
option(SWIFT_BUILD_SWIFT_SYNTAX
696+
"Enable building swift syntax"
697+
FALSE)
698698

699699
set(SWIFT_BUILD_HOST_DISPATCH FALSE)
700700
if(SWIFT_ENABLE_DISPATCH AND NOT CMAKE_SYSTEM_NAME STREQUAL "Darwin")
@@ -829,7 +829,7 @@ elseif(BOOTSTRAPPING_MODE MATCHES "BOOTSTRAPPING.*")
829829
else()
830830
set(BOOTSTRAPPING_MODE "HOSTTOOLS")
831831
endif()
832-
elseif(BOOTSTRAPPING_MODE STREQUAL "HOSTTOOLS" OR SWIFT_SWIFT_PARSER)
832+
elseif(BOOTSTRAPPING_MODE STREQUAL "HOSTTOOLS" OR SWIFT_BUILD_SWIFT_SYNTAX)
833833
# We are building using a pre-installed host toolchain but not bootstrapping
834834
# the Swift modules. This happens when building using 'build-tooling-libs'
835835
# where we haven't built a new Swift compiler. Use the Swift compiler from the
@@ -943,19 +943,6 @@ if(XCODE)
943943
set(SWIFT_SDKS "OSX")
944944
endif()
945945

946-
# When we have the early SwiftSyntax build, we can include its parser.
947-
if(SWIFT_PATH_TO_EARLYSWIFTSYNTAX_BUILD_DIR)
948-
set(SWIFT_PATH_TO_EARLYSWIFTSYNTAX_TARGETS
949-
${SWIFT_PATH_TO_EARLYSWIFTSYNTAX_BUILD_DIR}/cmake/SwiftSyntaxTargets.cmake)
950-
if(NOT EXISTS "${SWIFT_PATH_TO_EARLYSWIFTSYNTAX_TARGETS}")
951-
message(STATUS "Skipping Swift Swift parser integration due to missing early SwiftSyntax")
952-
else()
953-
set(SWIFT_SWIFT_PARSER TRUE)
954-
include(${SWIFT_PATH_TO_EARLYSWIFTSYNTAX_TARGETS})
955-
endif()
956-
endif()
957-
958-
959946
# FIXME: the parameters we specify in SWIFT_SDKS are lacking architecture specifics,
960947
# so we need to hard-code it. For example, the SDK for Android is just 'ANDROID',
961948
# and we have to specify SWIFT_SDK_ANDROID_ARCHITECTURES separately.
@@ -1164,13 +1151,29 @@ if("${CMAKE_SYSTEM_NAME}" STREQUAL "Darwin" AND NOT CMAKE_CROSSCOMPILING)
11641151
set(CMAKE_OSX_DEPLOYMENT_TARGET "")
11651152
endif()
11661153

1154+
set(SWIFT_HOST_MODULE_TRIPLE "${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_ARCH_${SWIFT_HOST_VARIANT_ARCH}_MODULE}")
1155+
set(SWIFT_HOST_LIBRARIES_DEST_DIR "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/swift/host")
1156+
if(SWIFT_BUILD_SWIFT_SYNTAX)
1157+
if(NOT EXISTS "${SWIFT_PATH_TO_SWIFT_SYNTAX_SOURCE}")
1158+
message(SEND_ERROR "swift-syntax is required to build the Swift compiler. Please run update-checkout or specify SWIFT_PATH_TO_SWIFT_SYNTAX_SOURCE")
1159+
endif()
1160+
1161+
set(BUILD_SHARED_LIBS_OLD "${BUILD_SHARED_LIBS}")
1162+
set(BUILD_SHARED_LIBS ON)
1163+
FetchContent_Declare(SwiftSyntax
1164+
SOURCE_DIR "${SWIFT_PATH_TO_SWIFT_SYNTAX_SOURCE}"
1165+
)
1166+
FetchContent_MakeAvailable(SwiftSyntax)
1167+
set(BUILD_SHARED_LIBS "${BUILD_SHARED_LIBS_OLD}")
1168+
endif()
1169+
11671170
if(SWIFT_INCLUDE_TOOLS)
11681171
message(STATUS "Building host Swift tools for ${SWIFT_HOST_VARIANT_SDK} ${SWIFT_HOST_VARIANT_ARCH}")
11691172
message(STATUS " Build type: ${CMAKE_BUILD_TYPE}")
11701173
message(STATUS " Assertions: ${LLVM_ENABLE_ASSERTIONS}")
11711174
message(STATUS " LTO: ${SWIFT_TOOLS_ENABLE_LTO}")
11721175
message(STATUS " Bootstrapping: ${BOOTSTRAPPING_MODE}")
1173-
message(STATUS " Swift parser: ${SWIFT_SWIFT_PARSER}")
1176+
message(STATUS " Swift parser: ${SWIFT_BUILD_SWIFT_SYNTAX}")
11741177
message(STATUS "")
11751178
else()
11761179
message(STATUS "Not building host Swift tools")

cmake/modules/AddPureSwift.cmake

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ endfunction()
103103
# source1 ...
104104
# Sources to add into this library.
105105
function(add_pure_swift_host_library name)
106-
if (NOT SWIFT_SWIFT_PARSER)
106+
if (NOT SWIFT_BUILD_SWIFT_SYNTAX)
107107
message(STATUS "Not building ${name} because swift-syntax is not available")
108108
return()
109109
endif()
@@ -178,13 +178,15 @@ function(add_pure_swift_host_library name)
178178

179179
# Make sure we can use the host libraries.
180180
target_include_directories(${name} PUBLIC
181-
${SWIFT_HOST_LIBRARIES_DEST_DIR})
181+
"${SWIFT_HOST_LIBRARIES_DEST_DIR}")
182+
target_link_directories(${name} PUBLIC
183+
"${SWIFT_HOST_LIBRARIES_DEST_DIR}")
182184

183185
if(APSHL_EMIT_MODULE)
184186
# Determine where Swift modules will be built and installed.
185187

186-
set(module_triple ${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_ARCH_${SWIFT_HOST_VARIANT_ARCH}_MODULE})
187-
set(module_dir ${SWIFT_HOST_LIBRARIES_DEST_DIR})
188+
set(module_triple "${SWIFT_HOST_MODULE_TRIPLE}")
189+
set(module_dir "${SWIFT_HOST_LIBRARIES_DEST_DIR}")
188190
set(module_base "${module_dir}/${name}.swiftmodule")
189191
set(module_file "${module_base}/${module_triple}.swiftmodule")
190192
set(module_interface_file "${module_base}/${module_triple}.swiftinterface")
@@ -216,14 +218,20 @@ function(add_pure_swift_host_library name)
216218
>)
217219
endif()
218220

221+
if(LLVM_USE_LINKER)
222+
target_link_options(${name} PRIVATE
223+
"-use-ld=${LLVM_USE_LINKER}"
224+
)
225+
endif()
226+
219227
# Export this target.
220228
set_property(GLOBAL APPEND PROPERTY SWIFT_EXPORTS ${name})
221229
endfunction()
222230

223231
# Add a new "pure" Swift host tool.
224232
#
225233
# "Pure" Swift host tools can only contain Swift code, and will be built
226-
# with the host compiler.
234+
# with the host compiler.
227235
#
228236
# Usage:
229237
# add_pure_swift_host_tool(name
@@ -244,7 +252,7 @@ endfunction()
244252
# source1 ...
245253
# Sources to add into this tool.
246254
function(add_pure_swift_host_tool name)
247-
if (NOT SWIFT_SWIFT_PARSER)
255+
if (NOT SWIFT_BUILD_SWIFT_SYNTAX)
248256
message(STATUS "Not building ${name} because swift-syntax is not available")
249257
return()
250258
endif()
@@ -302,7 +310,15 @@ function(add_pure_swift_host_tool name)
302310

303311
# Make sure we can use the host libraries.
304312
target_include_directories(${name} PUBLIC
305-
${SWIFT_HOST_LIBRARIES_DEST_DIR})
313+
"${SWIFT_HOST_LIBRARIES_DEST_DIR}")
314+
target_link_directories(${name} PUBLIC
315+
"${SWIFT_HOST_LIBRARIES_DEST_DIR}")
316+
317+
if(LLVM_USE_LINKER)
318+
target_link_options(${name} PRIVATE
319+
"-use-ld=${LLVM_USE_LINKER}"
320+
)
321+
endif()
306322

307323
# Workaround to touch the library and its objects so that we don't
308324
# continually rebuild (again, see corresponding change in swift-syntax).

cmake/modules/AddSwift.cmake

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ endfunction()
442442
443443
function(_add_swift_runtime_link_flags target relpath_to_lib_dir bootstrapping)
444444
if(NOT BOOTSTRAPPING_MODE)
445-
if (SWIFT_SWIFT_PARSER)
445+
if (SWIFT_BUILD_SWIFT_SYNTAX)
446446
set(ASRLF_BOOTSTRAPPING_MODE "HOSTTOOLS")
447447
else()
448448
return()
@@ -575,10 +575,7 @@ function(_add_swift_runtime_link_flags target relpath_to_lib_dir bootstrapping)
575575
endif()
576576
endif()
577577
578-
if(SWIFT_SWIFT_PARSER)
579-
# Make sure we can find the early SwiftSyntax libraries.
580-
target_link_directories(${target} PRIVATE "${SWIFT_PATH_TO_EARLYSWIFTSYNTAX_BUILD_DIR}/lib/swift/host")
581-
578+
if(SWIFT_BUILD_SWIFT_SYNTAX)
582579
# For the "end step" of bootstrapping configurations, we need to be
583580
# able to fall back to the SDK directory for libswiftCore et al.
584581
if (BOOTSTRAPPING_MODE MATCHES "BOOTSTRAPPING.*")
@@ -656,7 +653,7 @@ function(add_swift_host_library name)
656653
translate_flags(ASHL "${options}")
657654
658655
# Once the new Swift parser is linked, everything has Swift modules.
659-
if (SWIFT_SWIFT_PARSER AND ASHL_SHARED)
656+
if (SWIFT_BUILD_SWIFT_SYNTAX AND ASHL_SHARED)
660657
set(ASHL_HAS_SWIFT_MODULES ON)
661658
endif()
662659
@@ -702,7 +699,7 @@ function(add_swift_host_library name)
702699
703700
add_library(${name} ${libkind} ${ASHL_SOURCES})
704701
705-
target_link_directories(${name} PUBLIC ${CMAKE_LIBRARY_OUTPUT_DIRECTORY})
702+
target_link_directories(${name} PUBLIC "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}")
706703
707704
# Respect LLVM_COMMON_DEPENDS if it is set.
708705
#
@@ -889,7 +886,7 @@ function(add_swift_host_tool executable)
889886
endif()
890887
891888
# Once the new Swift parser is linked in, every host tool has Swift modules.
892-
if (SWIFT_SWIFT_PARSER)
889+
if (SWIFT_BUILD_SWIFT_SYNTAX)
893890
set(ASHT_HAS_SWIFT_MODULES ON)
894891
endif()
895892
@@ -928,7 +925,7 @@ function(add_swift_host_tool executable)
928925
endif()
929926
endif()
930927
931-
if(SWIFT_SWIFT_PARSER)
928+
if(SWIFT_BUILD_SWIFT_SYNTAX)
932929
set(extra_relative_rpath "")
933930
if(NOT "${ASHT_BOOTSTRAPPING}" STREQUAL "")
934931
if(executable MATCHES "-bootstrapping")

cmake/modules/AddSwiftUnittests.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ function(add_swift_unittest test_dirname)
8989
endif()
9090
endif()
9191

92-
if (SWIFT_SWIFT_PARSER)
92+
if (SWIFT_BUILD_SWIFT_SYNTAX)
9393
_add_swift_runtime_link_flags(${test_dirname} "../../lib" "")
9494
set_property(TARGET ${test_dirname} PROPERTY BUILD_WITH_INSTALL_RPATH OFF)
9595
endif()

lib/AST/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,10 +155,10 @@ target_link_libraries(swiftAST INTERFACE
155155
clangAPINotes
156156
clangBasic)
157157

158-
if(SWIFT_SWIFT_PARSER)
158+
if(SWIFT_BUILD_SWIFT_SYNTAX)
159159
target_compile_definitions(swiftAST
160160
PRIVATE
161-
SWIFT_SWIFT_PARSER
161+
SWIFT_BUILD_SWIFT_SYNTAX
162162
)
163163
endif()
164164

lib/ASTGen/CMakeLists.txt

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ add_pure_swift_host_library(swiftASTGen STATIC
2424
DEPENDENCIES
2525
swiftAST
2626
SWIFT_DEPENDENCIES
27-
SwiftSyntax::SwiftBasicFormat
28-
SwiftSyntax::SwiftCompilerPluginMessageHandling
29-
SwiftSyntax::SwiftDiagnostics
30-
SwiftSyntax::SwiftOperators
31-
SwiftSyntax::SwiftParser
32-
SwiftSyntax::SwiftParserDiagnostics
33-
SwiftSyntax::SwiftSyntax
34-
SwiftSyntax::SwiftSyntaxBuilder
35-
SwiftSyntax::SwiftSyntaxMacros
36-
SwiftSyntax::SwiftSyntaxMacroExpansion
27+
SwiftBasicFormat
28+
SwiftCompilerPluginMessageHandling
29+
SwiftDiagnostics
30+
SwiftOperators
31+
SwiftParser
32+
SwiftParserDiagnostics
33+
SwiftSyntax
34+
SwiftSyntaxBuilder
35+
SwiftSyntaxMacros
36+
SwiftSyntaxMacroExpansion
3737
swiftLLVMJSON
3838
)

lib/CMakeLists.txt

Lines changed: 0 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -12,99 +12,6 @@
1212
# directory.
1313
list(APPEND LLVM_COMMON_DEPENDS intrinsics_gen clang-tablegen-targets)
1414

15-
# Set up for linking against swift-syntax.
16-
if (SWIFT_SWIFT_PARSER)
17-
# Set up linking against the swift-syntax modules.
18-
# Link against the swift-syntax modules.
19-
set(SWIFT_SYNTAX_MODULES
20-
SwiftBasicFormat
21-
SwiftParser
22-
SwiftParserDiagnostics
23-
SwiftDiagnostics
24-
SwiftSyntax
25-
SwiftOperators
26-
SwiftSyntaxBuilder
27-
SwiftSyntaxMacros
28-
SwiftSyntaxMacroExpansion
29-
SwiftCompilerPluginMessageHandling
30-
)
31-
32-
# Compute the list of SwiftSyntax targets that we will link against.
33-
list(TRANSFORM SWIFT_SYNTAX_MODULES PREPEND "SwiftSyntax::"
34-
OUTPUT_VARIABLE SWIFT_SYNTAX_TARGETS)
35-
36-
set(SWIFT_SYNTAX_LIBRARIES_SOURCE_DIR
37-
"${SWIFT_PATH_TO_EARLYSWIFTSYNTAX_BUILD_DIR}/lib/swift/host")
38-
set(SWIFT_HOST_LIBRARIES_DEST_DIR
39-
"${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/swift/host")
40-
41-
# Determine the SwiftSyntax shared library files that were built as
42-
# part of earlyswiftsyntax.
43-
list(TRANSFORM SWIFT_SYNTAX_MODULES PREPEND ${CMAKE_SHARED_LIBRARY_PREFIX}
44-
OUTPUT_VARIABLE SWIFT_SYNTAX_SHARED_LIBRARIES)
45-
list(TRANSFORM SWIFT_SYNTAX_SHARED_LIBRARIES APPEND
46-
${CMAKE_SHARED_LIBRARY_SUFFIX}
47-
OUTPUT_VARIABLE SWIFT_SYNTAX_SHARED_LIBRARIES)
48-
49-
# Interface library to collect swiftinterfaces and swiftmodules from
50-
# SwiftSyntax
51-
add_library(swiftSyntaxLibraries INTERFACE)
52-
53-
# Copy over all of the shared libraries from earlyswiftsyntax so they can
54-
# be found via RPATH.
55-
foreach (sharedlib ${SWIFT_SYNTAX_SHARED_LIBRARIES})
56-
add_custom_command(
57-
OUTPUT "${SWIFT_HOST_LIBRARIES_DEST_DIR}/${sharedlib}"
58-
DEPENDS "${SWIFT_SYNTAX_LIBRARIES_SOURCE_DIR}/${sharedlib}"
59-
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${SWIFT_SYNTAX_LIBRARIES_SOURCE_DIR}/${sharedlib} ${SWIFT_HOST_LIBRARIES_DEST_DIR}/${sharedlib}
60-
)
61-
62-
add_custom_target(copy_swiftSyntaxLibrary_${sharedlib}
63-
DEPENDS "${SWIFT_HOST_LIBRARIES_DEST_DIR}/${sharedlib}"
64-
COMMENT "Copying ${sharedlib}"
65-
)
66-
67-
add_dependencies(swiftSyntaxLibraries copy_swiftSyntaxLibrary_${sharedlib})
68-
endforeach()
69-
70-
# Copy all of the Swift modules from earlyswiftsyntax so they can be found
71-
# in the same relative place within the build directory as in the final
72-
# toolchain.
73-
list(TRANSFORM SWIFT_SYNTAX_MODULES APPEND ".swiftmodule"
74-
OUTPUT_VARIABLE SWIFT_SYNTAX_MODULE_DIRS)
75-
76-
foreach(module_dir ${SWIFT_SYNTAX_MODULE_DIRS})
77-
# Find all of the source module files.
78-
file(GLOB module_files
79-
"${SWIFT_SYNTAX_LIBRARIES_SOURCE_DIR}/${module_dir}/*.swiftinterface")
80-
81-
# Determine the destination module files.
82-
set(dest_module_files)
83-
foreach(full_module_file ${module_files})
84-
get_filename_component(module_file ${full_module_file} NAME)
85-
list(APPEND dest_module_files
86-
"${SWIFT_HOST_LIBRARIES_DEST_DIR}/${module_dir}/${module_file}")
87-
endforeach()
88-
89-
add_custom_command(
90-
OUTPUT ${dest_module_files}
91-
DEPENDS ${module_files}
92-
COMMAND ${CMAKE_COMMAND} -E make_directory ${SWIFT_HOST_LIBRARIES_DEST_DIR}/${module_dir}
93-
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${module_files} ${SWIFT_HOST_LIBRARIES_DEST_DIR}/${module_dir}/
94-
)
95-
96-
add_custom_target(copy_swiftSyntaxModule_${module_dir}
97-
DEPENDS ${dest_module_files}
98-
COMMENT "Copying ${module_dir}"
99-
)
100-
101-
add_dependencies(swiftSyntaxLibraries copy_swiftSyntaxModule_${module_dir})
102-
endforeach()
103-
104-
# Add copied SwiftSyntax libraries to global dependencies.
105-
list(APPEND LLVM_COMMON_DEPENDS swiftSyntaxLibraries)
106-
endif()
107-
10815
add_subdirectory(APIDigester)
10916
add_subdirectory(AST)
11017
add_subdirectory(ASTGen)

lib/Frontend/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ target_link_libraries(swiftFrontend PRIVATE
3737

3838
set_swift_llvm_is_available(swiftFrontend)
3939

40-
if (SWIFT_SWIFT_PARSER)
40+
if (SWIFT_BUILD_SWIFT_SYNTAX)
4141
target_compile_definitions(swiftFrontend
4242
PRIVATE
43-
SWIFT_SWIFT_PARSER
43+
SWIFT_BUILD_SWIFT_SYNTAX
4444
)
4545
endif()

0 commit comments

Comments
 (0)