|
| 1 | +use std::borrow::Cow; |
| 2 | +use std::collections::BTreeMap; |
| 3 | +use std::env; |
| 4 | +use std::path::{Path, PathBuf}; |
| 5 | + |
| 6 | +use crate::spec::{ |
| 7 | + Cc, CheckEnvironment, LinkerFlavor, Lld, PanicStrategy, RelocModel, RelroLevel, SplitDebuginfo, |
| 8 | + StackProbeType, TargetOptions, cvs, |
| 9 | +}; |
| 10 | + |
| 11 | +/// Strip mutability from &mut str. |
| 12 | +fn immutable(value: &mut str) -> &str { |
| 13 | + let immutable = value; |
| 14 | + immutable |
| 15 | +} |
| 16 | + |
| 17 | +macro_rules! cow_format { |
| 18 | + ($arg:expr) => { |
| 19 | + // Cow::from works with &str but not with &mut str. |
| 20 | + Cow::from(immutable(Box::leak(format!($arg).into_boxed_str()))) |
| 21 | + }; |
| 22 | +} |
| 23 | + |
| 24 | +struct EnvVar<'a> { |
| 25 | + name: &'a str, |
| 26 | + description: &'a str, |
| 27 | +} |
| 28 | + |
| 29 | +static ENV_PREFIX_VAR: EnvVar<'_> = |
| 30 | + EnvVar { name: "ENV_PREFIX", description: "your LynxOS-178 Environment directory" }; |
| 31 | +static GCC_DIR_VAR: EnvVar<'_> = |
| 32 | + EnvVar { name: "GCC_DIR", description: "your LynxOS-178 GCC compiler directory" }; |
| 33 | + |
| 34 | +struct LynxosPath<'a> { |
| 35 | + env_var: &'a EnvVar<'a>, |
| 36 | + rel_path: &'a str, |
| 37 | +} |
| 38 | + |
| 39 | +impl LynxosPath<'_> { |
| 40 | + fn path(&self) -> Result<PathBuf, String> { |
| 41 | + let var_value = env::var(self.env_var.name).map_err(|_| { |
| 42 | + format!("Set {} to point at {}.", self.env_var.name, self.env_var.description) |
| 43 | + })?; |
| 44 | + Ok(Path::new(var_value.as_str()).join(self.rel_path)) |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +// Paths relative to ENV_PREFIX |
| 49 | +static LINK_DIRS: [LynxosPath<'static>; 3] = [ |
| 50 | + LynxosPath { env_var: &GCC_DIR_VAR, rel_path: "" }, |
| 51 | + LynxosPath { env_var: &ENV_PREFIX_VAR, rel_path: "lib/" }, |
| 52 | + LynxosPath { env_var: &ENV_PREFIX_VAR, rel_path: "usr/lib/" }, |
| 53 | +]; |
| 54 | +static LINK_LIBS: [LynxosPath<'static>; 5] = [ |
| 55 | + LynxosPath { env_var: &ENV_PREFIX_VAR, rel_path: "lib/crt1.o" }, |
| 56 | + LynxosPath { env_var: &ENV_PREFIX_VAR, rel_path: "lib/crti.o" }, |
| 57 | + LynxosPath { env_var: &GCC_DIR_VAR, rel_path: "crtbegin.o" }, |
| 58 | + LynxosPath { env_var: &GCC_DIR_VAR, rel_path: "crtend.o" }, |
| 59 | + LynxosPath { env_var: &ENV_PREFIX_VAR, rel_path: "lib/crtn.o" }, |
| 60 | +]; |
| 61 | + |
| 62 | +pub(crate) fn check_environment() -> Result<(), String> { |
| 63 | + for rel_dir in &LINK_DIRS { |
| 64 | + let dir = rel_dir.path()?; |
| 65 | + if !dir.is_dir() { |
| 66 | + return Err(format!( |
| 67 | + "{} is not a directory. Does {} point at {}?", |
| 68 | + dir.display(), |
| 69 | + rel_dir.env_var.name, |
| 70 | + rel_dir.env_var.description |
| 71 | + )); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + for rel_lib in &LINK_LIBS { |
| 76 | + let lib = rel_lib.path()?; |
| 77 | + if !lib.is_file() { |
| 78 | + return Err(format!( |
| 79 | + "{} is not a file. Does {} point at {}?", |
| 80 | + lib.display(), |
| 81 | + rel_lib.env_var.name, |
| 82 | + rel_lib.env_var.description |
| 83 | + )); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + Ok(()) |
| 88 | +} |
| 89 | + |
| 90 | +pub(crate) fn opts() -> TargetOptions { |
| 91 | + let linker_flavor = LinkerFlavor::Gnu(Cc::No, Lld::No); |
| 92 | + let other_linker_flavor = LinkerFlavor::Gnu(Cc::No, Lld::Yes); |
| 93 | + let env_prefix = env::var("ENV_PREFIX").unwrap_or("ENV_PREFIX".into()); |
| 94 | + let cdk_path = format!("{env_prefix}/cdk/linux-elf-x86_64"); |
| 95 | + |
| 96 | + let mut pre_link_args_v = vec![Cow::from("-m"), Cow::from("elf_x86_64_lynx178")]; |
| 97 | + |
| 98 | + // If there's an error determining paths, just use an empty path buf. This |
| 99 | + // code happens before check_environment. That will get called later in a |
| 100 | + // place where errors can be returned to the user. |
| 101 | + |
| 102 | + for link_path in LINK_DIRS.iter().map(|p| p.path().unwrap_or(PathBuf::new())) { |
| 103 | + let printable_link_path = link_path.display(); |
| 104 | + pre_link_args_v.push(cow_format!("-L{printable_link_path}")); |
| 105 | + } |
| 106 | + |
| 107 | + let mut post_link_args_v = Vec::new(); |
| 108 | + for path in LINK_LIBS.iter().map(|p| p.path().unwrap_or(PathBuf::new())) { |
| 109 | + let printable_path = path.display(); |
| 110 | + post_link_args_v.push(cow_format!("{printable_path}")); |
| 111 | + } |
| 112 | + post_link_args_v.push(Cow::from("-lc")); |
| 113 | + post_link_args_v.push(Cow::from("-lm")); |
| 114 | + post_link_args_v.push(Cow::from("-lpthread")); |
| 115 | + post_link_args_v.push(Cow::from("-lgcc")); |
| 116 | + |
| 117 | + TargetOptions { |
| 118 | + os: "lynxos_178".into(), |
| 119 | + dynamic_linking: false, |
| 120 | + families: cvs!["unix"], |
| 121 | + position_independent_executables: false, |
| 122 | + static_position_independent_executables: false, |
| 123 | + relro_level: RelroLevel::Full, |
| 124 | + has_thread_local: false, |
| 125 | + crt_static_respected: true, |
| 126 | + panic_strategy: PanicStrategy::Abort, |
| 127 | + // Don't rely on the path to find the correct ld. |
| 128 | + linker: Some(cow_format!("{cdk_path}/bin/ld")), |
| 129 | + linker_flavor, |
| 130 | + eh_frame_header: false, // GNU ld (GNU Binutils) 2.37.50 does not support --eh-frame-hdr |
| 131 | + max_atomic_width: Some(64), |
| 132 | + pre_link_args: BTreeMap::from([ |
| 133 | + (linker_flavor, pre_link_args_v.clone()), |
| 134 | + (other_linker_flavor, pre_link_args_v), |
| 135 | + ]), |
| 136 | + post_link_args: BTreeMap::from([ |
| 137 | + (linker_flavor, post_link_args_v.clone()), |
| 138 | + (other_linker_flavor, post_link_args_v), |
| 139 | + ]), |
| 140 | + supported_split_debuginfo: Cow::Borrowed(&[ |
| 141 | + SplitDebuginfo::Packed, |
| 142 | + SplitDebuginfo::Unpacked, |
| 143 | + SplitDebuginfo::Off, |
| 144 | + ]), |
| 145 | + relocation_model: RelocModel::Static, |
| 146 | + stack_probes: StackProbeType::Inline, |
| 147 | + check_environment: CheckEnvironment::Lynxos178, |
| 148 | + ..Default::default() |
| 149 | + } |
| 150 | +} |
0 commit comments