Skip to content

Commit e2b45df

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 e2b45df

File tree

2 files changed

+46
-28
lines changed

2 files changed

+46
-28
lines changed

Contributor Documentation/Environment variables.md

+1-1
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`: Instead of building object files for all modules to be statically linked, build a single dynamic library. 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

+45-27
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,48 @@
33
import Foundation
44
import PackageDescription
55

6+
let products: [Product]
7+
8+
if buildDynamicLibrary {
9+
products = [
10+
.library(
11+
name: "_SwiftSyntaxDynamic",
12+
type: .dynamic,
13+
targets: [
14+
"SwiftBasicFormat",
15+
"SwiftDiagnostics",
16+
"SwiftIDEUtils",
17+
"SwiftParser",
18+
"SwiftParserDiagnostics",
19+
"SwiftRefactor",
20+
"SwiftSyntax",
21+
"SwiftSyntaxBuilder",
22+
]
23+
)
24+
]
25+
} else {
26+
products = [
27+
.library(name: "SwiftBasicFormat", targets: ["SwiftBasicFormat"]),
28+
.library(name: "SwiftCompilerPlugin", targets: ["SwiftCompilerPlugin"]),
29+
.library(name: "SwiftDiagnostics", targets: ["SwiftDiagnostics"]),
30+
.library(name: "SwiftIDEUtils", targets: ["SwiftIDEUtils"]),
31+
.library(name: "SwiftIfConfig", targets: ["SwiftIfConfig"]),
32+
.library(name: "SwiftLexicalLookup", targets: ["SwiftLexicalLookup"]),
33+
.library(name: "SwiftOperators", targets: ["SwiftOperators"]),
34+
.library(name: "SwiftParser", targets: ["SwiftParser"]),
35+
.library(name: "SwiftParserDiagnostics", targets: ["SwiftParserDiagnostics"]),
36+
.library(name: "SwiftRefactor", targets: ["SwiftRefactor"]),
37+
.library(name: "SwiftSyntax", targets: ["SwiftSyntax"]),
38+
.library(name: "SwiftSyntaxBuilder", targets: ["SwiftSyntaxBuilder"]),
39+
.library(name: "SwiftSyntaxMacros", targets: ["SwiftSyntaxMacros"]),
40+
.library(name: "SwiftSyntaxMacroExpansion", targets: ["SwiftSyntaxMacroExpansion"]),
41+
.library(name: "SwiftSyntaxMacrosTestSupport", targets: ["SwiftSyntaxMacrosTestSupport"]),
42+
.library(name: "SwiftSyntaxMacrosGenericTestSupport", targets: ["SwiftSyntaxMacrosGenericTestSupport"]),
43+
.library(name: "_SwiftCompilerPluginMessageHandling", targets: ["SwiftCompilerPluginMessageHandling"]),
44+
.library(name: "_SwiftLibraryPluginProvider", targets: ["SwiftLibraryPluginProvider"]),
45+
]
46+
}
47+
648
let package = Package(
749
name: "swift-syntax",
850
platforms: [
@@ -12,26 +54,7 @@ let package = Package(
1254
.watchOS(.v6),
1355
.macCatalyst(.v13),
1456
],
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-
],
57+
products: products,
3558
targets: [
3659
// MARK: - Internal helper targets
3760
.target(
@@ -407,17 +430,12 @@ var rawSyntaxValidation: Bool { hasEnvironmentVariable("SWIFTSYNTAX_ENABLE_RAWSY
407430
/// See CONTRIBUTING.md for more information
408431
var alternateTokenIntrospection: Bool { hasEnvironmentVariable("SWIFTPARSER_ENABLE_ALTERNATE_TOKEN_INTROSPECTION") }
409432

410-
/// The types of libraries to build.
433+
/// Instead of building object files for all modules to be statically linked, build a single dynamic library.
411434
///
412435
/// This allows us to build swift-syntax as dynamic libraries, which in turn allows us to build SourceKit-LSP using
413436
/// SwiftPM on Windows. Linking swift-syntax statically into sourcekit-lsp exceeds the maximum number of exported
414437
/// symbols on Windows.
415-
var type: Product.Library.LibraryType? {
416-
if hasEnvironmentVariable("SWIFTSYNTAX_BUILD_DYNAMIC_LIBRARIES") {
417-
return .dynamic
418-
}
419-
return nil
420-
}
438+
var buildDynamicLibrary: Bool { hasEnvironmentVariable("SWIFTSYNTAX_BUILD_DYNAMIC_LIBRARY") }
421439

422440
// MARK: - Compute custom build settings
423441

0 commit comments

Comments
 (0)