2019-11-25 02:42:45 +00:00
|
|
|
//! Tests for custom json target specifications.
|
|
|
|
|
2019-09-12 17:14:29 +00:00
|
|
|
use cargo_test_support::{basic_manifest, project};
|
2021-03-01 20:19:14 +00:00
|
|
|
use std::fs;
|
|
|
|
|
|
|
|
const MINIMAL_LIB: &str = r#"
|
2023-08-05 19:11:07 +00:00
|
|
|
#![allow(internal_features)]
|
2021-03-01 20:19:14 +00:00
|
|
|
#![feature(no_core)]
|
|
|
|
#![feature(lang_items)]
|
|
|
|
#![no_core]
|
|
|
|
|
|
|
|
#[lang = "sized"]
|
|
|
|
pub trait Sized {
|
|
|
|
// Empty.
|
|
|
|
}
|
|
|
|
#[lang = "copy"]
|
|
|
|
pub trait Copy {
|
|
|
|
// Empty.
|
|
|
|
}
|
|
|
|
"#;
|
|
|
|
|
|
|
|
const SIMPLE_SPEC: &str = r#"
|
|
|
|
{
|
|
|
|
"llvm-target": "x86_64-unknown-none-gnu",
|
2024-01-28 20:52:14 +00:00
|
|
|
"data-layout": "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128",
|
2021-03-01 20:19:14 +00:00
|
|
|
"arch": "x86_64",
|
|
|
|
"target-endian": "little",
|
|
|
|
"target-pointer-width": "64",
|
|
|
|
"target-c-int-width": "32",
|
|
|
|
"os": "none",
|
|
|
|
"linker-flavor": "ld.lld",
|
|
|
|
"linker": "rust-lld",
|
|
|
|
"executables": true
|
|
|
|
}
|
|
|
|
"#;
|
2018-03-24 20:38:48 +00:00
|
|
|
|
2021-09-09 06:10:33 +00:00
|
|
|
#[cargo_test(nightly, reason = "requires features no_core, lang_items")]
|
2018-03-24 20:38:48 +00:00
|
|
|
fn custom_target_minimal() {
|
2018-07-20 11:47:47 +00:00
|
|
|
let p = project()
|
2018-03-24 20:38:48 +00:00
|
|
|
.file(
|
|
|
|
"src/lib.rs",
|
2021-03-01 20:19:14 +00:00
|
|
|
&"
|
|
|
|
__MINIMAL_LIB__
|
2020-09-27 00:59:58 +00:00
|
|
|
|
|
|
|
pub fn foo() -> u32 {
|
|
|
|
42
|
|
|
|
}
|
2021-03-01 20:19:14 +00:00
|
|
|
"
|
|
|
|
.replace("__MINIMAL_LIB__", MINIMAL_LIB),
|
2018-12-08 11:19:47 +00:00
|
|
|
)
|
2021-03-01 20:19:14 +00:00
|
|
|
.file("custom-target.json", SIMPLE_SPEC)
|
2018-12-08 11:19:47 +00:00
|
|
|
.build();
|
2018-03-24 20:38:48 +00:00
|
|
|
|
2018-08-28 09:20:03 +00:00
|
|
|
p.cargo("build --lib --target custom-target.json -v").run();
|
|
|
|
p.cargo("build --lib --target src/../custom-target.json -v")
|
|
|
|
.run();
|
2020-04-17 15:35:10 +00:00
|
|
|
|
|
|
|
// Ensure that the correct style of flag is passed to --target with doc tests.
|
|
|
|
p.cargo("test --doc --target src/../custom-target.json -v -Zdoctest-xcompile")
|
2022-07-16 02:32:23 +00:00
|
|
|
.masquerade_as_nightly_cargo(&["doctest-xcompile", "no_core", "lang_items"])
|
2020-04-17 15:35:10 +00:00
|
|
|
.with_stderr_contains("[RUNNING] `rustdoc [..]--target [..]foo/custom-target.json[..]")
|
|
|
|
.run();
|
2018-03-24 20:38:48 +00:00
|
|
|
}
|
|
|
|
|
2021-09-09 06:10:33 +00:00
|
|
|
#[cargo_test(nightly, reason = "requires features no_core, lang_items, auto_traits")]
|
2018-03-24 20:38:48 +00:00
|
|
|
fn custom_target_dependency() {
|
2018-07-20 11:47:47 +00:00
|
|
|
let p = project()
|
2018-03-24 20:38:48 +00:00
|
|
|
.file(
|
|
|
|
"Cargo.toml",
|
|
|
|
r#"
|
2020-09-27 00:59:58 +00:00
|
|
|
[package]
|
2018-03-24 20:38:48 +00:00
|
|
|
|
2020-09-27 00:59:58 +00:00
|
|
|
name = "foo"
|
|
|
|
version = "0.0.1"
|
|
|
|
authors = ["author@example.com"]
|
2018-03-24 20:38:48 +00:00
|
|
|
|
2020-09-27 00:59:58 +00:00
|
|
|
[dependencies]
|
|
|
|
bar = { path = "bar" }
|
|
|
|
"#,
|
2018-12-08 11:19:47 +00:00
|
|
|
)
|
|
|
|
.file(
|
2018-03-24 20:38:48 +00:00
|
|
|
"src/lib.rs",
|
|
|
|
r#"
|
2023-08-05 19:11:07 +00:00
|
|
|
#![allow(internal_features)]
|
2020-09-27 00:59:58 +00:00
|
|
|
#![feature(no_core)]
|
|
|
|
#![feature(lang_items)]
|
2020-11-27 14:50:08 +00:00
|
|
|
#![feature(auto_traits)]
|
2020-09-27 00:59:58 +00:00
|
|
|
#![no_core]
|
2018-03-24 20:38:48 +00:00
|
|
|
|
2020-09-27 00:59:58 +00:00
|
|
|
extern crate bar;
|
2018-03-24 20:38:48 +00:00
|
|
|
|
2020-09-27 00:59:58 +00:00
|
|
|
pub fn foo() -> u32 {
|
|
|
|
bar::bar()
|
|
|
|
}
|
2018-03-24 20:38:48 +00:00
|
|
|
|
2020-09-27 00:59:58 +00:00
|
|
|
#[lang = "freeze"]
|
|
|
|
unsafe auto trait Freeze {}
|
|
|
|
"#,
|
2018-12-08 11:19:47 +00:00
|
|
|
)
|
|
|
|
.file("bar/Cargo.toml", &basic_manifest("bar", "0.0.1"))
|
2018-03-24 20:38:48 +00:00
|
|
|
.file(
|
|
|
|
"bar/src/lib.rs",
|
2021-03-01 20:19:14 +00:00
|
|
|
&"
|
|
|
|
__MINIMAL_LIB__
|
2020-09-27 00:59:58 +00:00
|
|
|
|
|
|
|
pub fn bar() -> u32 {
|
|
|
|
42
|
|
|
|
}
|
2021-03-01 20:19:14 +00:00
|
|
|
"
|
|
|
|
.replace("__MINIMAL_LIB__", MINIMAL_LIB),
|
2018-12-08 11:19:47 +00:00
|
|
|
)
|
2021-03-01 20:19:14 +00:00
|
|
|
.file("custom-target.json", SIMPLE_SPEC)
|
2018-12-08 11:19:47 +00:00
|
|
|
.build();
|
2018-03-24 20:38:48 +00:00
|
|
|
|
2018-08-28 09:20:03 +00:00
|
|
|
p.cargo("build --lib --target custom-target.json -v").run();
|
2018-03-24 20:38:48 +00:00
|
|
|
}
|
2019-09-16 07:31:20 +00:00
|
|
|
|
2021-09-09 06:10:33 +00:00
|
|
|
#[cargo_test(nightly, reason = "requires features no_core, lang_items")]
|
2023-10-02 01:22:41 +00:00
|
|
|
// This is randomly crashing in lld. See https://github.com/rust-lang/rust/issues/115985
|
|
|
|
#[cfg_attr(all(windows, target_env = "gnu"), ignore = "windows-gnu lld crashing")]
|
2019-09-16 07:31:20 +00:00
|
|
|
fn custom_bin_target() {
|
|
|
|
let p = project()
|
|
|
|
.file(
|
|
|
|
"src/main.rs",
|
2021-03-01 20:19:14 +00:00
|
|
|
&"
|
2020-09-27 00:59:58 +00:00
|
|
|
#![no_main]
|
2021-03-01 20:19:14 +00:00
|
|
|
__MINIMAL_LIB__
|
|
|
|
"
|
|
|
|
.replace("__MINIMAL_LIB__", MINIMAL_LIB),
|
|
|
|
)
|
|
|
|
.file("custom-bin-target.json", SIMPLE_SPEC)
|
|
|
|
.build();
|
2020-09-27 00:59:58 +00:00
|
|
|
|
2021-03-01 20:19:14 +00:00
|
|
|
p.cargo("build --target custom-bin-target.json -v").run();
|
|
|
|
}
|
|
|
|
|
2021-09-09 06:10:33 +00:00
|
|
|
#[cargo_test(nightly, reason = "requires features no_core, lang_items")]
|
2021-03-01 20:19:14 +00:00
|
|
|
fn changing_spec_rebuilds() {
|
|
|
|
// Changing the .json file will trigger a rebuild.
|
|
|
|
let p = project()
|
|
|
|
.file(
|
|
|
|
"src/lib.rs",
|
|
|
|
&"
|
|
|
|
__MINIMAL_LIB__
|
|
|
|
|
|
|
|
pub fn foo() -> u32 {
|
|
|
|
42
|
2020-09-27 00:59:58 +00:00
|
|
|
}
|
2021-03-01 20:19:14 +00:00
|
|
|
"
|
|
|
|
.replace("__MINIMAL_LIB__", MINIMAL_LIB),
|
|
|
|
)
|
|
|
|
.file("custom-target.json", SIMPLE_SPEC)
|
|
|
|
.build();
|
|
|
|
|
|
|
|
p.cargo("build --lib --target custom-target.json -v").run();
|
|
|
|
p.cargo("build --lib --target custom-target.json -v")
|
|
|
|
.with_stderr(
|
|
|
|
"\
|
|
|
|
[FRESH] foo [..]
|
|
|
|
[FINISHED] [..]
|
|
|
|
",
|
|
|
|
)
|
|
|
|
.run();
|
|
|
|
let spec_path = p.root().join("custom-target.json");
|
|
|
|
let spec = fs::read_to_string(&spec_path).unwrap();
|
|
|
|
// Some arbitrary change that I hope is safe.
|
|
|
|
let spec = spec.replace('{', "{\n\"vendor\": \"unknown\",\n");
|
|
|
|
fs::write(&spec_path, spec).unwrap();
|
|
|
|
p.cargo("build --lib --target custom-target.json -v")
|
|
|
|
.with_stderr(
|
|
|
|
"\
|
|
|
|
[COMPILING] foo v0.0.1 [..]
|
|
|
|
[RUNNING] `rustc [..]
|
|
|
|
[FINISHED] [..]
|
|
|
|
",
|
2019-09-16 07:31:20 +00:00
|
|
|
)
|
2021-03-01 20:19:14 +00:00
|
|
|
.run();
|
|
|
|
}
|
|
|
|
|
2021-09-09 06:10:33 +00:00
|
|
|
#[cargo_test(nightly, reason = "requires features no_core, lang_items")]
|
2023-11-14 03:17:51 +00:00
|
|
|
// This is randomly crashing in lld. See https://github.com/rust-lang/rust/issues/115985
|
|
|
|
#[cfg_attr(all(windows, target_env = "gnu"), ignore = "windows-gnu lld crashing")]
|
2021-03-01 20:19:14 +00:00
|
|
|
fn changing_spec_relearns_crate_types() {
|
|
|
|
// Changing the .json file will invalidate the cache of crate types.
|
|
|
|
let p = project()
|
2019-09-16 07:31:20 +00:00
|
|
|
.file(
|
2021-03-01 20:19:14 +00:00
|
|
|
"Cargo.toml",
|
2019-09-16 07:31:20 +00:00
|
|
|
r#"
|
2021-03-01 20:19:14 +00:00
|
|
|
[package]
|
|
|
|
name = "foo"
|
|
|
|
version = "0.1.0"
|
|
|
|
|
|
|
|
[lib]
|
|
|
|
crate-type = ["cdylib"]
|
2020-09-27 00:59:58 +00:00
|
|
|
"#,
|
2019-09-16 07:31:20 +00:00
|
|
|
)
|
2021-03-01 20:19:14 +00:00
|
|
|
.file("src/lib.rs", MINIMAL_LIB)
|
|
|
|
.file("custom-target.json", SIMPLE_SPEC)
|
2019-09-16 07:31:20 +00:00
|
|
|
.build();
|
|
|
|
|
2021-03-01 20:19:14 +00:00
|
|
|
p.cargo("build --lib --target custom-target.json -v")
|
|
|
|
.with_status(101)
|
|
|
|
.with_stderr("error: cannot produce cdylib for `foo [..]")
|
|
|
|
.run();
|
|
|
|
|
|
|
|
// Enable dynamic linking.
|
|
|
|
let spec_path = p.root().join("custom-target.json");
|
|
|
|
let spec = fs::read_to_string(&spec_path).unwrap();
|
|
|
|
let spec = spec.replace('{', "{\n\"dynamic-linking\": true,\n");
|
|
|
|
fs::write(&spec_path, spec).unwrap();
|
|
|
|
|
|
|
|
p.cargo("build --lib --target custom-target.json -v")
|
|
|
|
.with_stderr(
|
|
|
|
"\
|
|
|
|
[COMPILING] foo [..]
|
|
|
|
[RUNNING] `rustc [..]
|
|
|
|
[FINISHED] [..]
|
|
|
|
",
|
|
|
|
)
|
|
|
|
.run();
|
2019-09-16 07:31:20 +00:00
|
|
|
}
|
2022-06-10 19:27:59 +00:00
|
|
|
|
2021-09-09 06:10:33 +00:00
|
|
|
#[cargo_test(nightly, reason = "requires features no_core, lang_items")]
|
2022-06-10 19:27:59 +00:00
|
|
|
fn custom_target_ignores_filepath() {
|
|
|
|
// Changing the path of the .json file will not trigger a rebuild.
|
|
|
|
let p = project()
|
|
|
|
.file(
|
|
|
|
"src/lib.rs",
|
|
|
|
&"
|
|
|
|
__MINIMAL_LIB__
|
|
|
|
|
|
|
|
pub fn foo() -> u32 {
|
|
|
|
42
|
|
|
|
}
|
|
|
|
"
|
|
|
|
.replace("__MINIMAL_LIB__", MINIMAL_LIB),
|
|
|
|
)
|
|
|
|
.file("b/custom-target.json", SIMPLE_SPEC)
|
|
|
|
.file("a/custom-target.json", SIMPLE_SPEC)
|
|
|
|
.build();
|
|
|
|
|
|
|
|
// Should build the library the first time.
|
|
|
|
p.cargo("build --lib --target a/custom-target.json")
|
|
|
|
.with_stderr(
|
|
|
|
"\
|
|
|
|
[..]Compiling foo v0.0.1 ([..])
|
|
|
|
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
|
|
|
|
",
|
|
|
|
)
|
|
|
|
.run();
|
|
|
|
|
|
|
|
// But not the second time, even though the path to the custom target is dfferent.
|
|
|
|
p.cargo("build --lib --target b/custom-target.json")
|
|
|
|
.with_stderr("[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]")
|
|
|
|
.run();
|
|
|
|
}
|