Skip to content

Commit

Permalink
Add cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
yutannihilation committed Sep 9, 2024
1 parent 43663b4 commit 830e2bb
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 16 deletions.
1 change: 1 addition & 0 deletions R-package/cleanup
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rm -f src/Makevars
4 changes: 3 additions & 1 deletion book/src/get_started.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ After `savvy::savvy_init()`, the structure of your R package should look like be
├── R
│ └── 000-wrappers.R <-------(1)
├── configure <-------(2)
├── cleanup <-------(2)
├── foofoofoofoo.Rproj
└── src
├── Makevars.in <-------(2)
Expand All @@ -89,7 +90,8 @@ After `savvy::savvy_init()`, the structure of your R package should look like be
```

1. `000-wrappers.R`: R functions for the corresponding Rust functions
2. `configure`, `Makevars.in`, and `Makevars.win`: Necessary build settings for compiling Rust code
2. `configure`, `cleanup`, `Makevars.in`, and `Makevars.win`: Necessary build
settings for compiling Rust code
3. `init.c` and `api.h`: C functions for the corresponding Rust functions
4. `<your package>-win.def` and `.cargo/config.toml`: These are tricks to avoid
a minor error on Windows. See [extendr/rextendr#211][1] and [savvy#98][2] for
Expand Down
4 changes: 4 additions & 0 deletions savvy-bindgen/src/gen/static_files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ pub fn generate_configure() -> String {
include_str!("./templates/configure").to_string()
}

pub fn generate_cleanup() -> String {
include_str!("./templates/cleanup").to_string()
}

pub fn generate_makevars_win(crate_name: &str) -> String {
format!(
include_str!("./templates/Makevars.win"),
Expand Down
1 change: 1 addition & 0 deletions savvy-bindgen/src/gen/templates/cleanup
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rm -f src/Makevars
5 changes: 3 additions & 2 deletions savvy-bindgen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ mod utils;
pub use gen::c::{generate_c_header_file, generate_c_impl_file};
pub use gen::r::generate_r_impl_file;
pub use gen::static_files::{
generate_cargo_toml, generate_config_toml, generate_configure, generate_example_lib_rs,
generate_gitignore, generate_makevars_in, generate_makevars_win, generate_win_def,
generate_cargo_toml, generate_cleanup, generate_config_toml, generate_configure,
generate_example_lib_rs, generate_gitignore, generate_makevars_in, generate_makevars_win,
generate_win_def,
};
pub use ir::savvy_enum::SavvyEnum;
pub use ir::savvy_fn::{SavvyFn, SavvyFnArg, SavvyFnType};
Expand Down
34 changes: 21 additions & 13 deletions savvy-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ use std::path::PathBuf;
use futures_lite::{io::BufReader, prelude::*};

use savvy_bindgen::{
generate_c_header_file, generate_c_impl_file, generate_cargo_toml, generate_config_toml,
generate_configure, generate_example_lib_rs, generate_gitignore, generate_makevars_in,
generate_makevars_win, generate_r_impl_file, generate_win_def, ParsedResult,
generate_c_header_file, generate_c_impl_file, generate_cargo_toml, generate_cleanup,
generate_config_toml, generate_configure, generate_example_lib_rs, generate_gitignore,
generate_makevars_in, generate_makevars_win, generate_r_impl_file, generate_win_def,
ParsedResult,
};

/// Generate C bindings and R bindings for a Rust library
Expand Down Expand Up @@ -124,6 +125,7 @@ const PATH_CONFIG_TOML: &str = "src/rust/.cargo/config.toml";
const PATH_LIB_RS: &str = "src/rust/src/lib.rs";
const PATH_MAKEVARS_IN: &str = "src/Makevars.in";
const PATH_CONFIGURE: &str = "configure";
const PATH_CLEANUP: &str = "cleanup";
const PATH_MAKEVARS_WIN: &str = "src/Makevars.win";
const PATH_GITIGNORE: &str = "src/.gitignore";
const PATH_C_HEADER: &str = "src/rust/api.h";
Expand Down Expand Up @@ -175,20 +177,25 @@ fn append_file(path: &Path, contents: &str) {
}

#[cfg(unix)]
fn set_executable(path: &Path) {
fn set_executable(paths: &[&Path]) {
use std::os::unix::fs::PermissionsExt;
for path in paths {
let path_str = path.to_string_lossy();
println!("Setting {} as executable", path_str);

let path_str = path.to_string_lossy();
println!("Setting {} as executable", path_str);

let mut perm = std::fs::metadata(path).unwrap().permissions();
perm.set_mode(0o755);
std::fs::set_permissions(path, perm).unwrap();
let mut perm = std::fs::metadata(path).unwrap().permissions();
perm.set_mode(0o755);
std::fs::set_permissions(path, perm).unwrap();
}
}

#[cfg(not(unix))]
fn set_executable(path: &Path) {
let path_str = path.to_string_lossy();
fn set_executable(paths: &[&Path]) {
let path_str = paths
.iter()
.map(|p| p.to_string_lossy())
.collect::<Vec<_>>()
.join(" ");
eprintln!(
"
### Warning ###################################################################
Expand Down Expand Up @@ -335,7 +342,8 @@ savvy = "*""#,
&generate_makevars_in(&pkg_metadata.package_name_for_rust()),
);
write_file(&path.join(PATH_CONFIGURE), &generate_configure());
set_executable(&path.join(PATH_CONFIGURE)); // This doesn't work on Windows!
write_file(&path.join(PATH_CLEANUP), &generate_cleanup());
set_executable([&path.join(PATH_CONFIGURE), &path.join(PATH_CLEANUP)]); // This doesn't work on Windows!
write_file(
&path.join(format!(
"src/{}-win.def",
Expand Down

0 comments on commit 830e2bb

Please sign in to comment.