crosvm/metrics/build.rs
Dennis Kempin 278e5ffeba test_runner: Add separate cargo target dir for each platform
Using the same CARGO_TARGET_DIR for all builds prevents us from
building for platforms in parallel, since cargo will lock the
directory to only run one build at a time.

Use the same directory for clippy as well, and ensure that
clippy won't invalidate caches created by run_tests.

This reduces the build time for tools/presubmit by about 50%.

Another advantage is that rust_analyzer and run_tests will no longer
block each other or invalidate the cache when run with different
feature flags.

Note: This introduces two subtle changes to the build that required nit
fixes:
- build.rs files are now run through clippy as well
- common/* crates are now also built for the target architecture instead
  the host.

BUG=None
TEST=tools/presubmit

Change-Id: I8da9ef53418c0b15827d512a04e77828621aef88
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/3984416
Commit-Queue: Dennis Kempin <denniskempin@google.com>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
2022-10-31 21:33:33 +00:00

55 lines
1.6 KiB
Rust

// Copyright 2022 The ChromiumOS Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
use std::env;
use std::fs;
use std::fs::File;
use std::io::Write;
use std::path::PathBuf;
use protoc_rust::Codegen;
use protoc_rust::Customize;
fn main() {
build_protos();
}
// TODO(mikehoyle): Unify all proto-building logic across crates into a common build dependency.
fn build_protos() {
let proto_files = vec!["protos/event_details.proto"];
let out_dir = format!(
"{}/metrics_protos",
env::var("OUT_DIR").expect("OUT_DIR env does not exist.")
);
fs::create_dir_all(&out_dir).unwrap();
Codegen::new()
.out_dir(&out_dir)
.inputs(&proto_files)
.customize(Customize {
serde_derive: Some(true),
..Default::default()
})
.run()
.unwrap();
create_gen_file(proto_files, &out_dir);
}
fn create_gen_file(proto_files: Vec<&str>, out_dir: &str) {
let generated = PathBuf::from(&out_dir).join("generated.rs");
let out = File::create(generated).expect("Failed to create generated file.");
for proto in proto_files {
let file_stem = PathBuf::from(proto)
.file_stem()
.unwrap()
.to_str()
.unwrap()
.to_owned();
let out_dir = out_dir.replace('\\', "/");
writeln!(&out, "#[path = \"{}/{}.rs\"]", out_dir, file_stem)
.expect("failed to write to generated.");
writeln!(&out, "pub mod {}_proto;", file_stem).expect("failed to write to generated.");
}
}