Cargo creates unexpected files


I thought I might improve @alexcrichton 's cc-rs to find the clang compiler when the Android NDK r19c is being used.
The directory layout of the NDK and the name of the compiler changed.
The name of the compiler is now dependent on the ANDROID_API
E.g.: aarch64-linux-android28-clang
Note the 28.

Android is deprecating gcc and moving the build tools to
More info is here: Use the NDK with other build systems  |  Android NDK  |  Android Developers

So I put something together here: GitHub - AxelNennker/cc-rs at android_ndk19

Bad things happen with cargo clean && cargo test --target=aarch64-linux-android

  • cargo creates files in the source directory
ignisvulpis@namenlos:~/development/cc-rs$ ls
azure-pipelines.yml  cc-test         LICENSE-MIT                  src     ???vF?H??9%O??????????<??????e?????_?????????????L??4????????????????8??????????s?????G?????#?8??????????R?]??j??????C??
Cargo.lock           ci              ????????P????x???????X??d??  target
Cargo.toml           LICENSE-APACHE                    tests
ignisvulpis@namenlos:~/development/cc-rs$ git status
On branch android_ndk19
Untracked files:
  (use "git add <file>..." to include in what will be committed)


nothing added to commit but untracked files present (use "git add" to track)

I think this should not happen.

I think the culprit is (again) my struggle with cfg

#[cfg(target_os = "android")]
fn test_android_append_host_tag() {
    if let Ok(ndk) = env::var("NDK") {
        let ndk= Path::new(&ndk);
        let host_tag = android_append_host_tag(&ndk.to_path_buf());
    } else {
        let ndk = android_get_ndk();
        match ndk {
            Ok(val) => {
                let host_tag = android_append_host_tag(&val);
            Err(_) => {}
//        let target = env::var("TARGET").unwrap();

I want to achieve that this test only runs when the user of cargo test has the Android SDK installed.
So cargo test works as expected.

cargo test --target=aarch64-linux-android produces this strange behaviour.

I commented out the two methods guarded by cfg. This stops the file from being produced but that still produces an error when the the tests are run:

ignisvulpis@namenlos:~/development/cc-rs$ cargo clean && cargo test  --target=aarch64-linux-android
   Compiling libc v0.2.55
   Compiling remove_dir_all v0.5.1
   Compiling cc v1.0.37 (/home/ignisvulpis/development/cc-rs)
   Compiling rand v0.4.6
   Compiling tempdir v0.3.7
    Finished dev [unoptimized + debuginfo] target(s) in 2.78s
     Running target/aarch64-linux-android/debug/deps/cc-a9cbc98948e5716d
/home/ignisvulpis/development/cc-rs/target/aarch64-linux-android/debug/deps/cc-a9cbc98948e5716d: 1: /home/ignisvulpis/development/cc-rs/target/aarch64-linux-android/debug/deps/cc-a9cbc98948e5716d: Syntax error: "(" unexpected
error: test failed, to rerun pass '--lib'

Completely remove the tests yields the same results.

Help welcome, thank you.


ignisvulpis@namenlos:~/development/cc-rs$ rustc --version
rustc 1.34.2 (6c2484dc3 2019-05-13)
ignisvulpis@namenlos:~/development/cc-rs$ uname -a
Linux namenlos 4.15.0-50-generic #54~16.04.1-Ubuntu SMP Wed May 8 15:55:19 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux

It would be great to have it just work! Just recently I needed to build a library for iOS and Android. For iOS it was a matter of just adding --target, and for Android it was a multi-step process.

The weird file looks like something may be putting file data in place of a file name (e.g. swapped put(filename, data) vs put(data, filename)). See what's in the file for clues.

&ndk.to_path_buf() is same as ndk (you make it owned and then borrow it), but that's not the source of the bug.

Not sure what is happening. I guess cargo builds some aarch64 executables and then runs them on x64_arm with the strange results.
What I really want to do is use cc-rs in another crate and use --target on that crate.
When I tell that crate to use my version of cc-rs by using path=../cc-rs the correct compiler is chosen.
Still it does not work out of the box because aarch64-linux-android28-clang is not on the PATH.
Setting the PATH fixes that but if I set the PATH then I can also set CC and CXX to the right compilers which is the whole point of the exercise.
So actually I need cc-rs to find the compilers with their full path for this to work.

I think I just remove the tests and make cc-rs work for --target=something-android-something-clang when called by cargo from another crate.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.