Skip to content

Commit

Permalink
Add extraRuntimeLibraryPaths option to autowrap
Browse files Browse the repository at this point in the history
  • Loading branch information
kylewlacy committed Jul 11, 2024
1 parent ca14433 commit b2d2355
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
17 changes: 16 additions & 1 deletion crates/brioche-autowrap/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ pub struct DynamicLinkingConfig {
#[derive(Debug, Clone)]
pub struct DynamicBinaryConfig {
pub packed_executable: PathBuf,
pub extra_runtime_library_paths: Vec<PathBuf>,
pub dynamic_linking: DynamicLinkingConfig,
}

Expand Down Expand Up @@ -262,6 +263,10 @@ fn autowrap_dynamic_binary(
return Ok(false);
};

let output_path_parent = output_path
.parent()
.ok_or_eyre("could not get parent of output path")?;

let contents = std::fs::read(source_path)?;
let program_object = goblin::Object::parse(&contents)?;

Expand Down Expand Up @@ -336,11 +341,21 @@ fn autowrap_dynamic_binary(
.map_err(|_| eyre::eyre!("invalid UTF-8 in path"))
})
.collect::<eyre::Result<Vec<_>>>()?;
let runtime_library_dirs = dynamic_binary_config
.extra_runtime_library_paths
.iter()
.map(|path| {
let path = pathdiff::diff_paths(path, output_path_parent).ok_or_else(|| eyre::eyre!("failed to get relative path from output path {output_path_parent:?} to runtime library path {path:?}"))?;
<Vec<u8>>::from_path_buf(path)
.map_err(|_| eyre::eyre!("invalid UTF-8 in path"))
})
.collect::<eyre::Result<Vec<_>>>()?;

let pack = brioche_pack::Pack::LdLinux {
program,
interpreter,
library_dirs,
runtime_library_dirs: vec![],
runtime_library_dirs,
};

let packed_exec_path = &dynamic_binary_config.packed_executable;
Expand Down
1 change: 1 addition & 0 deletions crates/brioche-ld/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ fn run() -> eyre::Result<ExitCode> {
link_dependencies: vec![ld_resource_dir],
dynamic_binary: Some(brioche_autowrap::DynamicBinaryConfig {
packed_executable: packed_path,
extra_runtime_library_paths: vec![],
dynamic_linking: dynamic_linking_config.clone(),
}),
shared_library: Some(brioche_autowrap::SharedLibraryConfig {
Expand Down
24 changes: 22 additions & 2 deletions crates/brioche-packer/src/autowrap_template.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{
collections::{HashMap, HashSet},
path::PathBuf,
path::{Path, PathBuf},
};

use bstr::ByteVec as _;
Expand Down Expand Up @@ -72,7 +72,9 @@ impl AutowrapConfigTemplate {
.into_iter()
.map(|path| path.build(ctx))
.collect::<eyre::Result<Vec<_>>>()?;
let dynamic_binary = dynamic_binary.map(|opts| opts.build(ctx)).transpose()?;
let dynamic_binary = dynamic_binary
.map(|opts| opts.build(ctx, &recipe_path))
.transpose()?;
let shared_library = shared_library.map(|opts| opts.build(ctx)).transpose()?;
let script = script.map(|opts| opts.build(ctx)).transpose()?;
let rewrap = rewrap.map(|opts| opts.build());
Expand Down Expand Up @@ -163,6 +165,9 @@ impl DynamicLinkingConfigTemplate {
pub struct DynamicBinaryConfigTemplate {
packed_executable: TemplatePath,

#[serde(default)]
extra_runtime_library_paths: Vec<PathBuf>,

#[serde(flatten)]
dynamic_linking: DynamicLinkingConfigTemplate,
}
Expand All @@ -171,17 +176,32 @@ impl DynamicBinaryConfigTemplate {
fn build(
self,
ctx: &AutowrapConfigTemplateContext,
recipe_path: &Path,
) -> eyre::Result<brioche_autowrap::DynamicBinaryConfig> {
let Self {
packed_executable,
extra_runtime_library_paths,
dynamic_linking,
} = self;

let packed_executable = packed_executable.build(ctx)?;
let dynamic_linking = dynamic_linking.build(ctx)?;

let extra_runtime_library_paths = extra_runtime_library_paths
.into_iter()
.map(|path| {
let path = recipe_path.join(path);
eyre::ensure!(
path.starts_with(recipe_path),
"path {path:?} is not relative to recipe path",
);
eyre::Ok(path)
})
.collect::<eyre::Result<_>>()?;

Ok(brioche_autowrap::DynamicBinaryConfig {
packed_executable,
extra_runtime_library_paths,
dynamic_linking,
})
}
Expand Down

0 comments on commit b2d2355

Please sign in to comment.