Browse Source

copy the c++ library on android

Jonathan Kelley 5 days ago
parent
commit
f977af1765
2 changed files with 31 additions and 7 deletions
  1. 13 7
      packages/cli/src/build/request.rs
  2. 18 0
      packages/cli/src/build/tools.rs

+ 13 - 7
packages/cli/src/build/request.rs

@@ -660,7 +660,6 @@ impl BuildRequest {
                 "-Clink-arg=-llog".to_string(),
                 "-Clink-arg=-lOpenSLES".to_string(),
                 "-Clink-arg=-lc++".to_string(),
-                "-Clink-arg=-static-libstdc++".to_string(),
                 "-Clink-arg=-lc++abi".to_string(),
                 "-Clink-arg=-Wl,--export-dynamic".to_string(),
                 format!(
@@ -1095,6 +1094,7 @@ impl BuildRequest {
             // todo - how do we handle windows dlls? we don't want to bundle the system dlls
             // for now, we don't do anything with dlls, and only use .dylibs and .so files
 
+            // Write dylibs and dlls to the frameworks folder
             if arg.ends_with(".dylib") | arg.ends_with(".so") {
                 let from = PathBuf::from(arg);
                 let to = framework_dir.join(from.file_name().unwrap());
@@ -1120,12 +1120,16 @@ impl BuildRequest {
                     std::fs::copy(from, to)?;
                 }
             }
-        }
 
-        if self.platform == Platform::Android {
-            std::fs::copy("/Users/jonathankelley/Library/Android/sdk/ndk/29.0.13113456/toolchains/llvm/prebuilt/darwin-x86_64/sysroot/usr/lib/aarch64-linux-android/libc++_shared.so",
-                          framework_dir.join("libc++_shared.so"))
+            // On android, the c++_static flag means we need to copy the libc++_shared.so precompiled
+            // library to the jniLibs folder
+            if arg.contains("-lc++_static") && self.platform == Platform::Android {
+                std::fs::copy(
+                    self.workspace.android_tools()?.libcpp_shared(&self.triple),
+                    framework_dir.join("libc++_shared.so"),
+                )
                 .with_context(|| "Failed to copy libc++_shared.so into bundle")?;
+            }
         }
 
         Ok(())
@@ -1904,7 +1908,10 @@ impl BuildRequest {
         let linker = self.select_linker()?;
 
         tracing::trace!("Fat linking with args: {:?} {:#?}", linker, args);
-        tracing::trace!("Fat linking with env: {:#?}", rustc_args.envs);
+        tracing::trace!("Fat linking with env:");
+        for e in rustc_args.envs.iter() {
+            tracing::trace!("  {}={}", e.0, e.1);
+        }
 
         // Run the linker directly!
         let out_arg = match self.triple.operating_system {
@@ -2502,7 +2509,6 @@ impl BuildRequest {
 
         // Environment variables for cargo
         let cargo_ar_key = cargo_env_target_cfg(&triple, "ar");
-        let cargo_linker_key = cargo_env_target_cfg(&triple, "linker");
         let cargo_rust_flags_key = cargo_env_target_cfg(&triple, "rustflags");
         let bindgen_clang_args_key =
             format!("BINDGEN_EXTRA_CLANG_ARGS_{}", &triple.replace('-', "_"));

+ 18 - 0
packages/cli/src/build/tools.rs

@@ -268,6 +268,24 @@ impl AndroidTools {
 
         triple
     }
+
+    pub(crate) fn libcpp_shared(&self, triple: &Triple) -> PathBuf {
+        // The libc++_shared.so is usually located in the sysroot under:
+        // "~/Library/Android/sdk/ndk/25.2.9519653/toolchains/llvm/prebuilt/darwin-x86_64/sysroot/usr/lib/<arch>/libc++_shared.so"
+        // or similar, depending on the platform.
+        self.sysroot()
+            .join("usr")
+            .join("lib")
+            .join(Self::sysroot_target(&triple.to_string()))
+            .join("libc++_shared.so")
+    }
+
+    pub(crate) fn sysroot_target(rust_target: &str) -> &str {
+        (match rust_target {
+            "armv7-linux-androideabi" => "arm-linux-androideabi",
+            _ => rust_target,
+        }) as _
+    }
 }
 
 fn var_or_debug(name: &str) -> Option<PathBuf> {