veilid/veilid-core/build.rs

144 lines
4.4 KiB
Rust
Raw Normal View History

use glob::glob;
use std::{
env,
process::{Command, Stdio},
};
2023-09-15 15:45:12 +00:00
2023-09-19 23:12:51 +00:00
const CAPNP_VERSION: &str = "1.0.1"; // Keep in sync with scripts/install_capnp.sh
const PROTOC_VERSION: &str = "24.3"; // Keep in sync with scripts/install_protoc.sh
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-19 23:12:51 +00:00
CAPNP_VERSION.to_string()
}
fn get_desired_protoc_version_string() -> String {
PROTOC_VERSION.to_string()
2023-09-15 15:45:12 +00:00
}
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_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-10-18 11:40:16 +00:00
if std::env::var("DOCS_RS").is_ok()
|| std::env::var("CARGO_CFG_DOC").is_ok()
|| std::env::var("BUILD_DOCS").is_ok()
{
2023-10-18 02:13:00 +00:00
return;
}
2023-09-15 15:45:12 +00:00
2023-10-18 02:13:00 +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();
2023-09-15 15:45:12 +00:00
2023-10-18 02:13:00 +00:00
// Check capnp version
let desired_capnp_major_version = desired_capnp_version_string
.split_once('.')
.unwrap()
.0
.parse::<usize>()
.expect("should be valid int");
2023-09-19 23:12:51 +00:00
2023-10-18 02:13:00 +00:00
if capnp_version_string
.split_once('.')
.unwrap()
.0
.parse::<usize>()
.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!(
"cargo:warning=capnproto version may be untested: {}",
capnp_version_string
);
}
2023-09-19 23:12:51 +00:00
2023-10-18 02:13:00 +00:00
// Check protoc version
let desired_protoc_major_version = desired_protoc_version_string
.split_once('.')
.unwrap()
.0
.parse::<usize>()
.expect("should be valid int");
if protoc_version_string
.split_once('.')
.unwrap()
.0
.parse::<usize>()
.expect("should be valid int")
< desired_protoc_major_version
{
panic!(
"protoc version should be at least major version {} but is {}",
desired_protoc_major_version, protoc_version_string
);
} else if protoc_version_string != desired_protoc_version_string {
println!(
"cargo:warning=protoc version may be untested: {}",
protoc_version_string
);
2023-09-19 23:12:51 +00:00
}
2023-10-18 02:13:00 +00:00
::capnpc::CompilerCommand::new()
.file("proto/veilid.capnp")
.run()
.expect("compiling schema");
// Fix for missing __extenddftf2 on Android x86_64 Emulator
let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap();
// if target_os == "android" || target_os == "linux" {
// println!("cargo:rustc-link-lib=stdc++");
// } else {
// println!("cargo:rustc-link-lib=c++");
// }
let target_arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap();
if target_arch == "x86_64" && target_os == "android" {
let missing_library = "clang_rt.builtins-x86_64-android";
let android_ndk_home = env::var("ANDROID_NDK_HOME").expect("ANDROID_NDK_HOME not set");
let lib_path = glob(&format!("{android_ndk_home}/**/lib{missing_library}.a"))
.expect("failed to glob")
.next()
.expect("Need libclang_rt.builtins-x86_64-android.a")
.unwrap();
let lib_dir = lib_path.parent().unwrap();
println!("cargo:rustc-link-search={}", lib_dir.display());
println!("cargo:rustc-link-lib=static={missing_library}");
}
2021-11-22 16:28:30 +00:00
}