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/Package.swift b/Package.swift index 63a7101..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: [ @@ -22,19 +12,26 @@ let package = Package( targets: ["CSQLite"] ), ], + 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..8db2472 100644 --- a/Tests/CSQLiteTests/CSQLiteTests.swift +++ b/Tests/CSQLiteTests/CSQLiteTests.swift @@ -1,9 +1,27 @@ 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) +} 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