Skip to content

Commit 5c881bc

Browse files
Fix Linux compile with --static-swift-stdlib (#80)
* Revert "remove #if DEBUG guard" This reverts commit cfdddbc. * Change #if DEBUG to #if canImport(XCTest) * Update proper location * Whitespace * Update stale comment * fixes and ci * Update static swift stdlib invocation * Run build-for-static-stdlib-docker on 5.8 * Add setup-swift with 5.8.0 to static-stdlib action * Install libcurl4-openssl-dev * Install •harder• --------- Co-authored-by: Brandon Williams <[email protected]>
1 parent 0f34848 commit 5c881bc

File tree

3 files changed

+86
-51
lines changed

3 files changed

+86
-51
lines changed

.github/workflows/ci.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,18 @@ jobs:
4949
- uses: actions/checkout@v3
5050
- name: Run tests
5151
run: make test-swift
52+
53+
static-stdlib:
54+
strategy:
55+
matrix:
56+
os: [ubuntu-20.04]
57+
runs-on: ${{ matrix.os }}
58+
steps:
59+
- uses: swift-actions/setup-swift@v1
60+
with:
61+
swift-version: '5.8.0'
62+
- name: Install dependencies
63+
run: sudo apt-get install -y libcurl4-openssl-dev
64+
- uses: actions/checkout@v3
65+
- name: Build for static-stdlib
66+
run: make build-for-static-stdlib

Makefile

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ test-linux:
3535
swift:5.7-focal \
3636
bash -c 'apt-get update && apt-get -y install make && make test-swift'
3737

38+
build-for-static-stdlib:
39+
@swift build -c debug --static-swift-stdlib
40+
@swift build -c release --static-swift-stdlib
41+
3842
test-integration:
3943
xcodebuild test \
4044
-scheme "Integration" \
@@ -47,6 +51,18 @@ build-for-library-evolution:
4751
-Xswiftc -emit-module-interface \
4852
-Xswiftc -enable-library-evolution
4953

54+
build-for-static-stdlib-docker:
55+
@docker run \
56+
-v "$(PWD):$(PWD)" \
57+
-w "$(PWD)" \
58+
swift:5.8-focal \
59+
bash -c "swift build -c debug --static-swift-stdlib"
60+
@docker run \
61+
-v "$(PWD):$(PWD)" \
62+
-w "$(PWD)" \
63+
swift:5.8-focal \
64+
bash -c "swift build -c release --static-swift-stdlib"
65+
5066
format:
5167
swift format \
5268
--ignore-unparsable-files \

Sources/Dependencies/DependencyValues.swift

Lines changed: 55 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,9 @@ public struct DependencyValues: Sendable {
9595
/// provide access only to default values. Instead, you rely on the dependency values' instance
9696
/// that the library manages for you when you use the ``Dependency`` property wrapper.
9797
public init() {
98-
_ = setUpTestObservers
98+
#if canImport(XCTest)
99+
_ = setUpTestObservers
100+
#endif
99101
}
100102

101103
/// Accesses the dependency value associated with a custom key.
@@ -348,61 +350,63 @@ private final class CachedValues: @unchecked Sendable {
348350
}
349351

350352
// NB: We cannot statically link/load XCTest on Apple platforms, so we dynamically load things
351-
// instead and we limit this to debug builds to avoid App Store binary rejection.
352-
#if !canImport(ObjectiveC)
353-
import XCTest
354-
#endif
353+
// instead on platforms where XCTest is available.
354+
#if canImport(XCTest)
355+
#if !canImport(ObjectiveC)
356+
import XCTest
357+
#endif
355358

356-
private let setUpTestObservers: Void = {
357-
if _XCTIsTesting {
358-
#if canImport(ObjectiveC)
359-
DispatchQueue.mainSync {
360-
guard
361-
let XCTestObservation = objc_getProtocol("XCTestObservation"),
362-
let XCTestObservationCenter = NSClassFromString("XCTestObservationCenter"),
363-
let XCTestObservationCenter = XCTestObservationCenter as Any as? NSObjectProtocol,
364-
let XCTestObservationCenterShared =
365-
XCTestObservationCenter
366-
.perform(Selector(("sharedTestObservationCenter")))?
367-
.takeUnretainedValue()
368-
else { return }
369-
let testCaseWillStartBlock: @convention(block) (AnyObject) -> Void = { _ in
370-
DependencyValues._current.cachedValues.cached = [:]
359+
private let setUpTestObservers: Void = {
360+
if _XCTIsTesting {
361+
#if canImport(ObjectiveC)
362+
DispatchQueue.mainSync {
363+
guard
364+
let XCTestObservation = objc_getProtocol("XCTestObservation"),
365+
let XCTestObservationCenter = NSClassFromString("XCTestObservationCenter"),
366+
let XCTestObservationCenter = XCTestObservationCenter as Any as? NSObjectProtocol,
367+
let XCTestObservationCenterShared =
368+
XCTestObservationCenter
369+
.perform(Selector(("sharedTestObservationCenter")))?
370+
.takeUnretainedValue()
371+
else { return }
372+
let testCaseWillStartBlock: @convention(block) (AnyObject) -> Void = { _ in
373+
DependencyValues._current.cachedValues.cached = [:]
374+
}
375+
let testCaseWillStartImp = imp_implementationWithBlock(testCaseWillStartBlock)
376+
class_addMethod(
377+
TestObserver.self, Selector(("testCaseWillStart:")), testCaseWillStartImp, nil)
378+
class_addProtocol(TestObserver.self, XCTestObservation)
379+
_ =
380+
XCTestObservationCenterShared
381+
.perform(Selector(("addTestObserver:")), with: TestObserver())
371382
}
372-
let testCaseWillStartImp = imp_implementationWithBlock(testCaseWillStartBlock)
373-
class_addMethod(
374-
TestObserver.self, Selector(("testCaseWillStart:")), testCaseWillStartImp, nil)
375-
class_addProtocol(TestObserver.self, XCTestObservation)
376-
_ =
377-
XCTestObservationCenterShared
378-
.perform(Selector(("addTestObserver:")), with: TestObserver())
383+
#else
384+
XCTestObservationCenter.shared.addTestObserver(TestObserver())
385+
#endif
386+
}
387+
}()
388+
389+
#if canImport(ObjectiveC)
390+
private final class TestObserver: NSObject {}
391+
#else
392+
private final class TestObserver: NSObject, XCTestObservation {
393+
func testCaseWillStart(_ testCase: XCTestCase) {
394+
DependencyValues._current.cachedValues.cached = [:]
379395
}
380-
#else
381-
XCTestObservationCenter.shared.addTestObserver(TestObserver())
382-
#endif
383-
}
384-
}()
385-
386-
#if canImport(ObjectiveC)
387-
private final class TestObserver: NSObject {}
388-
#else
389-
private final class TestObserver: NSObject, XCTestObservation {
390-
func testCaseWillStart(_ testCase: XCTestCase) {
391-
DependencyValues._current.cachedValues.cached = [:]
392396
}
393-
}
394-
#endif
397+
#endif
395398

396-
extension DispatchQueue {
397-
private static let key = DispatchSpecificKey<UInt8>()
398-
private static let value: UInt8 = 0
399+
extension DispatchQueue {
400+
private static let key = DispatchSpecificKey<UInt8>()
401+
private static let value: UInt8 = 0
399402

400-
fileprivate static func mainSync<R>(execute block: @Sendable () -> R) -> R {
401-
Self.main.setSpecific(key: Self.key, value: Self.value)
402-
if getSpecific(key: Self.key) == Self.value {
403-
return block()
404-
} else {
405-
return Self.main.sync(execute: block)
403+
fileprivate static func mainSync<R>(execute block: @Sendable () -> R) -> R {
404+
Self.main.setSpecific(key: Self.key, value: Self.value)
405+
if getSpecific(key: Self.key) == Self.value {
406+
return block()
407+
} else {
408+
return Self.main.sync(execute: block)
409+
}
406410
}
407411
}
408-
}
412+
#endif

0 commit comments

Comments
 (0)