blob: 6872809f4d580d929ba34f832465299e69c9ae6e [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 Kongstadfe753f52023-04-26 09:13:03 +020029mod protos;
Mårten Kongstadfe753f52023-04-26 09:13:03 +020030
Mårten Kongstad83a87602023-06-02 11:20:15 +020031#[cfg(test)]
32mod test;
33
Zhi Dou8ba6aa72023-06-26 21:03:40 +000034use commands::{CodegenMode, DumpFormat, Input, OutputFile};
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +020035
36fn cli() -> Command {
37 Command::new("aconfig")
38 .subcommand_required(true)
39 .subcommand(
40 Command::new("create-cache")
Mårten Kongstad9fb58962023-05-31 13:02:13 +020041 .arg(Arg::new("package").long("package").required(true))
Oriol Prieto Gasco7afc7e72023-11-22 13:26:02 +000042 // TODO(b/312769710): Make this argument required.
43 .arg(Arg::new("container").long("container"))
Mårten Kongstadfa23d292023-05-11 14:47:02 +020044 .arg(Arg::new("declarations").long("declarations").action(ArgAction::Append))
45 .arg(Arg::new("values").long("values").action(ArgAction::Append))
Zhi Dou24a0b6a2023-08-10 21:39:59 +000046 .arg(
47 Arg::new("default-permission")
48 .long("default-permission")
49 .value_parser(protos::flag_permission::parse_from_str)
50 .default_value(protos::flag_permission::to_string(
51 &commands::DEFAULT_FLAG_PERMISSION,
52 )),
53 )
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +020054 .arg(Arg::new("cache").long("cache").required(true)),
55 )
56 .subcommand(
Zhi Doueb744892023-05-10 04:02:33 +000057 Command::new("create-java-lib")
58 .arg(Arg::new("cache").long("cache").required(true))
Zhi Dou8ba6aa72023-06-26 21:03:40 +000059 .arg(Arg::new("out").long("out").required(true))
60 .arg(
61 Arg::new("mode")
62 .long("mode")
63 .value_parser(EnumValueParser::<commands::CodegenMode>::new())
64 .default_value("production"),
65 ),
Zhi Doueb744892023-05-10 04:02:33 +000066 )
67 .subcommand(
Dennis Shen1dc9ad42023-05-12 00:21:55 +000068 Command::new("create-cpp-lib")
69 .arg(Arg::new("cache").long("cache").required(true))
Dennis Shen8d544f72023-06-29 00:45:42 +000070 .arg(Arg::new("out").long("out").required(true))
71 .arg(
72 Arg::new("mode")
73 .long("mode")
74 .value_parser(EnumValueParser::<commands::CodegenMode>::new())
75 .default_value("production"),
76 ),
Dennis Shen1dc9ad42023-05-12 00:21:55 +000077 )
78 .subcommand(
Mårten Kongstadf73b9632023-05-24 15:43:47 +020079 Command::new("create-rust-lib")
80 .arg(Arg::new("cache").long("cache").required(true))
Dennis Shen3cfbcf52023-07-17 14:57:23 +000081 .arg(Arg::new("out").long("out").required(true))
82 .arg(
83 Arg::new("mode")
84 .long("mode")
85 .value_parser(EnumValueParser::<commands::CodegenMode>::new())
86 .default_value("production"),
87 ),
Mårten Kongstadf73b9632023-05-24 15:43:47 +020088 )
89 .subcommand(
Mårten Kongstadf02734e2023-06-02 11:34:24 +020090 Command::new("create-device-config-defaults")
91 .arg(Arg::new("cache").long("cache").action(ArgAction::Append).required(true))
92 .arg(Arg::new("out").long("out").default_value("-")),
93 )
94 .subcommand(
Mårten Kongstadc31a6ff2023-06-02 11:54:36 +020095 Command::new("create-device-config-sysprops")
96 .arg(Arg::new("cache").long("cache").action(ArgAction::Append).required(true))
97 .arg(Arg::new("out").long("out").default_value("-")),
98 )
99 .subcommand(
Mårten Kongstada1029092023-05-08 11:51:59 +0200100 Command::new("dump")
Colin Cross6befb342023-11-28 15:55:07 -0800101 .arg(Arg::new("cache").long("cache").action(ArgAction::Append))
Mårten Kongstada1029092023-05-08 11:51:59 +0200102 .arg(
103 Arg::new("format")
104 .long("format")
Mårten Kongstadba94e6a2023-05-16 11:00:16 +0200105 .value_parser(EnumValueParser::<commands::DumpFormat>::new())
Mårten Kongstada1029092023-05-08 11:51:59 +0200106 .default_value("text"),
107 )
Colin Cross6befb342023-11-28 15:55:07 -0800108 .arg(Arg::new("dedup").long("dedup").num_args(0).action(ArgAction::SetTrue))
Mårten Kongstada1029092023-05-08 11:51:59 +0200109 .arg(Arg::new("out").long("out").default_value("-")),
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +0200110 )
111}
Mårten Kongstad867a3492023-04-25 15:06:30 +0200112
Mårten Kongstad6b9e3822023-05-16 11:19:58 +0200113fn get_required_arg<'a, T>(matches: &'a ArgMatches, arg_name: &str) -> Result<&'a T>
114where
115 T: Any + Clone + Send + Sync + 'static,
116{
117 matches
118 .get_one::<T>(arg_name)
119 .ok_or(anyhow!("internal error: required argument '{}' not found", arg_name))
120}
121
Oriol Prieto Gasco7afc7e72023-11-22 13:26:02 +0000122fn get_optional_arg<'a, T>(matches: &'a ArgMatches, arg_name: &str) -> Option<&'a T>
123where
124 T: Any + Clone + Send + Sync + 'static,
125{
126 matches.get_one::<T>(arg_name)
127}
128
Mårten Kongstad98b0eeb2023-05-08 11:25:30 +0200129fn open_zero_or_more_files(matches: &ArgMatches, arg_name: &str) -> Result<Vec<Input>> {
130 let mut opened_files = vec![];
131 for path in matches.get_many::<String>(arg_name).unwrap_or_default() {
132 let file = Box::new(fs::File::open(path)?);
Mårten Kongstad403658f2023-06-14 09:51:56 +0200133 opened_files.push(Input { source: path.to_string(), reader: file });
Mårten Kongstad98b0eeb2023-05-08 11:25:30 +0200134 }
135 Ok(opened_files)
136}
137
Mårten Kongstad403658f2023-06-14 09:51:56 +0200138fn open_single_file(matches: &ArgMatches, arg_name: &str) -> Result<Input> {
139 let Some(path) = matches.get_one::<String>(arg_name) else {
140 bail!("missing argument {}", arg_name);
141 };
142 let file = Box::new(fs::File::open(path)?);
143 Ok(Input { source: path.to_string(), reader: file })
144}
145
Mårten Kongstadd42eeeb2023-05-12 10:01:00 +0200146fn write_output_file_realtive_to_dir(root: &Path, output_file: &OutputFile) -> Result<()> {
Mårten Kongstadb5133f62023-09-11 11:10:02 +0200147 let path = root.join(&output_file.path);
Mårten Kongstadd42eeeb2023-05-12 10:01:00 +0200148 let parent = path
149 .parent()
150 .ok_or(anyhow!("unable to locate parent of output file {}", path.display()))?;
Mårten Kongstadcd414d4c2023-07-27 14:25:33 +0200151 fs::create_dir_all(parent)
152 .with_context(|| format!("failed to create directory {}", parent.display()))?;
Mårten Kongstadb5133f62023-09-11 11:10:02 +0200153 let mut file =
154 fs::File::create(&path).with_context(|| format!("failed to open {}", path.display()))?;
Mårten Kongstadcd414d4c2023-07-27 14:25:33 +0200155 file.write_all(&output_file.contents)
156 .with_context(|| format!("failed to write to {}", path.display()))?;
Mårten Kongstadd42eeeb2023-05-12 10:01:00 +0200157 Ok(())
158}
159
Mårten Kongstadf02734e2023-06-02 11:34:24 +0200160fn write_output_to_file_or_stdout(path: &str, data: &[u8]) -> Result<()> {
161 if path == "-" {
Mårten Kongstadcd414d4c2023-07-27 14:25:33 +0200162 io::stdout().write_all(data).context("failed to write to stdout")?;
Mårten Kongstadf02734e2023-06-02 11:34:24 +0200163 } else {
Mårten Kongstadcd414d4c2023-07-27 14:25:33 +0200164 fs::File::create(path)
165 .with_context(|| format!("failed to open {}", path))?
166 .write_all(data)
167 .with_context(|| format!("failed to write to {}", path))?;
Mårten Kongstadf02734e2023-06-02 11:34:24 +0200168 }
169 Ok(())
170}
171
Mårten Kongstadbb520722023-04-26 13:16:41 +0200172fn main() -> Result<()> {
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +0200173 let matches = cli().get_matches();
174 match matches.subcommand() {
175 Some(("create-cache", sub_matches)) => {
Mårten Kongstad9fb58962023-05-31 13:02:13 +0200176 let package = get_required_arg::<String>(sub_matches, "package")?;
Oriol Prieto Gasco7afc7e72023-11-22 13:26:02 +0000177 let container =
178 get_optional_arg::<String>(sub_matches, "container").map(|c| c.as_str());
Mårten Kongstadfa23d292023-05-11 14:47:02 +0200179 let declarations = open_zero_or_more_files(sub_matches, "declarations")?;
180 let values = open_zero_or_more_files(sub_matches, "values")?;
Zhi Dou24a0b6a2023-08-10 21:39:59 +0000181 let default_permission =
182 get_required_arg::<protos::ProtoFlagPermission>(sub_matches, "default-permission")?;
Oriol Prieto Gasco7afc7e72023-11-22 13:26:02 +0000183 let output = commands::parse_flags(
184 package,
185 container,
186 declarations,
187 values,
188 *default_permission,
189 )
190 .context("failed to create cache")?;
Mårten Kongstad6b9e3822023-05-16 11:19:58 +0200191 let path = get_required_arg::<String>(sub_matches, "cache")?;
Mårten Kongstad403658f2023-06-14 09:51:56 +0200192 write_output_to_file_or_stdout(path, &output)?;
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +0200193 }
Zhi Doueb744892023-05-10 04:02:33 +0000194 Some(("create-java-lib", sub_matches)) => {
Mårten Kongstad403658f2023-06-14 09:51:56 +0200195 let cache = open_single_file(sub_matches, "cache")?;
Zhi Dou8ba6aa72023-06-26 21:03:40 +0000196 let mode = get_required_arg::<CodegenMode>(sub_matches, "mode")?;
Mårten Kongstadcd414d4c2023-07-27 14:25:33 +0200197 let generated_files =
198 commands::create_java_lib(cache, *mode).context("failed to create java lib")?;
Mårten Kongstad403658f2023-06-14 09:51:56 +0200199 let dir = PathBuf::from(get_required_arg::<String>(sub_matches, "out")?);
Zhi Dou4655c962023-06-12 15:56:03 +0000200 generated_files
201 .iter()
202 .try_for_each(|file| write_output_file_realtive_to_dir(&dir, file))?;
Dennis Shen1dc9ad42023-05-12 00:21:55 +0000203 }
204 Some(("create-cpp-lib", sub_matches)) => {
Mårten Kongstad403658f2023-06-14 09:51:56 +0200205 let cache = open_single_file(sub_matches, "cache")?;
Dennis Shen8d544f72023-06-29 00:45:42 +0000206 let mode = get_required_arg::<CodegenMode>(sub_matches, "mode")?;
Mårten Kongstadcd414d4c2023-07-27 14:25:33 +0200207 let generated_files =
208 commands::create_cpp_lib(cache, *mode).context("failed to create cpp lib")?;
Mårten Kongstad403658f2023-06-14 09:51:56 +0200209 let dir = PathBuf::from(get_required_arg::<String>(sub_matches, "out")?);
Dennis Shen8d544f72023-06-29 00:45:42 +0000210 generated_files
211 .iter()
212 .try_for_each(|file| write_output_file_realtive_to_dir(&dir, file))?;
Zhi Doueb744892023-05-10 04:02:33 +0000213 }
Mårten Kongstadf73b9632023-05-24 15:43:47 +0200214 Some(("create-rust-lib", sub_matches)) => {
Mårten Kongstad403658f2023-06-14 09:51:56 +0200215 let cache = open_single_file(sub_matches, "cache")?;
Dennis Shen3cfbcf52023-07-17 14:57:23 +0000216 let mode = get_required_arg::<CodegenMode>(sub_matches, "mode")?;
Mårten Kongstadcd414d4c2023-07-27 14:25:33 +0200217 let generated_file =
218 commands::create_rust_lib(cache, *mode).context("failed to create rust lib")?;
Mårten Kongstad403658f2023-06-14 09:51:56 +0200219 let dir = PathBuf::from(get_required_arg::<String>(sub_matches, "out")?);
Mårten Kongstadf73b9632023-05-24 15:43:47 +0200220 write_output_file_realtive_to_dir(&dir, &generated_file)?;
221 }
Mårten Kongstadf02734e2023-06-02 11:34:24 +0200222 Some(("create-device-config-defaults", sub_matches)) => {
Mårten Kongstad403658f2023-06-14 09:51:56 +0200223 let cache = open_single_file(sub_matches, "cache")?;
Mårten Kongstadcd414d4c2023-07-27 14:25:33 +0200224 let output = commands::create_device_config_defaults(cache)
225 .context("failed to create device config defaults")?;
Mårten Kongstadf02734e2023-06-02 11:34:24 +0200226 let path = get_required_arg::<String>(sub_matches, "out")?;
227 write_output_to_file_or_stdout(path, &output)?;
228 }
Mårten Kongstadc31a6ff2023-06-02 11:54:36 +0200229 Some(("create-device-config-sysprops", sub_matches)) => {
Mårten Kongstad403658f2023-06-14 09:51:56 +0200230 let cache = open_single_file(sub_matches, "cache")?;
Mårten Kongstadcd414d4c2023-07-27 14:25:33 +0200231 let output = commands::create_device_config_sysprops(cache)
232 .context("failed to create device config sysprops")?;
Mårten Kongstadc31a6ff2023-06-02 11:54:36 +0200233 let path = get_required_arg::<String>(sub_matches, "out")?;
234 write_output_to_file_or_stdout(path, &output)?;
235 }
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +0200236 Some(("dump", sub_matches)) => {
Mårten Kongstad403658f2023-06-14 09:51:56 +0200237 let input = open_zero_or_more_files(sub_matches, "cache")?;
Mårten Kongstadcd414d4c2023-07-27 14:25:33 +0200238 let format = get_required_arg::<DumpFormat>(sub_matches, "format")
239 .context("failed to dump previously parsed flags")?;
Colin Cross6befb342023-11-28 15:55:07 -0800240 let dedup = get_required_arg::<bool>(sub_matches, "dedup")?;
241 let output = commands::dump_parsed_flags(input, *format, *dedup)?;
Mårten Kongstad6b9e3822023-05-16 11:19:58 +0200242 let path = get_required_arg::<String>(sub_matches, "out")?;
Mårten Kongstadf02734e2023-06-02 11:34:24 +0200243 write_output_to_file_or_stdout(path, &output)?;
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +0200244 }
245 _ => unreachable!(),
246 }
Mårten Kongstadbb520722023-04-26 13:16:41 +0200247 Ok(())
Mårten Kongstad867a3492023-04-25 15:06:30 +0200248}