Skip to content

Commit 9420532

Browse files
committed
Support use of namespaced environment variables based on TARGET and HOST
1 parent d32b244 commit 9420532

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
lines changed

README.md

+25
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 `HOST` variables
38+
1439
# Windows notes
1540

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

src/lib.rs

+18-3
Original file line numberDiff line numberDiff line change
@@ -109,20 +109,35 @@ fn run(cmd: &mut Command) {
109109
}
110110
}
111111

112+
fn get_var(var_base: &str) -> Option<String> {
113+
let target = os::getenv("TARGET")
114+
.expect("Environment variable 'TARGET' is unset");
115+
let host = os::getenv("HOST")
116+
.expect("Environment variable 'HOST' is unset");
117+
let kind = if host == target { "HOST" } else { "TARGET" };
118+
let target_u = target.split('-')
119+
.collect::<Vec<&str>>()
120+
.connect("_");
121+
os::getenv(format!("{}_{}", var_base, target).as_slice())
122+
.or_else(|| os::getenv(format!("{}_{}", var_base, target_u).as_slice()))
123+
.or_else(|| os::getenv(format!("{}_{}", kind, var_base).as_slice()))
124+
.or_else(|| os::getenv(var_base))
125+
}
126+
112127
fn gcc() -> String {
113-
os::getenv("CC").unwrap_or(if cfg!(windows) {
128+
get_var("CC").unwrap_or(if cfg!(windows) {
114129
"gcc".to_string()
115130
} else {
116131
"cc".to_string()
117132
})
118133
}
119134

120135
fn ar() -> String {
121-
os::getenv("AR").unwrap_or("ar".to_string())
136+
get_var("AR").unwrap_or("ar".to_string())
122137
}
123138

124139
fn cflags() -> Vec<String> {
125-
os::getenv("CFLAGS").unwrap_or(String::new())
140+
get_var("CFLAGS").unwrap_or(String::new())
126141
.as_slice().words().map(|s| s.to_string())
127142
.collect()
128143
}

0 commit comments

Comments
 (0)