Skip to content

Commit 8bf8ad3

Browse files
committed
Add option to build a single dynamic library containing all swift-syntax modules
My previous approach of building each product as a dynamic library did not work because SwiftPM includes symbols from all dependencies in each `.dll`. Because of this, we ended up with duplicate symbols for each type in eg. SwiftSyntax at runtime. Build a single `.dll` with all required products instead to avoid that issue. To enforce that this is an internal target that should not be relied upon, only enable it when an environment variable is set.
1 parent c00cc6a commit 8bf8ad3

File tree

2 files changed

+44
-28
lines changed

2 files changed

+44
-28
lines changed

Contributor Documentation/Environment variables.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ The following environment variables can be used to control some behavior in swif
77
- `SWIFT_BUILD_SCRIPT_ENVIRONMENT`: Enable assertions in release builds and remove the dependency of swift-syntax on os_log.
88
- `SWIFTCI_USE_LOCAL_DEPS`: Assume that all of SourceKit-LSP’s dependencies are checked out next to it and use those instead of cloning the repositories. Primarily intended for CI environments that check out related branches.
99
- `SWIFTPARSER_ENABLE_ALTERNATE_TOKEN_INTROSPECTION`: Mutate the input of `assertParse` test cases. See [CONTRIBUTING](../CONTRIBUTING.md) for more information.
10-
- `SWIFTSYNTAX_BUILD_DYNAMIC_LIBRARIES`: Build all libraries as *dynamic* instead of *automatic*. This allows us to build swift-syntax as dynamic libraries, which in turn allows us to build SourceKit-LSP using SwiftPM on Windows. Linking swift-syntax statically into sourcekit-lsp exceeds the maximum number of exported symbols on Windows.
10+
- `SWIFTSYNTAX_BUILD_DYNAMIC_LIBRARY`: Build a single dynamic library containing all swift-syntax module. This allows us to build swift-syntax as dynamic libraries, which in turn allows us to build SourceKit-LSP using SwiftPM on Windows. Linking swift-syntax statically into sourcekit-lsp exceeds the maximum number of exported symbols on Windows.
1111
- `SWIFTSYNTAX_ENABLE_RAWSYNTAX_VALIDATION`: Check that the layout of the syntax tree is correct when creating or modifying nodes. See [CONTRIBUTING](../CONTRIBUTING.md) for more information.
1212

1313
## Testing

Package.swift

Lines changed: 43 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,46 @@
33
import Foundation
44
import PackageDescription
55

6+
var products: [Product] = [
7+
.library(name: "SwiftBasicFormat", targets: ["SwiftBasicFormat"]),
8+
.library(name: "SwiftCompilerPlugin", targets: ["SwiftCompilerPlugin"]),
9+
.library(name: "SwiftDiagnostics", targets: ["SwiftDiagnostics"]),
10+
.library(name: "SwiftIDEUtils", targets: ["SwiftIDEUtils"]),
11+
.library(name: "SwiftIfConfig", targets: ["SwiftIfConfig"]),
12+
.library(name: "SwiftLexicalLookup", targets: ["SwiftLexicalLookup"]),
13+
.library(name: "SwiftOperators", targets: ["SwiftOperators"]),
14+
.library(name: "SwiftParser", targets: ["SwiftParser"]),
15+
.library(name: "SwiftParserDiagnostics", targets: ["SwiftParserDiagnostics"]),
16+
.library(name: "SwiftRefactor", targets: ["SwiftRefactor"]),
17+
.library(name: "SwiftSyntax", targets: ["SwiftSyntax"]),
18+
.library(name: "SwiftSyntaxBuilder", targets: ["SwiftSyntaxBuilder"]),
19+
.library(name: "SwiftSyntaxMacros", targets: ["SwiftSyntaxMacros"]),
20+
.library(name: "SwiftSyntaxMacroExpansion", targets: ["SwiftSyntaxMacroExpansion"]),
21+
.library(name: "SwiftSyntaxMacrosTestSupport", targets: ["SwiftSyntaxMacrosTestSupport"]),
22+
.library(name: "SwiftSyntaxMacrosGenericTestSupport", targets: ["SwiftSyntaxMacrosGenericTestSupport"]),
23+
.library(name: "_SwiftCompilerPluginMessageHandling", targets: ["SwiftCompilerPluginMessageHandling"]),
24+
.library(name: "_SwiftLibraryPluginProvider", targets: ["SwiftLibraryPluginProvider"]),
25+
]
26+
27+
if buildDynamicLibrary {
28+
products.append(
29+
.library(
30+
name: "_SwiftSyntaxDynamic",
31+
type: .dynamic,
32+
targets: [
33+
"SwiftBasicFormat",
34+
"SwiftDiagnostics",
35+
"SwiftIDEUtils",
36+
"SwiftParser",
37+
"SwiftParserDiagnostics",
38+
"SwiftRefactor",
39+
"SwiftSyntax",
40+
"SwiftSyntaxBuilder",
41+
]
42+
)
43+
)
44+
}
45+
646
let package = Package(
747
name: "swift-syntax",
848
platforms: [
@@ -12,26 +52,7 @@ let package = Package(
1252
.watchOS(.v6),
1353
.macCatalyst(.v13),
1454
],
15-
products: [
16-
.library(name: "SwiftBasicFormat", type: type, targets: ["SwiftBasicFormat"]),
17-
.library(name: "SwiftCompilerPlugin", type: type, targets: ["SwiftCompilerPlugin"]),
18-
.library(name: "SwiftDiagnostics", type: type, targets: ["SwiftDiagnostics"]),
19-
.library(name: "SwiftIDEUtils", type: type, targets: ["SwiftIDEUtils"]),
20-
.library(name: "SwiftIfConfig", type: type, targets: ["SwiftIfConfig"]),
21-
.library(name: "SwiftLexicalLookup", type: type, targets: ["SwiftLexicalLookup"]),
22-
.library(name: "SwiftOperators", type: type, targets: ["SwiftOperators"]),
23-
.library(name: "SwiftParser", type: type, targets: ["SwiftParser"]),
24-
.library(name: "SwiftParserDiagnostics", type: type, targets: ["SwiftParserDiagnostics"]),
25-
.library(name: "SwiftRefactor", type: type, targets: ["SwiftRefactor"]),
26-
.library(name: "SwiftSyntax", type: type, targets: ["SwiftSyntax"]),
27-
.library(name: "SwiftSyntaxBuilder", type: type, targets: ["SwiftSyntaxBuilder"]),
28-
.library(name: "SwiftSyntaxMacros", type: type, targets: ["SwiftSyntaxMacros"]),
29-
.library(name: "SwiftSyntaxMacroExpansion", type: type, targets: ["SwiftSyntaxMacroExpansion"]),
30-
.library(name: "SwiftSyntaxMacrosTestSupport", type: type, targets: ["SwiftSyntaxMacrosTestSupport"]),
31-
.library(name: "SwiftSyntaxMacrosGenericTestSupport", type: type, targets: ["SwiftSyntaxMacrosGenericTestSupport"]),
32-
.library(name: "_SwiftCompilerPluginMessageHandling", type: type, targets: ["SwiftCompilerPluginMessageHandling"]),
33-
.library(name: "_SwiftLibraryPluginProvider", type: type, targets: ["SwiftLibraryPluginProvider"]),
34-
],
55+
products: products,
3556
targets: [
3657
// MARK: - Internal helper targets
3758
.target(
@@ -407,17 +428,12 @@ var rawSyntaxValidation: Bool { hasEnvironmentVariable("SWIFTSYNTAX_ENABLE_RAWSY
407428
/// See CONTRIBUTING.md for more information
408429
var alternateTokenIntrospection: Bool { hasEnvironmentVariable("SWIFTPARSER_ENABLE_ALTERNATE_TOKEN_INTROSPECTION") }
409430

410-
/// The types of libraries to build.
431+
/// Whether to build a single dynamic library containing all swift-syntax module.
411432
///
412433
/// This allows us to build swift-syntax as dynamic libraries, which in turn allows us to build SourceKit-LSP using
413434
/// SwiftPM on Windows. Linking swift-syntax statically into sourcekit-lsp exceeds the maximum number of exported
414435
/// symbols on Windows.
415-
var type: Product.Library.LibraryType? {
416-
if hasEnvironmentVariable("SWIFTSYNTAX_BUILD_DYNAMIC_LIBRARIES") {
417-
return .dynamic
418-
}
419-
return nil
420-
}
436+
var buildDynamicLibrary: Bool { hasEnvironmentVariable("SWIFTSYNTAX_BUILD_DYNAMIC_LIBRARY") }
421437

422438
// MARK: - Compute custom build settings
423439

0 commit comments

Comments
 (0)