move build_file and related functions to roc_build

This is needed of glue to be able to call build_file.
Also does some other changes to avoid circular dependencies.
This commit is contained in:
Brendan Hansknecht 2023-03-04 10:00:31 -08:00
parent 0ba7b8b2fb
commit 7a944113a0
No known key found for this signature in database
GPG key ID: 0EA784685083E75B
13 changed files with 776 additions and 814 deletions

View file

@ -1637,8 +1637,8 @@ fn surgery_elf_help(
mod tests {
use super::*;
use crate::preprocessed_host_filename;
use indoc::indoc;
use roc_build::link::preprocessed_host_filename;
use target_lexicon::Triple;
const ELF64_DYNHOST: &[u8] = include_bytes!("../dynhost_benchmarks_elf64") as &[_];

View file

@ -5,12 +5,11 @@
//! practical to use a regular linker.
use memmap2::{Mmap, MmapMut};
use object::Object;
use roc_build::link::{get_target_triple_str, rebuild_host, LinkType};
use roc_error_macros::internal_error;
use roc_load::{EntryPoint, ExecutionMode, LoadConfig, Threading};
use roc_mono::ir::OptLevel;
use roc_packaging::cache::RocCacheDir;
use roc_reporting::report::{RenderTarget, DEFAULT_PALETTE};
use roc_target::get_target_triple_str;
use std::cmp::Ordering;
use std::mem;
use std::path::{Path, PathBuf};
@ -23,6 +22,14 @@ mod pe;
mod generate_dylib;
mod metadata;
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum LinkType {
// These numbers correspond to the --lib and --no-link flags
Executable = 0,
Dylib = 1,
None = 2,
}
pub fn supported(link_type: LinkType, target: &Triple) -> bool {
if let LinkType::Executable = link_type {
match target {
@ -54,43 +61,10 @@ pub fn supported(link_type: LinkType, target: &Triple) -> bool {
}
}
pub fn build_and_preprocess_host(
opt_level: OptLevel,
target: &Triple,
platform_main_roc: &Path,
preprocessed_host_path: &Path,
exposed_to_host: Vec<String>,
exported_closure_types: Vec<String>,
) {
let stub_lib = if let target_lexicon::OperatingSystem::Windows = target.operating_system {
platform_main_roc.with_file_name("libapp.dll")
} else {
platform_main_roc.with_file_name("libapp.so")
};
pub const PRECOMPILED_HOST_EXT: &str = "rh1"; // Short for "roc host version 1" (so we can change format in the future)
let dynhost = if let target_lexicon::OperatingSystem::Windows = target.operating_system {
platform_main_roc.with_file_name("dynhost.exe")
} else {
platform_main_roc.with_file_name("dynhost")
};
let stub_dll_symbols = make_stub_dll_symbols(exposed_to_host, exported_closure_types);
generate_dynamic_lib(target, &stub_dll_symbols, &stub_lib);
rebuild_host(opt_level, target, platform_main_roc, Some(&stub_lib));
let metadata = platform_main_roc.with_file_name(metadata_file_name(target));
// let prehost = host_input_path.with_file_name(preprocessed_host_filename(target).unwrap());
preprocess(
target,
&dynhost,
&metadata,
preprocessed_host_path,
&stub_lib,
&stub_dll_symbols,
false,
false,
)
pub fn preprocessed_host_filename(target: &Triple) -> Option<String> {
roc_target::get_target_triple_str(target).map(|x| format!("{}.{}", x, PRECOMPILED_HOST_EXT))
}
fn metadata_file_name(target: &Triple) -> String {
@ -170,6 +144,24 @@ pub fn generate_stub_lib(
Ok(0)
}
pub fn generate_stub_lib_from_loaded(
target: &Triple,
platform_main_roc: &Path,
exposed_to_host: Vec<String>,
exported_closure_types: Vec<String>,
) -> (PathBuf, Vec<String>) {
let stub_lib = if let target_lexicon::OperatingSystem::Windows = target.operating_system {
platform_main_roc.with_file_name("libapp.dll")
} else {
platform_main_roc.with_file_name("libapp.so")
};
let stub_dll_symbols = make_stub_dll_symbols(exposed_to_host, exported_closure_types);
generate_dynamic_lib(target, &stub_dll_symbols, &stub_lib);
(stub_lib, stub_dll_symbols)
}
fn make_stub_dll_symbols(
exposed_to_host: Vec<String>,
exported_closure_types: Vec<String>,
@ -332,6 +324,32 @@ fn stub_lib_is_up_to_date(target: &Triple, stub_lib_path: &Path, custom_names: &
it1.eq(it2)
}
pub fn preprocess_host(
target: &Triple,
platform_main_roc: &Path,
preprocessed_path: &Path,
shared_lib: &Path,
stub_dll_symbols: &[String],
) {
let metadata_path = platform_main_roc.with_file_name(metadata_file_name(target));
let host_exe_path = if let target_lexicon::OperatingSystem::Windows = target.operating_system {
platform_main_roc.with_file_name("dynhost.exe")
} else {
platform_main_roc.with_file_name("dynhost")
};
preprocess(
target,
&host_exe_path,
&metadata_path,
preprocessed_path,
shared_lib,
stub_dll_symbols,
false,
false,
)
}
/// Constructs a `metadata::Metadata` from a host executable binary, and writes it to disk
#[allow(clippy::too_many_arguments)]
fn preprocess(

View file

@ -1357,8 +1357,8 @@ mod test {
use object::read::pe::PeFile64;
use object::{pe, LittleEndian as LE, Object};
use crate::preprocessed_host_filename;
use indoc::indoc;
use roc_build::link::preprocessed_host_filename;
use target_lexicon::Triple;
use super::*;