From faed3bb4faefc2c0b3f896259107bcf7682df80b Mon Sep 17 00:00:00 2001 From: Simon Binder Date: Fri, 9 Jan 2026 09:26:17 +0100 Subject: [PATCH 1/4] Add encryption target --- Package.swift | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Package.swift b/Package.swift index 63a7101..9fcc0b8 100644 --- a/Package.swift +++ b/Package.swift @@ -21,6 +21,10 @@ let package = Package( name: "CSQLite", targets: ["CSQLite"] ), + .library( + name: "CSQLite3MultipleCiphers", + targets: ["CSQLite3MultipleCiphers"], + ), ], targets: [ // Targets are the basic building blocks of a package, defining a module or a test suite. From b82bd6e17f9c1426d62f3f8488ffd74467624f12 Mon Sep 17 00:00:00 2001 From: Simon Binder Date: Fri, 9 Jan 2026 13:50:43 +0100 Subject: [PATCH 2/4] Enable encryption as an opt-in feature --- Package.swift | 39 ++++++++----------- Sources/CSQLite/{ => _sqlite}/sqlite3.c | 0 .../CSQLite/{include => _sqlite}/sqlite3.h | 0 .../include => CSQLite/_sqlite3mc}/sqlite3.h | 0 .../_sqlite3mc}/sqlite3mc_amalgamation.c | 0 Sources/CSQLite/include/module.modulemap | 2 +- Sources/CSQLite/include/sqlite3_entry.h | 5 +++ Sources/CSQLite/sqlite3_entry.c | 5 +++ .../include/module.modulemap | 4 -- Tests/CSQLiteTests/CSQLiteTests.swift | 21 +++++++++- 10 files changed, 47 insertions(+), 29 deletions(-) rename Sources/CSQLite/{ => _sqlite}/sqlite3.c (100%) rename Sources/CSQLite/{include => _sqlite}/sqlite3.h (100%) rename Sources/{CSQLite3MultipleCiphers/include => CSQLite/_sqlite3mc}/sqlite3.h (100%) rename Sources/{CSQLite3MultipleCiphers => CSQLite/_sqlite3mc}/sqlite3mc_amalgamation.c (100%) create mode 100644 Sources/CSQLite/include/sqlite3_entry.h create mode 100644 Sources/CSQLite/sqlite3_entry.c delete mode 100644 Sources/CSQLite3MultipleCiphers/include/module.modulemap diff --git a/Package.swift b/Package.swift index 9fcc0b8..2400b34 100644 --- a/Package.swift +++ b/Package.swift @@ -1,18 +1,8 @@ -// swift-tools-version: 5.9 +// swift-tools-version: 6.1 // The swift-tools-version declares the minimum version of Swift required to build this package. import PackageDescription -// Note: Keep in sync with https://github.com/powersync-ja/powersync-kotlin/blob/main/plugins/build-plugin/src/main/kotlin/com/powersync/compile/ClangCompile.kt -let compileTimeOptions: [CSetting] = [ - .define("HAVE_GETHOSTUUID", to: "0"), - .define("SQLITE_ENABLE_DBSTAT_VTAB"), - .define("SQLITE_ENABLE_FTS5"), - .define("SQLITE_ENABLE_SNAPSHOT"), - .define("SQLITE_ENABLE_SESSION"), - .define("SQLITE_ENABLE_PREUPDATE_HOOK") -] - let package = Package( name: "CSQLite", products: [ @@ -21,24 +11,27 @@ let package = Package( name: "CSQLite", targets: ["CSQLite"] ), - .library( - name: "CSQLite3MultipleCiphers", - targets: ["CSQLite3MultipleCiphers"], - ), + ], + traits: [ + .trait(name: "Encryption"), ], targets: [ // Targets are the basic building blocks of a package, defining a module or a test suite. // Targets can depend on other targets in this package and products from dependencies. .target( name: "CSQLite", - cSettings: compileTimeOptions, - linkerSettings: [ - .linkedLibrary("m") - ] - ), - .target( - name: "CSQLite3MultipleCiphers", - cSettings: compileTimeOptions, + exclude: ["_sqlite", "_sqlite3mc"], + cSettings: [ + // Note: Keep in sync with https://github.com/powersync-ja/powersync-kotlin/blob/main/plugins/build-plugin/src/main/kotlin/com/powersync/compile/ClangCompile.kt + .define("HAVE_GETHOSTUUID", to: "0"), + .define("SQLITE_ENABLE_DBSTAT_VTAB"), + .define("SQLITE_ENABLE_FTS5"), + .define("SQLITE_ENABLE_SNAPSHOT"), + .define("SQLITE_ENABLE_SESSION"), + .define("SQLITE_ENABLE_PREUPDATE_HOOK"), + + .define("USE_SQLITE3MC", .when(traits: ["Encryption"])) + ], linkerSettings: [ .linkedLibrary("m") ] diff --git a/Sources/CSQLite/sqlite3.c b/Sources/CSQLite/_sqlite/sqlite3.c similarity index 100% rename from Sources/CSQLite/sqlite3.c rename to Sources/CSQLite/_sqlite/sqlite3.c diff --git a/Sources/CSQLite/include/sqlite3.h b/Sources/CSQLite/_sqlite/sqlite3.h similarity index 100% rename from Sources/CSQLite/include/sqlite3.h rename to Sources/CSQLite/_sqlite/sqlite3.h diff --git a/Sources/CSQLite3MultipleCiphers/include/sqlite3.h b/Sources/CSQLite/_sqlite3mc/sqlite3.h similarity index 100% rename from Sources/CSQLite3MultipleCiphers/include/sqlite3.h rename to Sources/CSQLite/_sqlite3mc/sqlite3.h diff --git a/Sources/CSQLite3MultipleCiphers/sqlite3mc_amalgamation.c b/Sources/CSQLite/_sqlite3mc/sqlite3mc_amalgamation.c similarity index 100% rename from Sources/CSQLite3MultipleCiphers/sqlite3mc_amalgamation.c rename to Sources/CSQLite/_sqlite3mc/sqlite3mc_amalgamation.c diff --git a/Sources/CSQLite/include/module.modulemap b/Sources/CSQLite/include/module.modulemap index 46f12aa..d5a568d 100644 --- a/Sources/CSQLite/include/module.modulemap +++ b/Sources/CSQLite/include/module.modulemap @@ -1,4 +1,4 @@ module CSQLite { - header "sqlite3.h" + header "sqlite3_entry.h" export * } diff --git a/Sources/CSQLite/include/sqlite3_entry.h b/Sources/CSQLite/include/sqlite3_entry.h new file mode 100644 index 0000000..40ca403 --- /dev/null +++ b/Sources/CSQLite/include/sqlite3_entry.h @@ -0,0 +1,5 @@ +#ifdef USE_SQLITE3MC +#include "../_sqlite3mc/sqlite3.h" +#else +#include "../_sqlite/sqlite3.h" +#endif diff --git a/Sources/CSQLite/sqlite3_entry.c b/Sources/CSQLite/sqlite3_entry.c new file mode 100644 index 0000000..65b5115 --- /dev/null +++ b/Sources/CSQLite/sqlite3_entry.c @@ -0,0 +1,5 @@ +#ifdef USE_SQLITE3MC +#include "_sqlite3mc/sqlite3mc_amalgamation.c" +#else +#include "_sqlite/sqlite3.c" +#endif diff --git a/Sources/CSQLite3MultipleCiphers/include/module.modulemap b/Sources/CSQLite3MultipleCiphers/include/module.modulemap deleted file mode 100644 index be41564..0000000 --- a/Sources/CSQLite3MultipleCiphers/include/module.modulemap +++ /dev/null @@ -1,4 +0,0 @@ -module CSQLite3MultipleCiphers { - header "sqlite3.h" - export * -} diff --git a/Tests/CSQLiteTests/CSQLiteTests.swift b/Tests/CSQLiteTests/CSQLiteTests.swift index b287e5f..70bc2dd 100644 --- a/Tests/CSQLiteTests/CSQLiteTests.swift +++ b/Tests/CSQLiteTests/CSQLiteTests.swift @@ -1,9 +1,28 @@ import Testing @testable import CSQLite +#if Encryption +let encryption = true +#else +let encryption = false +#endif + + @Test func smoke_test() throws { var db: OpaquePointer? - #expect(sqlite3_open_v2(":memory:", &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, nil) == SQLITE_OK) + try #require(sqlite3_open_v2(":memory:", &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, nil) == SQLITE_OK) #expect(sqlite3_close_v2(db) == SQLITE_OK) // Write your test here and use APIs like `#expect(...)` to check expected conditions. } + +@Test(.disabled(if: !encryption)) func links_sqlite3mc() throws { + var db: OpaquePointer? + try #require(sqlite3_open_v2(":memory:", &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, nil) == SQLITE_OK) + + var stmt: OpaquePointer? + try #require(sqlite3_prepare_v2(db, "pragma cipher", 13, &stmt, nil) == SQLITE_OK) + try #require(sqlite3_step(stmt) == SQLITE_ROW) + let cipher = String(cString: sqlite3_column_text(stmt, 0)) + #expect(cipher == "chacha20") + #expect(sqlite3_close_v2(db) == SQLITE_OK) +} From efc4145cbed401aa5eaa8774e84a3f5298e54c52 Mon Sep 17 00:00:00 2001 From: Simon Binder Date: Fri, 9 Jan 2026 13:52:06 +0100 Subject: [PATCH 3/4] Test with encryption --- .github/workflows/swift.yml | 4 +++- Tests/CSQLiteTests/CSQLiteTests.swift | 1 - 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/.github/workflows/swift.yml b/.github/workflows/swift.yml index 3662627..d8d4ada 100644 --- a/.github/workflows/swift.yml +++ b/.github/workflows/swift.yml @@ -21,4 +21,6 @@ jobs: - name: Build run: swift build -v - name: Run tests - run: swift test -v + run: | + swift test -v + swift test -v --traits Encryption diff --git a/Tests/CSQLiteTests/CSQLiteTests.swift b/Tests/CSQLiteTests/CSQLiteTests.swift index 70bc2dd..8db2472 100644 --- a/Tests/CSQLiteTests/CSQLiteTests.swift +++ b/Tests/CSQLiteTests/CSQLiteTests.swift @@ -7,7 +7,6 @@ let encryption = true let encryption = false #endif - @Test func smoke_test() throws { var db: OpaquePointer? try #require(sqlite3_open_v2(":memory:", &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, nil) == SQLITE_OK) From 28e8b680457046ce3e733e284567ef85475c60e2 Mon Sep 17 00:00:00 2001 From: Simon Binder Date: Fri, 9 Jan 2026 13:53:50 +0100 Subject: [PATCH 4/4] Fix download script --- download.sh | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/download.sh b/download.sh index d66cdd1..40580ed 100755 --- a/download.sh +++ b/download.sh @@ -5,11 +5,11 @@ cd tmp curl -o amalgamation.zip -L https://github.com/utelle/SQLite3MultipleCiphers/releases/download/v2.2.6/sqlite3mc-2.2.6-sqlite-3.51.1-amalgamation.zip unzip amalgamation.zip -mv sqlite3.h ../Sources/CSQLite/include/ -mv sqlite3.c ../Sources/CSQLite/ +mv sqlite3.h ../Sources/CSQLite/_sqlite/ +mv sqlite3.c ../Sources/CSQLite/_sqlite/ -mv sqlite3mc_amalgamation.h ../Sources/CSQLite3MultipleCiphers/include/sqlite3.h -mv sqlite3mc_amalgamation.c ../Sources/CSQLite3MultipleCiphers/ +mv sqlite3mc_amalgamation.h ../Sources/CSQLite/_sqlite3mc/sqlite3.h +mv sqlite3mc_amalgamation.c ../Sources/CSQLite/_sqlite3mc/ cd .. rm -r tmp