]> git.proxmox.com Git - cargo.git/blob - tests/testsuite/cross_compile.rs
Auto merge of #5957 - lostiniceland:5717-curl-low_speed, r=alexcrichton
[cargo.git] / tests / testsuite / cross_compile.rs
1 use support::{basic_bin_manifest, basic_manifest, cross_compile, project};
2 use support::{is_nightly, rustc_host};
3
4 #[test]
5 fn simple_cross() {
6 if cross_compile::disabled() {
7 return;
8 }
9
10 let p = project()
11 .file(
12 "Cargo.toml",
13 r#"
14 [package]
15 name = "foo"
16 version = "0.0.0"
17 authors = []
18 build = "build.rs"
19 "#,
20 ).file(
21 "build.rs",
22 &format!(
23 r#"
24 fn main() {{
25 assert_eq!(std::env::var("TARGET").unwrap(), "{}");
26 }}
27 "#,
28 cross_compile::alternate()
29 ),
30 ).file(
31 "src/main.rs",
32 &format!(
33 r#"
34 use std::env;
35 fn main() {{
36 assert_eq!(env::consts::ARCH, "{}");
37 }}
38 "#,
39 cross_compile::alternate_arch()
40 ),
41 ).build();
42
43 let target = cross_compile::alternate();
44 p.cargo("build -v --target").arg(&target).run();
45 assert!(p.target_bin(&target, "foo").is_file());
46
47 p.process(&p.target_bin(&target, "foo")).run();
48 }
49
50 #[test]
51 fn simple_cross_config() {
52 if cross_compile::disabled() {
53 return;
54 }
55
56 let p = project()
57 .file(
58 ".cargo/config",
59 &format!(
60 r#"
61 [build]
62 target = "{}"
63 "#,
64 cross_compile::alternate()
65 ),
66 ).file(
67 "Cargo.toml",
68 r#"
69 [package]
70 name = "foo"
71 version = "0.0.0"
72 authors = []
73 build = "build.rs"
74 "#,
75 ).file(
76 "build.rs",
77 &format!(
78 r#"
79 fn main() {{
80 assert_eq!(std::env::var("TARGET").unwrap(), "{}");
81 }}
82 "#,
83 cross_compile::alternate()
84 ),
85 ).file(
86 "src/main.rs",
87 &format!(
88 r#"
89 use std::env;
90 fn main() {{
91 assert_eq!(env::consts::ARCH, "{}");
92 }}
93 "#,
94 cross_compile::alternate_arch()
95 ),
96 ).build();
97
98 let target = cross_compile::alternate();
99 p.cargo("build -v").run();
100 assert!(p.target_bin(&target, "foo").is_file());
101
102 p.process(&p.target_bin(&target, "foo")).run();
103 }
104
105 #[test]
106 fn simple_deps() {
107 if cross_compile::disabled() {
108 return;
109 }
110
111 let p = project()
112 .file(
113 "Cargo.toml",
114 r#"
115 [package]
116 name = "foo"
117 version = "0.0.1"
118 authors = []
119
120 [dependencies.bar]
121 path = "../bar"
122 "#,
123 ).file("src/main.rs", "extern crate bar; fn main() { bar::bar(); }")
124 .build();
125 let _p2 = project()
126 .at("bar")
127 .file("Cargo.toml", &basic_manifest("bar", "0.0.1"))
128 .file("src/lib.rs", "pub fn bar() {}")
129 .build();
130
131 let target = cross_compile::alternate();
132 p.cargo("build --target").arg(&target).run();
133 assert!(p.target_bin(&target, "foo").is_file());
134
135 p.process(&p.target_bin(&target, "foo")).run();
136 }
137
138 #[test]
139 fn plugin_deps() {
140 if cross_compile::disabled() {
141 return;
142 }
143 if !is_nightly() {
144 return;
145 }
146
147 let foo = project()
148 .file(
149 "Cargo.toml",
150 r#"
151 [package]
152 name = "foo"
153 version = "0.0.1"
154 authors = []
155
156 [dependencies.bar]
157 path = "../bar"
158
159 [dependencies.baz]
160 path = "../baz"
161 "#,
162 ).file(
163 "src/main.rs",
164 r#"
165 #![feature(plugin)]
166 #![plugin(bar)]
167 extern crate baz;
168 fn main() {
169 assert_eq!(bar!(), baz::baz());
170 }
171 "#,
172 ).build();
173 let _bar = project()
174 .at("bar")
175 .file(
176 "Cargo.toml",
177 r#"
178 [package]
179 name = "bar"
180 version = "0.0.1"
181 authors = []
182
183 [lib]
184 name = "bar"
185 plugin = true
186 "#,
187 ).file(
188 "src/lib.rs",
189 r#"
190 #![feature(plugin_registrar, rustc_private)]
191
192 extern crate rustc_plugin;
193 extern crate syntax;
194
195 use rustc_plugin::Registry;
196 use syntax::tokenstream::TokenTree;
197 use syntax::source_map::Span;
198 use syntax::ast::*;
199 use syntax::ext::base::{ExtCtxt, MacEager, MacResult};
200 use syntax::ext::build::AstBuilder;
201
202 #[plugin_registrar]
203 pub fn foo(reg: &mut Registry) {
204 reg.register_macro("bar", expand_bar);
205 }
206
207 fn expand_bar(cx: &mut ExtCtxt, sp: Span, tts: &[TokenTree])
208 -> Box<MacResult + 'static> {
209 MacEager::expr(cx.expr_lit(sp, LitKind::Int(1, LitIntType::Unsuffixed)))
210 }
211 "#,
212 ).build();
213 let _baz = project()
214 .at("baz")
215 .file("Cargo.toml", &basic_manifest("baz", "0.0.1"))
216 .file("src/lib.rs", "pub fn baz() -> i32 { 1 }")
217 .build();
218
219 let target = cross_compile::alternate();
220 foo.cargo("build --target").arg(&target).run();
221 assert!(foo.target_bin(&target, "foo").is_file());
222
223 foo.process(&foo.target_bin(&target, "foo")).run();
224 }
225
226 #[test]
227 fn plugin_to_the_max() {
228 if cross_compile::disabled() {
229 return;
230 }
231 if !is_nightly() {
232 return;
233 }
234
235 let foo = project()
236 .file(
237 "Cargo.toml",
238 r#"
239 [package]
240 name = "foo"
241 version = "0.0.1"
242 authors = []
243
244 [dependencies.bar]
245 path = "../bar"
246
247 [dependencies.baz]
248 path = "../baz"
249 "#,
250 ).file(
251 "src/main.rs",
252 r#"
253 #![feature(plugin)]
254 #![plugin(bar)]
255 extern crate baz;
256 fn main() {
257 assert_eq!(bar!(), baz::baz());
258 }
259 "#,
260 ).build();
261 let _bar = project()
262 .at("bar")
263 .file(
264 "Cargo.toml",
265 r#"
266 [package]
267 name = "bar"
268 version = "0.0.1"
269 authors = []
270
271 [lib]
272 name = "bar"
273 plugin = true
274
275 [dependencies.baz]
276 path = "../baz"
277 "#,
278 ).file(
279 "src/lib.rs",
280 r#"
281 #![feature(plugin_registrar, rustc_private)]
282
283 extern crate rustc_plugin;
284 extern crate syntax;
285 extern crate baz;
286
287 use rustc_plugin::Registry;
288 use syntax::tokenstream::TokenTree;
289 use syntax::source_map::Span;
290 use syntax::ast::*;
291 use syntax::ext::base::{ExtCtxt, MacEager, MacResult};
292 use syntax::ext::build::AstBuilder;
293 use syntax::ptr::P;
294
295 #[plugin_registrar]
296 pub fn foo(reg: &mut Registry) {
297 reg.register_macro("bar", expand_bar);
298 }
299
300 fn expand_bar(cx: &mut ExtCtxt, sp: Span, tts: &[TokenTree])
301 -> Box<MacResult + 'static> {
302 let bar = Ident::from_str("baz");
303 let path = cx.path(sp, vec![bar.clone(), bar]);
304 MacEager::expr(cx.expr_call(sp, cx.expr_path(path), vec![]))
305 }
306 "#,
307 ).build();
308 let _baz = project()
309 .at("baz")
310 .file("Cargo.toml", &basic_manifest("baz", "0.0.1"))
311 .file("src/lib.rs", "pub fn baz() -> i32 { 1 }")
312 .build();
313
314 let target = cross_compile::alternate();
315 foo.cargo("build -v --target").arg(&target).run();
316 println!("second");
317 foo.cargo("build -v --target").arg(&target).run();
318 assert!(foo.target_bin(&target, "foo").is_file());
319
320 foo.process(&foo.target_bin(&target, "foo")).run();
321 }
322
323 #[test]
324 fn linker_and_ar() {
325 if cross_compile::disabled() {
326 return;
327 }
328
329 let target = cross_compile::alternate();
330 let p = project()
331 .file(
332 ".cargo/config",
333 &format!(
334 r#"
335 [target.{}]
336 ar = "my-ar-tool"
337 linker = "my-linker-tool"
338 "#,
339 target
340 ),
341 ).file("Cargo.toml", &basic_bin_manifest("foo"))
342 .file(
343 "src/foo.rs",
344 &format!(
345 r#"
346 use std::env;
347 fn main() {{
348 assert_eq!(env::consts::ARCH, "{}");
349 }}
350 "#,
351 cross_compile::alternate_arch()
352 ),
353 ).build();
354
355 p.cargo("build -v --target")
356 .arg(&target)
357 .with_status(101)
358 .with_stderr_contains(&format!(
359 "\
360 [COMPILING] foo v0.5.0 (CWD)
361 [RUNNING] `rustc --crate-name foo src/foo.rs --crate-type bin \
362 --emit=dep-info,link -C debuginfo=2 \
363 -C metadata=[..] \
364 --out-dir CWD/target/{target}/debug/deps \
365 --target {target} \
366 -C ar=my-ar-tool -C linker=my-linker-tool \
367 -L dependency=CWD/target/{target}/debug/deps \
368 -L dependency=CWD/target/debug/deps`
369 ",
370 target = target,
371 )).run();
372 }
373
374 #[test]
375 fn plugin_with_extra_dylib_dep() {
376 if cross_compile::disabled() {
377 return;
378 }
379 if !is_nightly() {
380 return;
381 }
382
383 let foo = project()
384 .file(
385 "Cargo.toml",
386 r#"
387 [package]
388 name = "foo"
389 version = "0.0.1"
390 authors = []
391
392 [dependencies.bar]
393 path = "../bar"
394 "#,
395 ).file(
396 "src/main.rs",
397 r#"
398 #![feature(plugin)]
399 #![plugin(bar)]
400
401 fn main() {}
402 "#,
403 ).build();
404 let _bar = project()
405 .at("bar")
406 .file(
407 "Cargo.toml",
408 r#"
409 [package]
410 name = "bar"
411 version = "0.0.1"
412 authors = []
413
414 [lib]
415 name = "bar"
416 plugin = true
417
418 [dependencies.baz]
419 path = "../baz"
420 "#,
421 ).file(
422 "src/lib.rs",
423 r#"
424 #![feature(plugin_registrar, rustc_private)]
425
426 extern crate rustc_plugin;
427 extern crate baz;
428
429 use rustc_plugin::Registry;
430
431 #[plugin_registrar]
432 pub fn foo(reg: &mut Registry) {
433 println!("{}", baz::baz());
434 }
435 "#,
436 ).build();
437 let _baz = project()
438 .at("baz")
439 .file(
440 "Cargo.toml",
441 r#"
442 [package]
443 name = "baz"
444 version = "0.0.1"
445 authors = []
446
447 [lib]
448 name = "baz"
449 crate_type = ["dylib"]
450 "#,
451 ).file("src/lib.rs", "pub fn baz() -> i32 { 1 }")
452 .build();
453
454 let target = cross_compile::alternate();
455 foo.cargo("build --target").arg(&target).run();
456 }
457
458 #[test]
459 fn cross_tests() {
460 if cross_compile::disabled() {
461 return;
462 }
463
464 let p = project()
465 .file(
466 "Cargo.toml",
467 r#"
468 [project]
469 name = "foo"
470 authors = []
471 version = "0.0.0"
472
473 [[bin]]
474 name = "bar"
475 "#,
476 ).file(
477 "src/bin/bar.rs",
478 &format!(
479 r#"
480 #[allow(unused_extern_crates)]
481 extern crate foo;
482 use std::env;
483 fn main() {{
484 assert_eq!(env::consts::ARCH, "{}");
485 }}
486 #[test] fn test() {{ main() }}
487 "#,
488 cross_compile::alternate_arch()
489 ),
490 ).file(
491 "src/lib.rs",
492 &format!(
493 r#"
494 use std::env;
495 pub fn foo() {{ assert_eq!(env::consts::ARCH, "{}"); }}
496 #[test] fn test_foo() {{ foo() }}
497 "#,
498 cross_compile::alternate_arch()
499 ),
500 ).build();
501
502 let target = cross_compile::alternate();
503 p.cargo("test --target")
504 .arg(&target)
505 .with_stderr(&format!(
506 "\
507 [COMPILING] foo v0.0.0 (CWD)
508 [FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
509 [RUNNING] target/{triple}/debug/deps/foo-[..][EXE]
510 [RUNNING] target/{triple}/debug/deps/bar-[..][EXE]",
511 triple = target
512 )).with_stdout_contains("test test_foo ... ok")
513 .with_stdout_contains("test test ... ok")
514 .run();
515 }
516
517 #[test]
518 fn no_cross_doctests() {
519 if cross_compile::disabled() {
520 return;
521 }
522
523 let p = project()
524 .file(
525 "src/lib.rs",
526 r#"
527 //! ```
528 //! extern crate foo;
529 //! assert!(true);
530 //! ```
531 "#,
532 ).build();
533
534 let host_output =
535 "\
536 [COMPILING] foo v0.0.1 (CWD)
537 [FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
538 [RUNNING] target/debug/deps/foo-[..][EXE]
539 [DOCTEST] foo
540 ";
541
542 println!("a");
543 p.cargo("test").with_stderr(&host_output).run();
544
545 println!("b");
546 let target = cross_compile::host();
547 p.cargo("test --target")
548 .arg(&target)
549 .with_stderr(&format!(
550 "\
551 [COMPILING] foo v0.0.1 (CWD)
552 [FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
553 [RUNNING] target/{triple}/debug/deps/foo-[..][EXE]
554 [DOCTEST] foo
555 ",
556 triple = target
557 )).run();
558
559 println!("c");
560 let target = cross_compile::alternate();
561 p.cargo("test --target")
562 .arg(&target)
563 .with_stderr(&format!(
564 "\
565 [COMPILING] foo v0.0.1 (CWD)
566 [FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
567 [RUNNING] target/{triple}/debug/deps/foo-[..][EXE]
568 ",
569 triple = target
570 )).run();
571 }
572
573 #[test]
574 fn simple_cargo_run() {
575 if cross_compile::disabled() {
576 return;
577 }
578
579 let p = project()
580 .file(
581 "src/main.rs",
582 &format!(
583 r#"
584 use std::env;
585 fn main() {{
586 assert_eq!(env::consts::ARCH, "{}");
587 }}
588 "#,
589 cross_compile::alternate_arch()
590 ),
591 ).build();
592
593 let target = cross_compile::alternate();
594 p.cargo("run --target").arg(&target).run();
595 }
596
597 #[test]
598 fn cross_with_a_build_script() {
599 if cross_compile::disabled() {
600 return;
601 }
602
603 let target = cross_compile::alternate();
604 let p = project()
605 .file(
606 "Cargo.toml",
607 r#"
608 [package]
609 name = "foo"
610 version = "0.0.0"
611 authors = []
612 build = 'build.rs'
613 "#,
614 ).file(
615 "build.rs",
616 &format!(
617 r#"
618 use std::env;
619 use std::path::PathBuf;
620 fn main() {{
621 assert_eq!(env::var("TARGET").unwrap(), "{0}");
622 let mut path = PathBuf::from(env::var_os("OUT_DIR").unwrap());
623 assert_eq!(path.file_name().unwrap().to_str().unwrap(), "out");
624 path.pop();
625 assert!(path.file_name().unwrap().to_str().unwrap()
626 .starts_with("foo-"));
627 path.pop();
628 assert_eq!(path.file_name().unwrap().to_str().unwrap(), "build");
629 path.pop();
630 assert_eq!(path.file_name().unwrap().to_str().unwrap(), "debug");
631 path.pop();
632 assert_eq!(path.file_name().unwrap().to_str().unwrap(), "{0}");
633 path.pop();
634 assert_eq!(path.file_name().unwrap().to_str().unwrap(), "target");
635 }}
636 "#,
637 target
638 ),
639 ).file("src/main.rs", "fn main() {}")
640 .build();
641
642 p.cargo("build -v --target")
643 .arg(&target)
644 .with_stderr(&format!(
645 "\
646 [COMPILING] foo v0.0.0 (CWD)
647 [RUNNING] `rustc [..] build.rs [..] --out-dir CWD/target/debug/build/foo-[..]`
648 [RUNNING] `CWD/target/debug/build/foo-[..]/build-script-build`
649 [RUNNING] `rustc [..] src/main.rs [..] --target {target} [..]`
650 [FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
651 ",
652 target = target,
653 )).run();
654 }
655
656 #[test]
657 fn build_script_needed_for_host_and_target() {
658 if cross_compile::disabled() {
659 return;
660 }
661
662 let target = cross_compile::alternate();
663 let host = rustc_host();
664 let p = project()
665 .file(
666 "Cargo.toml",
667 r#"
668 [package]
669 name = "foo"
670 version = "0.0.0"
671 authors = []
672 build = 'build.rs'
673
674 [dependencies.d1]
675 path = "d1"
676 [build-dependencies.d2]
677 path = "d2"
678 "#,
679 ).file(
680 "build.rs",
681 r#"
682 #[allow(unused_extern_crates)]
683 extern crate d2;
684 fn main() { d2::d2(); }
685 "#,
686 ).file(
687 "src/main.rs",
688 "
689 #[allow(unused_extern_crates)]
690 extern crate d1;
691 fn main() { d1::d1(); }
692 ",
693 ).file(
694 "d1/Cargo.toml",
695 r#"
696 [package]
697 name = "d1"
698 version = "0.0.0"
699 authors = []
700 build = 'build.rs'
701 "#,
702 ).file("d1/src/lib.rs", "pub fn d1() {}")
703 .file(
704 "d1/build.rs",
705 r#"
706 use std::env;
707 fn main() {
708 let target = env::var("TARGET").unwrap();
709 println!("cargo:rustc-flags=-L /path/to/{}", target);
710 }
711 "#,
712 ).file(
713 "d2/Cargo.toml",
714 r#"
715 [package]
716 name = "d2"
717 version = "0.0.0"
718 authors = []
719
720 [dependencies.d1]
721 path = "../d1"
722 "#,
723 ).file(
724 "d2/src/lib.rs",
725 "
726 #[allow(unused_extern_crates)]
727 extern crate d1;
728 pub fn d2() { d1::d1(); }
729 ",
730 ).build();
731
732 p.cargo("build -v --target")
733 .arg(&target)
734 .with_stderr_contains(&"[COMPILING] d1 v0.0.0 (CWD/d1)")
735 .with_stderr_contains(
736 "[RUNNING] `rustc [..] d1/build.rs [..] --out-dir CWD/target/debug/build/d1-[..]`",
737 )
738 .with_stderr_contains("[RUNNING] `CWD/target/debug/build/d1-[..]/build-script-build`")
739 .with_stderr_contains("[RUNNING] `rustc [..] d1/src/lib.rs [..]`")
740 .with_stderr_contains("[COMPILING] d2 v0.0.0 (CWD/d2)")
741 .with_stderr_contains(&format!(
742 "[RUNNING] `rustc [..] d2/src/lib.rs [..] -L /path/to/{host}`",
743 host = host
744 )).with_stderr_contains("[COMPILING] foo v0.0.0 (CWD)")
745 .with_stderr_contains(&format!(
746 "[RUNNING] `rustc [..] build.rs [..] --out-dir CWD/target/debug/build/foo-[..] \
747 -L /path/to/{host}`",
748 host = host
749 )).with_stderr_contains(&format!(
750 "\
751 [RUNNING] `rustc [..] src/main.rs [..] --target {target} [..] \
752 -L /path/to/{target}`",
753 target = target
754 )).run();
755 }
756
757 #[test]
758 fn build_deps_for_the_right_arch() {
759 if cross_compile::disabled() {
760 return;
761 }
762
763 let p = project()
764 .file(
765 "Cargo.toml",
766 r#"
767 [package]
768 name = "foo"
769 version = "0.0.0"
770 authors = []
771
772 [dependencies.d2]
773 path = "d2"
774 "#,
775 ).file("src/main.rs", "extern crate d2; fn main() {}")
776 .file("d1/Cargo.toml", &basic_manifest("d1", "0.0.0"))
777 .file("d1/src/lib.rs", "pub fn d1() {}")
778 .file(
779 "d2/Cargo.toml",
780 r#"
781 [package]
782 name = "d2"
783 version = "0.0.0"
784 authors = []
785 build = "build.rs"
786
787 [build-dependencies.d1]
788 path = "../d1"
789 "#,
790 ).file("d2/build.rs", "extern crate d1; fn main() {}")
791 .file("d2/src/lib.rs", "")
792 .build();
793
794 let target = cross_compile::alternate();
795 p.cargo("build -v --target").arg(&target).run();
796 }
797
798 #[test]
799 fn build_script_only_host() {
800 if cross_compile::disabled() {
801 return;
802 }
803
804 let p = project()
805 .file(
806 "Cargo.toml",
807 r#"
808 [package]
809 name = "foo"
810 version = "0.0.0"
811 authors = []
812 build = "build.rs"
813
814 [build-dependencies.d1]
815 path = "d1"
816 "#,
817 ).file("src/main.rs", "fn main() {}")
818 .file("build.rs", "extern crate d1; fn main() {}")
819 .file(
820 "d1/Cargo.toml",
821 r#"
822 [package]
823 name = "d1"
824 version = "0.0.0"
825 authors = []
826 build = "build.rs"
827 "#,
828 ).file("d1/src/lib.rs", "pub fn d1() {}")
829 .file(
830 "d1/build.rs",
831 r#"
832 use std::env;
833
834 fn main() {
835 assert!(env::var("OUT_DIR").unwrap().replace("\\", "/")
836 .contains("target/debug/build/d1-"),
837 "bad: {:?}", env::var("OUT_DIR"));
838 }
839 "#,
840 ).build();
841
842 let target = cross_compile::alternate();
843 p.cargo("build -v --target").arg(&target).run();
844 }
845
846 #[test]
847 fn plugin_build_script_right_arch() {
848 if cross_compile::disabled() {
849 return;
850 }
851 let p = project()
852 .file(
853 "Cargo.toml",
854 r#"
855 [package]
856 name = "foo"
857 version = "0.0.1"
858 authors = []
859 build = "build.rs"
860
861 [lib]
862 name = "foo"
863 plugin = true
864 "#,
865 ).file("build.rs", "fn main() {}")
866 .file("src/lib.rs", "")
867 .build();
868
869 p.cargo("build -v --target")
870 .arg(cross_compile::alternate())
871 .with_stderr(
872 "\
873 [COMPILING] foo v0.0.1 ([..])
874 [RUNNING] `rustc [..] build.rs [..]`
875 [RUNNING] `[..]/build-script-build`
876 [RUNNING] `rustc [..] src/lib.rs [..]`
877 [FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
878 ",
879 ).run();
880 }
881
882 #[test]
883 fn build_script_with_platform_specific_dependencies() {
884 if cross_compile::disabled() {
885 return;
886 }
887
888 let target = cross_compile::alternate();
889 let host = rustc_host();
890 let p = project()
891 .file(
892 "Cargo.toml",
893 r#"
894 [package]
895 name = "foo"
896 version = "0.0.1"
897 authors = []
898 build = "build.rs"
899
900 [build-dependencies.d1]
901 path = "d1"
902 "#,
903 ).file(
904 "build.rs",
905 "
906 #[allow(unused_extern_crates)]
907 extern crate d1;
908 fn main() {}
909 ",
910 ).file("src/lib.rs", "")
911 .file(
912 "d1/Cargo.toml",
913 &format!(
914 r#"
915 [package]
916 name = "d1"
917 version = "0.0.0"
918 authors = []
919
920 [target.{}.dependencies]
921 d2 = {{ path = "../d2" }}
922 "#,
923 host
924 ),
925 ).file(
926 "d1/src/lib.rs",
927 "#[allow(unused_extern_crates)] extern crate d2;",
928 ).file("d2/Cargo.toml", &basic_manifest("d2", "0.0.0"))
929 .file("d2/src/lib.rs", "")
930 .build();
931
932 p.cargo("build -v --target")
933 .arg(&target)
934 .with_stderr(&format!(
935 "\
936 [COMPILING] d2 v0.0.0 ([..])
937 [RUNNING] `rustc [..] d2/src/lib.rs [..]`
938 [COMPILING] d1 v0.0.0 ([..])
939 [RUNNING] `rustc [..] d1/src/lib.rs [..]`
940 [COMPILING] foo v0.0.1 ([..])
941 [RUNNING] `rustc [..] build.rs [..]`
942 [RUNNING] `CWD/target/debug/build/foo-[..]/build-script-build`
943 [RUNNING] `rustc [..] src/lib.rs [..] --target {target} [..]`
944 [FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
945 ",
946 target = target
947 )).run();
948 }
949
950 #[test]
951 fn platform_specific_dependencies_do_not_leak() {
952 if cross_compile::disabled() {
953 return;
954 }
955
956 let target = cross_compile::alternate();
957 let host = rustc_host();
958 let p = project()
959 .file(
960 "Cargo.toml",
961 r#"
962 [package]
963 name = "foo"
964 version = "0.0.1"
965 authors = []
966 build = "build.rs"
967
968 [dependencies.d1]
969 path = "d1"
970
971 [build-dependencies.d1]
972 path = "d1"
973 "#,
974 ).file("build.rs", "extern crate d1; fn main() {}")
975 .file("src/lib.rs", "")
976 .file(
977 "d1/Cargo.toml",
978 &format!(
979 r#"
980 [package]
981 name = "d1"
982 version = "0.0.0"
983 authors = []
984
985 [target.{}.dependencies]
986 d2 = {{ path = "../d2" }}
987 "#,
988 host
989 ),
990 ).file("d1/src/lib.rs", "extern crate d2;")
991 .file("d1/Cargo.toml", &basic_manifest("d1", "0.0.0"))
992 .file("d2/src/lib.rs", "")
993 .build();
994
995 p.cargo("build -v --target")
996 .arg(&target)
997 .with_status(101)
998 .with_stderr_contains("[..] can't find crate for `d2`[..]")
999 .run();
1000 }
1001
1002 #[test]
1003 fn platform_specific_variables_reflected_in_build_scripts() {
1004 if cross_compile::disabled() {
1005 return;
1006 }
1007
1008 let target = cross_compile::alternate();
1009 let host = rustc_host();
1010 let p = project()
1011 .file(
1012 "Cargo.toml",
1013 &format!(
1014 r#"
1015 [package]
1016 name = "foo"
1017 version = "0.0.1"
1018 authors = []
1019 build = "build.rs"
1020
1021 [target.{host}.dependencies]
1022 d1 = {{ path = "d1" }}
1023
1024 [target.{target}.dependencies]
1025 d2 = {{ path = "d2" }}
1026 "#,
1027 host = host,
1028 target = target
1029 ),
1030 ).file(
1031 "build.rs",
1032 &format!(
1033 r#"
1034 use std::env;
1035
1036 fn main() {{
1037 let platform = env::var("TARGET").unwrap();
1038 let (expected, not_expected) = match &platform[..] {{
1039 "{host}" => ("DEP_D1_VAL", "DEP_D2_VAL"),
1040 "{target}" => ("DEP_D2_VAL", "DEP_D1_VAL"),
1041 _ => panic!("unknown platform")
1042 }};
1043
1044 env::var(expected).ok()
1045 .expect(&format!("missing {{}}", expected));
1046 env::var(not_expected).err()
1047 .expect(&format!("found {{}}", not_expected));
1048 }}
1049 "#,
1050 host = host,
1051 target = target
1052 ),
1053 ).file("src/lib.rs", "")
1054 .file(
1055 "d1/Cargo.toml",
1056 r#"
1057 [package]
1058 name = "d1"
1059 version = "0.0.0"
1060 authors = []
1061 links = "d1"
1062 build = "build.rs"
1063 "#,
1064 ).file("d1/build.rs", r#"fn main() { println!("cargo:val=1") }"#)
1065 .file("d1/src/lib.rs", "")
1066 .file(
1067 "d2/Cargo.toml",
1068 r#"
1069 [package]
1070 name = "d2"
1071 version = "0.0.0"
1072 authors = []
1073 links = "d2"
1074 build = "build.rs"
1075 "#,
1076 ).file("d2/build.rs", r#"fn main() { println!("cargo:val=1") }"#)
1077 .file("d2/src/lib.rs", "")
1078 .build();
1079
1080 p.cargo("build -v").run();
1081 p.cargo("build -v --target").arg(&target).run();
1082 }
1083
1084 #[test]
1085 fn cross_test_dylib() {
1086 if cross_compile::disabled() {
1087 return;
1088 }
1089
1090 let target = cross_compile::alternate();
1091
1092 let p = project()
1093 .file(
1094 "Cargo.toml",
1095 r#"
1096 [package]
1097 name = "foo"
1098 version = "0.0.1"
1099 authors = []
1100
1101 [lib]
1102 name = "foo"
1103 crate_type = ["dylib"]
1104
1105 [dependencies.bar]
1106 path = "bar"
1107 "#,
1108 ).file(
1109 "src/lib.rs",
1110 r#"
1111 extern crate bar as the_bar;
1112
1113 pub fn bar() { the_bar::baz(); }
1114
1115 #[test]
1116 fn foo() { bar(); }
1117 "#,
1118 ).file(
1119 "tests/test.rs",
1120 r#"
1121 extern crate foo as the_foo;
1122
1123 #[test]
1124 fn foo() { the_foo::bar(); }
1125 "#,
1126 ).file(
1127 "bar/Cargo.toml",
1128 r#"
1129 [package]
1130 name = "bar"
1131 version = "0.0.1"
1132 authors = []
1133
1134 [lib]
1135 name = "bar"
1136 crate_type = ["dylib"]
1137 "#,
1138 ).file(
1139 "bar/src/lib.rs",
1140 &format!(
1141 r#"
1142 use std::env;
1143 pub fn baz() {{
1144 assert_eq!(env::consts::ARCH, "{}");
1145 }}
1146 "#,
1147 cross_compile::alternate_arch()
1148 ),
1149 ).build();
1150
1151 p.cargo("test --target")
1152 .arg(&target)
1153 .with_stderr(&format!(
1154 "\
1155 [COMPILING] bar v0.0.1 (CWD/bar)
1156 [COMPILING] foo v0.0.1 (CWD)
1157 [FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
1158 [RUNNING] target/{arch}/debug/deps/foo-[..][EXE]
1159 [RUNNING] target/{arch}/debug/deps/test-[..][EXE]",
1160 arch = cross_compile::alternate()
1161 )).with_stdout_contains_n("test foo ... ok", 2)
1162 .run();
1163 }