Skip to content

Commit ca898c6

Browse files
committed
Merge pull request swiftlang#68408 from rintaro/fetch-content
[CMake] Replace early swift-syntax with FetchContent (cherry picked from commit 8dbde04) Conflicts: lib/Frontend/PrintingDiagnosticConsumer.cpp lib/Parse/ParseType.cpp
1 parent 7bd77a4 commit ca898c6

33 files changed

+232
-418
lines changed

CMakeLists.txt

Lines changed: 43 additions & 23 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.
@@ -697,10 +698,9 @@ if(CMAKE_C_COMPILER_ID MATCHES Clang)
697698
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-Werror=c++98-compat-extra-semi>)
698699
endif()
699700

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

705705
set(SWIFT_BUILD_HOST_DISPATCH FALSE)
706706
if(SWIFT_ENABLE_DISPATCH AND NOT CMAKE_SYSTEM_NAME STREQUAL "Darwin")
@@ -800,7 +800,6 @@ include(SwiftConfigureSDK)
800800
include(SwiftComponents)
801801
include(SwiftList)
802802
include(AddPureSwift)
803-
include(SetRPATH)
804803

805804
# Configure swift include, install, build components.
806805
swift_configure_components()
@@ -836,29 +835,19 @@ elseif(BOOTSTRAPPING_MODE MATCHES "BOOTSTRAPPING.*")
836835
else()
837836
set(BOOTSTRAPPING_MODE "HOSTTOOLS")
838837
endif()
839-
elseif(BOOTSTRAPPING_MODE STREQUAL "HOSTTOOLS" OR SWIFT_SWIFT_PARSER)
838+
elseif(BOOTSTRAPPING_MODE STREQUAL "HOSTTOOLS" OR SWIFT_BUILD_SWIFT_SYNTAX)
840839
# We are building using a pre-installed host toolchain but not bootstrapping
841840
# the Swift modules. This happens when building using 'build-tooling-libs'
842841
# where we haven't built a new Swift compiler. Use the Swift compiler from the
843842
# pre-installed host toolchain to build the Swift modules.
844843
set(SWIFT_EXEC_FOR_SWIFT_MODULES "${CMAKE_Swift_COMPILER}")
845844
endif()
846845

847-
# When we have the early SwiftSyntax build, we can include its parser.
848-
if(SWIFT_PATH_TO_EARLYSWIFTSYNTAX_BUILD_DIR)
849-
set(SWIFT_PATH_TO_EARLYSWIFTSYNTAX_TARGETS
850-
${SWIFT_PATH_TO_EARLYSWIFTSYNTAX_BUILD_DIR}/cmake/SwiftSyntaxTargets.cmake)
851-
if(NOT EXISTS "${SWIFT_PATH_TO_EARLYSWIFTSYNTAX_TARGETS}")
852-
message(STATUS "Skipping Swift Swift parser integration due to missing early SwiftSyntax")
853-
else()
854-
set(SWIFT_SWIFT_PARSER TRUE)
855-
include(${SWIFT_PATH_TO_EARLYSWIFTSYNTAX_TARGETS})
856-
857-
if(SWIFT_HOST_VARIANT_SDK MATCHES "LINUX|ANDROID|OPENBSD|FREEBSD" AND NOT BOOTSTRAPPING_MODE STREQUAL "HOSTTOOLS")
858-
# Only "HOSTTOOLS" is supported in Linux when Swift parser integration is enabled.
859-
message(WARNING "Force setting BOOTSTRAPPING=HOSTTOOLS because Swift parser integration is enabled")
860-
set(BOOTSTRAPPING_MODE "HOSTTOOLS")
861-
endif()
846+
if(SWIFT_BUILD_SWIFT_SYNTAX)
847+
# Only "HOSTTOOLS" is supported in Linux when Swift parser integration is enabled.
848+
if(SWIFT_HOST_VARIANT_SDK MATCHES "LINUX|ANDROID|OPENBSD|FREEBSD" AND NOT BOOTSTRAPPING_MODE STREQUAL "HOSTTOOLS")
849+
message(WARNING "Force setting BOOTSTRAPPING=HOSTTOOLS because Swift parser integration is enabled")
850+
set(BOOTSTRAPPING_MODE "HOSTTOOLS")
862851
endif()
863852
endif()
864853

@@ -968,7 +957,6 @@ if(XCODE)
968957
set(SWIFT_SDKS "OSX")
969958
endif()
970959

971-
972960
# FIXME: the parameters we specify in SWIFT_SDKS are lacking architecture specifics,
973961
# so we need to hard-code it. For example, the SDK for Android is just 'ANDROID',
974962
# and we have to specify SWIFT_SDK_ANDROID_ARCHITECTURES separately.
@@ -1169,13 +1157,17 @@ if("${CMAKE_SYSTEM_NAME}" STREQUAL "Darwin" AND NOT CMAKE_CROSSCOMPILING)
11691157
set(CMAKE_OSX_DEPLOYMENT_TARGET "")
11701158
endif()
11711159

1160+
swift_get_host_triple(SWIFT_HOST_TRIPLE)
1161+
set(SWIFT_HOST_MODULE_TRIPLE "${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_ARCH_${SWIFT_HOST_VARIANT_ARCH}_MODULE}")
1162+
set(SWIFT_HOST_LIBRARIES_DEST_DIR "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/swift/host")
1163+
11721164
if(SWIFT_INCLUDE_TOOLS)
11731165
message(STATUS "Building host Swift tools for ${SWIFT_HOST_VARIANT_SDK} ${SWIFT_HOST_VARIANT_ARCH}")
11741166
message(STATUS " Build type: ${CMAKE_BUILD_TYPE}")
11751167
message(STATUS " Assertions: ${LLVM_ENABLE_ASSERTIONS}")
11761168
message(STATUS " LTO: ${SWIFT_TOOLS_ENABLE_LTO}")
11771169
message(STATUS " Bootstrapping: ${BOOTSTRAPPING_MODE}")
1178-
message(STATUS " Swift parser: ${SWIFT_SWIFT_PARSER}")
1170+
message(STATUS " Swift parser: ${SWIFT_BUILD_SWIFT_SYNTAX}")
11791171
message(STATUS "")
11801172
else()
11811173
message(STATUS "Not building host Swift tools")
@@ -1326,6 +1318,34 @@ endif()
13261318
add_subdirectory(include)
13271319

13281320
if(SWIFT_INCLUDE_TOOLS)
1321+
# Include 'swift-syntax'.
1322+
# This is a function because we want to set some 'CMAKE_*' variables temporarily.'
1323+
# TODO: Replace this with 'block()' after CMake 3.25
1324+
function(include_swift_syntax)
1325+
if(NOT SWIFT_BUILD_SWIFT_SYNTAX)
1326+
return()
1327+
endif()
1328+
if(NOT EXISTS "${SWIFT_PATH_TO_SWIFT_SYNTAX_SOURCE}")
1329+
message(SEND_ERROR "swift-syntax is required to build the Swift compiler. Please run update-checkout or specify SWIFT_PATH_TO_SWIFT_SYNTAX_SOURCE")
1330+
return()
1331+
endif()
1332+
1333+
set(CMAKE_Swift_COMPILER_TARGET ${SWIFT_HOST_TRIPLE})
1334+
set(BUILD_SHARED_LIBS ON)
1335+
# All libraries in 'swift-syntax' goes to 'lib/swift/host'.
1336+
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${SWIFT_HOST_LIBRARIES_DEST_DIR}")
1337+
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${SWIFT_HOST_LIBRARIES_DEST_DIR}")
1338+
if(SWIFT_HOST_VARIANT_SDK MATCHES "LINUX|ANDROID|OPENBSD|FREEBSD")
1339+
set(SWIFT_HOST_LIBRARIES_RPATH "$ORIGIN;$ORIGIN/../${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_LIB_SUBDIR}")
1340+
endif()
1341+
1342+
FetchContent_Declare(SwiftSyntax
1343+
SOURCE_DIR "${SWIFT_PATH_TO_SWIFT_SYNTAX_SOURCE}"
1344+
)
1345+
FetchContent_MakeAvailable(SwiftSyntax)
1346+
endfunction()
1347+
include_swift_syntax()
1348+
13291349
add_subdirectory(lib)
13301350

13311351
# SwiftCompilerSources must come before "tools".

cmake/modules/AddPureSwift.cmake

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,15 @@ include(macCatalystUtils)
22

33
# Workaround a cmake bug, see the corresponding function in swift-syntax
44
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 ${DEPENDENCY})
9-
add_dependencies(${TARGET} ${DEPENDENCY})
5+
target_link_libraries(${TARGET} ${ARGN})
106

7+
cmake_parse_arguments(ARGS "PUBLIC;PRIVATE;INTERFACE" "" "" ${ARGN})
8+
foreach(DEPENDENCY ${ARGS_UNPARSED_ARGUMENTS})
119
string(REGEX REPLACE [<>:\"/\\|?*] _ sanitized ${DEPENDENCY})
1210
add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/forced-${sanitized}-dep.swift
1311
COMMAND ${CMAKE_COMMAND} -E touch ${CMAKE_CURRENT_BINARY_DIR}/forced-${sanitized}-dep.swift
1412
DEPENDS ${DEPENDENCY}
15-
)
13+
)
1614
target_sources(${TARGET} PRIVATE
1715
${CMAKE_CURRENT_BINARY_DIR}/forced-${sanitized}-dep.swift
1816
)
@@ -46,21 +44,7 @@ function(_add_host_swift_compile_options name)
4644
$<$<COMPILE_LANGUAGE:Swift>:-runtime-compatibility-version>
4745
$<$<COMPILE_LANGUAGE:Swift>:none>)
4846

49-
# Set the appropriate target triple.
50-
# FIXME: This should be set by CMake.
51-
if(SWIFT_HOST_VARIANT_SDK IN_LIST SWIFT_DARWIN_PLATFORMS)
52-
set(DEPLOYMENT_VERSION "${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_DEPLOYMENT_VERSION}")
53-
endif()
54-
55-
if(SWIFT_HOST_VARIANT_SDK STREQUAL "ANDROID")
56-
set(DEPLOYMENT_VERSION ${SWIFT_ANDROID_API_LEVEL})
57-
endif()
58-
59-
get_target_triple(target target_variant "${SWIFT_HOST_VARIANT_SDK}" "${SWIFT_HOST_VARIANT_ARCH}"
60-
MACCATALYST_BUILD_FLAVOR ""
61-
DEPLOYMENT_VERSION "${DEPLOYMENT_VERSION}")
62-
63-
target_compile_options(${name} PRIVATE $<$<COMPILE_LANGUAGE:Swift>:-target;${target}>)
47+
target_compile_options(${name} PRIVATE $<$<COMPILE_LANGUAGE:Swift>:-target;${SWIFT_HOST_TRIPLE}>)
6448
_add_host_variant_swift_sanitizer_flags(${name})
6549
endfunction()
6650

@@ -121,7 +105,7 @@ endfunction()
121105
# source1 ...
122106
# Sources to add into this library.
123107
function(add_pure_swift_host_library name)
124-
if (NOT SWIFT_SWIFT_PARSER)
108+
if (NOT SWIFT_BUILD_SWIFT_SYNTAX)
125109
message(STATUS "Not building ${name} because swift-syntax is not available")
126110
return()
127111
endif()
@@ -196,13 +180,15 @@ function(add_pure_swift_host_library name)
196180

197181
# Make sure we can use the host libraries.
198182
target_include_directories(${name} PUBLIC
199-
${SWIFT_HOST_LIBRARIES_DEST_DIR})
183+
"${SWIFT_HOST_LIBRARIES_DEST_DIR}")
184+
target_link_directories(${name} PUBLIC
185+
"${SWIFT_HOST_LIBRARIES_DEST_DIR}")
200186

201187
if(APSHL_EMIT_MODULE)
202188
# Determine where Swift modules will be built and installed.
203189

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})
190+
set(module_triple "${SWIFT_HOST_MODULE_TRIPLE}")
191+
set(module_dir "${SWIFT_HOST_LIBRARIES_DEST_DIR}")
206192
set(module_base "${module_dir}/${name}.swiftmodule")
207193
set(module_file "${module_base}/${module_triple}.swiftmodule")
208194
set(module_interface_file "${module_base}/${module_triple}.swiftinterface")
@@ -234,14 +220,20 @@ function(add_pure_swift_host_library name)
234220
>)
235221
endif()
236222

223+
if(LLVM_USE_LINKER)
224+
target_link_options(${name} PRIVATE
225+
"-use-ld=${LLVM_USE_LINKER}"
226+
)
227+
endif()
228+
237229
# Export this target.
238230
set_property(GLOBAL APPEND PROPERTY SWIFT_EXPORTS ${name})
239231
endfunction()
240232

241233
# Add a new "pure" Swift host tool.
242234
#
243235
# "Pure" Swift host tools can only contain Swift code, and will be built
244-
# with the host compiler.
236+
# with the host compiler.
245237
#
246238
# Usage:
247239
# add_pure_swift_host_tool(name
@@ -262,7 +254,7 @@ endfunction()
262254
# source1 ...
263255
# Sources to add into this tool.
264256
function(add_pure_swift_host_tool name)
265-
if (NOT SWIFT_SWIFT_PARSER)
257+
if (NOT SWIFT_BUILD_SWIFT_SYNTAX)
266258
message(STATUS "Not building ${name} because swift-syntax is not available")
267259
return()
268260
endif()
@@ -322,7 +314,15 @@ function(add_pure_swift_host_tool name)
322314

323315
# Make sure we can use the host libraries.
324316
target_include_directories(${name} PUBLIC
325-
${SWIFT_HOST_LIBRARIES_DEST_DIR})
317+
"${SWIFT_HOST_LIBRARIES_DEST_DIR}")
318+
target_link_directories(${name} PUBLIC
319+
"${SWIFT_HOST_LIBRARIES_DEST_DIR}")
320+
321+
if(LLVM_USE_LINKER)
322+
target_link_options(${name} PRIVATE
323+
"-use-ld=${LLVM_USE_LINKER}"
324+
)
325+
endif()
326326

327327
if(NOT APSHT_SWIFT_COMPONENT STREQUAL no_component)
328328
add_dependencies(${APSHT_SWIFT_COMPONENT} ${name})

cmake/modules/AddSwift.cmake

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,7 @@ function(_add_host_variant_swift_sanitizer_flags target)
113113
endif()
114114
endfunction()
115115

116-
# Usage:
117-
# _add_host_variant_c_compile_link_flags(name)
118-
function(_add_host_variant_c_compile_link_flags name)
116+
function(swift_get_host_triple out_var)
119117
if(SWIFT_HOST_VARIANT_SDK IN_LIST SWIFT_DARWIN_PLATFORMS)
120118
set(DEPLOYMENT_VERSION "${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_DEPLOYMENT_VERSION}")
121119
endif()
@@ -124,26 +122,30 @@ function(_add_host_variant_c_compile_link_flags name)
124122
set(DEPLOYMENT_VERSION ${SWIFT_ANDROID_API_LEVEL})
125123
endif()
126124

125+
get_target_triple(target target_variant "${SWIFT_HOST_VARIANT_SDK}" "${SWIFT_HOST_VARIANT_ARCH}"
126+
MACCATALYST_BUILD_FLAVOR ""
127+
DEPLOYMENT_VERSION "${DEPLOYMENT_VERSION}")
128+
129+
set(${out_var} "${target}" PARENT_SCOPE)
130+
endfunction()
131+
132+
# Usage:
133+
# _add_host_variant_c_compile_link_flags(name)
134+
function(_add_host_variant_c_compile_link_flags name)
127135
# MSVC and gcc don't understand -target.
128136
# clang-cl understands --target.
129137
if(CMAKE_C_COMPILER_ID MATCHES "Clang")
130-
get_target_triple(target target_variant "${SWIFT_HOST_VARIANT_SDK}" "${SWIFT_HOST_VARIANT_ARCH}"
131-
MACCATALYST_BUILD_FLAVOR ""
132-
DEPLOYMENT_VERSION "${DEPLOYMENT_VERSION}")
133138
if("${CMAKE_C_COMPILER_FRONTEND_VARIANT}" STREQUAL "MSVC") # clang-cl options
134-
target_compile_options(${name} PRIVATE $<$<COMPILE_LANGUAGE:C,CXX,OBJC,OBJCXX>:--target=${target}>)
135-
target_link_options(${name} PRIVATE $<$<COMPILE_LANGUAGE:C,CXX,OBJC,OBJCXX>:--target=${target}>)
139+
target_compile_options(${name} PRIVATE $<$<COMPILE_LANGUAGE:C,CXX,OBJC,OBJCXX>:--target=${SWIFT_HOST_TRIPLE}>)
140+
target_link_options(${name} PRIVATE $<$<COMPILE_LANGUAGE:C,CXX,OBJC,OBJCXX>:--target=${SWIFT_HOST_TRIPLE}>)
136141
else()
137-
target_compile_options(${name} PRIVATE $<$<COMPILE_LANGUAGE:C,CXX,OBJC,OBJCXX>:-target;${target}>)
138-
target_link_options(${name} PRIVATE $<$<COMPILE_LANGUAGE:C,CXX,OBJC,OBJCXX>:-target;${target}>)
142+
target_compile_options(${name} PRIVATE $<$<COMPILE_LANGUAGE:C,CXX,OBJC,OBJCXX>:-target;${SWIFT_HOST_TRIPLE}>)
143+
target_link_options(${name} PRIVATE $<$<COMPILE_LANGUAGE:C,CXX,OBJC,OBJCXX>:-target;${SWIFT_HOST_TRIPLE}>)
139144
endif()
140145
endif()
141146

142147
if (CMAKE_Swift_COMPILER)
143-
get_target_triple(target target_variant "${SWIFT_HOST_VARIANT_SDK}" "${SWIFT_HOST_VARIANT_ARCH}"
144-
MACCATALYST_BUILD_FLAVOR ""
145-
DEPLOYMENT_VERSION "${DEPLOYMENT_VERSION}")
146-
target_compile_options(${name} PRIVATE $<$<COMPILE_LANGUAGE:Swift>:-target;${target}>)
148+
target_compile_options(${name} PRIVATE $<$<COMPILE_LANGUAGE:Swift>:-target;${SWIFT_HOST_TRIPLE}>)
147149

148150
_add_host_variant_swift_sanitizer_flags(${name})
149151
endif()
@@ -439,7 +441,7 @@ endfunction()
439441

440442
function(_add_swift_runtime_link_flags target relpath_to_lib_dir bootstrapping)
441443
if(NOT BOOTSTRAPPING_MODE)
442-
if (SWIFT_SWIFT_PARSER)
444+
if (SWIFT_BUILD_SWIFT_SYNTAX)
443445
set(ASRLF_BOOTSTRAPPING_MODE "HOSTTOOLS")
444446
else()
445447
return()
@@ -576,7 +578,7 @@ function(_add_swift_runtime_link_flags target relpath_to_lib_dir bootstrapping)
576578
${SWIFT_PATH_TO_SWIFT_SDK}/usr/lib/swift/${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_LIB_SUBDIR}/${SWIFT_HOST_VARIANT_ARCH})
577579
endif()
578580

579-
if(SWIFT_SWIFT_PARSER)
581+
if(SWIFT_BUILD_SWIFT_SYNTAX)
580582
# For the "end step" of bootstrapping configurations, we need to be
581583
# able to fall back to the SDK directory for libswiftCore et al.
582584
if (BOOTSTRAPPING_MODE MATCHES "BOOTSTRAPPING.*")
@@ -654,7 +656,7 @@ function(add_swift_host_library name)
654656
translate_flags(ASHL "${options}")
655657

656658
# Once the new Swift parser is linked, everything has Swift modules.
657-
if (SWIFT_SWIFT_PARSER AND ASHL_SHARED)
659+
if (SWIFT_BUILD_SWIFT_SYNTAX AND ASHL_SHARED)
658660
set(ASHL_HAS_SWIFT_MODULES ON)
659661
endif()
660662

@@ -700,7 +702,7 @@ function(add_swift_host_library name)
700702

701703
add_library(${name} ${libkind} ${ASHL_SOURCES})
702704

703-
target_link_directories(${name} PUBLIC ${CMAKE_LIBRARY_OUTPUT_DIRECTORY})
705+
target_link_directories(${name} PUBLIC "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}")
704706

705707
# Respect LLVM_COMMON_DEPENDS if it is set.
706708
#
@@ -922,7 +924,7 @@ function(add_swift_host_tool executable)
922924
endif()
923925

924926
# Once the new Swift parser is linked in, every host tool has Swift modules.
925-
if (SWIFT_SWIFT_PARSER AND NOT ASHT_DOES_NOT_USE_SWIFT)
927+
if (SWIFT_BUILD_SWIFT_SYNTAX AND NOT ASHT_DOES_NOT_USE_SWIFT)
926928
set(ASHT_HAS_SWIFT_MODULES ON)
927929
endif()
928930

@@ -961,7 +963,7 @@ function(add_swift_host_tool executable)
961963
endif()
962964
endif()
963965

964-
if(SWIFT_SWIFT_PARSER)
966+
if(SWIFT_BUILD_SWIFT_SYNTAX)
965967
set(extra_relative_rpath "")
966968
if(NOT "${ASHT_BOOTSTRAPPING}" STREQUAL "")
967969
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

cmake/modules/SetRPATH.cmake

Lines changed: 0 additions & 24 deletions
This file was deleted.

0 commit comments

Comments
 (0)