blob: c6adff73cd364bc67da387c5cd04113cc2c70515 [file] [log] [blame]
Mårten Kongstad867a3492023-04-25 15:06:30 +02001/*
2 * Copyright (C) 2023 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17//! `aconfig` is a build time tool to manage build time configurations, such as feature flags.
18
Mårten Kongstadcd414d4c2023-07-27 14:25:33 +020019use anyhow::{anyhow, bail, Context, Result};
Mårten Kongstad98b0eeb2023-05-08 11:25:30 +020020use clap::{builder::ArgAction, builder::EnumValueParser, Arg, ArgMatches, Command};
Mårten Kongstad6b9e3822023-05-16 11:19:58 +020021use core::any::Any;
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +020022use std::fs;
Mårten Kongstada1029092023-05-08 11:51:59 +020023use std::io;
24use std::io::Write;
Mårten Kongstadd42eeeb2023-05-12 10:01:00 +020025use std::path::{Path, PathBuf};
Mårten Kongstad867a3492023-04-25 15:06:30 +020026
Mårten Kongstad00cf0452023-05-26 16:48:01 +020027mod codegen;
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +020028mod commands;
Mårten Kongstadee58f982023-12-15 08:31:51 +010029mod dump;
Mårten Kongstadfe753f52023-04-26 09:13:03 +020030mod protos;
Dennis Shen0d1c5622023-12-01 21:04:29 +000031mod storage;
Mårten Kongstadfe753f52023-04-26 09:13:03 +020032
Zhi Doudcfa0402023-12-19 18:59:26 +000033use codegen::CodegenMode;
Mårten Kongstadee58f982023-12-15 08:31:51 +010034use dump::DumpFormat;
35
Mårten Kongstad83a87602023-06-02 11:20:15 +020036#[cfg(test)]
37mod test;
38
Zhi Doudcfa0402023-12-19 18:59:26 +000039use commands::{Input, OutputFile};
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +020040
Mårten Kongstad49e4d6e2023-12-15 10:21:57 +010041const HELP_DUMP_FILTER: &str = r#"
42Limit which flags to output. If multiple --filter arguments are provided, the output will be
43limited to flags that match any of the filters.
44"#;
45
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +020046fn cli() -> Command {
47 Command::new("aconfig")
48 .subcommand_required(true)
49 .subcommand(
50 Command::new("create-cache")
Mårten Kongstad9fb58962023-05-31 13:02:13 +020051 .arg(Arg::new("package").long("package").required(true))
Oriol Prieto Gasco7afc7e72023-11-22 13:26:02 +000052 // TODO(b/312769710): Make this argument required.
53 .arg(Arg::new("container").long("container"))
Mårten Kongstadfa23d292023-05-11 14:47:02 +020054 .arg(Arg::new("declarations").long("declarations").action(ArgAction::Append))
55 .arg(Arg::new("values").long("values").action(ArgAction::Append))
Zhi Dou24a0b6a2023-08-10 21:39:59 +000056 .arg(
57 Arg::new("default-permission")
58 .long("default-permission")
59 .value_parser(protos::flag_permission::parse_from_str)
60 .default_value(protos::flag_permission::to_string(
61 &commands::DEFAULT_FLAG_PERMISSION,
62 )),
63 )
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +020064 .arg(Arg::new("cache").long("cache").required(true)),
65 )
66 .subcommand(
Zhi Doueb744892023-05-10 04:02:33 +000067 Command::new("create-java-lib")
68 .arg(Arg::new("cache").long("cache").required(true))
Zhi Dou8ba6aa72023-06-26 21:03:40 +000069 .arg(Arg::new("out").long("out").required(true))
70 .arg(
71 Arg::new("mode")
72 .long("mode")
Zhi Doudcfa0402023-12-19 18:59:26 +000073 .value_parser(EnumValueParser::<CodegenMode>::new())
Zhi Dou8ba6aa72023-06-26 21:03:40 +000074 .default_value("production"),
75 ),
Zhi Doueb744892023-05-10 04:02:33 +000076 )
77 .subcommand(
Dennis Shen1dc9ad42023-05-12 00:21:55 +000078 Command::new("create-cpp-lib")
79 .arg(Arg::new("cache").long("cache").required(true))
Dennis Shen8d544f72023-06-29 00:45:42 +000080 .arg(Arg::new("out").long("out").required(true))
81 .arg(
82 Arg::new("mode")
83 .long("mode")
Zhi Doudcfa0402023-12-19 18:59:26 +000084 .value_parser(EnumValueParser::<CodegenMode>::new())
Dennis Shen8d544f72023-06-29 00:45:42 +000085 .default_value("production"),
86 ),
Dennis Shen1dc9ad42023-05-12 00:21:55 +000087 )
88 .subcommand(
Mårten Kongstadf73b9632023-05-24 15:43:47 +020089 Command::new("create-rust-lib")
90 .arg(Arg::new("cache").long("cache").required(true))
Dennis Shen3cfbcf52023-07-17 14:57:23 +000091 .arg(Arg::new("out").long("out").required(true))
92 .arg(
93 Arg::new("mode")
94 .long("mode")
Zhi Doudcfa0402023-12-19 18:59:26 +000095 .value_parser(EnumValueParser::<CodegenMode>::new())
Dennis Shen3cfbcf52023-07-17 14:57:23 +000096 .default_value("production"),
97 ),
Mårten Kongstadf73b9632023-05-24 15:43:47 +020098 )
99 .subcommand(
Mårten Kongstadf02734e2023-06-02 11:34:24 +0200100 Command::new("create-device-config-defaults")
101 .arg(Arg::new("cache").long("cache").action(ArgAction::Append).required(true))
102 .arg(Arg::new("out").long("out").default_value("-")),
103 )
104 .subcommand(
Mårten Kongstadc31a6ff2023-06-02 11:54:36 +0200105 Command::new("create-device-config-sysprops")
106 .arg(Arg::new("cache").long("cache").action(ArgAction::Append).required(true))
107 .arg(Arg::new("out").long("out").default_value("-")),
108 )
109 .subcommand(
Mårten Kongstada1029092023-05-08 11:51:59 +0200110 Command::new("dump")
Colin Cross6befb342023-11-28 15:55:07 -0800111 .arg(Arg::new("cache").long("cache").action(ArgAction::Append))
Mårten Kongstada1029092023-05-08 11:51:59 +0200112 .arg(
113 Arg::new("format")
114 .long("format")
Mårten Kongstadee58f982023-12-15 08:31:51 +0100115 .value_parser(|s: &str| DumpFormat::try_from(s))
Mårten Kongstada1029092023-05-08 11:51:59 +0200116 .default_value("text"),
117 )
Zhi Doudcfa0402023-12-19 18:59:26 +0000118 .arg(
119 Arg::new("filter")
120 .long("filter")
121 .action(ArgAction::Append)
122 .help(HELP_DUMP_FILTER.trim()),
123 )
Colin Cross6befb342023-11-28 15:55:07 -0800124 .arg(Arg::new("dedup").long("dedup").num_args(0).action(ArgAction::SetTrue))
Mårten Kongstada1029092023-05-08 11:51:59 +0200125 .arg(Arg::new("out").long("out").default_value("-")),
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +0200126 )
Dennis Shen0d1c5622023-12-01 21:04:29 +0000127 .subcommand(
128 Command::new("create-storage")
129 .arg(
130 Arg::new("container")
131 .long("container")
132 .required(true)
133 .help("The target container for the generated storage file."),
134 )
135 .arg(Arg::new("cache").long("cache").required(true))
136 .arg(Arg::new("out").long("out").required(true)),
137 )
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +0200138}
Mårten Kongstad867a3492023-04-25 15:06:30 +0200139
Mårten Kongstad6b9e3822023-05-16 11:19:58 +0200140fn get_required_arg<'a, T>(matches: &'a ArgMatches, arg_name: &str) -> Result<&'a T>
141where
142 T: Any + Clone + Send + Sync + 'static,
143{
144 matches
145 .get_one::<T>(arg_name)
146 .ok_or(anyhow!("internal error: required argument '{}' not found", arg_name))
147}
148
Oriol Prieto Gasco7afc7e72023-11-22 13:26:02 +0000149fn get_optional_arg<'a, T>(matches: &'a ArgMatches, arg_name: &str) -> Option<&'a T>
150where
151 T: Any + Clone + Send + Sync + 'static,
152{
153 matches.get_one::<T>(arg_name)
154}
155
Mårten Kongstad98b0eeb2023-05-08 11:25:30 +0200156fn open_zero_or_more_files(matches: &ArgMatches, arg_name: &str) -> Result<Vec<Input>> {
157 let mut opened_files = vec![];
158 for path in matches.get_many::<String>(arg_name).unwrap_or_default() {
159 let file = Box::new(fs::File::open(path)?);
Mårten Kongstad403658f2023-06-14 09:51:56 +0200160 opened_files.push(Input { source: path.to_string(), reader: file });
Mårten Kongstad98b0eeb2023-05-08 11:25:30 +0200161 }
162 Ok(opened_files)
163}
164
Mårten Kongstad403658f2023-06-14 09:51:56 +0200165fn open_single_file(matches: &ArgMatches, arg_name: &str) -> Result<Input> {
166 let Some(path) = matches.get_one::<String>(arg_name) else {
167 bail!("missing argument {}", arg_name);
168 };
169 let file = Box::new(fs::File::open(path)?);
170 Ok(Input { source: path.to_string(), reader: file })
171}
172
Mårten Kongstadd42eeeb2023-05-12 10:01:00 +0200173fn write_output_file_realtive_to_dir(root: &Path, output_file: &OutputFile) -> Result<()> {
Mårten Kongstadb5133f62023-09-11 11:10:02 +0200174 let path = root.join(&output_file.path);
Mårten Kongstadd42eeeb2023-05-12 10:01:00 +0200175 let parent = path
176 .parent()
177 .ok_or(anyhow!("unable to locate parent of output file {}", path.display()))?;
Mårten Kongstadcd414d4c2023-07-27 14:25:33 +0200178 fs::create_dir_all(parent)
179 .with_context(|| format!("failed to create directory {}", parent.display()))?;
Mårten Kongstadb5133f62023-09-11 11:10:02 +0200180 let mut file =
181 fs::File::create(&path).with_context(|| format!("failed to open {}", path.display()))?;
Mårten Kongstadcd414d4c2023-07-27 14:25:33 +0200182 file.write_all(&output_file.contents)
183 .with_context(|| format!("failed to write to {}", path.display()))?;
Mårten Kongstadd42eeeb2023-05-12 10:01:00 +0200184 Ok(())
185}
186
Mårten Kongstadf02734e2023-06-02 11:34:24 +0200187fn write_output_to_file_or_stdout(path: &str, data: &[u8]) -> Result<()> {
188 if path == "-" {
Mårten Kongstadcd414d4c2023-07-27 14:25:33 +0200189 io::stdout().write_all(data).context("failed to write to stdout")?;
Mårten Kongstadf02734e2023-06-02 11:34:24 +0200190 } else {
Mårten Kongstadcd414d4c2023-07-27 14:25:33 +0200191 fs::File::create(path)
192 .with_context(|| format!("failed to open {}", path))?
193 .write_all(data)
194 .with_context(|| format!("failed to write to {}", path))?;
Mårten Kongstadf02734e2023-06-02 11:34:24 +0200195 }
196 Ok(())
197}
198
Mårten Kongstadbb520722023-04-26 13:16:41 +0200199fn main() -> Result<()> {
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +0200200 let matches = cli().get_matches();
201 match matches.subcommand() {
202 Some(("create-cache", sub_matches)) => {
Mårten Kongstad9fb58962023-05-31 13:02:13 +0200203 let package = get_required_arg::<String>(sub_matches, "package")?;
Oriol Prieto Gasco7afc7e72023-11-22 13:26:02 +0000204 let container =
205 get_optional_arg::<String>(sub_matches, "container").map(|c| c.as_str());
Mårten Kongstadfa23d292023-05-11 14:47:02 +0200206 let declarations = open_zero_or_more_files(sub_matches, "declarations")?;
207 let values = open_zero_or_more_files(sub_matches, "values")?;
Zhi Dou24a0b6a2023-08-10 21:39:59 +0000208 let default_permission =
209 get_required_arg::<protos::ProtoFlagPermission>(sub_matches, "default-permission")?;
Oriol Prieto Gasco7afc7e72023-11-22 13:26:02 +0000210 let output = commands::parse_flags(
211 package,
212 container,
213 declarations,
214 values,
215 *default_permission,
216 )
217 .context("failed to create cache")?;
Mårten Kongstad6b9e3822023-05-16 11:19:58 +0200218 let path = get_required_arg::<String>(sub_matches, "cache")?;
Mårten Kongstad403658f2023-06-14 09:51:56 +0200219 write_output_to_file_or_stdout(path, &output)?;
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +0200220 }
Zhi Doueb744892023-05-10 04:02:33 +0000221 Some(("create-java-lib", sub_matches)) => {
Mårten Kongstad403658f2023-06-14 09:51:56 +0200222 let cache = open_single_file(sub_matches, "cache")?;
Zhi Dou8ba6aa72023-06-26 21:03:40 +0000223 let mode = get_required_arg::<CodegenMode>(sub_matches, "mode")?;
Mårten Kongstadcd414d4c2023-07-27 14:25:33 +0200224 let generated_files =
225 commands::create_java_lib(cache, *mode).context("failed to create java lib")?;
Mårten Kongstad403658f2023-06-14 09:51:56 +0200226 let dir = PathBuf::from(get_required_arg::<String>(sub_matches, "out")?);
Zhi Dou4655c962023-06-12 15:56:03 +0000227 generated_files
228 .iter()
229 .try_for_each(|file| write_output_file_realtive_to_dir(&dir, file))?;
Dennis Shen1dc9ad42023-05-12 00:21:55 +0000230 }
231 Some(("create-cpp-lib", sub_matches)) => {
Mårten Kongstad403658f2023-06-14 09:51:56 +0200232 let cache = open_single_file(sub_matches, "cache")?;
Dennis Shen8d544f72023-06-29 00:45:42 +0000233 let mode = get_required_arg::<CodegenMode>(sub_matches, "mode")?;
Mårten Kongstadcd414d4c2023-07-27 14:25:33 +0200234 let generated_files =
235 commands::create_cpp_lib(cache, *mode).context("failed to create cpp lib")?;
Mårten Kongstad403658f2023-06-14 09:51:56 +0200236 let dir = PathBuf::from(get_required_arg::<String>(sub_matches, "out")?);
Dennis Shen8d544f72023-06-29 00:45:42 +0000237 generated_files
238 .iter()
239 .try_for_each(|file| write_output_file_realtive_to_dir(&dir, file))?;
Zhi Doueb744892023-05-10 04:02:33 +0000240 }
Mårten Kongstadf73b9632023-05-24 15:43:47 +0200241 Some(("create-rust-lib", sub_matches)) => {
Mårten Kongstad403658f2023-06-14 09:51:56 +0200242 let cache = open_single_file(sub_matches, "cache")?;
Dennis Shen3cfbcf52023-07-17 14:57:23 +0000243 let mode = get_required_arg::<CodegenMode>(sub_matches, "mode")?;
Mårten Kongstadcd414d4c2023-07-27 14:25:33 +0200244 let generated_file =
245 commands::create_rust_lib(cache, *mode).context("failed to create rust lib")?;
Mårten Kongstad403658f2023-06-14 09:51:56 +0200246 let dir = PathBuf::from(get_required_arg::<String>(sub_matches, "out")?);
Mårten Kongstadf73b9632023-05-24 15:43:47 +0200247 write_output_file_realtive_to_dir(&dir, &generated_file)?;
248 }
Mårten Kongstadf02734e2023-06-02 11:34:24 +0200249 Some(("create-device-config-defaults", sub_matches)) => {
Mårten Kongstad403658f2023-06-14 09:51:56 +0200250 let cache = open_single_file(sub_matches, "cache")?;
Mårten Kongstadcd414d4c2023-07-27 14:25:33 +0200251 let output = commands::create_device_config_defaults(cache)
252 .context("failed to create device config defaults")?;
Mårten Kongstadf02734e2023-06-02 11:34:24 +0200253 let path = get_required_arg::<String>(sub_matches, "out")?;
254 write_output_to_file_or_stdout(path, &output)?;
255 }
Mårten Kongstadc31a6ff2023-06-02 11:54:36 +0200256 Some(("create-device-config-sysprops", sub_matches)) => {
Mårten Kongstad403658f2023-06-14 09:51:56 +0200257 let cache = open_single_file(sub_matches, "cache")?;
Mårten Kongstadcd414d4c2023-07-27 14:25:33 +0200258 let output = commands::create_device_config_sysprops(cache)
259 .context("failed to create device config sysprops")?;
Mårten Kongstadc31a6ff2023-06-02 11:54:36 +0200260 let path = get_required_arg::<String>(sub_matches, "out")?;
261 write_output_to_file_or_stdout(path, &output)?;
262 }
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +0200263 Some(("dump", sub_matches)) => {
Mårten Kongstad403658f2023-06-14 09:51:56 +0200264 let input = open_zero_or_more_files(sub_matches, "cache")?;
Mårten Kongstadcd414d4c2023-07-27 14:25:33 +0200265 let format = get_required_arg::<DumpFormat>(sub_matches, "format")
266 .context("failed to dump previously parsed flags")?;
Mårten Kongstad49e4d6e2023-12-15 10:21:57 +0100267 let filters = sub_matches
268 .get_many::<String>("filter")
269 .unwrap_or_default()
270 .map(String::as_ref)
271 .collect::<Vec<_>>();
Colin Cross6befb342023-11-28 15:55:07 -0800272 let dedup = get_required_arg::<bool>(sub_matches, "dedup")?;
Mårten Kongstad49e4d6e2023-12-15 10:21:57 +0100273 let output = commands::dump_parsed_flags(input, format.clone(), &filters, *dedup)?;
Mårten Kongstad6b9e3822023-05-16 11:19:58 +0200274 let path = get_required_arg::<String>(sub_matches, "out")?;
Mårten Kongstadf02734e2023-06-02 11:34:24 +0200275 write_output_to_file_or_stdout(path, &output)?;
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +0200276 }
Dennis Shen0d1c5622023-12-01 21:04:29 +0000277 Some(("create-storage", sub_matches)) => {
278 let cache = open_zero_or_more_files(sub_matches, "cache")?;
279 let container = get_required_arg::<String>(sub_matches, "container")?;
280 let dir = PathBuf::from(get_required_arg::<String>(sub_matches, "out")?);
281 let generated_files = commands::create_storage(cache, container)
282 .context("failed to create storage files")?;
283 generated_files
284 .iter()
285 .try_for_each(|file| write_output_file_realtive_to_dir(&dir, file))?;
286 }
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +0200287 _ => unreachable!(),
288 }
Mårten Kongstadbb520722023-04-26 13:16:41 +0200289 Ok(())
Mårten Kongstad867a3492023-04-25 15:06:30 +0200290}