/tests/testsuite/custom_target.rs

https://gitlab.com/frewsxcv/cargo · Rust · 236 lines · 209 code · 16 blank · 11 comment · 5 complexity · e081b4c0e2354001177ca2b4df8df90e MD5 · raw file

  1. //! Tests for custom json target specifications.
  2. use cargo_test_support::is_nightly;
  3. use cargo_test_support::{basic_manifest, project};
  4. use std::fs;
  5. const MINIMAL_LIB: &str = r#"
  6. #![feature(no_core)]
  7. #![feature(lang_items)]
  8. #![no_core]
  9. #[lang = "sized"]
  10. pub trait Sized {
  11. // Empty.
  12. }
  13. #[lang = "copy"]
  14. pub trait Copy {
  15. // Empty.
  16. }
  17. "#;
  18. const SIMPLE_SPEC: &str = r#"
  19. {
  20. "llvm-target": "x86_64-unknown-none-gnu",
  21. "data-layout": "e-m:e-i64:64-f80:128-n8:16:32:64-S128",
  22. "arch": "x86_64",
  23. "target-endian": "little",
  24. "target-pointer-width": "64",
  25. "target-c-int-width": "32",
  26. "os": "none",
  27. "linker-flavor": "ld.lld",
  28. "linker": "rust-lld",
  29. "executables": true
  30. }
  31. "#;
  32. #[cargo_test]
  33. fn custom_target_minimal() {
  34. if !is_nightly() {
  35. // Requires features no_core, lang_items
  36. return;
  37. }
  38. let p = project()
  39. .file(
  40. "src/lib.rs",
  41. &"
  42. __MINIMAL_LIB__
  43. pub fn foo() -> u32 {
  44. 42
  45. }
  46. "
  47. .replace("__MINIMAL_LIB__", MINIMAL_LIB),
  48. )
  49. .file("custom-target.json", SIMPLE_SPEC)
  50. .build();
  51. p.cargo("build --lib --target custom-target.json -v").run();
  52. p.cargo("build --lib --target src/../custom-target.json -v")
  53. .run();
  54. // Ensure that the correct style of flag is passed to --target with doc tests.
  55. p.cargo("test --doc --target src/../custom-target.json -v -Zdoctest-xcompile")
  56. .masquerade_as_nightly_cargo()
  57. .with_stderr_contains("[RUNNING] `rustdoc [..]--target [..]foo/custom-target.json[..]")
  58. .run();
  59. }
  60. #[cargo_test]
  61. fn custom_target_dependency() {
  62. if !is_nightly() {
  63. // Requires features no_core, lang_items, auto_traits
  64. return;
  65. }
  66. let p = project()
  67. .file(
  68. "Cargo.toml",
  69. r#"
  70. [package]
  71. name = "foo"
  72. version = "0.0.1"
  73. authors = ["author@example.com"]
  74. [dependencies]
  75. bar = { path = "bar" }
  76. "#,
  77. )
  78. .file(
  79. "src/lib.rs",
  80. r#"
  81. #![feature(no_core)]
  82. #![feature(lang_items)]
  83. #![feature(auto_traits)]
  84. #![no_core]
  85. extern crate bar;
  86. pub fn foo() -> u32 {
  87. bar::bar()
  88. }
  89. #[lang = "freeze"]
  90. unsafe auto trait Freeze {}
  91. "#,
  92. )
  93. .file("bar/Cargo.toml", &basic_manifest("bar", "0.0.1"))
  94. .file(
  95. "bar/src/lib.rs",
  96. &"
  97. __MINIMAL_LIB__
  98. pub fn bar() -> u32 {
  99. 42
  100. }
  101. "
  102. .replace("__MINIMAL_LIB__", MINIMAL_LIB),
  103. )
  104. .file("custom-target.json", SIMPLE_SPEC)
  105. .build();
  106. p.cargo("build --lib --target custom-target.json -v").run();
  107. }
  108. #[cargo_test]
  109. fn custom_bin_target() {
  110. if !is_nightly() {
  111. // Requires features no_core, lang_items
  112. return;
  113. }
  114. let p = project()
  115. .file(
  116. "src/main.rs",
  117. &"
  118. #![no_main]
  119. __MINIMAL_LIB__
  120. "
  121. .replace("__MINIMAL_LIB__", MINIMAL_LIB),
  122. )
  123. .file("custom-bin-target.json", SIMPLE_SPEC)
  124. .build();
  125. p.cargo("build --target custom-bin-target.json -v").run();
  126. }
  127. #[cargo_test]
  128. fn changing_spec_rebuilds() {
  129. // Changing the .json file will trigger a rebuild.
  130. if !is_nightly() {
  131. // Requires features no_core, lang_items
  132. return;
  133. }
  134. let p = project()
  135. .file(
  136. "src/lib.rs",
  137. &"
  138. __MINIMAL_LIB__
  139. pub fn foo() -> u32 {
  140. 42
  141. }
  142. "
  143. .replace("__MINIMAL_LIB__", MINIMAL_LIB),
  144. )
  145. .file("custom-target.json", SIMPLE_SPEC)
  146. .build();
  147. p.cargo("build --lib --target custom-target.json -v").run();
  148. p.cargo("build --lib --target custom-target.json -v")
  149. .with_stderr(
  150. "\
  151. [FRESH] foo [..]
  152. [FINISHED] [..]
  153. ",
  154. )
  155. .run();
  156. let spec_path = p.root().join("custom-target.json");
  157. let spec = fs::read_to_string(&spec_path).unwrap();
  158. // Some arbitrary change that I hope is safe.
  159. let spec = spec.replace('{', "{\n\"vendor\": \"unknown\",\n");
  160. fs::write(&spec_path, spec).unwrap();
  161. p.cargo("build --lib --target custom-target.json -v")
  162. .with_stderr(
  163. "\
  164. [COMPILING] foo v0.0.1 [..]
  165. [RUNNING] `rustc [..]
  166. [FINISHED] [..]
  167. ",
  168. )
  169. .run();
  170. }
  171. #[cargo_test]
  172. fn changing_spec_relearns_crate_types() {
  173. // Changing the .json file will invalidate the cache of crate types.
  174. if !is_nightly() {
  175. // Requires features no_core, lang_items
  176. return;
  177. }
  178. let p = project()
  179. .file(
  180. "Cargo.toml",
  181. r#"
  182. [package]
  183. name = "foo"
  184. version = "0.1.0"
  185. [lib]
  186. crate-type = ["cdylib"]
  187. "#,
  188. )
  189. .file("src/lib.rs", MINIMAL_LIB)
  190. .file("custom-target.json", SIMPLE_SPEC)
  191. .build();
  192. p.cargo("build --lib --target custom-target.json -v")
  193. .with_status(101)
  194. .with_stderr("error: cannot produce cdylib for `foo [..]")
  195. .run();
  196. // Enable dynamic linking.
  197. let spec_path = p.root().join("custom-target.json");
  198. let spec = fs::read_to_string(&spec_path).unwrap();
  199. let spec = spec.replace('{', "{\n\"dynamic-linking\": true,\n");
  200. fs::write(&spec_path, spec).unwrap();
  201. p.cargo("build --lib --target custom-target.json -v")
  202. .with_stderr(
  203. "\
  204. [COMPILING] foo [..]
  205. [RUNNING] `rustc [..]
  206. [FINISHED] [..]
  207. ",
  208. )
  209. .run();
  210. }