Skip to content

Commit d4e3e33

Browse files
authored
Merge pull request #1341 from stephencelis/simplify-trait
Add more traits to Package.swift
2 parents af28502 + 5ae282e commit d4e3e33

22 files changed

Lines changed: 178 additions & 113 deletions

.github/workflows/build.yml

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,23 @@ jobs:
1717
runs-on: ${{ matrix.os }}
1818
env:
1919
IOS_SIMULATOR: ${{ matrix.os == 'macos-15' && 'iPhone 16' || 'iPhone 16e' }}
20-
IOS_VERSION: ${{ matrix.os == 'macos-15' && '18.4' || '26.2' }}
20+
IOS_VERSION: ${{ matrix.os == 'macos-15' && '18.5' || '26.0.1' }}
2121
steps:
2222
- uses: actions/checkout@v4
23+
- name: "xcrun simctl list"
24+
run: "xcrun simctl list"
25+
- name: Update pod repo
26+
run: pod repo update
2327
- name: "Lint"
2428
run: make lint
2529
- name: "Run tests (PACKAGE_MANAGER_COMMAND: test)"
2630
env:
2731
PACKAGE_MANAGER_COMMAND: test -Xswiftc -warnings-as-errors
2832
run: ./run-tests.sh
33+
- name: "Run tests (PACKAGE_MANAGER_COMMAND: test --traits SwiftToolchainCSQLite)"
34+
env:
35+
PACKAGE_MANAGER_COMMAND: test -Xswiftc -warnings-as-errors --traits SwiftToolchainCSQLite
36+
run: ./run-tests.sh
2937
- name: "Run tests (PACKAGE_MANAGER_COMMAND: test --traits SQLCipher)"
3038
env:
3139
PACKAGE_MANAGER_COMMAND: test -Xswiftc -warnings-as-errors --traits SQLCipher
@@ -67,7 +75,7 @@ jobs:
6775
sudo apt-get update -qq
6876
sudo apt-get install -y libsqlite3-dev
6977
- name: Test
70-
run: swift test
78+
run: swift test --traits SwiftToolchainCSQLite
7179
- name: "Run tests (SPM integration test)"
7280
env:
7381
SPM: run
@@ -81,3 +89,4 @@ jobs:
8189
with:
8290
# Ubuntu runners low on space causes the emulator to fail to install
8391
free-disk-space: true
92+
swift-build-flags: --traits SwiftToolchainCSQLite

.swiftlint.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ disabled_rules: # rule identifiers to exclude from running
1010
included: # paths to include during linting. `--path` is ignored if present. takes precendence over `excluded`.
1111
- Sources
1212
- Tests
13+
- Package.swift
1314
excluded: # paths to ignore during linting. overridden by `included`.
1415

1516
identifier_name:

Documentation/Index.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,29 @@ process of downloading, compiling, and linking dependencies.
119119
$ swift build
120120
```
121121

122+
#### Available traits
123+
124+
The Swift package manager now supports [traits][], which can be used to configure
125+
SQLite.swift for different use cases.
126+
127+
```swift
128+
dependencies: [
129+
.package(url: "https://github.com/stephencelis/SQLite.swift.git",
130+
from: "0.15.4",
131+
traits: ["XXX"])
132+
]
133+
```
134+
135+
| Trait | Description |
136+
|--------------------------|---------------------------------------------------|
137+
| `SystemSQLite` (default) | Uses the system SQLite (provided by Apple) |
138+
| `SwiftToolchainCSQLite` | Embeds the SQLite provided by [swift-toolchain][] |
139+
| `StandaloneSQLite` | Only used by CocoaPods |
140+
| `SQLCipher` | Embeds [SQLCipher][] (see below) |
141+
142+
[traits]: https://docs.swift.org/swiftpm/documentation/packagemanagerdocs/packagetraits/
143+
[swift-toolchain]: https://github.com/swiftlang/swift-toolchain-sqlite
144+
122145
#### Using SQLite.swift with SQLCipher
123146

124147
If you want to use [SQLCipher][] with SQLite.swift you can specify the `SQLCipher` trait when consuming SQLite.swift.

Documentation/Linux.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,21 @@
55
* Custom functions/aggregations are currently not supported and crash, caused by a bug in Swift.
66
See [#1071](https://github.com/stephencelis/SQLite.swift/issues/1071).
77

8+
## Installation
9+
10+
On Linux you should enable the `SwiftToolchainCSQLite` trait to automatically
11+
embed SQLite:
12+
13+
```swift
14+
dependencies: [
15+
.package(url: "https://github.com/stephencelis/SQLite.swift.git",
16+
from: "0.15.4",
17+
traits: ["SwiftToolchainCSQLite"])
18+
]
19+
```
20+
21+
See the [main documentation](Index.md#available-traits) for a list of all available traits.
22+
823
## Debugging
924

1025
### Create and launch docker container

Makefile

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ IOS_VERSION = 16.4
77
SWIFTLINT_VERSION=0.52.2
88
SWIFTLINT=bin/swiftlint-$(SWIFTLINT_VERSION)
99
SWIFTLINT_URL=https://github.com/realm/SwiftLint/releases/download/$(SWIFTLINT_VERSION)/portable_swiftlint.zip
10-
XCBEAUTIFY_VERSION=0.20.0
10+
XCBEAUTIFY_VERSION=3.1.2
1111
XCBEAUTIFY=bin/xcbeautify-$(XCBEAUTIFY_VERSION)
1212
ifeq ($(shell uname), Linux)
1313
XCBEAUTIFY_PLATFORM=x86_64-unknown-linux-gnu.tar.xz
1414
else
1515
XCBEAUTIFY_PLATFORM=universal-apple-macosx.zip
1616
endif
17-
XCBEAUTIFY_URL=https://github.com/tuist/xcbeautify/releases/download/$(XCBEAUTIFY_VERSION)/xcbeautify-$(XCBEAUTIFY_VERSION)-$(XCBEAUTIFY_PLATFORM)
17+
XCBEAUTIFY_URL=https://github.com/cpisciotta/xcbeautify/releases/download/$(XCBEAUTIFY_VERSION)/xcbeautify-$(XCBEAUTIFY_VERSION)-$(XCBEAUTIFY_PLATFORM)
1818
CURL_OPTS=--fail --silent -L --retry 3
1919

2020
ifeq ($(BUILD_SCHEME),SQLite iOS)
@@ -60,17 +60,17 @@ $(XCBEAUTIFY):
6060
curl $(CURL_OPTS) $(XCBEAUTIFY_URL) -o $$FILE; \
6161
case "$${FILE#*.}" in \
6262
"zip") \
63-
unzip -o $$FILE xcbeautify; \
63+
unzip -o $$FILE release/xcbeautify; \
6464
;; \
6565
"tar.xz") \
66-
tar -xvf $$FILE xcbeautify; \
66+
tar -xvf $$FILE release/xcbeautify; \
6767
;; \
6868
*) \
6969
echo "unknown extension $${FILE#*.}!"; \
7070
exit 1; \
7171
;; \
7272
esac; \
7373
mkdir -p bin; \
74-
mv xcbeautify $@ && rm -f $$FILE;
74+
mv release/xcbeautify $@ && rm -f $$FILE;
7575

7676
.PHONY: test clean repl sloc

Package.swift

Lines changed: 37 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,29 @@
11
// swift-tools-version: 6.1
22
import PackageDescription
3-
4-
let deps: [Package.Dependency] = [
5-
.package(url: "https://github.com/swiftlang/swift-toolchain-sqlite", from: "1.0.7"),
6-
.package(url: "https://github.com/sqlcipher/SQLCipher.swift.git", from: "4.11.0")
7-
]
8-
93
let applePlatforms: [PackageDescription.Platform] = [.iOS, .macOS, .watchOS, .tvOS, .visionOS]
10-
let sqlcipherTraitBuildSettingCondition: BuildSettingCondition? = .when(platforms: applePlatforms, traits: ["SQLCipher"])
11-
let cSettings: [CSetting] = [.define("SQLITE_HAS_CODEC", to: nil, sqlcipherTraitBuildSettingCondition)]
12-
let swiftSettings: [SwiftSetting] = [.define("SQLITE_HAS_CODEC", sqlcipherTraitBuildSettingCondition)]
134

14-
let targets: [Target] = [
15-
.target(
16-
name: "SQLite",
17-
dependencies: [
18-
.product(name: "SwiftToolchainCSQLite", package: "swift-toolchain-sqlite", condition: .when(platforms: [.linux, .windows, .android])),
19-
.product(name: "SQLCipher", package: "SQLCipher.swift", condition: .when(platforms: applePlatforms, traits: ["SQLCipher"]))
20-
],
21-
exclude: ["Info.plist"],
22-
cSettings: cSettings,
23-
swiftSettings: swiftSettings
24-
)
25-
]
5+
let target: Target = .target(
6+
name: "SQLite",
7+
dependencies: [
8+
.product(name: "SwiftToolchainCSQLite",
9+
package: "swift-toolchain-sqlite",
10+
condition: .when(traits: ["SwiftToolchainCSQLite"])),
11+
.product(name: "SQLCipher",
12+
package: "SQLCipher.swift",
13+
condition: .when(platforms: applePlatforms, traits: ["SQLCipher"]))
14+
],
15+
exclude: ["Info.plist"],
16+
cSettings: [
17+
.define("SQLITE_HAS_CODEC", .when(platforms: applePlatforms, traits: ["SQLCipher"]))
18+
]
19+
)
2620

27-
let testTargets: [Target] = [
28-
.testTarget(
29-
name: "SQLiteTests",
30-
dependencies: ["SQLite"],
31-
path: "Tests/SQLiteTests",
32-
exclude: ["Info.plist"],
33-
resources: [.copy("Resources")],
34-
swiftSettings: swiftSettings
35-
)
36-
]
21+
let testTarget: Target = .testTarget(
22+
name: "SQLiteTests",
23+
dependencies: ["SQLite"],
24+
exclude: ["Info.plist"],
25+
resources: [.copy("Resources")]
26+
)
3727

3828
let package = Package(
3929
name: "SQLite.swift",
@@ -45,15 +35,24 @@ let package = Package(
4535
.visionOS(.v1)
4636
],
4737
products: [
48-
.library(
49-
name: "SQLite",
50-
targets: ["SQLite"]
51-
)
38+
.library(name: "SQLite", targets: ["SQLite"])
5239
],
5340
traits: [
54-
.trait(name: "SQLCipher", description: "Enables SQLCipher encryption when a key is supplied to Connection")
41+
.trait(name: "SystemSQLite",
42+
description: "Uses the system-provided SQLite (on Apple platforms)"),
43+
.trait(name: "SwiftToolchainCSQLite",
44+
description: "Include SQLite from the Swift toolchain"),
45+
// this will note compile, just included for sake of completeness
46+
.trait(name: "StandaloneSQLite",
47+
description: "Assumes SQLite to be already available as 'sqlite3'"),
48+
.trait(name: "SQLCipher",
49+
description: "Enables SQLCipher encryption when a key is supplied to Connection"),
50+
.default(enabledTraits: ["SystemSQLite"])
51+
],
52+
dependencies: [
53+
.package(url: "https://github.com/swiftlang/swift-toolchain-sqlite", from: "1.0.7"),
54+
.package(url: "https://github.com/sqlcipher/SQLCipher.swift.git", from: "4.11.0")
5555
],
56-
dependencies: deps,
57-
targets: targets + testTargets,
56+
targets: [target, testTarget],
5857
swiftLanguageModes: [.v5],
5958
)

SQLite.swift.podspec

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,52 +24,51 @@ Pod::Spec.new do |s|
2424
s.watchos.deployment_target = '4.0'
2525
s.visionos.deployment_target = '1.0'
2626

27+
# uses the built-in sqlite3 library
2728
s.subspec 'standard' do |ss|
29+
ss.library = 'sqlite3'
2830
ss.source_files = 'Sources/SQLite/**/*.{c,h,m,swift}'
2931
ss.exclude_files = 'Sources/**/Cipher.swift'
30-
ss.library = 'sqlite3'
3132
ss.resource_bundle = { 'SQLite.swift' => 'Sources/SQLite/PrivacyInfo.xcprivacy' }
32-
33+
ss.xcconfig = {
34+
'OTHER_SWIFT_FLAGS' => '$(inherited) -DSystemSQLite'
35+
}
3336
ss.test_spec 'tests' do |test_spec|
3437
test_spec.resources = 'Tests/SQLiteTests/Resources/*'
3538
test_spec.source_files = 'Tests/SQLiteTests/**/*.swift'
3639
end
3740
end
3841

42+
# uses SQLite from https://github.com/clemensg/sqlite3pod
3943
s.subspec 'standalone' do |ss|
44+
ss.dependency 'sqlite3'
4045
ss.source_files = 'Sources/SQLite/**/*.{c,h,m,swift}'
4146
ss.exclude_files = 'Sources/**/Cipher.swift'
4247
ss.resource_bundle = { 'SQLite.swift' => 'Sources/SQLite/PrivacyInfo.xcprivacy' }
43-
4448
ss.xcconfig = {
45-
'OTHER_SWIFT_FLAGS' => '$(inherited) -DSQLITE_SWIFT_STANDALONE',
46-
'GCC_PREPROCESSOR_DEFINITIONS' => '$(inherited) SQLITE_SWIFT_STANDALONE=1'
49+
'OTHER_SWIFT_FLAGS' => '$(inherited) -DStandaloneSQLite'
4750
}
48-
ss.dependency 'sqlite3'
49-
5051
ss.test_spec 'tests' do |test_spec|
5152
test_spec.resources = 'Tests/SQLiteTests/Resources/*'
5253
test_spec.source_files = 'Tests/SQLiteTests/**/*.swift'
5354
end
5455
end
5556

57+
# uses SQLCipher from https://github.com/sqlcipher/sqlcipher
5658
s.subspec 'SQLCipher' do |ss|
59+
ss.dependency 'SQLCipher', '>= 4.0.0'
5760
# Disable unsupported visionOS
5861
# https://github.com/sqlcipher/sqlcipher/issues/483
5962
ss.ios.deployment_target = s.deployment_target(:ios)
6063
ss.tvos.deployment_target = s.deployment_target(:tvos)
6164
ss.osx.deployment_target = s.deployment_target(:osx)
6265
ss.watchos.deployment_target = s.deployment_target(:watchos)
63-
6466
ss.source_files = 'Sources/SQLite/**/*.{c,h,m,swift}'
6567
ss.resource_bundle = { 'SQLite.swift' => 'Sources/SQLite/PrivacyInfo.xcprivacy' }
66-
6768
ss.xcconfig = {
68-
'OTHER_SWIFT_FLAGS' => '$(inherited) -DSQLITE_HAS_CODEC',
69+
'OTHER_SWIFT_FLAGS' => '$(inherited) -DSQLCipher',
6970
'GCC_PREPROCESSOR_DEFINITIONS' => '$(inherited) SQLITE_HAS_CODEC=1'
7071
}
71-
ss.dependency 'SQLCipher', '>= 4.0.0'
72-
7372
ss.test_spec 'tests' do |test_spec|
7473
test_spec.resources = 'Tests/SQLiteTests/Resources/*'
7574
test_spec.source_files = 'Tests/SQLiteTests/**/*.swift'

Sources/SQLite/Core/Backup.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@
2424

2525
import Foundation
2626
import Dispatch
27-
#if canImport(sqlite3)
27+
#if StandaloneSQLite
2828
import sqlite3
29-
#elseif canImport(SQLCipher)
29+
#elseif SQLCipher
3030
import SQLCipher
31-
#elseif canImport(SwiftToolchainCSQLite)
31+
#elseif SwiftToolchainCSQLite
3232
import SwiftToolchainCSQLite
3333
#else
34-
import SQLite3
34+
import SQLite3 // SystemSQLite
3535
#endif
3636

3737
/// An object representing database backup.

Sources/SQLite/Core/Connection+Aggregation.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import Foundation
2-
#if canImport(sqlite3)
2+
#if StandaloneSQLite
33
import sqlite3
4-
#elseif canImport(SQLCipher)
4+
#elseif SQLCipher
55
import SQLCipher
6-
#elseif canImport(SwiftToolchainCSQLite)
6+
#elseif SwiftToolchainCSQLite
77
import SwiftToolchainCSQLite
88
#else
9-
import SQLite3
9+
import SQLite3 // SystemSQLite
1010
#endif
1111

1212
extension Connection {

Sources/SQLite/Core/Connection+Attach.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import Foundation
22

33
extension Connection {
4-
#if SQLITE_HAS_CODEC
4+
#if SQLCipher
55
/// See https://www.zetetic.net/sqlcipher/sqlcipher-api/#attach
66
public func attach(_ location: Location, as schemaName: String, key: String? = nil) throws {
77
if let key {

0 commit comments

Comments
 (0)