Skip to content

Commit 87d268f

Browse files
bnbarhamrintaro
authored andcommitted
[CMake] Updates to allow inclusion using FetchContent
Various updates that allow swift-syntax to be included using FetchContent. Mostly this is just not replacing `target_link_libraries` since that is a global replacement, but we also now use a couple of variables from the swift if they're set (eg. `SWIFT_HOST_LIBRARIES_DEST_DIR` and `SWIFT_HOST_MODULE_TRIPLE`).
1 parent c937cb7 commit 87d268f

File tree

15 files changed

+114
-92
lines changed

15 files changed

+114
-92
lines changed

CMakeLists.txt

Lines changed: 41 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,28 @@ 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(SWIFT_HOST_LIBRARIES_DEST_DIR)
29+
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${SWIFT_HOST_LIBRARIES_DEST_DIR}")
30+
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${SWIFT_HOST_LIBRARIES_DEST_DIR}")
31+
else()
32+
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib/${SWIFT_HOST_LIBRARIES_SUBDIRECTORY}")
33+
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib/${SWIFT_HOST_LIBRARIES_SUBDIRECTORY}")
34+
endif()
35+
if(SWIFT_HOST_RUNTIME_DEST_DIR)
36+
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${SWIFT_HOST_RUNTIME_DEST_DIR}")
37+
else()
38+
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
39+
endif()
2440

2541
set(CMAKE_MACOSX_RPATH YES)
2642

@@ -49,22 +65,25 @@ if (NOT SWIFT_SUPPORTS_DISABLE_IMPLICIT_STRING_PROCESSING_MODULE_IMPORT)
4965
endif()
5066

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

6988
# Force single-threaded-only syntax trees to eliminate the Darwin
7089
# dependency in the compiler.
@@ -79,9 +98,9 @@ endif()
7998

8099
add_subdirectory(Sources)
81100

82-
export(EXPORT SwiftSyntaxTargets
83-
FILE "${CMAKE_CURRENT_BINARY_DIR}/cmake/SwiftSyntaxTargets.cmake"
84-
NAMESPACE SwiftSyntax::
85-
)
86-
87-
add_subdirectory(cmake/modules)
101+
if(PROJECT_IS_TOP_LEVEL)
102+
export(EXPORT SwiftSyntaxTargets
103+
FILE "${CMAKE_CURRENT_BINARY_DIR}/cmake/SwiftSyntaxTargets.cmake"
104+
NAMESPACE SwiftSyntax::
105+
)
106+
endif()

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
Syntax+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: 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(SwiftDiagnostics
9+
add_swift_syntax_library(SwiftDiagnostics
1010
Convenience.swift
1111
Diagnostic.swift
1212
DiagnosticsFormatter.swift
@@ -16,5 +16,5 @@ add_swift_host_library(SwiftDiagnostics
1616
Note.swift
1717
)
1818

19-
target_link_libraries(SwiftDiagnostics PUBLIC
19+
target_link_swift_syntax_libraries(SwiftDiagnostics PUBLIC
2020
SwiftSyntax)

Sources/SwiftIDEUtils/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(SwiftIDEUtils
9+
add_swift_syntax_library(SwiftIDEUtils
1010
SwiftIDEUtilsCompatibility.swift
1111
Syntax+Classifications.swift
1212
SyntaxClassification.swift
1313
SyntaxClassifier.swift
1414
)
1515

16-
target_link_libraries(SwiftIDEUtils PUBLIC
16+
target_link_swift_syntax_libraries(SwiftIDEUtils PUBLIC
1717
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
@@ -52,6 +52,6 @@ add_swift_host_library(SwiftParser
5252
Lexer/UnicodeScalarExtensions.swift
5353
)
5454

55-
target_link_libraries(SwiftParser PUBLIC
55+
target_link_swift_syntax_libraries(SwiftParser PUBLIC
5656
SwiftSyntax
5757
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
AbsoluteRawSyntax.swift
1212
AbsoluteSyntaxInfo.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
DeclSyntaxParseable.swift
1212
Indenter.swift
@@ -30,7 +30,7 @@ add_swift_host_library(SwiftSyntaxBuilder
3030
target_compile_options(SwiftSyntaxBuilder PRIVATE
3131
$<$<COMPILE_LANGUAGE:Swift>:-D;SWIFTSYNTAX_NO_OSLOG_DEPENDENCY>)
3232

33-
target_link_libraries(SwiftSyntaxBuilder PUBLIC
33+
target_link_swift_syntax_libraries(SwiftSyntaxBuilder PUBLIC
3434
SwiftBasicFormat
3535
SwiftParser
3636
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
IndentationUtils.swift
@@ -8,7 +8,7 @@ add_swift_host_library(SwiftSyntaxMacroExpansion
88
MacroSystem.swift
99
)
1010

11-
target_link_libraries(SwiftSyntaxMacroExpansion PUBLIC
11+
target_link_swift_syntax_libraries(SwiftSyntaxMacroExpansion PUBLIC
1212
SwiftSyntax
1313
SwiftSyntaxMacros
1414
)

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
@@ -24,6 +24,6 @@ add_swift_host_library(SwiftSyntaxMacros
2424
MacroExpansionContext.swift
2525
)
2626

27-
target_link_libraries(SwiftSyntaxMacros PUBLIC
27+
target_link_swift_syntax_libraries(SwiftSyntaxMacros PUBLIC
2828
SwiftSyntaxBuilder
2929
)

cmake/modules/AddSwiftHostLibrary.cmake

Lines changed: 52 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,44 @@
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+
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+
934
# Add a new host library with the given name.
10-
function(add_swift_host_library name)
35+
function(add_swift_syntax_library name)
1136
set(ASHL_SOURCES ${ARGN})
1237

1338
# Create the library target.
1439
add_library(${name} ${ASHL_SOURCES})
1540

16-
# Add this to the list of exported targets.
17-
set_property(GLOBAL APPEND PROPERTY SWIFTSYNTAX_EXPORTS ${name})
18-
1941
# Determine where Swift modules will be built and installed.
2042
set(module_dir ${CMAKE_LIBRARY_OUTPUT_DIRECTORY})
2143
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")
44+
set(module_file "${module_base}/${SWIFT_HOST_MODULE_TRIPLE}.swiftmodule")
45+
set(module_interface_file "${module_base}/${SWIFT_HOST_MODULE_TRIPLE}.swiftinterface")
46+
set(module_sourceinfo_file "${module_base}/${SWIFT_HOST_MODULE_TRIPLE}.swiftsourceinfo")
2547

2648
# Add a custom target to create the module directory.
2749
add_custom_command(
@@ -61,6 +83,12 @@ function(add_swift_host_library name)
6183
$<$<COMPILE_LANGUAGE:Swift>:-wmo>)
6284
endif()
6385

86+
if(LLVM_USE_LINKER)
87+
target_link_options(${name} PRIVATE
88+
"-use-ld=${LLVM_USE_LINKER}"
89+
)
90+
endif()
91+
6492
# NOTE: workaround for CMake not setting up include flags yet
6593
set_target_properties(${name} PROPERTIES
6694
INTERFACE_INCLUDE_DIRECTORIES ${module_dir}
@@ -83,18 +111,22 @@ function(add_swift_host_library name)
83111
endif()
84112
endif()
85113

86-
# Install this target
87-
install(TARGETS ${name}
88-
EXPORT SwiftSyntaxTargets
89-
ARCHIVE DESTINATION lib/${SWIFT_HOST_LIBRARIES_SUBDIRECTORY}
90-
LIBRARY DESTINATION lib/${SWIFT_HOST_LIBRARIES_SUBDIRECTORY}
91-
RUNTIME DESTINATION bin
92-
)
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+
)
93122

94-
# Install the module files.
95-
install(
96-
DIRECTORY ${module_base}
97-
DESTINATION lib/${SWIFT_HOST_LIBRARIES_SUBDIRECTORY}
98-
FILES_MATCHING PATTERN "*.swiftinterface"
99-
)
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()
100132
endfunction()

0 commit comments

Comments
 (0)