Skip to content

Commit

Permalink
Add configure.win and cleanup.win (#298)
Browse files Browse the repository at this point in the history
  • Loading branch information
yutannihilation committed Sep 17, 2024
1 parent e7e840b commit 4566a2b
Show file tree
Hide file tree
Showing 15 changed files with 98 additions and 26 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ target

/Cargo.lock
/R-package/src/rust/Cargo.lock
/R-package/src/Makevars
/R-package/src/Makevars
/R-package/src/Makevars.win
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
* `savvy init` now generates
* slightly better `configure` script that checks if `cargo` command is available
* `cleanup` script to remove the generated `Makevars` after compilation
* `configure.win` and `cleanup.win`
* `src/Makevars.win.in` instead of `src/Makevars.win` for consistency with Unix-alikes

## [v0.6.7] (2024-09-05)

Expand Down
1 change: 1 addition & 0 deletions R-package/cleanup.win
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rm -f src/Makevars.win
5 changes: 0 additions & 5 deletions R-package/configure
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
# Note: this script is not executed on Windows. Please add `configure.win` if
# you want to check the Rust installation also on Windows. In addition,
# don't forget `cleanup.win` if you decide to generate `src/Makevars.win`
# in the script.

# Even when `cargo` is on `PATH`, `rustc` might not in some cases. This adds
# ~/.cargo/bin to PATH to address such cases. Note that is not always available
# (e.g. or on Ubuntu with Rust installed via APT).
Expand Down
24 changes: 24 additions & 0 deletions R-package/configure.win
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
CARGO_VERSION="$(cargo --version)"

if [ $ret -ne 0 ]; then
echo "-------------- ERROR: CONFIGURATION FAILED --------------------"
echo ""
echo "The cargo command is not available. To install Rust, please refer"
echo "to the official instruction:"
echo ""
echo "https://www.rust-lang.org/tools/install"
echo ""
echo "---------------------------------------------------------------"

exit $ret
fi

# There's a little chance that rustc is not available on PATH while cargo is.
# So, just ignore the error case.
RUSTC_VERSION="$(rustc --version || true)"

# Report the version of Rustc to comply with the CRAN policy
echo "using Rust package manager: '${CARGO_VERSION}'"
echo "using Rust compiler: '${RUSTC_VERSION}'"

sed -e "s/@TARGET@/x86_64-pc-windows-gnu/" src/Makevars.win.in > src/Makevars.win
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
TARGET = x86_64-pc-windows-gnu
TARGET = @TARGET@

# catch DEBUG envvar, which is passed from pkgbuild::compile_dll()
PROFILE = $(subst x,release,$(subst truex,dev,$(DEBUG)x))
Expand Down
8 changes: 5 additions & 3 deletions book/src/get_started.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,13 @@ After `savvy::savvy_init()`, the structure of your R package should look like be
├── R
│ └── 000-wrappers.R <-------(1)
├── configure <-------(2)
├── configure.win <-------(2)
├── cleanup <-------(2)
├── cleanup.win <-------(2)
├── foofoofoofoo.Rproj
└── src
├── Makevars.in <-------(2)
├── Makevars.win <-------(2)
├── Makevars.win.in <-------(2)
├── init.c <-------(3)
├── <your package>-win.def <---(4)
└── rust
Expand All @@ -90,8 +92,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`, `cleanup`, `Makevars.in`, and `Makevars.win`: Necessary build
settings for compiling Rust code
2. `configure*`, `cleanup*`, `Makevars.in`, and `Makevars.win.in`: 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
12 changes: 10 additions & 2 deletions savvy-bindgen/src/gen/static_files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,21 @@ pub fn generate_cleanup() -> String {
include_str!("./templates/cleanup").to_string()
}

pub fn generate_makevars_win(crate_name: &str) -> String {
pub fn generate_makevars_win_in(crate_name: &str) -> String {
format!(
include_str!("./templates/Makevars.win"),
include_str!("./templates/Makevars.win.in"),
crate_name, crate_name
)
}

pub fn generate_configure_win() -> String {
include_str!("./templates/configure.win").to_string()
}

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

pub fn generate_win_def(crate_name: &str) -> String {
format!(include_str!("./templates/dllname-win.def"), crate_name)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
TARGET = x86_64-pc-windows-gnu
TARGET = @TARGET@

# catch DEBUG envvar, which is passed from pkgbuild::compile_dll()
PROFILE = $(subst x,release,$(subst truex,dev,$(DEBUG)x))
Expand Down
1 change: 1 addition & 0 deletions savvy-bindgen/src/gen/templates/cleanup.win
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rm -f src/Makevars.win
5 changes: 0 additions & 5 deletions savvy-bindgen/src/gen/templates/configure
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
# Note: this script is not executed on Windows. Please add `configure.win` if
# you want to check the Rust installation also on Windows. In addition,
# don't forget `cleanup.win` if you decide to generate `src/Makevars.win`
# in the script.

# Even when `cargo` is on `PATH`, `rustc` might not in some cases. This adds
# ~/.cargo/bin to PATH to address such cases. Note that is not always available
# (e.g. or on Ubuntu with Rust installed via APT).
Expand Down
24 changes: 24 additions & 0 deletions savvy-bindgen/src/gen/templates/configure.win
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
CARGO_VERSION="$(cargo --version)"

if [ $ret -ne 0 ]; then
echo "-------------- ERROR: CONFIGURATION FAILED --------------------"
echo ""
echo "The cargo command is not available. To install Rust, please refer"
echo "to the official instruction:"
echo ""
echo "https://www.rust-lang.org/tools/install"
echo ""
echo "---------------------------------------------------------------"

exit $ret
fi

# There's a little chance that rustc is not available on PATH while cargo is.
# So, just ignore the error case.
RUSTC_VERSION="$(rustc --version || true)"

# Report the version of Rustc to comply with the CRAN policy
echo "using Rust package manager: '${CARGO_VERSION}'"
echo "using Rust compiler: '${RUSTC_VERSION}'"

sed -e "s/@TARGET@/x86_64-pc-windows-gnu/" src/Makevars.win.in > src/Makevars.win
3 changes: 3 additions & 0 deletions savvy-bindgen/src/gen/templates/gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@
*.dll
target
.cargo

Makevars
Makevars.win
6 changes: 3 additions & 3 deletions savvy-bindgen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +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_cleanup, 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_cleanup_win, generate_config_toml,
generate_configure, generate_configure_win, generate_example_lib_rs, generate_gitignore,
generate_makevars_in, generate_makevars_win_in, generate_win_def,
};
pub use ir::savvy_enum::SavvyEnum;
pub use ir::savvy_fn::{SavvyFn, SavvyFnArg, SavvyFnType};
Expand Down
26 changes: 21 additions & 5 deletions savvy-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ mod parse_manifest;
mod utils;

use parse_manifest::*;
use savvy_bindgen::generate_cleanup_win;
use savvy_bindgen::generate_configure_win;
use utils::*;

use async_process::Stdio;
Expand All @@ -22,7 +24,7 @@ use futures_lite::{io::BufReader, prelude::*};
use savvy_bindgen::{
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,
generate_makevars_in, generate_makevars_win_in, generate_r_impl_file, generate_win_def,
ParsedResult,
};

Expand Down Expand Up @@ -126,7 +128,9 @@ 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_MAKEVARS_WIN_IN: &str = "src/Makevars.win.in";
const PATH_CONFIGURE_WIN: &str = "configure.win";
const PATH_CLEANUP_WIN: &str = "cleanup.win";
const PATH_GITIGNORE: &str = "src/.gitignore";
const PATH_C_HEADER: &str = "src/rust/api.h";
const PATH_C_IMPL: &str = "src/init.c";
Expand Down Expand Up @@ -337,13 +341,14 @@ savvy = "*""#,
);
write_file(&path.join(PATH_CONFIG_TOML), &generate_config_toml());
write_file(&path.join(PATH_LIB_RS), &generate_example_lib_rs());

write_file(
&path.join(PATH_MAKEVARS_IN),
&generate_makevars_in(&pkg_metadata.package_name_for_rust()),
);
write_file(&path.join(PATH_CONFIGURE), &generate_configure());
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 All @@ -352,9 +357,20 @@ savvy = "*""#,
&generate_win_def(&pkg_metadata.package_name_for_r()),
);
write_file(
&path.join(PATH_MAKEVARS_WIN),
&generate_makevars_win(&pkg_metadata.package_name_for_rust()),
&path.join(PATH_MAKEVARS_WIN_IN),
&generate_makevars_win_in(&pkg_metadata.package_name_for_rust()),
);
write_file(&path.join(PATH_CONFIGURE_WIN), &generate_configure_win());
write_file(&path.join(PATH_CLEANUP_WIN), &generate_cleanup_win());

// This doesn't work on Windows!
set_executable(&[
&path.join(PATH_CONFIGURE),
&path.join(PATH_CONFIGURE_WIN),
&path.join(PATH_CLEANUP),
&path.join(PATH_CLEANUP_WIN),
]);

write_file(&path.join(PATH_GITIGNORE), &generate_gitignore());

if pkg_metadata.has_sysreq {
Expand Down

0 comments on commit 4566a2b

Please sign in to comment.