Skip to content

Commit 0d0250d

Browse files
committed
Enable encryption for mobile targets
1 parent c6ebbe1 commit 0d0250d

File tree

4 files changed

+75
-31
lines changed

4 files changed

+75
-31
lines changed

bindings/c/Cargo.toml

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,14 @@ cbindgen = "0.24.0"
1313
[dependencies]
1414
bytes = "1.5.0"
1515
lazy_static = "1.4.0"
16+
libsql = { path = "../../libsql", features = ["encryption"] }
1617
tokio = { version = "1.29.1", features = [ "rt-multi-thread" ] }
1718
hyper-rustls = { version = "0.25", features = ["webpki-roots"]}
1819
tracing = "0.1.40"
1920
tracing-subscriber = "0.3.18"
2021
http = "1.1.0"
2122
anyhow = "1.0.86"
2223

23-
[target.'cfg(not(any(target_os = "ios", target_os = "android")))'.dependencies]
24-
libsql = { path = "../../libsql", features = ["encryption"] }
25-
26-
# Disable encryption for ios and android targets
27-
[target.'cfg(any(target_os = "ios", target_os = "android"))'.dependencies]
28-
libsql = { path = "../../libsql"}
29-
30-
3124
# The produced binaries are too large for mobiles
3225
# When compiling for iOS or Android, you should turn on symbol stripping, lto and cut debug symbols
3326
# [profile.release]

bindings/c/Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ CFLAGS := -Iinclude
33
LDFLAGS := -lm
44
ARCHS_IOS = x86_64-apple-ios aarch64-apple-ios aarch64-apple-ios-sim
55
ARCHS_ANDROID = aarch64-linux-android armv7-linux-androideabi x86_64-linux-android i686-linux-android
6+
ANDROID_PLATFORM := 31
67
LIB = libsql_experimental.a
78
HEADER = libsql.h
89
XCFRAMEWORK = libsql.xcframework
@@ -35,7 +36,7 @@ android: $(ARCHS_ANDROID)
3536
cp ../../target/i686-linux-android/release/$(LIB) generated/jniLibs/x86/$(LIB)
3637

3738
$(ARCHS_ANDROID): %:
38-
cargo ndk --target $@ --platform 31 build --release
39+
ANDROID_PLATFORM=$(ANDROID_PLATFORM) cargo ndk --target $@ --platform $(ANDROID_PLATFORM) build --release
3940

4041
ios: $(XCFRAMEWORK)
4142

libsql-ffi/build.rs

Lines changed: 60 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@ fn build_multiple_ciphers(target: &str, out_path: &Path) {
452452
let bundled_dir = format!("{out_dir}/sqlite3mc");
453453
let sqlite3mc_build_dir = env::current_dir().unwrap().join(out_dir).join("sqlite3mc");
454454

455-
let mut cmake_opts: Vec<&str> = vec![];
455+
let mut cmake_opts: Vec<String> = vec![];
456456

457457
let target_postfix = target.to_string().replace("-", "_");
458458
let cross_cc_var_name = format!("CC_{}", target_postfix);
@@ -462,8 +462,15 @@ fn build_multiple_ciphers(target: &str, out_path: &Path) {
462462
let cross_cxx_var_name = format!("CXX_{}", target_postfix);
463463
let cross_cxx = env::var(&cross_cxx_var_name).ok();
464464

465-
let toolchain_path = sqlite3mc_build_dir.join("toolchain.cmake");
466-
let cmake_toolchain_opt = "-DCMAKE_TOOLCHAIN_FILE=toolchain.cmake".to_string();
465+
let ndk_cmake_toolchain_path = env::var("CARGO_NDK_CMAKE_TOOLCHAIN_PATH").ok();
466+
let toolchain_path = ndk_cmake_toolchain_path
467+
.clone()
468+
.map(PathBuf::from)
469+
.unwrap_or_else(|| sqlite3mc_build_dir.join("toolchain.cmake"));
470+
let cmake_toolchain_opt = ndk_cmake_toolchain_path
471+
.clone()
472+
.map(|path| format!("-DCMAKE_TOOLCHAIN_FILE={}", path))
473+
.unwrap_or_else(|| "-DCMAKE_TOOLCHAIN_FILE=toolchain.cmake".to_string());
467474

468475
let mut toolchain_file = OpenOptions::new()
469476
.create(true)
@@ -493,7 +500,7 @@ fn build_multiple_ciphers(target: &str, out_path: &Path) {
493500
panic!("Unsupported cross target {}", cc)
494501
};
495502

496-
cmake_opts.push(&cmake_toolchain_opt);
503+
cmake_opts.push(cmake_toolchain_opt.clone());
497504
writeln!(toolchain_file, "set(CMAKE_SYSTEM_NAME \"{}\")", system_name).unwrap();
498505
writeln!(
499506
toolchain_file,
@@ -508,20 +515,57 @@ fn build_multiple_ciphers(target: &str, out_path: &Path) {
508515
writeln!(toolchain_file, "set(CMAKE_CXX_COMPILER {})", cxx).unwrap();
509516
}
510517

511-
cmake_opts.push("-DCMAKE_BUILD_TYPE=Release");
512-
cmake_opts.push("-DSQLITE3MC_STATIC=ON");
513-
cmake_opts.push("-DCODEC_TYPE=AES256");
514-
cmake_opts.push("-DSQLITE3MC_BUILD_SHELL=OFF");
515-
cmake_opts.push("-DSQLITE_SHELL_IS_UTF8=OFF");
516-
cmake_opts.push("-DSQLITE_USER_AUTHENTICATION=OFF");
517-
cmake_opts.push("-DSQLITE_SECURE_DELETE=OFF");
518-
cmake_opts.push("-DSQLITE_ENABLE_COLUMN_METADATA=ON");
519-
cmake_opts.push("-DSQLITE_USE_URI=ON");
520-
cmake_opts.push("-DCMAKE_POSITION_INDEPENDENT_CODE=ON");
518+
cmake_opts.push("-DCMAKE_BUILD_TYPE=Release".to_string());
519+
cmake_opts.push("-DSQLITE3MC_STATIC=ON".to_string());
520+
cmake_opts.push("-DCODEC_TYPE=AES256".to_string());
521+
cmake_opts.push("-DSQLITE3MC_BUILD_SHELL=OFF".to_string());
522+
cmake_opts.push("-DSQLITE_SHELL_IS_UTF8=OFF".to_string());
523+
cmake_opts.push("-DSQLITE_USER_AUTHENTICATION=OFF".to_string());
524+
cmake_opts.push("-DSQLITE_SECURE_DELETE=OFF".to_string());
525+
cmake_opts.push("-DSQLITE_ENABLE_COLUMN_METADATA=ON".to_string());
526+
cmake_opts.push("-DSQLITE_USE_URI=ON".to_string());
527+
cmake_opts.push("-DCMAKE_POSITION_INDEPENDENT_CODE=ON".to_string());
521528

522529
if target.contains("musl") {
523-
cmake_opts.push("-DCMAKE_C_FLAGS=\"-U_FORTIFY_SOURCE\" -D_FILE_OFFSET_BITS=32");
524-
cmake_opts.push("-DCMAKE_CXX_FLAGS=\"-U_FORTIFY_SOURCE\" -D_FILE_OFFSET_BITS=32");
530+
cmake_opts.push("-DCMAKE_C_FLAGS=\"-U_FORTIFY_SOURCE\" -D_FILE_OFFSET_BITS=32".to_string());
531+
cmake_opts.push("-DCMAKE_CXX_FLAGS=\"-U_FORTIFY_SOURCE\" -D_FILE_OFFSET_BITS=32".to_string());
532+
}
533+
534+
if target.contains("android") {
535+
let android_abi = match target {
536+
"aarch64-linux-android" => "arm64-v8a",
537+
"armv7-linux-androideabi" => "armeabi-v7a",
538+
"i686-linux-android" => "x86",
539+
"x86_64-linux-android" => "x86_64",
540+
_ => panic!("Unsupported Android target: {}", target),
541+
};
542+
let android_platform = std::env::var("ANDROID_PLATFORM")
543+
.expect("ANDROID_PLATFORM environment variable must be set");
544+
545+
cmake_opts.push(cmake_toolchain_opt);
546+
cmake_opts.push(format!("-DANDROID_ABI={}", android_abi));
547+
cmake_opts.push(format!("-DANDROID_PLATFORM=android-{}", android_platform));
548+
}
549+
550+
if target.contains("ios") {
551+
cmake_opts.push("-DCMAKE_SYSTEM_NAME=iOS".to_string());
552+
553+
let (arch, processor, sysroot) = if target.contains("x86_64") {
554+
("x86_64", "x86_64", "iphonesimulator")
555+
} else if target.contains("aarch64") {
556+
let sysroot = if target.contains("sim") {
557+
"iphonesimulator"
558+
} else {
559+
"iphoneos"
560+
};
561+
("arm64", "arm64", sysroot)
562+
} else {
563+
panic!("Unsupported iOS target: {}", target);
564+
};
565+
566+
cmake_opts.push(format!("-DCMAKE_OSX_ARCHITECTURES={}", arch));
567+
cmake_opts.push(format!("-DCMAKE_SYSTEM_PROCESSOR={}", processor));
568+
cmake_opts.push(format!("-DCMAKE_OSX_SYSROOT={}", sysroot));
525569
}
526570

527571
let mut cmake = Command::new("cmake");

libsql-ffi/bundled/SQLite3MultipleCiphers/CMakeLists.txt

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -273,14 +273,17 @@ if(MSVC)
273273
)
274274
endif()
275275

276-
if (CMAKE_SYSTEM_NAME STREQUAL "Linux"
277-
OR CMAKE_SYSTEM_NAME STREQUAL "Darwin")
276+
if (CMAKE_SYSTEM_NAME STREQUAL "Android"
277+
OR CMAKE_SYSTEM_NAME STREQUAL "Darwin"
278+
OR CMAKE_SYSTEM_NAME STREQUAL "iOS"
279+
OR CMAKE_SYSTEM_NAME STREQUAL "Linux")
278280

279281
# Do not set `-maes -msee4.2` when we are on arm which doesn't support
280282
# this instruction set.
281283
if (CMAKE_SYSTEM_PROCESSOR STREQUAL "aarch64"
284+
OR CMAKE_SYSTEM_PROCESSOR STREQUAL "arm"
282285
OR CMAKE_SYSTEM_PROCESSOR STREQUAL "arm64"
283-
OR CMAKE_SYSTEM_PROCESSOR STREQUAL "arm")
286+
OR CMAKE_SYSTEM_PROCESSOR STREQUAL "armv7-a")
284287
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
285288
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
286289
else()
@@ -293,15 +296,17 @@ if (CMAKE_SYSTEM_NAME STREQUAL "Linux"
293296
dl
294297
m
295298
)
296-
if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
299+
if(CMAKE_SYSTEM_NAME STREQUAL "Darwin"
300+
OR CMAKE_SYSTEM_NAME STREQUAL "iOS")
297301
list(APPEND SQLITE3MC_LINK_LIBRARIES "-framework Security")
298302
endif()
299303
set(SHARED_LIB_EXPORT_DEFINITION "__attribute__((visibility(\"default\")))")
300304
else()
301305
if (CMAKE_C_COMPILER_ID STREQUAL "GNU" AND NOT (
302306
CMAKE_SYSTEM_PROCESSOR STREQUAL "aarch64"
303-
OR CMAKE_SYSTEM_PROCESSOR STREQUAL "arm64"
304307
OR CMAKE_SYSTEM_PROCESSOR STREQUAL "arm"
308+
OR CMAKE_SYSTEM_PROCESSOR STREQUAL "arm64"
309+
OR CMAKE_SYSTEM_PROCESSOR STREQUAL "armv7-a"
305310
))
306311
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -msse4.2 -maes")
307312
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -msse4.2 -maes")
@@ -311,8 +316,9 @@ endif()
311316

312317
if (CMAKE_C_COMPILER_ID STREQUAL "Clang" AND NOT (
313318
CMAKE_SYSTEM_PROCESSOR STREQUAL "aarch64"
314-
OR CMAKE_SYSTEM_PROCESSOR STREQUAL "arm64"
315319
OR CMAKE_SYSTEM_PROCESSOR STREQUAL "arm"
320+
OR CMAKE_SYSTEM_PROCESSOR STREQUAL "arm64"
321+
OR CMAKE_SYSTEM_PROCESSOR STREQUAL "armv7-a"
316322
))
317323
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -msse4.2 -maes -Wno-error=incompatible-function-pointer-types")
318324
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -msse4.2 -maes")

0 commit comments

Comments
 (0)