Skip to content

Commit

Permalink
Switch to the crates.io getopts crate
Browse files Browse the repository at this point in the history
This commit deletes the in-tree `getopts` crate in favor of the crates.io-based
`getopts` crate. The main difference here is with a new builder-style API, but
otherwise everything else remains relatively standard.
  • Loading branch information
alexcrichton committed Jun 20, 2017
1 parent a4024c5 commit 5c3d0e6
Show file tree
Hide file tree
Showing 14 changed files with 317 additions and 277 deletions.
20 changes: 5 additions & 15 deletions src/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion src/bootstrap/dist.rs
Expand Up @@ -567,7 +567,6 @@ pub fn rust_src(build: &Build) {
"src/rustc/libc_shim",
"src/libtest",
"src/libterm",
"src/libgetopts",
"src/compiler-rt",
"src/jemalloc",
"src/libprofiler_builtins",
Expand Down
9 changes: 0 additions & 9 deletions src/libgetopts/Cargo.toml

This file was deleted.

4 changes: 4 additions & 0 deletions src/librustc/lib.rs
Expand Up @@ -41,6 +41,7 @@
#![feature(discriminant_value)]
#![feature(sort_unstable)]
#![feature(trace_macros)]
#![feature(test)]

#![recursion_limit="256"]

Expand All @@ -63,7 +64,10 @@ extern crate syntax_pos;

extern crate serialize as rustc_serialize; // used by deriving

// Note that librustc doesn't actually depend on these crates, see the note in
// `Cargo.toml` for this crate about why these are here.
extern crate flate2;
extern crate test;

#[macro_use]
mod macros;
Expand Down
124 changes: 72 additions & 52 deletions src/librustc/session/config.rs
Expand Up @@ -1135,9 +1135,9 @@ pub enum OptionStability {
Unstable,
}

#[derive(Clone, PartialEq, Eq)]
pub struct RustcOptGroup {
pub opt_group: getopts::OptGroup,
pub apply: Box<Fn(&mut getopts::Options) -> &mut getopts::Options>,
pub name: &'static str,
pub stability: OptionStability,
}

Expand All @@ -1146,12 +1146,24 @@ impl RustcOptGroup {
self.stability == OptionStability::Stable
}

pub fn stable(g: getopts::OptGroup) -> RustcOptGroup {
RustcOptGroup { opt_group: g, stability: OptionStability::Stable }
pub fn stable<F>(name: &'static str, f: F) -> RustcOptGroup
where F: Fn(&mut getopts::Options) -> &mut getopts::Options + 'static,
{
RustcOptGroup {
name: name,
apply: Box::new(f),
stability: OptionStability::Stable,
}
}

pub fn unstable(g: getopts::OptGroup) -> RustcOptGroup {
RustcOptGroup { opt_group: g, stability: OptionStability::Unstable }
pub fn unstable<F>(name: &'static str, f: F) -> RustcOptGroup
where F: Fn(&mut getopts::Options) -> &mut getopts::Options + 'static,
{
RustcOptGroup {
name: name,
apply: Box::new(f),
stability: OptionStability::Unstable,
}
}
}

Expand All @@ -1170,55 +1182,65 @@ mod opt {
use super::RustcOptGroup;

pub type R = RustcOptGroup;
pub type S<'a> = &'a str;
pub type S = &'static str;

fn stable<F>(name: S, f: F) -> R
where F: Fn(&mut getopts::Options) -> &mut getopts::Options + 'static
{
RustcOptGroup::stable(name, f)
}

fn stable(g: getopts::OptGroup) -> R { RustcOptGroup::stable(g) }
fn unstable(g: getopts::OptGroup) -> R { RustcOptGroup::unstable(g) }
fn unstable<F>(name: S, f: F) -> R
where F: Fn(&mut getopts::Options) -> &mut getopts::Options + 'static
{
RustcOptGroup::unstable(name, f)
}

fn longer(a: S, b: S) -> S {
if a.len() > b.len() {
a
} else {
b
}
}

pub fn opt_s(a: S, b: S, c: S, d: S) -> R {
stable(getopts::optopt(a, b, c, d))
stable(longer(a, b), move |opts| opts.optopt(a, b, c, d))
}
pub fn multi_s(a: S, b: S, c: S, d: S) -> R {
stable(getopts::optmulti(a, b, c, d))
stable(longer(a, b), move |opts| opts.optmulti(a, b, c, d))
}
pub fn flag_s(a: S, b: S, c: S) -> R {
stable(getopts::optflag(a, b, c))
stable(longer(a, b), move |opts| opts.optflag(a, b, c))
}
pub fn flagopt_s(a: S, b: S, c: S, d: S) -> R {
stable(getopts::optflagopt(a, b, c, d))
stable(longer(a, b), move |opts| opts.optflagopt(a, b, c, d))
}
pub fn flagmulti_s(a: S, b: S, c: S) -> R {
stable(getopts::optflagmulti(a, b, c))
stable(longer(a, b), move |opts| opts.optflagmulti(a, b, c))
}

pub fn opt(a: S, b: S, c: S, d: S) -> R {
unstable(getopts::optopt(a, b, c, d))
unstable(longer(a, b), move |opts| opts.optopt(a, b, c, d))
}
pub fn multi(a: S, b: S, c: S, d: S) -> R {
unstable(getopts::optmulti(a, b, c, d))
unstable(longer(a, b), move |opts| opts.optmulti(a, b, c, d))
}
pub fn flag(a: S, b: S, c: S) -> R {
unstable(getopts::optflag(a, b, c))
unstable(longer(a, b), move |opts| opts.optflag(a, b, c))
}
pub fn flagopt(a: S, b: S, c: S, d: S) -> R {
unstable(getopts::optflagopt(a, b, c, d))
unstable(longer(a, b), move |opts| opts.optflagopt(a, b, c, d))
}
pub fn flagmulti(a: S, b: S, c: S) -> R {
unstable(getopts::optflagmulti(a, b, c))
unstable(longer(a, b), move |opts| opts.optflagmulti(a, b, c))
}
}

/// Returns the "short" subset of the rustc command line options,
/// including metadata for each option, such as whether the option is
/// part of the stable long-term interface for rustc.
pub fn rustc_short_optgroups() -> Vec<RustcOptGroup> {
let mut print_opts = vec!["crate-name", "file-names", "sysroot", "cfg",
"target-list", "target-cpus", "target-features",
"relocation-models", "code-models"];
if nightly_options::is_nightly_build() {
print_opts.push("target-spec-json");
}

vec![
opt::flag_s("h", "help", "Display this message"),
opt::multi_s("", "cfg", "Configure the compilation environment", "SPEC"),
Expand All @@ -1238,8 +1260,10 @@ pub fn rustc_short_optgroups() -> Vec<RustcOptGroup> {
the compiler to emit",
"[asm|llvm-bc|llvm-ir|obj|metadata|link|dep-info|mir]"),
opt::multi_s("", "print", "Comma separated list of compiler information to \
print on stdout", &format!("[{}]",
&print_opts.join("|"))),
print on stdout",
"[crate-name|file-names|sysroot|cfg|target-list|\
target-cpus|target-features|relocation-models|\
code-models|target-spec-json]"),
opt::flagmulti_s("g", "", "Equivalent to -C debuginfo=2"),
opt::flagmulti_s("O", "", "Equivalent to -C opt-level=2"),
opt::opt_s("o", "", "Write output to <filename>", "FILENAME"),
Expand Down Expand Up @@ -1267,7 +1291,7 @@ pub fn rustc_short_optgroups() -> Vec<RustcOptGroup> {
/// long-term interface for rustc.
pub fn rustc_optgroups() -> Vec<RustcOptGroup> {
let mut opts = rustc_short_optgroups();
opts.extend_from_slice(&[
opts.extend(vec![
opt::multi_s("", "extern", "Specify where an external rust library is located",
"NAME=PATH"),
opt::opt_s("", "sysroot", "Override the system root", "PATH"),
Expand Down Expand Up @@ -1680,27 +1704,22 @@ pub mod nightly_options {
if opt.stability == OptionStability::Stable {
continue
}
let opt_name = if opt.opt_group.long_name.is_empty() {
&opt.opt_group.short_name
} else {
&opt.opt_group.long_name
};
if !matches.opt_present(opt_name) {
if !matches.opt_present(opt.name) {
continue
}
if opt_name != "Z" && !has_z_unstable_option {
if opt.name != "Z" && !has_z_unstable_option {
early_error(ErrorOutputType::default(),
&format!("the `-Z unstable-options` flag must also be passed to enable \
the flag `{}`",
opt_name));
opt.name));
}
if really_allows_unstable_options {
continue
}
match opt.stability {
OptionStability::Unstable => {
let msg = format!("the option `{}` is only accepted on the \
nightly compiler", opt_name);
nightly compiler", opt.name);
early_error(ErrorOutputType::default(), &msg);
}
OptionStability::Stable => {}
Expand Down Expand Up @@ -1869,7 +1888,7 @@ mod dep_tracking {
mod tests {
use dep_graph::DepGraph;
use errors;
use getopts::{getopts, OptGroup};
use getopts;
use lint;
use middle::cstore::{self, DummyCrateStore};
use session::config::{build_configuration, build_session_options_and_crate_config};
Expand All @@ -1882,10 +1901,12 @@ mod tests {
use rustc_back::PanicStrategy;
use syntax::symbol::Symbol;

fn optgroups() -> Vec<OptGroup> {
super::rustc_optgroups().into_iter()
.map(|a| a.opt_group)
.collect()
fn optgroups() -> getopts::Options {
let mut opts = getopts::Options::new();
for group in super::rustc_optgroups() {
(group.apply)(&mut opts);
}
return opts
}

fn mk_map<K: Ord, V>(entries: Vec<(K, V)>) -> BTreeMap<K, V> {
Expand All @@ -1901,7 +1922,7 @@ mod tests {
fn test_switch_implies_cfg_test() {
let dep_graph = DepGraph::new(false);
let matches =
&match getopts(&["--test".to_string()], &optgroups()) {
&match optgroups().parse(&["--test".to_string()]) {
Ok(m) => m,
Err(f) => panic!("test_switch_implies_cfg_test: {}", f)
};
Expand All @@ -1918,8 +1939,7 @@ mod tests {
fn test_switch_implies_cfg_test_unless_cfg_test() {
let dep_graph = DepGraph::new(false);
let matches =
&match getopts(&["--test".to_string(), "--cfg=test".to_string()],
&optgroups()) {
&match optgroups().parse(&["--test".to_string(), "--cfg=test".to_string()]) {
Ok(m) => m,
Err(f) => {
panic!("test_switch_implies_cfg_test_unless_cfg_test: {}", f)
Expand All @@ -1939,9 +1959,9 @@ mod tests {
fn test_can_print_warnings() {
let dep_graph = DepGraph::new(false);
{
let matches = getopts(&[
let matches = optgroups().parse(&[
"-Awarnings".to_string()
], &optgroups()).unwrap();
]).unwrap();
let registry = errors::registry::Registry::new(&[]);
let (sessopts, _) = build_session_options_and_crate_config(&matches);
let sess = build_session(sessopts, &dep_graph, None, registry,
Expand All @@ -1950,10 +1970,10 @@ mod tests {
}

{
let matches = getopts(&[
let matches = optgroups().parse(&[
"-Awarnings".to_string(),
"-Dwarnings".to_string()
], &optgroups()).unwrap();
]).unwrap();
let registry = errors::registry::Registry::new(&[]);
let (sessopts, _) = build_session_options_and_crate_config(&matches);
let sess = build_session(sessopts, &dep_graph, None, registry,
Expand All @@ -1962,9 +1982,9 @@ mod tests {
}

{
let matches = getopts(&[
let matches = optgroups().parse(&[
"-Adead_code".to_string()
], &optgroups()).unwrap();
]).unwrap();
let registry = errors::registry::Registry::new(&[]);
let (sessopts, _) = build_session_options_and_crate_config(&matches);
let sess = build_session(sessopts, &dep_graph, None, registry,
Expand Down

0 comments on commit 5c3d0e6

Please sign in to comment.