1 use cargo
::util
::process
;
2 use support
::{is_nightly, rustc_host}
;
3 use support
::{basic_manifest, basic_bin_manifest, cross_compile, execs, project}
;
4 use support
::hamcrest
::{assert_that, existing_file}
;
8 if cross_compile
::disabled() {
28 assert_eq!(std::env::var("TARGET").unwrap(), "{}");
31 cross_compile
::alternate()
40 assert_eq!(env::consts::ARCH, "{}");
43 cross_compile
::alternate_arch()
48 let target
= cross_compile
::alternate();
50 p
.cargo("build").arg("--target").arg(&target
).arg("-v"),
51 execs().with_status(0),
53 assert_that(&p
.target_bin(&target
, "foo"), existing_file());
56 process(&p
.target_bin(&target
, "foo")),
57 execs().with_status(0),
62 fn simple_cross_config() {
63 if cross_compile
::disabled() {
75 cross_compile
::alternate()
93 assert_eq!(std::env::var("TARGET").unwrap(), "{}");
96 cross_compile
::alternate()
105 assert_eq!(env::consts::ARCH, "{}");
108 cross_compile
::alternate_arch()
113 let target
= cross_compile
::alternate();
114 assert_that(p
.cargo("build").arg("-v"), execs().with_status(0));
115 assert_that(&p
.target_bin(&target
, "foo"), existing_file());
118 process(&p
.target_bin(&target
, "foo")),
119 execs().with_status(0),
125 if cross_compile
::disabled() {
142 .file("src/main.rs", "extern crate bar; fn main() { bar::bar(); }")
144 let _p2
= project().at("bar")
145 .file("Cargo.toml", &basic_manifest("bar", "0.0.1"))
146 .file("src/lib.rs", "pub fn bar() {}")
149 let target
= cross_compile
::alternate();
151 p
.cargo("build").arg("--target").arg(&target
),
152 execs().with_status(0),
154 assert_that(&p
.target_bin(&target
, "foo"), existing_file());
157 process(&p
.target_bin(&target
, "foo")),
158 execs().with_status(0),
164 if cross_compile
::disabled() {
194 assert_eq!(bar!(), baz::baz());
199 let _bar
= project().at("bar")
216 #![feature(plugin_registrar, rustc_private)]
218 extern crate rustc_plugin;
221 use rustc_plugin::Registry;
222 use syntax::tokenstream::TokenTree;
223 use syntax::codemap::Span;
225 use syntax::ext::base::{ExtCtxt, MacEager, MacResult};
226 use syntax::ext::build::AstBuilder;
229 pub fn foo(reg: &mut Registry) {
230 reg.register_macro("bar", expand_bar);
233 fn expand_bar(cx: &mut ExtCtxt, sp: Span, tts: &[TokenTree])
234 -> Box<MacResult + 'static> {
235 MacEager::expr(cx.expr_lit(sp, LitKind::Int(1, LitIntType::Unsuffixed)))
240 let _baz
= project().at("baz")
241 .file("Cargo.toml", &basic_manifest("baz", "0.0.1"))
242 .file("src/lib.rs", "pub fn baz() -> i32 { 1 }")
245 let target
= cross_compile
::alternate();
247 foo
.cargo("build").arg("--target").arg(&target
),
248 execs().with_status(0),
250 assert_that(&foo
.target_bin(&target
, "foo"), existing_file());
253 process(&foo
.target_bin(&target
, "foo")),
254 execs().with_status(0),
259 fn plugin_to_the_max() {
260 if cross_compile
::disabled() {
290 assert_eq!(bar!(), baz::baz());
295 let _bar
= project().at("bar")
315 #![feature(plugin_registrar, rustc_private)]
317 extern crate rustc_plugin;
321 use rustc_plugin::Registry;
322 use syntax::tokenstream::TokenTree;
323 use syntax::codemap::Span;
325 use syntax::ext::base::{ExtCtxt, MacEager, MacResult};
326 use syntax::ext::build::AstBuilder;
330 pub fn foo(reg: &mut Registry) {
331 reg.register_macro("bar", expand_bar);
334 fn expand_bar(cx: &mut ExtCtxt, sp: Span, tts: &[TokenTree])
335 -> Box<MacResult + 'static> {
336 let bar = Ident::from_str("baz");
337 let path = cx.path(sp, vec![bar.clone(), bar]);
338 MacEager::expr(cx.expr_call(sp, cx.expr_path(path), vec![]))
343 let _baz
= project().at("baz")
344 .file("Cargo.toml", &basic_manifest("baz", "0.0.1"))
345 .file("src/lib.rs", "pub fn baz() -> i32 { 1 }")
348 let target
= cross_compile
::alternate();
350 foo
.cargo("build").arg("--target").arg(&target
).arg("-v"),
351 execs().with_status(0),
355 foo
.cargo("build").arg("-v").arg("--target").arg(&target
),
356 execs().with_status(0),
358 assert_that(&foo
.target_bin(&target
, "foo"), existing_file());
361 process(&foo
.target_bin(&target
, "foo")),
362 execs().with_status(0),
368 if cross_compile
::disabled() {
372 let target
= cross_compile
::alternate();
380 linker = "my-linker-tool"
385 .file("Cargo.toml", &basic_bin_manifest("foo"))
392 assert_eq!(env::consts::ARCH, "{}");
395 cross_compile
::alternate_arch()
401 p
.cargo("build").arg("--target").arg(&target
).arg("-v"),
402 execs().with_status(101).with_stderr_contains(&format
!(
404 [COMPILING] foo v0.5.0 ({url})
405 [RUNNING] `rustc --crate-name foo src[/]foo.rs --crate-type bin \
406 --emit=dep-info,link -C debuginfo=2 \
408 --out-dir {dir}[/]target[/]{target}[/]debug[/]deps \
410 -C ar=my-ar-tool -C linker=my-linker-tool \
411 -L dependency={dir}[/]target[/]{target}[/]debug[/]deps \
412 -L dependency={dir}[/]target[/]debug[/]deps`
414 dir
= p
.root().display(),
422 fn plugin_with_extra_dylib_dep() {
423 if cross_compile
::disabled() {
453 let _bar
= project().at("bar")
473 #![feature(plugin_registrar, rustc_private)]
475 extern crate rustc_plugin;
478 use rustc_plugin::Registry;
481 pub fn foo(reg: &mut Registry) {
482 println!("{}", baz::baz());
487 let _baz
= project().at("baz")
498 crate_type = ["dylib"]
501 .file("src/lib.rs", "pub fn baz() -> i32 { 1 }")
504 let target
= cross_compile
::alternate();
506 foo
.cargo("build").arg("--target").arg(&target
),
507 execs().with_status(0),
513 if cross_compile
::disabled() {
534 #[allow(unused_extern_crates)]
538 assert_eq!(env::consts::ARCH, "{}");
540 #[test] fn test() {{ main() }}
542 cross_compile
::alternate_arch()
550 pub fn foo() {{ assert_eq!(env::consts::ARCH, "{}"); }}
551 #[test] fn test_foo() {{ foo() }}
553 cross_compile::alternate_arch()
558 let target = cross_compile::alternate();
560 p.cargo("test
").arg("--target
").arg(&target),
563 .with_stderr(&format!(
565 [COMPILING
] foo v0
.0
.0 ({foo}
)
566 [FINISHED
] dev
[unoptimized
+ debuginfo
] target(s
) in [..]
567 [RUNNING
] target
[/]{triple}
[/]debug
[/]deps
[/]foo
-[..][EXE
]
568 [RUNNING
] target
[/]{triple}
[/]debug
[/]deps
[/]bar
-[..][EXE
]",
572 .with_stdout_contains("test test_foo
... ok
")
573 .with_stdout_contains("test test
... ok
"),
578 fn no_cross_doctests() {
579 if cross_compile::disabled() {
588 //! extern crate foo;
595 let host_output = format!(
597 [COMPILING
] foo v0
.0
.1 ({foo}
)
598 [FINISHED
] dev
[unoptimized
+ debuginfo
] target(s
) in [..]
599 [RUNNING
] target
[/]debug
[/]deps
[/]foo
-[..][EXE
]
608 execs().with_status(0).with_stderr(&host_output),
612 let target = cross_compile::host();
614 p.cargo("test
").arg("--target
").arg(&target),
615 execs().with_status(0).with_stderr(&format!(
617 [COMPILING
] foo v0
.0
.1 ({foo}
)
618 [FINISHED
] dev
[unoptimized
+ debuginfo
] target(s
) in [..]
619 [RUNNING
] target
[/]{triple}
[/]debug
[/]deps
[/]foo
-[..][EXE
]
628 let target = cross_compile::alternate();
630 p.cargo("test
").arg("--target
").arg(&target),
631 execs().with_status(0).with_stderr(&format!(
633 [COMPILING
] foo v0
.0
.1 ({foo}
)
634 [FINISHED
] dev
[unoptimized
+ debuginfo
] target(s
) in [..]
635 [RUNNING
] target
[/]{triple}
[/]debug
[/]deps
[/]foo
-[..][EXE
]
644 fn simple_cargo_run() {
645 if cross_compile::disabled() {
656 assert_eq
!(env
::consts
::ARCH
, "{}");
659 cross_compile::alternate_arch()
664 let target = cross_compile::alternate();
666 p.cargo("run
").arg("--target
").arg(&target),
667 execs().with_status(0),
672 fn cross_with_a_build_script() {
673 if cross_compile::disabled() {
677 let target = cross_compile::alternate();
694 use std
::path
::PathBuf
;
696 assert_eq
!(env
::var("TARGET").unwrap(), "{0}");
697 let mut path
= PathBuf
::from(env
::var_os("OUT_DIR").unwrap());
698 assert_eq
!(path
.file_name().unwrap().to_str().unwrap(), "out");
700 assert
!(path
.file_name().unwrap().to_str().unwrap()
701 .starts_with("foo-"));
703 assert_eq
!(path
.file_name().unwrap().to_str().unwrap(), "build");
705 assert_eq
!(path
.file_name().unwrap().to_str().unwrap(), "debug");
707 assert_eq
!(path
.file_name().unwrap().to_str().unwrap(), "{0}");
709 assert_eq
!(path
.file_name().unwrap().to_str().unwrap(), "target");
715 .file("src
/main
.rs
", "fn main() {}
")
719 p.cargo("build
").arg("--target
").arg(&target).arg("-v
"),
720 execs().with_status(0).with_stderr(&format!(
722 [COMPILING
] foo v0
.0
.0 (file
://[..])
723 [RUNNING
] `rustc
[..] build
.rs
[..] --out
-dir {dir}
[/]target
[/]debug
[/]build
[/]foo
-[..]`
724 [RUNNING
] `{dir}
[/]target
[/]debug
[/]build
[/]foo
-[..][/]build
-script
-build`
725 [RUNNING
] `rustc
[..] src
[/]main
.rs
[..] --target {target}
[..]`
726 [FINISHED
] dev
[unoptimized
+ debuginfo
] target(s
) in [..]
729 dir = p.root().display()
735 fn build_script_needed_for_host_and_target() {
736 if cross_compile::disabled() {
740 let target = cross_compile::alternate();
741 let host = rustc_host();
754 [build
-dependencies
.d2
]
761 #[allow(unused_extern_crates)]
763 fn main() { d2::d2(); }
769 #[allow(unused_extern_crates)]
771 fn main() { d1::d1(); }
784 .file("d1
/src
/lib
.rs
", "pub fn d1() {}
")
790 let target
= env
::var("TARGET").unwrap();
791 println
!("cargo:rustc-flags=-L /path/to/{}", target
);
810 #[allow(unused_extern_crates)]
812 pub fn d2() { d1::d1(); }
818 p.cargo("build
").arg("--target
").arg(&target).arg("-v
"),
821 .with_stderr_contains(&format!(
822 "[COMPILING
] d1 v0
.0
.0 ({url}
/d1
)",
825 .with_stderr_contains(&format!("[RUNNING
] `rustc
[..] d1
[/]build
.rs
[..] --out
-dir {dir}
[/]target
[/]debug
[/]build
[/]d1
-[..]`
",
826 dir = p.root().display()))
827 .with_stderr_contains(&format!(
828 "[RUNNING
] `{dir}
[/]target
[/]debug
[/]build
[/]d1
-[..][/]build
-script
-build`
",
829 dir = p.root().display()
831 .with_stderr_contains(
832 "[RUNNING
] `rustc
[..] d1
[/]src
[/]lib
.rs
[..]`
",
834 .with_stderr_contains(&format!(
835 "[COMPILING
] d2 v0
.0
.0 ({url}
/d2
)",
838 .with_stderr_contains(&format!(
840 [RUNNING
] `rustc
[..] d2
[/]src
[/]lib
.rs
[..] \
841 -L
/path
/to
/{host}`
",
844 .with_stderr_contains(&format!(
845 "[COMPILING
] foo v0
.0
.0 ({url}
)",
848 .with_stderr_contains(&format!("\
849 [RUNNING
] `rustc
[..] build
.rs
[..] --out
-dir {dir}
[/]target
[/]debug
[/]build
[/]foo
-[..] \
850 -L
/path
/to
/{host}`
", dir = p.root().display(), host = host))
851 .with_stderr_contains(&format!(
853 [RUNNING
] `rustc
[..] src
[/]main
.rs
[..] --target {target}
[..] \
854 -L
/path
/to
/{target}`
",
861 fn build_deps_for_the_right_arch() {
862 if cross_compile::disabled() {
879 .file("src
/main
.rs
", "extern crate d2
; fn main() {}
")
880 .file("d1
/Cargo
.toml
", &basic_manifest("d1
", "0.0.0"))
881 .file("d1
/src
/lib
.rs
", "pub fn d1() {}
")
891 [build
-dependencies
.d1
]
895 .file("d2
/build
.rs
", "extern crate d1
; fn main() {}
")
896 .file("d2
/src
/lib
.rs
", "")
899 let target = cross_compile::alternate();
901 p.cargo("build
").arg("--target
").arg(&target).arg("-v
"),
902 execs().with_status(0),
907 fn build_script_only_host() {
908 if cross_compile::disabled() {
922 [build
-dependencies
.d1
]
926 .file("src
/main
.rs
", "fn main() {}
")
927 .file("build
.rs
", "extern crate d1
; fn main() {}
")
938 .file("d1
/src
/lib
.rs
", "pub fn d1() {}
")
945 assert
!(env
::var("OUT_DIR").unwrap().replace("\\", "/")
946 .contains("target/debug/build/d1-"),
947 "bad: {:?}", env
::var("OUT_DIR"));
953 let target = cross_compile::alternate();
955 p.cargo("build
").arg("--target
").arg(&target).arg("-v
"),
956 execs().with_status(0),
961 fn plugin_build_script_right_arch() {
962 if cross_compile::disabled() {
980 .file("build
.rs
", "fn main() {}
")
981 .file("src
/lib
.rs
", "")
988 .arg(cross_compile::alternate()),
989 execs().with_status(0).with_stderr(
991 [COMPILING
] foo v0
.0
.1 ([..])
992 [RUNNING
] `rustc
[..] build
.rs
[..]`
993 [RUNNING
] `
[..][/]build
-script
-build`
994 [RUNNING
] `rustc
[..] src
[/]lib
.rs
[..]`
995 [FINISHED
] dev
[unoptimized
+ debuginfo
] target(s
) in [..]
1002 fn build_script_with_platform_specific_dependencies() {
1003 if cross_compile::disabled() {
1007 let target = cross_compile::alternate();
1008 let host = rustc_host();
1019 [build
-dependencies
.d1
]
1026 #[allow(unused_extern_crates)]
1031 .file("src
/lib
.rs
", "")
1041 [target
.{}
.dependencies
]
1042 d2
= {{ path = "../d2" }
}
1047 .file("d1
/src
/lib
.rs
", "#[allow(unused_extern_crates)] extern crate d2;")
1048 .file("d2/Cargo.toml", &basic_manifest("d2", "0.0.0"))
1049 .file("d2/src/lib.rs", "")
1053 p.cargo("build").arg("-v").arg("--target").arg(&target),
1054 execs().with_status(0).with_stderr(&format!(
1056 [COMPILING] d2 v0.0.0 ([..])
1057 [RUNNING
] `rustc
[..] d2
[/]src
[/]lib
.rs
[..]`
1058 [COMPILING
] d1 v0
.0
.0 ([..])
1059 [RUNNING
] `rustc
[..] d1
[/]src
[/]lib
.rs
[..]`
1060 [COMPILING
] foo v0
.0
.1 ([..])
1061 [RUNNING
] `rustc
[..] build
.rs
[..]`
1062 [RUNNING
] `{dir}
[/]target
[/]debug
[/]build
[/]foo
-[..][/]build
-script
-build`
1063 [RUNNING
] `rustc
[..] src
[/]lib
.rs
[..] --target {target}
[..]`
1064 [FINISHED
] dev
[unoptimized
+ debuginfo
] target(s
) in [..]
1066 dir = p.root().display(),
1073 fn platform_specific_dependencies_do_not_leak() {
1074 if cross_compile::disabled() {
1078 let target = cross_compile::alternate();
1079 let host = rustc_host();
1093 [build
-dependencies
.d1
]
1097 .file("build
.rs
", "extern crate d1
; fn main() {}
")
1098 .file("src
/lib
.rs
", "")
1108 [target
.{}
.dependencies
]
1109 d2
= {{ path = "../d2" }
}
1114 .file("d1
/src
/lib
.rs
", "extern crate d2
;")
1115 .file("d1
/Cargo
.toml
", &basic_manifest("d1
", "0.0.0"))
1116 .file("d2
/src
/lib
.rs
", "")
1120 p.cargo("build
").arg("-v
").arg("--target
").arg(&target),
1123 .with_stderr_contains("[..] can't find
crate for `d2`
[..]"),
1128 fn platform_specific_variables_reflected_in_build_scripts() {
1129 if cross_compile::disabled() {
1133 let target = cross_compile::alternate();
1134 let host = rustc_host();
1146 [target
.{host}
.dependencies
]
1147 d1
= {{ path = "d1" }
}
1149 [target
.{target}
.dependencies
]
1150 d2
= {{ path = "d2" }
}
1163 let platform
= env
::var("TARGET").unwrap();
1164 let (expected
, not_expected
) = match &platform
[..] {{
1165 "{host}" => ("DEP_D1_VAL", "DEP_D2_VAL"),
1166 "{target}" => ("DEP_D2_VAL", "DEP_D1_VAL"),
1167 _
=> panic
!("unknown platform")
1170 env
::var(expected
).ok()
1171 .expect(&format
!("missing {{}}", expected
));
1172 env
::var(not_expected
).err()
1173 .expect(&format
!("found {{}}", not_expected
));
1180 .file("src
/lib
.rs
", "")
1192 .file("d1
/build
.rs
", r#"fn main() { println!("cargo:val=1") }
"#)
1193 .file("d1
/src
/lib
.rs
", "")
1205 .file("d2
/build
.rs
", r#"fn main() { println!("cargo:val=1") }
"#)
1206 .file("d2
/src
/lib
.rs
", "")
1209 assert_that(p.cargo("build
").arg("-v
"), execs().with_status(0));
1211 p.cargo("build
").arg("-v
").arg("--target
").arg(&target),
1212 execs().with_status(0),
1217 fn cross_test_dylib() {
1218 if cross_compile::disabled() {
1222 let target = cross_compile::alternate();
1235 crate_type
= ["dylib"]
1244 extern crate bar
as the_bar
;
1246 pub fn bar() { the_bar::baz(); }
1255 extern crate foo
as the_foo
;
1258 fn foo() { the_foo::bar(); }
1271 crate_type
= ["dylib"]
1280 assert_eq
!(env
::consts
::ARCH
, "{}");
1283 cross_compile::alternate_arch()
1289 p.cargo("test
").arg("--target
").arg(&target),
1292 .with_stderr(&format!(
1294 [COMPILING
] bar v0
.0
.1 ({dir}
/bar
)
1295 [COMPILING
] foo v0
.0
.1 ({dir}
)
1296 [FINISHED
] dev
[unoptimized
+ debuginfo
] target(s
) in [..]
1297 [RUNNING
] target
[/]{arch}
[/]debug
[/]deps
[/]foo
-[..][EXE
]
1298 [RUNNING
] target
[/]{arch}
[/]debug
[/]deps
[/]test
-[..][EXE
]",
1300 arch = cross_compile::alternate()
1302 .with_stdout_contains_n("test foo
... ok
", 2),