Replace every Vec in Target(Options) with it's Cow equivalent

This commit is contained in:
Loïc BRANSTETT 2022-03-28 01:08:17 +02:00
parent ccff48f97b
commit ce61d4044d
31 changed files with 117 additions and 53 deletions

View File

@ -674,10 +674,10 @@ fn link_natively<'a, B: ArchiveBuilder<'a>>(
linker::disable_localization(&mut cmd);
for &(ref k, ref v) in &sess.target.link_env {
for &(ref k, ref v) in sess.target.link_env.iter() {
cmd.env(k.as_ref(), v.as_ref());
}
for k in &sess.target.link_env_remove {
for k in sess.target.link_env_remove.iter() {
cmd.env_remove(k.as_ref());
}

View File

@ -2247,6 +2247,15 @@ impl<A: ToJson> ToJson for Vec<A> {
}
}
impl<'a, A: ToJson> ToJson for Cow<'a, [A]>
where
[A]: ToOwned,
{
fn to_json(&self) -> Json {
Json::Array(self.iter().map(|elt| elt.to_json()).collect())
}
}
impl<T: ToString, A: ToJson> ToJson for BTreeMap<T, A> {
fn to_json(&self) -> Json {
let mut d = BTreeMap::new();

View File

@ -956,7 +956,7 @@ fn default_configuration(sess: &Session) -> CrateConfig {
ret.reserve(7); // the minimum number of insertions
// Target bindings.
ret.insert((sym::target_os, Some(Symbol::intern(os))));
for fam in &sess.target.families {
for fam in sess.target.families.iter() {
ret.insert((sym::target_family, Some(Symbol::intern(fam))));
if fam == "windows" {
ret.insert((sym::windows, None));

View File

@ -9,7 +9,7 @@ pub fn target() -> Target {
base.supported_sanitizers = SanitizerSet::ADDRESS | SanitizerSet::CFI | SanitizerSet::THREAD;
base.pre_link_args.insert(LinkerFlavor::Gcc, vec!["-arch".into(), "arm64".into()]);
base.link_env_remove.extend(super::apple_base::macos_link_env_remove());
base.link_env_remove.to_mut().extend(super::apple_base::macos_link_env_remove());
// Clang automatically chooses a more specific target based on
// MACOSX_DEPLOYMENT_TARGET. To enable cross-language LTO to work

View File

@ -2,6 +2,8 @@ use std::{borrow::Cow, env};
use crate::spec::{FramePointer, LldFlavor, SplitDebuginfo, TargetOptions};
use super::cvs;
pub fn opts(os: &'static str) -> TargetOptions {
// ELF TLS is only available in macOS 10.7+. If you try to compile for 10.6
// either the linker will complain if it is used or the binary will end up
@ -26,7 +28,7 @@ pub fn opts(os: &'static str) -> TargetOptions {
dynamic_linking: true,
linker_is_gnu: false,
executables: true,
families: vec!["unix".into()],
families: cvs!["unix"],
is_like_osx: true,
dwarf_version: Some(2),
frame_pointer: FramePointer::Always,
@ -51,7 +53,7 @@ pub fn opts(os: &'static str) -> TargetOptions {
// this environment variable too in recent versions.
//
// For some more info see the commentary on #47086
link_env: vec![("ZERO_AR_DATE".into(), "1".into())],
link_env: Cow::Borrowed(&[(Cow::Borrowed("ZERO_AR_DATE"), Cow::Borrowed("1"))]),
..Default::default()
}

View File

@ -1,4 +1,4 @@
use crate::spec::TargetOptions;
use crate::{spec::cvs, spec::TargetOptions};
use std::borrow::Cow;
use Arch::*;
@ -36,12 +36,12 @@ fn target_cpu(arch: Arch) -> &'static str {
}
}
fn link_env_remove(arch: Arch) -> Vec<Cow<'static, str>> {
fn link_env_remove(arch: Arch) -> Cow<'static, [Cow<'static, str>]> {
match arch {
Armv7 | Armv7s | Arm64 | I386 | X86_64 | Arm64_sim => {
vec!["MACOSX_DEPLOYMENT_TARGET".into()]
cvs!["MACOSX_DEPLOYMENT_TARGET"]
}
X86_64_macabi | Arm64_macabi => vec!["IPHONEOS_DEPLOYMENT_TARGET".into()],
X86_64_macabi | Arm64_macabi => cvs!["IPHONEOS_DEPLOYMENT_TARGET"],
}
}

View File

@ -1,5 +1,7 @@
use crate::spec::{LinkArgs, LinkerFlavor, RelocModel, Target, TargetOptions};
use super::cvs;
/// A base target for Nintendo 3DS devices using the devkitARM toolchain.
///
/// Requires the devkitARM toolchain for 3DS targets on the host system.
@ -30,7 +32,7 @@ pub fn target() -> Target {
linker_flavor: LinkerFlavor::Gcc,
cpu: "mpcore".into(),
executables: true,
families: vec!["unix".into()],
families: cvs!["unix"],
linker: Some("arm-none-eabi-gcc".into()),
relocation_model: RelocModel::Static,
features: "+vfp2".into(),

View File

@ -1,11 +1,13 @@
use crate::spec::{RelroLevel, TargetOptions};
use super::cvs;
pub fn opts() -> TargetOptions {
TargetOptions {
os: "dragonfly".into(),
dynamic_linking: true,
executables: true,
families: vec!["unix".into()],
families: cvs!["unix"],
has_rpath: true,
position_independent_executables: true,
relro_level: RelroLevel::Full,

View File

@ -1,11 +1,13 @@
use crate::spec::{RelroLevel, TargetOptions};
use super::cvs;
pub fn opts() -> TargetOptions {
TargetOptions {
os: "freebsd".into(),
dynamic_linking: true,
executables: true,
families: vec!["unix".into()],
families: cvs!["unix"],
has_rpath: true,
position_independent_executables: true,
relro_level: RelroLevel::Full,

View File

@ -1,5 +1,7 @@
use crate::spec::{crt_objects, LinkArgs, LinkOutputKind, LinkerFlavor, LldFlavor, TargetOptions};
use super::cvs;
pub fn opts() -> TargetOptions {
let mut pre_link_args = LinkArgs::new();
pre_link_args.insert(
@ -25,7 +27,7 @@ pub fn opts() -> TargetOptions {
linker: Some("rust-lld".into()),
dynamic_linking: true,
executables: true,
families: vec!["unix".into()],
families: cvs!["unix"],
is_like_fuchsia: true,
pre_link_args,
pre_link_objects: crt_objects::new(&[

View File

@ -1,11 +1,13 @@
use crate::spec::{RelroLevel, TargetOptions};
use super::cvs;
pub fn opts() -> TargetOptions {
TargetOptions {
os: "haiku".into(),
dynamic_linking: true,
executables: true,
families: vec!["unix".into()],
families: cvs!["unix"],
relro_level: RelroLevel::Full,
..Default::default()
}

View File

@ -5,7 +5,7 @@ pub fn target() -> Target {
base.cpu = "yonah".into();
base.max_atomic_width = Some(64);
base.pre_link_args.insert(LinkerFlavor::Gcc, vec!["-m32".into()]);
base.link_env_remove.extend(super::apple_base::macos_link_env_remove());
base.link_env_remove.to_mut().extend(super::apple_base::macos_link_env_remove());
// don't use probe-stack=inline-asm until rust#83139 and rust#84667 are resolved
base.stack_probes = StackProbeType::Call;
base.frame_pointer = FramePointer::Always;

View File

@ -1,6 +1,8 @@
use crate::spec::{FramePointer, LinkArgs, LinkerFlavor, TargetOptions};
use std::default::Default;
use super::cvs;
pub fn opts() -> TargetOptions {
let mut late_link_args = LinkArgs::new();
late_link_args.insert(
@ -31,7 +33,7 @@ pub fn opts() -> TargetOptions {
dynamic_linking: true,
executables: true,
has_rpath: true,
families: vec!["unix".into()],
families: cvs!["unix"],
is_like_solaris: true,
linker_is_gnu: false,
limit_rdylib_exports: false, // Linker doesn't support this

View File

@ -1,6 +1,8 @@
use crate::spec::{LinkerFlavor, PanicStrategy, TargetOptions};
use std::default::Default;
use super::cvs;
pub fn opts() -> TargetOptions {
TargetOptions {
os: "l4re".into(),
@ -10,7 +12,7 @@ pub fn opts() -> TargetOptions {
panic_strategy: PanicStrategy::Abort,
linker: Some("l4-bender".into()),
linker_is_gnu: false,
families: vec!["unix".into()],
families: cvs!["unix"],
..Default::default()
}
}

View File

@ -1,11 +1,13 @@
use crate::spec::{RelroLevel, TargetOptions};
use super::cvs;
pub fn opts() -> TargetOptions {
TargetOptions {
os: "linux".into(),
dynamic_linking: true,
executables: true,
families: vec!["unix".into()],
families: cvs!["unix"],
has_rpath: true,
position_independent_executables: true,
relro_level: RelroLevel::Full,

View File

@ -1,6 +1,8 @@
use crate::spec::{LinkArgs, LinkerFlavor, LldFlavor, RelocModel};
use crate::spec::{Target, TargetOptions};
use super::cvs;
// The PSP has custom linker requirements.
const LINKER_SCRIPT: &str = include_str!("./mipsel_sony_psp_linker_script.ld");
@ -27,7 +29,7 @@ pub fn target() -> Target {
features: "+single-float".into(),
// PSP does not support trap-on-condition instructions.
llvm_args: vec!["-mno-check-zero-division".into()],
llvm_args: cvs!["-mno-check-zero-division"],
pre_link_args,
link_script: Some(LINKER_SCRIPT.into()),
..Default::default()

View File

@ -1027,6 +1027,25 @@ supported_targets! {
("mips64-openwrt-linux-musl", mips64_openwrt_linux_musl),
}
/// Cow-Vec-Str: Cow<'static, [Cow<'static, str>]>
// FIXME(Urgau): Figure out why the obvious form `["".into()].into()` doesn't work.
macro_rules! cvs {
() => {
::std::borrow::Cow::Borrowed(&[])
};
($($x:expr),+ $(,)?) => {
{
::std::borrow::Cow::Borrowed(&[
$(
::std::borrow::Cow::Borrowed($x),
)*
])
}
};
}
pub(crate) use cvs;
/// Warnings encountered when parsing the target `json`.
///
/// Includes fields that weren't recognized and fields that don't have the expected type.
@ -1160,12 +1179,12 @@ pub struct TargetOptions {
pub link_script: Option<Cow<'static, str>>,
/// Environment variables to be set for the linker invocation.
pub link_env: Vec<(Cow<'static, str>, Cow<'static, str>)>,
pub link_env: Cow<'static, [(Cow<'static, str>, Cow<'static, str>)]>,
/// Environment variables to be removed for the linker invocation.
pub link_env_remove: Vec<Cow<'static, str>>,
pub link_env_remove: Cow<'static, [Cow<'static, str>]>,
/// Extra arguments to pass to the external assembler (when used)
pub asm_args: Vec<Cow<'static, str>>,
pub asm_args: Cow<'static, [Cow<'static, str>]>,
/// Default CPU to pass to LLVM. Corresponds to `llc -mcpu=$cpu`. Defaults
/// to "generic".
@ -1211,7 +1230,7 @@ pub struct TargetOptions {
/// Common options are: "unix", "windows". Defaults to no families.
///
/// See <https://doc.rust-lang.org/reference/conditional-compilation.html#target_family>.
pub families: Vec<Cow<'static, str>>,
pub families: Cow<'static, [Cow<'static, str>]>,
/// Whether the target toolchain's ABI supports returning small structs as an integer.
pub abi_return_struct_as_int: bool,
/// Whether the target toolchain is like macOS's. Only useful for compiling against iOS/macOS,
@ -1371,7 +1390,7 @@ pub struct TargetOptions {
/// If set, have the linker export exactly these symbols, instead of using
/// the usual logic to figure this out from the crate itself.
pub override_export_symbols: Option<Vec<Cow<'static, str>>>,
pub override_export_symbols: Option<Cow<'static, [Cow<'static, str>]>>,
/// Determines how or whether the MergeFunctions LLVM pass should run for
/// this target. Either "disabled", "trampolines", or "aliases".
@ -1391,7 +1410,7 @@ pub struct TargetOptions {
pub relax_elf_relocations: bool,
/// Additional arguments to pass to LLVM, similar to the `-C llvm-args` codegen option.
pub llvm_args: Vec<Cow<'static, str>>,
pub llvm_args: Cow<'static, [Cow<'static, str>]>,
/// Whether to use legacy .ctors initialization hooks rather than .init_array. Defaults
/// to false (uses .init_array).
@ -1449,7 +1468,7 @@ impl Default for TargetOptions {
pre_link_args: LinkArgs::new(),
post_link_args: LinkArgs::new(),
link_script: None,
asm_args: Vec::new(),
asm_args: Cow::Borrowed(&[]),
cpu: "generic".into(),
features: Cow::from(""),
dynamic_linking: false,
@ -1466,7 +1485,7 @@ impl Default for TargetOptions {
exe_suffix: Cow::from(""),
staticlib_prefix: "lib".into(),
staticlib_suffix: ".a".into(),
families: Vec::new(),
families: cvs![],
abi_return_struct_as_int: false,
is_like_osx: false,
is_like_solaris: false,
@ -1492,8 +1511,8 @@ impl Default for TargetOptions {
late_link_args: LinkArgs::new(),
late_link_args_dynamic: LinkArgs::new(),
late_link_args_static: LinkArgs::new(),
link_env: Vec::new(),
link_env_remove: Vec::new(),
link_env: Cow::Borrowed(&[]),
link_env_remove: Cow::Borrowed(&[]),
archive_format: "gnu".into(),
main_needs_argc_argv: true,
allow_asm: true,
@ -1526,7 +1545,7 @@ impl Default for TargetOptions {
mcount: "mcount".into(),
llvm_abiname: "".into(),
relax_elf_relocations: false,
llvm_args: vec![],
llvm_args: cvs![],
use_ctors_section: false,
eh_frame_header: true,
has_thumb_interworking: false,
@ -1978,7 +1997,7 @@ impl Target {
if p.len() == 2 {
let k = p[0].to_string();
let v = p[1].to_string();
base.$key_name.push((k.into(), v.into()));
base.$key_name.to_mut().push((k.into(), v.into()));
}
}
}
@ -2004,7 +2023,7 @@ impl Target {
.map(|a| a.as_string().unwrap().to_string().into())
.collect();
} else if let Some(v) = Json::as_string(&value) {
base.$key_name = vec![v.to_string().into()];
base.$key_name = vec![v.to_string().into()].into();
}
}
} );

View File

@ -1,3 +1,4 @@
use super::cvs;
use crate::spec::{PanicStrategy, RelocModel, Target, TargetOptions};
pub fn target() -> Target {
@ -15,7 +16,7 @@ pub fn target() -> Target {
// workaround this LLVM generates assembly files which then we feed
// to gcc to get object files. For this reason we have a hard
// dependency on this specific gcc.
asm_args: vec!["-mcpu=msp430".into()],
asm_args: cvs!["-mcpu=msp430"],
linker: Some("msp430-elf-gcc".into()),
linker_is_gnu: false,

View File

@ -1,11 +1,13 @@
use crate::spec::{RelroLevel, TargetOptions};
use super::cvs;
pub fn opts() -> TargetOptions {
TargetOptions {
os: "netbsd".into(),
dynamic_linking: true,
executables: true,
families: vec!["unix".into()],
families: cvs!["unix"],
no_default_libraries: false,
has_rpath: true,
position_independent_executables: true,

View File

@ -1,11 +1,13 @@
use crate::spec::{FramePointer, RelroLevel, TargetOptions};
use super::cvs;
pub fn opts() -> TargetOptions {
TargetOptions {
os: "openbsd".into(),
dynamic_linking: true,
executables: true,
families: vec!["unix".into()],
families: cvs!["unix"],
has_rpath: true,
abi_return_struct_as_int: true,
position_independent_executables: true,

View File

@ -1,12 +1,14 @@
use crate::spec::{RelroLevel, TargetOptions};
use super::cvs;
pub fn opts() -> TargetOptions {
TargetOptions {
os: "redox".into(),
env: "relibc".into(),
dynamic_linking: true,
executables: true,
families: vec!["unix".into()],
families: cvs!["unix"],
has_rpath: true,
position_independent_executables: true,
relro_level: RelroLevel::Full,

View File

@ -1,6 +1,8 @@
use crate::spec::{LinkerFlavor, PanicStrategy, RelocModel};
use crate::spec::{Target, TargetOptions};
use super::cvs;
pub fn target() -> Target {
Target {
data_layout: "e-m:e-p:32:32-i64:64-n32-S128".into(),
@ -9,7 +11,7 @@ pub fn target() -> Target {
arch: "riscv32".into(),
options: TargetOptions {
families: vec!["unix".into()],
families: cvs!["unix"],
os: "espidf".into(),
env: "newlib".into(),
vendor: "espressif".into(),

View File

@ -1,12 +1,14 @@
use crate::spec::TargetOptions;
use super::cvs;
pub fn opts() -> TargetOptions {
TargetOptions {
os: "solaris".into(),
dynamic_linking: true,
executables: true,
has_rpath: true,
families: vec!["unix".into()],
families: cvs!["unix"],
is_like_solaris: true,
linker_is_gnu: false,
limit_rdylib_exports: false, // Linker doesn't support this

View File

@ -8,6 +8,7 @@
//!
//! **Important:** This target profile **does not** specify a linker script. You just get the default link script when you build a binary for this target. The default link script is very likely wrong, so you should use `-Clink-arg=-Tmy_script.ld` to override that with a correct linker script.
use super::cvs;
use crate::spec::{LinkerFlavor, Target, TargetOptions};
pub fn target() -> Target {
@ -34,11 +35,7 @@ pub fn target() -> Target {
// * activate t32/a32 interworking
// * use arch ARMv4T
// * use little-endian
asm_args: vec![
"-mthumb-interwork".into(),
"-march=armv4t".into(),
"-mlittle-endian".into(),
],
asm_args: cvs!["-mthumb-interwork", "-march=armv4t", "-mlittle-endian",],
// minimum extra features, these cannot be disabled via -C
features: "+soft-float,+strict-align".into(),

View File

@ -1,5 +1,7 @@
use crate::spec::TargetOptions;
use super::cvs;
pub fn opts() -> TargetOptions {
TargetOptions {
os: "vxworks".into(),
@ -9,7 +11,7 @@ pub fn opts() -> TargetOptions {
exe_suffix: ".vxe".into(),
dynamic_linking: true,
executables: true,
families: vec!["unix".into()],
families: cvs!["unix"],
has_rpath: true,
has_thread_local: true,
crt_static_default: true,

View File

@ -1,4 +1,4 @@
use super::wasm_base;
use super::{cvs, wasm_base};
use super::{LinkArgs, LinkerFlavor, PanicStrategy, Target, TargetOptions};
pub fn target() -> Target {
@ -37,7 +37,7 @@ pub fn target() -> Target {
is_like_emscripten: true,
panic_strategy: PanicStrategy::Unwind,
post_link_args,
families: vec!["unix".into(), "wasm".into()],
families: cvs!["unix", "wasm"],
..options
};
Target {

View File

@ -1,5 +1,5 @@
use super::crt_objects::CrtObjectsFallback;
use super::{LinkerFlavor, LldFlavor, PanicStrategy, RelocModel, TargetOptions, TlsModel};
use super::{cvs, LinkerFlavor, LldFlavor, PanicStrategy, RelocModel, TargetOptions, TlsModel};
use std::collections::BTreeMap;
pub fn options() -> TargetOptions {
@ -61,7 +61,7 @@ pub fn options() -> TargetOptions {
TargetOptions {
is_like_wasm: true,
families: vec!["wasm".into()],
families: cvs!["wasm"],
// we allow dynamic linking, but only cdylibs. Basically we allow a
// final library artifact that exports some symbols (a wasm module) but

View File

@ -1,6 +1,8 @@
use crate::spec::crt_objects::{self, CrtObjectsFallback};
use crate::spec::{LinkArgs, LinkerFlavor, LldFlavor, TargetOptions};
use super::cvs;
pub fn opts() -> TargetOptions {
let mut pre_link_args = LinkArgs::new();
pre_link_args.insert(
@ -71,7 +73,7 @@ pub fn opts() -> TargetOptions {
dll_prefix: "".into(),
dll_suffix: ".dll".into(),
exe_suffix: ".exe".into(),
families: vec!["windows".into()],
families: cvs!["windows"],
is_like_windows: true,
allows_weak_linkage: false,
pre_link_args,

View File

@ -1,5 +1,7 @@
use crate::spec::TargetOptions;
use super::cvs;
pub fn opts() -> TargetOptions {
let base = super::msvc_base::opts();
@ -13,7 +15,7 @@ pub fn opts() -> TargetOptions {
exe_suffix: ".exe".into(),
staticlib_prefix: "".into(),
staticlib_suffix: ".lib".into(),
families: vec!["windows".into()],
families: cvs!["windows"],
crt_static_allows_dylibs: true,
crt_static_respected: true,
requires_uwtable: true,

View File

@ -8,7 +8,7 @@ pub fn target() -> Target {
base.frame_pointer = FramePointer::Always;
base.pre_link_args
.insert(LinkerFlavor::Gcc, vec!["-m64".into(), "-arch".into(), "x86_64".into()]);
base.link_env_remove.extend(super::apple_base::macos_link_env_remove());
base.link_env_remove.to_mut().extend(super::apple_base::macos_link_env_remove());
// don't use probe-stack=inline-asm until rust#83139 and rust#84667 are resolved
base.stack_probes = StackProbeType::Call;
base.supported_sanitizers =

View File

@ -1,5 +1,7 @@
use std::{borrow::Cow, iter};
use crate::spec::cvs;
use super::{LinkerFlavor, LldFlavor, Target, TargetOptions};
pub fn target() -> Target {
@ -62,7 +64,7 @@ pub fn target() -> Target {
max_atomic_width: Some(64),
cpu: "x86-64".into(),
features: "+rdrnd,+rdseed,+lvi-cfi,+lvi-load-hardening".into(),
llvm_args: vec!["--x86-experimental-lvi-inline-asm-hardening".into()],
llvm_args: cvs!["--x86-experimental-lvi-inline-asm-hardening"],
position_independent_executables: true,
pre_link_args: iter::once((
LinkerFlavor::Lld(LldFlavor::Ld),