Fix building outside workspace

rslib/build.rs needs descriptors, even if env var not set
This commit is contained in:
Damien Elmes 2023-07-02 16:25:40 +10:00
parent c6f429ab17
commit 0f77de896d
4 changed files with 23 additions and 16 deletions

View File

@ -3,10 +3,9 @@
mod rust_interface;
use std::env;
use std::fs;
use std::path::PathBuf;
use anki_proto_gen::descriptors_path;
use anyhow::Result;
use prost_reflect::DescriptorPool;
@ -15,7 +14,7 @@ fn main() -> Result<()> {
let buildhash = fs::read_to_string("../out/buildhash").unwrap_or_default();
println!("cargo:rustc-env=BUILDHASH={buildhash}");
let descriptors_path = env::var("DESCRIPTORS_BIN").ok().map(PathBuf::from).unwrap();
let descriptors_path = descriptors_path();
println!("cargo:rerun-if-changed={}", descriptors_path.display());
let pool = DescriptorPool::decode(std::fs::read(descriptors_path)?.as_ref())?;
rust_interface::write_rust_interface(&pool)?;

View File

@ -5,14 +5,12 @@ pub mod python;
pub mod rust;
pub mod ts;
use std::env;
use std::path::PathBuf;
use anki_proto_gen::descriptors_path;
use anki_proto_gen::get_services;
use anyhow::Result;
fn main() -> Result<()> {
let descriptors_path = env::var("DESCRIPTORS_BIN").ok().map(PathBuf::from);
let descriptors_path = descriptors_path();
let pool = rust::write_rust_protos(descriptors_path)?;
let (_, services) = get_services(&pool);

View File

@ -14,7 +14,7 @@ use anyhow::Context;
use anyhow::Result;
use prost_reflect::DescriptorPool;
pub fn write_rust_protos(descriptors_path: Option<PathBuf>) -> Result<DescriptorPool> {
pub fn write_rust_protos(descriptors_path: PathBuf) -> Result<DescriptorPool> {
set_protoc_path();
let proto_dir = PathBuf::from("../../proto");
let paths = gather_proto_paths(&proto_dir)?;
@ -49,14 +49,12 @@ pub fn write_rust_protos(descriptors_path: Option<PathBuf>) -> Result<Descriptor
.context("prost build")?;
let descriptors = read_file(&tmp_descriptors)?;
if let Some(descriptors_path) = descriptors_path {
create_dir_all(
descriptors_path
.parent()
.context("missing parent of descriptor")?,
)?;
write_file_if_changed(descriptors_path, &descriptors)?;
}
create_dir_all(
descriptors_path
.parent()
.context("missing parent of descriptor")?,
)?;
write_file_if_changed(descriptors_path, &descriptors)?;
let pool = DescriptorPool::decode(descriptors.as_ref())?;
add_must_use_annotations(

View File

@ -5,6 +5,7 @@
//! match.
use std::collections::HashMap;
use std::env;
use std::path::PathBuf;
use anki_io::read_to_string;
@ -266,3 +267,14 @@ pub fn determine_if_message_is_empty(pool: &DescriptorPool, path: &Utf8Path, nam
false
}
}
/// - When building via a local checkout, the path defined in .cargo/config
/// - When building via cargo install or a third-party crate,
/// OUT_DIR/../../anki_descriptors.bin (so it can be seen by the rslib crate)
pub fn descriptors_path() -> PathBuf {
if let Ok(path) = env::var("DESCRIPTORS_BIN") {
PathBuf::from(path)
} else {
PathBuf::from(env::var("OUT_DIR").unwrap()).join("../../anki_descriptors.bin")
}
}