Skip to content

Commit

Permalink
Walk back docs and src
Browse files Browse the repository at this point in the history
  • Loading branch information
anshumanmohan committed Aug 23, 2023
1 parent 3f4d7f5 commit e9a5ff5
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 22 deletions.
2 changes: 1 addition & 1 deletion docs/compiler-as-library.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ The [`calyx` binary][calyx-crate] is published using Rust's crates.io repository

1. The [`calyx-stdlib`][calyx-stdlib] package pulls in the sources of all the primitives using the Rust `include_str!` macro.
2. The `calyx` binary defines a build script that depends on `calyx-stdlib` as a build dependency.
3. During build time, the script loads the string representation of all the primitives files and writes them to `$OUT_DIR/primitives`. The `OUT_DIR` environment variable is defined by `cargo`.
3. During build time, the script loads the string representation of all the primitives files and writes them to `$CALYX_PRIMITIVE_DIR/primitives`. If the variable is not set, the location defaults to `$HOME/.calyx`.
4. If (3) succeeds, the build scripts defines the `CALYX_PRIMITIVES_LIB` environment variable which is used when compiling the `calyx` crate.
5. During compilation, `calyx` embeds the value of this environment variable as the default argument to the `-l` flag. If the variable is not defined, the default value of the `-l` flag is `.`.

Expand Down
80 changes: 67 additions & 13 deletions src/build.rs
Original file line number Diff line number Diff line change
@@ -1,43 +1,97 @@
use std::fs;
use std::io::Result;
use std::path;
use std::{env, fs, io::Result, path};

fn write_primitive() -> Result<path::PathBuf> {
// Get the OUT_DIR environment variable from Cargo.
let base: path::PathBuf = std::env::var("OUT_DIR").unwrap().into();
/// Location to install the primitives library
const PRIM_DIR: &str = "CALYX_PRIMITIVES_DIR";

struct PrimState {
base: path::PathBuf,
old_prims: Option<path::PathBuf>,
new_prims: path::PathBuf,
}

/// Move the old primitives directory to a different location if present and create a new one.
fn move_primitives() -> Result<PrimState> {
let base: path::PathBuf = match env::var_os(PRIM_DIR) {
Some(v) => path::PathBuf::from(v),
None => {
let mut path: path::PathBuf = env::var_os("HOME").unwrap().into();
path.push(".calyx");
path
}
};
let mut prims = base.clone();
prims.push("primitives");
let old_prims = if prims.exists() {
let mut old_prims = base.clone();
old_prims.push("old_primitives");
fs::rename(&prims, &old_prims)?;
Some(old_prims)
} else {
None
};

// Create the directory again
fs::create_dir_all(&prims)?;
Ok(PrimState {
base,
old_prims,
new_prims: prims,
})
}

fn write_primitive(prims: &path::Path) -> Result<()> {
// Get the OUT_DIR environment variable from Cargo.
// Write the compile primitives
for (loc, src) in calyx_stdlib::KNOWN_LIBS
.into_iter()
.flat_map(|(_, info)| info)
.chain(Some(calyx_stdlib::COMPILE_LIB))
{
let mut path = prims.clone();
let mut path = prims.to_owned().clone();
path.push(loc);
fs::write(path, src)?;
}
Ok(())
}

fn create_primitives() -> Result<path::PathBuf> {
let PrimState {
base,
old_prims,
new_prims: prims,
} = move_primitives()?;
match write_primitive(prims.as_path()) {
Ok(_) => {
if let Some(old) = old_prims {
fs::remove_dir_all(old)?;
}
}
Err(e) => {
// Move the old primitives back
println!("cargo:warning=Failed to write primitives directory. Restoring old directory");
if let Some(old) = old_prims {
fs::rename(old, &prims)?;
}
return Err(e);
}
}
Ok(base)
}

// The build script does not fail
fn main() {
println!("cargo:rerun-if-changed=src/build.rs");
println!("cargo:rerun-if-changed=src/cmdline.rs");
match write_primitive() {
match create_primitives() {
Ok(p) => {
// The build succeeded. We're going to define the CALYX_PRIMITVE_DIR environment variable
// so that it can be used by the compiler.
println!(
"cargo:rustc-env=CALYX_PRIMITIVES_DIR={}",
p.to_string_lossy()
);
println!("cargo:rustc-env={PRIM_DIR}={}", p.to_string_lossy());
}
Err(e) => {
println!(
"cargo:warning=Failed to create the `primitives` folder. Importing `primitives` will require passing an explicit `-l` when running the Calyx compiler. Error: {e}",
);
}
}
}
}
8 changes: 0 additions & 8 deletions src/futil.rs

This file was deleted.

0 comments on commit e9a5ff5

Please sign in to comment.