-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract Platform to a separate crate.
- Loading branch information
Showing
18 changed files
with
589 additions
and
265 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
[package] | ||
name = "cargo-platform" | ||
version = "0.1.0" | ||
authors = ["The Cargo Project Developers"] | ||
edition = "2018" | ||
license = "MIT OR Apache-2.0" | ||
homepage = "https://github.com/rust-lang/cargo" | ||
repository = "https://github.com/rust-lang/cargo" | ||
documentation = "https://docs.rs/cargo-platform" | ||
description = "Cargo's representation of a target platform." | ||
|
||
[dependencies] | ||
serde = { version = "1.0.82", features = ['derive'] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
//! This example demonstrates how to filter a Platform based on the current | ||
//! host target. | ||
use cargo_platform::{Cfg, Platform}; | ||
use std::process::Command; | ||
use std::str::FromStr; | ||
|
||
static EXAMPLES: &[&str] = &[ | ||
"cfg(windows)", | ||
"cfg(unix)", | ||
"cfg(target_os=\"macos\")", | ||
"cfg(target_os=\"linux\")", | ||
"cfg(any(target_arch=\"x86\", target_arch=\"x86_64\"))", | ||
]; | ||
|
||
fn main() { | ||
let target = get_target(); | ||
let cfgs = get_cfgs(); | ||
println!("host target={} cfgs:", target); | ||
for cfg in &cfgs { | ||
println!(" {}", cfg); | ||
} | ||
let mut examples: Vec<&str> = EXAMPLES.iter().copied().collect(); | ||
examples.push(target.as_str()); | ||
for example in examples { | ||
let p = Platform::from_str(example).unwrap(); | ||
println!("{:?} matches: {:?}", example, p.matches(&target, &cfgs)); | ||
} | ||
} | ||
|
||
fn get_target() -> String { | ||
let output = Command::new("rustc") | ||
.arg("-Vv") | ||
.output() | ||
.expect("rustc failed to run"); | ||
let stdout = String::from_utf8(output.stdout).unwrap(); | ||
for line in stdout.lines() { | ||
if line.starts_with("host: ") { | ||
return String::from(&line[6..]); | ||
} | ||
} | ||
panic!("Failed to find host: {}", stdout); | ||
} | ||
|
||
fn get_cfgs() -> Vec<Cfg> { | ||
let output = Command::new("rustc") | ||
.arg("--print=cfg") | ||
.output() | ||
.expect("rustc failed to run"); | ||
let stdout = String::from_utf8(output.stdout).unwrap(); | ||
stdout | ||
.lines() | ||
.map(|line| Cfg::from_str(line).unwrap()) | ||
.collect() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.