-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3f4d7f5
commit e9a5ff5
Showing
3 changed files
with
68 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}", | ||
); | ||
} | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.