use std::{env, error::Error, fs, path::PathBuf};
const SUBSTRAIT_DEP: &str = "substrait-prost";
fn substrait_version() -> Result<semver::Version, Box<dyn Error>> {
let manifest_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR")?);
let manifest_path = manifest_dir.join("Cargo.toml");
println!("cargo:rerun-if-changed={}", manifest_path.display());
let manifest = fs::read_to_string(&manifest_path)?.parse::<toml::Table>()?;
let requirement = manifest
.get("dependencies")
.and_then(toml::Value::as_table)
.and_then(|deps| deps.get(SUBSTRAIT_DEP))
.and_then(|dep| match dep {
toml::Value::String(version) => Some(version.as_str()),
toml::Value::Table(table) => table.get("version").and_then(toml::Value::as_str),
_ => None,
})
.ok_or_else(|| {
format!("missing `{SUBSTRAIT_DEP}` dependency version in {manifest_path:?}")
})?;
let version = requirement.trim_start_matches(['=', '^', '~', '>', '<', ' ']);
Ok(semver::Version::parse(version)?)
}
fn main() -> Result<(), Box<dyn Error>> {
println!("cargo:rerun-if-env-changed=FORCE_REBUILD");
let version = substrait_version()?;
let semver::Version {
major,
minor,
patch,
..
} = version;
let out_dir = PathBuf::from(env::var("OUT_DIR")?);
fs::write(
out_dir.join("version.in"),
format!(
r#"// SPDX-License-Identifier: Apache-2.0
// Note that this file is auto-generated by `build.rs` from the pinned
// `{SUBSTRAIT_DEP}` version. It is included in `version.rs`.
/// The major version of Substrait used to build this crate
pub const SUBSTRAIT_MAJOR_VERSION: u32 = {major};
/// The minor version of Substrait used to build this crate
pub const SUBSTRAIT_MINOR_VERSION: u32 = {minor};
/// The patch version of Substrait used to build this crate
pub const SUBSTRAIT_PATCH_VERSION: u32 = {patch};
/// The Git SHA (lower hex) of Substrait used to build this crate.
///
/// Empty: the Substrait version is derived from the packaged crate version, not
/// a git checkout.
pub const SUBSTRAIT_GIT_SHA: &str = "";
/// The `git describe`-style output of the Substrait version used to build this
/// crate.
pub const SUBSTRAIT_GIT_DESCRIBE: &str = "v{major}.{minor}.{patch}";
/// The amount of commits between the latest tag and the version of Substrait
/// used to build this crate. Always `0`: a tagged, packaged release is used.
pub const SUBSTRAIT_GIT_DEPTH: u32 = 0;
/// The dirty state of the Substrait version used to build this crate. Always
/// `false`: a tagged, packaged release is used.
pub const SUBSTRAIT_GIT_DIRTY: bool = false;
/// A constant with the Substrait version as name, to trigger semver bumps when
/// the Substrait version changes.
pub const SUBSTRAIT_{major}_{minor}_{patch}: () = ();
"#
),
)?;
Ok(())
}