Cleanup after some refactoring in rustc_target

This commit is contained in:
Loïc BRANSTETT 2022-04-03 18:42:39 +02:00
parent c16a558f24
commit 1a1f5b89a4
25 changed files with 79 additions and 112 deletions

View file

@ -304,8 +304,12 @@ pub(crate) fn run_aot(
};
// FIXME handle `-Ctarget-cpu=native`
let target_cpu =
tcx.sess.opts.cg.target_cpu.as_ref().unwrap_or(&tcx.sess.target.cpu).to_owned();
let target_cpu = match tcx.sess.opts.cg.target_cpu {
Some(ref name) => name,
None => tcx.sess.target.cpu.as_ref(),
}
.to_owned();
Box::new((
CodegenResults {
modules,

View file

@ -287,8 +287,10 @@ fn handle_native(name: &str) -> &str {
}
pub fn target_cpu(sess: &Session) -> &str {
let name = sess.opts.cg.target_cpu.as_ref().unwrap_or(&sess.target.cpu);
handle_native(name)
match sess.opts.cg.target_cpu {
Some(ref name) => handle_native(name),
None => handle_native(sess.target.cpu.as_ref()),
}
}
pub fn target_features(sess: &Session) -> Vec<Symbol> {

View file

@ -675,10 +675,10 @@ fn link_natively<'a, B: ArchiveBuilder<'a>>(
linker::disable_localization(&mut cmd);
for &(ref k, ref v) in sess.target.link_env.iter() {
for &(ref k, ref v) in sess.target.link_env.as_ref() {
cmd.env(k.as_ref(), v.as_ref());
}
for k in sess.target.link_env_remove.iter() {
for k in sess.target.link_env_remove.as_ref() {
cmd.env_remove(k.as_ref());
}
@ -1217,7 +1217,7 @@ pub fn linker_and_flavor(sess: &Session) -> (PathBuf, LinkerFlavor) {
if let Some(ret) = infer_from(
sess,
sess.target.linker.as_ref().map(|l| PathBuf::from(l.as_ref())),
sess.target.linker.as_deref().map(PathBuf::from),
Some(sess.target.linker_flavor),
) {
return ret;

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.iter() {
for fam in sess.target.families.as_ref() {
ret.insert((sym::target_family, Some(Symbol::intern(fam))));
if fam == "windows" {
ret.insert((sym::windows, None));

View file

@ -1,8 +1,6 @@
use std::{borrow::Cow, env};
use crate::spec::{FramePointer, LldFlavor, SplitDebuginfo, TargetOptions};
use super::cvs;
use crate::spec::{cvs, FramePointer, LldFlavor, SplitDebuginfo, TargetOptions};
pub fn opts(os: &'static str) -> TargetOptions {
// ELF TLS is only available in macOS 10.7+. If you try to compile for 10.6

View file

@ -1,6 +1,4 @@
use crate::spec::{LinkArgs, LinkerFlavor, RelocModel, Target, TargetOptions};
use super::cvs;
use crate::spec::{cvs, LinkArgs, LinkerFlavor, RelocModel, Target, TargetOptions};
/// A base target for Nintendo 3DS devices using the devkitARM toolchain.
///

View file

@ -1,6 +1,4 @@
use crate::spec::{RelroLevel, TargetOptions};
use super::cvs;
use crate::spec::{cvs, RelroLevel, TargetOptions};
pub fn opts() -> TargetOptions {
TargetOptions {

View file

@ -1,6 +1,4 @@
use crate::spec::{RelroLevel, TargetOptions};
use super::cvs;
use crate::spec::{cvs, RelroLevel, TargetOptions};
pub fn opts() -> TargetOptions {
TargetOptions {

View file

@ -1,6 +1,6 @@
use crate::spec::{crt_objects, LinkArgs, LinkOutputKind, LinkerFlavor, LldFlavor, TargetOptions};
use super::cvs;
use crate::spec::{
crt_objects, cvs, LinkArgs, LinkOutputKind, LinkerFlavor, LldFlavor, TargetOptions,
};
pub fn opts() -> TargetOptions {
let mut pre_link_args = LinkArgs::new();

View file

@ -1,6 +1,4 @@
use crate::spec::{RelroLevel, TargetOptions};
use super::cvs;
use crate::spec::{cvs, RelroLevel, TargetOptions};
pub fn opts() -> TargetOptions {
TargetOptions {

View file

@ -1,7 +1,4 @@
use crate::spec::{FramePointer, LinkArgs, LinkerFlavor, TargetOptions};
use std::default::Default;
use super::cvs;
use crate::spec::{cvs, FramePointer, LinkArgs, LinkerFlavor, TargetOptions};
pub fn opts() -> TargetOptions {
let mut late_link_args = LinkArgs::new();

View file

@ -1,7 +1,4 @@
use crate::spec::{LinkerFlavor, PanicStrategy, TargetOptions};
use std::default::Default;
use super::cvs;
use crate::spec::{cvs, LinkerFlavor, PanicStrategy, TargetOptions};
pub fn opts() -> TargetOptions {
TargetOptions {

View file

@ -1,6 +1,4 @@
use crate::spec::{RelroLevel, TargetOptions};
use super::cvs;
use crate::spec::{cvs, RelroLevel, TargetOptions};
pub fn opts() -> TargetOptions {
TargetOptions {

View file

@ -1,7 +1,5 @@
use crate::spec::{cvs, Target, TargetOptions};
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");

View file

@ -459,7 +459,7 @@ impl fmt::Display for LinkOutputKind {
}
}
pub type LinkArgs = BTreeMap<LinkerFlavor, Vec<Cow<'static, str>>>;
pub type LinkArgs = BTreeMap<LinkerFlavor, Vec<StaticCow<str>>>;
#[derive(Clone, Copy, Hash, Debug, PartialEq, Eq)]
pub enum SplitDebuginfo {
@ -1028,19 +1028,16 @@ supported_targets! {
}
/// 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),
)*
])
}
::std::borrow::Cow::Borrowed(&[
$(
::std::borrow::Cow::Borrowed($x),
)*
])
};
}
@ -1084,14 +1081,14 @@ impl TargetWarnings {
#[derive(PartialEq, Clone, Debug)]
pub struct Target {
/// Target triple to pass to LLVM.
pub llvm_target: Cow<'static, str>,
pub llvm_target: StaticCow<str>,
/// Number of bits in a pointer. Influences the `target_pointer_width` `cfg` variable.
pub pointer_width: u32,
/// Architecture to use for ABI considerations. Valid options include: "x86",
/// "x86_64", "arm", "aarch64", "mips", "powerpc", "powerpc64", and others.
pub arch: Cow<'static, str>,
pub arch: StaticCow<str>,
/// [Data layout](https://llvm.org/docs/LangRef.html#data-layout) to pass to LLVM.
pub data_layout: Cow<'static, str>,
pub data_layout: StaticCow<str>,
/// Optional settings with defaults.
pub options: TargetOptions,
}
@ -1107,6 +1104,8 @@ impl HasTargetSpec for Target {
}
}
type StaticCow<T> = Cow<'static, T>;
/// Optional aspects of a target specification.
///
/// This has an implementation of `Default`, see each field for what the default is. In general,
@ -1123,25 +1122,25 @@ pub struct TargetOptions {
/// Used as the `target_endian` `cfg` variable. Defaults to little endian.
pub endian: Endian,
/// Width of c_int type. Defaults to "32".
pub c_int_width: Cow<'static, str>,
pub c_int_width: StaticCow<str>,
/// OS name to use for conditional compilation (`target_os`). Defaults to "none".
/// "none" implies a bare metal target without `std` library.
/// A couple of targets having `std` also use "unknown" as an `os` value,
/// but they are exceptions.
pub os: Cow<'static, str>,
pub os: StaticCow<str>,
/// Environment name to use for conditional compilation (`target_env`). Defaults to "".
pub env: Cow<'static, str>,
pub env: StaticCow<str>,
/// ABI name to distinguish multiple ABIs on the same OS and architecture. For instance, `"eabi"`
/// or `"eabihf"`. Defaults to "".
pub abi: Cow<'static, str>,
pub abi: StaticCow<str>,
/// Vendor name to use for conditional compilation (`target_vendor`). Defaults to "unknown".
pub vendor: Cow<'static, str>,
pub vendor: StaticCow<str>,
/// Default linker flavor used if `-C linker-flavor` or `-C linker` are not passed
/// on the command line. Defaults to `LinkerFlavor::Gcc`.
pub linker_flavor: LinkerFlavor,
/// Linker to invoke
pub linker: Option<Cow<'static, str>>,
pub linker: Option<StaticCow<str>>,
/// LLD flavor used if `lld` (or `rust-lld`) is specified as a linker
/// without clarifying its flavor in any way.
@ -1176,23 +1175,23 @@ pub struct TargetOptions {
/// Optional link script applied to `dylib` and `executable` crate types.
/// This is a string containing the script, not a path. Can only be applied
/// to linkers where `linker_is_gnu` is true.
pub link_script: Option<Cow<'static, str>>,
pub link_script: Option<StaticCow<str>>,
/// Environment variables to be set for the linker invocation.
pub link_env: Cow<'static, [(Cow<'static, str>, Cow<'static, str>)]>,
pub link_env: StaticCow<[(StaticCow<str>, StaticCow<str>)]>,
/// Environment variables to be removed for the linker invocation.
pub link_env_remove: Cow<'static, [Cow<'static, str>]>,
pub link_env_remove: StaticCow<[StaticCow<str>]>,
/// Extra arguments to pass to the external assembler (when used)
pub asm_args: Cow<'static, [Cow<'static, str>]>,
pub asm_args: StaticCow<[StaticCow<str>]>,
/// Default CPU to pass to LLVM. Corresponds to `llc -mcpu=$cpu`. Defaults
/// to "generic".
pub cpu: Cow<'static, str>,
pub cpu: StaticCow<str>,
/// Default target features to pass to LLVM. These features will *always* be
/// passed, and cannot be disabled even via `-C`. Corresponds to `llc
/// -mattr=$features`.
pub features: Cow<'static, str>,
pub features: StaticCow<str>,
/// Whether dynamic linking is available on this target. Defaults to false.
pub dynamic_linking: bool,
/// If dynamic linking is available, whether only cdylibs are supported.
@ -1216,21 +1215,21 @@ pub struct TargetOptions {
/// Emit each function in its own section. Defaults to true.
pub function_sections: bool,
/// String to prepend to the name of every dynamic library. Defaults to "lib".
pub dll_prefix: Cow<'static, str>,
pub dll_prefix: StaticCow<str>,
/// String to append to the name of every dynamic library. Defaults to ".so".
pub dll_suffix: Cow<'static, str>,
pub dll_suffix: StaticCow<str>,
/// String to append to the name of every executable.
pub exe_suffix: Cow<'static, str>,
pub exe_suffix: StaticCow<str>,
/// String to prepend to the name of every static library. Defaults to "lib".
pub staticlib_prefix: Cow<'static, str>,
pub staticlib_prefix: StaticCow<str>,
/// String to append to the name of every static library. Defaults to ".a".
pub staticlib_suffix: Cow<'static, str>,
pub staticlib_suffix: StaticCow<str>,
/// Values of the `target_family` cfg set for this target.
///
/// Common options are: "unix", "windows". Defaults to no families.
///
/// See <https://doc.rust-lang.org/reference/conditional-compilation.html#target_family>.
pub families: Cow<'static, [Cow<'static, str>]>,
pub families: StaticCow<[StaticCow<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,
@ -1302,7 +1301,7 @@ pub struct TargetOptions {
/// LLVM to assemble an archive or fall back to the system linker, and
/// currently only "gnu" is used to fall into LLVM. Unknown strings cause
/// the system linker to be used.
pub archive_format: Cow<'static, str>,
pub archive_format: StaticCow<str>,
/// Is asm!() allowed? Defaults to true.
pub allow_asm: bool,
/// Whether the runtime startup code requires the `main` function be passed
@ -1318,7 +1317,7 @@ pub struct TargetOptions {
/// Whether the target requires that emitted object code includes bitcode.
pub forces_embed_bitcode: bool,
/// Content of the LLVM cmdline section associated with embedded bitcode.
pub bitcode_llvm_cmdline: Cow<'static, str>,
pub bitcode_llvm_cmdline: StaticCow<str>,
/// Don't use this field; instead use the `.min_atomic_width()` method.
pub min_atomic_width: Option<u64>,
@ -1390,7 +1389,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<Cow<'static, [Cow<'static, str>]>>,
pub override_export_symbols: Option<StaticCow<[StaticCow<str>]>>,
/// Determines how or whether the MergeFunctions LLVM pass should run for
/// this target. Either "disabled", "trampolines", or "aliases".
@ -1401,16 +1400,16 @@ pub struct TargetOptions {
pub merge_functions: MergeFunctions,
/// Use platform dependent mcount function
pub mcount: Cow<'static, str>,
pub mcount: StaticCow<str>,
/// LLVM ABI name, corresponds to the '-mabi' parameter available in multilib C compilers
pub llvm_abiname: Cow<'static, str>,
pub llvm_abiname: StaticCow<str>,
/// Whether or not RelaxElfRelocation flag will be passed to the linker
pub relax_elf_relocations: bool,
/// Additional arguments to pass to LLVM, similar to the `-C llvm-args` codegen option.
pub llvm_args: Cow<'static, [Cow<'static, str>]>,
pub llvm_args: StaticCow<[StaticCow<str>]>,
/// Whether to use legacy .ctors initialization hooks rather than .init_array. Defaults
/// to false (uses .init_array).
@ -1459,8 +1458,8 @@ impl Default for TargetOptions {
endian: Endian::Little,
c_int_width: "32".into(),
os: "none".into(),
env: Cow::from(""),
abi: Cow::from(""),
env: "".into(),
abi: "".into(),
vendor: "unknown".into(),
linker_flavor: LinkerFlavor::Gcc,
linker: option_env!("CFG_DEFAULT_LINKER").map(|s| s.into()),
@ -1468,9 +1467,9 @@ impl Default for TargetOptions {
pre_link_args: LinkArgs::new(),
post_link_args: LinkArgs::new(),
link_script: None,
asm_args: Cow::Borrowed(&[]),
asm_args: cvs![],
cpu: "generic".into(),
features: Cow::from(""),
features: "".into(),
dynamic_linking: false,
only_cdylib: false,
executables: false,
@ -1482,7 +1481,7 @@ impl Default for TargetOptions {
function_sections: true,
dll_prefix: "lib".into(),
dll_suffix: ".so".into(),
exe_suffix: Cow::from(""),
exe_suffix: "".into(),
staticlib_prefix: "lib".into(),
staticlib_suffix: ".a".into(),
families: cvs![],
@ -1511,15 +1510,15 @@ impl Default for TargetOptions {
late_link_args: LinkArgs::new(),
late_link_args_dynamic: LinkArgs::new(),
late_link_args_static: LinkArgs::new(),
link_env: Cow::Borrowed(&[]),
link_env_remove: Cow::Borrowed(&[]),
link_env: cvs![],
link_env_remove: cvs![],
archive_format: "gnu".into(),
main_needs_argc_argv: true,
allow_asm: true,
has_thread_local: false,
obj_is_bitcode: false,
forces_embed_bitcode: false,
bitcode_llvm_cmdline: Cow::from(""),
bitcode_llvm_cmdline: "".into(),
min_atomic_width: None,
max_atomic_width: None,
atomic_cas: true,

View file

@ -1,5 +1,4 @@
use super::cvs;
use crate::spec::{PanicStrategy, RelocModel, Target, TargetOptions};
use crate::spec::{cvs, PanicStrategy, RelocModel, Target, TargetOptions};
pub fn target() -> Target {
Target {

View file

@ -1,6 +1,4 @@
use crate::spec::{RelroLevel, TargetOptions};
use super::cvs;
use crate::spec::{cvs, RelroLevel, TargetOptions};
pub fn opts() -> TargetOptions {
TargetOptions {

View file

@ -1,6 +1,4 @@
use crate::spec::{FramePointer, RelroLevel, TargetOptions};
use super::cvs;
use crate::spec::{cvs, FramePointer, RelroLevel, TargetOptions};
pub fn opts() -> TargetOptions {
TargetOptions {

View file

@ -1,6 +1,4 @@
use crate::spec::{RelroLevel, TargetOptions};
use super::cvs;
use crate::spec::{cvs, RelroLevel, TargetOptions};
pub fn opts() -> TargetOptions {
TargetOptions {

View file

@ -1,7 +1,5 @@
use crate::spec::{cvs, Target, TargetOptions};
use crate::spec::{LinkerFlavor, PanicStrategy, RelocModel};
use crate::spec::{Target, TargetOptions};
use super::cvs;
pub fn target() -> Target {
Target {

View file

@ -1,6 +1,4 @@
use crate::spec::TargetOptions;
use super::cvs;
use crate::spec::{cvs, TargetOptions};
pub fn opts() -> TargetOptions {
TargetOptions {

View file

@ -8,8 +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};
use crate::spec::{cvs, LinkerFlavor, Target, TargetOptions};
pub fn target() -> Target {
Target {

View file

@ -1,6 +1,4 @@
use crate::spec::TargetOptions;
use super::cvs;
use crate::spec::{cvs, TargetOptions};
pub fn opts() -> TargetOptions {
TargetOptions {

View file

@ -1,7 +1,5 @@
use crate::spec::crt_objects::{self, CrtObjectsFallback};
use crate::spec::{LinkArgs, LinkerFlavor, LldFlavor, TargetOptions};
use super::cvs;
use crate::spec::{cvs, LinkArgs, LinkerFlavor, LldFlavor, TargetOptions};
pub fn opts() -> TargetOptions {
let mut pre_link_args = LinkArgs::new();

View file

@ -1,6 +1,4 @@
use crate::spec::TargetOptions;
use super::cvs;
use crate::spec::{cvs, TargetOptions};
pub fn opts() -> TargetOptions {
let base = super::msvc_base::opts();