Repository: rust-lang/cc-rs (Rust)

Issue:
Selecting compiler specifically for `thumbv8m.main-none-eabi` and other `.` containing targets does not work
`cc-rs` allows users to override the compiler used for a specific target triple by setting the environment variable `CC_<triple>`, but for targets such as `thumv8m.main-none-eabi` this doesn't work as `.`-s are not allowed to be used in the name of an environment variable in (most?) shells, so setting e.g. `CC_thumbv8m.main_none_eabi` is not allowed.

It would be great if this could be supported, either by replacing the `.` with an `_`, or some other solution that allows us to actually set this environment variable.


Discussion:
Does using `env` command works?

Replacing underscore with `-` sounds good but we would basically have to iterate over all variable to do that and it's not very great
Ah, `env` seems to work, yes, thanks! I hadn't thought of that. Then closing this issue should be fine, unless someone feels like adding a note to the README w.r.t. using `env` for some of these.

When mentioning the replacement thing, I was just thinking that it could be added to [this existing function](https://github.com/rust-lang/cc-rs/blob/9b4389c78837c1c5201624ee8a5d95e4a37824e7/src/lib.rs#L3924), not some big new feature.
Yeah we can simply replace `-` with `_` and don't have to iterate over entire env vars, so it would work.

I would welcome a PR adding that.



Patch:
diff --git a/src/lib.rs b/src/lib.rs
index e63b7b0d8..e3d1cc251 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -114,8 +114,8 @@
 //! Each of these variables can also be supplied with certain prefixes and suffixes,
 //! in the following prioritized order:
 //!
-//!   1. `<var>_<target>` - for example, `CC_x86_64-unknown-linux-gnu`
-//!   2. `<var>_<target_with_underscores>` - for example, `CC_x86_64_unknown_linux_gnu`
+//!   1. `<var>_<target>` - for example, `CC_x86_64-unknown-linux-gnu` or `CC_thumbv8m.main-none-eabi`
+//!   2. `<var>_<target_with_underscores>` - for example, `CC_x86_64_unknown_linux_gnu` or `CC_thumbv8m_main_none_eabi` (both periods and underscores are replaced)
 //!   3. `<build-kind>_<var>` - for example, `HOST_CC` or `TARGET_CFLAGS`
 //!   4. `<var>` - a plain `CC`, `AR` as above.
 //!
@@ -3928,7 +3928,7 @@ impl Build {
         } else {
             "HOST"
         };
-        let target_u = target.replace('-', "_");
+        let target_u = target.replace(['-', '.'], "_");
 
         Ok([
             format!("{env}_{target}"),


Test patch:
diff --git a/tests/cc_env.rs b/tests/cc_env.rs
index 65015f783..5f090b32e 100644
--- a/tests/cc_env.rs
+++ b/tests/cc_env.rs
@@ -133,12 +133,14 @@ fn env_var_alternatives_override() {
     let compiler2 = format!("clang2{}", env::consts::EXE_SUFFIX);
     let compiler3 = format!("clang3{}", env::consts::EXE_SUFFIX);
     let compiler4 = format!("clang4{}", env::consts::EXE_SUFFIX);
+    let compiler5 = format!("clang5{}", env::consts::EXE_SUFFIX);
 
     let test = Test::new();
     test.shim(&compiler1);
     test.shim(&compiler2);
     test.shim(&compiler3);
     test.shim(&compiler4);
+    test.shim(&compiler5);
 
     env::set_var("CC", &compiler1);
     let compiler = test.gcc().target("x86_64-unknown-none").get_compiler();
@@ -156,4 +158,8 @@ fn env_var_alternatives_override() {
     env::set_var("CC_x86_64-unknown-none", &compiler4);
     let compiler = test.gcc().target("x86_64-unknown-none").get_compiler();
     assert_eq!(compiler.path(), Path::new(&compiler4));
+
+    env::set_var("CC_thumbv8m_main_none_eabi", &compiler5);
+    let compiler = test.gcc().target("thumbv8m.main-none-eabi").get_compiler();
+    assert_eq!(compiler.path(), Path::new(&compiler5));
 }