-
Notifications
You must be signed in to change notification settings - Fork 0
Working with cargo #47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
9771163
Adding `cargo` integration
dkcumming 8f59104
Added `.stable-mir-json` directory containing `*.rlib`
dkcumming eba1642
`LD_LIBRARY_PATH` saved in `.stable_mir_json`. Some tests and results…
dkcumming 96d5b01
Removed unnecessary functions, added run script
dkcumming a163c71
removed notes.md
dkcumming 4b8c544
cargo fmt
dkcumming 27d2e9a
Merge branch 'master' into dc/cargo-stable-mir-json
dkcumming fdc3880
Update src/bin/cargo_stable_mir_json.rs
dkcumming d887f9c
User can provide location for`.stable_mir_json`
dkcumming 25ba774
Adding LD_LIBRARY_PATH to script directly
dkcumming be6aca3
Fixed path in run script
dkcumming 948937d
`record_ld_library_path` error handling improved
dkcumming 3a0e951
Removed copying the `deps` over and removed duplication of info in `c…
dkcumming 9a80293
Documentation
dkcumming 3eb9d11
Corrected documentation
dkcumming 7d28c52
changed run.sh to debug.sh and release.sh
dkcumming 1f5b7a2
clippy and fmt
dkcumming d9ce356
Add "success" message
dkcumming 83dee4e
fixed print messages
dkcumming 88c8276
Delete directory if it already exists
dkcumming File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
@@ -0,0 +1,189 @@ | ||
use std::env; | ||
|
||
use std::io::Write; | ||
use std::os::unix::fs::PermissionsExt; | ||
use std::path::{Path, PathBuf}; | ||
|
||
use anyhow::{bail, Result}; | ||
|
||
fn main() -> Result<()> { | ||
let args: Vec<_> = env::args().collect(); | ||
|
||
let (repo_dir, maybe_user_provided_dir): (PathBuf, Option<PathBuf>) = match &args[..] { | ||
[_, repo_dir] => (repo_dir.into(), None), | ||
[_, repo_dir, user_provided_dir] => (repo_dir.into(), Some(user_provided_dir.into())), | ||
_ => bail!("Usage: cargo run --bin cargo_stable_mir_json -- <PATH-TO-STABLE-MIR-JSON-REPO> [OPTIONAL-PATH-TO-CREATE-BUILD-DIR]"), | ||
}; | ||
|
||
if !repo_dir.is_dir() { | ||
bail!( | ||
"Provided should be path to stable_mir_json repo, but {} is not a dir", | ||
repo_dir.display() | ||
); | ||
} | ||
|
||
if let Some(ref user_provided_dir) = maybe_user_provided_dir { | ||
if !user_provided_dir.is_dir() { | ||
bail!( | ||
"Provided should be path to create the .stable-mir-json dir, but {} is not a dir", | ||
user_provided_dir.display() | ||
); | ||
} | ||
} | ||
|
||
setup(repo_dir, maybe_user_provided_dir) | ||
} | ||
|
||
fn setup(repo_dir: PathBuf, maybe_user_provided_dir: Option<PathBuf>) -> Result<()> { | ||
let smir_json_dir = smir_json_dir(maybe_user_provided_dir)?; | ||
if smir_json_dir.exists() { | ||
// Delete the directory if it already exists to overwrite it | ||
std::fs::remove_dir_all(&smir_json_dir)? | ||
} | ||
println!("Creating {} directory", smir_json_dir.display()); | ||
std::fs::create_dir_all(&smir_json_dir)?; | ||
|
||
let ld_library_path = record_ld_library_path(&smir_json_dir)?; | ||
copy_artefacts(&repo_dir, &smir_json_dir, &ld_library_path)?; | ||
println!("Artefacts installed in {}.", &smir_json_dir.display()); | ||
println!( | ||
"To use stable-mir-json, set `RUSTC={}/{{debug,release}}.sh` when calling `cargo`.", | ||
&smir_json_dir.display() | ||
); | ||
Ok(()) | ||
} | ||
|
||
fn smir_json_dir(maybe_user_provided_dir: Option<PathBuf>) -> Result<PathBuf> { | ||
let user_provided_dir = match maybe_user_provided_dir { | ||
Some(user_provided_dir) => user_provided_dir, | ||
None => home::home_dir().expect("couldn't find home directory"), | ||
}; | ||
if !user_provided_dir.is_dir() { | ||
bail!( | ||
// We know this is home because main checked user_provided_dir already | ||
"got home directory `{}` which isn't a directory", | ||
user_provided_dir.display() | ||
); | ||
} | ||
let smir_json_dir = user_provided_dir.join(".stable-mir-json"); | ||
Ok(smir_json_dir) | ||
} | ||
|
||
fn copy_artefacts(repo_dir: &Path, smir_json_dir: &Path, ld_library_path: &Path) -> Result<()> { | ||
let dev_dir = repo_dir.join("target/debug/"); | ||
let dev_rlib = dev_dir.join("libstable_mir_json.rlib"); | ||
|
||
let release_dir = repo_dir.join("target/release/"); | ||
let release_rlib = release_dir.join("libstable_mir_json.rlib"); | ||
|
||
if !dev_rlib.exists() && !release_rlib.exists() { | ||
bail!( | ||
"Neither dev rlib `{}`, nor release rlib `{}` exists", | ||
dev_dir.display(), | ||
release_dir.display() | ||
); | ||
} | ||
|
||
// Debug | ||
if dev_rlib.exists() { | ||
cp_artefacts_from_profile(smir_json_dir, Profile::Dev(repo_dir))?; | ||
add_run_script(smir_json_dir, ld_library_path, Profile::Dev(repo_dir))?; | ||
} | ||
|
||
// Release | ||
if release_rlib.exists() { | ||
cp_artefacts_from_profile(smir_json_dir, Profile::Release(repo_dir))?; | ||
add_run_script(smir_json_dir, ld_library_path, Profile::Release(repo_dir))?; | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
enum Profile<'a> { | ||
Dev(&'a Path), | ||
Release(&'a Path), | ||
} | ||
|
||
impl Profile<'_> { | ||
fn name(&self) -> String { | ||
match self { | ||
Profile::Dev(_) => "debug".into(), | ||
Profile::Release(_) => "release".into(), | ||
} | ||
} | ||
|
||
fn profile_dir(&self) -> PathBuf { | ||
match self { | ||
Profile::Dev(repo_dir) => repo_dir.join("target/debug/"), | ||
Profile::Release(repo_dir) => repo_dir.join("target/release/"), | ||
} | ||
} | ||
} | ||
|
||
fn cp_artefacts_from_profile(smir_json_dir: &Path, profile: Profile) -> Result<()> { | ||
let rlib = profile.profile_dir().join("libstable_mir_json.rlib"); | ||
let bin = profile.profile_dir().join("stable_mir_json"); | ||
|
||
// Stable MIR JSON bin and rlib | ||
let smir_json_profile_dir = smir_json_dir.join(format!("{}/", profile.name())); | ||
std::fs::create_dir(&smir_json_profile_dir)?; | ||
|
||
let smir_json_profile_rlib = smir_json_profile_dir.join("libstable_mir_json.rlib"); | ||
println!( | ||
"Copying {} to {}", | ||
rlib.display(), | ||
smir_json_profile_rlib.display() | ||
); | ||
std::fs::copy(rlib, smir_json_profile_rlib)?; | ||
|
||
let smir_json_profile_bin = smir_json_profile_dir.join("stable_mir_json"); | ||
println!( | ||
"Copying {} to {}", | ||
bin.display(), | ||
smir_json_profile_bin.display() | ||
); | ||
std::fs::copy(bin, smir_json_profile_bin)?; | ||
|
||
Ok(()) | ||
} | ||
|
||
fn add_run_script(smir_json_dir: &Path, ld_library_path: &Path, profile: Profile) -> Result<()> { | ||
let run_script_path = smir_json_dir.join(format!("{}.sh", profile.name())); | ||
let mut run_script = std::fs::File::create(&run_script_path)?; | ||
writeln!(run_script, "#!/bin/bash")?; | ||
writeln!(run_script, "set -eu")?; | ||
writeln!(run_script)?; | ||
writeln!( | ||
run_script, | ||
"export LD_LIBRARY_PATH={}", | ||
ld_library_path.display(), | ||
)?; | ||
writeln!( | ||
run_script, | ||
"exec \"{}/{}/stable_mir_json\" \"$@\"", | ||
smir_json_dir.display(), | ||
profile.name() | ||
)?; | ||
|
||
// Set the script permissions to -rwxr-xr-x | ||
std::fs::set_permissions(run_script_path, std::fs::Permissions::from_mode(0o755))?; | ||
Ok(()) | ||
} | ||
|
||
fn record_ld_library_path(smir_json_dir: &Path) -> Result<PathBuf> { | ||
const LOADER_PATH: &str = "LD_LIBRARY_PATH"; | ||
if let Some(paths) = env::var_os(LOADER_PATH) { | ||
// Note: kani filters the LD_LIBRARY_PATH, not sure why as it is working locally as is | ||
let mut ld_library_file = std::fs::File::create(smir_json_dir.join("ld_library_path"))?; | ||
|
||
match paths.to_str() { | ||
Some(ld_library_path) => { | ||
writeln!(ld_library_file, "{}", ld_library_path)?; | ||
Ok(ld_library_path.into()) | ||
} | ||
None => bail!("Couldn't cast LD_LIBRARY_PATH to str"), | ||
} | ||
} else { | ||
bail!("Couldn't read LD_LIBRARY_PATH from env"); // This should be unreachable | ||
} | ||
} |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.