Skip to content

Commit 52d513a

Browse files
committed
Build swift-syntax as shared libraries instead of static libraries.
This allows the various binaries (swift-frontend, SourceKit, etc.) to share the same code, as well as allowing plugins to link against these shared libraries.
1 parent 5462c0a commit 52d513a

File tree

3 files changed

+51
-23
lines changed

3 files changed

+51
-23
lines changed

lib/ASTGen/CMakeLists.txt

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,8 @@ if (SWIFT_SWIFT_PARSER)
5151
cmake_parse_arguments(ARGS "" "" "PUBLIC" ${ARGN})
5252

5353
foreach(DEPENDENCY ${ARGS_PUBLIC})
54-
# This is a hack to workaround a cmake bug that results in multiple ninja targets producing
55-
# the same file in a downstream SourceKit target. This should use `PUBLIC`, the dependency
56-
# directly (rather than `TARGET_OBJECTS`), and no `add_dependencies`.
5754
target_link_libraries(${TARGET} PRIVATE
58-
$<TARGET_OBJECTS:${DEPENDENCY}>
55+
${DEPENDENCY}
5956
)
6057
add_dependencies(${TARGET} ${DEPENDENCY})
6158

@@ -69,23 +66,54 @@ if (SWIFT_SWIFT_PARSER)
6966
endforeach()
7067
endfunction()
7168

69+
set(SWIFT_SYNTAX_MODULES
70+
SwiftBasicFormat
71+
SwiftParser
72+
SwiftParserDiagnostics
73+
SwiftDiagnostics
74+
SwiftSyntax
75+
SwiftOperators
76+
SwiftSyntaxBuilder
77+
_SwiftSyntaxMacros
78+
SwiftCompilerSupport
79+
)
80+
81+
# Compute the list of SwiftSyntax targets
82+
list(TRANSFORM SWIFT_SYNTAX_MODULES PREPEND "SwiftSyntax::"
83+
OUTPUT_VARIABLE SWIFT_SYNTAX_TARGETS)
84+
7285
# TODO: Change to target_link_libraries when cmake is fixed
7386
force_target_link_libraries(swiftASTGen PUBLIC
74-
SwiftSyntax::SwiftBasicFormat
75-
SwiftSyntax::SwiftParser
76-
SwiftSyntax::SwiftParserDiagnostics
77-
SwiftSyntax::SwiftDiagnostics
78-
SwiftSyntax::SwiftSyntax
79-
SwiftSyntax::SwiftOperators
80-
SwiftSyntax::SwiftSyntaxBuilder
81-
SwiftSyntax::_SwiftSyntaxMacros
82-
SwiftSyntax::SwiftCompilerSupport
87+
${SWIFT_SYNTAX_TARGETS}
8388
)
8489
target_link_libraries(swiftASTGen PUBLIC
8590
swiftAST
8691
swift_CompilerPluginSupport
8792
)
8893

94+
set(SWIFT_SYNTAX_LIBRARIES_SOURCE_DIR
95+
"${SWIFT_PATH_TO_EARLYSWIFTSYNTAX_BUILD_DIR}/lib/swift/host")
96+
set(SWIFT_SYNTAX_LIBRARIES_DEST_DIR
97+
"${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/swift/host")
98+
99+
# Determine the SwiftSyntax shared library files that were built as
100+
# part of earlyswiftsyntax.
101+
list(TRANSFORM SWIFT_SYNTAX_MODULES PREPEND ${CMAKE_SHARED_LIBRARY_PREFIX}
102+
OUTPUT_VARIABLE SWIFT_SYNTAX_SHARED_LIBRARIES)
103+
list(TRANSFORM SWIFT_SYNTAX_SHARED_LIBRARIES APPEND
104+
${CMAKE_SHARED_LIBRARY_SUFFIX}
105+
OUTPUT_VARIABLE SWIFT_SYNTAX_SHARED_LIBRARIES)
106+
107+
# Copy over all of the shared libraries from earlyswiftsyntax so they can
108+
# be found via RPATH.
109+
foreach (sharedlib ${SWIFT_SYNTAX_SHARED_LIBRARIES})
110+
add_custom_command(
111+
TARGET swiftASTGen PRE_BUILD
112+
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${SWIFT_SYNTAX_LIBRARIES_SOURCE_DIR}/${sharedlib} ${SWIFT_SYNTAX_LIBRARIES_DEST_DIR}/${sharedlib}
113+
COMMENT "Copying ${sharedlib}"
114+
)
115+
endforeach()
116+
89117
target_include_directories(swiftASTGen PUBLIC
90118
"${SWIFT_PATH_TO_EARLYSWIFTSYNTAX_BUILD_DIR}/lib/swift/host")
91119

lib/Parse/CMakeLists.txt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,15 @@ if (SWIFT_SWIFT_PARSER)
3838
# )
3939
target_link_libraries(swiftParse
4040
PRIVATE
41-
$<TARGET_OBJECTS:SwiftSyntax::SwiftBasicFormat>
42-
$<TARGET_OBJECTS:SwiftSyntax::SwiftParser>
43-
$<TARGET_OBJECTS:SwiftSyntax::SwiftParserDiagnostics>
44-
$<TARGET_OBJECTS:SwiftSyntax::SwiftDiagnostics>
45-
$<TARGET_OBJECTS:SwiftSyntax::SwiftSyntax>
46-
$<TARGET_OBJECTS:SwiftSyntax::SwiftOperators>
47-
$<TARGET_OBJECTS:SwiftSyntax::SwiftSyntaxBuilder>
48-
$<TARGET_OBJECTS:SwiftSyntax::_SwiftSyntaxMacros>
49-
$<TARGET_OBJECTS:SwiftSyntax::SwiftCompilerSupport>
41+
SwiftSyntax::SwiftBasicFormat
42+
SwiftSyntax::SwiftParser
43+
SwiftSyntax::SwiftParserDiagnostics
44+
SwiftSyntax::SwiftDiagnostics
45+
SwiftSyntax::SwiftSyntax
46+
SwiftSyntax::SwiftOperators
47+
SwiftSyntax::SwiftSyntaxBuilder
48+
SwiftSyntax::_SwiftSyntaxMacros
49+
SwiftSyntax::SwiftCompilerSupport
5050
$<TARGET_OBJECTS:swiftASTGen>
5151
)
5252

utils/swift_build_support/swift_build_support/products/earlyswiftsyntax.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def get_dependencies(cls):
4949
def build(self, host_target):
5050
self.cmake_options.define('CMAKE_BUILD_TYPE:STRING',
5151
self.args.swift_build_variant)
52-
self.cmake_options.define('BUILD_SHARED_LIBS:STRING', 'NO')
52+
self.cmake_options.define('BUILD_SHARED_LIBS:STRING', 'YES')
5353

5454
(platform, arch) = host_target.split('-')
5555

0 commit comments

Comments
 (0)