Allen Webb | ed175c7 | 2019-09-25 10:36:34 -0700 | [diff] [blame] | 1 | // Copyright 2019 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Ryo Hashimoto | 82dd68c | 2021-08-30 22:26:21 +0900 | [diff] [blame^] | 5 | // Generates the Rust D-Bus bindings and protobuf definitions for system_api. |
| 6 | // The generated bindings are included in the published crate since the source XML files are only |
| 7 | // available from the original path or the ebuild. |
Allen Webb | ed175c7 | 2019-09-25 10:36:34 -0700 | [diff] [blame] | 8 | |
Ryo Hashimoto | 82dd68c | 2021-08-30 22:26:21 +0900 | [diff] [blame^] | 9 | use std::env; |
| 10 | use std::error::Error; |
| 11 | use std::fs::{self, File}; |
| 12 | use std::io::Write; |
| 13 | use std::path::{Path, PathBuf}; |
| 14 | |
| 15 | type Result<T> = std::result::Result<T, Box<dyn Error>>; |
Allen Webb | ed175c7 | 2019-09-25 10:36:34 -0700 | [diff] [blame] | 16 | |
Allen Webb | 91bf46e | 2020-09-18 12:02:28 -0700 | [diff] [blame] | 17 | use chromeos_dbus_bindings::{self, generate_module, BindingsType}; |
Allen Webb | ed175c7 | 2019-09-25 10:36:34 -0700 | [diff] [blame] | 18 | |
| 19 | // The parent path of system_api. |
| 20 | const SOURCE_DIR: &str = ".."; |
| 21 | |
| 22 | // (<module name>, <relative path to source xml>) |
| 23 | // When adding additional bindings, remember to include the source project and subtree in the |
Ryo Hashimoto | 82dd68c | 2021-08-30 22:26:21 +0900 | [diff] [blame^] | 24 | // ebuild. Otherwise, the source files will not be accessible when building dev-rust/system_api. |
Allen Webb | 91bf46e | 2020-09-18 12:02:28 -0700 | [diff] [blame] | 25 | const BINDINGS_TO_GENERATE: &[(&str, &str, BindingsType)] = &[ |
Allen Webb | ed175c7 | 2019-09-25 10:36:34 -0700 | [diff] [blame] | 26 | ( |
| 27 | "org_chromium_authpolicy", |
| 28 | "authpolicy/dbus_bindings/org.chromium.AuthPolicy.xml", |
Allen Webb | 91bf46e | 2020-09-18 12:02:28 -0700 | [diff] [blame] | 29 | BindingsType::Client, |
Allen Webb | ed175c7 | 2019-09-25 10:36:34 -0700 | [diff] [blame] | 30 | ), |
| 31 | ( |
| 32 | "org_chromium_debugd", |
| 33 | "debugd/dbus_bindings/org.chromium.debugd.xml", |
Allen Webb | 91bf46e | 2020-09-18 12:02:28 -0700 | [diff] [blame] | 34 | BindingsType::Client, |
Allen Webb | ed175c7 | 2019-09-25 10:36:34 -0700 | [diff] [blame] | 35 | ), |
| 36 | ( |
Jie Jiang | ca887ae | 2021-06-03 21:04:58 +0900 | [diff] [blame] | 37 | "org_chromium_flimflam_manager", |
| 38 | "shill/dbus_bindings/org.chromium.flimflam.Manager.dbus-xml", |
| 39 | BindingsType::Client, |
| 40 | ), |
| 41 | ( |
| 42 | "org_chromium_flimflam_service", |
| 43 | "shill/dbus_bindings/org.chromium.flimflam.Service.dbus-xml", |
| 44 | BindingsType::Client, |
| 45 | ), |
| 46 | ( |
Allen Webb | ed175c7 | 2019-09-25 10:36:34 -0700 | [diff] [blame] | 47 | "org_chromium_sessionmanagerinterface", |
| 48 | "login_manager/dbus_bindings/org.chromium.SessionManagerInterface.xml", |
Allen Webb | 91bf46e | 2020-09-18 12:02:28 -0700 | [diff] [blame] | 49 | BindingsType::Client, |
Allen Webb | ed175c7 | 2019-09-25 10:36:34 -0700 | [diff] [blame] | 50 | ), |
Ryo Hashimoto | 9976901 | 2021-07-08 20:34:43 +0900 | [diff] [blame] | 51 | ( |
| 52 | "org_chromium_userdataauth", |
| 53 | "cryptohome/dbus_bindings/org.chromium.UserDataAuth.xml", |
| 54 | BindingsType::Client, |
| 55 | ), |
Allen Webb | ed175c7 | 2019-09-25 10:36:34 -0700 | [diff] [blame] | 56 | ]; |
| 57 | |
Ryo Hashimoto | 82dd68c | 2021-08-30 22:26:21 +0900 | [diff] [blame^] | 58 | // (<module name>, <relative path to .proto file>) |
| 59 | // When adding additional protos, remember to include the source project and subtree in the |
| 60 | // ebuild. Otherwise, the source files will not be accessible when building dev-rust/system_api. |
| 61 | const PROTOS_TO_GENERATE: &[(&str, &str)] = &[ |
| 62 | ("fido", "system_api/dbus/cryptohome/fido.proto"), |
| 63 | ("key", "system_api/dbus/cryptohome/key.proto"), |
| 64 | ("rpc", "system_api/dbus/cryptohome/rpc.proto"), |
| 65 | ( |
| 66 | "UserDataAuth", |
| 67 | "system_api/dbus/cryptohome/UserDataAuth.proto", |
| 68 | ), |
| 69 | ]; |
| 70 | |
| 71 | fn generate_protos(source_dir: &Path, protos: &[(&str, &str)]) -> Result<()> { |
| 72 | let out_dir = PathBuf::from("src/protos"); |
| 73 | if out_dir.exists() { |
| 74 | // If CROS_RUST is set, skip generation. |
| 75 | if env::var("CROS_RUST") == Ok(String::from("1")) { |
| 76 | return Ok(()); |
| 77 | } |
| 78 | fs::remove_dir_all(&out_dir)?; |
| 79 | } |
| 80 | fs::create_dir_all(&out_dir)?; |
| 81 | |
| 82 | let mut out = File::create(out_dir.join("include_protos.rs"))?; |
| 83 | |
| 84 | for (module, input_path) in protos { |
| 85 | let input_path = source_dir.join(input_path); |
| 86 | let input_dir = input_path.parent().unwrap(); |
| 87 | |
| 88 | // Invoke protobuf compiler. |
| 89 | protoc_rust::Codegen::new() |
| 90 | .input(input_path.as_os_str().to_str().unwrap()) |
| 91 | .include(input_dir.as_os_str().to_str().unwrap()) |
| 92 | .out_dir(&out_dir) |
| 93 | .run() |
| 94 | .expect("protoc"); |
| 95 | |
| 96 | // Write out a `mod` that refers to the generated module. |
| 97 | writeln!(out, "pub mod {};", module)?; |
| 98 | } |
| 99 | Ok(()) |
| 100 | } |
| 101 | |
Allen Webb | ed175c7 | 2019-09-25 10:36:34 -0700 | [diff] [blame] | 102 | fn main() { |
| 103 | generate_module(Path::new(SOURCE_DIR), BINDINGS_TO_GENERATE).unwrap(); |
Ryo Hashimoto | 82dd68c | 2021-08-30 22:26:21 +0900 | [diff] [blame^] | 104 | generate_protos(Path::new(SOURCE_DIR), PROTOS_TO_GENERATE).unwrap(); |
Allen Webb | ed175c7 | 2019-09-25 10:36:34 -0700 | [diff] [blame] | 105 | } |