cc-rs icon indicating copy to clipboard operation
cc-rs copied to clipboard

Set default compiler to clang on apple hosts

Open spl opened this issue 6 years ago • 5 comments

The change in this PR (615c53bf23b3a026b90a613c6364dd6ade869cd1) seems to be the right choice to make. clang/clang++ should be considered the default on macOS, and it allows the ToolFamily to be chosen correctly based on the name.

Many tests fail, however, and I believe that's because (a) they are duplicated for Linux and macOS targets and (b) each uses the same target and host. For example, consider this test from tests/test.rs:

#[test]
fn gnu_x86_64() {
    for vendor in &["unknown-linux-gnu", "apple-darwin"] {
        let target = format!("x86_64-{}", vendor);
        let test = Test::gnu();
        test.gcc()
            .target(&target)
            .host(&target)
            .file("foo.c")
            .compile("foo");
        test.cmd(0).must_have("-fPIC").must_have("-m64");
    }   
}

So, first, is my premise about choosing the default compiler on macOS is correct? If so, how should the tests change?

spl avatar May 30 '19 17:05 spl

This is how one test for the target and host x86_64-unknown-linux-gnu fails on Azure Pipelines:

test gnu_i686 ... cargo:warning=clang: error: no such file or directory: 'foo.c'
cargo:warning=clang: error: no input files


error occurred: Command "clang" "-O2" "-ffunction-sections" "-fdata-sections" "-fPIC" "--target=i686-apple-darwin" "-Wall" "-Wextra" "-o" "/tmp/gcc-test.TJJ1vYB38xne/foo.o" "-c" "foo.c" with args "clang" did not execute successfully (status code exit code: 1).

spl avatar May 30 '19 17:05 spl

Seems reasonable to me! I'm not sure why CI didn't fire here, but this plus with fixed tests sounds reasonableto me.

alexcrichton avatar May 31 '19 14:05 alexcrichton

What's the best way to fix the tests?

It's not clear to me why the host and target are both set to the same or even why the host is set at all? For one thing, I think you'd want any current or future logic in cc-rs to be able to assume the host has the expected environment, whereas using x86_64-apple-darwin as the host on a Linux machine does not fit that expectation.

Would it be acceptable to let the tests assume the actual host instead of the target as the host?

spl avatar Jun 01 '19 18:06 spl

Okay, I have better understanding of the test infrastructure. Ignore my musings above. I'm currently performing some renovations to the tests.

spl avatar Jun 03 '19 13:06 spl

Ok thanks for that!

I wonder if in the meantime that https://github.com/alexcrichton/cc-rs/issues/408 is a better solution for this? I'm somewhat wary of changing defaults as that can have weird unintended rippling effects, but if we can detect that /usr/bin/cc is a symlink to a binary called clang that seems like a pretty clear indicator that it's clang-by-default rather than gcc

alexcrichton avatar Jun 03 '19 15:06 alexcrichton