1 //! Tests for the `cargo rustc` command.
3 use cargo_test_support
::{basic_bin_manifest, basic_lib_manifest, basic_manifest, project}
;
5 const CARGO_RUSTC_ERROR
: &str =
6 "[ERROR] extra arguments to `rustc` can only be passed to one target, consider filtering
7 the package by passing, e.g., `--lib` or `--bin NAME` to specify a single target";
10 fn build_lib_for_foo() {
12 .file("src/main.rs", "fn main() {}")
13 .file("src/lib.rs", r
#" "#)
16 p
.cargo("rustc --lib -v")
19 [COMPILING] foo v0.0.1 ([CWD])
20 [RUNNING] `rustc --crate-name foo src/lib.rs [..]--crate-type lib \
21 --emit=[..]link[..]-C debuginfo=2 \
24 -L dependency=[CWD]/target/debug/deps`
25 [FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
34 .file("src/main.rs", "fn main() {}")
35 .file("src/lib.rs", r
#" "#)
38 p
.cargo("rustc --lib -v -- -C debug-assertions=off")
41 [COMPILING] foo v0.0.1 ([CWD])
42 [RUNNING] `rustc --crate-name foo src/lib.rs [..]--crate-type lib \
43 --emit=[..]link[..]-C debuginfo=2 \
44 -C debug-assertions=off \
47 -L dependency=[CWD]/target/debug/deps`
48 [FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
55 fn build_main_and_allow_unstable_options() {
57 .file("src/main.rs", "fn main() {}")
58 .file("src/lib.rs", r
#" "#)
61 p
.cargo("rustc -v --bin foo -- -C debug-assertions")
64 [COMPILING] {name} v{version} ([CWD])
65 [RUNNING] `rustc --crate-name {name} src/lib.rs [..]--crate-type lib \
66 --emit=[..]link[..]-C debuginfo=2 \
69 -L dependency=[CWD]/target/debug/deps`
70 [RUNNING] `rustc --crate-name {name} src/main.rs [..]--crate-type bin \
71 --emit=[..]link[..]-C debuginfo=2 \
75 -L dependency=[CWD]/target/debug/deps \
76 --extern {name}=[CWD]/target/debug/deps/lib{name}-[..].rlib`
77 [FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
86 fn fails_when_trying_to_build_main_and_lib_with_args() {
88 .file("src/main.rs", "fn main() {}")
89 .file("src/lib.rs", r
#" "#)
92 p
.cargo("rustc -v -- -C debug-assertions")
94 .with_stderr(CARGO_RUSTC_ERROR
)
99 fn build_with_args_to_one_of_multiple_binaries() {
101 .file("src/bin/foo.rs", "fn main() {}")
102 .file("src/bin/bar.rs", "fn main() {}")
103 .file("src/bin/baz.rs", "fn main() {}")
104 .file("src/lib.rs", r
#" "#)
107 p
.cargo("rustc -v --bin bar -- -C debug-assertions")
110 [COMPILING] foo v0.0.1 ([CWD])
111 [RUNNING] `rustc --crate-name foo src/lib.rs [..]--crate-type lib --emit=[..]link[..]\
112 -C debuginfo=2 -C metadata=[..] \
114 [RUNNING] `rustc --crate-name bar src/bin/bar.rs [..]--crate-type bin --emit=[..]link[..]\
115 -C debuginfo=2 -C debug-assertions [..]`
116 [FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
123 fn fails_with_args_to_all_binaries() {
125 .file("src/bin/foo.rs", "fn main() {}")
126 .file("src/bin/bar.rs", "fn main() {}")
127 .file("src/bin/baz.rs", "fn main() {}")
128 .file("src/lib.rs", r
#" "#)
131 p
.cargo("rustc -v -- -C debug-assertions")
133 .with_stderr(CARGO_RUSTC_ERROR
)
138 fn fails_with_crate_type_and_without_unstable_options() {
139 let p
= project().file("src/lib.rs", r
#" "#).build();
141 p
.cargo("rustc --crate-type lib")
142 .masquerade_as_nightly_cargo()
145 "[ERROR] the `crate-type` flag is unstable, pass `-Z unstable-options` to enable it
146 See https://github.com/rust-lang/cargo/issues/10083 for more information about the `crate-type` flag.",
152 fn fails_with_crate_type_to_multi_binaries() {
154 .file("src/bin/foo.rs", "fn main() {}")
155 .file("src/bin/bar.rs", "fn main() {}")
156 .file("src/bin/baz.rs", "fn main() {}")
157 .file("src/lib.rs", r
#" "#)
160 p
.cargo("rustc --crate-type lib -Zunstable-options")
161 .masquerade_as_nightly_cargo()
164 "[ERROR] crate types to rustc can only be passed to one target, consider filtering
165 the package by passing, e.g., `--lib` or `--example` to specify a single target",
171 fn fails_with_crate_type_to_multi_examples() {
183 crate-type = ["rlib"]
186 crate-type = ["rlib"]
189 .file("src/lib.rs", "")
190 .file("examples/ex1.rs", "")
191 .file("examples/ex2.rs", "")
194 p
.cargo("rustc -v --example ex1 --example ex2 --crate-type lib,cdylib -Zunstable-options")
195 .masquerade_as_nightly_cargo()
198 "[ERROR] crate types to rustc can only be passed to one target, consider filtering
199 the package by passing, e.g., `--lib` or `--example` to specify a single target",
205 fn fails_with_crate_type_to_binary() {
206 let p
= project().file("src/bin/foo.rs", "fn main() {}").build();
208 p
.cargo("rustc --crate-type lib -Zunstable-options")
209 .masquerade_as_nightly_cargo()
212 "[ERROR] crate types can only be specified for libraries and example libraries.
213 Binaries, tests, and benchmarks are always the `bin` crate type",
219 fn build_with_crate_type_for_foo() {
220 let p
= project().file("src/lib.rs", "").build();
222 p
.cargo("rustc -v --crate-type cdylib -Zunstable-options")
223 .masquerade_as_nightly_cargo()
226 [COMPILING] foo v0.0.1 ([CWD])
227 [RUNNING] `rustc --crate-name foo src/lib.rs [..]--crate-type cdylib [..]
228 [FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
235 fn build_with_crate_type_for_foo_with_deps() {
241 pub fn foo() { a::hello(); }
256 .file("a/Cargo.toml", &basic_manifest("a", "0.1.0"))
257 .file("a/src/lib.rs", "pub fn hello() {}")
260 p
.cargo("rustc -v --crate-type cdylib -Zunstable-options")
261 .masquerade_as_nightly_cargo()
264 [COMPILING] a v0.1.0 ([CWD]/a)
265 [RUNNING] `rustc --crate-name a a/src/lib.rs [..]--crate-type lib [..]
266 [COMPILING] foo v0.0.1 ([CWD])
267 [RUNNING] `rustc --crate-name foo src/lib.rs [..]--crate-type cdylib [..]
268 [FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
275 fn build_with_crate_types_for_foo() {
276 let p
= project().file("src/lib.rs", "").build();
278 p
.cargo("rustc -v --crate-type lib,cdylib -Zunstable-options")
279 .masquerade_as_nightly_cargo()
282 [COMPILING] foo v0.0.1 ([CWD])
283 [RUNNING] `rustc --crate-name foo src/lib.rs [..]--crate-type lib,cdylib [..]
284 [FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
291 fn build_with_crate_type_to_example() {
303 crate-type = ["rlib"]
306 .file("src/lib.rs", "")
307 .file("examples/ex.rs", "")
310 p
.cargo("rustc -v --example ex --crate-type cdylib -Zunstable-options")
311 .masquerade_as_nightly_cargo()
314 [COMPILING] foo v0.0.1 ([CWD])
315 [RUNNING] `rustc --crate-name foo src/lib.rs [..]--crate-type lib [..]
316 [RUNNING] `rustc --crate-name ex examples/ex.rs [..]--crate-type cdylib [..]
317 [FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
324 fn build_with_crate_types_to_example() {
336 crate-type = ["rlib"]
339 .file("src/lib.rs", "")
340 .file("examples/ex.rs", "")
343 p
.cargo("rustc -v --example ex --crate-type lib,cdylib -Zunstable-options")
344 .masquerade_as_nightly_cargo()
347 [COMPILING] foo v0.0.1 ([CWD])
348 [RUNNING] `rustc --crate-name foo src/lib.rs [..]--crate-type lib [..]
349 [RUNNING] `rustc --crate-name ex examples/ex.rs [..]--crate-type lib,cdylib [..]
350 [FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
357 fn build_with_crate_types_to_one_of_multi_examples() {
369 crate-type = ["rlib"]
372 crate-type = ["rlib"]
375 .file("src/lib.rs", "")
376 .file("examples/ex1.rs", "")
377 .file("examples/ex2.rs", "")
380 p
.cargo("rustc -v --example ex1 --crate-type lib,cdylib -Zunstable-options")
381 .masquerade_as_nightly_cargo()
384 [COMPILING] foo v0.0.1 ([CWD])
385 [RUNNING] `rustc --crate-name foo src/lib.rs [..]--crate-type lib [..]
386 [RUNNING] `rustc --crate-name ex1 examples/ex1.rs [..]--crate-type lib,cdylib [..]
387 [FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
394 fn build_with_args_to_one_of_multiple_tests() {
396 .file("tests/foo.rs", r
#" "#)
397 .file("tests/bar.rs", r
#" "#)
398 .file("tests/baz.rs", r
#" "#)
399 .file("src/lib.rs", r
#" "#)
402 p
.cargo("rustc -v --test bar -- -C debug-assertions")
405 [COMPILING] foo v0.0.1 ([CWD])
406 [RUNNING] `rustc --crate-name foo src/lib.rs [..]--crate-type lib --emit=[..]link[..]\
407 -C debuginfo=2 -C metadata=[..] \
409 [RUNNING] `rustc --crate-name bar tests/bar.rs [..]--emit=[..]link[..]-C debuginfo=2 \
410 -C debug-assertions [..]--test[..]`
411 [FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
418 fn build_foo_with_bar_dependency() {
432 .file("src/main.rs", "extern crate bar; fn main() { bar::baz() }")
436 .file("Cargo.toml", &basic_manifest("bar", "0.1.0"))
437 .file("src/lib.rs", "pub fn baz() {}")
440 foo
.cargo("rustc -v -- -C debug-assertions")
443 [COMPILING] bar v0.1.0 ([..])
444 [RUNNING] `[..] -C debuginfo=2 [..]`
445 [COMPILING] foo v0.0.1 ([CWD])
446 [RUNNING] `[..] -C debuginfo=2 -C debug-assertions [..]`
447 [FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
454 fn build_only_bar_dependency() {
468 .file("src/main.rs", "extern crate bar; fn main() { bar::baz() }")
472 .file("Cargo.toml", &basic_manifest("bar", "0.1.0"))
473 .file("src/lib.rs", "pub fn baz() {}")
476 foo
.cargo("rustc -v -p bar -- -C debug-assertions")
479 [COMPILING] bar v0.1.0 ([..])
480 [RUNNING] `rustc --crate-name bar [..]--crate-type lib [..] -C debug-assertions [..]`
481 [FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
488 fn targets_selected_default() {
489 let p
= project().file("src/main.rs", "fn main() {}").build();
492 .with_stderr_contains(
493 "[RUNNING] `rustc --crate-name foo src/main.rs [..]--crate-type bin \
494 --emit=[..]link[..]",
497 .with_stderr_does_not_contain(
498 "[RUNNING] `rustc --crate-name foo src/main.rs [..]--emit=[..]link \
499 -C opt-level=3 --test [..]",
502 .with_stderr_does_not_contain(
503 "[RUNNING] `rustc --crate-name foo src/main.rs [..]--emit=[..]link \
504 -C debuginfo=2 --test [..]",
510 fn targets_selected_all() {
511 let p
= project().file("src/main.rs", "fn main() {}").build();
512 p
.cargo("rustc -v --all-targets")
514 .with_stderr_contains(
515 "[RUNNING] `rustc --crate-name foo src/main.rs [..]--crate-type bin \
516 --emit=[..]link[..]",
519 .with_stderr_contains(
520 "[RUNNING] `rustc --crate-name foo src/main.rs [..]--emit=[..]link[..]\
521 -C debuginfo=2 --test [..]",
527 fn fail_with_multiple_packages() {
544 .file("src/main.rs", "fn main() {}")
549 .file("Cargo.toml", &basic_manifest("bar", "0.1.0"))
554 if cfg!(flag = "1") { println!("Yeah from bar!"); }
562 .file("Cargo.toml", &basic_manifest("baz", "0.1.0"))
567 if cfg!(flag = "1") { println!("Yeah from baz!"); }
573 foo
.cargo("rustc -v -p bar -p baz")
575 .with_stderr_contains(
577 error: The argument '--package [<SPEC>...]' was provided more than once, \
578 but cannot be used multiple times
585 fn fail_with_glob() {
594 .file("bar/Cargo.toml", &basic_manifest("bar", "0.1.0"))
595 .file("bar/src/lib.rs", "pub fn bar() { break_the_build(); }")
598 p
.cargo("rustc -p '*z'")
600 .with_stderr("[ERROR] Glob patterns on package selection are not supported.")
605 fn rustc_with_other_profile() {
622 #[cfg(test)] extern crate a;
628 .file("a/Cargo.toml", &basic_manifest("a", "0.1.0"))
629 .file("a/src/lib.rs", "")
632 p
.cargo("rustc --profile test").run();
636 fn rustc_fingerprint() {
637 // Verify that the fingerprint includes the rustc args.
639 .file("Cargo.toml", &basic_lib_manifest("foo"))
640 .file("src/lib.rs", "")
643 p
.cargo("rustc -v -- -C debug-assertions")
647 [RUNNING] `rustc [..]-C debug-assertions [..]
653 p
.cargo("rustc -v -- -C debug-assertions")
663 .with_stderr_does_not_contain("-C debug-assertions")
667 [RUNNING] `rustc [..]
684 fn rustc_test_with_implicit_bin() {
686 .file("Cargo.toml", &basic_bin_manifest("foo"))
691 fn f() { compile_fail!("Foo shouldn't be set."); }
699 fn f() { compile_fail!("Foo should be set."); }
704 p
.cargo("rustc --test test1 -v -- --cfg foo")
705 .with_stderr_contains(
707 [RUNNING] `rustc --crate-name test1 tests/test1.rs [..] --cfg foo [..]
710 .with_stderr_contains(
712 [RUNNING] `rustc --crate-name foo src/main.rs [..]
719 fn rustc_with_print_cfg_single_target() {
721 .file("Cargo.toml", &basic_bin_manifest("foo"))
722 .file("src/main.rs", r
#"fn main() {} "#)
725 p
.cargo("rustc -Z unstable-options --target x86_64-pc-windows-msvc --print cfg")
726 .masquerade_as_nightly_cargo()
727 .with_stdout_contains("debug_assertions")
728 .with_stdout_contains("target_arch=\"x86_64\"")
729 .with_stdout_contains("target_endian=\"little\"")
730 .with_stdout_contains("target_env=\"msvc\"")
731 .with_stdout_contains("target_family=\"windows\"")
732 .with_stdout_contains("target_os=\"windows\"")
733 .with_stdout_contains("target_pointer_width=\"64\"")
734 .with_stdout_contains("target_vendor=\"pc\"")
735 .with_stdout_contains("windows")
740 fn rustc_with_print_cfg_multiple_targets() {
742 .file("Cargo.toml", &basic_bin_manifest("foo"))
743 .file("src/main.rs", r
#"fn main() {} "#)
746 p
.cargo("rustc -Z unstable-options -Z multitarget --target x86_64-pc-windows-msvc --target i686-unknown-linux-gnu --print cfg")
747 .masquerade_as_nightly_cargo()
748 .with_stdout_contains("debug_assertions")
749 .with_stdout_contains("target_arch=\"x86_64\"")
750 .with_stdout_contains("target_endian=\"little\"")
751 .with_stdout_contains("target_env=\"msvc\"")
752 .with_stdout_contains("target_family=\"windows\"")
753 .with_stdout_contains("target_os=\"windows\"")
754 .with_stdout_contains("target_pointer_width=\"64\"")
755 .with_stdout_contains("target_vendor=\"pc\"")
756 .with_stdout_contains("windows")
757 .with_stdout_contains("target_env=\"gnu\"")
758 .with_stdout_contains("target_family=\"unix\"")
759 .with_stdout_contains("target_pointer_width=\"32\"")
760 .with_stdout_contains("target_vendor=\"unknown\"")
761 .with_stdout_contains("target_os=\"linux\"")
762 .with_stdout_contains("unix")
767 fn rustc_with_print_cfg_rustflags_env_var() {
769 .file("Cargo.toml", &basic_bin_manifest("foo"))
770 .file("src/main.rs", r
#"fn main() {} "#)
773 p
.cargo("rustc -Z unstable-options --target x86_64-pc-windows-msvc --print cfg")
774 .masquerade_as_nightly_cargo()
775 .env("RUSTFLAGS", "-C target-feature=+crt-static")
776 .with_stdout_contains("debug_assertions")
777 .with_stdout_contains("target_arch=\"x86_64\"")
778 .with_stdout_contains("target_endian=\"little\"")
779 .with_stdout_contains("target_env=\"msvc\"")
780 .with_stdout_contains("target_family=\"windows\"")
781 .with_stdout_contains("target_feature=\"crt-static\"")
782 .with_stdout_contains("target_os=\"windows\"")
783 .with_stdout_contains("target_pointer_width=\"64\"")
784 .with_stdout_contains("target_vendor=\"pc\"")
785 .with_stdout_contains("windows")
790 fn rustc_with_print_cfg_config_toml() {
792 .file("Cargo.toml", &basic_bin_manifest("foo"))
794 ".cargo/config.toml",
796 [target.x86_64-pc-windows-msvc]
797 rustflags = ["-C", "target-feature=+crt-static"]
800 .file("src/main.rs", r
#"fn main() {} "#)
803 p
.cargo("rustc -Z unstable-options --target x86_64-pc-windows-msvc --print cfg")
804 .masquerade_as_nightly_cargo()
805 .env("RUSTFLAGS", "-C target-feature=+crt-static")
806 .with_stdout_contains("debug_assertions")
807 .with_stdout_contains("target_arch=\"x86_64\"")
808 .with_stdout_contains("target_endian=\"little\"")
809 .with_stdout_contains("target_env=\"msvc\"")
810 .with_stdout_contains("target_family=\"windows\"")
811 .with_stdout_contains("target_feature=\"crt-static\"")
812 .with_stdout_contains("target_os=\"windows\"")
813 .with_stdout_contains("target_pointer_width=\"64\"")
814 .with_stdout_contains("target_vendor=\"pc\"")
815 .with_stdout_contains("windows")