Skip to content

Commit

Permalink
Support use of namespaced environment variables based on TARGET and HOST
Browse files Browse the repository at this point in the history
  • Loading branch information
codyps committed Dec 4, 2014
1 parent d32b244 commit 9420532
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,31 @@ fn main() {
}
```

# External configuration via environment variables

To control the programs and flags used for building, the builder can set a number of different enviroment variables.
* `CFLAGS` - a series of space seperated flags passed to "gcc". Note that
individual flags cannot currently contain spaces, so doing
something like: "-L=foo\ bar" is not possible.
* `CC` - the actual c compiler used. Note that this is used as an exact
executable name, so (for example) no extra flags can be passed inside
this variable, and the builder must ensure that there aren't any
trailing spaces. This compiler must understand the `-c` flag. For
certain `TARGET`s, it also is assumed to know about other flags (most
common is `-fPIC`).
* `AR` - the `ar` (archiver) executable to use to build the static library.

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`
1. `<var>_<target_with_underscores>` - for example, `CC_x86_64_unknown_linux_gnu`
1. `<build-kind>_<var>` - for example, `HOST_CC` or `TARGET_CFLAGS`
1. `<var>` - a plain `CC`, `AR` as above.

If none of these varaibles exist, gcc-rs uses built-in defaults

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

# Windows notes

Currently use of this crate means that Windows users will require gcc to be
Expand Down
21 changes: 18 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,20 +109,35 @@ fn run(cmd: &mut Command) {
}
}

fn get_var(var_base: &str) -> Option<String> {
let target = os::getenv("TARGET")
.expect("Environment variable 'TARGET' is unset");
let host = os::getenv("HOST")
.expect("Environment variable 'HOST' is unset");
let kind = if host == target { "HOST" } else { "TARGET" };
let target_u = target.split('-')
.collect::<Vec<&str>>()
.connect("_");
os::getenv(format!("{}_{}", var_base, target).as_slice())
.or_else(|| os::getenv(format!("{}_{}", var_base, target_u).as_slice()))
.or_else(|| os::getenv(format!("{}_{}", kind, var_base).as_slice()))
.or_else(|| os::getenv(var_base))
}

fn gcc() -> String {
os::getenv("CC").unwrap_or(if cfg!(windows) {
get_var("CC").unwrap_or(if cfg!(windows) {
"gcc".to_string()
} else {
"cc".to_string()
})
}

fn ar() -> String {
os::getenv("AR").unwrap_or("ar".to_string())
get_var("AR").unwrap_or("ar".to_string())
}

fn cflags() -> Vec<String> {
os::getenv("CFLAGS").unwrap_or(String::new())
get_var("CFLAGS").unwrap_or(String::new())
.as_slice().words().map(|s| s.to_string())
.collect()
}
Expand Down

0 comments on commit 9420532

Please sign in to comment.