Skip to content

Commit

Permalink
Build script with stak-compile (#1890)
Browse files Browse the repository at this point in the history
  • Loading branch information
raviqqe authored Dec 26, 2024
1 parent 9bd4c2c commit baf7c19
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 10 deletions.
42 changes: 42 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion build/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@ repository.workspace = true
[dependencies]
glob = "0.3.1"
stak-compiler = { version = "0.1.88", path = "../compiler" }
tokio = { version = "1.42.0", features = ["fs", "rt-multi-thread"] }
tokio = { version = "1.42.0", features = [
"fs",
"io-util",
"process",
"rt-multi-thread",
] }
which = "7.0.1"

[lints]
workspace = true
60 changes: 51 additions & 9 deletions build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,38 @@
//! in your crate. Then, you can include them into source files in Rust
//! using the [`stak::include_module`][include_module] macro.
//!
//! ```rust
//! ```rust no_run
//! use stak_build::{build_r7rs, BuildError};
//!
//! fn main() -> Result<(), BuildError> {
//! # unsafe { std::env::set_var("OUT_DIR", "target/tmp") };
//! build_r7rs()
//! }
//! ```
//!
//! [include_module]: https://docs.rs/stak/latest/stak/macro.include_module.html
extern crate alloc;

mod error;

use alloc::sync::Arc;
pub use error::BuildError;
use glob::{glob, Paths};
use stak_compiler::compile_r7rs;
use std::{
env,
ffi::OsStr,
path::{Path, PathBuf},
process::Stdio,
};
use tokio::{
fs::{create_dir_all, read_to_string, write},
fs::{create_dir_all, read, write},
io::{AsyncReadExt, AsyncWriteExt},
process::Command,
runtime::Runtime,
spawn,
};
use which::which;

/// Builds R7RS Scheme source files into bytecode files.
///
Expand All @@ -50,6 +56,19 @@ pub fn build_r7rs() -> Result<(), BuildError> {
}

async fn build(paths: Paths) -> Result<(), BuildError> {
let compiler = which("stak-compile").ok().map(Arc::new);

if compiler.is_none() {
println!("cargo::warning={}",
[
"Using an internal compiler for Stak Scheme.",
"It can be very slow unless you modify `profile.<profile>.build-override` in your `Cargo.toml` file to set `opt-level = 3`.",
"For more information, see https://doc.rust-lang.org/cargo/reference/profiles.html#build-dependencies.",
"Also, consider installing the external compiler by running `cargo install stak-compile`.",
].join(" ")
);
}

let out_directory_variable = env::var("OUT_DIR")?;
let out_directory = Path::new(&out_directory_variable);

Expand All @@ -65,11 +84,10 @@ async fn build(paths: Paths) -> Result<(), BuildError> {
continue;
}

let out_path = out_directory.join(&path);

println!("cargo::rerun-if-changed={}", path.display());

handles.push(spawn(compile(path, out_path)))
let out_path = out_directory.join(&path);
handles.push(spawn(compile(path, out_path, compiler.clone())))
}

for handle in handles {
Expand All @@ -79,11 +97,35 @@ async fn build(paths: Paths) -> Result<(), BuildError> {
Ok(())
}

async fn compile(src_path: PathBuf, out_path: PathBuf) -> Result<(), BuildError> {
let string = read_to_string(src_path).await?;
async fn compile(
src_path: PathBuf,
out_path: PathBuf,
compiler: Option<Arc<PathBuf>>,
) -> Result<(), BuildError> {
let mut buffer = vec![];

compile_r7rs(string.as_bytes(), &mut buffer)?;
if let Some(path) = compiler {
let mut command = Command::new(&*path)
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.spawn()?;
let stdin = command.stdin.as_mut().expect("stdin");

stdin
.write_all(include_str!("prelude.scm").as_bytes())
.await?;
stdin.write_all(&read(src_path).await?).await?;

command.wait().await?;

command
.stdout
.expect("stdout")
.read_to_end(&mut buffer)
.await?;
} else {
compile_r7rs(&*read(&src_path).await?, &mut buffer)?;
}

if let Some(path) = out_path.parent() {
create_dir_all(path).await?;
Expand Down
1 change: 1 addition & 0 deletions build/src/prelude.scm

0 comments on commit baf7c19

Please sign in to comment.