1 use cargo
::util
::process
;
2 use cargotest
::{is_nightly, rustc_host}
;
3 use cargotest
::support
::{basic_bin_manifest, cross_compile, execs, project}
;
4 use hamcrest
::{assert_that, existing_file}
;
8 if cross_compile
::disabled() {
12 let p
= project("foo")
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() {
67 let p
= project("foo")
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() {
129 let p
= project("foo")
146 fn main() { bar::bar(); }
150 let _p2
= project("bar")
160 .file("src/lib.rs", "pub fn bar() {}")
163 let target
= cross_compile
::alternate();
165 p
.cargo("build").arg("--target").arg(&target
),
166 execs().with_status(0),
168 assert_that(&p
.target_bin(&target
, "foo"), existing_file());
171 process(&p
.target_bin(&target
, "foo")),
172 execs().with_status(0),
178 if cross_compile
::disabled() {
185 let foo
= project("foo")
208 assert_eq!(bar!(), baz::baz());
213 let _bar
= project("bar")
230 #![feature(plugin_registrar, rustc_private)]
232 extern crate rustc_plugin;
235 use rustc_plugin::Registry;
236 use syntax::tokenstream::TokenTree;
237 use syntax::codemap::Span;
239 use syntax::ext::base::{ExtCtxt, MacEager, MacResult};
240 use syntax::ext::build::AstBuilder;
243 pub fn foo(reg: &mut Registry) {
244 reg.register_macro("bar", expand_bar);
247 fn expand_bar(cx: &mut ExtCtxt, sp: Span, tts: &[TokenTree])
248 -> Box<MacResult + 'static> {
249 MacEager::expr(cx.expr_lit(sp, LitKind::Int(1, LitIntType::Unsuffixed)))
254 let _baz
= project("baz")
264 .file("src/lib.rs", "pub fn baz() -> i32 { 1 }")
267 let target
= cross_compile
::alternate();
269 foo
.cargo("build").arg("--target").arg(&target
),
270 execs().with_status(0),
272 assert_that(&foo
.target_bin(&target
, "foo"), existing_file());
275 process(&foo
.target_bin(&target
, "foo")),
276 execs().with_status(0),
281 fn plugin_to_the_max() {
282 if cross_compile
::disabled() {
289 let foo
= project("foo")
312 assert_eq!(bar!(), baz::baz());
317 let _bar
= project("bar")
337 #![feature(plugin_registrar, rustc_private)]
339 extern crate rustc_plugin;
343 use rustc_plugin::Registry;
344 use syntax::tokenstream::TokenTree;
345 use syntax::codemap::Span;
347 use syntax::ext::base::{ExtCtxt, MacEager, MacResult};
348 use syntax::ext::build::AstBuilder;
352 pub fn foo(reg: &mut Registry) {
353 reg.register_macro("bar", expand_bar);
356 fn expand_bar(cx: &mut ExtCtxt, sp: Span, tts: &[TokenTree])
357 -> Box<MacResult + 'static> {
358 let bar = Ident::from_str("baz");
359 let path = cx.path(sp, vec![bar.clone(), bar]);
360 MacEager::expr(cx.expr_call(sp, cx.expr_path(path), vec![]))
365 let _baz
= project("baz")
375 .file("src/lib.rs", "pub fn baz() -> i32 { 1 }")
378 let target
= cross_compile
::alternate();
380 foo
.cargo("build").arg("--target").arg(&target
).arg("-v"),
381 execs().with_status(0),
385 foo
.cargo("build").arg("-v").arg("--target").arg(&target
),
386 execs().with_status(0),
388 assert_that(&foo
.target_bin(&target
, "foo"), existing_file());
391 process(&foo
.target_bin(&target
, "foo")),
392 execs().with_status(0),
398 if cross_compile
::disabled() {
402 let target
= cross_compile
::alternate();
403 let p
= project("foo")
410 linker = "my-linker-tool"
415 .file("Cargo.toml", &basic_bin_manifest("foo"))
422 assert_eq!(env::consts::ARCH, "{}");
425 cross_compile
::alternate_arch()
431 p
.cargo("build").arg("--target").arg(&target
).arg("-v"),
432 execs().with_status(101).with_stderr_contains(&format
!(
434 [COMPILING] foo v0.5.0 ({url})
435 [RUNNING] `rustc --crate-name foo src[/]foo.rs --crate-type bin \
436 --emit=dep-info,link -C debuginfo=2 \
438 --out-dir {dir}[/]target[/]{target}[/]debug[/]deps \
440 -C ar=my-ar-tool -C linker=my-linker-tool \
441 -L dependency={dir}[/]target[/]{target}[/]debug[/]deps \
442 -L dependency={dir}[/]target[/]debug[/]deps`
444 dir
= p
.root().display(),
452 fn plugin_with_extra_dylib_dep() {
453 if cross_compile
::disabled() {
460 let foo
= project("foo")
483 let _bar
= project("bar")
503 #![feature(plugin_registrar, rustc_private)]
505 extern crate rustc_plugin;
508 use rustc_plugin::Registry;
511 pub fn foo(reg: &mut Registry) {
512 println!("{}", baz::baz());
517 let _baz
= project("baz")
528 crate_type = ["dylib"]
531 .file("src/lib.rs", "pub fn baz() -> i32 { 1 }")
534 let target
= cross_compile
::alternate();
536 foo
.cargo("build").arg("--target").arg(&target
),
537 execs().with_status(0),
543 if cross_compile
::disabled() {
547 let p
= project("foo")
564 #[allow(unused_extern_crates)]
568 assert_eq!(env::consts::ARCH, "{}");
570 #[test] fn test() {{ main() }}
572 cross_compile
::alternate_arch()
580 pub fn foo() {{ assert_eq!(env::consts::ARCH, "{}"); }}
581 #[test] fn test_foo() {{ foo() }}
583 cross_compile::alternate_arch()
588 let target = cross_compile::alternate();
590 p.cargo("test
").arg("--target
").arg(&target),
593 .with_stderr(&format!(
595 [COMPILING
] foo v0
.0
.0 ({foo}
)
596 [FINISHED
] dev
[unoptimized
+ debuginfo
] target(s
) in [..]
597 [RUNNING
] target
[/]{triple}
[/]debug
[/]deps
[/]foo
-[..][EXE
]
598 [RUNNING
] target
[/]{triple}
[/]debug
[/]deps
[/]bar
-[..][EXE
]",
602 .with_stdout_contains("test test_foo
... ok
")
603 .with_stdout_contains("test test
... ok
"),
608 fn no_cross_doctests() {
609 if cross_compile::disabled() {
613 let p = project("foo
")
627 //! extern crate foo;
634 let host_output = format!(
636 [COMPILING
] foo v0
.0
.0 ({foo}
)
637 [FINISHED
] dev
[unoptimized
+ debuginfo
] target(s
) in [..]
638 [RUNNING
] target
[/]debug
[/]deps
[/]foo
-[..][EXE
]
647 execs().with_status(0).with_stderr(&host_output),
651 let target = cross_compile::host();
653 p.cargo("test
").arg("--target
").arg(&target),
654 execs().with_status(0).with_stderr(&format!(
656 [COMPILING
] foo v0
.0
.0 ({foo}
)
657 [FINISHED
] dev
[unoptimized
+ debuginfo
] target(s
) in [..]
658 [RUNNING
] target
[/]{triple}
[/]debug
[/]deps
[/]foo
-[..][EXE
]
667 let target = cross_compile::alternate();
669 p.cargo("test
").arg("--target
").arg(&target),
670 execs().with_status(0).with_stderr(&format!(
672 [COMPILING
] foo v0
.0
.0 ({foo}
)
673 [FINISHED
] dev
[unoptimized
+ debuginfo
] target(s
) in [..]
674 [RUNNING
] target
[/]{triple}
[/]debug
[/]deps
[/]foo
-[..][EXE
]
683 fn simple_cargo_run() {
684 if cross_compile::disabled() {
688 let p = project("foo
")
704 assert_eq
!(env
::consts
::ARCH
, "{}");
707 cross_compile::alternate_arch()
712 let target = cross_compile::alternate();
714 p.cargo("run
").arg("--target
").arg(&target),
715 execs().with_status(0),
720 fn cross_with_a_build_script() {
721 if cross_compile::disabled() {
725 let target = cross_compile::alternate();
726 let p = project("foo
")
742 use std
::path
::PathBuf
;
744 assert_eq
!(env
::var("TARGET").unwrap(), "{0}");
745 let mut path
= PathBuf
::from(env
::var_os("OUT_DIR").unwrap());
746 assert_eq
!(path
.file_name().unwrap().to_str().unwrap(), "out");
748 assert
!(path
.file_name().unwrap().to_str().unwrap()
749 .starts_with("foo-"));
751 assert_eq
!(path
.file_name().unwrap().to_str().unwrap(), "build");
753 assert_eq
!(path
.file_name().unwrap().to_str().unwrap(), "debug");
755 assert_eq
!(path
.file_name().unwrap().to_str().unwrap(), "{0}");
757 assert_eq
!(path
.file_name().unwrap().to_str().unwrap(), "target");
763 .file("src
/main
.rs
", "fn main() {}
")
767 p.cargo("build
").arg("--target
").arg(&target).arg("-v
"),
768 execs().with_status(0).with_stderr(&format!(
770 [COMPILING
] foo v0
.0
.0 (file
://[..])
771 [RUNNING
] `rustc
[..] build
.rs
[..] --out
-dir {dir}
[/]target
[/]debug
[/]build
[/]foo
-[..]`
772 [RUNNING
] `{dir}
[/]target
[/]debug
[/]build
[/]foo
-[..][/]build
-script
-build`
773 [RUNNING
] `rustc
[..] src
[/]main
.rs
[..] --target {target}
[..]`
774 [FINISHED
] dev
[unoptimized
+ debuginfo
] target(s
) in [..]
777 dir = p.root().display()
783 fn build_script_needed_for_host_and_target() {
784 if cross_compile::disabled() {
788 let target = cross_compile::alternate();
789 let host = rustc_host();
790 let p = project("foo
")
802 [build
-dependencies
.d2
]
809 #[allow(unused_extern_crates)]
811 fn main() { d2::d2(); }
817 #[allow(unused_extern_crates)]
819 fn main() { d1::d1(); }
843 let target
= env
::var("TARGET").unwrap();
844 println
!("cargo:rustc-flags=-L /path/to/{}", target
);
863 #[allow(unused_extern_crates)]
865 pub fn d2() { d1::d1(); }
871 p.cargo("build
").arg("--target
").arg(&target).arg("-v
"),
874 .with_stderr_contains(&format!(
875 "[COMPILING
] d1 v0
.0
.0 ({url}
/d1
)",
878 .with_stderr_contains(&format!("[RUNNING
] `rustc
[..] d1
[/]build
.rs
[..] --out
-dir {dir}
[/]target
[/]debug
[/]build
[/]d1
-[..]`
",
879 dir = p.root().display()))
880 .with_stderr_contains(&format!(
881 "[RUNNING
] `{dir}
[/]target
[/]debug
[/]build
[/]d1
-[..][/]build
-script
-build`
",
882 dir = p.root().display()
884 .with_stderr_contains(
885 "[RUNNING
] `rustc
[..] d1
[/]src
[/]lib
.rs
[..]`
",
887 .with_stderr_contains(&format!(
888 "[COMPILING
] d2 v0
.0
.0 ({url}
/d2
)",
891 .with_stderr_contains(&format!(
893 [RUNNING
] `rustc
[..] d2
[/]src
[/]lib
.rs
[..] \
894 -L
/path
/to
/{host}`
",
897 .with_stderr_contains(&format!(
898 "[COMPILING
] foo v0
.0
.0 ({url}
)",
901 .with_stderr_contains(&format!("\
902 [RUNNING
] `rustc
[..] build
.rs
[..] --out
-dir {dir}
[/]target
[/]debug
[/]build
[/]foo
-[..] \
903 -L
/path
/to
/{host}`
", dir = p.root().display(), host = host))
904 .with_stderr_contains(&format!(
906 [RUNNING
] `rustc
[..] src
[/]main
.rs
[..] --target {target}
[..] \
907 -L
/path
/to
/{target}`
",
914 fn build_deps_for_the_right_arch() {
915 if cross_compile::disabled() {
919 let p = project("foo
")
932 .file("src
/main
.rs
", "extern crate d2
; fn main() {}
")
957 [build
-dependencies
.d1
]
961 .file("d2
/build
.rs
", "extern crate d1
; fn main() {}
")
962 .file("d2
/src
/lib
.rs
", "")
965 let target = cross_compile::alternate();
967 p.cargo("build
").arg("--target
").arg(&target).arg("-v
"),
968 execs().with_status(0),
973 fn build_script_only_host() {
974 if cross_compile::disabled() {
978 let p = project("foo
")
988 [build
-dependencies
.d1
]
992 .file("src
/main
.rs
", "fn main() {}
")
993 .file("build
.rs
", "extern crate d1
; fn main() {}
")
1016 assert
!(env
::var("OUT_DIR").unwrap().replace("\\", "/")
1017 .contains("target/debug/build/d1-"),
1018 "bad: {:?}", env
::var("OUT_DIR"));
1024 let target = cross_compile::alternate();
1026 p.cargo("build
").arg("--target
").arg(&target).arg("-v
"),
1027 execs().with_status(0),
1032 fn plugin_build_script_right_arch() {
1033 if cross_compile::disabled() {
1036 let p = project("foo
")
1051 .file("build
.rs
", "fn main() {}
")
1052 .file("src
/lib
.rs
", "")
1059 .arg(cross_compile::alternate()),
1060 execs().with_status(0).with_stderr(
1062 [COMPILING
] foo v0
.0
.1 ([..])
1063 [RUNNING
] `rustc
[..] build
.rs
[..]`
1064 [RUNNING
] `
[..][/]build
-script
-build`
1065 [RUNNING
] `rustc
[..] src
[/]lib
.rs
[..]`
1066 [FINISHED
] dev
[unoptimized
+ debuginfo
] target(s
) in [..]
1073 fn build_script_with_platform_specific_dependencies() {
1074 if cross_compile::disabled() {
1078 let target = cross_compile::alternate();
1079 let host = rustc_host();
1080 let p = project("foo
")
1090 [build
-dependencies
.d1
]
1097 #[allow(unused_extern_crates)]
1102 .file("src
/lib
.rs
", "")
1112 [target
.{}
.dependencies
]
1113 d2
= {{ path = "../d2" }
}
1121 #[allow(unused_extern_crates)]
1134 .file("d2
/src
/lib
.rs
", "")
1138 p.cargo("build
").arg("-v
").arg("--target
").arg(&target),
1139 execs().with_status(0).with_stderr(&format!(
1141 [COMPILING
] d2 v0
.0
.0 ([..])
1142 [RUNNING
] `rustc
[..] d2
[/]src
[/]lib
.rs
[..]`
1143 [COMPILING
] d1 v0
.0
.0 ([..])
1144 [RUNNING
] `rustc
[..] d1
[/]src
[/]lib
.rs
[..]`
1145 [COMPILING
] foo v0
.0
.1 ([..])
1146 [RUNNING
] `rustc
[..] build
.rs
[..]`
1147 [RUNNING
] `{dir}
[/]target
[/]debug
[/]build
[/]foo
-[..][/]build
-script
-build`
1148 [RUNNING
] `rustc
[..] src
[/]lib
.rs
[..] --target {target}
[..]`
1149 [FINISHED
] dev
[unoptimized
+ debuginfo
] target(s
) in [..]
1151 dir = p.root().display(),
1158 fn platform_specific_dependencies_do_not_leak() {
1159 if cross_compile::disabled() {
1163 let target = cross_compile::alternate();
1164 let host = rustc_host();
1165 let p = project("foo
")
1178 [build
-dependencies
.d1
]
1182 .file("build
.rs
", "extern crate d1
; fn main() {}
")
1183 .file("src
/lib
.rs
", "")
1193 [target
.{}
.dependencies
]
1194 d2
= {{ path = "../d2" }
}
1199 .file("d1
/src
/lib
.rs
", "extern crate d2
;")
1209 .file("d2
/src
/lib
.rs
", "")
1213 p.cargo("build
").arg("-v
").arg("--target
").arg(&target),
1216 .with_stderr_contains("[..] can't find
crate for `d2`
[..]"),
1221 fn platform_specific_variables_reflected_in_build_scripts() {
1222 if cross_compile::disabled() {
1226 let target = cross_compile::alternate();
1227 let host = rustc_host();
1228 let p = project("foo
")
1239 [target
.{host}
.dependencies
]
1240 d1
= {{ path = "d1" }
}
1242 [target
.{target}
.dependencies
]
1243 d2
= {{ path = "d2" }
}
1256 let platform
= env
::var("TARGET").unwrap();
1257 let (expected
, not_expected
) = match &platform
[..] {{
1258 "{host}" => ("DEP_D1_VAL", "DEP_D2_VAL"),
1259 "{target}" => ("DEP_D2_VAL", "DEP_D1_VAL"),
1260 _
=> panic
!("unknown platform")
1263 env
::var(expected
).ok()
1264 .expect(&format
!("missing {{}}", expected
));
1265 env
::var(not_expected
).err()
1266 .expect(&format
!("found {{}}", not_expected
));
1273 .file("src
/lib
.rs
", "")
1288 fn main() { println!("cargo:val=1") }
1291 .file("d1
/src
/lib
.rs
", "")
1306 fn main() { println!("cargo:val=1") }
1309 .file("d2
/src
/lib
.rs
", "")
1312 assert_that(p.cargo("build
").arg("-v
"), execs().with_status(0));
1314 p.cargo("build
").arg("-v
").arg("--target
").arg(&target),
1315 execs().with_status(0),
1320 fn cross_test_dylib() {
1321 if cross_compile::disabled() {
1325 let target = cross_compile::alternate();
1327 let p = project("foo
")
1338 crate_type
= ["dylib"]
1347 extern crate bar
as the_bar
;
1349 pub fn bar() { the_bar::baz(); }
1358 extern crate foo
as the_foo
;
1361 fn foo() { the_foo::bar(); }
1374 crate_type
= ["dylib"]
1383 assert_eq
!(env
::consts
::ARCH
, "{}");
1386 cross_compile::alternate_arch()
1392 p.cargo("test
").arg("--target
").arg(&target),
1395 .with_stderr(&format!(
1397 [COMPILING
] bar v0
.0
.1 ({dir}
/bar
)
1398 [COMPILING
] foo v0
.0
.1 ({dir}
)
1399 [FINISHED
] dev
[unoptimized
+ debuginfo
] target(s
) in [..]
1400 [RUNNING
] target
[/]{arch}
[/]debug
[/]deps
[/]foo
-[..][EXE
]
1401 [RUNNING
] target
[/]{arch}
[/]debug
[/]deps
[/]test
-[..][EXE
]",
1403 arch = cross_compile::alternate()
1405 .with_stdout_contains_n("test foo
... ok
", 2),