rust-lang / cc-rs

Rust library for build scripts to compile C/C++ code into a Rust library
https://docs.rs/cc
Apache License 2.0
1.87k stars 452 forks source link

Regression for MacOS using cmake with c++11 (unknown target aarch64-apple-darwin11) #1276

Open drewkett opened 2 weeks ago

drewkett commented 2 weeks ago

With version 1.1.32, it now is not possible to build a project on MacOS using the cmake crate enabling c++11. I'm not sure whether to make this issue here or in cmake-rs but i'll make it here because its specifically a release of cc that broke it.

See this example

Cargo.toml

[package]
name = "cmake_example"
version = "0.1.0"
edition = "2021"

[build-dependencies]
cmake = "0.1.51"

build.rs

fn main() {
    cmake::Config::new("src").uses_cxx11().build();
}

"src" is irrelevant here.

This fails to build with

% cargo build
   Compiling cmake_example v0.1.0 (/Users/andrew/cmake_example)
error: failed to run custom build command for `cmake_example v0.1.0 (/Users/andrew/cmake_example)`

Caused by:
  process didn't exit successfully: `/Users/andrew/cmake_example/target/debug/build/cmake_example-066fd92fa2dd9dd8/build-script-build` (exit status: 1)
  --- stdout
  CMAKE_TOOLCHAIN_FILE_aarch64-apple-darwin = None
  CMAKE_TOOLCHAIN_FILE_aarch64_apple_darwin = None
  HOST_CMAKE_TOOLCHAIN_FILE = None
  CMAKE_TOOLCHAIN_FILE = None
  CMAKE_GENERATOR_aarch64-apple-darwin = None
  CMAKE_GENERATOR_aarch64_apple_darwin = None
  HOST_CMAKE_GENERATOR = None
  CMAKE_GENERATOR = None

  --- stderr

  error occurred: unknown target `aarch64-apple-darwin11`

Looking at cmake code, it appends 11 as the target for C++11. My understanding is that darwin11 here is the MacOS clang way of specifying the minimum target MacOS version. In the cmake crate, the TARGET env var is set with that aarch64-apple-darwin11 which then causes cc to fail when trying to parse the target to a target triple.

After having typed this out, I'm guessing i can in all likelihood just stop setting uses_cxx11 from cmake because any toolchains i'd be compiling with likely have C++11 available. I also wonder if instead cmake could use a different mechanism to ensure a minimum toolchain version, but I don't know enough about how that works to have an immediate answer there. However, I'll leave this ticket here to call out the issue (and in case others run into the same thing).

madsmtm commented 2 weeks ago

I think this is a cmake issue, they are modifying the Rust target triple and adding 11 to it, see https://github.com/rust-lang/cmake-rs/pull/39.

This is redundant nowadays, where:

  1. The minimum supported OS version in Rust is way above macOS 10.7
  2. cc sets this value in the deployment target.

And wrong because the target value in cc is the rustc target, while cmake invalidly assumed it refers to the Clang target.