Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: adds link.rs #122

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
))))?,
)?;

return linker.encode().map_err(|e| anyhow::anyhow!(e));
}
17 changes: 9 additions & 8 deletions src/prelink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::{
path::{Path, PathBuf},
};

use anyhow::{anyhow, bail, Context, Result};
use anyhow::{anyhow, bail, Context, Error, Result};
use indexmap::IndexMap;
use tar::Archive;
use tempfile::TempDir;
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, Error> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could change the Result<_, Error> types to just Result<_> in this file since anyhow::Result defaults to anyhow::Error for its second type parameter.

// Untar the embedded copy of the Python standard library into a temporary directory
let stdlib = tempfile::tempdir()?;

Expand Down Expand Up @@ -50,8 +50,8 @@ pub fn embedded_helper_utils() -> Result<TempDir, io::Error> {
}

pub fn bundle_libraries(
library_path: Vec<(&str, Vec<PathBuf>)>,
) -> Result<Vec<Library>, anyhow::Error> {
library_path: Vec<(&str, Vec<std::path::PathBuf>)>,
) -> Result<Vec<Library>, Error> {
let mut libraries = vec![
Library {
name: "libcomponentize_py_runtime.so".into(),
Expand Down Expand Up @@ -152,9 +152,10 @@ 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> {
let mut raw_configs: Vec<ConfigContext<RawComponentizePyConfig>> = Vec::new();
let mut library_path: Vec<(&str, Vec<PathBuf>)> = Vec::with_capacity(python_path.len());
) -> Result<(ConfigsMatchedWorlds<'a>, Vec<Library>), Error> {
let mut raw_configs: Vec<crate::ConfigContext<crate::RawComponentizePyConfig>> = Vec::new();
let mut library_path: Vec<(&str, Vec<std::path::PathBuf>)> =
Vec::with_capacity(python_path.len());
for path in python_path {
let mut libraries = Vec::new();
search_directory(
Expand Down Expand Up @@ -219,7 +220,7 @@ fn search_directory(
libraries: &mut Vec<PathBuf>,
configs: &mut Vec<ConfigContext<RawComponentizePyConfig>>,
modules_seen: &mut HashSet<String>,
) -> Result<(), anyhow::Error> {
) -> Result<(), Error> {
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
6 changes: 3 additions & 3 deletions src/stubwasi.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::collections::HashMap;

use anyhow::{bail, Error};
use anyhow::{bail, Result};
use wasm_convert::IntoValType;
use wasm_encoder::{
CodeSection, ExportKind, ExportSection, Function, FunctionSection, Instruction as Ins, Module,
Expand All @@ -12,7 +12,7 @@ use crate::Library;

pub fn link_stub_modules(
libraries: Vec<Library>,
) -> Result<Option<(Vec<u8>, impl Fn(u32) -> u32)>, Error> {
) -> Result<Option<(Vec<u8>, impl Fn(u32) -> u32)>> {
let mut wasi_imports = HashMap::new();
let mut linker = wit_component::Linker::default()
.validate(true)
Expand Down Expand Up @@ -64,7 +64,7 @@ pub fn link_stub_modules(
fn add_wasi_imports<'a>(
module: &'a [u8],
imports: &mut HashMap<&'a str, HashMap<&'a str, FuncType>>,
) -> Result<(), Error> {
) -> Result<()> {
let mut types = Vec::new();
for payload in Parser::new(0).parse_all(module) {
match payload? {
Expand Down
Loading