Skip to content

Commit 1f1ccd0

Browse files
qqwaphil-opp
authored andcommitted
Adds command xrustc (#9)
1 parent 0989dd6 commit 1f1ccd0

File tree

7 files changed

+30
-18
lines changed

7 files changed

+30
-18
lines changed

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ name = "cargo-xbuild"
1010
repository = "https://github.com/rust-osdev/cargo-xbuild"
1111
version = "0.4.7"
1212

13+
[lib]
14+
name = "xargo_lib"
15+
1316
[dependencies]
1417
error-chain = "0.7.2"
1518
fs2 = "0.4.1"

src/bin/cargo-xbuild.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
extern crate xargo_lib;
2+
3+
pub fn main() {
4+
xargo_lib::main_common("build");
5+
}

src/bin/cargo-xrustc.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
extern crate xargo_lib;
2+
3+
pub fn main() {
4+
xargo_lib::main_common("rustc");
5+
}

src/cli.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ impl Args {
2727
}
2828
}
2929

30-
pub fn args() -> Result<(Command, Args), String> {
30+
pub fn args(command_name: &str) -> Result<(Command, Args), String> {
3131
let mut args = env::args().skip(1);
32-
if args.next() != Some("xbuild".into()) {
33-
Err("must be invoked as cargo subcommand: `cargo xbuild`")?;
32+
if args.next() != Some("x".to_string() + command_name) {
33+
Err(format!("must be invoked as cargo subcommand: `cargo x{}`", command_name))?;
3434
}
3535
let all = args.collect::<Vec<_>>();
3636
let command = match all.first().map(|s| s.as_str()) {

src/help.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
Wrapper for `cargo build` that cross-compiles core, compiler_builtins and alloc
1+
Wrapper for `cargo {command_name}` that cross-compiles core, compiler_builtins and alloc
22

33
USAGE:
4-
cargo xbuild [OPTIONS]
4+
cargo x{command_name} [OPTIONS]
55

66
OPTIONS:
77
-h, --help Prints help information and exit
88
---version Prints version information and exit
99

10-
Any additional options are directly passed to `cargo build` (see
11-
`cargo build --help` for possible options).
10+
Any additional options are directly passed to `cargo {command_name}` (see
11+
`cargo {command_name} --help` for possible options).
1212

1313
CONFIGURATION:
1414
Configuration is possible through a `package.metadata.cargo-xbuild` table

src/main.rs renamed to src/lib.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ mod sysroot;
3434
mod util;
3535
mod xargo;
3636

37-
const HELP: &str = include_str!("help.txt");
38-
3937
// We use a different sysroot for Native compilation to avoid file locking
4038
//
4139
// Cross compilation requires `lib/rustlib/$HOST` to match `rustc`'s sysroot,
@@ -84,12 +82,12 @@ impl CompilationMode {
8482
}
8583
}
8684

87-
pub fn main() {
85+
pub fn main_common(command_name: &str) {
8886
fn show_backtrace() -> bool {
8987
env::var("RUST_BACKTRACE").as_ref().map(|s| &s[..]) == Ok("1")
9088
}
9189

92-
match run() {
90+
match run(command_name) {
9391
Err(e) => {
9492
let stderr = io::stderr();
9593
let mut stderr = stderr.lock();
@@ -117,14 +115,14 @@ pub fn main() {
117115
}
118116
}
119117

120-
fn run() -> Result<Option<ExitStatus>> {
118+
fn run(command_name: &str) -> Result<Option<ExitStatus>> {
121119
use cli::Command;
122120

123-
let (command, args) = cli::args()?;
121+
let (command, args) = cli::args(command_name)?;
124122
match command {
125-
Command::Build => Ok(Some(build(args)?)),
123+
Command::Build => Ok(Some(build(args, command_name)?)),
126124
Command::Help => {
127-
print!("{}", HELP);
125+
print!(include_str!("help.txt"), command_name = command_name);
128126
Ok(None)
129127
}
130128
Command::Version => {
@@ -138,7 +136,7 @@ fn run() -> Result<Option<ExitStatus>> {
138136
}
139137
}
140138

141-
fn build(args: cli::Args) -> Result<(ExitStatus)> {
139+
fn build(args: cli::Args, command_name: &str) -> Result<(ExitStatus)> {
142140
let verbose = args.verbose();
143141
let meta = rustc::version();
144142
let cd = CurrentDirectory::get()?;
@@ -206,7 +204,7 @@ fn build(args: cli::Args) -> Result<(ExitStatus)> {
206204
&sysroot,
207205
verbose,
208206
)?;
209-
return xargo::run(&args, &cmode, rustflags, &home, &meta, verbose);
207+
return xargo::run(&args, &cmode, rustflags, &home, &meta, command_name, verbose);
210208
}
211209

212210
cargo::run(&args, verbose)

src/xargo.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@ pub fn run(
2020
rustflags: Rustflags,
2121
home: &Home,
2222
meta: &VersionMeta,
23+
command_name: &str,
2324
verbose: bool,
2425
) -> Result<ExitStatus> {
2526
let mut cmd = Command::new("cargo");
26-
cmd.arg("build");
27+
cmd.arg(command_name);
2728
cmd.args(args.all());
2829

2930
let flags = rustflags.for_xargo(home);

0 commit comments

Comments
 (0)