Mike Frysinger | a73e9e4 | 2022-09-08 03:37:14 -0400 | [diff] [blame] | 1 | // Copyright 2019 The ChromiumOS Authors |
Allen Webb | ed175c7 | 2019-09-25 10:36:34 -0700 | [diff] [blame] | 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 | |
Allen Webb | 40f89cd | 2021-09-01 16:59:27 -0500 | [diff] [blame] | 22 | const OPTS: Option<&[&str]> = None; |
| 23 | |
Allen Webb | ed175c7 | 2019-09-25 10:36:34 -0700 | [diff] [blame] | 24 | // (<module name>, <relative path to source xml>) |
| 25 | // 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] | 26 | // 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] | 27 | const BINDINGS_TO_GENERATE: &[(&str, &str, BindingsType)] = &[ |
Allen Webb | ed175c7 | 2019-09-25 10:36:34 -0700 | [diff] [blame] | 28 | ( |
| 29 | "org_chromium_authpolicy", |
| 30 | "authpolicy/dbus_bindings/org.chromium.AuthPolicy.xml", |
Allen Webb | 40f89cd | 2021-09-01 16:59:27 -0500 | [diff] [blame] | 31 | BindingsType::Client(OPTS), |
Allen Webb | ed175c7 | 2019-09-25 10:36:34 -0700 | [diff] [blame] | 32 | ), |
| 33 | ( |
| 34 | "org_chromium_debugd", |
| 35 | "debugd/dbus_bindings/org.chromium.debugd.xml", |
Allen Webb | 40f89cd | 2021-09-01 16:59:27 -0500 | [diff] [blame] | 36 | BindingsType::Client(OPTS), |
Allen Webb | ed175c7 | 2019-09-25 10:36:34 -0700 | [diff] [blame] | 37 | ), |
| 38 | ( |
Chih-Yang Hsia | 9aac35e | 2022-07-20 13:48:02 +0000 | [diff] [blame] | 39 | "org_chromium_dlcservice", |
| 40 | "dlcservice/dbus_adaptors/org.chromium.DlcServiceInterface.xml", |
| 41 | BindingsType::Client(OPTS), |
| 42 | ), |
| 43 | ( |
Jie Jiang | ca887ae | 2021-06-03 21:04:58 +0900 | [diff] [blame] | 44 | "org_chromium_flimflam_manager", |
| 45 | "shill/dbus_bindings/org.chromium.flimflam.Manager.dbus-xml", |
Allen Webb | 40f89cd | 2021-09-01 16:59:27 -0500 | [diff] [blame] | 46 | BindingsType::Client(OPTS), |
Jie Jiang | ca887ae | 2021-06-03 21:04:58 +0900 | [diff] [blame] | 47 | ), |
| 48 | ( |
| 49 | "org_chromium_flimflam_service", |
| 50 | "shill/dbus_bindings/org.chromium.flimflam.Service.dbus-xml", |
Allen Webb | 40f89cd | 2021-09-01 16:59:27 -0500 | [diff] [blame] | 51 | BindingsType::Client(OPTS), |
Jie Jiang | ca887ae | 2021-06-03 21:04:58 +0900 | [diff] [blame] | 52 | ), |
| 53 | ( |
Evan Green | 1b3c809 | 2022-04-07 09:03:38 -0700 | [diff] [blame] | 54 | "org_chromium_power_manager", |
| 55 | "power_manager/dbus_bindings/org.chromium.PowerManager.xml", |
| 56 | BindingsType::Client(OPTS), |
| 57 | ), |
| 58 | ( |
Allen Webb | ed175c7 | 2019-09-25 10:36:34 -0700 | [diff] [blame] | 59 | "org_chromium_sessionmanagerinterface", |
| 60 | "login_manager/dbus_bindings/org.chromium.SessionManagerInterface.xml", |
Allen Webb | 40f89cd | 2021-09-01 16:59:27 -0500 | [diff] [blame] | 61 | BindingsType::Client(OPTS), |
Allen Webb | ed175c7 | 2019-09-25 10:36:34 -0700 | [diff] [blame] | 62 | ), |
Ryo Hashimoto | 9976901 | 2021-07-08 20:34:43 +0900 | [diff] [blame] | 63 | ( |
| 64 | "org_chromium_userdataauth", |
| 65 | "cryptohome/dbus_bindings/org.chromium.UserDataAuth.xml", |
Allen Webb | 40f89cd | 2021-09-01 16:59:27 -0500 | [diff] [blame] | 66 | BindingsType::Client(OPTS), |
Ryo Hashimoto | 9976901 | 2021-07-08 20:34:43 +0900 | [diff] [blame] | 67 | ), |
Yi Chou | 343a26d | 2022-06-10 18:41:35 +0800 | [diff] [blame] | 68 | ( |
| 69 | "org_chromium_vtpm", |
| 70 | "vtpm/dbus_bindings/org.chromium.Vtpm.xml", |
| 71 | BindingsType::Client(OPTS), |
| 72 | ), |
Allen Webb | ed175c7 | 2019-09-25 10:36:34 -0700 | [diff] [blame] | 73 | ]; |
| 74 | |
Ryo Hashimoto | 82dd68c | 2021-08-30 22:26:21 +0900 | [diff] [blame] | 75 | // (<module name>, <relative path to .proto file>) |
| 76 | // When adding additional protos, remember to include the source project and subtree in the |
| 77 | // ebuild. Otherwise, the source files will not be accessible when building dev-rust/system_api. |
| 78 | const PROTOS_TO_GENERATE: &[(&str, &str)] = &[ |
Bryan Yu | 03955af | 2022-07-22 15:39:30 +0900 | [diff] [blame] | 79 | ("arc", "system_api/dbus/arc/arc.proto"), |
Hardik Goyal | 0e8404a | 2021-12-06 02:40:08 -0800 | [diff] [blame] | 80 | ( |
| 81 | "auth_factor", |
| 82 | "system_api/dbus/cryptohome/auth_factor.proto", |
| 83 | ), |
Max Lee | 125b2ef | 2022-08-24 13:35:15 +1000 | [diff] [blame] | 84 | ( |
| 85 | "concierge_service", |
| 86 | "system_api/dbus/vm_concierge/concierge_service.proto", |
| 87 | ), |
Chih-Yang Hsia | 9aac35e | 2022-07-20 13:48:02 +0000 | [diff] [blame] | 88 | ("dlcservice", "system_api/dbus/dlcservice/dlcservice.proto"), |
Ryo Hashimoto | 82dd68c | 2021-08-30 22:26:21 +0900 | [diff] [blame] | 89 | ("fido", "system_api/dbus/cryptohome/fido.proto"), |
| 90 | ("key", "system_api/dbus/cryptohome/key.proto"), |
| 91 | ("rpc", "system_api/dbus/cryptohome/rpc.proto"), |
| 92 | ( |
Max Lee | 125b2ef | 2022-08-24 13:35:15 +1000 | [diff] [blame] | 93 | "shadercached", |
| 94 | "system_api/dbus/shadercached/shadercached.proto", |
| 95 | ), |
| 96 | ( |
Ryo Hashimoto | 82dd68c | 2021-08-30 22:26:21 +0900 | [diff] [blame] | 97 | "UserDataAuth", |
| 98 | "system_api/dbus/cryptohome/UserDataAuth.proto", |
| 99 | ), |
Yi Chou | 343a26d | 2022-06-10 18:41:35 +0800 | [diff] [blame] | 100 | ("vtpm_interface", "vtpm/vtpm_interface.proto"), |
Evan Green | 5409d5c | 2022-10-20 13:22:40 -0700 | [diff] [blame^] | 101 | ( |
| 102 | "update_engine", |
| 103 | "system_api/dbus/update_engine/update_engine.proto", |
| 104 | ), |
Ryo Hashimoto | 82dd68c | 2021-08-30 22:26:21 +0900 | [diff] [blame] | 105 | ]; |
| 106 | |
| 107 | fn generate_protos(source_dir: &Path, protos: &[(&str, &str)]) -> Result<()> { |
| 108 | let out_dir = PathBuf::from("src/protos"); |
| 109 | if out_dir.exists() { |
| 110 | // If CROS_RUST is set, skip generation. |
| 111 | if env::var("CROS_RUST") == Ok(String::from("1")) { |
| 112 | return Ok(()); |
| 113 | } |
| 114 | fs::remove_dir_all(&out_dir)?; |
| 115 | } |
| 116 | fs::create_dir_all(&out_dir)?; |
| 117 | |
| 118 | let mut out = File::create(out_dir.join("include_protos.rs"))?; |
| 119 | |
| 120 | for (module, input_path) in protos { |
| 121 | let input_path = source_dir.join(input_path); |
| 122 | let input_dir = input_path.parent().unwrap(); |
Bryan Yu | 03955af | 2022-07-22 15:39:30 +0900 | [diff] [blame] | 123 | let parent_input_dir = source_dir.join("system_api/dbus"); |
Ryo Hashimoto | 82dd68c | 2021-08-30 22:26:21 +0900 | [diff] [blame] | 124 | |
| 125 | // Invoke protobuf compiler. |
| 126 | protoc_rust::Codegen::new() |
| 127 | .input(input_path.as_os_str().to_str().unwrap()) |
| 128 | .include(input_dir.as_os_str().to_str().unwrap()) |
Bryan Yu | 03955af | 2022-07-22 15:39:30 +0900 | [diff] [blame] | 129 | .include(parent_input_dir) |
Ryo Hashimoto | 82dd68c | 2021-08-30 22:26:21 +0900 | [diff] [blame] | 130 | .out_dir(&out_dir) |
| 131 | .run() |
| 132 | .expect("protoc"); |
| 133 | |
| 134 | // Write out a `mod` that refers to the generated module. |
| 135 | writeln!(out, "pub mod {};", module)?; |
| 136 | } |
| 137 | Ok(()) |
| 138 | } |
| 139 | |
Allen Webb | ed175c7 | 2019-09-25 10:36:34 -0700 | [diff] [blame] | 140 | fn main() { |
| 141 | generate_module(Path::new(SOURCE_DIR), BINDINGS_TO_GENERATE).unwrap(); |
Ryo Hashimoto | 82dd68c | 2021-08-30 22:26:21 +0900 | [diff] [blame] | 142 | generate_protos(Path::new(SOURCE_DIR), PROTOS_TO_GENERATE).unwrap(); |
Allen Webb | ed175c7 | 2019-09-25 10:36:34 -0700 | [diff] [blame] | 143 | } |