Skip to content
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

Update to 2018 edition #2

Closed
wants to merge 7 commits into from
Closed
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
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ readme = "README.md"
keywords = []
categories = ["development-tools"]
license = "MIT/Apache-2.0"
edition = "2018"

[badges]
travis-ci = { repository = "DanielKeep/rust-build-helper" }
Expand All @@ -26,4 +27,4 @@ members = [
]

[dependencies]
semver = "0.6.0"
semver = "0.9.0"
136 changes: 71 additions & 65 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ This should only be used on variables which are *guaranteed* to be defined by Ca
*/
macro_rules! env_var {
($name:expr) => {
::std::env::var($name)
.expect(concat!($name, " environment variable is not set"))
::std::env::var($name).expect(concat!($name, " environment variable is not set"))
};
}

Expand All @@ -65,8 +64,7 @@ This should only be used on variables which are *guaranteed* to be defined by Ca
*/
macro_rules! env_var_os {
($name:expr) => {
::std::env::var_os($name)
.expect(concat!($name, " environment variable is not set"))
::std::env::var_os($name).expect(concat!($name, " environment variable is not set"))
};
}

Expand All @@ -76,23 +74,22 @@ Reads, unwraps, and parses an environment variable.
This should only be used on variables which are *guaranteed* to be defined by Cargo.
*/
macro_rules! parse_env_var {
(try: $name:expr, $ty_desc:expr) => {
{
::std::env::var($name)
.ok()
.map(|v| v.parse()
.expect(&format!(concat!($name, " {:?} is not a valid ", $ty_desc), v))
)
}
};

($name:expr, $ty_desc:expr) => {
{
let v = env_var!($name);
v.parse()
.expect(&format!(concat!($name, " {:?} is not a valid ", $ty_desc), v))
}
};
(try: $name:expr, $ty_desc:expr) => {{
::std::env::var($name).ok().map(|v| {
v.parse().expect(&format!(
concat!($name, " {:?} is not a valid ", $ty_desc),
v
))
})
}};

($name:expr, $ty_desc:expr) => {{
let v = env_var!($name);
v.parse().expect(&format!(
concat!($name, " {:?} is not a valid ", $ty_desc),
v
))
}};
}

/**
Expand Down Expand Up @@ -150,7 +147,7 @@ impl FromStr for Atomic {
if s == "ptr" {
Ok(Atomic::Pointer)
} else if let Ok(bits) = s.parse() {
Ok(Atomic::Integer { bits: bits })
Ok(Atomic::Integer { bits })
} else {
Err(InvalidInput(s.into()))
}
Expand Down Expand Up @@ -182,7 +179,7 @@ impl FromStr for Endianness {
match s {
"big" => Ok(Endianness::Big),
"little" => Ok(Endianness::Little),
_ => Err(InvalidInput(s.into()))
_ => Err(InvalidInput(s.into())),
}
}
}
Expand Down Expand Up @@ -220,7 +217,7 @@ impl FromStr for LibKind {
"static" => Ok(LibKind::Static),
"dylib" => Ok(LibKind::DyLib),
"framework" => Ok(LibKind::Framework),
_ => Err(InvalidInput(s.into()))
_ => Err(InvalidInput(s.into())),
}
}
}
Expand Down Expand Up @@ -250,7 +247,7 @@ impl FromStr for Profile {
match s {
"debug" => Ok(Profile::Debug),
"release" => Ok(Profile::Release),
_ => Err(InvalidInput(s.into()))
_ => Err(InvalidInput(s.into())),
}
}
}
Expand Down Expand Up @@ -289,7 +286,7 @@ impl FromStr for SearchKind {
"native" => Ok(SearchKind::Native),
"framework" => Ok(SearchKind::Framework),
"all" => Ok(SearchKind::All),
_ => Err(InvalidInput(s.into()))
_ => Err(InvalidInput(s.into())),
}
}
}
Expand All @@ -315,24 +312,29 @@ impl Triple {
let os;

{
let mut parts = triple.splitn(4, '-')
.map(|s| {
let off = subslice_offset(&triple, s);
off..(off + s.len())
});

arch = parts.next().expect(&format!("could not find architecture in triple {:?}", triple));
family = parts.next().expect(&format!("could not find family in triple {:?}", triple));
os = parts.next().expect(&format!("could not find os in triple {:?}", triple));
let mut parts = triple.splitn(4, '-').map(|s| {
let off = subslice_offset(&triple, s);
off..(off + s.len())
});

arch = parts
.next()
.unwrap_or_else(|| panic!("could not find architecture in triple {:?}", triple));
family = parts
.next()
.unwrap_or_else(|| panic!("could not find family in triple {:?}", triple));
os = parts
.next()
.unwrap_or_else(|| panic!("could not find os in triple {:?}", triple));
env = parts.next();
}

Triple {
triple: triple,
arch: arch,
env: env,
family: family,
os: os,
triple,
arch,
env,
family,
os,
}
}

Expand All @@ -356,8 +358,7 @@ impl Triple {
Values include `"gnu"`, `"msvc"`, `"musl"`, `"android"` *etc.* Value is `None` if the platform doesn't specify an environment.
*/
pub fn env(&self) -> Option<&str> {
self.env.as_ref()
.map(|s| &self.triple[s.clone()])
self.env.as_ref().map(|s| &self.triple[s.clone()])
}

/**
Expand Down Expand Up @@ -428,7 +429,6 @@ pub mod cargo {
Package features.
*/
pub mod features {
use std::ascii::AsciiExt;
use std::env;

/**
Expand Down Expand Up @@ -459,9 +459,7 @@ pub mod cargo {
Due to the name mangling used by Cargo, features are returned as all lower case, with hypens instead of underscores.
*/
pub fn all() -> Iter {
Iter {
iter: env::vars(),
}
Iter { iter: env::vars() }
}

/// Determine if a specific feature is enabled.
Expand All @@ -475,7 +473,7 @@ pub mod cargo {
.map(|c| match c {
'A'...'Z' => c.to_ascii_lowercase(),
'_' => '-',
c => c
c => c,
})
.collect()
}
Expand All @@ -485,7 +483,7 @@ pub mod cargo {
.map(|c| match c {
'a'...'z' => c.to_ascii_uppercase(),
'-' => '_',
c => c
c => c,
})
.collect()
}
Expand Down Expand Up @@ -577,23 +575,29 @@ pub mod metadata {
Functions for communicating with `rustc`.
*/
pub mod rustc {
use std::path::Path;
use ::{LibKind, SearchKind};

use crate::{LibKind, SearchKind};
use std::path::Path;
/// Link a library into the output.
pub fn link_lib<P: AsRef<Path>>(link_kind: Option<LibKind>, name: P) {
println!("cargo:rustc-link-lib={}{}",
link_kind.map(|v| format!("{}=", v))
println!(
"cargo:rustc-link-lib={}{}",
link_kind
.map(|v| format!("{}=", v))
.unwrap_or_else(|| "".into()),
name.as_ref().display());
name.as_ref().display()
);
}

/// Add a search directory.
pub fn link_search<P: AsRef<Path>>(link_kind: Option<SearchKind>, path: P) {
println!("cargo:rustc-link-search={}{}",
link_kind.map(|v| format!("{}=", v))
println!(
"cargo:rustc-link-search={}{}",
link_kind
.map(|v| format!("{}=", v))
.unwrap_or_else(|| "".into()),
path.as_ref().display());
path.as_ref().display()
);
}

/**
Expand Down Expand Up @@ -649,15 +653,17 @@ pub mod target {
*/
#[cfg(feature = "nightly")]
pub fn has_atomic() -> Option<Vec<Atomic>> {
env::var("CARGO_CFG_TARGET_HAS_ATOMIC")
.ok()
.map(|v| {
v.split(',')
.map(|s| s.parse()
.expect(&format!("CARGO_CFG_TARGET_HAS_ATOMIC \
contained invalid atomic type {:?}", s)))
.collect()
})
env::var("CARGO_CFG_TARGET_HAS_ATOMIC").ok().map(|v| {
v.split(',')
.map(|s| {
s.parse().expect(&format!(
"CARGO_CFG_TARGET_HAS_ATOMIC \
contained invalid atomic type {:?}",
s
))
})
.collect()
})
}

/**
Expand Down
16 changes: 12 additions & 4 deletions tests/pkgs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,21 @@ Licensed under the MIT license (see LICENSE or <http://opensource.org
files in the project carrying such notice may not be copied, modified,
or distributed except according to those terms.
*/
#[macro_use] mod util;

mod util;

macro_rules! test_pkg {
($name:expr) => {
cargo!("build", "--manifest-path", concat!("tests/pkgs/", $name, "/Cargo.toml"))
.expect(concat!("failed to build command to build tests/pkgs/", $name))
.succeeded(concat!("failed to build tests/pkgs/", $name))
cargo!(
"build",
"--manifest-path",
concat!("tests/pkgs/", $name, "/Cargo.toml")
)
.expect(concat!(
"failed to build command to build tests/pkgs/",
$name
))
.succeeded(concat!("failed to build tests/pkgs/", $name))
};
}

Expand Down
35 changes: 24 additions & 11 deletions tests/pkgs/basic/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,48 @@ or distributed except according to those terms.
*/
extern crate build_helper;

use build_helper::*;
use build_helper::semver::Version;
use build_helper::*;

macro_rules! show {
($value:expr) => {
println!(concat!("DEBUG: ", stringify!($value), ": {:?}"), $value);
};
($value:expr, |$i:ident| $map:expr) => {
{
let $i = $value;
println!(concat!("DEBUG: ", stringify!($value), ": {:?}"), $map);
}
};
($value:expr, |$i:ident| $map:expr) => {{
let $i = $value;
println!(concat!("DEBUG: ", stringify!($value), ": {:?}"), $map);
}};
}

fn main() {
// For things which have a stable value, test for that value. For everything else, just ensure can read the value at all.
let _ = bin::cargo();
let _ = bin::rustc();
let _ = bin::rustdoc();
assert_eq!(cargo::features::all().collect::<Vec<_>>().sorted(), vec!["a", "default"]);
assert_eq!(
cargo::features::all().collect::<Vec<_>>().sorted(),
vec!["a", "default"]
);
assert!(cargo::features::enabled("a"));
assert!(!cargo::features::enabled("b"));
assert!(cargo::features::enabled("default"));
let _ = cargo::manifest::dir();
assert_eq!(cargo::manifest::links(), Some("awakening".into()));
assert_eq!(cargo::pkg::authors().sorted(), vec!["Daniel Keep <[email protected]>", "John Smith <null@null>"]);
assert_eq!(cargo::pkg::description().unwrap_or("".into()), "A description of this crate.");
assert_eq!(cargo::pkg::homepage().unwrap_or("".into()), "http://example.org/basic.rs");
assert_eq!(
cargo::pkg::authors().sorted(),
vec![
"Daniel Keep <[email protected]>",
"John Smith <null@null>"
]
);
assert_eq!(
cargo::pkg::description().unwrap_or("".into()),
"A description of this crate."
);
assert_eq!(
cargo::pkg::homepage().unwrap_or("".into()),
"http://example.org/basic.rs"
);
assert_eq!(cargo::pkg::name(), "basic");
assert_eq!(cargo::pkg::version(), Version::parse("1.2.3-pre").unwrap());
let _ = debug();
Expand Down
Loading