Skip to content

Commit

Permalink
refactors linking (#130)
Browse files Browse the repository at this point in the history
* refactors linking

* remove , anyhow::Error from prelink

* fmt

* formatting

* nightly compiler/lint fixes (#129)

* test

* trigger test workflow

* remove lint from workflow

* lint/compiler fixes

* re-adds readme

---------

Co-authored-by: Phil Cummins <philip.cummins@bsc.es>

---------

Co-authored-by: Phil Cummins <philip.cummins@bsc.es>
  • Loading branch information
philrhc and Phil Cummins authored Dec 6, 2024
1 parent 0041761 commit c66d95b
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 34 deletions.
29 changes: 3 additions & 26 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ use {
serde::Deserialize,
std::{
collections::HashMap,
env, fs,
io::Cursor,
iter,
fs, iter,
ops::Deref,
path::{Path, PathBuf},
str,
Expand All @@ -34,6 +32,7 @@ mod abi;
mod bindgen;
mod bindings;
pub mod command;
mod link;
mod prelink;
#[cfg(feature = "pyo3")]
mod python;
Expand Down Expand Up @@ -326,29 +325,7 @@ pub async fn componentize(
dl_openable: false,
});

// Link all the libraries (including any native extensions) into a single component.
let mut linker = wit_component::Linker::default()
.validate(true)
.use_built_in_libdl(true);

for Library {
name,
module,
dl_openable,
} in &libraries
{
linker = linker.library(name, module, *dl_openable)?;
}

linker = linker.adapter(
"wasi_snapshot_preview1",
&zstd::decode_all(Cursor::new(include_bytes!(concat!(
env!("OUT_DIR"),
"/wasi_snapshot_preview1.reactor.wasm.zst"
))))?,
)?;

let component = linker.encode()?;
let component = link::link_libraries(&libraries)?;

let stubbed_component = if stub_wasi {
stubwasi::link_stub_modules(libraries)?
Expand Down
30 changes: 30 additions & 0 deletions src/link.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use std::io::Cursor;

use anyhow::Result;

use crate::Library;

pub fn link_libraries(libraries: &[Library]) -> Result<Vec<u8>> {
let mut linker = wit_component::Linker::default()
.validate(true)
.use_built_in_libdl(true);

for Library {
name,
module,
dl_openable,
} in libraries
{
linker = linker.library(name, module, *dl_openable)?;
}

linker = linker.adapter(
"wasi_snapshot_preview1",
&zstd::decode_all(Cursor::new(include_bytes!(concat!(
env!("OUT_DIR"),
"/wasi_snapshot_preview1.reactor.wasm.zst"
))))?,
)?;

linker.encode().map_err(|e| anyhow::anyhow!(e))
}
14 changes: 6 additions & 8 deletions src/prelink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use std::{
collections::{HashMap, HashSet},
fs::{self},
io::{self, Cursor},
io::Cursor,
ops::Deref,
path::{Path, PathBuf},
};
Expand All @@ -21,7 +21,7 @@ static NATIVE_EXTENSION_SUFFIX: &str = ".cpython-312-wasm32-wasi.so";
type ConfigsMatchedWorlds<'a> =
IndexMap<String, (ConfigContext<ComponentizePyConfig>, Option<&'a str>)>;

pub fn embedded_python_standard_library() -> Result<TempDir, io::Error> {
pub fn embedded_python_standard_library() -> Result<TempDir> {
// Untar the embedded copy of the Python standard library into a temporary directory
let stdlib = tempfile::tempdir()?;

Expand All @@ -35,7 +35,7 @@ pub fn embedded_python_standard_library() -> Result<TempDir, io::Error> {
Ok(stdlib)
}

pub fn embedded_helper_utils() -> Result<TempDir, io::Error> {
pub fn embedded_helper_utils() -> Result<TempDir> {
// Untar the embedded copy of helper utilities into a temporary directory
let bundled = tempfile::tempdir()?;

Expand All @@ -49,9 +49,7 @@ pub fn embedded_helper_utils() -> Result<TempDir, io::Error> {
Ok(bundled)
}

pub fn bundle_libraries(
library_path: Vec<(&str, Vec<PathBuf>)>,
) -> Result<Vec<Library>, anyhow::Error> {
pub fn bundle_libraries(library_path: Vec<(&str, Vec<PathBuf>)>) -> Result<Vec<Library>> {
let mut libraries = vec![
Library {
name: "libcomponentize_py_runtime.so".into(),
Expand Down Expand Up @@ -152,7 +150,7 @@ pub fn search_for_libraries_and_configs<'a>(
python_path: &'a Vec<&'a str>,
module_worlds: &'a [(&'a str, &'a str)],
world: Option<&'a str>,
) -> Result<(ConfigsMatchedWorlds<'a>, Vec<Library>), anyhow::Error> {
) -> Result<(ConfigsMatchedWorlds<'a>, Vec<Library>)> {
let mut raw_configs: Vec<ConfigContext<RawComponentizePyConfig>> = Vec::new();
let mut library_path: Vec<(&str, Vec<PathBuf>)> = Vec::with_capacity(python_path.len());
for path in python_path {
Expand Down Expand Up @@ -219,7 +217,7 @@ fn search_directory(
libraries: &mut Vec<PathBuf>,
configs: &mut Vec<ConfigContext<RawComponentizePyConfig>>,
modules_seen: &mut HashSet<String>,
) -> Result<(), anyhow::Error> {
) -> Result<()> {
if path.is_dir() {
for entry in fs::read_dir(path).with_context(|| path.display().to_string())? {
search_directory(root, &entry?.path(), libraries, configs, modules_seen)?;
Expand Down

0 comments on commit c66d95b

Please sign in to comment.