/tests/testsuite/rustc.rs

https://gitlab.com/frewsxcv/cargo · Rust · 817 lines · 745 code · 65 blank · 7 comment · 0 complexity · ce6b14f794de18a4db778a0c3b6c969d MD5 · raw file

  1. //! Tests for the `cargo rustc` command.
  2. use cargo_test_support::{basic_bin_manifest, basic_lib_manifest, basic_manifest, project};
  3. const CARGO_RUSTC_ERROR: &str =
  4. "[ERROR] extra arguments to `rustc` can only be passed to one target, consider filtering
  5. the package by passing, e.g., `--lib` or `--bin NAME` to specify a single target";
  6. #[cargo_test]
  7. fn build_lib_for_foo() {
  8. let p = project()
  9. .file("src/main.rs", "fn main() {}")
  10. .file("src/lib.rs", r#" "#)
  11. .build();
  12. p.cargo("rustc --lib -v")
  13. .with_stderr(
  14. "\
  15. [COMPILING] foo v0.0.1 ([CWD])
  16. [RUNNING] `rustc --crate-name foo src/lib.rs [..]--crate-type lib \
  17. --emit=[..]link[..]-C debuginfo=2 \
  18. -C metadata=[..] \
  19. --out-dir [..] \
  20. -L dependency=[CWD]/target/debug/deps`
  21. [FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
  22. ",
  23. )
  24. .run();
  25. }
  26. #[cargo_test]
  27. fn lib() {
  28. let p = project()
  29. .file("src/main.rs", "fn main() {}")
  30. .file("src/lib.rs", r#" "#)
  31. .build();
  32. p.cargo("rustc --lib -v -- -C debug-assertions=off")
  33. .with_stderr(
  34. "\
  35. [COMPILING] foo v0.0.1 ([CWD])
  36. [RUNNING] `rustc --crate-name foo src/lib.rs [..]--crate-type lib \
  37. --emit=[..]link[..]-C debuginfo=2 \
  38. -C debug-assertions=off \
  39. -C metadata=[..] \
  40. --out-dir [..] \
  41. -L dependency=[CWD]/target/debug/deps`
  42. [FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
  43. ",
  44. )
  45. .run();
  46. }
  47. #[cargo_test]
  48. fn build_main_and_allow_unstable_options() {
  49. let p = project()
  50. .file("src/main.rs", "fn main() {}")
  51. .file("src/lib.rs", r#" "#)
  52. .build();
  53. p.cargo("rustc -v --bin foo -- -C debug-assertions")
  54. .with_stderr(format!(
  55. "\
  56. [COMPILING] {name} v{version} ([CWD])
  57. [RUNNING] `rustc --crate-name {name} src/lib.rs [..]--crate-type lib \
  58. --emit=[..]link[..]-C debuginfo=2 \
  59. -C metadata=[..] \
  60. --out-dir [..] \
  61. -L dependency=[CWD]/target/debug/deps`
  62. [RUNNING] `rustc --crate-name {name} src/main.rs [..]--crate-type bin \
  63. --emit=[..]link[..]-C debuginfo=2 \
  64. -C debug-assertions \
  65. -C metadata=[..] \
  66. --out-dir [..] \
  67. -L dependency=[CWD]/target/debug/deps \
  68. --extern {name}=[CWD]/target/debug/deps/lib{name}-[..].rlib`
  69. [FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
  70. ",
  71. name = "foo",
  72. version = "0.0.1"
  73. ))
  74. .run();
  75. }
  76. #[cargo_test]
  77. fn fails_when_trying_to_build_main_and_lib_with_args() {
  78. let p = project()
  79. .file("src/main.rs", "fn main() {}")
  80. .file("src/lib.rs", r#" "#)
  81. .build();
  82. p.cargo("rustc -v -- -C debug-assertions")
  83. .with_status(101)
  84. .with_stderr(CARGO_RUSTC_ERROR)
  85. .run();
  86. }
  87. #[cargo_test]
  88. fn build_with_args_to_one_of_multiple_binaries() {
  89. let p = project()
  90. .file("src/bin/foo.rs", "fn main() {}")
  91. .file("src/bin/bar.rs", "fn main() {}")
  92. .file("src/bin/baz.rs", "fn main() {}")
  93. .file("src/lib.rs", r#" "#)
  94. .build();
  95. p.cargo("rustc -v --bin bar -- -C debug-assertions")
  96. .with_stderr(
  97. "\
  98. [COMPILING] foo v0.0.1 ([CWD])
  99. [RUNNING] `rustc --crate-name foo src/lib.rs [..]--crate-type lib --emit=[..]link[..]\
  100. -C debuginfo=2 -C metadata=[..] \
  101. --out-dir [..]`
  102. [RUNNING] `rustc --crate-name bar src/bin/bar.rs [..]--crate-type bin --emit=[..]link[..]\
  103. -C debuginfo=2 -C debug-assertions [..]`
  104. [FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
  105. ",
  106. )
  107. .run();
  108. }
  109. #[cargo_test]
  110. fn fails_with_args_to_all_binaries() {
  111. let p = project()
  112. .file("src/bin/foo.rs", "fn main() {}")
  113. .file("src/bin/bar.rs", "fn main() {}")
  114. .file("src/bin/baz.rs", "fn main() {}")
  115. .file("src/lib.rs", r#" "#)
  116. .build();
  117. p.cargo("rustc -v -- -C debug-assertions")
  118. .with_status(101)
  119. .with_stderr(CARGO_RUSTC_ERROR)
  120. .run();
  121. }
  122. #[cargo_test]
  123. fn fails_with_crate_type_and_without_unstable_options() {
  124. let p = project().file("src/lib.rs", r#" "#).build();
  125. p.cargo("rustc --crate-type lib")
  126. .masquerade_as_nightly_cargo()
  127. .with_status(101)
  128. .with_stderr(
  129. "[ERROR] the `crate-type` flag is unstable, pass `-Z unstable-options` to enable it
  130. See https://github.com/rust-lang/cargo/issues/10083 for more information about the `crate-type` flag.",
  131. )
  132. .run();
  133. }
  134. #[cargo_test]
  135. fn fails_with_crate_type_to_multi_binaries() {
  136. let p = project()
  137. .file("src/bin/foo.rs", "fn main() {}")
  138. .file("src/bin/bar.rs", "fn main() {}")
  139. .file("src/bin/baz.rs", "fn main() {}")
  140. .file("src/lib.rs", r#" "#)
  141. .build();
  142. p.cargo("rustc --crate-type lib -Zunstable-options")
  143. .masquerade_as_nightly_cargo()
  144. .with_status(101)
  145. .with_stderr(
  146. "[ERROR] crate types to rustc can only be passed to one target, consider filtering
  147. the package by passing, e.g., `--lib` or `--example` to specify a single target",
  148. )
  149. .run();
  150. }
  151. #[cargo_test]
  152. fn fails_with_crate_type_to_multi_examples() {
  153. let p = project()
  154. .file(
  155. "Cargo.toml",
  156. r#"
  157. [package]
  158. name = "foo"
  159. version = "0.0.1"
  160. authors = []
  161. [[example]]
  162. name = "ex1"
  163. crate-type = ["rlib"]
  164. [[example]]
  165. name = "ex2"
  166. crate-type = ["rlib"]
  167. "#,
  168. )
  169. .file("src/lib.rs", "")
  170. .file("examples/ex1.rs", "")
  171. .file("examples/ex2.rs", "")
  172. .build();
  173. p.cargo("rustc -v --example ex1 --example ex2 --crate-type lib,cdylib -Zunstable-options")
  174. .masquerade_as_nightly_cargo()
  175. .with_status(101)
  176. .with_stderr(
  177. "[ERROR] crate types to rustc can only be passed to one target, consider filtering
  178. the package by passing, e.g., `--lib` or `--example` to specify a single target",
  179. )
  180. .run();
  181. }
  182. #[cargo_test]
  183. fn fails_with_crate_type_to_binary() {
  184. let p = project().file("src/bin/foo.rs", "fn main() {}").build();
  185. p.cargo("rustc --crate-type lib -Zunstable-options")
  186. .masquerade_as_nightly_cargo()
  187. .with_status(101)
  188. .with_stderr(
  189. "[ERROR] crate types can only be specified for libraries and example libraries.
  190. Binaries, tests, and benchmarks are always the `bin` crate type",
  191. )
  192. .run();
  193. }
  194. #[cargo_test]
  195. fn build_with_crate_type_for_foo() {
  196. let p = project().file("src/lib.rs", "").build();
  197. p.cargo("rustc -v --crate-type cdylib -Zunstable-options")
  198. .masquerade_as_nightly_cargo()
  199. .with_stderr(
  200. "\
  201. [COMPILING] foo v0.0.1 ([CWD])
  202. [RUNNING] `rustc --crate-name foo src/lib.rs [..]--crate-type cdylib [..]
  203. [FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
  204. ",
  205. )
  206. .run();
  207. }
  208. #[cargo_test]
  209. fn build_with_crate_type_for_foo_with_deps() {
  210. let p = project()
  211. .file(
  212. "src/lib.rs",
  213. r#"
  214. extern crate a;
  215. pub fn foo() { a::hello(); }
  216. "#,
  217. )
  218. .file(
  219. "Cargo.toml",
  220. r#"
  221. [package]
  222. name = "foo"
  223. version = "0.0.1"
  224. authors = []
  225. [dependencies]
  226. a = { path = "a" }
  227. "#,
  228. )
  229. .file("a/Cargo.toml", &basic_manifest("a", "0.1.0"))
  230. .file("a/src/lib.rs", "pub fn hello() {}")
  231. .build();
  232. p.cargo("rustc -v --crate-type cdylib -Zunstable-options")
  233. .masquerade_as_nightly_cargo()
  234. .with_stderr(
  235. "\
  236. [COMPILING] a v0.1.0 ([CWD]/a)
  237. [RUNNING] `rustc --crate-name a a/src/lib.rs [..]--crate-type lib [..]
  238. [COMPILING] foo v0.0.1 ([CWD])
  239. [RUNNING] `rustc --crate-name foo src/lib.rs [..]--crate-type cdylib [..]
  240. [FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
  241. ",
  242. )
  243. .run();
  244. }
  245. #[cargo_test]
  246. fn build_with_crate_types_for_foo() {
  247. let p = project().file("src/lib.rs", "").build();
  248. p.cargo("rustc -v --crate-type lib,cdylib -Zunstable-options")
  249. .masquerade_as_nightly_cargo()
  250. .with_stderr(
  251. "\
  252. [COMPILING] foo v0.0.1 ([CWD])
  253. [RUNNING] `rustc --crate-name foo src/lib.rs [..]--crate-type lib,cdylib [..]
  254. [FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
  255. ",
  256. )
  257. .run();
  258. }
  259. #[cargo_test]
  260. fn build_with_crate_type_to_example() {
  261. let p = project()
  262. .file(
  263. "Cargo.toml",
  264. r#"
  265. [package]
  266. name = "foo"
  267. version = "0.0.1"
  268. authors = []
  269. [[example]]
  270. name = "ex"
  271. crate-type = ["rlib"]
  272. "#,
  273. )
  274. .file("src/lib.rs", "")
  275. .file("examples/ex.rs", "")
  276. .build();
  277. p.cargo("rustc -v --example ex --crate-type cdylib -Zunstable-options")
  278. .masquerade_as_nightly_cargo()
  279. .with_stderr(
  280. "\
  281. [COMPILING] foo v0.0.1 ([CWD])
  282. [RUNNING] `rustc --crate-name foo src/lib.rs [..]--crate-type lib [..]
  283. [RUNNING] `rustc --crate-name ex examples/ex.rs [..]--crate-type cdylib [..]
  284. [FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
  285. ",
  286. )
  287. .run();
  288. }
  289. #[cargo_test]
  290. fn build_with_crate_types_to_example() {
  291. let p = project()
  292. .file(
  293. "Cargo.toml",
  294. r#"
  295. [package]
  296. name = "foo"
  297. version = "0.0.1"
  298. authors = []
  299. [[example]]
  300. name = "ex"
  301. crate-type = ["rlib"]
  302. "#,
  303. )
  304. .file("src/lib.rs", "")
  305. .file("examples/ex.rs", "")
  306. .build();
  307. p.cargo("rustc -v --example ex --crate-type lib,cdylib -Zunstable-options")
  308. .masquerade_as_nightly_cargo()
  309. .with_stderr(
  310. "\
  311. [COMPILING] foo v0.0.1 ([CWD])
  312. [RUNNING] `rustc --crate-name foo src/lib.rs [..]--crate-type lib [..]
  313. [RUNNING] `rustc --crate-name ex examples/ex.rs [..]--crate-type lib,cdylib [..]
  314. [FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
  315. ",
  316. )
  317. .run();
  318. }
  319. #[cargo_test]
  320. fn build_with_crate_types_to_one_of_multi_examples() {
  321. let p = project()
  322. .file(
  323. "Cargo.toml",
  324. r#"
  325. [package]
  326. name = "foo"
  327. version = "0.0.1"
  328. authors = []
  329. [[example]]
  330. name = "ex1"
  331. crate-type = ["rlib"]
  332. [[example]]
  333. name = "ex2"
  334. crate-type = ["rlib"]
  335. "#,
  336. )
  337. .file("src/lib.rs", "")
  338. .file("examples/ex1.rs", "")
  339. .file("examples/ex2.rs", "")
  340. .build();
  341. p.cargo("rustc -v --example ex1 --crate-type lib,cdylib -Zunstable-options")
  342. .masquerade_as_nightly_cargo()
  343. .with_stderr(
  344. "\
  345. [COMPILING] foo v0.0.1 ([CWD])
  346. [RUNNING] `rustc --crate-name foo src/lib.rs [..]--crate-type lib [..]
  347. [RUNNING] `rustc --crate-name ex1 examples/ex1.rs [..]--crate-type lib,cdylib [..]
  348. [FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
  349. ",
  350. )
  351. .run();
  352. }
  353. #[cargo_test]
  354. fn build_with_args_to_one_of_multiple_tests() {
  355. let p = project()
  356. .file("tests/foo.rs", r#" "#)
  357. .file("tests/bar.rs", r#" "#)
  358. .file("tests/baz.rs", r#" "#)
  359. .file("src/lib.rs", r#" "#)
  360. .build();
  361. p.cargo("rustc -v --test bar -- -C debug-assertions")
  362. .with_stderr(
  363. "\
  364. [COMPILING] foo v0.0.1 ([CWD])
  365. [RUNNING] `rustc --crate-name foo src/lib.rs [..]--crate-type lib --emit=[..]link[..]\
  366. -C debuginfo=2 -C metadata=[..] \
  367. --out-dir [..]`
  368. [RUNNING] `rustc --crate-name bar tests/bar.rs [..]--emit=[..]link[..]-C debuginfo=2 \
  369. -C debug-assertions [..]--test[..]`
  370. [FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
  371. ",
  372. )
  373. .run();
  374. }
  375. #[cargo_test]
  376. fn build_foo_with_bar_dependency() {
  377. let foo = project()
  378. .file(
  379. "Cargo.toml",
  380. r#"
  381. [package]
  382. name = "foo"
  383. version = "0.0.1"
  384. authors = []
  385. [dependencies.bar]
  386. path = "../bar"
  387. "#,
  388. )
  389. .file("src/main.rs", "extern crate bar; fn main() { bar::baz() }")
  390. .build();
  391. let _bar = project()
  392. .at("bar")
  393. .file("Cargo.toml", &basic_manifest("bar", "0.1.0"))
  394. .file("src/lib.rs", "pub fn baz() {}")
  395. .build();
  396. foo.cargo("rustc -v -- -C debug-assertions")
  397. .with_stderr(
  398. "\
  399. [COMPILING] bar v0.1.0 ([..])
  400. [RUNNING] `[..] -C debuginfo=2 [..]`
  401. [COMPILING] foo v0.0.1 ([CWD])
  402. [RUNNING] `[..] -C debuginfo=2 -C debug-assertions [..]`
  403. [FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
  404. ",
  405. )
  406. .run();
  407. }
  408. #[cargo_test]
  409. fn build_only_bar_dependency() {
  410. let foo = project()
  411. .file(
  412. "Cargo.toml",
  413. r#"
  414. [package]
  415. name = "foo"
  416. version = "0.0.1"
  417. authors = []
  418. [dependencies.bar]
  419. path = "../bar"
  420. "#,
  421. )
  422. .file("src/main.rs", "extern crate bar; fn main() { bar::baz() }")
  423. .build();
  424. let _bar = project()
  425. .at("bar")
  426. .file("Cargo.toml", &basic_manifest("bar", "0.1.0"))
  427. .file("src/lib.rs", "pub fn baz() {}")
  428. .build();
  429. foo.cargo("rustc -v -p bar -- -C debug-assertions")
  430. .with_stderr(
  431. "\
  432. [COMPILING] bar v0.1.0 ([..])
  433. [RUNNING] `rustc --crate-name bar [..]--crate-type lib [..] -C debug-assertions [..]`
  434. [FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
  435. ",
  436. )
  437. .run();
  438. }
  439. #[cargo_test]
  440. fn targets_selected_default() {
  441. let p = project().file("src/main.rs", "fn main() {}").build();
  442. p.cargo("rustc -v")
  443. // bin
  444. .with_stderr_contains(
  445. "[RUNNING] `rustc --crate-name foo src/main.rs [..]--crate-type bin \
  446. --emit=[..]link[..]",
  447. )
  448. // bench
  449. .with_stderr_does_not_contain(
  450. "[RUNNING] `rustc --crate-name foo src/main.rs [..]--emit=[..]link \
  451. -C opt-level=3 --test [..]",
  452. )
  453. // unit test
  454. .with_stderr_does_not_contain(
  455. "[RUNNING] `rustc --crate-name foo src/main.rs [..]--emit=[..]link \
  456. -C debuginfo=2 --test [..]",
  457. )
  458. .run();
  459. }
  460. #[cargo_test]
  461. fn targets_selected_all() {
  462. let p = project().file("src/main.rs", "fn main() {}").build();
  463. p.cargo("rustc -v --all-targets")
  464. // bin
  465. .with_stderr_contains(
  466. "[RUNNING] `rustc --crate-name foo src/main.rs [..]--crate-type bin \
  467. --emit=[..]link[..]",
  468. )
  469. // unit test
  470. .with_stderr_contains(
  471. "[RUNNING] `rustc --crate-name foo src/main.rs [..]--emit=[..]link[..]\
  472. -C debuginfo=2 --test [..]",
  473. )
  474. .run();
  475. }
  476. #[cargo_test]
  477. fn fail_with_multiple_packages() {
  478. let foo = project()
  479. .file(
  480. "Cargo.toml",
  481. r#"
  482. [package]
  483. name = "foo"
  484. version = "0.0.1"
  485. authors = []
  486. [dependencies.bar]
  487. path = "../bar"
  488. [dependencies.baz]
  489. path = "../baz"
  490. "#,
  491. )
  492. .file("src/main.rs", "fn main() {}")
  493. .build();
  494. let _bar = project()
  495. .at("bar")
  496. .file("Cargo.toml", &basic_manifest("bar", "0.1.0"))
  497. .file(
  498. "src/main.rs",
  499. r#"
  500. fn main() {
  501. if cfg!(flag = "1") { println!("Yeah from bar!"); }
  502. }
  503. "#,
  504. )
  505. .build();
  506. let _baz = project()
  507. .at("baz")
  508. .file("Cargo.toml", &basic_manifest("baz", "0.1.0"))
  509. .file(
  510. "src/main.rs",
  511. r#"
  512. fn main() {
  513. if cfg!(flag = "1") { println!("Yeah from baz!"); }
  514. }
  515. "#,
  516. )
  517. .build();
  518. foo.cargo("rustc -v -p bar -p baz")
  519. .with_status(1)
  520. .with_stderr_contains(
  521. "\
  522. error: The argument '--package [<SPEC>...]' was provided more than once, \
  523. but cannot be used multiple times
  524. ",
  525. )
  526. .run();
  527. }
  528. #[cargo_test]
  529. fn fail_with_glob() {
  530. let p = project()
  531. .file(
  532. "Cargo.toml",
  533. r#"
  534. [workspace]
  535. members = ["bar"]
  536. "#,
  537. )
  538. .file("bar/Cargo.toml", &basic_manifest("bar", "0.1.0"))
  539. .file("bar/src/lib.rs", "pub fn bar() { break_the_build(); }")
  540. .build();
  541. p.cargo("rustc -p '*z'")
  542. .with_status(101)
  543. .with_stderr("[ERROR] Glob patterns on package selection are not supported.")
  544. .run();
  545. }
  546. #[cargo_test]
  547. fn rustc_with_other_profile() {
  548. let p = project()
  549. .file(
  550. "Cargo.toml",
  551. r#"
  552. [package]
  553. name = "foo"
  554. version = "0.0.1"
  555. authors = []
  556. [dev-dependencies]
  557. a = { path = "a" }
  558. "#,
  559. )
  560. .file(
  561. "src/main.rs",
  562. r#"
  563. #[cfg(test)] extern crate a;
  564. #[test]
  565. fn foo() {}
  566. "#,
  567. )
  568. .file("a/Cargo.toml", &basic_manifest("a", "0.1.0"))
  569. .file("a/src/lib.rs", "")
  570. .build();
  571. p.cargo("rustc --profile test").run();
  572. }
  573. #[cargo_test]
  574. fn rustc_fingerprint() {
  575. // Verify that the fingerprint includes the rustc args.
  576. let p = project()
  577. .file("Cargo.toml", &basic_lib_manifest("foo"))
  578. .file("src/lib.rs", "")
  579. .build();
  580. p.cargo("rustc -v -- -C debug-assertions")
  581. .with_stderr(
  582. "\
  583. [COMPILING] foo [..]
  584. [RUNNING] `rustc [..]-C debug-assertions [..]
  585. [FINISHED] [..]
  586. ",
  587. )
  588. .run();
  589. p.cargo("rustc -v -- -C debug-assertions")
  590. .with_stderr(
  591. "\
  592. [FRESH] foo [..]
  593. [FINISHED] [..]
  594. ",
  595. )
  596. .run();
  597. p.cargo("rustc -v")
  598. .with_stderr_does_not_contain("-C debug-assertions")
  599. .with_stderr(
  600. "\
  601. [COMPILING] foo [..]
  602. [RUNNING] `rustc [..]
  603. [FINISHED] [..]
  604. ",
  605. )
  606. .run();
  607. p.cargo("rustc -v")
  608. .with_stderr(
  609. "\
  610. [FRESH] foo [..]
  611. [FINISHED] [..]
  612. ",
  613. )
  614. .run();
  615. }
  616. #[cargo_test]
  617. fn rustc_test_with_implicit_bin() {
  618. let p = project()
  619. .file("Cargo.toml", &basic_bin_manifest("foo"))
  620. .file(
  621. "src/main.rs",
  622. r#"
  623. #[cfg(foo)]
  624. fn f() { compile_fail!("Foo shouldn't be set."); }
  625. fn main() {}
  626. "#,
  627. )
  628. .file(
  629. "tests/test1.rs",
  630. r#"
  631. #[cfg(not(foo))]
  632. fn f() { compile_fail!("Foo should be set."); }
  633. "#,
  634. )
  635. .build();
  636. p.cargo("rustc --test test1 -v -- --cfg foo")
  637. .with_stderr_contains(
  638. "\
  639. [RUNNING] `rustc --crate-name test1 tests/test1.rs [..] --cfg foo [..]
  640. ",
  641. )
  642. .with_stderr_contains(
  643. "\
  644. [RUNNING] `rustc --crate-name foo src/main.rs [..]
  645. ",
  646. )
  647. .run();
  648. }
  649. #[cargo_test]
  650. fn rustc_with_print_cfg_single_target() {
  651. let p = project()
  652. .file("Cargo.toml", &basic_bin_manifest("foo"))
  653. .file("src/main.rs", r#"fn main() {} "#)
  654. .build();
  655. p.cargo("rustc -Z unstable-options --target x86_64-pc-windows-msvc --print cfg")
  656. .masquerade_as_nightly_cargo()
  657. .with_stdout_contains("debug_assertions")
  658. .with_stdout_contains("target_arch=\"x86_64\"")
  659. .with_stdout_contains("target_endian=\"little\"")
  660. .with_stdout_contains("target_env=\"msvc\"")
  661. .with_stdout_contains("target_family=\"windows\"")
  662. .with_stdout_contains("target_os=\"windows\"")
  663. .with_stdout_contains("target_pointer_width=\"64\"")
  664. .with_stdout_contains("target_vendor=\"pc\"")
  665. .with_stdout_contains("windows")
  666. .run();
  667. }
  668. #[cargo_test]
  669. fn rustc_with_print_cfg_multiple_targets() {
  670. let p = project()
  671. .file("Cargo.toml", &basic_bin_manifest("foo"))
  672. .file("src/main.rs", r#"fn main() {} "#)
  673. .build();
  674. p.cargo("rustc -Z unstable-options -Z multitarget --target x86_64-pc-windows-msvc --target i686-unknown-linux-gnu --print cfg")
  675. .masquerade_as_nightly_cargo()
  676. .with_stdout_contains("debug_assertions")
  677. .with_stdout_contains("target_arch=\"x86_64\"")
  678. .with_stdout_contains("target_endian=\"little\"")
  679. .with_stdout_contains("target_env=\"msvc\"")
  680. .with_stdout_contains("target_family=\"windows\"")
  681. .with_stdout_contains("target_os=\"windows\"")
  682. .with_stdout_contains("target_pointer_width=\"64\"")
  683. .with_stdout_contains("target_vendor=\"pc\"")
  684. .with_stdout_contains("windows")
  685. .with_stdout_contains("target_env=\"gnu\"")
  686. .with_stdout_contains("target_family=\"unix\"")
  687. .with_stdout_contains("target_pointer_width=\"32\"")
  688. .with_stdout_contains("target_vendor=\"unknown\"")
  689. .with_stdout_contains("target_os=\"linux\"")
  690. .with_stdout_contains("unix")
  691. .run();
  692. }
  693. #[cargo_test]
  694. fn rustc_with_print_cfg_rustflags_env_var() {
  695. let p = project()
  696. .file("Cargo.toml", &basic_bin_manifest("foo"))
  697. .file("src/main.rs", r#"fn main() {} "#)
  698. .build();
  699. p.cargo("rustc -Z unstable-options --target x86_64-pc-windows-msvc --print cfg")
  700. .masquerade_as_nightly_cargo()
  701. .env("RUSTFLAGS", "-C target-feature=+crt-static")
  702. .with_stdout_contains("debug_assertions")
  703. .with_stdout_contains("target_arch=\"x86_64\"")
  704. .with_stdout_contains("target_endian=\"little\"")
  705. .with_stdout_contains("target_env=\"msvc\"")
  706. .with_stdout_contains("target_family=\"windows\"")
  707. .with_stdout_contains("target_feature=\"crt-static\"")
  708. .with_stdout_contains("target_os=\"windows\"")
  709. .with_stdout_contains("target_pointer_width=\"64\"")
  710. .with_stdout_contains("target_vendor=\"pc\"")
  711. .with_stdout_contains("windows")
  712. .run();
  713. }
  714. #[cargo_test]
  715. fn rustc_with_print_cfg_config_toml() {
  716. let p = project()
  717. .file("Cargo.toml", &basic_bin_manifest("foo"))
  718. .file(
  719. ".cargo/config.toml",
  720. r#"
  721. [target.x86_64-pc-windows-msvc]
  722. rustflags = ["-C", "target-feature=+crt-static"]
  723. "#,
  724. )
  725. .file("src/main.rs", r#"fn main() {} "#)
  726. .build();
  727. p.cargo("rustc -Z unstable-options --target x86_64-pc-windows-msvc --print cfg")
  728. .masquerade_as_nightly_cargo()
  729. .env("RUSTFLAGS", "-C target-feature=+crt-static")
  730. .with_stdout_contains("debug_assertions")
  731. .with_stdout_contains("target_arch=\"x86_64\"")
  732. .with_stdout_contains("target_endian=\"little\"")
  733. .with_stdout_contains("target_env=\"msvc\"")
  734. .with_stdout_contains("target_family=\"windows\"")
  735. .with_stdout_contains("target_feature=\"crt-static\"")
  736. .with_stdout_contains("target_os=\"windows\"")
  737. .with_stdout_contains("target_pointer_width=\"64\"")
  738. .with_stdout_contains("target_vendor=\"pc\"")
  739. .with_stdout_contains("windows")
  740. .run();
  741. }