Skip to content

Commit

Permalink
also add static lib support with fallback to object file
Browse files Browse the repository at this point in the history
  • Loading branch information
bhansconnect committed Nov 6, 2023
1 parent 7790bba commit 0b85424
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
20 changes: 13 additions & 7 deletions crates/compiler/build/src/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,20 @@ pub fn link(
}

/// Same format as the precompiled host filename, except with a file extension like ".o" or ".obj"
pub fn legacy_host_filename(target: &Triple) -> Option<String> {
pub fn legacy_host_file(target: &Triple, platform_main_roc: &Path) -> Option<PathBuf> {
let os = roc_target::OperatingSystem::from(target.operating_system);
let ext = os.object_file_ext();
let lib_ext = os.static_libary_file_ext();

Some(
roc_linker::preprocessed_host_filename(target)?
.replace(roc_linker::PRECOMPILED_HOST_EXT, ext),
)
let file_name = roc_linker::preprocessed_host_filename(target)?
.replace(roc_linker::PRECOMPILED_HOST_EXT, lib_ext);

let lib_path = platform_main_roc.with_file_name(file_name);
if lib_path.exists() {
Some(lib_path)
} else {
let obj_ext = os.object_file_ext();
Some(lib_path.with_extension(obj_ext))
}
}

// Attempts to find a file that is stored relative to the roc executable.
Expand Down Expand Up @@ -461,7 +467,7 @@ pub fn rebuild_host(
.with_file_name("dynhost")
.with_extension(executable_extension)
} else {
platform_main_roc.with_file_name(legacy_host_filename(target).unwrap())
legacy_host_file(target, &platform_main_roc).unwrap()
};

let env_path = env::var("PATH").unwrap_or_else(|_| "".to_string());
Expand Down
7 changes: 3 additions & 4 deletions crates/compiler/build/src/program.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::link::{
legacy_host_filename, link, preprocess_host_wasm32, rebuild_host, LinkType, LinkingStrategy,
legacy_host_file, link, preprocess_host_wasm32, rebuild_host, LinkType, LinkingStrategy,
};
use bumpalo::Bump;
use inkwell::memory_buffer::MemoryBuffer;
Expand Down Expand Up @@ -828,8 +828,7 @@ fn build_loaded_file<'a>(
// Fallback to legacy linking if the preprocessed host file does not exist, but a legacy host does exist.
let preprocessed_host_path = platform_main_roc
.with_file_name(roc_linker::preprocessed_host_filename(target).unwrap());
let legacy_host_path =
platform_main_roc.with_file_name(legacy_host_filename(target).unwrap());
let legacy_host_path = legacy_host_file(target, &platform_main_roc).unwrap();
if !preprocessed_host_path.exists() && legacy_host_path.exists() {
linking_strategy = LinkingStrategy::Legacy;
}
Expand All @@ -842,7 +841,7 @@ fn build_loaded_file<'a>(
// and has a file called "host.zig"
platform_main_roc.with_file_name("host.zig")
} else {
platform_main_roc.with_file_name(legacy_host_filename(target).unwrap())
legacy_host_file(target, &platform_main_roc).unwrap()
}
} else {
platform_main_roc.with_file_name(roc_linker::preprocessed_host_filename(target).unwrap())
Expand Down
8 changes: 8 additions & 0 deletions crates/compiler/roc_target/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ impl OperatingSystem {
}
}

pub const fn static_libary_file_ext(&self) -> &str {
match self {
OperatingSystem::Windows => "lib",
OperatingSystem::Unix => "a",
OperatingSystem::Wasi => "wasm",
}
}

pub const fn executable_file_ext(&self) -> Option<&str> {
match self {
OperatingSystem::Windows => Some("exe"),
Expand Down

0 comments on commit 0b85424

Please sign in to comment.