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

feat: directly call git to get substrait submodule version #175

Merged
merged 5 commits into from
Apr 8, 2024
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: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
Cargo.lock

# Generated by build.rs (substrait submodule version information)
src/version.in
gen/

.idea

package.json
package-lock.json
node_modules/
.vscode/
.vscode/
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ build = "build.rs"
include = [
"LICENSE",
"build.rs",
"gen/",
"src/**/*.rs",
"src/version.in",
"substrait/LICENSE",
"substrait/README.md",
"substrait/extensions/**/*.yaml",
Expand Down Expand Up @@ -44,7 +44,6 @@ serde_json = "1.0.114"
thiserror = { version = "1.0.57", optional = true }

[build-dependencies]
git2 = { version = "0.18.2", default-features = false }
heck = "0.5.0"
pbjson-build = { version = "0.6.2", optional = true }
prettyplease = "0.2.4"
Expand Down
108 changes: 56 additions & 52 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,60 +7,68 @@ use std::{
fs::{self, File},
io::Write,
path::{Path, PathBuf},
process::Command,
};
use walkdir::{DirEntry, WalkDir};

const SUBMODULE_ROOT: &str = "substrait";
const PROTO_ROOT: &str = "substrait/proto";
const TEXT_ROOT: &str = "substrait/text";
const GEN_ROOT: &str = "gen";

/// Add Substrait version information to the build
fn substrait_version() -> Result<(), Box<dyn Error>> {
use git2::{DescribeFormatOptions, DescribeOptions, Repository};
let gen_dir = Path::new(GEN_ROOT);
fs::create_dir_all(gen_dir)?;

let substrait_version_file =
PathBuf::from(env::var("CARGO_MANIFEST_DIR")?).join("src/version.in");
let version_in_file = gen_dir.join("version.in");

// Rerun if the Substrait submodule changed (to allow setting `dirty`)
println!(
"cargo:rerun-if-changed={}",
Path::new("substrait").display()
);

// Get the version from the submodule
match Repository::open("substrait") {
Ok(repo) => {
// Rerun if the Substrait submodule HEAD changed (when there is a submodule)
println!(
"cargo:rerun-if-changed={}",
Path::new(".git/modules/substrait/HEAD").display()
);

// Get describe output
let mut describe_options = DescribeOptions::default();
describe_options.describe_tags();
let mut describe_format_options = DescribeFormatOptions::default();
describe_format_options.always_use_long_format(true);
describe_format_options.dirty_suffix("-dirty");
let git_describe = repo
.describe(&describe_options)?
.format(Some(&describe_format_options))?;

let mut split = git_describe.split('-');
let git_version = split.next().unwrap_or_default();
let git_depth = split.next().unwrap_or_default();
let git_dirty = git_describe.ends_with("dirty");
let git_hash = repo.head()?.peel_to_commit()?.id().to_string();
let semver::Version {
major,
minor,
patch,
..
} = semver::Version::parse(git_version.trim_start_matches('v'))?;

fs::write(
substrait_version_file,
format!(
r#"// SPDX-License-Identifier: Apache-2.0
// Check if there is a submodule. This file is not included in the packaged crate.
if Path::new(SUBMODULE_ROOT).join(".git").exists() {
// Rerun if the Substrait submodule HEAD changed (when there is a submodule)
println!(
"cargo:rerun-if-changed={}",
Path::new(".git/modules/substrait/HEAD").display()
);

// Get the version of the submodule by directly calling `git describe`.
let version = String::from_utf8(
Command::new("git")
.current_dir(SUBMODULE_ROOT)
.arg("describe")
.arg("--tags")
.arg("--long")
.arg("--dirty=-dirty")
.arg("--abbrev=40")
.output()?
.stdout,
)?;

// Extract the parts.
let mut split = version.split('-');
let git_version = split.next().unwrap_or_default();
let git_depth = split.next().unwrap_or_default();
let git_hash = split.next().unwrap_or_default().trim_end();
let git_dirty = version.ends_with("dirty");
let version = semver::Version::parse(git_version.trim_start_matches('v'))?;

let &semver::Version {
major,
minor,
patch,
..
} = &version;

fs::write(
version_in_file,
format!(
r#"// SPDX-License-Identifier: Apache-2.0

// Note that this file is auto-generated and auto-synced using `build.rs`. It is
// included in `version.rs`.
Expand All @@ -79,27 +87,23 @@ pub const SUBSTRAIT_GIT_SHA: &str = "{git_hash}";

/// The `git describe` output of the Substrait submodule used to build this
/// crate
pub const SUBSTRAIT_GIT_DESCRIBE: &str = "{git_describe}";
pub const SUBSTRAIT_GIT_DESCRIBE: &str = "{version}";

/// The amount of commits between the latest tag and this version of the
/// Substrait module used to build this crate
/// The amount of commits between the latest tag and the version of the
/// Substrait submodule used to build this crate
pub const SUBSTRAIT_GIT_DEPTH: u32 = {git_depth};

/// The dirty state of the Substrait submodule used to build this crate
pub const SUBSTRAIT_GIT_DIRTY: bool = {git_dirty};
"#
),
)?;
),
)?;
} else {
// If we don't have a version file yet we fail the build.
if !version_in_file.exists() {
panic!("Couldn't find the substrait submodule. Please clone the submodule: `git submodule update --init`.")
}
Err(e) => {
// If this is a package build the `substrait_version_file` should
// exist. If it does not, it means this is probably a Git build that
// did not clone the substrait submodule.
if !substrait_version_file.exists() {
panic!("Couldn't open the substrait submodule: {e}")
}
}
};
}

Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion src/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

use crate::proto::Version;

include!("version.in");
include!("../gen/version.in");

/// Returns the version of Substrait used to build this crate.
///
Expand Down