From ebeae9a5a049b575c2be209992605a71e7fb51f6 Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Mon, 21 Dec 2020 15:59:16 +1000 Subject: [PATCH] don't pass BUILDINFO into build script It was causing the build script to be recompiled each time a commit was made, even though buildinfo.txt was not changing. --- rslib/BUILD.bazel | 9 +++++++-- rslib/build/main.rs | 19 ++++++++++--------- rslib/src/version.rs | 2 +- 3 files changed, 18 insertions(+), 12 deletions(-) diff --git a/rslib/BUILD.bazel b/rslib/BUILD.bazel index c6fc8f87a..51d89ba7a 100644 --- a/rslib/BUILD.bazel +++ b/rslib/BUILD.bazel @@ -14,13 +14,12 @@ cargo_build_script( "PROTOC": "$(location @com_google_protobuf//:protoc)", "RSLIB_FTL_ROOT": "$(location @rslib_ftl//:l10n.toml)", "EXTRA_FTL_ROOT": "$(location @extra_ftl//:l10n.toml)", - "BUILDINFO": "$(location //:buildinfo.txt)", + "BAZEL": "1", }, crate_root = "build/main.rs", data = [ "//ftl", "backend.proto", - "//:buildinfo.txt", "@com_google_protobuf//:protoc", # bazel requires us to list these out separately "@rslib_ftl//:l10n.toml", @@ -50,6 +49,10 @@ _anki_features = [ "translations", ] +_anki_rustc_env = { + "BUILDINFO": "$(location //:buildinfo.txt)", +} + rust_library( name = "anki", srcs = glob([ @@ -61,6 +64,7 @@ rust_library( "//rslib/cargo:serde_derive", "//rslib/cargo:serde_repr", ], + rustc_env = _anki_rustc_env, visibility = ["//visibility:public"], deps = [ ":build_script", @@ -121,6 +125,7 @@ rust_test( data = glob([ "tests/support/**", ]), + rustc_env = _anki_rustc_env, deps = ["//rslib/cargo:env_logger"], ) diff --git a/rslib/build/main.rs b/rslib/build/main.rs index 597d84686..45c316a6f 100644 --- a/rslib/build/main.rs +++ b/rslib/build/main.rs @@ -5,13 +5,14 @@ fn main() { mergeftl::write_ftl_files_and_fluent_rs(); protobuf::write_backend_proto_rs(); - // copy or mock buildinfo in out_dir - let buildinfo = if let Ok(buildinfo) = std::env::var("BUILDINFO") { - std::fs::read_to_string(&buildinfo).expect("buildinfo missing") - } else { - "".to_string() - }; - let buildinfo_out = - std::path::Path::new(&std::env::var("OUT_DIR").unwrap()).join("buildinfo.txt"); - std::fs::write(&buildinfo_out, buildinfo).unwrap(); + // when building with cargo (eg for rust analyzer), generate a dummy BUILDINFO + if std::env::var("BAZEL").is_err() { + let buildinfo_out = + std::path::Path::new(&std::env::var("OUT_DIR").unwrap()).join("buildinfo.txt"); + std::fs::write(&buildinfo_out, "").unwrap(); + println!( + "cargo:rustc-env=BUILDINFO={}", + buildinfo_out.to_str().expect("buildinfo") + ); + } } diff --git a/rslib/src/version.rs b/rslib/src/version.rs index b7203ae21..b537a4074 100644 --- a/rslib/src/version.rs +++ b/rslib/src/version.rs @@ -5,7 +5,7 @@ use lazy_static::lazy_static; use std::env; fn buildinfo(key: &str) -> &'static str { - let buildinfo = include_str!(concat!(env!("OUT_DIR"), "/buildinfo.txt")); + let buildinfo = include_str!(env!("BUILDINFO")); for line in buildinfo.split('\n') { let mut it = line.split(' '); if it.next().unwrap() == key {