Skip to content

Commit 73aad0a

Browse files
committed
[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 1060677 commit 73aad0a

File tree

15 files changed

+110
-92
lines changed

15 files changed

+110
-92
lines changed

CMakeLists.txt

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,22 @@ 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")
20-
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}")
27+
if(SWIFT_HOST_LIBRARIES_DEST_DIR)
28+
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${SWIFT_HOST_LIBRARIES_DEST_DIR}")
29+
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${SWIFT_HOST_LIBRARIES_DEST_DIR}")
30+
else()
31+
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib/${SWIFT_HOST_LIBRARIES_SUBDIRECTORY}")
32+
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib/${SWIFT_HOST_LIBRARIES_SUBDIRECTORY}")
33+
endif()
2334

2435
set(CMAKE_MACOSX_RPATH YES)
2536

@@ -48,22 +59,25 @@ if (NOT SWIFT_SUPPORTS_DISABLE_IMPLICIT_STRING_PROCESSING_MODULE_IMPORT)
4859
endif()
4960

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

6882
# Force single-threaded-only syntax trees to eliminate the Darwin
6983
# dependency in the compiler.
@@ -78,9 +92,9 @@ endif()
7892

7993
add_subdirectory(Sources)
8094

81-
export(EXPORT SwiftSyntaxTargets
82-
FILE "${CMAKE_CURRENT_BINARY_DIR}/cmake/SwiftSyntaxTargets.cmake"
83-
NAMESPACE SwiftSyntax::
84-
)
85-
86-
add_subdirectory(cmake/modules)
95+
if(PROJECT_IS_TOP_LEVEL)
96+
export(EXPORT SwiftSyntaxTargets
97+
FILE "${CMAKE_CURRENT_BINARY_DIR}/cmake/SwiftSyntaxTargets.cmake"
98+
NAMESPACE SwiftSyntax::
99+
)
100+
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: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@
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
10+
generated/SyntaxClassification.swift
1011
SwiftIDEUtilsCompatibility.swift
1112
Syntax+Classifications.swift
1213
SyntaxClassification.swift
1314
SyntaxClassifier.swift
1415
)
1516

16-
target_link_libraries(SwiftIDEUtils PUBLIC
17+
target_link_swift_syntax_libraries(SwiftIDEUtils PUBLIC
1718
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
Assert.swift
1212
BumpPtrAllocator.swift

0 commit comments

Comments
 (0)