Skip to content

Commit 1043139

Browse files
bnbarhamrintaro
authored andcommitted
[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 7a499de commit 1043139

31 files changed

+145
-369
lines changed

CMakeLists.txt

Lines changed: 27 additions & 22 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")
@@ -830,7 +830,7 @@ elseif(BOOTSTRAPPING_MODE MATCHES "BOOTSTRAPPING.*")
830830
else()
831831
set(BOOTSTRAPPING_MODE "HOSTTOOLS")
832832
endif()
833-
elseif(BOOTSTRAPPING_MODE STREQUAL "HOSTTOOLS" OR SWIFT_SWIFT_PARSER)
833+
elseif(BOOTSTRAPPING_MODE STREQUAL "HOSTTOOLS" OR SWIFT_BUILD_SWIFT_SYNTAX)
834834
# We are building using a pre-installed host toolchain but not bootstrapping
835835
# the Swift modules. This happens when building using 'build-tooling-libs'
836836
# where we haven't built a new Swift compiler. Use the Swift compiler from the
@@ -944,25 +944,14 @@ if(XCODE)
944944
set(SWIFT_SDKS "OSX")
945945
endif()
946946

947-
# When we have the early SwiftSyntax build, we can include its parser.
948-
if(SWIFT_PATH_TO_EARLYSWIFTSYNTAX_BUILD_DIR)
949-
set(SWIFT_PATH_TO_EARLYSWIFTSYNTAX_TARGETS
950-
${SWIFT_PATH_TO_EARLYSWIFTSYNTAX_BUILD_DIR}/cmake/SwiftSyntaxTargets.cmake)
951-
if(NOT EXISTS "${SWIFT_PATH_TO_EARLYSWIFTSYNTAX_TARGETS}")
952-
message(STATUS "Skipping Swift Swift parser integration due to missing early SwiftSyntax")
953-
else()
954-
set(SWIFT_SWIFT_PARSER TRUE)
955-
include(${SWIFT_PATH_TO_EARLYSWIFTSYNTAX_TARGETS})
956-
957-
if(SWIFT_HOST_VARIANT_SDK MATCHES "LINUX|ANDROID|OPENBSD|FREEBSD" AND NOT BOOTSTRAPPING_MODE STREQUAL "HOSTTOOLS")
958-
# Only "HOSTTOOLS" is supported in Linux when Swift parser integration is enabled.
959-
message(WARNING "Force setting BOOTSTRAPPING=HOSTTOOLS because Swift parser integration is enabled")
960-
set(BOOTSTRAPPING_MODE "HOSTTOOLS")
961-
endif()
947+
if(SWIFT_BUILD_SWIFT_SYNTAX)
948+
# Only "HOSTTOOLS" is supported in Linux when Swift parser integration is enabled.
949+
if(SWIFT_HOST_VARIANT_SDK MATCHES "LINUX|ANDROID|OPENBSD|FREEBSD" AND NOT BOOTSTRAPPING_MODE STREQUAL "HOSTTOOLS")
950+
message(WARNING "Force setting BOOTSTRAPPING=HOSTTOOLS because Swift parser integration is enabled")
951+
set(BOOTSTRAPPING_MODE "HOSTTOOLS")
962952
endif()
963953
endif()
964954

965-
966955
# FIXME: the parameters we specify in SWIFT_SDKS are lacking architecture specifics,
967956
# so we need to hard-code it. For example, the SDK for Android is just 'ANDROID',
968957
# and we have to specify SWIFT_SDK_ANDROID_ARCHITECTURES separately.
@@ -1171,13 +1160,29 @@ if("${CMAKE_SYSTEM_NAME}" STREQUAL "Darwin" AND NOT CMAKE_CROSSCOMPILING)
11711160
set(CMAKE_OSX_DEPLOYMENT_TARGET "")
11721161
endif()
11731162

1163+
set(SWIFT_HOST_MODULE_TRIPLE "${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_ARCH_${SWIFT_HOST_VARIANT_ARCH}_MODULE}")
1164+
set(SWIFT_HOST_LIBRARIES_DEST_DIR "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/swift/host")
1165+
if(SWIFT_BUILD_SWIFT_SYNTAX)
1166+
if(NOT EXISTS "${SWIFT_PATH_TO_SWIFT_SYNTAX_SOURCE}")
1167+
message(SEND_ERROR "swift-syntax is required to build the Swift compiler. Please run update-checkout or specify SWIFT_PATH_TO_SWIFT_SYNTAX_SOURCE")
1168+
endif()
1169+
1170+
set(BUILD_SHARED_LIBS_OLD "${BUILD_SHARED_LIBS}")
1171+
set(BUILD_SHARED_LIBS ON)
1172+
FetchContent_Declare(SwiftSyntax
1173+
SOURCE_DIR "${SWIFT_PATH_TO_SWIFT_SYNTAX_SOURCE}"
1174+
)
1175+
FetchContent_MakeAvailable(SwiftSyntax)
1176+
set(BUILD_SHARED_LIBS "${BUILD_SHARED_LIBS_OLD}")
1177+
endif()
1178+
11741179
if(SWIFT_INCLUDE_TOOLS)
11751180
message(STATUS "Building host Swift tools for ${SWIFT_HOST_VARIANT_SDK} ${SWIFT_HOST_VARIANT_ARCH}")
11761181
message(STATUS " Build type: ${CMAKE_BUILD_TYPE}")
11771182
message(STATUS " Assertions: ${LLVM_ENABLE_ASSERTIONS}")
11781183
message(STATUS " LTO: ${SWIFT_TOOLS_ENABLE_LTO}")
11791184
message(STATUS " Bootstrapping: ${BOOTSTRAPPING_MODE}")
1180-
message(STATUS " Swift parser: ${SWIFT_SWIFT_PARSER}")
1185+
message(STATUS " Swift parser: ${SWIFT_BUILD_SWIFT_SYNTAX}")
11811186
message(STATUS "")
11821187
else()
11831188
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
@@ -121,7 +121,7 @@ endfunction()
121121
# source1 ...
122122
# Sources to add into this library.
123123
function(add_pure_swift_host_library name)
124-
if (NOT SWIFT_SWIFT_PARSER)
124+
if (NOT SWIFT_BUILD_SWIFT_SYNTAX)
125125
message(STATUS "Not building ${name} because swift-syntax is not available")
126126
return()
127127
endif()
@@ -196,13 +196,15 @@ function(add_pure_swift_host_library name)
196196

197197
# Make sure we can use the host libraries.
198198
target_include_directories(${name} PUBLIC
199-
${SWIFT_HOST_LIBRARIES_DEST_DIR})
199+
"${SWIFT_HOST_LIBRARIES_DEST_DIR}")
200+
target_link_directories(${name} PUBLIC
201+
"${SWIFT_HOST_LIBRARIES_DEST_DIR}")
200202

201203
if(APSHL_EMIT_MODULE)
202204
# Determine where Swift modules will be built and installed.
203205

204-
set(module_triple ${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_ARCH_${SWIFT_HOST_VARIANT_ARCH}_MODULE})
205-
set(module_dir ${SWIFT_HOST_LIBRARIES_DEST_DIR})
206+
set(module_triple "${SWIFT_HOST_MODULE_TRIPLE}")
207+
set(module_dir "${SWIFT_HOST_LIBRARIES_DEST_DIR}")
206208
set(module_base "${module_dir}/${name}.swiftmodule")
207209
set(module_file "${module_base}/${module_triple}.swiftmodule")
208210
set(module_interface_file "${module_base}/${module_triple}.swiftinterface")
@@ -234,14 +236,20 @@ function(add_pure_swift_host_library name)
234236
>)
235237
endif()
236238

239+
if(LLVM_USE_LINKER)
240+
target_link_options(${name} PRIVATE
241+
"-use-ld=${LLVM_USE_LINKER}"
242+
)
243+
endif()
244+
237245
# Export this target.
238246
set_property(GLOBAL APPEND PROPERTY SWIFT_EXPORTS ${name})
239247
endfunction()
240248

241249
# Add a new "pure" Swift host tool.
242250
#
243251
# "Pure" Swift host tools can only contain Swift code, and will be built
244-
# with the host compiler.
252+
# with the host compiler.
245253
#
246254
# Usage:
247255
# add_pure_swift_host_tool(name
@@ -262,7 +270,7 @@ endfunction()
262270
# source1 ...
263271
# Sources to add into this tool.
264272
function(add_pure_swift_host_tool name)
265-
if (NOT SWIFT_SWIFT_PARSER)
273+
if (NOT SWIFT_BUILD_SWIFT_SYNTAX)
266274
message(STATUS "Not building ${name} because swift-syntax is not available")
267275
return()
268276
endif()
@@ -322,7 +330,15 @@ function(add_pure_swift_host_tool name)
322330

323331
# Make sure we can use the host libraries.
324332
target_include_directories(${name} PUBLIC
325-
${SWIFT_HOST_LIBRARIES_DEST_DIR})
333+
"${SWIFT_HOST_LIBRARIES_DEST_DIR}")
334+
target_link_directories(${name} PUBLIC
335+
"${SWIFT_HOST_LIBRARIES_DEST_DIR}")
336+
337+
if(LLVM_USE_LINKER)
338+
target_link_options(${name} PRIVATE
339+
"-use-ld=${LLVM_USE_LINKER}"
340+
)
341+
endif()
326342

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

cmake/modules/AddSwift.cmake

Lines changed: 6 additions & 6 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()
@@ -578,7 +578,7 @@ function(_add_swift_runtime_link_flags target relpath_to_lib_dir bootstrapping)
578578
${SWIFT_PATH_TO_SWIFT_SDK}/usr/lib/swift/${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_LIB_SUBDIR}/${SWIFT_HOST_VARIANT_ARCH})
579579
endif()
580580
581-
if(SWIFT_SWIFT_PARSER)
581+
if(SWIFT_BUILD_SWIFT_SYNTAX)
582582
# For the "end step" of bootstrapping configurations, we need to be
583583
# able to fall back to the SDK directory for libswiftCore et al.
584584
if (BOOTSTRAPPING_MODE MATCHES "BOOTSTRAPPING.*")
@@ -656,7 +656,7 @@ function(add_swift_host_library name)
656656
translate_flags(ASHL "${options}")
657657
658658
# Once the new Swift parser is linked, everything has Swift modules.
659-
if (SWIFT_SWIFT_PARSER AND ASHL_SHARED)
659+
if (SWIFT_BUILD_SWIFT_SYNTAX AND ASHL_SHARED)
660660
set(ASHL_HAS_SWIFT_MODULES ON)
661661
endif()
662662
@@ -702,7 +702,7 @@ function(add_swift_host_library name)
702702
703703
add_library(${name} ${libkind} ${ASHL_SOURCES})
704704
705-
target_link_directories(${name} PUBLIC ${CMAKE_LIBRARY_OUTPUT_DIRECTORY})
705+
target_link_directories(${name} PUBLIC "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}")
706706
707707
# Respect LLVM_COMMON_DEPENDS if it is set.
708708
#
@@ -924,7 +924,7 @@ function(add_swift_host_tool executable)
924924
endif()
925925
926926
# Once the new Swift parser is linked in, every host tool has Swift modules.
927-
if (SWIFT_SWIFT_PARSER AND NOT ASHT_DOES_NOT_USE_SWIFT)
927+
if (SWIFT_BUILD_SWIFT_SYNTAX AND NOT ASHT_DOES_NOT_USE_SWIFT)
928928
set(ASHT_HAS_SWIFT_MODULES ON)
929929
endif()
930930
@@ -963,7 +963,7 @@ function(add_swift_host_tool executable)
963963
endif()
964964
endif()
965965
966-
if(SWIFT_SWIFT_PARSER)
966+
if(SWIFT_BUILD_SWIFT_SYNTAX)
967967
set(extra_relative_rpath "")
968968
if(NOT "${ASHT_BOOTSTRAPPING}" STREQUAL "")
969969
if(executable MATCHES "-bootstrapping")

cmake/modules/AddSwiftUnittests.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ function(add_swift_unittest test_dirname)
123123
APPEND PROPERTY INSTALL_RPATH "$ORIGIN/${relative_lib_path}")
124124
endif()
125125

126-
if (SWIFT_SWIFT_PARSER AND NOT ASU_IS_TARGET_TEST)
126+
if (SWIFT_BUILD_SWIFT_SYNTAX AND NOT ASU_IS_TARGET_TEST)
127127
# Link to stdlib the compiler uses.
128128
_add_swift_runtime_link_flags(${test_dirname} "${relative_lib_path}" "")
129129

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
)

0 commit comments

Comments
 (0)