Skip to content

Commit fc2b5f2

Browse files
authored
Merge pull request #1036 from DougGregor/cmake-reduce-build-dependencies
[CMake] Reduce build dependencies
2 parents 1c4f45b + bf144cf commit fc2b5f2

File tree

5 files changed

+47
-3
lines changed

5 files changed

+47
-3
lines changed

CMakeLists.txt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,31 @@ 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. But we can
27+
# only pass this flag for new-enough compilers that support it.
28+
file(WRITE "${CMAKE_BINARY_DIR}/tmp/empty-check-string-processing.swift" "")
29+
execute_process(
30+
COMMAND
31+
"${CMAKE_Swift_COMPILER}"
32+
-Xfrontend -disable-implicit-string-processing-module-import
33+
-c - -o /dev/null
34+
INPUT_FILE
35+
"${CMAKE_BINARY_DIR}/tmp/empty-check-string-processing.swift"
36+
OUTPUT_QUIET ERROR_QUIET
37+
RESULT_VARIABLE
38+
SWIFT_SUPPORTS_DISABLE_IMPLICIT_STRING_PROCESSING_MODULE_IMPORT)
39+
if (NOT SWIFT_SUPPORTS_DISABLE_IMPLICIT_STRING_PROCESSING_MODULE_IMPORT)
40+
add_compile_options(
41+
$<$<COMPILE_LANGUAGE:Swift>:-Xfrontend>
42+
$<$<COMPILE_LANGUAGE:Swift>:-disable-implicit-string-processing-module-import>)
43+
endif()
44+
45+
# Force single-threaded-only syntax trees to eliminate the Darwin
46+
# dependency in the compiler.
47+
add_compile_definitions(
48+
$<$<COMPILE_LANGUAGE:Swift>:SWIFT_SYNTAX_ALWAYS_SINGLE_THREADED>
49+
)
50+
2651
add_subdirectory(Sources)
2752

2853
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)