Skip to content

Commit 17d95a6

Browse files
authored
Avoid unnecessary copies of BoringSSL (#94)
Motivation: When using only the CryptoKit API on Apple platforms we have always thunked through to the CryptoKit interface and implementation. However, we didn't do a thorough job of preventing the BoringSSL target from getting compiled and linked. We can do a better job now, which will save compile times and binary sizes in many cases. Modifications: - Change Package.swift to express a target specific dependency in most cases. - Preserve a development mode which overrides that target specific dependency. - Add the missing compile guards. Results: Smaller binaries and faster compiles on Apple platforms.
1 parent 12a1784 commit 17d95a6

File tree

9 files changed

+90
-18
lines changed

9 files changed

+90
-18
lines changed

Package.swift

Lines changed: 62 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,34 @@
2424

2525
import PackageDescription
2626

27-
let swiftSettings: [SwiftSetting] = [
28-
.define("CRYPTO_IN_SWIFTPM"),
29-
// To develop this on Apple platforms, uncomment this define.
30-
// .define("CRYPTO_IN_SWIFTPM_FORCE_BUILD_API"),
31-
]
27+
// To develop this on Apple platforms, set this to true
28+
let development = false
29+
30+
let swiftSettings: [SwiftSetting]
31+
let dependencies: [Target.Dependency]
32+
if development {
33+
swiftSettings = [
34+
.define("CRYPTO_IN_SWIFTPM"),
35+
.define("CRYPTO_IN_SWIFTPM_FORCE_BUILD_API"),
36+
]
37+
dependencies = [
38+
"CCryptoBoringSSL",
39+
"CCryptoBoringSSLShims"
40+
]
41+
} else {
42+
swiftSettings = [
43+
.define("CRYPTO_IN_SWIFTPM"),
44+
]
45+
let platforms: [Platform] = [
46+
Platform.linux,
47+
Platform.android,
48+
Platform.windows,
49+
]
50+
dependencies = [
51+
.target(name: "CCryptoBoringSSL", condition: .when(platforms: platforms)),
52+
.target(name: "CCryptoBoringSSLShims", condition: .when(platforms: platforms))
53+
]
54+
}
3255

3356
let package = Package(
3457
name: "swift-crypto",
@@ -48,20 +71,42 @@ let package = Package(
4871
dependencies: [],
4972
targets: [
5073
.target(
51-
name: "CCryptoBoringSSL",
52-
cSettings: [
53-
/*
54-
* This define is required on Windows, but because we need older
55-
* versions of SPM, we cannot conditionally define this on Windows
56-
* only. Unconditionally define it instead.
57-
*/
58-
.define("WIN32_LEAN_AND_MEAN"),
59-
]
74+
name: "CCryptoBoringSSL",
75+
exclude: [
76+
"hash.txt",
77+
"include/boringssl_prefix_symbols_nasm.inc",
78+
"CMakeLists.txt",
79+
],
80+
cSettings: [
81+
/*
82+
* This define is required on Windows, but because we need older
83+
* versions of SPM, we cannot conditionally define this on Windows
84+
* only. Unconditionally define it instead.
85+
*/
86+
.define("WIN32_LEAN_AND_MEAN"),
87+
]
88+
),
89+
.target(
90+
name: "CCryptoBoringSSLShims",
91+
dependencies: ["CCryptoBoringSSL"],
92+
exclude: [
93+
"CMakeLists.txt"
94+
]
95+
),
96+
.target(
97+
name: "Crypto",
98+
dependencies: dependencies,
99+
exclude: [
100+
"CMakeLists.txt",
101+
"AEADs/Nonces.swift.gyb",
102+
"Digests/Digests.swift.gyb",
103+
"Key Agreement/ECDH.swift.gyb",
104+
"Signatures/ECDSA.swift.gyb",
105+
],
106+
swiftSettings: swiftSettings
60107
),
61-
.target(name: "CCryptoBoringSSLShims", dependencies: ["CCryptoBoringSSL"]),
62-
.target(name: "Crypto", dependencies: ["CCryptoBoringSSL", "CCryptoBoringSSLShims"], swiftSettings: swiftSettings),
63108
.target(name: "_CryptoExtras", dependencies: ["CCryptoBoringSSL", "CCryptoBoringSSLShims", "Crypto"]),
64-
.target(name: "crypto-shasum", dependencies: ["Crypto"]),
109+
.executableTarget(name: "crypto-shasum", dependencies: ["Crypto"]),
65110
.testTarget(name: "CryptoTests", dependencies: ["Crypto"], swiftSettings: swiftSettings),
66111
.testTarget(name: "_CryptoExtrasTests", dependencies: ["_CryptoExtras"]),
67112
],

Sources/Crypto/Keys/EC/BoringSSL/EllipticCurvePoint_boring.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
// SPDX-License-Identifier: Apache-2.0
1212
//
1313
//===----------------------------------------------------------------------===//
14+
#if (os(macOS) || os(iOS) || os(watchOS) || os(tvOS)) && CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API
15+
@_exported import CryptoKit
16+
#else
1417
@_implementationOnly import CCryptoBoringSSL
1518

1619
/// A wrapper around BoringSSL's EC_POINT with some lifetime management.
@@ -76,3 +79,4 @@ extension EllipticCurvePoint {
7679
return (x: x, y: y)
7780
}
7881
}
82+
#endif // (os(macOS) || os(iOS) || os(watchOS) || os(tvOS)) && CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API

Sources/Crypto/Keys/EC/BoringSSL/EllipticCurve_boring.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
// SPDX-License-Identifier: Apache-2.0
1212
//
1313
//===----------------------------------------------------------------------===//
14+
#if (os(macOS) || os(iOS) || os(watchOS) || os(tvOS)) && CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API
15+
@_exported import CryptoKit
16+
#else
1417
@_implementationOnly import CCryptoBoringSSL
1518

1619
/// A wrapper around BoringSSL's EC_GROUP object that handles reference counting and
@@ -117,3 +120,4 @@ extension BoringSSLEllipticCurveGroup.CurveName {
117120
}
118121
}
119122
}
123+
#endif // (os(macOS) || os(iOS) || os(watchOS) || os(tvOS)) && CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API

Sources/Crypto/Signatures/BoringSSL/ECDSASignature_boring.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
// SPDX-License-Identifier: Apache-2.0
1212
//
1313
//===----------------------------------------------------------------------===//
14+
#if (os(macOS) || os(iOS) || os(watchOS) || os(tvOS)) && CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API
15+
@_exported import CryptoKit
16+
#else
1417
@_implementationOnly import CCryptoBoringSSL
1518
@_implementationOnly import CCryptoBoringSSLShims
1619
import Foundation
@@ -101,3 +104,4 @@ class ECDSASignature {
101104
try body(self._baseSig)
102105
}
103106
}
107+
#endif // (os(macOS) || os(iOS) || os(watchOS) || os(tvOS)) && CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API

Sources/Crypto/Util/BoringSSL/ArbitraryPrecisionInteger_boring.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
// SPDX-License-Identifier: Apache-2.0
1212
//
1313
//===----------------------------------------------------------------------===//
14+
#if (os(macOS) || os(iOS) || os(watchOS) || os(tvOS)) && CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API
15+
@_exported import CryptoKit
16+
#else
1417
@_implementationOnly import CCryptoBoringSSL
1518
@_implementationOnly import CCryptoBoringSSLShims
1619
import Foundation
@@ -439,3 +442,4 @@ extension ArbitraryPrecisionInteger: CustomDebugStringConvertible {
439442
return String(decoding: UnsafeBufferPointer(start: stringPointer, count: length), as: Unicode.UTF8.self)
440443
}
441444
}
445+
#endif // (os(macOS) || os(iOS) || os(watchOS) || os(tvOS)) && CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API

Sources/Crypto/Util/BoringSSL/CryptoKitErrors_boring.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
// SPDX-License-Identifier: Apache-2.0
1212
//
1313
//===----------------------------------------------------------------------===//
14+
#if (os(macOS) || os(iOS) || os(watchOS) || os(tvOS)) && CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API
15+
@_exported import CryptoKit
16+
#else
1417
@_implementationOnly import CCryptoBoringSSL
1518

1619
extension CryptoKitError {
@@ -20,3 +23,4 @@ extension CryptoKitError {
2023
.underlyingCoreCryptoError(error: Int32(bitPattern: CCryptoBoringSSL_ERR_get_error()))
2124
}
2225
}
26+
#endif // (os(macOS) || os(iOS) || os(watchOS) || os(tvOS)) && CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API

Sources/Crypto/Util/BoringSSL/FiniteFieldArithmeticContext_boring.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
// SPDX-License-Identifier: Apache-2.0
1212
//
1313
//===----------------------------------------------------------------------===//
14+
#if (os(macOS) || os(iOS) || os(watchOS) || os(tvOS)) && CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API
15+
@_exported import CryptoKit
16+
#else
1417
@_implementationOnly import CCryptoBoringSSL
1518
import Foundation
1619

@@ -153,3 +156,4 @@ extension FiniteFieldArithmeticContext {
153156
return try ArbitraryPrecisionInteger(copying: actualOutputPointer)
154157
}
155158
}
159+
#endif // (os(macOS) || os(iOS) || os(watchOS) || os(tvOS)) && CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API

Tests/CryptoTests/BoringSSL/ArbitraryPrecisionIntegerTests.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
// SPDX-License-Identifier: Apache-2.0
1212
//
1313
//===----------------------------------------------------------------------===//
14+
#if (os(macOS) || os(iOS) || os(watchOS) || os(tvOS)) && CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API
15+
#else
1416
@testable import Crypto
1517
import XCTest
1618

@@ -110,3 +112,4 @@ final class ArbitraryPrecisionIntegerTests: XCTestCase {
110112
XCTAssertTrue(two >= two)
111113
}
112114
}
115+
#endif // (os(macOS) || os(iOS) || os(watchOS) || os(tvOS)) && CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API

scripts/soundness.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ find Sources/* Tests/* -name BoringSSL -type d | while IFS= read -r d; do
8585
done
8686

8787
printf "=> Checking #defines..."
88-
if grep '\.define("CRYPTO_IN_SWIFTPM_FORCE_BUILD_API")' Package.swift | grep -v "//" > /dev/null; then
88+
if grep 'development = true' Package.swift > /dev/null; then
8989
printf "\033[0;31mstill in development mode!\033[0m Comment out CRYPTO_IN_SWIFTPM_FORCE_BUILD_API.\n"
9090
exit 1
9191
else

0 commit comments

Comments
 (0)