Skip to content

Support use of namespaced environment variables based on "TARGET" and "HOST" #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 5, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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