Skip to content

Commit 6928a10

Browse files
committed
auto merge of #8362 : sfackler/rust/env, r=alexcrichton
env! aborts compilation of the specified environment variable is not defined and takes an optional second argument containing a custom error message. option_env! creates an Option<&'static str> containing the value of the environment variable. There are no run-pass tests that check the behavior when the environment variable is defined since the test framework doesn't support setting environment variables at compile time as opposed to runtime. However, both env! and option_env! are used inside of rustc itself, which should act as a sufficient test. Fixes #2248.
2 parents 2fe2e59 + c3825c8 commit 6928a10

19 files changed

+168
-32
lines changed

src/librustc/back/rpath.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ pub fn get_absolute_rpath(lib: &Path) -> Path {
168168
os::make_absolute(lib).dir_path()
169169
}
170170

171+
#[cfg(stage0)]
171172
pub fn get_install_prefix_rpath(target_triple: &str) -> Path {
172173
let install_prefix = env!("CFG_PREFIX");
173174

@@ -179,6 +180,14 @@ pub fn get_install_prefix_rpath(target_triple: &str) -> Path {
179180
os::make_absolute(&Path(install_prefix).push_rel(&tlib))
180181
}
181182

183+
#[cfg(not(stage0))]
184+
pub fn get_install_prefix_rpath(target_triple: &str) -> Path {
185+
let install_prefix = env!("CFG_PREFIX");
186+
187+
let tlib = filesearch::relative_target_lib_path(target_triple);
188+
os::make_absolute(&Path(install_prefix).push_rel(&tlib))
189+
}
190+
182191
pub fn minimize_rpaths(rpaths: &[Path]) -> ~[Path] {
183192
let mut set = HashSet::new();
184193
let mut minimized = ~[];

src/librustc/driver/driver.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
1+
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
22
// file at the top-level directory of this distribution and at
33
// http://rust-lang.org/COPYRIGHT.
44
//
@@ -578,6 +578,7 @@ pub fn build_target_config(sopts: @session::options,
578578
return target_cfg;
579579
}
580580
581+
#[cfg(stage0)]
581582
pub fn host_triple() -> ~str {
582583
// Get the host triple out of the build environment. This ensures that our
583584
// idea of the host triple is the same as for the set of libraries we've
@@ -595,6 +596,19 @@ pub fn host_triple() -> ~str {
595596
};
596597
}
597598

599+
#[cfg(not(stage0))]
600+
pub fn host_triple() -> ~str {
601+
// Get the host triple out of the build environment. This ensures that our
602+
// idea of the host triple is the same as for the set of libraries we've
603+
// actually built. We can't just take LLVM's host triple because they
604+
// normalize all ix86 architectures to i386.
605+
//
606+
// Instead of grabbing the host triple (for the current host), we grab (at
607+
// compile time) the target triple that this rustc is built with and
608+
// calling that (at runtime) the host triple.
609+
(env!("CFG_COMPILER_TRIPLE")).to_owned()
610+
}
611+
598612
pub fn build_session_options(binary: @str,
599613
matches: &getopts::Matches,
600614
demitter: diagnostic::Emitter)

src/librustc/metadata/filesearch.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
1+
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
22
// file at the top-level directory of this distribution and at
33
// http://rust-lang.org/COPYRIGHT.
44
//
@@ -186,10 +186,16 @@ fn get_rustpkg_lib_path_nearest() -> Result<Path, ~str> {
186186

187187
// The name of the directory rustc expects libraries to be located.
188188
// On Unix should be "lib", on windows "bin"
189+
#[cfg(stage0)]
189190
pub fn libdir() -> ~str {
190191
let libdir = env!("CFG_LIBDIR");
191192
if libdir.is_empty() {
192193
fail!("rustc compiled without CFG_LIBDIR environment variable");
193194
}
194195
libdir.to_owned()
195196
}
197+
198+
#[cfg(not(stage0))]
199+
pub fn libdir() -> ~str {
200+
(env!("CFG_LIBDIR")).to_owned()
201+
}

src/librustc/middle/resolve.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
1+
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
22
// file at the top-level directory of this distribution and at
33
// http://rust-lang.org/COPYRIGHT.
44
//
@@ -4994,7 +4994,9 @@ impl Resolver {
49944994
if self.structs.contains(&class_id) => {
49954995
self.record_def(expr.id, definition);
49964996
}
4997-
_ => {
4997+
result => {
4998+
debug!("(resolving expression) didn't find struct \
4999+
def: %?", result);
49985000
self.session.span_err(
49995001
path.span,
50005002
fmt!("`%s` does not name a structure",

src/librustc/rustc.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ mod std {
117117
}
118118
*/
119119

120+
#[cfg(stage0)]
120121
pub fn version(argv0: &str) {
121122
let mut vers = ~"unknown version";
122123
let env_vers = env!("CFG_VERSION");
@@ -125,6 +126,16 @@ pub fn version(argv0: &str) {
125126
printfln!("host: %s", host_triple());
126127
}
127128

129+
#[cfg(not(stage0))]
130+
pub fn version(argv0: &str) {
131+
let vers = match option_env!("CFG_VERSION") {
132+
Some(vers) => vers,
133+
None => "unknown version"
134+
};
135+
printfln!("%s %s", argv0, vers);
136+
printfln!("host: %s", host_triple());
137+
}
138+
128139
pub fn usage(argv0: &str) {
129140
let message = fmt!("Usage: %s [OPTIONS] INPUT", argv0);
130141
printfln!("%s\

src/libsyntax/ext/asm.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
1+
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
22
// file at the top-level directory of this distribution and at
33
// http://rust-lang.org/COPYRIGHT.
44
//
@@ -59,7 +59,7 @@ pub fn expand_asm(cx: @ExtCtxt, sp: span, tts: &[ast::token_tree])
5959
match state {
6060
Asm => {
6161
asm = expr_to_str(cx, p.parse_expr(),
62-
~"inline assembly must be a string literal.");
62+
"inline assembly must be a string literal.");
6363
}
6464
Outputs => {
6565
while *p.token != token::EOF &&

src/libsyntax/ext/base.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,9 @@ pub fn syntax_expander_table() -> SyntaxEnv {
148148
intern(&"auto_decode"),
149149
@SE(ItemDecorator(ext::auto_encode::expand_auto_decode)));
150150
syntax_expanders.insert(intern(&"env"),
151-
builtin_normal_tt(ext::env::expand_syntax_ext));
151+
builtin_normal_tt(ext::env::expand_env));
152+
syntax_expanders.insert(intern(&"option_env"),
153+
builtin_normal_tt(ext::env::expand_option_env));
152154
syntax_expanders.insert(intern("bytes"),
153155
builtin_normal_tt(ext::bytes::expand_syntax_ext));
154156
syntax_expanders.insert(intern("concat_idents"),
@@ -313,7 +315,7 @@ impl ExtCtxt {
313315
}
314316
}
315317

316-
pub fn expr_to_str(cx: @ExtCtxt, expr: @ast::expr, err_msg: ~str) -> @str {
318+
pub fn expr_to_str(cx: @ExtCtxt, expr: @ast::expr, err_msg: &str) -> @str {
317319
match expr.node {
318320
ast::expr_lit(l) => match l.node {
319321
ast::lit_str(s) => s,
@@ -538,8 +540,8 @@ mod test {
538540
a.insert (@"abc",@15);
539541
let m = MapChain::new(~a);
540542
m.insert (@"def",@16);
541-
// FIXME: #4492 (ICE) assert_eq!(m.find(&@"abc"),Some(@15));
542-
// .... assert_eq!(m.find(&@"def"),Some(@16));
543+
assert_eq!(m.find(&@"abc"),Some(@15));
544+
assert_eq!(m.find(&@"def"),Some(@16));
543545
assert_eq!(*(m.find(&@"abc").unwrap()),15);
544546
assert_eq!(*(m.find(&@"def").unwrap()),16);
545547
let n = m.push_frame();
@@ -551,8 +553,8 @@ mod test {
551553
assert_eq!(*(n.find(&@"abc").unwrap()),15);
552554
assert_eq!(*(n.find(&@"def").unwrap()),17);
553555
// ... but m still has the old ones
554-
// FIXME: #4492: assert_eq!(m.find(&@"abc"),Some(@15));
555-
// FIXME: #4492: assert_eq!(m.find(&@"def"),Some(@16));
556+
assert_eq!(m.find(&@"abc"),Some(@15));
557+
assert_eq!(m.find(&@"def"),Some(@16));
556558
assert_eq!(*(m.find(&@"abc").unwrap()),15);
557559
assert_eq!(*(m.find(&@"def").unwrap()),16);
558560
}

src/libsyntax/ext/env.rs

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
1+
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
22
// file at the top-level directory of this distribution and at
33
// http://rust-lang.org/COPYRIGHT.
44
//
@@ -22,17 +22,35 @@ use ext::build::AstBuilder;
2222

2323
use std::os;
2424

25-
pub fn expand_syntax_ext(cx: @ExtCtxt, sp: span, tts: &[ast::token_tree])
25+
pub fn expand_option_env(ext_cx: @ExtCtxt, sp: span, tts: &[ast::token_tree])
2626
-> base::MacResult {
27+
let var = get_single_str_from_tts(ext_cx, sp, tts, "option_env!");
2728

28-
let var = get_single_str_from_tts(cx, sp, tts, "env!");
29+
let e = match os::getenv(var) {
30+
None => quote_expr!(::std::option::None),
31+
Some(s) => quote_expr!(::std::option::Some($s))
32+
};
33+
MRExpr(e)
34+
}
2935

30-
// FIXME (#2248): if this was more thorough it would manufacture an
31-
// Option<str> rather than just an maybe-empty string.
36+
pub fn expand_env(ext_cx: @ExtCtxt, sp: span, tts: &[ast::token_tree])
37+
-> base::MacResult {
38+
let exprs = get_exprs_from_tts(ext_cx, sp, tts);
39+
40+
if exprs.len() == 0 {
41+
ext_cx.span_fatal(sp, "env! takes 1 or 2 arguments");
42+
}
43+
44+
let var = expr_to_str(ext_cx, exprs[0], "expected string literal");
45+
let msg = match exprs.len() {
46+
1 => fmt!("Environment variable %s not defined", var).to_managed(),
47+
2 => expr_to_str(ext_cx, exprs[1], "expected string literal"),
48+
_ => ext_cx.span_fatal(sp, "env! takes 1 or 2 arguments")
49+
};
3250

3351
let e = match os::getenv(var) {
34-
None => cx.expr_str(sp, @""),
35-
Some(s) => cx.expr_str(sp, s.to_managed())
52+
None => ext_cx.span_fatal(sp, msg),
53+
Some(s) => ext_cx.expr_str(sp, s.to_managed())
3654
};
3755
MRExpr(e)
3856
}

src/libsyntax/ext/fmt.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
1+
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
22
// file at the top-level directory of this distribution and at
33
// http://rust-lang.org/COPYRIGHT.
44
//
@@ -32,7 +32,7 @@ pub fn expand_syntax_ext(cx: @ExtCtxt, sp: span, tts: &[ast::token_tree])
3232
}
3333
let fmt =
3434
expr_to_str(cx, args[0],
35-
~"first argument to fmt! must be a string literal.");
35+
"first argument to fmt! must be a string literal.");
3636
let fmtspan = args[0].span;
3737
debug!("Format string: %s", fmt);
3838
fn parse_fmt_err_(cx: @ExtCtxt, sp: span, msg: &str) -> ! {
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
fn main() { env!("one", 10); } //~ ERROR: expected string literal
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
1+
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
22
// file at the top-level directory of this distribution and at
33
// http://rust-lang.org/COPYRIGHT.
44
//
@@ -8,6 +8,4 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
// error-pattern: env! takes 1 argument
12-
13-
fn main() { env!(); }
11+
fn main() { env!(); } //~ ERROR: env! takes 1 or 2 arguments
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
fn main() { env!("__HOPEFULLY_NOT_DEFINED__", "my error message"); } //~ ERROR: my error message
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
fn main() { env!("__HOPEFULLY_NOT_DEFINED__"); } //~ ERROR: Environment variable __HOPEFULLY_NOT_DEFINED__ not defined
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
1+
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
22
// file at the top-level directory of this distribution and at
33
// http://rust-lang.org/COPYRIGHT.
44
//
@@ -8,6 +8,4 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
// error-pattern:requires a string
12-
13-
fn main() { env!(10); }
11+
fn main() { env!(10, "two"); } //~ ERROR: expected string literal

src/test/compile-fail/extenv-too-many-args.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,4 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
// error-pattern: env! takes 1 argument
12-
13-
fn main() { env!("one", "two"); }
11+
fn main() { env!("one", "two", "three"); } //~ ERROR: env! takes 1 or 2 arguments
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
fn main() { option_env!(); } //~ ERROR: option_env! takes 1 argument
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
fn main() { option_env!(10); } //~ ERROR: requires a string
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
fn main() { option_env!("one", "two"); } //~ ERROR: option_env! takes 1 argument
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
fn main() {
12+
let opt: Option<&'static str> = option_env!("__HOPEFULLY_DOESNT_EXIST__");
13+
assert!(opt.is_none());
14+
}

0 commit comments

Comments
 (0)