Skip to content

Commit 45b5066

Browse files
committed
[CMake] Eliminate the use of Darwin/Glibc in the CMake build.
Reduce platform dependencies by going single-threaded-only when building within the compiler (which is hopelessly single-threaded anyway). This helps eliminate cyclic build dependencies.
1 parent 32482ba commit 45b5066

File tree

5 files changed

+28
-3
lines changed

5 files changed

+28
-3
lines changed

CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,16 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
2323

2424
set(CMAKE_MACOSX_RPATH YES)
2525

26+
# Ensure that we do not link the _StringProcessing module.
2627
add_compile_options(
2728
$<$<COMPILE_LANGUAGE:Swift>:-Xfrontend>
2829
$<$<COMPILE_LANGUAGE:Swift>:-disable-implicit-string-processing-module-import>)
2930

31+
# Force single-threaded builds to eliminate the Darwin dependency.
32+
add_compile_definitions(
33+
$<$<COMPILE_LANGUAGE:Swift>:SWIFT_SYNTAX_ALWAYS_SINGLE_THREADED>
34+
)
35+
3036
add_subdirectory(Sources)
3137

3238
export(EXPORT SwiftSyntaxTargets

Sources/SwiftSyntax/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ add_library(SwiftSyntax STATIC
1111
BumpPtrAllocator.swift
1212
CommonAncestor.swift
1313
IncrementalParseTransition.swift
14-
PlatformMutex.swift
14+
# PlatformMutex.swift is intentionally excluded because it brings in
15+
# platform dependencies we don't want within the CMake build, which is
16+
# trying to minimize such dependencies.
1517
SourceLength.swift
1618
SourceLocation.swift
1719
SourcePresence.swift

Sources/SwiftSyntax/SyntaxArena.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,43 +28,55 @@ public class SyntaxArena {
2828
private var hasParent: Bool
2929
private var parseTriviaFunction: ParseTriviaFunction
3030

31+
#if !SWIFT_SYNTAX_ALWAYS_SINGLE_THREADED
3132
/// Thread safe guard.
3233
private let lock: PlatformMutex
3334
private var singleThreadMode: Bool
35+
#endif
3436

3537
@_spi(RawSyntax)
3638
public init(parseTriviaFunction: @escaping ParseTriviaFunction) {
3739
self.allocator = BumpPtrAllocator()
40+
#if !SWIFT_SYNTAX_ALWAYS_SINGLE_THREADED
3841
self.lock = PlatformMutex(allocator: self.allocator)
3942
self.singleThreadMode = false
43+
#endif
4044
self.children = []
4145
self.sourceBuffer = .init(start: nil, count: 0)
4246
self.hasParent = false
4347
self.parseTriviaFunction = parseTriviaFunction
4448
}
4549

4650
deinit {
51+
#if !SWIFT_SYNTAX_ALWAYS_SINGLE_THREADED
4752
// Make sure we give the platform lock a chance to deinitialize any
4853
// memory it used.
4954
lock.deinitialize()
55+
#endif
5056
}
5157

5258
public convenience init() {
5359
self.init(parseTriviaFunction: _defaultParseTriviaFunction(_:_:))
5460
}
5561

5662
private func withGuard<R>(_ body: () throws -> R) rethrows -> R {
63+
#if SWIFT_SYNTAX_ALWAYS_SINGLE_THREADED
64+
return try body()
65+
#else
5766
if self.singleThreadMode {
5867
return try body()
5968
} else {
6069
return try self.lock.withGuard(body: body)
6170
}
71+
#endif
6272
}
6373

6474
public func assumingSingleThread<R>(body: () throws -> R) rethrows -> R {
75+
#if !SWIFT_SYNTAX_ALWAYS_SINGLE_THREADED
6576
let oldValue = self.singleThreadMode
6677
defer { self.singleThreadMode = oldValue }
6778
self.singleThreadMode = true
79+
#endif
6880
return try body()
6981
}
7082

Sources/SwiftSyntax/SyntaxText.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13+
#if !SWIFT_SYNTAX_ALWAYS_SINGLE_THREADED
1314
#if canImport(Darwin)
1415
@_implementationOnly import Darwin
1516
#elseif canImport(Glibc)
1617
@_implementationOnly import Glibc
1718
#endif
19+
#endif
1820

1921
/// Represent a string.
2022
///
@@ -222,7 +224,10 @@ private func compareMemory(
222224
_ s1: UnsafePointer<UInt8>, _ s2: UnsafePointer<UInt8>, _ count: Int
223225
) -> Bool {
224226
assert(count >= 0)
225-
#if canImport(Darwin)
227+
#if SWIFT_SYNTAX_ALWAYS_SINGLE_THREADED
228+
return UnsafeBufferPointer(start: s1, count: count)
229+
.elementsEqual(UnsafeBufferPointer(start: s2, count: count))
230+
#elseif canImport(Darwin)
226231
return Darwin.memcmp(s1, s2, count) == 0
227232
#elseif canImport(Glibc)
228233
return Glibc.memcmp(s1, s2, count) == 0

Tests/SwiftParserTest/LinkageTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ final class LinkageTest: XCTestCase {
3232
.library("-lswiftCompatibility56", condition: .mayBeAbsent("Starting in Xcode 14 this library is not always autolinked")),
3333
.library("-lswiftCompatibilityConcurrency"),
3434
.library("-lswiftCore"),
35-
.library("-lswiftDarwin"),
35+
.library("-lswiftDarwin", condition: .mayBeAbsent("Not present when building inside the compiler"),
3636
.library("-lswiftSwiftOnoneSupport", condition: .when(configuration: .debug)),
3737
.library("-lswift_Concurrency"),
3838
.library("-lswift_StringProcessing", condition: .mayBeAbsent("Starting in Xcode 14 this library is not always autolinked")),

0 commit comments

Comments
 (0)