Skip to content

Commit 4866360

Browse files
committed
Merge pull request swiftlang#2173 from rintaro/fetch-content
[CMake] Updates to allow inclusion using FetchContent (cherry picked from commit e0e5ad8) Conflicts: Sources/SwiftDiagnostics/CMakeLists.txt Sources/SwiftIDEUtils/CMakeLists.txt cmake/modules/AddSwiftHostLibrary.cmake
1 parent d3fe9e7 commit 4866360

File tree

15 files changed

+118
-95
lines changed

15 files changed

+118
-95
lines changed

CMakeLists.txt

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,25 @@ project(SwiftSyntax LANGUAGES C Swift)
1515
set(SWIFT_VERSION 5)
1616
set(CMAKE_Swift_LANGUAGE_VERSION ${SWIFT_VERSION})
1717

18+
if(CMAKE_VERSION VERSION_LESS 3.21)
19+
get_property(parent_dir DIRECTORY PROPERTY PARENT_DIRECTORY)
20+
if(NOT parent_dir)
21+
set(PROJECT_IS_TOP_LEVEL TRUE)
22+
endif()
23+
endif()
24+
1825
# The subdirectory into which host libraries will be installed.
1926
set(SWIFT_HOST_LIBRARIES_SUBDIRECTORY "swift/host")
2027

21-
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib/${SWIFT_HOST_LIBRARIES_SUBDIRECTORY}")
22-
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib/${SWIFT_HOST_LIBRARIES_SUBDIRECTORY}")
23-
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
28+
if(NOT CMAKE_ARCHIVE_OUTPUT_DIRECTORY)
29+
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib/${SWIFT_HOST_LIBRARIES_SUBDIRECTORY}")
30+
endif()
31+
if(NOT CMAKE_LIBRARY_OUTPUT_DIRECTORY)
32+
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib/${SWIFT_HOST_LIBRARIES_SUBDIRECTORY}")
33+
endif()
34+
if(NOT CMAKE_RUNTIME_OUTPUT_DIRECTORY)
35+
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
36+
endif()
2437

2538
set(CMAKE_MACOSX_RPATH YES)
2639

@@ -47,22 +60,25 @@ if (NOT SWIFT_SUPPORTS_DISABLE_IMPLICIT_STRING_PROCESSING_MODULE_IMPORT)
4760
endif()
4861

4962
# Determine the module triple.
50-
# FIXME: This is a hack. It's all a hack. Windows isn't setting
51-
# CMAKE_Swift_COMPILER_TARGET.
52-
if(CMAKE_Swift_COMPILER_TARGET)
53-
string(REGEX REPLACE "macosx[0-9]+([.][0-9]+)?" "macos" SWIFT_MODULE_TRIPLE
54-
${CMAKE_Swift_COMPILER_TARGET})
55-
elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Windows")
56-
if(CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64|AMD64")
57-
set(SWIFT_MODULE_TRIPLE "x86_64-unknown-windows-msvc")
58-
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64|ARM64|arm64")
59-
set(SWIFT_MODULE_TRIPLE "aarch64-unknown-windows-msvc")
63+
if("${SWIFT_HOST_MODULE_TRIPLE}" STREQUAL "")
64+
# FIXME: This is a hack. It's all a hack. Windows isn't setting
65+
# CMAKE_Swift_COMPILER_TARGET.
66+
if(CMAKE_Swift_COMPILER_TARGET)
67+
string(REGEX REPLACE "macosx[0-9]+([.][0-9]+)?" "macos" SWIFT_HOST_MODULE_TRIPLE
68+
${CMAKE_Swift_COMPILER_TARGET})
69+
elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Windows")
70+
if(CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64|AMD64")
71+
set(SWIFT_HOST_MODULE_TRIPLE "x86_64-unknown-windows-msvc")
72+
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64|ARM64|arm64")
73+
set(SWIFT_HOST_MODULE_TRIPLE "aarch64-unknown-windows-msvc")
74+
else()
75+
message(FATAL_ERROR "Unrecognized architecture for Windows host")
76+
endif()
6077
else()
61-
message(FATAL_ERROR "Unrecognized architecture for Windows host")
78+
message(FATAL_ERROR "Host module triple required")
6279
endif()
6380
endif()
64-
65-
message(STATUS "Module triple: ${SWIFT_MODULE_TRIPLE}")
81+
message(STATUS "Module triple: ${SWIFT_HOST_MODULE_TRIPLE}")
6682

6783
# Force single-threaded-only syntax trees to eliminate the Darwin
6884
# dependency in the compiler.
@@ -76,10 +92,4 @@ if (SWIFTSYNTAX_ENABLE_ASSERTIONS)
7692
endif()
7793

7894
add_subdirectory(Sources)
79-
80-
export(EXPORT SwiftSyntaxTargets
81-
FILE "${CMAKE_CURRENT_BINARY_DIR}/cmake/SwiftSyntaxTargets.cmake"
82-
NAMESPACE SwiftSyntax::
83-
)
84-
8595
add_subdirectory(cmake/modules)

Sources/CMakeLists.txt

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,6 @@
66
# See http://swift.org/LICENSE.txt for license information
77
# See http://swift.org/CONTRIBUTORS.txt for Swift project authors
88

9-
# cmake generation for Swift adds an order only dependency, which matches how C-family languages
10-
# works. In that case, however, ninja (and presumably other generators) will rebuild on header
11-
# changes. That's not the case for Swift, and thus if A -> B, A is not being rebuilt when the
12-
# ABI/API of B changes.
13-
#
14-
# For now workaround this by touching a file whenever B is rebuilt and then compiling that file as
15-
# part of A. Ideally this file would be generated by each of the targets, but that dependency didn't
16-
# seem to be being tracked.
17-
#
18-
# Remove once rdar://102202478 is fixed.
19-
function(target_link_libraries TARGET)
20-
cmake_parse_arguments(ARGS "" "" "PUBLIC" ${ARGN})
21-
22-
_target_link_libraries(${TARGET} PUBLIC ${ARGS_PUBLIC})
23-
foreach(DEPENDENCY ${ARGS_PUBLIC})
24-
add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/forced-${DEPENDENCY}-dep.swift
25-
COMMAND ${CMAKE_COMMAND} -E touch ${CMAKE_CURRENT_BINARY_DIR}/forced-${DEPENDENCY}-dep.swift
26-
DEPENDS ${DEPENDENCY}
27-
)
28-
target_sources(${TARGET} PRIVATE
29-
${CMAKE_CURRENT_BINARY_DIR}/forced-${DEPENDENCY}-dep.swift
30-
)
31-
endforeach()
32-
endfunction()
33-
349
add_subdirectory(SwiftBasicFormat)
3510
add_subdirectory(SwiftSyntax)
3611
add_subdirectory(SwiftDiagnostics)

Sources/SwiftBasicFormat/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
# See http://swift.org/LICENSE.txt for license information
77
# See http://swift.org/CONTRIBUTORS.txt for Swift project authors
88

9-
add_swift_host_library(SwiftBasicFormat
9+
add_swift_syntax_library(SwiftBasicFormat
1010
BasicFormat.swift
1111
generated/BasicFormat+Extensions.swift
1212
SyntaxProtocol+Formatted.swift
1313
Trivia+FormatExtensions.swift
1414
)
1515

16-
target_link_libraries(SwiftBasicFormat PUBLIC
16+
target_link_swift_syntax_libraries(SwiftBasicFormat PUBLIC
1717
SwiftSyntax)

Sources/SwiftCompilerPluginMessageHandling/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
# See http://swift.org/LICENSE.txt for license information
77
# See http://swift.org/CONTRIBUTORS.txt for Swift project authors
88

9-
add_swift_host_library(SwiftCompilerPluginMessageHandling
9+
add_swift_syntax_library(SwiftCompilerPluginMessageHandling
1010
CompilerPluginMessageHandler.swift
1111
Diagnostics.swift
1212
Macros.swift
@@ -15,7 +15,7 @@ add_swift_host_library(SwiftCompilerPluginMessageHandling
1515
PluginMessages.swift
1616
)
1717

18-
target_link_libraries(SwiftCompilerPluginMessageHandling PUBLIC
18+
target_link_swift_syntax_libraries(SwiftCompilerPluginMessageHandling PUBLIC
1919
SwiftSyntax
2020
SwiftBasicFormat
2121
SwiftDiagnostics

Sources/SwiftDiagnostics/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@ add_swift_host_library(SwiftDiagnostics
1515
Note.swift
1616
)
1717

18-
target_link_libraries(SwiftDiagnostics PUBLIC
18+
target_link_swift_syntax_libraries(SwiftDiagnostics PUBLIC
1919
SwiftSyntax)

Sources/SwiftIDEUtils/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
# See http://swift.org/LICENSE.txt for license information
77
# See http://swift.org/CONTRIBUTORS.txt for Swift project authors
88

9-
add_swift_host_library(SwiftIDEUtils
9+
add_swift_syntax_library(SwiftIDEUtils
1010
generated/SyntaxClassification.swift
1111
Syntax+Classifications.swift
1212
SyntaxClassifier.swift
1313
)
1414

15-
target_link_libraries(SwiftIDEUtils PUBLIC
15+
target_link_swift_syntax_libraries(SwiftIDEUtils PUBLIC
1616
SwiftSyntax)

Sources/SwiftOperators/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
# See http://swift.org/LICENSE.txt for license information
77
# See http://swift.org/CONTRIBUTORS.txt for Swift project authors
88

9-
add_swift_host_library(SwiftOperators
9+
add_swift_syntax_library(SwiftOperators
1010
Operator.swift
1111
OperatorError+Diagnostics.swift
1212
OperatorError.swift
@@ -19,7 +19,7 @@ add_swift_host_library(SwiftOperators
1919
SyntaxSynthesis.swift
2020
)
2121

22-
target_link_libraries(SwiftOperators PUBLIC
22+
target_link_swift_syntax_libraries(SwiftOperators PUBLIC
2323
SwiftSyntax
2424
SwiftDiagnostics
2525
SwiftParser)

Sources/SwiftParser/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
# See http://swift.org/LICENSE.txt for license information
77
# See http://swift.org/CONTRIBUTORS.txt for Swift project authors
88

9-
add_swift_host_library(SwiftParser
9+
add_swift_syntax_library(SwiftParser
1010
Attributes.swift
1111
Availability.swift
1212
CharacterInfo.swift
@@ -49,6 +49,6 @@ add_swift_host_library(SwiftParser
4949
Lexer/UnicodeScalarExtensions.swift
5050
)
5151

52-
target_link_libraries(SwiftParser PUBLIC
52+
target_link_swift_syntax_libraries(SwiftParser PUBLIC
5353
SwiftSyntax
5454
SwiftDiagnostics)

Sources/SwiftParserDiagnostics/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
# See http://swift.org/LICENSE.txt for license information
77
# See http://swift.org/CONTRIBUTORS.txt for Swift project authors
88

9-
add_swift_host_library(SwiftParserDiagnostics
9+
add_swift_syntax_library(SwiftParserDiagnostics
1010
DiagnosticExtensions.swift
1111
LexerDiagnosticMessages.swift
1212
MissingNodesError.swift
@@ -23,7 +23,7 @@ add_swift_host_library(SwiftParserDiagnostics
2323
generated/TokenNameForDiagnostics.swift
2424
)
2525

26-
target_link_libraries(SwiftParserDiagnostics PUBLIC
26+
target_link_swift_syntax_libraries(SwiftParserDiagnostics PUBLIC
2727
SwiftBasicFormat
2828
SwiftDiagnostics
2929
SwiftParser

Sources/SwiftSyntax/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
# See http://swift.org/LICENSE.txt for license information
77
# See http://swift.org/CONTRIBUTORS.txt for Swift project authors
88

9-
add_swift_host_library(SwiftSyntax
9+
add_swift_syntax_library(SwiftSyntax
1010
AbsolutePosition.swift
1111
Assert.swift
1212
BumpPtrAllocator.swift

Sources/SwiftSyntaxBuilder/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
# See http://swift.org/LICENSE.txt for license information
77
# See http://swift.org/CONTRIBUTORS.txt for Swift project authors
88

9-
add_swift_host_library(SwiftSyntaxBuilder
9+
add_swift_syntax_library(SwiftSyntaxBuilder
1010
ConvenienceInitializers.swift
1111
Indenter.swift
1212
ResultBuilderExtensions.swift
@@ -28,7 +28,7 @@ add_swift_host_library(SwiftSyntaxBuilder
2828
target_compile_options(SwiftSyntaxBuilder PRIVATE
2929
$<$<COMPILE_LANGUAGE:Swift>:-D;SWIFTSYNTAX_NO_OSLOG_DEPENDENCY>)
3030

31-
target_link_libraries(SwiftSyntaxBuilder PUBLIC
31+
target_link_swift_syntax_libraries(SwiftSyntaxBuilder PUBLIC
3232
SwiftBasicFormat
3333
SwiftParser
3434
SwiftParserDiagnostics
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
add_swift_host_library(SwiftSyntaxMacroExpansion
1+
add_swift_syntax_library(SwiftSyntaxMacroExpansion
22
BasicMacroExpansionContext.swift
33
FunctionParameterUtils.swift
44
MacroExpansion.swift
@@ -7,7 +7,7 @@ add_swift_host_library(SwiftSyntaxMacroExpansion
77
Syntax+MacroEvaluation.swift
88
)
99

10-
target_link_libraries(SwiftSyntaxMacroExpansion PUBLIC
10+
target_link_swift_syntax_libraries(SwiftSyntaxMacroExpansion PUBLIC
1111
SwiftSyntax
1212
SwiftSyntaxMacros
1313
)

Sources/SwiftSyntaxMacros/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
# See http://swift.org/LICENSE.txt for license information
77
# See http://swift.org/CONTRIBUTORS.txt for Swift project authors
88

9-
add_swift_host_library(SwiftSyntaxMacros
9+
add_swift_syntax_library(SwiftSyntaxMacros
1010
MacroProtocols/AccessorMacro.swift
1111
MacroProtocols/AttachedMacro.swift
1212
MacroProtocols/CodeItemMacro.swift
@@ -25,6 +25,6 @@ add_swift_host_library(SwiftSyntaxMacros
2525
MacroExpansionContext.swift
2626
)
2727

28-
target_link_libraries(SwiftSyntaxMacros PUBLIC
28+
target_link_swift_syntax_libraries(SwiftSyntaxMacros PUBLIC
2929
SwiftSyntaxBuilder
3030
)

cmake/modules/AddSwiftHostLibrary.cmake

Lines changed: 61 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,45 @@
66
# See http://swift.org/LICENSE.txt for license information
77
# See http://swift.org/CONTRIBUTORS.txt for Swift project authors
88

9+
# cmake generation for Swift adds an order only dependency, which matches how C-family languages
10+
# works. In that case, however, ninja (and presumably other generators) will rebuild on header
11+
# changes. That's not the case for Swift, and thus if A -> B, A is not being rebuilt when the
12+
# ABI/API of B changes.
13+
#
14+
# For now workaround this by touching a file whenever B is rebuilt and then compiling that file as
15+
# part of A. Ideally this file would be generated by each of the targets, but that dependency didn't
16+
# seem to be being tracked.
17+
#
18+
# Remove once rdar://102202478 is fixed.
19+
function(target_link_swift_syntax_libraries TARGET)
20+
target_link_libraries(${TARGET} ${ARGN})
21+
22+
cmake_parse_arguments(ARGS "PUBLIC;PRIVATE;INTERFACE" "" "" ${ARGN})
23+
foreach(DEPENDENCY ${ARGS_UNPARSED_ARGUMENTS})
24+
string(REGEX REPLACE [<>:\"/\\|?*] _ sanitized ${DEPENDENCY})
25+
add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/forced-${sanitized}-dep.swift
26+
COMMAND ${CMAKE_COMMAND} -E touch ${CMAKE_CURRENT_BINARY_DIR}/forced-${sanitized}-dep.swift
27+
DEPENDS ${DEPENDENCY}
28+
)
29+
target_sources(${TARGET} PRIVATE
30+
${CMAKE_CURRENT_BINARY_DIR}/forced-${sanitized}-dep.swift
31+
)
32+
endforeach()
33+
endfunction()
34+
935
# Add a new host library with the given name.
10-
function(add_swift_host_library name)
36+
function(add_swift_syntax_library name)
1137
set(ASHL_SOURCES ${ARGN})
1238

1339
# Create the library target.
1440
add_library(${name} ${ASHL_SOURCES})
1541

16-
# Add this to the list of exported targets.
17-
set_property(GLOBAL APPEND PROPERTY SWIFTSYNTAX_EXPORTS ${name})
18-
1942
# Determine where Swift modules will be built and installed.
2043
set(module_dir ${CMAKE_LIBRARY_OUTPUT_DIRECTORY})
2144
set(module_base "${module_dir}/${name}.swiftmodule")
22-
set(module_file "${module_base}/${SWIFT_MODULE_TRIPLE}.swiftmodule")
23-
set(module_interface_file "${module_base}/${SWIFT_MODULE_TRIPLE}.swiftinterface")
24-
set(module_sourceinfo_file "${module_base}/${SWIFT_MODULE_TRIPLE}.swiftsourceinfo")
45+
set(module_file "${module_base}/${SWIFT_HOST_MODULE_TRIPLE}.swiftmodule")
46+
set(module_interface_file "${module_base}/${SWIFT_HOST_MODULE_TRIPLE}.swiftinterface")
47+
set(module_sourceinfo_file "${module_base}/${SWIFT_HOST_MODULE_TRIPLE}.swiftsourceinfo")
2548

2649
# Add a custom target to create the module directory.
2750
add_custom_command(
@@ -56,6 +79,12 @@ function(add_swift_host_library name)
5679
-emit-module-interface-path;${module_interface_file}
5780
>)
5881

82+
if(LLVM_USE_LINKER)
83+
target_link_options(${name} PRIVATE
84+
"-use-ld=${LLVM_USE_LINKER}"
85+
)
86+
endif()
87+
5988
# NOTE: workaround for CMake not setting up include flags yet
6089
set_target_properties(${name} PROPERTIES
6190
INTERFACE_INCLUDE_DIRECTORIES ${module_dir}
@@ -65,31 +94,39 @@ function(add_swift_host_library name)
6594
BUILD_WITH_INSTALL_RPATH YES
6695
)
6796

97+
if(SWIFT_HOST_LIBRARIES_RPATH)
98+
# Don't add builder's stdlib RPATH automatically.
99+
target_compile_options(${name} PRIVATE -no-toolchain-stdlib-rpath)
100+
set_property(TARGET ${name}
101+
PROPERTY INSTALL_RPATH "${SWIFT_HOST_LIBRARIES_RPATH}"
102+
)
103+
endif()
104+
68105
get_target_property(lib_type ${name} TYPE)
69106
if(lib_type STREQUAL SHARED_LIBRARY)
70107
if (CMAKE_SYSTEM_NAME STREQUAL Darwin)
71108
# Allow install_name_tool to update paths (for rdar://109473564)
72109
set_property(TARGET ${name} APPEND_STRING PROPERTY
73110
LINK_FLAGS " -Xlinker -headerpad_max_install_names")
74-
elseif (CMAKE_SYSTEM_NAME STREQUAL Linux)
75-
# Make some room to update paths.
76-
set_property(TARGET ${name} APPEND PROPERTY
77-
INSTALL_RPATH ":::::::::::::::::::::::::::::::::::::::::::::::::::::::")
78111
endif()
79112
endif()
80113

81-
# Install this target
82-
install(TARGETS ${name}
83-
EXPORT SwiftSyntaxTargets
84-
ARCHIVE DESTINATION lib/${SWIFT_HOST_LIBRARIES_SUBDIRECTORY}
85-
LIBRARY DESTINATION lib/${SWIFT_HOST_LIBRARIES_SUBDIRECTORY}
86-
RUNTIME DESTINATION bin
87-
)
114+
if(PROJECT_IS_TOP_LEVEL)
115+
# Install this target
116+
install(TARGETS ${name}
117+
EXPORT SwiftSyntaxTargets
118+
ARCHIVE DESTINATION lib/${SWIFT_HOST_LIBRARIES_SUBDIRECTORY}
119+
LIBRARY DESTINATION lib/${SWIFT_HOST_LIBRARIES_SUBDIRECTORY}
120+
RUNTIME DESTINATION bin
121+
)
88122

89-
# Install the module files.
90-
install(
91-
DIRECTORY ${module_base}
92-
DESTINATION lib/${SWIFT_HOST_LIBRARIES_SUBDIRECTORY}
93-
FILES_MATCHING PATTERN "*.swiftinterface"
94-
)
123+
# Install the module files.
124+
install(
125+
DIRECTORY ${module_base}
126+
DESTINATION lib/${SWIFT_HOST_LIBRARIES_SUBDIRECTORY}
127+
FILES_MATCHING PATTERN "*.swiftinterface"
128+
)
129+
else()
130+
set_property(GLOBAL APPEND PROPERTY SWIFT_EXPORTS ${name})
131+
endif()
95132
endfunction()

0 commit comments

Comments
 (0)