Skip to content

Commit

Permalink
Add additional build args for passing on to cargo (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
HexyWitch committed Mar 19, 2018
1 parent 7e79e37 commit a68cd7e
Show file tree
Hide file tree
Showing 3 changed files with 186 additions and 4 deletions.
32 changes: 32 additions & 0 deletions crates/wasm-build-support/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,24 @@ pub enum Error {

#[derive(Default)]
pub struct Options {
pub package: Option<String>,
pub all: bool,
pub exclude: Option<String>,
pub jobs: Option<String>,
pub lib: bool,
pub bin: Option<String>,
pub bins: bool,
pub all_targets: bool,
pub release: bool,
pub features: Option<String>,
pub all_features: bool,
pub no_default_features: bool,
pub manifest_path: Option<String>,
pub verbose: bool,
pub quiet: bool,
pub frozen: bool,
pub locked: bool,
pub cargo_flags: Option<String>,
}

pub enum PackageType {
Expand All @@ -32,8 +48,24 @@ pub struct TargetPackage {
pub fn build(options: &Options) -> Result<Vec<TargetPackage>, Error> {
println!("wasm-build: Starting cargo build step");
let cargo_options = cargo::BuildOptions {
package: options.package.clone(),
all: options.all.clone(),
exclude: options.exclude.clone(),
jobs: options.jobs.clone(),
lib: options.lib.clone(),
bin: options.bin.clone(),
bins: options.bins.clone(),
all_targets: options.all_targets.clone(),
release: options.release.clone(),
features: options.features.clone(),
all_features: options.all_features.clone(),
no_default_features: options.no_default_features.clone(),
manifest_path: options.manifest_path.clone(),
verbose: options.verbose.clone(),
quiet: options.quiet.clone(),
frozen: options.frozen.clone(),
locked: options.locked.clone(),
cargo_flags: options.cargo_flags.clone(),
};
let artifacts = cargo::build(&cargo_options).map_err(Error::CargoBuildError)?;

Expand Down
66 changes: 64 additions & 2 deletions crates/wasm-build-support/src/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,24 @@ pub enum Error {

#[derive(Default)]
pub struct BuildOptions {
pub package: Option<String>,
pub all: bool,
pub exclude: Option<String>,
pub jobs: Option<String>,
pub lib: bool,
pub bin: Option<String>,
pub bins: bool,
pub all_targets: bool,
pub release: bool,
pub features: Option<String>,
pub all_features: bool,
pub no_default_features: bool,
pub manifest_path: Option<String>,
pub verbose: bool,
pub quiet: bool,
pub frozen: bool,
pub locked: bool,
pub cargo_flags: Option<String>,
}

#[derive(Debug)]
Expand Down Expand Up @@ -180,18 +196,64 @@ pub fn build(options: &BuildOptions) -> Result<Vec<WasmArtifact>, Error> {
cmd.stdout(Stdio::piped())
.arg("build")
.arg("--target=wasm32-unknown-unknown")
.arg("--release")
.args(&["--message-format", "json"]);

if let Some(ref package) = options.package {
cmd.arg("--package").arg(package);
}
if options.all {
cmd.arg("--all");
}
if let Some(ref exclude) = options.exclude {
cmd.arg("--exclude").arg(exclude);
}
if let Some(ref jobs) = options.jobs {
cmd.arg("--jobs").arg(jobs);
}
if options.lib {
cmd.arg("--lib");
}
if let Some(ref bin) = options.bin {
cmd.arg("--bin").arg(bin);
}
if options.bins {
cmd.arg("--bins");
}
if options.all_targets {
cmd.arg("--all_targets");
}
if options.release {
cmd.arg("--release");
}
if let Some(ref features) = options.features {
cmd.arg("--features").arg(features);
}
if options.all_features {
cmd.arg("--all_features");
}
if options.no_default_features {
cmd.arg("--no_default_features");
}
if let Some(ref manifest_path) = options.manifest_path {
cmd.arg("--manifest_path").arg(manifest_path);
}
if options.verbose {
cmd.arg("--verbose");
}
if options.quiet {
cmd.arg("--quiet");
}
if options.frozen {
cmd.arg("--frozen");
}
if options.locked {
cmd.arg("--locked");
}
if let Some(ref cargo_flags) = options.cargo_flags {
cmd.arg("--cargo_flags").arg(cargo_flags);
}

let child = cmd.spawn().map_err(|e| Error::RunCommandError(e))?;

let stdout = BufReader::new(child.stdout.ok_or_else(|| Error::CaptureStdoutError)?);

let mut artifacts = Vec::new();
Expand Down
92 changes: 90 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,81 @@ use wasm_build_support::build;

fn shared_args<'a, 'b>() -> Vec<Arg<'a, 'b>> {
vec![
Arg::with_name("package")
.long("package")
.value_name("SPEC")
.help("Package to build")
.takes_value(true),
Arg::with_name("exclude")
.value_name("SPEC")
.help("Exclude packages from the build")
.takes_value(true),
Arg::with_name("j")
.short("j")
.long("jobs")
.value_name("N")
.help("Number of parallel jobs, defaults to # of CPUs")
.takes_value(true),
Arg::with_name("bin")
.long("bin")
.value_name("NAME")
.help("Build only the specified binary")
.takes_value(true),
Arg::with_name("release")
.long("release")
.help("Build artifacts in release mode, with optimizations"),
Arg::with_name("features")
.long("features")
.value_name("FEATURES")
.help("Space-separated list of features to also build")
.takes_value(true),
Arg::with_name("all-features")
.long("all-features")
.help("Build all available features"),
Arg::with_name("no-default-features")
.long("no-default-features")
.help("Do not build the `default` feature"),
Arg::with_name("manifest-path")
.long("manifest-path")
.value_name("PATH")
.help("Path to the manifest to compile")
.takes_value(true),
Arg::with_name("verbose")
.short("v")
.long("verbose")
.help("Use verbose output (-vv very verbose/build.rs output)"),
Arg::with_name("quiet")
.short("q")
.long("quiet")
.help("No output printed to stdout"),
Arg::with_name("frozen")
.long("frozen")
.help("Require Cargo.lock and cache are up to date"),
Arg::with_name("locked")
.long("locked")
.help("Require Cargo.lock is up to date"),
Arg::with_name("cargo-flags")
.short("Z")
.value_name("FLAG")
.help("Unstable (nightly-only) flags to Cargo")
.takes_value(true),
]
}

fn build_args<'a, 'b>() -> Vec<Arg<'a, 'b>> {
vec![
Arg::with_name("all")
.long("all")
.help("Build all packages in the workspace"),
Arg::with_name("lib")
.long("lib")
.help("Build only this package's library"),
Arg::with_name("bins")
.long("bins")
.help("Build all binaries"),
Arg::with_name("all-targets")
.long("all-targets")
.help("Build all targets (lib and bin targets by default)"),
]
}

Expand All @@ -35,12 +100,35 @@ fn build_options(matches: &ArgMatches) -> build::Options {
if let Some(features) = matches.value_of("features") {
build_options.features = Some(features.to_string());
}
build_options
build::Options {
package: matches.value_of("package").map(String::from),
all: matches.is_present("all"),
exclude: matches.value_of("exclude").map(String::from),
jobs: matches.value_of("jobs").map(String::from),
lib: matches.is_present("lib"),
bin: matches.value_of("bin").map(String::from),
bins: matches.is_present("bins"),
all_targets: matches.is_present("all-targets"),
release: matches.is_present("release"),
features: matches.value_of("features").map(String::from),
all_features: matches.is_present("all-features"),
no_default_features: matches.is_present("no-default-features"),
manifest_path: matches.value_of("manifest-path").map(String::from),
verbose: matches.is_present("verbose"),
quiet: matches.is_present("quiet"),
frozen: matches.is_present("frozen"),
locked: matches.is_present("locked"),
cargo_flags: matches.value_of("Z").map(String::from),
}
}

fn main() {
let app = App::new("wasm-build")
.subcommand(SubCommand::with_name("build").args(&shared_args()))
.subcommand(
SubCommand::with_name("build")
.args(&shared_args())
.args(&build_args()),
)
.subcommand(SubCommand::with_name("run").args(&shared_args()))
.get_matches();

Expand Down

0 comments on commit a68cd7e

Please sign in to comment.