Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 1 addition & 8 deletions bindings/c/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,14 @@ cbindgen = "0.24.0"
[dependencies]
bytes = "1.5.0"
lazy_static = "1.4.0"
libsql = { path = "../../libsql", features = ["encryption"] }
tokio = { version = "1.29.1", features = [ "rt-multi-thread" ] }
hyper-rustls = { version = "0.25", features = ["webpki-roots"]}
tracing = "0.1.40"
tracing-subscriber = "0.3.18"
http = "1.1.0"
anyhow = "1.0.86"

[target.'cfg(not(any(target_os = "ios", target_os = "android")))'.dependencies]
libsql = { path = "../../libsql", features = ["encryption"] }

# Disable encryption for ios and android targets
[target.'cfg(any(target_os = "ios", target_os = "android"))'.dependencies]
libsql = { path = "../../libsql"}


# The produced binaries are too large for mobiles
# When compiling for iOS or Android, you should turn on symbol stripping, lto and cut debug symbols
# [profile.release]
Expand Down
3 changes: 2 additions & 1 deletion bindings/c/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ CFLAGS := -Iinclude
LDFLAGS := -lm
ARCHS_IOS = x86_64-apple-ios aarch64-apple-ios aarch64-apple-ios-sim
ARCHS_ANDROID = aarch64-linux-android armv7-linux-androideabi x86_64-linux-android i686-linux-android
ANDROID_PLATFORM := 31
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note

This value comes from the value already passed to the platform option when running cargo ndk. It has been pulled out and assigned here so that it can also be passed as an environment variable for libSQL FFI's build script which uses it to set the ANDROID_PLATFORM variable when running CMake.

LIB = libsql_experimental.a
HEADER = libsql.h
XCFRAMEWORK = libsql.xcframework
Expand Down Expand Up @@ -35,7 +36,7 @@ android: $(ARCHS_ANDROID)
cp ../../target/i686-linux-android/release/$(LIB) generated/jniLibs/x86/$(LIB)

$(ARCHS_ANDROID): %:
cargo ndk --target $@ --platform 31 build --release
ANDROID_PLATFORM=$(ANDROID_PLATFORM) cargo ndk --target $@ --platform $(ANDROID_PLATFORM) build --release

ios: $(XCFRAMEWORK)

Expand Down
77 changes: 61 additions & 16 deletions libsql-ffi/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ fn build_multiple_ciphers(target: &str, out_path: &Path) {
let bundled_dir = format!("{out_dir}/sqlite3mc");
let sqlite3mc_build_dir = env::current_dir().unwrap().join(out_dir).join("sqlite3mc");

let mut cmake_opts: Vec<&str> = vec![];
let mut cmake_opts: Vec<String> = vec![];
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note

cmake_opts changed from Vec<&str> to Vec<String> to allow for interpolating options values into a string with their option names.


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

let toolchain_path = sqlite3mc_build_dir.join("toolchain.cmake");
let cmake_toolchain_opt = "-DCMAKE_TOOLCHAIN_FILE=toolchain.cmake".to_string();
let ndk_cmake_toolchain_path = env::var("CARGO_NDK_CMAKE_TOOLCHAIN_PATH").ok();
let toolchain_path = ndk_cmake_toolchain_path
.clone()
.map(PathBuf::from)
.unwrap_or_else(|| sqlite3mc_build_dir.join("toolchain.cmake"));
let cmake_toolchain_opt = ndk_cmake_toolchain_path
.clone()
.map(|path| format!("-DCMAKE_TOOLCHAIN_FILE={}", path))
.unwrap_or_else(|| "-DCMAKE_TOOLCHAIN_FILE=toolchain.cmake".to_string());
Comment on lines +465 to +473
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note

Use the Android NDK toolchain if available (determined by the value of the CARGO_NDK_CMAKE_TOOLCHAIN_PATH). This seems to be necessary for a successful build for Android.


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

cmake_opts.push(&cmake_toolchain_opt);
cmake_opts.push(cmake_toolchain_opt.clone());
writeln!(toolchain_file, "set(CMAKE_SYSTEM_NAME \"{}\")", system_name).unwrap();
writeln!(
toolchain_file,
Expand All @@ -508,20 +515,58 @@ fn build_multiple_ciphers(target: &str, out_path: &Path) {
writeln!(toolchain_file, "set(CMAKE_CXX_COMPILER {})", cxx).unwrap();
}

cmake_opts.push("-DCMAKE_BUILD_TYPE=Release");
cmake_opts.push("-DSQLITE3MC_STATIC=ON");
cmake_opts.push("-DCODEC_TYPE=AES256");
cmake_opts.push("-DSQLITE3MC_BUILD_SHELL=OFF");
cmake_opts.push("-DSQLITE_SHELL_IS_UTF8=OFF");
cmake_opts.push("-DSQLITE_USER_AUTHENTICATION=OFF");
cmake_opts.push("-DSQLITE_SECURE_DELETE=OFF");
cmake_opts.push("-DSQLITE_ENABLE_COLUMN_METADATA=ON");
cmake_opts.push("-DSQLITE_USE_URI=ON");
cmake_opts.push("-DCMAKE_POSITION_INDEPENDENT_CODE=ON");
cmake_opts.push("-DCMAKE_BUILD_TYPE=Release".to_string());
cmake_opts.push("-DSQLITE3MC_STATIC=ON".to_string());
cmake_opts.push("-DCODEC_TYPE=AES256".to_string());
cmake_opts.push("-DSQLITE3MC_BUILD_SHELL=OFF".to_string());
cmake_opts.push("-DSQLITE_SHELL_IS_UTF8=OFF".to_string());
cmake_opts.push("-DSQLITE_USER_AUTHENTICATION=OFF".to_string());
cmake_opts.push("-DSQLITE_SECURE_DELETE=OFF".to_string());
cmake_opts.push("-DSQLITE_ENABLE_COLUMN_METADATA=ON".to_string());
cmake_opts.push("-DSQLITE_USE_URI=ON".to_string());
cmake_opts.push("-DCMAKE_POSITION_INDEPENDENT_CODE=ON".to_string());

if target.contains("musl") {
cmake_opts.push("-DCMAKE_C_FLAGS=\"-U_FORTIFY_SOURCE\" -D_FILE_OFFSET_BITS=32");
cmake_opts.push("-DCMAKE_CXX_FLAGS=\"-U_FORTIFY_SOURCE\" -D_FILE_OFFSET_BITS=32");
cmake_opts.push("-DCMAKE_C_FLAGS=\"-U_FORTIFY_SOURCE\" -D_FILE_OFFSET_BITS=32".to_string());
cmake_opts
.push("-DCMAKE_CXX_FLAGS=\"-U_FORTIFY_SOURCE\" -D_FILE_OFFSET_BITS=32".to_string());
Comment on lines +518 to +532
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note

Changing cmake_opts from Vec<&str> to Vec<String> necessitated calling to_string on these string literals.

This seems very verbose but I'm not familiar with Rust so please let me know if there is a better way.

}

if target.contains("android") {
let android_abi = match target {
"aarch64-linux-android" => "arm64-v8a",
"armv7-linux-androideabi" => "armeabi-v7a",
"i686-linux-android" => "x86",
"x86_64-linux-android" => "x86_64",
_ => panic!("Unsupported Android target: {}", target),
};
let android_platform = std::env::var("ANDROID_PLATFORM")
.expect("ANDROID_PLATFORM environment variable must be set");

cmake_opts.push(cmake_toolchain_opt);
cmake_opts.push(format!("-DANDROID_ABI={}", android_abi));
cmake_opts.push(format!("-DANDROID_PLATFORM=android-{}", android_platform));
}
Comment on lines +535 to +549
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Important

This is probably the critical change to build for Android. It configures CMake in the following ways:

  • Sets CMAKE_TOOLCHAIN_FILE to the path to the Android NDK toolchain file (available via the CARGO_NDK_CMAKE_TOOLCHAIN_PATH environment variable).
  • Sets the ANDROID_ABI variable according to the defined target.
  • Sets the ANDROID_PLATFORM variable to the platform level specified in the C bindings package manifest, via an environment variable named ANDROID_PLATFORM.


if target.contains("ios") {
cmake_opts.push("-DCMAKE_SYSTEM_NAME=iOS".to_string());

let (arch, processor, sysroot) = if target.contains("x86_64") {
("x86_64", "x86_64", "iphonesimulator")
} else if target.contains("aarch64") {
let sysroot = if target.contains("sim") {
"iphonesimulator"
} else {
"iphoneos"
};
("arm64", "arm64", sysroot)
} else {
panic!("Unsupported iOS target: {}", target);
};

cmake_opts.push(format!("-DCMAKE_OSX_ARCHITECTURES={}", arch));
cmake_opts.push(format!("-DCMAKE_SYSTEM_PROCESSOR={}", processor));
cmake_opts.push(format!("-DCMAKE_OSX_SYSROOT={}", sysroot));
Comment on lines +551 to +569
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Important

This is probably the critical change to build for iOS. It configures CMake in the following ways:

  • Sets the CMAKE_OSX_ARCHITECTURES variable to the architecture according to the target.
  • Sets the CMAKE_SYSTEM_PROCESSOR variable to the processor according to the target.
  • Sets the CMAKE_OSX_SYSROOT variable to iphonesimulator if the target is a simulator or the x86_64 architecture (there are no physical x86_64 iPhones) or iphoneos if the target is a physical device.

}

let mut cmake = Command::new("cmake");
Expand Down
18 changes: 12 additions & 6 deletions libsql-ffi/bundled/SQLite3MultipleCiphers/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -273,14 +273,17 @@ if(MSVC)
)
endif()

if (CMAKE_SYSTEM_NAME STREQUAL "Linux"
OR CMAKE_SYSTEM_NAME STREQUAL "Darwin")
if (CMAKE_SYSTEM_NAME STREQUAL "Android"
OR CMAKE_SYSTEM_NAME STREQUAL "Darwin"
OR CMAKE_SYSTEM_NAME STREQUAL "iOS"
OR CMAKE_SYSTEM_NAME STREQUAL "Linux")

# Do not set `-maes -msee4.2` when we are on arm which doesn't support
# this instruction set.
if (CMAKE_SYSTEM_PROCESSOR STREQUAL "aarch64"
OR CMAKE_SYSTEM_PROCESSOR STREQUAL "arm"
OR CMAKE_SYSTEM_PROCESSOR STREQUAL "arm64"
OR CMAKE_SYSTEM_PROCESSOR STREQUAL "arm")
OR CMAKE_SYSTEM_PROCESSOR STREQUAL "armv7-a")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
else()
Expand All @@ -293,15 +296,17 @@ if (CMAKE_SYSTEM_NAME STREQUAL "Linux"
dl
m
)
if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
if(CMAKE_SYSTEM_NAME STREQUAL "Darwin"
OR CMAKE_SYSTEM_NAME STREQUAL "iOS")
list(APPEND SQLITE3MC_LINK_LIBRARIES "-framework Security")
endif()
set(SHARED_LIB_EXPORT_DEFINITION "__attribute__((visibility(\"default\")))")
else()
if (CMAKE_C_COMPILER_ID STREQUAL "GNU" AND NOT (
CMAKE_SYSTEM_PROCESSOR STREQUAL "aarch64"
OR CMAKE_SYSTEM_PROCESSOR STREQUAL "arm64"
OR CMAKE_SYSTEM_PROCESSOR STREQUAL "arm"
OR CMAKE_SYSTEM_PROCESSOR STREQUAL "arm64"
OR CMAKE_SYSTEM_PROCESSOR STREQUAL "armv7-a"
))
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -msse4.2 -maes")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -msse4.2 -maes")
Expand All @@ -311,8 +316,9 @@ endif()

if (CMAKE_C_COMPILER_ID STREQUAL "Clang" AND NOT (
CMAKE_SYSTEM_PROCESSOR STREQUAL "aarch64"
OR CMAKE_SYSTEM_PROCESSOR STREQUAL "arm64"
OR CMAKE_SYSTEM_PROCESSOR STREQUAL "arm"
OR CMAKE_SYSTEM_PROCESSOR STREQUAL "arm64"
OR CMAKE_SYSTEM_PROCESSOR STREQUAL "armv7-a"
))
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -msse4.2 -maes -Wno-error=incompatible-function-pointer-types")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -msse4.2 -maes")
Expand Down
Loading