blob: 2656d6cd995a2c22962a333b3c04e9c9094c7b95 [file] [log] [blame]
Allen Webbed175c72019-09-25 10:36:34 -07001// 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 Hashimoto82dd68c2021-08-30 22:26:21 +09005// 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 Webbed175c72019-09-25 10:36:34 -07008
Ryo Hashimoto82dd68c2021-08-30 22:26:21 +09009use std::env;
10use std::error::Error;
11use std::fs::{self, File};
12use std::io::Write;
13use std::path::{Path, PathBuf};
14
15type Result<T> = std::result::Result<T, Box<dyn Error>>;
Allen Webbed175c72019-09-25 10:36:34 -070016
Allen Webb91bf46e2020-09-18 12:02:28 -070017use chromeos_dbus_bindings::{self, generate_module, BindingsType};
Allen Webbed175c72019-09-25 10:36:34 -070018
19// The parent path of system_api.
20const SOURCE_DIR: &str = "..";
21
Allen Webb40f89cd2021-09-01 16:59:27 -050022const OPTS: Option<&[&str]> = None;
23
Allen Webbed175c72019-09-25 10:36:34 -070024// (<module name>, <relative path to source xml>)
25// When adding additional bindings, remember to include the source project and subtree in the
Ryo Hashimoto82dd68c2021-08-30 22:26:21 +090026// ebuild. Otherwise, the source files will not be accessible when building dev-rust/system_api.
Allen Webb91bf46e2020-09-18 12:02:28 -070027const BINDINGS_TO_GENERATE: &[(&str, &str, BindingsType)] = &[
Allen Webbed175c72019-09-25 10:36:34 -070028 (
29 "org_chromium_authpolicy",
30 "authpolicy/dbus_bindings/org.chromium.AuthPolicy.xml",
Allen Webb40f89cd2021-09-01 16:59:27 -050031 BindingsType::Client(OPTS),
Allen Webbed175c72019-09-25 10:36:34 -070032 ),
33 (
34 "org_chromium_debugd",
35 "debugd/dbus_bindings/org.chromium.debugd.xml",
Allen Webb40f89cd2021-09-01 16:59:27 -050036 BindingsType::Client(OPTS),
Allen Webbed175c72019-09-25 10:36:34 -070037 ),
38 (
Jie Jiangca887ae2021-06-03 21:04:58 +090039 "org_chromium_flimflam_manager",
40 "shill/dbus_bindings/org.chromium.flimflam.Manager.dbus-xml",
Allen Webb40f89cd2021-09-01 16:59:27 -050041 BindingsType::Client(OPTS),
Jie Jiangca887ae2021-06-03 21:04:58 +090042 ),
43 (
44 "org_chromium_flimflam_service",
45 "shill/dbus_bindings/org.chromium.flimflam.Service.dbus-xml",
Allen Webb40f89cd2021-09-01 16:59:27 -050046 BindingsType::Client(OPTS),
Jie Jiangca887ae2021-06-03 21:04:58 +090047 ),
48 (
Evan Green1b3c8092022-04-07 09:03:38 -070049 "org_chromium_power_manager",
50 "power_manager/dbus_bindings/org.chromium.PowerManager.xml",
51 BindingsType::Client(OPTS),
52 ),
53 (
Allen Webbed175c72019-09-25 10:36:34 -070054 "org_chromium_sessionmanagerinterface",
55 "login_manager/dbus_bindings/org.chromium.SessionManagerInterface.xml",
Allen Webb40f89cd2021-09-01 16:59:27 -050056 BindingsType::Client(OPTS),
Allen Webbed175c72019-09-25 10:36:34 -070057 ),
Ryo Hashimoto99769012021-07-08 20:34:43 +090058 (
59 "org_chromium_userdataauth",
60 "cryptohome/dbus_bindings/org.chromium.UserDataAuth.xml",
Allen Webb40f89cd2021-09-01 16:59:27 -050061 BindingsType::Client(OPTS),
Ryo Hashimoto99769012021-07-08 20:34:43 +090062 ),
Yi Chou343a26d2022-06-10 18:41:35 +080063 (
64 "org_chromium_vtpm",
65 "vtpm/dbus_bindings/org.chromium.Vtpm.xml",
66 BindingsType::Client(OPTS),
67 ),
Allen Webbed175c72019-09-25 10:36:34 -070068];
69
Ryo Hashimoto82dd68c2021-08-30 22:26:21 +090070// (<module name>, <relative path to .proto file>)
71// When adding additional protos, remember to include the source project and subtree in the
72// ebuild. Otherwise, the source files will not be accessible when building dev-rust/system_api.
73const PROTOS_TO_GENERATE: &[(&str, &str)] = &[
Hardik Goyal0e8404a2021-12-06 02:40:08 -080074 (
75 "auth_factor",
76 "system_api/dbus/cryptohome/auth_factor.proto",
77 ),
Ryo Hashimoto82dd68c2021-08-30 22:26:21 +090078 ("fido", "system_api/dbus/cryptohome/fido.proto"),
79 ("key", "system_api/dbus/cryptohome/key.proto"),
80 ("rpc", "system_api/dbus/cryptohome/rpc.proto"),
81 (
82 "UserDataAuth",
83 "system_api/dbus/cryptohome/UserDataAuth.proto",
84 ),
Yi Chou343a26d2022-06-10 18:41:35 +080085 ("vtpm_interface", "vtpm/vtpm_interface.proto"),
Ryo Hashimoto82dd68c2021-08-30 22:26:21 +090086];
87
88fn generate_protos(source_dir: &Path, protos: &[(&str, &str)]) -> Result<()> {
89 let out_dir = PathBuf::from("src/protos");
90 if out_dir.exists() {
91 // If CROS_RUST is set, skip generation.
92 if env::var("CROS_RUST") == Ok(String::from("1")) {
93 return Ok(());
94 }
95 fs::remove_dir_all(&out_dir)?;
96 }
97 fs::create_dir_all(&out_dir)?;
98
99 let mut out = File::create(out_dir.join("include_protos.rs"))?;
100
101 for (module, input_path) in protos {
102 let input_path = source_dir.join(input_path);
103 let input_dir = input_path.parent().unwrap();
104
105 // Invoke protobuf compiler.
106 protoc_rust::Codegen::new()
107 .input(input_path.as_os_str().to_str().unwrap())
108 .include(input_dir.as_os_str().to_str().unwrap())
109 .out_dir(&out_dir)
110 .run()
111 .expect("protoc");
112
113 // Write out a `mod` that refers to the generated module.
114 writeln!(out, "pub mod {};", module)?;
115 }
116 Ok(())
117}
118
Allen Webbed175c72019-09-25 10:36:34 -0700119fn main() {
120 generate_module(Path::new(SOURCE_DIR), BINDINGS_TO_GENERATE).unwrap();
Ryo Hashimoto82dd68c2021-08-30 22:26:21 +0900121 generate_protos(Path::new(SOURCE_DIR), PROTOS_TO_GENERATE).unwrap();
Allen Webbed175c72019-09-25 10:36:34 -0700122}