Skip to content

Commit c66d95b

Browse files
philrhcPhil Cummins
andauthored
refactors linking (#130)
* 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>
1 parent 0041761 commit c66d95b

File tree

3 files changed

+39
-34
lines changed

3 files changed

+39
-34
lines changed

src/lib.rs

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@ use {
1111
serde::Deserialize,
1212
std::{
1313
collections::HashMap,
14-
env, fs,
15-
io::Cursor,
16-
iter,
14+
fs, iter,
1715
ops::Deref,
1816
path::{Path, PathBuf},
1917
str,
@@ -34,6 +32,7 @@ mod abi;
3432
mod bindgen;
3533
mod bindings;
3634
pub mod command;
35+
mod link;
3736
mod prelink;
3837
#[cfg(feature = "pyo3")]
3938
mod python;
@@ -326,29 +325,7 @@ pub async fn componentize(
326325
dl_openable: false,
327326
});
328327

329-
// Link all the libraries (including any native extensions) into a single component.
330-
let mut linker = wit_component::Linker::default()
331-
.validate(true)
332-
.use_built_in_libdl(true);
333-
334-
for Library {
335-
name,
336-
module,
337-
dl_openable,
338-
} in &libraries
339-
{
340-
linker = linker.library(name, module, *dl_openable)?;
341-
}
342-
343-
linker = linker.adapter(
344-
"wasi_snapshot_preview1",
345-
&zstd::decode_all(Cursor::new(include_bytes!(concat!(
346-
env!("OUT_DIR"),
347-
"/wasi_snapshot_preview1.reactor.wasm.zst"
348-
))))?,
349-
)?;
350-
351-
let component = linker.encode()?;
328+
let component = link::link_libraries(&libraries)?;
352329

353330
let stubbed_component = if stub_wasi {
354331
stubwasi::link_stub_modules(libraries)?

src/link.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
use std::io::Cursor;
2+
3+
use anyhow::Result;
4+
5+
use crate::Library;
6+
7+
pub fn link_libraries(libraries: &[Library]) -> Result<Vec<u8>> {
8+
let mut linker = wit_component::Linker::default()
9+
.validate(true)
10+
.use_built_in_libdl(true);
11+
12+
for Library {
13+
name,
14+
module,
15+
dl_openable,
16+
} in libraries
17+
{
18+
linker = linker.library(name, module, *dl_openable)?;
19+
}
20+
21+
linker = linker.adapter(
22+
"wasi_snapshot_preview1",
23+
&zstd::decode_all(Cursor::new(include_bytes!(concat!(
24+
env!("OUT_DIR"),
25+
"/wasi_snapshot_preview1.reactor.wasm.zst"
26+
))))?,
27+
)?;
28+
29+
linker.encode().map_err(|e| anyhow::anyhow!(e))
30+
}

src/prelink.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use std::{
44
collections::{HashMap, HashSet},
55
fs::{self},
6-
io::{self, Cursor},
6+
io::Cursor,
77
ops::Deref,
88
path::{Path, PathBuf},
99
};
@@ -21,7 +21,7 @@ static NATIVE_EXTENSION_SUFFIX: &str = ".cpython-312-wasm32-wasi.so";
2121
type ConfigsMatchedWorlds<'a> =
2222
IndexMap<String, (ConfigContext<ComponentizePyConfig>, Option<&'a str>)>;
2323

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

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

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

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

52-
pub fn bundle_libraries(
53-
library_path: Vec<(&str, Vec<PathBuf>)>,
54-
) -> Result<Vec<Library>, anyhow::Error> {
52+
pub fn bundle_libraries(library_path: Vec<(&str, Vec<PathBuf>)>) -> Result<Vec<Library>> {
5553
let mut libraries = vec![
5654
Library {
5755
name: "libcomponentize_py_runtime.so".into(),
@@ -152,7 +150,7 @@ pub fn search_for_libraries_and_configs<'a>(
152150
python_path: &'a Vec<&'a str>,
153151
module_worlds: &'a [(&'a str, &'a str)],
154152
world: Option<&'a str>,
155-
) -> Result<(ConfigsMatchedWorlds<'a>, Vec<Library>), anyhow::Error> {
153+
) -> Result<(ConfigsMatchedWorlds<'a>, Vec<Library>)> {
156154
let mut raw_configs: Vec<ConfigContext<RawComponentizePyConfig>> = Vec::new();
157155
let mut library_path: Vec<(&str, Vec<PathBuf>)> = Vec::with_capacity(python_path.len());
158156
for path in python_path {
@@ -219,7 +217,7 @@ fn search_directory(
219217
libraries: &mut Vec<PathBuf>,
220218
configs: &mut Vec<ConfigContext<RawComponentizePyConfig>>,
221219
modules_seen: &mut HashSet<String>,
222-
) -> Result<(), anyhow::Error> {
220+
) -> Result<()> {
223221
if path.is_dir() {
224222
for entry in fs::read_dir(path).with_context(|| path.display().to_string())? {
225223
search_directory(root, &entry?.path(), libraries, configs, modules_seen)?;

0 commit comments

Comments
 (0)