2023-10-03 06:58:20 +03:00
|
|
|
use std::process::Command;
|
2023-10-06 07:12:31 +03:00
|
|
|
use std::{
|
|
|
|
fs, io,
|
|
|
|
io::{Read, Write},
|
|
|
|
};
|
2023-10-03 06:58:20 +03:00
|
|
|
|
|
|
|
fn run_command(cmd: &mut Command) -> io::Result<()> {
|
|
|
|
let status = cmd.status()?;
|
|
|
|
if status.success() {
|
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
Err(io::Error::new(io::ErrorKind::Other, "Command failed"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-09 03:55:19 +03:00
|
|
|
fn file_outdated<P1, P2>(input: P1, output: P2) -> io::Result<bool>
|
|
|
|
where
|
|
|
|
P1: AsRef<std::path::Path>,
|
|
|
|
P2: AsRef<std::path::Path>,
|
|
|
|
{
|
|
|
|
let out_meta = std::fs::metadata(output);
|
|
|
|
if let Ok(meta) = out_meta {
|
|
|
|
let output_mtime = meta.modified()?;
|
|
|
|
|
|
|
|
// if input file is more recent than our output, we are outdated
|
|
|
|
let input_meta = fs::metadata(input)?;
|
|
|
|
let input_mtime = input_meta.modified()?;
|
|
|
|
|
|
|
|
Ok(input_mtime > output_mtime)
|
|
|
|
} else {
|
|
|
|
// output file not found, we are outdated
|
|
|
|
Ok(true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-10 02:31:49 +03:00
|
|
|
fn build_app(target_path: &str, name: &str, parent_pkg_path: Option<&str>) {
|
|
|
|
let pwd = std::env::current_dir().unwrap();
|
|
|
|
|
2023-10-25 00:26:33 +03:00
|
|
|
let start = std::time::Instant::now();
|
2023-10-14 00:32:59 +03:00
|
|
|
|
2023-10-25 00:26:33 +03:00
|
|
|
// if and only if module's wit is outdated, re-set-up build environment
|
2023-10-10 02:31:49 +03:00
|
|
|
if file_outdated(
|
2023-11-02 23:42:18 +03:00
|
|
|
format!("{}/target.wasm", pwd.display()),
|
|
|
|
format!("{}/target/bindings/{}/target.wasm", target_path, name),
|
2023-10-10 02:31:49 +03:00
|
|
|
)
|
|
|
|
.unwrap_or(true)
|
|
|
|
{
|
|
|
|
// create target/bindings directory
|
2023-11-13 22:59:17 +03:00
|
|
|
fs::create_dir_all(format!("{}/target/bindings/{}", target_path, name,)).unwrap();
|
2023-10-10 02:31:49 +03:00
|
|
|
// copy newly-made target.wasm into target/bindings
|
2023-11-13 22:59:17 +03:00
|
|
|
run_command(Command::new("cp").args([
|
2023-10-10 02:31:49 +03:00
|
|
|
"target.wasm",
|
2023-10-10 02:32:23 +03:00
|
|
|
&format!("{}/target/bindings/{}/", target_path, name,),
|
2023-10-10 02:31:49 +03:00
|
|
|
]))
|
|
|
|
.unwrap();
|
|
|
|
// copy newly-made world into target/bindings
|
2023-11-13 22:59:17 +03:00
|
|
|
run_command(Command::new("cp").args([
|
2023-10-10 02:31:49 +03:00
|
|
|
"world",
|
2023-10-10 02:32:23 +03:00
|
|
|
&format!("{}/target/bindings/{}/", target_path, name,),
|
2023-10-10 02:31:49 +03:00
|
|
|
]))
|
|
|
|
.unwrap();
|
|
|
|
}
|
|
|
|
// Build the module targeting wasm32-wasi
|
2023-10-24 21:56:02 +03:00
|
|
|
let bash_build_path = &format!("{}/build.sh", target_path);
|
|
|
|
if std::path::Path::new(&bash_build_path).exists() {
|
|
|
|
let cwd = std::env::current_dir().unwrap();
|
|
|
|
std::env::set_current_dir(target_path).unwrap();
|
2023-11-13 22:59:17 +03:00
|
|
|
run_command(Command::new("/bin/bash").arg("build.sh")).unwrap();
|
2023-10-24 21:56:02 +03:00
|
|
|
std::env::set_current_dir(cwd).unwrap();
|
|
|
|
} else {
|
2023-11-13 22:59:17 +03:00
|
|
|
run_command(Command::new("cargo").args([
|
2023-10-25 06:42:47 +03:00
|
|
|
"+nightly",
|
2023-10-24 21:56:02 +03:00
|
|
|
"build",
|
|
|
|
"--release",
|
|
|
|
"--no-default-features",
|
|
|
|
&format!("--manifest-path={}/Cargo.toml", target_path),
|
|
|
|
"--target",
|
|
|
|
"wasm32-wasi",
|
|
|
|
]))
|
|
|
|
.unwrap();
|
|
|
|
}
|
2023-10-10 02:31:49 +03:00
|
|
|
// Adapt module to component with adapter based on wasi_snapshot_preview1.wasm
|
2023-11-13 22:59:17 +03:00
|
|
|
run_command(Command::new("wasm-tools").args([
|
2023-10-10 02:31:49 +03:00
|
|
|
"component",
|
|
|
|
"new",
|
|
|
|
&format!("{}/target/wasm32-wasi/release/{}.wasm", target_path, name),
|
|
|
|
"-o",
|
2023-10-10 02:32:23 +03:00
|
|
|
&format!(
|
|
|
|
"{}/target/wasm32-wasi/release/{}_adapted.wasm",
|
|
|
|
target_path, name
|
|
|
|
),
|
2023-10-10 02:31:49 +03:00
|
|
|
"--adapt",
|
|
|
|
&format!("{}/wasi_snapshot_preview1.wasm", pwd.display()),
|
|
|
|
]))
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
// Determine the destination for the .wasm file after embedding wit
|
|
|
|
let wasm_dest_path = if let Some(parent_pkg) = parent_pkg_path {
|
|
|
|
format!("{}/{}.wasm", parent_pkg, name)
|
|
|
|
} else {
|
|
|
|
let pkg_folder = format!("{}/pkg/", target_path);
|
2023-11-13 22:59:17 +03:00
|
|
|
let _ = run_command(Command::new("mkdir").args(["-p", &pkg_folder]));
|
2023-10-10 02:31:49 +03:00
|
|
|
format!("{}/{}.wasm", pkg_folder, name)
|
|
|
|
};
|
|
|
|
|
|
|
|
// Embed "wit" into the component
|
2023-11-13 22:59:17 +03:00
|
|
|
run_command(Command::new("wasm-tools").args([
|
2023-10-10 02:31:49 +03:00
|
|
|
"component",
|
|
|
|
"embed",
|
|
|
|
"wit",
|
|
|
|
"--world",
|
2023-11-03 06:43:10 +03:00
|
|
|
"process",
|
2023-10-10 02:32:23 +03:00
|
|
|
&format!(
|
|
|
|
"{}/target/wasm32-wasi/release/{}_adapted.wasm",
|
|
|
|
target_path, name
|
|
|
|
),
|
2023-10-10 02:31:49 +03:00
|
|
|
"-o",
|
|
|
|
&wasm_dest_path,
|
|
|
|
]))
|
|
|
|
.unwrap();
|
2023-10-25 00:26:33 +03:00
|
|
|
|
|
|
|
let end = std::time::Instant::now();
|
|
|
|
println!(
|
|
|
|
"cargo:warning=building {} took {:?}",
|
|
|
|
target_path,
|
|
|
|
end.duration_since(start)
|
|
|
|
);
|
2023-10-10 02:31:49 +03:00
|
|
|
}
|
|
|
|
|
2023-10-03 06:58:20 +03:00
|
|
|
fn main() {
|
|
|
|
if std::env::var("SKIP_BUILD_SCRIPT").is_ok() {
|
|
|
|
println!("Skipping build script");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let pwd = std::env::current_dir().unwrap();
|
2023-10-24 21:56:02 +03:00
|
|
|
|
2023-12-16 00:21:06 +03:00
|
|
|
// Pull wit from git repo
|
|
|
|
let wit_dir = pwd.join("wit");
|
|
|
|
fs::create_dir_all(&wit_dir).unwrap();
|
|
|
|
let wit_file = wit_dir.join("uqbar.wit");
|
|
|
|
if !wit_file.exists() {
|
|
|
|
let mut wit_file = std::fs::File::create(&wit_file).unwrap();
|
2023-12-18 20:45:07 +03:00
|
|
|
let uqbar_wit_url = "https://raw.githubusercontent.com/uqbar-dao/uqwit/master/uqbar.wit";
|
2023-12-16 00:21:06 +03:00
|
|
|
let mut response = reqwest::blocking::get(uqbar_wit_url).unwrap();
|
|
|
|
io::copy(&mut response, &mut wit_file).unwrap();
|
|
|
|
}
|
|
|
|
|
2023-10-10 02:31:49 +03:00
|
|
|
// Create target.wasm (compiled .wit) & world
|
2023-11-13 22:59:17 +03:00
|
|
|
run_command(Command::new("wasm-tools").args([
|
2023-10-03 06:58:20 +03:00
|
|
|
"component",
|
|
|
|
"wit",
|
|
|
|
&format!("{}/wit/", pwd.display()),
|
|
|
|
"-o",
|
|
|
|
"target.wasm",
|
|
|
|
"--wasm",
|
|
|
|
]))
|
|
|
|
.unwrap();
|
2023-11-13 22:59:17 +03:00
|
|
|
run_command(Command::new("touch").args([&format!("{}/world", pwd.display())])).unwrap();
|
2023-10-03 06:58:20 +03:00
|
|
|
|
2023-12-20 01:04:41 +03:00
|
|
|
// Build wasm32-wasi apps, zip, and add to bootstrapped_processes.rs
|
2023-12-20 01:05:20 +03:00
|
|
|
let mut bootstrapped_processes =
|
|
|
|
fs::File::create(format!("{}/src/bootstrapped_processes.rs", pwd.display(),)).unwrap();
|
2023-12-20 01:04:41 +03:00
|
|
|
writeln!(
|
|
|
|
bootstrapped_processes,
|
|
|
|
"pub static BOOTSTRAPPED_PROCESSES: &[(&str, &'static [u8])] = &[",
|
2023-12-20 01:05:20 +03:00
|
|
|
)
|
|
|
|
.unwrap();
|
2023-10-10 02:31:49 +03:00
|
|
|
let modules_dir = format!("{}/modules", pwd.display());
|
2023-11-13 22:59:17 +03:00
|
|
|
for entry in std::fs::read_dir(modules_dir).unwrap() {
|
2023-10-10 02:31:49 +03:00
|
|
|
let entry_path = entry.unwrap().path();
|
2023-10-12 23:06:51 +03:00
|
|
|
let package_name = entry_path.file_name().unwrap().to_str().unwrap();
|
2023-10-16 17:11:43 +03:00
|
|
|
|
2023-10-10 02:31:49 +03:00
|
|
|
// If Cargo.toml is present, build the app
|
2023-10-10 08:09:20 +03:00
|
|
|
let parent_pkg_path = format!("{}/pkg", entry_path.display());
|
2023-10-10 02:31:49 +03:00
|
|
|
if entry_path.join("Cargo.toml").exists() {
|
2023-11-13 22:59:17 +03:00
|
|
|
build_app(&entry_path.display().to_string(), package_name, None);
|
2023-10-10 02:31:49 +03:00
|
|
|
} else if entry_path.is_dir() {
|
|
|
|
fs::create_dir_all(&parent_pkg_path).unwrap();
|
|
|
|
|
|
|
|
// Otherwise, consider it a directory containing subdirectories with potential apps
|
|
|
|
for sub_entry in std::fs::read_dir(&entry_path).unwrap() {
|
|
|
|
let sub_entry_path = sub_entry.unwrap().path();
|
|
|
|
if sub_entry_path.join("Cargo.toml").exists() {
|
2023-10-10 02:32:23 +03:00
|
|
|
build_app(
|
|
|
|
&sub_entry_path.display().to_string(),
|
2023-11-13 22:59:17 +03:00
|
|
|
sub_entry_path.file_name().unwrap().to_str().unwrap(),
|
2023-10-10 02:32:23 +03:00
|
|
|
Some(&parent_pkg_path),
|
|
|
|
);
|
2023-10-10 02:31:49 +03:00
|
|
|
}
|
|
|
|
}
|
2023-10-10 08:09:20 +03:00
|
|
|
}
|
2023-10-10 02:31:49 +03:00
|
|
|
|
2023-10-10 08:09:20 +03:00
|
|
|
// After processing all sub-apps, zip the parent's pkg/ directory
|
2023-12-20 01:05:20 +03:00
|
|
|
let zip_filename = format!("{}.zip", entry_path.file_name().unwrap().to_str().unwrap(),);
|
|
|
|
let zip_path = format!("{}/target/{}", pwd.display(), zip_filename,);
|
2023-12-20 01:04:41 +03:00
|
|
|
let writer = std::fs::File::create(&zip_path).unwrap();
|
2023-10-10 08:09:20 +03:00
|
|
|
let options = zip::write::FileOptions::default()
|
|
|
|
.compression_method(zip::CompressionMethod::Stored)
|
|
|
|
.unix_permissions(0o755);
|
|
|
|
let mut zip = zip::ZipWriter::new(writer);
|
|
|
|
for sub_entry in walkdir::WalkDir::new(&parent_pkg_path) {
|
|
|
|
let sub_entry = sub_entry.unwrap();
|
|
|
|
let path = sub_entry.path();
|
|
|
|
let name = path
|
|
|
|
.strip_prefix(std::path::Path::new(&parent_pkg_path))
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
// Write a directory or file to the ZIP archive
|
|
|
|
if path.is_file() {
|
|
|
|
zip.start_file(name.to_string_lossy().into_owned(), options)
|
|
|
|
.unwrap();
|
|
|
|
let mut file = std::fs::File::open(path).unwrap();
|
|
|
|
let mut buffer = Vec::new();
|
|
|
|
file.read_to_end(&mut buffer).unwrap();
|
|
|
|
zip.write_all(&buffer).unwrap();
|
2023-11-13 22:59:17 +03:00
|
|
|
} else if !name.as_os_str().is_empty() {
|
2023-10-10 08:09:20 +03:00
|
|
|
zip.add_directory(name.to_string_lossy().into_owned(), options)
|
2023-10-10 02:32:23 +03:00
|
|
|
.unwrap();
|
2023-10-06 07:12:31 +03:00
|
|
|
}
|
|
|
|
}
|
2023-10-10 08:09:20 +03:00
|
|
|
zip.finish().unwrap();
|
2023-12-20 01:04:41 +03:00
|
|
|
|
|
|
|
// Add zip bytes to bootstrapped_processes.rs
|
|
|
|
writeln!(
|
|
|
|
bootstrapped_processes,
|
|
|
|
" (\"{}\", include_bytes!(\"{}\")),",
|
2023-12-20 01:05:20 +03:00
|
|
|
zip_filename, zip_path,
|
|
|
|
)
|
|
|
|
.unwrap();
|
2023-10-03 06:58:20 +03:00
|
|
|
}
|
2023-12-20 01:04:41 +03:00
|
|
|
writeln!(bootstrapped_processes, "];").unwrap();
|
2023-10-03 06:58:48 +03:00
|
|
|
}
|