Skip to content

Commit 049f19e

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 f651afe commit 049f19e

30 files changed

+138
-319
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.
@@ -686,10 +687,9 @@ if(CMAKE_C_COMPILER_ID MATCHES Clang)
686687
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-Werror=c++98-compat-extra-semi>)
687688
endif()
688689

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

694694
# Use dispatch as the system scheduler by default.
695695
# For convenience, we set this to false when concurrency is disabled.
@@ -831,7 +831,7 @@ elseif(BOOTSTRAPPING_MODE MATCHES "BOOTSTRAPPING.*")
831831
else()
832832
set(BOOTSTRAPPING_MODE "HOSTTOOLS")
833833
endif()
834-
elseif(BOOTSTRAPPING_MODE STREQUAL "HOSTTOOLS" OR SWIFT_SWIFT_PARSER)
834+
elseif(BOOTSTRAPPING_MODE STREQUAL "HOSTTOOLS" OR SWIFT_BUILD_SWIFT_SYNTAX)
835835
# We are building using a pre-installed host toolchain but not bootstrapping
836836
# the Swift modules. This happens when building using 'build-tooling-libs'
837837
# where we haven't built a new Swift compiler. Use the Swift compiler from the
@@ -945,19 +945,6 @@ if(XCODE)
945945
set(SWIFT_SDKS "OSX")
946946
endif()
947947

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

1148+
set(SWIFT_HOST_MODULE_TRIPLE "${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_ARCH_${SWIFT_HOST_VARIANT_ARCH}_MODULE}")
1149+
set(SWIFT_HOST_LIBRARIES_DEST_DIR "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/swift/host")
1150+
if(SWIFT_BUILD_SWIFT_SYNTAX)
1151+
if(NOT EXISTS "${SWIFT_PATH_TO_SWIFT_SYNTAX_SOURCE}")
1152+
message(SEND_ERROR "swift-syntax is required to build the Swift compiler. Please run update-checkout or specify SWIFT_PATH_TO_SWIFT_SYNTAX_SOURCE")
1153+
endif()
1154+
1155+
set(BUILD_SHARED_LIBS_OLD "${BUILD_SHARED_LIBS}")
1156+
set(BUILD_SHARED_LIBS ON)
1157+
FetchContent_Declare(SwiftSyntax
1158+
SOURCE_DIR "${SWIFT_PATH_TO_SWIFT_SYNTAX_SOURCE}"
1159+
)
1160+
FetchContent_MakeAvailable(SwiftSyntax)
1161+
set(BUILD_SHARED_LIBS "${BUILD_SHARED_LIBS_OLD}")
1162+
endif()
1163+
11611164
if(SWIFT_INCLUDE_TOOLS)
11621165
message(STATUS "Building host Swift tools for ${SWIFT_HOST_VARIANT_SDK} ${SWIFT_HOST_VARIANT_ARCH}")
11631166
message(STATUS " Build type: ${CMAKE_BUILD_TYPE}")
11641167
message(STATUS " Assertions: ${LLVM_ENABLE_ASSERTIONS}")
11651168
message(STATUS " LTO: ${SWIFT_TOOLS_ENABLE_LTO}")
11661169
message(STATUS " Bootstrapping: ${BOOTSTRAPPING_MODE}")
1167-
message(STATUS " Swift parser: ${SWIFT_SWIFT_PARSER}")
1170+
message(STATUS " Swift parser: ${SWIFT_BUILD_SWIFT_SYNTAX}")
11681171
message(STATUS "")
11691172
else()
11701173
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()
@@ -172,13 +172,15 @@ function(add_pure_swift_host_library name)
172172

173173
# Make sure we can use the host libraries.
174174
target_include_directories(${name} PUBLIC
175-
${SWIFT_HOST_LIBRARIES_DEST_DIR})
175+
"${SWIFT_HOST_LIBRARIES_DEST_DIR}")
176+
target_link_directories(${name} PUBLIC
177+
"${SWIFT_HOST_LIBRARIES_DEST_DIR}")
176178

177179
if(APSHL_EMIT_MODULE)
178180
# Determine where Swift modules will be built and installed.
179181

180-
set(module_triple ${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_ARCH_${SWIFT_HOST_VARIANT_ARCH}_MODULE})
181-
set(module_dir ${SWIFT_HOST_LIBRARIES_DEST_DIR})
182+
set(module_triple "${SWIFT_HOST_MODULE_TRIPLE}")
183+
set(module_dir "${SWIFT_HOST_LIBRARIES_DEST_DIR}")
182184
set(module_base "${module_dir}/${name}.swiftmodule")
183185
set(module_file "${module_base}/${module_triple}.swiftmodule")
184186
set(module_interface_file "${module_base}/${module_triple}.swiftinterface")
@@ -210,14 +212,20 @@ function(add_pure_swift_host_library name)
210212
>)
211213
endif()
212214

215+
if(LLVM_USE_LINKER)
216+
target_link_options(${name} PRIVATE
217+
"-use-ld=${LLVM_USE_LINKER}"
218+
)
219+
endif()
220+
213221
# Export this target.
214222
set_property(GLOBAL APPEND PROPERTY SWIFT_EXPORTS ${name})
215223
endfunction()
216224

217225
# Add a new "pure" Swift host tool.
218226
#
219227
# "Pure" Swift host tools can only contain Swift code, and will be built
220-
# with the host compiler.
228+
# with the host compiler.
221229
#
222230
# Usage:
223231
# add_pure_swift_host_tool(name
@@ -238,7 +246,7 @@ endfunction()
238246
# source1 ...
239247
# Sources to add into this tool.
240248
function(add_pure_swift_host_tool name)
241-
if (NOT SWIFT_SWIFT_PARSER)
249+
if (NOT SWIFT_BUILD_SWIFT_SYNTAX)
242250
message(STATUS "Not building ${name} because swift-syntax is not available")
243251
return()
244252
endif()
@@ -290,7 +298,15 @@ function(add_pure_swift_host_tool name)
290298

291299
# Make sure we can use the host libraries.
292300
target_include_directories(${name} PUBLIC
293-
${SWIFT_HOST_LIBRARIES_DEST_DIR})
301+
"${SWIFT_HOST_LIBRARIES_DEST_DIR}")
302+
target_link_directories(${name} PUBLIC
303+
"${SWIFT_HOST_LIBRARIES_DEST_DIR}")
304+
305+
if(LLVM_USE_LINKER)
306+
target_link_options(${name} PRIVATE
307+
"-use-ld=${LLVM_USE_LINKER}"
308+
)
309+
endif()
294310

295311
# Export this target.
296312
set_property(GLOBAL APPEND PROPERTY SWIFT_EXPORTS ${name})

cmake/modules/AddSwift.cmake

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ endfunction()
439439
440440
function(_add_swift_runtime_link_flags target relpath_to_lib_dir bootstrapping)
441441
if(NOT BOOTSTRAPPING_MODE)
442-
if (SWIFT_SWIFT_PARSER)
442+
if (SWIFT_BUILD_SWIFT_SYNTAX)
443443
set(ASRLF_BOOTSTRAPPING_MODE "HOSTTOOLS")
444444
else()
445445
return()
@@ -572,10 +572,7 @@ function(_add_swift_runtime_link_flags target relpath_to_lib_dir bootstrapping)
572572
endif()
573573
endif()
574574
575-
if(SWIFT_SWIFT_PARSER)
576-
# Make sure we can find the early SwiftSyntax libraries.
577-
target_link_directories(${target} PRIVATE "${SWIFT_PATH_TO_EARLYSWIFTSYNTAX_BUILD_DIR}/lib/swift/host")
578-
575+
if(SWIFT_BUILD_SWIFT_SYNTAX)
579576
# For the "end step" of bootstrapping configurations on Darwin, need to be
580577
# able to fall back to the SDK directory for libswiftCore et al.
581578
if (BOOTSTRAPPING_MODE MATCHES "BOOTSTRAPPING.*")
@@ -646,7 +643,7 @@ function(add_swift_host_library name)
646643
translate_flags(ASHL "${options}")
647644
648645
# Once the new Swift parser is linked, everything has Swift modules.
649-
if (SWIFT_SWIFT_PARSER AND ASHL_SHARED)
646+
if (SWIFT_BUILD_SWIFT_SYNTAX AND ASHL_SHARED)
650647
set(ASHL_HAS_SWIFT_MODULES ON)
651648
endif()
652649
@@ -692,7 +689,7 @@ function(add_swift_host_library name)
692689
693690
add_library(${name} ${libkind} ${ASHL_SOURCES})
694691
695-
target_link_directories(${name} PUBLIC ${CMAKE_LIBRARY_OUTPUT_DIRECTORY})
692+
target_link_directories(${name} PUBLIC "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}")
696693
697694
# Respect LLVM_COMMON_DEPENDS if it is set.
698695
#
@@ -879,7 +876,7 @@ function(add_swift_host_tool executable)
879876
endif()
880877
881878
# Once the new Swift parser is linked in, every host tool has Swift modules.
882-
if (SWIFT_SWIFT_PARSER)
879+
if (SWIFT_BUILD_SWIFT_SYNTAX)
883880
set(ASHT_HAS_SWIFT_MODULES ON)
884881
endif()
885882
@@ -918,7 +915,7 @@ function(add_swift_host_tool executable)
918915
endif()
919916
endif()
920917
921-
if(SWIFT_SWIFT_PARSER)
918+
if(SWIFT_BUILD_SWIFT_SYNTAX)
922919
set(extra_relative_rpath "")
923920
if(NOT ${ASHT_BOOTSTRAPPING} STREQUAL "")
924921
if (${executable} MATCHES "-bootstrapping")

cmake/modules/AddSwiftUnittests.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ function(add_swift_unittest test_dirname)
8181
endif()
8282
endif()
8383

84-
if (SWIFT_SWIFT_PARSER)
84+
if (SWIFT_BUILD_SWIFT_SYNTAX)
8585
_add_swift_runtime_link_flags(${test_dirname} "../../lib" "")
8686
set_property(TARGET ${test_dirname} PROPERTY BUILD_WITH_INSTALL_RPATH OFF)
8787
endif()

lib/AST/CMakeLists.txt

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

156-
if(SWIFT_SWIFT_PARSER)
156+
if(SWIFT_BUILD_SWIFT_SYNTAX)
157157
target_compile_definitions(swiftAST
158158
PRIVATE
159-
SWIFT_SWIFT_PARSER
159+
SWIFT_BUILD_SWIFT_SYNTAX
160160
)
161161
endif()
162162

lib/ASTGen/CMakeLists.txt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ add_pure_swift_host_library(swiftASTGen STATIC
2525
DEPENDENCIES
2626
swiftAST
2727
SWIFT_DEPENDENCIES
28-
SwiftSyntax::SwiftBasicFormat
29-
SwiftSyntax::SwiftDiagnostics
30-
SwiftSyntax::SwiftOperators
31-
SwiftSyntax::SwiftParser
32-
SwiftSyntax::SwiftParserDiagnostics
33-
SwiftSyntax::SwiftSyntax
34-
SwiftSyntax::SwiftSyntaxMacros
35-
SwiftSyntax::SwiftSyntaxMacroExpansion
28+
SwiftBasicFormat
29+
SwiftDiagnostics
30+
SwiftOperators
31+
SwiftParser
32+
SwiftParserDiagnostics
33+
SwiftSyntax
34+
SwiftSyntaxMacros
35+
SwiftSyntaxMacroExpansion
3636
swiftLLVMJSON
3737
)

lib/CMakeLists.txt

Lines changed: 0 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -15,99 +15,6 @@ list(APPEND LLVM_COMMON_DEPENDS intrinsics_gen clang-tablegen-targets)
1515
# Add generated Swift Syntax headers to global dependencies.
1616
list(APPEND LLVM_COMMON_DEPENDS swift-ast-generated-headers)
1717

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