mozilla / rust-android-gradle

Apache License 2.0
1.11k stars 71 forks source link

support for #[cfg(target_arch = "...")] in build.rs? #63

Open lattice0 opened 3 years ago

lattice0 commented 3 years ago

This is my build.rs of a project that uses rust-android-gradle to be built:

fn main() {

    let ffmpeg_location = match env::var("CARGO_FFMPEG_MULTI_ARCHITECTURE_PREBUILT") {
        Ok(val) => val,
        Err(_e) => panic!("could not find CARGO_FFMPEG_MULTI_ARCHITECTURE_PREBUILT")
    };

    #[cfg(target_arch = "aarch64")]
    let ffmpeg_for_architecture = format!("{}/{}",ffmpeg_location,"android/arm64-v8a");
    #[cfg(target_arch = "arm")]
    let ffmpeg_for_architecture = format!("{}/{}",ffmpeg_location,"android/armeabi-v7a");
    #[cfg(target_arch = "x86")]
    let ffmpeg_for_architecture = format!("{}/{}",ffmpeg_location,"android/x86");
    #[cfg(target_arch = "x86_64")]
    let ffmpeg_for_architecture = format!("{}/{}",ffmpeg_location,"android/x86_64");
    println!("{}", format!("cargo:rustc-link-search=native={}", ffmpeg_for_architecture));
    println!("{}", format!("cargo:rustc-link-search=shared={}", ffmpeg_for_architecture));

    //println!("cargo:rustc-link-lib=dylib=android");
    //println!("cargo:rustc-link-lib=dylib=log");
    println!("cargo:rustc-link-lib=dylib=libavcodec");
    println!("cargo:rustc-link-lib=dylib=libavutil");
    println!("cargo:rustc-link-lib=dylib=libswscale");
    println!("cargo:rustc-link-lib=dylib=libswresample");
}

it gives errors saying:

  = note: ld: error: unable to find library -llibavcodec
          ld: error: unable to find library -llibavutil
          ld: error: unable to find library -llibswscale
          ld: error: unable to find library -llibswresample

On the error details, it lists this link command:

-L /home/dev/orwell/deps/ffmpeg/build/android/x86_64 -L shared=/home/dev/orwell/deps/ffmpeg/build/android/x86_64

this means that #[cfg(target_arch = "...")] does not work, it simply picks my processor as target.

Am I doing something wrong?

ncalexan commented 3 years ago

This is tricky. Can you provide complete build logs with ./gradlew --verbose cargoBuild? I think that should include enough to see what's happening.

But I have a theory. Under the hood, rust-android-gradle is simply invoking Cargo multiple times, once for each architecture. My guess is that the build script part doesn't see these different invocations as being different, so build.rs isn't "seeing" each of the different targets. But I'd need to see the logs to confirm this.

If this theory is correct, then the only way that I can think of to address this is to ensure that the Cargo cache sees these different targets as truly different. I've never tried to do what you're doing so I don't no how to proceed. The plugin does support per-target compiler options, though, so you might be able to work around these types of issue by configuring those options explicitly rather than programatically via build.rs.

The logs will be very large so gist or similar will be best.

lattice0 commented 3 years ago

@ncalexan there's no verbose option in gradlew

Unknown command-line option '--verbose'.

./gradlew --version

------------------------------------------------------------
Gradle 6.5
------------------------------------------------------------
ncalexan commented 3 years ago

./gradlew --info or --debug.

lattice0 commented 3 years ago

Ok I think I know what's happening.

Here's a better output:

error: linking with `/home/dev/orwell/flutter_orwell_app/build/linker-wrapper/linker-wrapper.sh` failed: exit code: 1
  |
  = note: "/home/dev/orwell/flutter_orwell_app/build/linker-wrapper/linker-wrapper.sh" "-Wl,--as-needed" "-Wl,-z,noexecstack" "-Wl,--allow-multiple-definition" "-march=armv7-a" "-Wl,--eh-frame-hdr" "-L" "/usr/local/rustup/toolchains/1.52.1-x86_64-unknown-linux-gnu/lib/rustlib/armv7-linux-androideabi/lib" 
"/home/dev/orwell/flutter_orwell_app/android/app/src/main/rust/liborwell_rust_android/target/armv7-linux-androideabi/debug
/deps/orwell_rust_android.orwell_rust_android.1puiopvy-cgu.0.rcgu.o" "/home/dev/orwell/flutter_orwell_app/android/app/src
/main/rust/liborwell_rust_android/target/armv7-linux-androideabi/debug/deps/orwell_rust_android.orwell_rust_android.1puiopvy-
cgu.1.rcgu.o" "/home/dev/orwell/flutter_orwell_app/android/app/src/main/rust/liborwell_rust_android/target/armv7-linux-
androideabi/debug/deps/orwell_rust_android.orwell_rust_android.1puiopvy-cgu.2.rcgu.o" "/home/dev/orwell/flutter_orwell_app
/android/app/src/main/rust/liborwell_rust_android/target/armv7-linux-androideabi/debug
/deps/orwell_rust_android.orwell_rust_android.1puiopvy-cgu.3.rcgu.o" "/home/dev/orwell/flutter_orwell_app/android/app/src
/main/rust/liborwell_rust_android/target/armv7-linux-androideabi/debug/deps/orwell_rust_android.orwell_rust_android.1puiopvy-

cgu.4.rcgu.o" "/home/dev/orwell/flutter_orwell_app/android/app/src/main/rust/liborwell_rust_android/target/armv7-linux-
androideabi/debug/deps/orwell_rust_android.orwell_rust_android.1puiopvy-cgu.5.rcgu.o" "-o" "/home/dev/orwell
/flutter_orwell_app/android/app/src/main/rust/liborwell_rust_android/target/armv7-linux-androideabi/debug
/deps/liborwell_rust_android.so" "-Wl,--version-script=/tmp/rustcNwuuSz/list" "/home/dev/orwell/flutter_orwell_app/android/app/src
/main/rust/liborwell_rust_android/target/armv7-linux-androideabi/debug/deps/orwell_rust_android.1lkyuccy1x9dir34.rcgu.o" \
"/home/dev/orwell/flutter_orwell_app/android/app/src/main/rust/liborwell_rust_android/target/armv7-linux-androideabi/debug/deps/orwell_rust_android.porrrqbj37xx5a2.rcgu.o" "-shared" "-Wl,-zrelro" "-Wl,-znow" "-nodefaultlibs" "-L" "/home/dev/orwell/flutter_orwell_app/android/app/src/main/rust/liborwell_rust_android/target/armv7-linux-androideabi/debug/deps" "-L" "/home
/dev/orwell/flutter_orwell_app/android/app/src/main/rust/liborwell_rust_android/target/debug/deps" "-L" "/home/dev/orwell
/deps/ffmpeg/build/android/x86_64" "-L" "/usr/local/rustup/toolchains/1.52.1-x86_64-unknown-linux-gnu/lib/rustlib/armv7-linux-
androideabi/lib" "-lavcodec" "-lavutil" "-lswscale" "-lswresample" "-Wl,-Bstatic" "-Wl,--whole-archive" "/tmp/rustcNwuuSz/libjni-
f3124597b53f380c.rlib" "-Wl,--no-whole-archive" "-Wl,--whole-archive" "/tmp/rustcNwuuSz/libcesu8-f01a964297c1f0d1.rlib" "-Wl,--
no-whole-archive" "-Wl,--whole-archive" "/tmp/rustcNwuuSz/liblog-0b4cb90a7c9f797e.rlib" "-Wl,--no-whole-archive" "-Wl,--whole-
archive" "/tmp/rustcNwuuSz/libcfg_if-887c440cb135db11.rlib" "-Wl,--no-whole-archive" "-Wl,--whole-archive" "/tmp/rustcNwuuSz
/libcombine-75e5d5aa2f594e2f.rlib" "-Wl,--no-whole-archive" "-Wl,--whole-archive" "/tmp/rustcNwuuSz/libmemchr-
61ea3eb5f5694de9.rlib" "-Wl,--no-whole-archive" "-Wl,--whole-archive" "/tmp/rustcNwuuSz/libbytes-1af04e93b796429f.rlib" "-Wl,--
no-whole-archive" "-Wl,--whole-archive" "/tmp/rustcNwuuSz/libthiserror-6f95056aba1d6d91.rlib" "-Wl,--no-whole-archive" "-Wl,--
whole-archive" "/tmp/rustcNwuuSz/libjni_sys-b384ed00a5c66d42.rlib" "-Wl,--no-whole-archive" "-Wl,--start-group" "-Wl,--whole-
archive" "/tmp/rustcNwuuSz/libstd-9645fb52a10e1442.rlib" "-Wl,--no-whole-archive" "-Wl,--whole-archive" "/tmp/rustcNwuuSz
/libpanic_unwind-a1bdb04986d578f8.rlib" "-Wl,--no-whole-archive" "-Wl,--whole-archive" "/tmp/rustcNwuuSz/libminiz_oxide-
682faed215453b78.rlib" "-Wl,--no-whole-archive" "-Wl,--whole-archive" "/tmp/rustcNwuuSz/libadler-419dd832270dc9b9.rlib" 
"-Wl,--no-whole-archive" "-Wl,--whole-archive" "/tmp/rustcNwuuSz/libobject-46592f53dff83526.rlib" "-Wl,--no-whole-archive" "-Wl,--
whole-archive" "/tmp/rustcNwuuSz/libaddr2line-bc61ed3a4a3adba9.rlib" "-Wl,--no-whole-archive" "-Wl,--whole-archive" 
"/tmp/rustcNwuuSz/libgimli-d3394c27272c923f.rlib" "-Wl,--no-whole-archive" "-Wl,--whole-archive" "/tmp/rustcNwuuSz
/librustc_demangle-f23f929a57821d9c.rlib" "-Wl,--no-whole-archive" "-Wl,--whole-archive" "/tmp/rustcNwuuSz/libhashbrown-
c0fae21465d0a565.rlib" "-Wl,--no-whole-archive" "-Wl,--whole-archive" "/tmp/rustcNwuuSz/librustc_std_workspace_alloc-
47bf5f3747427081.rlib" "-Wl,--no-whole-archive" "-Wl,--whole-archive" "/tmp/rustcNwuuSz/libunwind-1d3836a614333674.rlib" 
"-Wl,--no-whole-archive" "-Wl,--whole-archive" "/tmp/rustcNwuuSz/libcfg_if-60ad5ac86dc5ee52.rlib" "-Wl,--no-whole-archive" 
"-Wl,--whole-archive" "/tmp/rustcNwuuSz/liblibc-2691d47254b3dc4d.rlib" "-Wl,--no-whole-archive" "-Wl,--whole-archive" 
"/tmp/rustcNwuuSz/liballoc-3abb241bf7490d2e.rlib" "-Wl,--no-whole-archive" "-Wl,--whole-archive" "/tmp/rustcNwuuSz
/librustc_std_workspace_core-37fd06111f9c0876.rlib" "-Wl,--no-whole-archive" "-Wl,--whole-archive" "/tmp/rustcNwuuSz/libcore-
94e9e31a9bba2722.rlib" "-Wl,--no-whole-archive" "-Wl,--end-group" "/tmp/rustcNwuuSz/libcompiler_builtins-
de55a90df56ad8f9.rlib" "-Wl,-Bdynamic" "-ldl" "-llog" "-lgcc" "-lc" "-lm"

  = note: ld: error: /home/dev/orwell/deps/ffmpeg/build/android/x86_64/libavcodec.so is incompatible with armelf_linux_eabi
          ld: error: /home/dev/orwell/deps/ffmpeg/build/android/x86_64/libavutil.so is incompatible with armelf_linux_eabi
          ld: error: /home/dev/orwell/deps/ffmpeg/build/android/x86_64/libswscale.so is incompatible with armelf_linux_eabi
          ld: error: /home/dev/orwell/deps/ffmpeg/build/android/x86_64/libswresample.so is incompatible with armelf_linux_eabi
          clang: error: linker command failed with exit code 1 (use -v to see invocation)
          /opt/android-sdk-linux/ndk/22.1.7171670/toolchains/llvm/prebuilt/linux-x86_64/bin/armv7a-linux-androideabi21-clang -Wl,-
soname,liborwell_rust_android.so -Wl,--as-needed -Wl,-z,noexecstack -Wl,--allow-multiple-definition -march=armv7-a -Wl,--eh-
frame-hdr -L /usr/local/rustup/toolchains/1.52.1-x86_64-unknown-linux-gnu/lib/rustlib/armv7-linux-androideabi/lib /home/dev/orwell
/flutter_orwell_app/android/app/src/main/rust/liborwell_rust_android/target/armv7-linux-androideabi/debug
/deps/orwell_rust_android.orwell_rust_android.1puiopvy-cgu.0.rcgu.o /home/dev/orwell/flutter_orwell_app/android/app/src
/main/rust/liborwell_rust_android/target/armv7-linux-androideabi/debug/deps/orwell_rust_android.orwell_rust_android.1puiopvy-
cgu.1.rcgu.o /home/dev/orwell/flutter_orwell_app/android/app/src/main/rust/liborwell_rust_android/target/armv7-linux-androideabi
/debug/deps/orwell_rust_android.orwell_rust_android.1puiopvy-cgu.2.rcgu.o /home/dev/orwell/flutter_orwell_app/android/app/src
/main/rust/liborwell_rust_android/target/armv7-linux-androideabi/debug/deps/orwell_rust_android.orwell_rust_android.1puiopvy-
cgu.3.rcgu.o /home/dev/orwell/flutter_orwell_app/android/app/src/main/rust/liborwell_rust_android/target/armv7-linux-androideabi
/debug/deps/orwell_rust_android.orwell_rust_android.1puiopvy-cgu.4.rcgu.o /home/dev/orwell/flutter_orwell_app/android/app/src
/main/rust/liborwell_rust_android/target/armv7-linux-androideabi/debug/deps/orwell_rust_android.orwell_rust_android.1puiopvy-
cgu.5.rcgu.o -o /home/dev/orwell/flutter_orwell_app/android/app/src/main/rust/liborwell_rust_android/target/armv7-linux-
androideabi/debug/deps/liborwell_rust_android.so -Wl,--version-script=/tmp/rustcNwuuSz/list /home/dev/orwell/flutter_orwell_app
/android/app/src/main/rust/liborwell_rust_android/target/armv7-linux-androideabi/debug
/deps/orwell_rust_android.1lkyuccy1x9dir34.rcgu.o /home/dev/orwell/flutter_orwell_app/android/app/src/main/rust
/liborwell_rust_android/target/armv7-linux-androideabi/debug/deps/orwell_rust_android.porrrqbj37xx5a2.rcgu.o -shared -Wl,-zrelro 
-Wl,-znow -nodefaultlibs -L /home/dev/orwell/flutter_orwell_app/android/app/src/main/rust/liborwell_rust_android/target/armv7-
linux-androideabi/debug/deps -L /home/dev/orwell/flutter_orwell_app/android/app/src/main/rust/liborwell_rust_android/target
/debug/deps -L /home/dev/orwell/deps/ffmpeg/build/android/x86_64 -L /usr/local/rustup/toolchains/1.52.1-x86_64-unknown-linux-
gnu/lib/rustlib/armv7-linux-androideabi/lib -lavcodec -lavutil -lswscale -lswresample -Wl,-Bstatic -Wl,--whole-archive 
/tmp/rustcNwuuSz/libjni-f3124597b53f380c.rlib -Wl,--no-whole-archive -Wl,--whole-archive /tmp/rustcNwuuSz/libcesu8-
f01a964297c1f0d1.rlib -Wl,--no-whole-archive -Wl,--whole-archive /tmp/rustcNwuuSz/liblog-0b4cb90a7c9f797e.rlib -Wl,--no-
whole-archive -Wl,--whole-archive /tmp/rustcNwuuSz/libcfg_if-887c440cb135db11.rlib -Wl,--no-whole-archive -Wl,--whole-archive 
/tmp/rustcNwuuSz/libcombine-75e5d5aa2f594e2f.rlib -Wl,--no-whole-archive -Wl,--whole-archive /tmp/rustcNwuuSz/libmemchr-
61ea3eb5f5694de9.rlib -Wl,--no-whole-archive -Wl,--whole-archive /tmp/rustcNwuuSz/libbytes-1af04e93b796429f.rlib -Wl,--no-
whole-archive -Wl,--whole-archive /tmp/rustcNwuuSz/libthiserror-6f95056aba1d6d91.rlib -Wl,--no-whole-archive -Wl,--whole-
archive /tmp/rustcNwuuSz/libjni_sys-b384ed00a5c66d42.rlib -Wl,--no-whole-archive -Wl,--start-group -Wl,--whole-archive 
/tmp/rustcNwuuSz/libstd-9645fb52a10e1442.rlib -Wl,--no-whole-archive -Wl,--whole-archive /tmp/rustcNwuuSz/libpanic_unwind-
a1bdb04986d578f8.rlib -Wl,--no-whole-archive -Wl,--whole-archive /tmp/rustcNwuuSz/libminiz_oxide-682faed215453b78.rlib 
-Wl,--no-whole-archive -Wl,--whole-archive /tmp/rustcNwuuSz/libadler-419dd832270dc9b9.rlib -Wl,--no-whole-archive -Wl,--whole-
archive /tmp/rustcNwuuSz/libobject-46592f53dff83526.rlib -Wl,--no-whole-archive -Wl,--whole-archive /tmp/rustcNwuuSz
/libaddr2line-bc61ed3a4a3adba9.rlib -Wl,--no-whole-archive -Wl,--whole-archive /tmp/rustcNwuuSz/libgimli-
d3394c27272c923f.rlib -Wl,--no-whole-archive -Wl,--whole-archive /tmp/rustcNwuuSz/librustc_demangle-f23f929a57821d9c.rlib 
-Wl,--no-whole-archive -Wl,--whole-archive /tmp/rustcNwuuSz/libhashbrown-c0fae21465d0a565.rlib -Wl,--no-whole-archive -Wl,--
whole-archive /tmp/rustcNwuuSz/librustc_std_workspace_alloc-47bf5f3747427081.rlib -Wl,--no-whole-archive -Wl,--whole-archive 
/tmp/rustcNwuuSz/libunwind-1d3836a614333674.rlib -Wl,--no-whole-archive -Wl,--whole-archive /tmp/rustcNwuuSz/libcfg_if-
60ad5ac86dc5ee52.rlib -Wl,--no-whole-archive -Wl,--whole-archive /tmp/rustcNwuuSz/liblibc-2691d47254b3dc4d.rlib -Wl,--no-
whole-archive -Wl,--whole-archive /tmp/rustcNwuuSz/liballoc-3abb241bf7490d2e.rlib -Wl,--no-whole-archive -Wl,--whole-archive 
/tmp/rustcNwuuSz/librustc_std_workspace_core-37fd06111f9c0876.rlib -Wl,--no-whole-archive -Wl,--whole-archive 
/tmp/rustcNwuuSz/libcore-94e9e31a9bba2722.rlib -Wl,--no-whole-archive -Wl,--end-group /tmp/rustcNwuuSz/libcompiler_builtins-
de55a90df56ad8f9.rlib -Wl,-Bdynamic -ldl -llog -lgcc -lc -lm

It's finding the libs at "/home/dev/orwell/deps/ffmpeg/build/android/x86_64"`` but the target isarmv7` so it says incompatible elf.

The problem is not in cargo build but actually in the linker wrapper. Look:

"/home/dev/orwell/flutter_orwell_app/build/linker-wrapper/linker-wrapper.sh" "-Wl,--as-needed" "-Wl,-z,noexecstack" ...

linker wrapper is being called with "-L" "/home/dev/orwell/deps/ffmpeg/build/android/x86_64" but the target is armv7.

I couldn't find the line where the linker-wrapper is called, but it should be where the problem happens. Linker wrapper is defined here: https://github.com/mozilla/rust-android-gradle/blob/master/plugin/src/main/kotlin/com/nishtahir/CargoBuildTask.kt#L212 but I can't see where it's called. If I could find it, I'd know how it gets its arguments, and thus know why it passes just "-L" "/home/dev/orwell/deps/ffmpeg/build/android/x86_64"

I had the idea to pass multiple paths:


    println!("{}", format!("cargo:rustc-link-search=native={}", format!("{}/{}", ffmpeg_location, "android/arm64-v8a")));
    println!("{}", format!("cargo:rustc-link-search=native={}", format!("{}/{}", ffmpeg_location, "android/armeabi-v7a")));
    println!("{}", format!("cargo:rustc-link-search=native={}", format!("{}/{}", ffmpeg_location, "android/x86")));
    println!("{}", format!("cargo:rustc-link-search=native={}", format!("{}/{}", ffmpeg_location, "android/x86_64")));

so it would find the right one, but this didn't work, it simply chooses the last one (""android/x86_64"") to look for the libraries. I think this proves it has nothing to do with cargo but with the linker wrapper?