veilid/veilid-core/build.rs

118 lines
3.9 KiB
Rust
Raw Normal View History

2023-09-15 15:45:12 +00:00
use std::path::PathBuf;
use std::process::{Command, Stdio};
2023-09-16 16:23:56 +00:00
fn search_file<T: AsRef<str>, P: AsRef<str>>(start: T, name: P) -> Option<PathBuf> {
let start_path = PathBuf::from(start.as_ref()).canonicalize().ok();
2023-09-17 18:14:39 +00:00
let mut path = start_path.as_deref();
2023-09-16 16:23:56 +00:00
while let Some(some_path) = path {
let file_path = some_path.join(name.as_ref());
if file_path.exists() {
return Some(file_path.to_owned());
}
path = some_path.parent();
}
None
2023-09-15 15:45:12 +00:00
}
2023-09-16 16:23:56 +00:00
2023-09-15 15:45:12 +00:00
fn get_desired_capnp_version_string() -> String {
2023-09-16 16:23:56 +00:00
let capnp_path = search_file(env!("CARGO_MANIFEST_DIR"), ".capnp_version")
.expect("should find .capnp_version file");
std::fs::read_to_string(&capnp_path)
2023-09-17 18:14:39 +00:00
.unwrap_or_else(|_| panic!("can't read .capnp_version file here: {:?}",
capnp_path))
2023-09-15 15:45:12 +00:00
.trim()
.to_owned()
}
fn get_capnp_version_string() -> String {
2023-09-16 17:07:12 +00:00
let output = Command::new("capnp")
2023-09-15 15:45:12 +00:00
.arg("--version")
.stdout(Stdio::piped())
.output()
2023-09-16 17:07:12 +00:00
.expect("capnp was not in the PATH");
2023-09-15 15:45:12 +00:00
let s = String::from_utf8(output.stdout)
2023-09-16 17:07:12 +00:00
.expect("'capnp --version' output was not a valid string")
2023-09-15 15:45:12 +00:00
.trim()
.to_owned();
if !s.starts_with("Cap'n Proto version ") {
2023-09-16 17:07:12 +00:00
panic!("invalid capnp version string: {}", s);
2023-09-15 15:45:12 +00:00
}
s[20..].to_owned()
}
fn get_desired_protoc_version_string() -> String {
2023-09-16 16:23:56 +00:00
let protoc_path = search_file(env!("CARGO_MANIFEST_DIR"), ".protoc_version")
.expect("should find .protoc_version file");
std::fs::read_to_string(&protoc_path)
2023-09-17 18:14:39 +00:00
.unwrap_or_else(|_| panic!("can't read .protoc_version file here: {:?}",
protoc_path))
2023-09-15 15:45:12 +00:00
.trim()
.to_owned()
}
fn get_protoc_version_string() -> String {
let output = Command::new("protoc")
.arg("--version")
.stdout(Stdio::piped())
.output()
.expect("protoc was not in the PATH");
let s = String::from_utf8(output.stdout)
.expect("'protoc --version' output was not a valid string")
.trim()
.to_owned();
if !s.starts_with("libprotoc ") {
panic!("invalid protoc version string: {}", s);
}
s[10..].to_owned()
}
2021-11-22 16:28:30 +00:00
fn main() {
2023-09-15 15:45:12 +00:00
let desired_capnp_version_string = get_desired_capnp_version_string();
let capnp_version_string = get_capnp_version_string();
let desired_protoc_version_string = get_desired_protoc_version_string();
let protoc_version_string = get_protoc_version_string();
// Check capnp version
let desired_capnp_major_version =
2023-09-17 18:14:39 +00:00
usize::from_str_radix(desired_capnp_version_string.split_once('.').unwrap().0, 10)
2023-09-15 15:45:12 +00:00
.expect("should be valid int");
2023-09-17 18:14:39 +00:00
if usize::from_str_radix(capnp_version_string.split_once('.').unwrap().0, 10)
2023-09-15 15:45:12 +00:00
.expect("should be valid int")
!= desired_capnp_major_version
{
panic!(
"capnproto version should be major version 1, preferably {} but is {}",
desired_capnp_version_string, capnp_version_string
);
} else if capnp_version_string != desired_capnp_version_string {
println!(
"capnproto version may be untested: {}",
capnp_version_string
);
}
// Check protoc version
let desired_protoc_major_version =
2023-09-17 18:14:39 +00:00
usize::from_str_radix(desired_protoc_version_string.split_once('.').unwrap().0, 10)
2023-09-15 15:45:12 +00:00
.expect("should be valid int");
2023-09-17 18:14:39 +00:00
if usize::from_str_radix(protoc_version_string.split_once('.').unwrap().0, 10)
2023-09-15 15:45:12 +00:00
.expect("should be valid int")
< desired_protoc_major_version
{
panic!(
2023-09-15 15:58:34 +00:00
"protoc version should be at least major version {} but is {}",
2023-09-15 15:45:12 +00:00
desired_protoc_major_version, protoc_version_string
);
} else if protoc_version_string != desired_protoc_version_string {
println!("protoc version may be untested: {}", protoc_version_string);
}
2021-11-22 16:28:30 +00:00
::capnpc::CompilerCommand::new()
.file("proto/veilid.capnp")
.run()
.expect("compiling schema");
}