Skip to content
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

Refactor version logging in generated bindings #159

Merged
merged 6 commits into from
Apr 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion bindings/bindings-linux-aarch64-R4.1.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
/* automatically generated by rust-bindgen 0.64.0 */

/* libR-sys version: 0.4.0 */
/* bindgen clang version: Ubuntu clang version 14.0.0-1ubuntu1 */
/* clang-rs version: Ubuntu clang version 14.0.0-1ubuntu1 */
/* r version: 4.1.3 */
/* automatically generated by rust-bindgen 0.64.0 */

pub const M_E: f64 = 2.718281828459045;
pub const M_LOG2E: f64 = 1.4426950408889634;
Expand Down
4 changes: 3 additions & 1 deletion bindings/bindings-linux-aarch64-R4.2.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
/* automatically generated by rust-bindgen 0.64.0 */

/* libR-sys version: 0.4.0 */
/* bindgen clang version: Ubuntu clang version 14.0.0-1ubuntu1 */
/* clang-rs version: Ubuntu clang version 14.0.0-1ubuntu1 */
/* r version: 4.2.3 */
/* automatically generated by rust-bindgen 0.64.0 */

#[repr(C)]
#[derive(Copy, Clone, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
Expand Down
4 changes: 3 additions & 1 deletion bindings/bindings-linux-aarch64-R4.4-devel.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
/* automatically generated by rust-bindgen 0.64.0 */

/* libR-sys version: 0.4.0 */
/* bindgen clang version: Ubuntu clang version 14.0.0-1ubuntu1 */
/* clang-rs version: Ubuntu clang version 14.0.0-1ubuntu1 */
/* r version: 4.4.0-devel */
/* automatically generated by rust-bindgen 0.64.0 */

#[repr(C)]
#[derive(Copy, Clone, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
Expand Down
4 changes: 3 additions & 1 deletion bindings/bindings-linux-x86_64-R4.1.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
/* automatically generated by rust-bindgen 0.64.0 */

/* libR-sys version: 0.4.0 */
/* bindgen clang version: Ubuntu clang version 14.0.0-1ubuntu1 */
/* clang-rs version: Ubuntu clang version 14.0.0-1ubuntu1 */
/* r version: 4.1.3 */
/* automatically generated by rust-bindgen 0.64.0 */

pub const M_E: f64 = 2.718281828459045;
pub const M_LOG2E: f64 = 1.4426950408889634;
Expand Down
4 changes: 3 additions & 1 deletion bindings/bindings-linux-x86_64-R4.2.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
/* automatically generated by rust-bindgen 0.64.0 */

/* libR-sys version: 0.4.0 */
/* bindgen clang version: Ubuntu clang version 14.0.0-1ubuntu1 */
/* clang-rs version: Ubuntu clang version 14.0.0-1ubuntu1 */
/* r version: 4.2.3 */
/* automatically generated by rust-bindgen 0.64.0 */

#[repr(C)]
#[derive(Copy, Clone, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
Expand Down
4 changes: 3 additions & 1 deletion bindings/bindings-linux-x86_64-R4.4-devel.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
/* automatically generated by rust-bindgen 0.64.0 */

/* libR-sys version: 0.4.0 */
/* bindgen clang version: Ubuntu clang version 14.0.0-1ubuntu1 */
/* clang-rs version: Ubuntu clang version 14.0.0-1ubuntu1 */
/* r version: 4.4.0-devel */
/* automatically generated by rust-bindgen 0.64.0 */

#[repr(C)]
#[derive(Copy, Clone, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
Expand Down
4 changes: 3 additions & 1 deletion bindings/bindings-macos-aarch64-R4.2.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
/* automatically generated by rust-bindgen 0.64.0 */

/* libR-sys version: 0.4.0 */
/* bindgen clang version: Homebrew clang version 16.0.1 */
/* clang-rs version: Homebrew clang version 16.0.1 */
/* r version: 4.2.3 */
/* automatically generated by rust-bindgen 0.64.0 */

#[repr(C)]
#[derive(Copy, Clone, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
Expand Down
4 changes: 3 additions & 1 deletion bindings/bindings-macos-x86_64-R4.1.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
/* automatically generated by rust-bindgen 0.64.0 */

/* libR-sys version: 0.4.0 */
/* bindgen clang version: Homebrew clang version 16.0.1 */
/* clang-rs version: Homebrew clang version 16.0.1 */
/* r version: 4.1.3 */
/* automatically generated by rust-bindgen 0.64.0 */

pub const INT_MIN: i32 = -2147483648;
pub const M_E: f64 = 2.718281828459045;
Expand Down
4 changes: 3 additions & 1 deletion bindings/bindings-macos-x86_64-R4.2.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
/* automatically generated by rust-bindgen 0.64.0 */

/* libR-sys version: 0.4.0 */
/* bindgen clang version: Homebrew clang version 16.0.1 */
/* clang-rs version: Homebrew clang version 16.0.1 */
/* r version: 4.2.3 */
/* automatically generated by rust-bindgen 0.64.0 */

#[repr(C)]
#[derive(Copy, Clone, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
Expand Down
4 changes: 3 additions & 1 deletion bindings/bindings-macos-x86_64-R4.4-devel.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
/* automatically generated by rust-bindgen 0.64.0 */

/* libR-sys version: 0.4.0 */
/* bindgen clang version: Homebrew clang version 16.0.1 */
/* clang-rs version: Homebrew clang version 16.0.1 */
/* r version: 4.4.0-devel */
/* automatically generated by rust-bindgen 0.64.0 */

#[repr(C)]
#[derive(Copy, Clone, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
Expand Down
4 changes: 3 additions & 1 deletion bindings/bindings-windows-x86-R4.1.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
/* automatically generated by rust-bindgen 0.64.0 */

/* libR-sys version: 0.4.0 */
/* bindgen clang version: clang version 15.0.7 */
/* clang-rs version: clang version 15.0.7 */
/* r version: 4.1.3 */
/* automatically generated by rust-bindgen 0.64.0 */

pub const INT_MIN: i32 = -2147483648;
pub const M_E: f64 = 2.718281828459045;
Expand Down
4 changes: 3 additions & 1 deletion bindings/bindings-windows-x86_64-R4.1.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
/* automatically generated by rust-bindgen 0.64.0 */

/* libR-sys version: 0.4.0 */
/* bindgen clang version: clang version 15.0.7 */
/* clang-rs version: clang version 15.0.7 */
/* r version: 4.1.3 */
/* automatically generated by rust-bindgen 0.64.0 */

pub const INT_MIN: i32 = -2147483648;
pub const M_E: f64 = 2.718281828459045;
Expand Down
12 changes: 7 additions & 5 deletions bindings/bindings-windows-x86_64-R4.2.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
/* bindgen clang version: clang version 15.0.7 */
/* clang-rs version: clang version 15.0.7 */
/* r version: 4.2.3 */
/* automatically generated by rust-bindgen 0.64.0 */

/* automatically generated by rust-bindgen 0.64.0 */

/* libR-sys version: 0.4.0 */
/* bindgen clang version: clang version 15.0.7 */
/* clang-rs version: clang version 15.0.7 */
/* r version: 4.2.3 */

pub const INT_MIN: i32 = -2147483648;
pub const M_E: f64 = 2.718281828459045;
pub const M_LOG2E: f64 = 1.4426950408889634;
Expand Down
12 changes: 7 additions & 5 deletions bindings/bindings-windows-x86_64-R4.4-devel.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
/* bindgen clang version: clang version 15.0.7 */
/* clang-rs version: clang version 15.0.7 */
/* r version: 4.4.0-devel */
/* automatically generated by rust-bindgen 0.64.0 */

/* automatically generated by rust-bindgen 0.64.0 */

/* libR-sys version: 0.4.0 */
/* bindgen clang version: clang version 15.0.7 */
/* clang-rs version: clang version 15.0.7 */
/* r version: 4.4.0-devel */

pub const INT_MIN: i32 = -2147483648;
pub const M_E: f64 = 2.718281828459045;
pub const M_LOG2E: f64 = 1.4426950408889634;
Expand Down
56 changes: 24 additions & 32 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const ENVVAR_R_HOME: &str = "R_HOME";
const ENVVAR_R_VERSION: &str = "LIBRSYS_R_VERSION";

// A path to a dir containing pre-computed bindings (default: "bindings").
#[cfg(not(feature = "use-bindgen"))]
const ENVVAR_BINDINGS_PATH: &str = "LIBRSYS_BINDINGS_PATH";

// A path to libclang toolchain. If this is set, the path is added to the
Expand All @@ -41,7 +42,8 @@ const ENVVAR_LIBCLANG_INCLUDE_PATH: &str = "LIBRSYS_LIBCLANG_INCLUDE_PATH";
// dir. If this is set, generated bindings are also put there.
#[cfg(feature = "use-bindgen")]
const ENVVAR_BINDINGS_OUTPUT_PATH: &str = "LIBRSYS_BINDINGS_OUTPUT_PATH";
#[allow(dead_code)]

#[derive(Debug)]
struct InstallationPaths {
r_home: PathBuf,
include: PathBuf,
Expand Down Expand Up @@ -69,11 +71,14 @@ struct RVersionInfo {
}

impl RVersionInfo {
/// Returns the name for precompiled bindings, given R version and targets.
/// e.g. `bindings-windows-x86_64-R4.4-devel.rs`
fn get_r_bindings_filename(&self, target_os: &str, target_arch: &str) -> PathBuf {
let devel_suffix = if self.devel { "-devel" } else { "" };
let major = &self.major;
let minor = &self.minor;
PathBuf::from(format!(
"bindings-{}-{}-R{}.{}{}.rs",
target_os, target_arch, self.major, self.minor, devel_suffix
"bindings-{target_os}-{target_arch}-R{major}.{minor}{devel_suffix}.rs"
))
}
}
Expand Down Expand Up @@ -464,8 +469,7 @@ fn generate_bindings(r_paths: &InstallationPaths, version_info: &RVersionInfo) {
let target_arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap();

println!(
"Generating bindings for target: {}, os: {}, architecture: {}",
target, target_os, target_arch
"Generating bindings for target: {target}, os: {target_os}, architecture: {target_arch}"
);

// Point to the correct headers
Expand All @@ -492,6 +496,16 @@ fn generate_bindings(r_paths: &InstallationPaths, version_info: &RVersionInfo) {

// Finish the builder and generate the bindings.
let bindings = bindgen_builder
.raw_line(format!(
"/* libR-sys version: {} */",
env!("CARGO_PKG_VERSION")
))
.raw_line(format!(
"/* bindgen clang version: {} */",
bindgen::clang_version().full
))
.raw_line(format!("/* clang-rs version: {} */", clang::get_version()))
.raw_line(format!("/* r version: {} */", version_info.full))
CGMossa marked this conversation as resolved.
Show resolved Hide resolved
.generate_comments(true)
.parse_callbacks(Box::new(TrimCommentsCallbacks))
.clang_arg("-fparse-all-comments")
Expand Down Expand Up @@ -522,39 +536,17 @@ fn generate_bindings(r_paths: &InstallationPaths, version_info: &RVersionInfo) {
let bindings_file_full = version_info.get_r_bindings_filename(&target_os, &target_arch);
let out_file = out_path.join(bindings_file_full);

save_bindings_to_file(bindings, version_info, out_file);
bindings
.write_to_file(&out_file)
.expect(&format!("Couldn't write bindings: {}", out_file.display()));
} else {
println!(
"Warning: Couldn't write the bindings since `LIBRSYS_BINDINGS_OUTPUT_PATH` is not set."
);
}
}

#[cfg(feature = "use-bindgen")]
fn save_bindings_to_file(
bindings: bindgen::Bindings,
version_info: &RVersionInfo,
out_file: PathBuf,
) {
let clang_version_bindgen = bindgen::clang_version();
let clang_rs_version = clang::get_version();
let header = [
format!(
"/* bindgen clang version: {} */",
clang_version_bindgen.full
),
format!("/* clang-rs version: {} */", clang_rs_version),
format!("/* r version: {} */", version_info.full),
];
let header = header.join("\n");

let bindings = bindings.to_string();
let bindings = format!("{header}\n{bindings}");
std::fs::write(&out_file, bindings)
.expect(&format!("Couldn't write bindings: {}", out_file.display()));
}

#[allow(dead_code)]
#[cfg(not(feature = "use-bindgen"))]
/// Retrieve bindings from cache, if available. Errors out otherwise.
fn retrieve_prebuild_bindings(version_info: &RVersionInfo) {
let target_os = std::env::var("CARGO_CFG_TARGET_OS").unwrap();
Expand All @@ -565,7 +557,7 @@ fn retrieve_prebuild_bindings(version_info: &RVersionInfo) {

// we try a few different file names, from more specific to less specific
let bindings_file_full = version_info.get_r_bindings_filename(&target_os, &target_arch);
let bindings_file_novers = PathBuf::from(format!("bindings-{}-{}.rs", target_os, target_arch));
let bindings_file_novers = PathBuf::from(format!("bindings-{target_os}-{target_arch}.rs"));

let mut from = bindings_path.join(bindings_file_full);
if !from.exists() {
Expand Down