Skip to content

Commit 364ff39

Browse files
committed
Support use of namespaced environment variables based on target triplet and BUILD_KIND
1 parent d32b244 commit 364ff39

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,31 @@ fn main() {
1111
}
1212
```
1313

14+
# External configuration via environment variables
15+
16+
To control the programs and flags used for building, the builder can set a number of different enviroment variables.
17+
* `CFLAGS` - a series of space seperated flags passed to "gcc". Note that
18+
individual flags cannot currently contain spaces, so doing
19+
something like: "-L=foo\ bar" is not possible.
20+
* `CC` - the actual c compiler used. Note that this is used as an exact
21+
executable name, so (for example) no extra flags can be passed inside
22+
this variable, and the builder must ensure that there aren't any
23+
trailing spaces. This compiler must understand the `-c` flag. For
24+
certain `TARGET`s, it also is assumed to know about other flags (most
25+
common is `-fPIC`).
26+
* `AR` - the `ar` (archiver) executable to use to build the static library.
27+
28+
Each of these variables can also be supplied with certain prefixes and suffixes, in the following prioritized order:
29+
30+
1. `<var>_<target>` - for example, `CC_x86_64-unknown-linux-gnu`
31+
1. `<var>_<target_with_underscores>` - for example, `CC_x86_64_unknown_linux_gnu`
32+
1. `<build-kind>_<var>` - for example, `HOST_CC` or `TARGET_CFLAGS`
33+
1. `<var>` - a plain `CC`, `AR` as above.
34+
35+
If none of these varaibles exist, gcc-rs uses built-in defaults
36+
37+
In addition to the the above optional environment variables, `gcc-rs` has some functions with hard requirements on some variables supplied by [cargo's build-script driver][cargo] that it has the `TARGET`, `OUT_DIR`, `OPT_LEVEL`, and `BUILD_KIND` variables
38+
1439
# Windows notes
1540

1641
Currently use of this crate means that Windows users will require gcc to be

src/lib.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,20 +109,32 @@ fn run(cmd: &mut Command) {
109109
}
110110
}
111111

112+
fn get_var(var_base: &str) -> Option<String> {
113+
let target = os::getenv("TARGET").unwrap();
114+
let target_u = target.split('-')
115+
.collect::<Vec<&str>>()
116+
.connect("_");
117+
let kind = os::getenv("BUILD_KIND").unwrap();
118+
os::getenv(format!("{}_{}", var_base, target).as_slice())
119+
.or_else(|| os::getenv(format!("{}_{}", var_base, target_u).as_slice()))
120+
.or_else(|| os::getenv(format!("{}_{}", kind, var_base).as_slice()))
121+
.or_else(|| os::getenv(var_base))
122+
}
123+
112124
fn gcc() -> String {
113-
os::getenv("CC").unwrap_or(if cfg!(windows) {
125+
get_var("CC").unwrap_or(if cfg!(windows) {
114126
"gcc".to_string()
115127
} else {
116128
"cc".to_string()
117129
})
118130
}
119131

120132
fn ar() -> String {
121-
os::getenv("AR").unwrap_or("ar".to_string())
133+
get_var("AR").unwrap_or("ar".to_string())
122134
}
123135

124136
fn cflags() -> Vec<String> {
125-
os::getenv("CFLAGS").unwrap_or(String::new())
137+
get_var("CFLAGS").unwrap_or(String::new())
126138
.as_slice().words().map(|s| s.to_string())
127139
.collect()
128140
}

0 commit comments

Comments
 (0)