replace stray strings with impl on Target

This commit is contained in:
Luke Boswell 2024-07-09 14:39:36 +10:00
parent 753cfb4285
commit 85447ec2cc
No known key found for this signature in database
GPG key ID: F6DB3C9DB47377B0
7 changed files with 58 additions and 54 deletions

View file

@ -169,14 +169,13 @@ fn main() -> io::Result<()> {
let verbose_and_time = matches.get_one::<bool>(roc_cli::FLAG_VERBOSE).unwrap();
let preprocessed_path = platform_path.with_file_name(format!("{}.rh", target));
let metadata_path =
platform_path.with_file_name(roc_linker::metadata_file_name(target));
let preprocessed_path = platform_path.with_file_name(target.prebuilt_surgical_host());
let metadata_path = platform_path.with_file_name(target.metadata_file_name());
roc_linker::preprocess_host(
target,
host_path,
&metadata_path,
metadata_path.as_path(),
preprocessed_path.as_path(),
dylib_path,
*verbose_and_time,

View file

@ -771,7 +771,7 @@ fn build_loaded_file<'a>(
let output_exe_path = get_exe_path(
out_path,
&app_module_path,
app_module_path.as_path(),
target,
linking_strategy,
link_type,
@ -866,8 +866,7 @@ fn build_loaded_file<'a>(
match (linking_strategy, link_type) {
(LinkingStrategy::Surgical, _) => {
let metadata_file =
platform_main_roc.with_file_name(roc_linker::metadata_file_name(target));
let metadata_file = platform_main_roc.with_file_name(target.metadata_file_name());
roc_linker::link_preprocessed_host(
target,
@ -950,7 +949,7 @@ fn build_loaded_file<'a>(
/// use that directory, but use the app module's filename for the filename.
fn get_exe_path(
out_path: Option<&Path>,
app_module_path: &PathBuf,
app_module_path: &Path,
target: Target,
linking_strategy: LinkingStrategy,
link_type: LinkType,
@ -986,7 +985,7 @@ fn get_exe_path(
path.to_path_buf()
}
}
None => with_output_extension(&app_module_path, target, linking_strategy, link_type),
None => with_output_extension(app_module_path, target, linking_strategy, link_type),
}
}
@ -1000,8 +999,7 @@ fn get_host_path(
output_exe_path: &Path,
dll_stub_symbols: Vec<String>,
) -> PathBuf {
let preprocessed_host_path =
platform_main_roc.with_file_name(roc_linker::preprocessed_host_filename(target));
let preprocessed_host_path = platform_main_roc.with_file_name(target.prebuilt_surgical_host());
if build_host_requested {
let rebuild_thread = match linking_strategy {
@ -1012,9 +1010,9 @@ fn get_host_path(
preprocessed_host_path.to_owned(),
),
LinkingStrategy::Surgical => {
let preprocessed_path = platform_main_roc.with_file_name(format!("{}.rh", target));
let metadata_path =
platform_main_roc.with_file_name(roc_linker::metadata_file_name(target));
let preprocessed_path =
platform_main_roc.with_file_name(target.prebuilt_surgical_host());
let metadata_path = platform_main_roc.with_file_name(target.metadata_file_name());
spawn_surgical_host_build_thread(
code_gen_options.opt_level,
@ -1139,7 +1137,7 @@ fn spawn_surgical_host_build_thread(
// Copy preprocessed host to executable location.
// The surgical linker will modify that copy in-place.
std::fs::copy(&preprocessed_path, output_exe_path.to_owned()).unwrap();
std::fs::copy(&preprocessed_path, &output_exe_path).unwrap();
(start.elapsed().as_millis(), output_exe_path)
})

View file

@ -134,6 +134,7 @@ impl Target {
self.architecture().ptr_alignment_bytes()
}
// file extension for an object file
pub const fn object_file_ext(&self) -> &str {
use Target::*;
match self {
@ -143,6 +144,7 @@ impl Target {
}
}
// file extension for a static library file
pub const fn static_library_file_ext(&self) -> &str {
use Target::*;
match self {
@ -152,6 +154,18 @@ impl Target {
}
}
// file extension for a dynamic/shared library file
pub const fn dynamic_library_file_ext(&self) -> &str {
use Target::*;
match self {
LinuxX32 | LinuxX64 | LinuxArm64 => "so",
MacX64 | MacArm64 => "dylib",
WinX32 | WinX64 | WinArm64 => "dll",
Wasm32 => "wasm",
}
}
// file extension for an executable file
pub const fn executable_file_ext(&self) -> Option<&str> {
use Target::*;
match self {
@ -161,32 +175,49 @@ impl Target {
}
}
// file name for a prebuilt host object file
// used for legacy linking
pub fn prebuilt_static_object(&self) -> String {
use Target::*;
match self {
LinuxX32 | LinuxX64 | LinuxArm64 | MacX64 | MacArm64 | Wasm32 => {
format!("{}-{}.o", self.operating_system(), self.architecture())
format!("{}.o", self)
}
WinX32 | WinX64 | WinArm64 => {
format!("{}-{}.obj", self.operating_system(), self.architecture())
format!("{}.obj", self)
}
}
}
// file name for a prebuilt host static library file
// used for legacy linking
pub fn prebuilt_static_library(&self) -> String {
use Target::*;
match self {
LinuxX32 | LinuxX64 | LinuxArm64 | MacX64 | MacArm64 | Wasm32 => {
format!("{}-{}.a", self.operating_system(), self.architecture())
format!("{}.a", self)
}
WinX32 | WinX64 | WinArm64 => {
format!("{}-{}.lib", self.operating_system(), self.architecture())
format!("{}.lib", self)
}
}
}
// file name for a preprocessed host executable file
// used for surgical linking
pub fn prebuilt_surgical_host(&self) -> String {
format!("{}-{}.rh", self.operating_system(), self.architecture())
format!("{}.rh", self) // short for roc host
}
// file name for a preprocessed host metadata file
// used for surgical linking
pub fn metadata_file_name(&self) -> String {
format!("metadata_{}.rm", self) // short for roc metadata
}
// file name for a stubbed app dynamic library file
pub fn stub_app_lib_file_name(&self) -> String {
format!("libapp.{}", self.dynamic_library_file_ext())
}
}

View file

@ -1706,8 +1706,6 @@ fn surgery_elf_help(
#[cfg(test)]
mod tests {
use super::*;
use crate::preprocessed_host_filename;
use indoc::indoc;
use roc_target::Target;
@ -1848,7 +1846,7 @@ mod tests {
panic!("zig build-exe failed");
}
let preprocessed_host_filename = dir.join(preprocessed_host_filename(target));
let preprocessed_host_filename = dir.join(target.prebuilt_surgical_host());
preprocess_elf_le(
&dir.join("host"),

View file

@ -45,16 +45,6 @@ pub fn supported(link_type: LinkType, target: Target) -> bool {
}
}
pub const PRECOMPILED_HOST_EXT: &str = "rh"; // Short for "roc host"
pub fn preprocessed_host_filename(target: Target) -> String {
format!("{target}.{PRECOMPILED_HOST_EXT}")
}
pub fn metadata_file_name(target: Target) -> String {
format!("metadata_{}.rm", target)
}
pub fn link_preprocessed_host(
target: Target,
roc_app_bytes: &[u8],
@ -118,14 +108,9 @@ pub fn generate_stub_lib(
};
if let EntryPoint::Executable { platform_path, .. } = &loaded.entry_point {
let stub_lib = if target.operating_system() == OperatingSystem::Windows {
platform_path.with_file_name("libapp.obj")
} else {
platform_path.with_file_name("libapp.so")
};
let stub_lib = platform_path.with_file_name(target.stub_app_lib_file_name());
let stub_dll_symbols = exposed_symbols.stub_dll_symbols();
generate_dynamic_lib(target, &stub_dll_symbols, &stub_lib);
generate_dynamic_lib(target, &stub_dll_symbols, stub_lib.as_path());
(platform_path.into(), stub_lib, stub_dll_symbols)
} else {
unreachable!();
@ -137,15 +122,10 @@ pub fn generate_stub_lib_from_loaded(
platform_main_roc: &Path,
stub_dll_symbols: &[String],
) -> PathBuf {
let stub_lib_path = if target.operating_system() == OperatingSystem::Windows {
platform_main_roc.with_file_name("libapp.dll")
} else {
platform_main_roc.with_file_name("libapp.so")
};
let stub_lib = platform_main_roc.with_file_name(target.stub_app_lib_file_name());
generate_dynamic_lib(target, stub_dll_symbols, stub_lib.as_path());
generate_dynamic_lib(target, stub_dll_symbols, &stub_lib_path);
stub_lib_path
stub_lib
}
pub struct ExposedSymbols {

View file

@ -1435,11 +1435,9 @@ const ___CHKSTK_MS: [u8; 48] = [
mod test {
const PE_DYNHOST: &[u8] = include_bytes!("../dynhost_benchmarks_windows.exe") as &[_];
use indoc::indoc;
use object::read::pe::PeFile64;
use object::{pe, LittleEndian as LE, Object};
use crate::preprocessed_host_filename;
use indoc::indoc;
use serial_test::serial;
use target_lexicon::Triple;
@ -1792,8 +1790,8 @@ mod test {
panic!("zig build-exe failed: {command_str}");
}
let preprocessed_host_filename =
dir.join(preprocessed_host_filename(Triple::host().into()));
let target: roc_target::Target = Triple::host().into();
let preprocessed_host_filename = dir.join(target.prebuilt_surgical_host());
preprocess_windows(
&dir.join("host.exe"),

View file

@ -42,7 +42,7 @@ fn build_host() {
Some(&stub_lib),
);
let preprocessed_path = platform_main_roc.with_file_name(format!("{}.rh", target));
let preprocessed_path = platform_main_roc.with_file_name(target.prebuilt_surgical_host());
let metadata_path = platform_main_roc.with_file_name(roc_linker::metadata_file_name(target));
roc_linker::preprocess_host(