Skip to content

Commit bdaa0b1

Browse files
author
Phil Cummins
committed
refactors linking
1 parent 528a3a0 commit bdaa0b1

File tree

2 files changed

+33
-25
lines changed

2 files changed

+33
-25
lines changed

src/lib.rs

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ use {
1111
serde::Deserialize,
1212
std::{
1313
collections::HashMap,
14-
env, fs,
15-
io::Cursor,
14+
fs,
1615
iter,
1716
ops::Deref,
1817
path::{Path, PathBuf},
@@ -34,6 +33,7 @@ mod abi;
3433
mod bindgen;
3534
mod bindings;
3635
pub mod command;
36+
mod link;
3737
mod prelink;
3838
#[cfg(feature = "pyo3")]
3939
mod python;
@@ -326,29 +326,7 @@ pub async fn componentize(
326326
dl_openable: false,
327327
});
328328

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()?;
329+
let component = link::link_libraries(&libraries)?;
352330

353331
let stubbed_component = if stub_wasi {
354332
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+
return linker.encode().map_err(|e| anyhow::anyhow!(e));
30+
}

0 commit comments

Comments
 (0)