From 8646f017adf25675c45e0d19061477a966157f89 Mon Sep 17 00:00:00 2001 From: Nolan Darilek Date: Mon, 18 Nov 2024 14:21:35 -0600 Subject: [PATCH] Support building on Android * Remove `-stdlib=libstdc++` flag on Android since it breaks the build * Add `bundled-android-static-libstdcpp` feature for selecting the static C++ runtime (I.e. https://developer.android.com/ndk/guides/cpp-support#use_clang_directly) --- crates/duckdb/Cargo.toml | 1 + crates/libduckdb-sys/Cargo.toml | 1 + crates/libduckdb-sys/build.rs | 12 +++++++++++- 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/crates/duckdb/Cargo.toml b/crates/duckdb/Cargo.toml index ddea979a..cac43393 100644 --- a/crates/duckdb/Cargo.toml +++ b/crates/duckdb/Cargo.toml @@ -20,6 +20,7 @@ name = "duckdb" [features] default = [] bundled = ["libduckdb-sys/bundled"] +bundled-android-static-libstdcpp = ["libduckdb-sys/bundled-android-static-libstdcpp"] json = ["libduckdb-sys/json", "bundled"] parquet = ["libduckdb-sys/parquet", "bundled"] vtab = [] diff --git a/crates/libduckdb-sys/Cargo.toml b/crates/libduckdb-sys/Cargo.toml index 3bc64030..c0333a9a 100644 --- a/crates/libduckdb-sys/Cargo.toml +++ b/crates/libduckdb-sys/Cargo.toml @@ -18,6 +18,7 @@ exclude = ["duckdb-sources"] [features] default = ["vcpkg", "pkg-config"] bundled = ["cc"] +bundled-android-static-libstdcpp = ["bundled"] buildtime_bindgen = ["bindgen", "pkg-config", "vcpkg"] json = ["bundled"] parquet = ["bundled"] diff --git a/crates/libduckdb-sys/build.rs b/crates/libduckdb-sys/build.rs index 2507b589..a54ff3d4 100644 --- a/crates/libduckdb-sys/build.rs +++ b/crates/libduckdb-sys/build.rs @@ -37,6 +37,7 @@ fn main() { mod build_bundled { use std::{ collections::{HashMap, HashSet}, + env, path::Path, }; @@ -151,11 +152,20 @@ mod build_bundled { cfg.cpp(true) .flag_if_supported("-std=c++11") .flag_if_supported("-stdlib=libc++") - .flag_if_supported("-stdlib=libstdc++") .flag_if_supported("/bigobj") .warnings(false) .flag_if_supported("-w"); + // The Android NDK doesn't build with this flag set. + if env::var("CARGO_CFG_TARGET_OS").unwrap() != "android" { + cfg.flag_if_supported("-stdlib=libstdc++"); + } + + #[cfg(feature = "bundled-android-static-libstdcpp")] + if env::var("CARGO_CFG_TARGET_OS").unwrap() == "android" { + cfg.flag_if_supported("-static-libstdc++"); + } + if win_target() { cfg.define("DUCKDB_BUILD_LIBRARY", None); }