Skip to content

Commit 92ae57f

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 814ca43 commit 92ae57f

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.
@@ -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
set(SWIFT_BUILD_HOST_DISPATCH FALSE)
695695
if(SWIFT_ENABLE_DISPATCH AND NOT CMAKE_SYSTEM_NAME STREQUAL Darwin)
@@ -824,7 +824,7 @@ elseif(BOOTSTRAPPING_MODE MATCHES "BOOTSTRAPPING.*")
824824
else()
825825
set(BOOTSTRAPPING_MODE "HOSTTOOLS")
826826
endif()
827-
elseif(BOOTSTRAPPING_MODE STREQUAL "HOSTTOOLS" OR SWIFT_SWIFT_PARSER)
827+
elseif(BOOTSTRAPPING_MODE STREQUAL "HOSTTOOLS" OR SWIFT_BUILD_SWIFT_SYNTAX)
828828
# We are building using a pre-installed host toolchain but not bootstrapping
829829
# the Swift modules. This happens when building using 'build-tooling-libs'
830830
# where we haven't built a new Swift compiler. Use the Swift compiler from the
@@ -938,19 +938,6 @@ if(XCODE)
938938
set(SWIFT_SDKS "OSX")
939939
endif()
940940

941-
# When we have the early SwiftSyntax build, we can include its parser.
942-
if(SWIFT_PATH_TO_EARLYSWIFTSYNTAX_BUILD_DIR)
943-
set(SWIFT_PATH_TO_EARLYSWIFTSYNTAX_TARGETS
944-
${SWIFT_PATH_TO_EARLYSWIFTSYNTAX_BUILD_DIR}/cmake/SwiftSyntaxTargets.cmake)
945-
if(NOT EXISTS "${SWIFT_PATH_TO_EARLYSWIFTSYNTAX_TARGETS}")
946-
message(STATUS "Skipping Swift Swift parser integration due to missing early SwiftSyntax")
947-
else()
948-
set(SWIFT_SWIFT_PARSER TRUE)
949-
include(${SWIFT_PATH_TO_EARLYSWIFTSYNTAX_TARGETS})
950-
endif()
951-
endif()
952-
953-
954941
# FIXME: the parameters we specify in SWIFT_SDKS are lacking architecture specifics,
955942
# so we need to hard-code it. For example, the SDK for Android is just 'ANDROID',
956943
# and we have to specify SWIFT_SDK_ANDROID_ARCHITECTURES separately.
@@ -1151,13 +1138,29 @@ if("${CMAKE_SYSTEM_NAME}" STREQUAL "Darwin" AND NOT CMAKE_CROSSCOMPILING)
11511138
set(CMAKE_OSX_DEPLOYMENT_TARGET "")
11521139
endif()
11531140

1141+
set(SWIFT_HOST_MODULE_TRIPLE "${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_ARCH_${SWIFT_HOST_VARIANT_ARCH}_MODULE}")
1142+
set(SWIFT_HOST_LIBRARIES_DEST_DIR "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/swift/host")
1143+
if(SWIFT_BUILD_SWIFT_SYNTAX)
1144+
if(NOT EXISTS "${SWIFT_PATH_TO_SWIFT_SYNTAX_SOURCE}")
1145+
message(SEND_ERROR "swift-syntax is required to build the Swift compiler. Please run update-checkout or specify SWIFT_PATH_TO_SWIFT_SYNTAX_SOURCE")
1146+
endif()
1147+
1148+
set(BUILD_SHARED_LIBS_OLD "${BUILD_SHARED_LIBS}")
1149+
set(BUILD_SHARED_LIBS ON)
1150+
FetchContent_Declare(SwiftSyntax
1151+
SOURCE_DIR "${SWIFT_PATH_TO_SWIFT_SYNTAX_SOURCE}"
1152+
)
1153+
FetchContent_MakeAvailable(SwiftSyntax)
1154+
set(BUILD_SHARED_LIBS "${BUILD_SHARED_LIBS_OLD}")
1155+
endif()
1156+
11541157
if(SWIFT_INCLUDE_TOOLS)
11551158
message(STATUS "Building host Swift tools for ${SWIFT_HOST_VARIANT_SDK} ${SWIFT_HOST_VARIANT_ARCH}")
11561159
message(STATUS " Build type: ${CMAKE_BUILD_TYPE}")
11571160
message(STATUS " Assertions: ${LLVM_ENABLE_ASSERTIONS}")
11581161
message(STATUS " LTO: ${SWIFT_TOOLS_ENABLE_LTO}")
11591162
message(STATUS " Bootstrapping: ${BOOTSTRAPPING_MODE}")
1160-
message(STATUS " Swift parser: ${SWIFT_SWIFT_PARSER}")
1163+
message(STATUS " Swift parser: ${SWIFT_BUILD_SWIFT_SYNTAX}")
11611164
message(STATUS "")
11621165
else()
11631166
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()
@@ -296,7 +304,15 @@ function(add_pure_swift_host_tool name)
296304

297305
# Make sure we can use the host libraries.
298306
target_include_directories(${name} PUBLIC
299-
${SWIFT_HOST_LIBRARIES_DEST_DIR})
307+
"${SWIFT_HOST_LIBRARIES_DEST_DIR}")
308+
target_link_directories(${name} PUBLIC
309+
"${SWIFT_HOST_LIBRARIES_DEST_DIR}")
310+
311+
if(LLVM_USE_LINKER)
312+
target_link_options(${name} PRIVATE
313+
"-use-ld=${LLVM_USE_LINKER}"
314+
)
315+
endif()
300316

301317
# Export this target.
302318
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, we need to be
580577
# able to fall back to the SDK directory for libswiftCore et al.
581578
if (BOOTSTRAPPING_MODE MATCHES "BOOTSTRAPPING.*")
@@ -653,7 +650,7 @@ function(add_swift_host_library name)
653650
translate_flags(ASHL "${options}")
654651
655652
# Once the new Swift parser is linked, everything has Swift modules.
656-
if (SWIFT_SWIFT_PARSER AND ASHL_SHARED)
653+
if (SWIFT_BUILD_SWIFT_SYNTAX AND ASHL_SHARED)
657654
set(ASHL_HAS_SWIFT_MODULES ON)
658655
endif()
659656
@@ -699,7 +696,7 @@ function(add_swift_host_library name)
699696
700697
add_library(${name} ${libkind} ${ASHL_SOURCES})
701698
702-
target_link_directories(${name} PUBLIC ${CMAKE_LIBRARY_OUTPUT_DIRECTORY})
699+
target_link_directories(${name} PUBLIC "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}")
703700
704701
# Respect LLVM_COMMON_DEPENDS if it is set.
705702
#
@@ -886,7 +883,7 @@ function(add_swift_host_tool executable)
886883
endif()
887884
888885
# Once the new Swift parser is linked in, every host tool has Swift modules.
889-
if (SWIFT_SWIFT_PARSER)
886+
if (SWIFT_BUILD_SWIFT_SYNTAX)
890887
set(ASHT_HAS_SWIFT_MODULES ON)
891888
endif()
892889
@@ -925,7 +922,7 @@ function(add_swift_host_tool executable)
925922
endif()
926923
endif()
927924
928-
if(SWIFT_SWIFT_PARSER)
925+
if(SWIFT_BUILD_SWIFT_SYNTAX)
929926
set(extra_relative_rpath "")
930927
if(NOT ${ASHT_BOOTSTRAPPING} STREQUAL "")
931928
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
@@ -154,10 +154,10 @@ target_link_libraries(swiftAST INTERFACE
154154
clangAPINotes
155155
clangBasic)
156156

157-
if(SWIFT_SWIFT_PARSER)
157+
if(SWIFT_BUILD_SWIFT_SYNTAX)
158158
target_compile_definitions(swiftAST
159159
PRIVATE
160-
SWIFT_SWIFT_PARSER
160+
SWIFT_BUILD_SWIFT_SYNTAX
161161
)
162162
endif()
163163

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)