Cargo creates unexpected files

Hi,

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
$NDK/toolchains/llvm/prebuild/$HOST_TAG
More info is here: https://developer.android.com/ndk/guides/other_build_systems

So I put something together here: https://github.com/AxelNennker/cc-rs/tree/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  README.md                    tests
ignisvulpis@namenlos:~/development/cc-rs$ 
ignisvulpis@namenlos:~/development/cc-rs$ git status
On branch android_ndk19
Untracked files:
  (use "git add <file>..." to include in what will be committed)

	"\006\304\004\f\022\v\244\323P\304\f\021\023x\363\b\b\215\r\022\vX\205\004d\002\025"
	"\034\036\016vF\336H\230\3719%O\267\003\v\340\316\003\024\210\363\b<\022\377\002\022\356\022e\002\022\343\002\022_\022\371\002\022\223\022\034\002\022\345\022\264\022L\003\0224\001\022\022\003\022\333\002\022\225\001\022\246\002\022\255\0228\002\022\362\002\022\326\001\022\336\022s\001\022\037\001\022G\003\022\234\001\022#\0028\003\022\266\001\022\323\002\022\020\022R\022]\002\022j\001\022\365\022\300\022C\001\022"

nothing added to commit but untracked files present (use "git add" to track)
ignisvulpis@namenlos:~/development/cc-rs$

I think this should not happen.

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

#[cfg(target_os = "android")]
#[test]
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());
        assert!(host_tag.unwrap().is_dir());
    } else {
        let ndk = android_get_ndk();
        match ndk {
            Ok(val) => {
                let host_tag = android_append_host_tag(&val);
                assert!(host_tag.unwrap().is_dir());
            },
            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'
ignisvulpis@namenlos:~/development/cc-rs$ 

Completely remove the tests yields the same results.

Help welcome, thank you.

Axel

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
ignisvulpis@namenlos:~/development/cc-rs$ 

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.