2022-09-13 17:55:17 +00:00
|
|
|
// Copyright 2022 The ChromiumOS Authors
|
2022-04-14 08:15:23 +00:00
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
2022-03-25 23:10:28 +00:00
|
|
|
|
|
|
|
use std::env;
|
2023-03-13 08:09:13 +00:00
|
|
|
use std::fs;
|
2022-03-25 23:10:28 +00:00
|
|
|
use std::path::PathBuf;
|
|
|
|
|
2023-05-10 19:10:52 +00:00
|
|
|
use anyhow::bail;
|
2022-07-27 18:11:32 +00:00
|
|
|
use anyhow::Context;
|
|
|
|
use anyhow::Result;
|
|
|
|
use cbindgen::Config;
|
2023-04-28 05:38:25 +00:00
|
|
|
use cbindgen::EnumConfig;
|
2022-07-27 18:11:32 +00:00
|
|
|
use cbindgen::Language;
|
2023-04-28 05:38:25 +00:00
|
|
|
use cbindgen::RenameRule;
|
2023-03-13 08:09:13 +00:00
|
|
|
use tempfile::TempDir;
|
2022-04-14 08:56:47 +00:00
|
|
|
|
2022-09-13 17:55:17 +00:00
|
|
|
static COPYRIGHT_CLAUSE: &str = "// Copyright 2022 The ChromiumOS Authors
|
2022-06-20 08:19:59 +00:00
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.";
|
|
|
|
|
|
|
|
static AUTOGENERATED_DISCLAIMER: &str =
|
|
|
|
"/* Warning, this file is autogenerated by cbindgen. Don't modify this manually. */";
|
|
|
|
|
|
|
|
static INCLUDE_GUARD: &str = "CROSVM_CONTROL_H_";
|
|
|
|
|
2023-03-13 08:09:13 +00:00
|
|
|
static CROSVM_CONTROL_HEADER_NAME: &str = "crosvm_control.h";
|
|
|
|
|
2023-05-10 19:10:52 +00:00
|
|
|
static REGISTERED_EVENTS_PROTO_FILENAME: &str = "registered_events.proto";
|
|
|
|
static REGISTERED_EVENTS_PROTO_SRC: &str = "../protos/src";
|
|
|
|
|
2022-04-14 08:56:47 +00:00
|
|
|
fn main() -> Result<()> {
|
2022-04-14 08:59:29 +00:00
|
|
|
// Skip building dependencies when generating documents.
|
|
|
|
if std::env::var("CARGO_DOC").is_ok() {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
2023-05-10 19:10:52 +00:00
|
|
|
let proto_src =
|
|
|
|
PathBuf::from(REGISTERED_EVENTS_PROTO_SRC).join(REGISTERED_EVENTS_PROTO_FILENAME);
|
|
|
|
|
|
|
|
if !proto_src.exists() {
|
|
|
|
bail!(
|
|
|
|
"can't find {} in {}, won't be able to export for users",
|
|
|
|
REGISTERED_EVENTS_PROTO_FILENAME,
|
|
|
|
REGISTERED_EVENTS_PROTO_SRC
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-03-25 23:10:28 +00:00
|
|
|
let crate_dir = env::var("CARGO_MANIFEST_DIR").unwrap();
|
|
|
|
|
2023-03-13 08:09:13 +00:00
|
|
|
let output_dir = PathBuf::from(env::var("OUT_DIR").context("failed to get OUT_DIR")?);
|
|
|
|
|
2023-05-10 19:10:52 +00:00
|
|
|
let proto_out = output_dir.join(REGISTERED_EVENTS_PROTO_FILENAME);
|
|
|
|
|
|
|
|
fs::copy(proto_src, proto_out).context("couldn't copy proto to OUT_DIR")?;
|
|
|
|
|
2023-03-13 08:09:13 +00:00
|
|
|
let output_file = output_dir
|
|
|
|
.join(CROSVM_CONTROL_HEADER_NAME)
|
2022-03-25 23:10:28 +00:00
|
|
|
.display()
|
|
|
|
.to_string();
|
|
|
|
|
2022-06-20 08:19:59 +00:00
|
|
|
let config = Config {
|
|
|
|
language: Language::C,
|
|
|
|
cpp_compat: true,
|
|
|
|
header: Some(String::from(COPYRIGHT_CLAUSE)),
|
|
|
|
include_guard: Some(String::from(INCLUDE_GUARD)),
|
|
|
|
autogen_warning: Some(String::from(AUTOGENERATED_DISCLAIMER)),
|
|
|
|
include_version: true,
|
2023-04-28 05:38:25 +00:00
|
|
|
enumeration: EnumConfig {
|
|
|
|
rename_variants: RenameRule::ScreamingSnakeCase,
|
|
|
|
..Default::default()
|
|
|
|
},
|
2022-06-20 08:19:59 +00:00
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
|
2022-03-25 23:10:28 +00:00
|
|
|
cbindgen::Builder::new()
|
|
|
|
.with_crate(crate_dir)
|
2022-06-20 08:19:59 +00:00
|
|
|
.with_config(config)
|
2023-04-28 05:38:25 +00:00
|
|
|
.with_parse_deps(true)
|
|
|
|
.with_parse_include(&["swap"])
|
2022-03-25 23:10:28 +00:00
|
|
|
.generate()
|
2022-04-14 08:56:47 +00:00
|
|
|
.context("Unable to generate bindings")?
|
2023-03-31 21:57:46 +00:00
|
|
|
.write_to_file(output_file);
|
2022-04-14 08:56:47 +00:00
|
|
|
|
2023-03-13 08:09:13 +00:00
|
|
|
// Do not perform the compilation check on Windows since GCC might not be installed.
|
|
|
|
if std::env::var("CARGO_CFG_WINDOWS").is_ok() {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do a quick compile test of the generated header to ensure it is valid
|
|
|
|
let temp_dir = TempDir::new()?;
|
|
|
|
let test_file = temp_dir
|
|
|
|
.path()
|
|
|
|
.join("crosvm_control_test.c")
|
|
|
|
.display()
|
|
|
|
.to_string();
|
|
|
|
|
|
|
|
fs::write(
|
|
|
|
&test_file,
|
|
|
|
format!("{}{}{}", "#include \"", CROSVM_CONTROL_HEADER_NAME, "\""),
|
|
|
|
)
|
|
|
|
.context("Failed to write crosvm_control test C file")?;
|
|
|
|
|
|
|
|
cc::Build::new()
|
|
|
|
.include(output_dir)
|
|
|
|
.file(test_file)
|
|
|
|
.compile("crosvm_control_test");
|
|
|
|
|
2023-04-05 01:27:38 +00:00
|
|
|
// The above outputs cargo:rerun-if-env-changed directives, so we need to explicitly tell cargo
|
|
|
|
// to rerun this script if anything in src/ is changed.
|
2023-03-30 22:47:17 +00:00
|
|
|
println!("cargo:rerun-if-changed=src");
|
2022-04-14 08:56:47 +00:00
|
|
|
Ok(())
|
2022-03-25 23:10:28 +00:00
|
|
|
}
|