diff --git a/docs/compiler-as-library.md b/docs/compiler-as-library.md index b4336f32c0..473c0ba47a 100644 --- a/docs/compiler-as-library.md +++ b/docs/compiler-as-library.md @@ -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 `.`. diff --git a/src/build.rs b/src/build.rs index 3a916d1f66..bb283c0eaa 100644 --- a/src/build.rs +++ b/src/build.rs @@ -1,23 +1,80 @@ -use std::fs; -use std::io::Result; -use std::path; +use std::{env, fs, io::Result, path}; -fn write_primitive() -> Result { - // 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, + new_prims: path::PathBuf, +} + +/// Move the old primitives directory to a different location if present and create a new one. +fn move_primitives() -> Result { + 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 { + 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) } @@ -25,14 +82,11 @@ fn write_primitive() -> Result { 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!( @@ -40,4 +94,4 @@ fn main() { ); } } -} +} \ No newline at end of file diff --git a/src/futil.rs b/src/futil.rs deleted file mode 100644 index 7229f9ff9a..0000000000 --- a/src/futil.rs +++ /dev/null @@ -1,8 +0,0 @@ -use calyx::driver; -use calyx_utils::CalyxResult; - -fn main() -> CalyxResult<()> { - driver::run_compiler()?; - log::warn!("The `futil` binary is deprecated. Please use `calyx` instead."); - Ok(()) -}