Skip to content

[CMake] Reduce build dependencies #1036

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,31 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)

set(CMAKE_MACOSX_RPATH YES)

# Ensure that we do not link the _StringProcessing module. But we can
# only pass this flag for new-enough compilers that support it.
file(WRITE "${CMAKE_BINARY_DIR}/tmp/empty-check-string-processing.swift" "")
execute_process(
COMMAND
"${CMAKE_Swift_COMPILER}"
-Xfrontend -disable-implicit-string-processing-module-import
-c - -o /dev/null
INPUT_FILE
"${CMAKE_BINARY_DIR}/tmp/empty-check-string-processing.swift"
OUTPUT_QUIET ERROR_QUIET
RESULT_VARIABLE
SWIFT_SUPPORTS_DISABLE_IMPLICIT_STRING_PROCESSING_MODULE_IMPORT)
if (NOT SWIFT_SUPPORTS_DISABLE_IMPLICIT_STRING_PROCESSING_MODULE_IMPORT)
add_compile_options(
$<$<COMPILE_LANGUAGE:Swift>:-Xfrontend>
$<$<COMPILE_LANGUAGE:Swift>:-disable-implicit-string-processing-module-import>)
endif()

# Force single-threaded-only syntax trees to eliminate the Darwin
# dependency in the compiler.
add_compile_definitions(
$<$<COMPILE_LANGUAGE:Swift>:SWIFT_SYNTAX_ALWAYS_SINGLE_THREADED>
)

add_subdirectory(Sources)

export(EXPORT SwiftSyntaxTargets
Expand Down
4 changes: 3 additions & 1 deletion Sources/SwiftSyntax/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ add_library(SwiftSyntax STATIC
BumpPtrAllocator.swift
CommonAncestor.swift
IncrementalParseTransition.swift
PlatformMutex.swift
# PlatformMutex.swift is intentionally excluded because it brings in
# platform dependencies we don't want within the CMake build, which is
# trying to minimize such dependencies.
SourceLength.swift
SourceLocation.swift
SourcePresence.swift
Expand Down
12 changes: 12 additions & 0 deletions Sources/SwiftSyntax/SyntaxArena.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,43 +28,55 @@ public class SyntaxArena {
private var hasParent: Bool
private var parseTriviaFunction: ParseTriviaFunction

#if !SWIFT_SYNTAX_ALWAYS_SINGLE_THREADED
/// Thread safe guard.
private let lock: PlatformMutex
private var singleThreadMode: Bool
#endif

@_spi(RawSyntax)
public init(parseTriviaFunction: @escaping ParseTriviaFunction) {
self.allocator = BumpPtrAllocator()
#if !SWIFT_SYNTAX_ALWAYS_SINGLE_THREADED
self.lock = PlatformMutex(allocator: self.allocator)
self.singleThreadMode = false
#endif
self.children = []
self.sourceBuffer = .init(start: nil, count: 0)
self.hasParent = false
self.parseTriviaFunction = parseTriviaFunction
}

deinit {
#if !SWIFT_SYNTAX_ALWAYS_SINGLE_THREADED
// Make sure we give the platform lock a chance to deinitialize any
// memory it used.
lock.deinitialize()
#endif
}

public convenience init() {
self.init(parseTriviaFunction: _defaultParseTriviaFunction(_:_:))
}

private func withGuard<R>(_ body: () throws -> R) rethrows -> R {
#if SWIFT_SYNTAX_ALWAYS_SINGLE_THREADED
return try body()
#else
if self.singleThreadMode {
return try body()
} else {
return try self.lock.withGuard(body: body)
}
#endif
}

public func assumingSingleThread<R>(body: () throws -> R) rethrows -> R {
#if !SWIFT_SYNTAX_ALWAYS_SINGLE_THREADED
let oldValue = self.singleThreadMode
defer { self.singleThreadMode = oldValue }
self.singleThreadMode = true
#endif
return try body()
}

Expand Down
7 changes: 6 additions & 1 deletion Sources/SwiftSyntax/SyntaxText.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@
//
//===----------------------------------------------------------------------===//

#if !SWIFT_SYNTAX_ALWAYS_SINGLE_THREADED
#if canImport(Darwin)
@_implementationOnly import Darwin
#elseif canImport(Glibc)
@_implementationOnly import Glibc
#endif
#endif

/// Represent a string.
///
Expand Down Expand Up @@ -222,7 +224,10 @@ private func compareMemory(
_ s1: UnsafePointer<UInt8>, _ s2: UnsafePointer<UInt8>, _ count: Int
) -> Bool {
assert(count >= 0)
#if canImport(Darwin)
#if SWIFT_SYNTAX_ALWAYS_SINGLE_THREADED
return UnsafeBufferPointer(start: s1, count: count)
.elementsEqual(UnsafeBufferPointer(start: s2, count: count))
#elseif canImport(Darwin)
return Darwin.memcmp(s1, s2, count) == 0
#elseif canImport(Glibc)
return Glibc.memcmp(s1, s2, count) == 0
Expand Down
2 changes: 1 addition & 1 deletion Tests/SwiftParserTest/LinkageTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ final class LinkageTest: XCTestCase {
.library("-lswiftCompatibility56", condition: .mayBeAbsent("Starting in Xcode 14 this library is not always autolinked")),
.library("-lswiftCompatibilityConcurrency"),
.library("-lswiftCore"),
.library("-lswiftDarwin"),
.library("-lswiftDarwin", condition: .mayBeAbsent("Not present when building inside the compiler")),
.library("-lswiftSwiftOnoneSupport", condition: .when(configuration: .debug)),
.library("-lswift_Concurrency"),
.library("-lswift_StringProcessing", condition: .mayBeAbsent("Starting in Xcode 14 this library is not always autolinked")),
Expand Down