blob: af0368a545fe6e4f22921cb42f5dedebe3db998d [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;
Dennis Shen1dc9ad42023-05-12 00:21:55 +000028mod codegen_cpp;
Zhi Doueb744892023-05-10 04:02:33 +000029mod codegen_java;
Mårten Kongstadf73b9632023-05-24 15:43:47 +020030mod codegen_rust;
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +020031mod commands;
Mårten Kongstadfe753f52023-04-26 09:13:03 +020032mod protos;
Mårten Kongstadfe753f52023-04-26 09:13:03 +020033
Mårten Kongstad83a87602023-06-02 11:20:15 +020034#[cfg(test)]
35mod test;
36
Zhi Dou8ba6aa72023-06-26 21:03:40 +000037use commands::{CodegenMode, DumpFormat, Input, OutputFile};
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +020038
39fn cli() -> Command {
40 Command::new("aconfig")
41 .subcommand_required(true)
42 .subcommand(
43 Command::new("create-cache")
Mårten Kongstad9fb58962023-05-31 13:02:13 +020044 .arg(Arg::new("package").long("package").required(true))
Oriol Prieto Gasco7afc7e72023-11-22 13:26:02 +000045 // TODO(b/312769710): Make this argument required.
46 .arg(Arg::new("container").long("container"))
Mårten Kongstadfa23d292023-05-11 14:47:02 +020047 .arg(Arg::new("declarations").long("declarations").action(ArgAction::Append))
48 .arg(Arg::new("values").long("values").action(ArgAction::Append))
Zhi Dou24a0b6a2023-08-10 21:39:59 +000049 .arg(
50 Arg::new("default-permission")
51 .long("default-permission")
52 .value_parser(protos::flag_permission::parse_from_str)
53 .default_value(protos::flag_permission::to_string(
54 &commands::DEFAULT_FLAG_PERMISSION,
55 )),
56 )
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +020057 .arg(Arg::new("cache").long("cache").required(true)),
58 )
59 .subcommand(
Zhi Doueb744892023-05-10 04:02:33 +000060 Command::new("create-java-lib")
61 .arg(Arg::new("cache").long("cache").required(true))
Zhi Dou8ba6aa72023-06-26 21:03:40 +000062 .arg(Arg::new("out").long("out").required(true))
63 .arg(
64 Arg::new("mode")
65 .long("mode")
66 .value_parser(EnumValueParser::<commands::CodegenMode>::new())
67 .default_value("production"),
68 ),
Zhi Doueb744892023-05-10 04:02:33 +000069 )
70 .subcommand(
Dennis Shen1dc9ad42023-05-12 00:21:55 +000071 Command::new("create-cpp-lib")
72 .arg(Arg::new("cache").long("cache").required(true))
Dennis Shen8d544f72023-06-29 00:45:42 +000073 .arg(Arg::new("out").long("out").required(true))
74 .arg(
75 Arg::new("mode")
76 .long("mode")
77 .value_parser(EnumValueParser::<commands::CodegenMode>::new())
78 .default_value("production"),
79 ),
Dennis Shen1dc9ad42023-05-12 00:21:55 +000080 )
81 .subcommand(
Mårten Kongstadf73b9632023-05-24 15:43:47 +020082 Command::new("create-rust-lib")
83 .arg(Arg::new("cache").long("cache").required(true))
Dennis Shen3cfbcf52023-07-17 14:57:23 +000084 .arg(Arg::new("out").long("out").required(true))
85 .arg(
86 Arg::new("mode")
87 .long("mode")
88 .value_parser(EnumValueParser::<commands::CodegenMode>::new())
89 .default_value("production"),
90 ),
Mårten Kongstadf73b9632023-05-24 15:43:47 +020091 )
92 .subcommand(
Mårten Kongstadf02734e2023-06-02 11:34:24 +020093 Command::new("create-device-config-defaults")
94 .arg(Arg::new("cache").long("cache").action(ArgAction::Append).required(true))
95 .arg(Arg::new("out").long("out").default_value("-")),
96 )
97 .subcommand(
Mårten Kongstadc31a6ff2023-06-02 11:54:36 +020098 Command::new("create-device-config-sysprops")
99 .arg(Arg::new("cache").long("cache").action(ArgAction::Append).required(true))
100 .arg(Arg::new("out").long("out").default_value("-")),
101 )
102 .subcommand(
Mårten Kongstada1029092023-05-08 11:51:59 +0200103 Command::new("dump")
Mårten Kongstadaf677032023-05-17 16:18:25 +0200104 .arg(Arg::new("cache").long("cache").action(ArgAction::Append).required(true))
Mårten Kongstada1029092023-05-08 11:51:59 +0200105 .arg(
106 Arg::new("format")
107 .long("format")
Mårten Kongstadba94e6a2023-05-16 11:00:16 +0200108 .value_parser(EnumValueParser::<commands::DumpFormat>::new())
Mårten Kongstada1029092023-05-08 11:51:59 +0200109 .default_value("text"),
110 )
111 .arg(Arg::new("out").long("out").default_value("-")),
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +0200112 )
113}
Mårten Kongstad867a3492023-04-25 15:06:30 +0200114
Mårten Kongstad6b9e3822023-05-16 11:19:58 +0200115fn get_required_arg<'a, T>(matches: &'a ArgMatches, arg_name: &str) -> Result<&'a T>
116where
117 T: Any + Clone + Send + Sync + 'static,
118{
119 matches
120 .get_one::<T>(arg_name)
121 .ok_or(anyhow!("internal error: required argument '{}' not found", arg_name))
122}
123
Oriol Prieto Gasco7afc7e72023-11-22 13:26:02 +0000124fn get_optional_arg<'a, T>(matches: &'a ArgMatches, arg_name: &str) -> Option<&'a T>
125where
126 T: Any + Clone + Send + Sync + 'static,
127{
128 matches.get_one::<T>(arg_name)
129}
130
Mårten Kongstad98b0eeb2023-05-08 11:25:30 +0200131fn open_zero_or_more_files(matches: &ArgMatches, arg_name: &str) -> Result<Vec<Input>> {
132 let mut opened_files = vec![];
133 for path in matches.get_many::<String>(arg_name).unwrap_or_default() {
134 let file = Box::new(fs::File::open(path)?);
Mårten Kongstad403658f2023-06-14 09:51:56 +0200135 opened_files.push(Input { source: path.to_string(), reader: file });
Mårten Kongstad98b0eeb2023-05-08 11:25:30 +0200136 }
137 Ok(opened_files)
138}
139
Mårten Kongstad403658f2023-06-14 09:51:56 +0200140fn open_single_file(matches: &ArgMatches, arg_name: &str) -> Result<Input> {
141 let Some(path) = matches.get_one::<String>(arg_name) else {
142 bail!("missing argument {}", arg_name);
143 };
144 let file = Box::new(fs::File::open(path)?);
145 Ok(Input { source: path.to_string(), reader: file })
146}
147
Mårten Kongstadd42eeeb2023-05-12 10:01:00 +0200148fn write_output_file_realtive_to_dir(root: &Path, output_file: &OutputFile) -> Result<()> {
Mårten Kongstadb5133f62023-09-11 11:10:02 +0200149 let path = root.join(&output_file.path);
Mårten Kongstadd42eeeb2023-05-12 10:01:00 +0200150 let parent = path
151 .parent()
152 .ok_or(anyhow!("unable to locate parent of output file {}", path.display()))?;
Mårten Kongstadcd414d4c2023-07-27 14:25:33 +0200153 fs::create_dir_all(parent)
154 .with_context(|| format!("failed to create directory {}", parent.display()))?;
Mårten Kongstadb5133f62023-09-11 11:10:02 +0200155 let mut file =
156 fs::File::create(&path).with_context(|| format!("failed to open {}", path.display()))?;
Mårten Kongstadcd414d4c2023-07-27 14:25:33 +0200157 file.write_all(&output_file.contents)
158 .with_context(|| format!("failed to write to {}", path.display()))?;
Mårten Kongstadd42eeeb2023-05-12 10:01:00 +0200159 Ok(())
160}
161
Mårten Kongstadf02734e2023-06-02 11:34:24 +0200162fn write_output_to_file_or_stdout(path: &str, data: &[u8]) -> Result<()> {
163 if path == "-" {
Mårten Kongstadcd414d4c2023-07-27 14:25:33 +0200164 io::stdout().write_all(data).context("failed to write to stdout")?;
Mårten Kongstadf02734e2023-06-02 11:34:24 +0200165 } else {
Mårten Kongstadcd414d4c2023-07-27 14:25:33 +0200166 fs::File::create(path)
167 .with_context(|| format!("failed to open {}", path))?
168 .write_all(data)
169 .with_context(|| format!("failed to write to {}", path))?;
Mårten Kongstadf02734e2023-06-02 11:34:24 +0200170 }
171 Ok(())
172}
173
Mårten Kongstadbb520722023-04-26 13:16:41 +0200174fn main() -> Result<()> {
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +0200175 let matches = cli().get_matches();
176 match matches.subcommand() {
177 Some(("create-cache", sub_matches)) => {
Mårten Kongstad9fb58962023-05-31 13:02:13 +0200178 let package = get_required_arg::<String>(sub_matches, "package")?;
Oriol Prieto Gasco7afc7e72023-11-22 13:26:02 +0000179 let container =
180 get_optional_arg::<String>(sub_matches, "container").map(|c| c.as_str());
Mårten Kongstadfa23d292023-05-11 14:47:02 +0200181 let declarations = open_zero_or_more_files(sub_matches, "declarations")?;
182 let values = open_zero_or_more_files(sub_matches, "values")?;
Zhi Dou24a0b6a2023-08-10 21:39:59 +0000183 let default_permission =
184 get_required_arg::<protos::ProtoFlagPermission>(sub_matches, "default-permission")?;
Oriol Prieto Gasco7afc7e72023-11-22 13:26:02 +0000185 let output = commands::parse_flags(
186 package,
187 container,
188 declarations,
189 values,
190 *default_permission,
191 )
192 .context("failed to create cache")?;
Mårten Kongstad6b9e3822023-05-16 11:19:58 +0200193 let path = get_required_arg::<String>(sub_matches, "cache")?;
Mårten Kongstad403658f2023-06-14 09:51:56 +0200194 write_output_to_file_or_stdout(path, &output)?;
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +0200195 }
Zhi Doueb744892023-05-10 04:02:33 +0000196 Some(("create-java-lib", sub_matches)) => {
Mårten Kongstad403658f2023-06-14 09:51:56 +0200197 let cache = open_single_file(sub_matches, "cache")?;
Zhi Dou8ba6aa72023-06-26 21:03:40 +0000198 let mode = get_required_arg::<CodegenMode>(sub_matches, "mode")?;
Mårten Kongstadcd414d4c2023-07-27 14:25:33 +0200199 let generated_files =
200 commands::create_java_lib(cache, *mode).context("failed to create java lib")?;
Mårten Kongstad403658f2023-06-14 09:51:56 +0200201 let dir = PathBuf::from(get_required_arg::<String>(sub_matches, "out")?);
Zhi Dou4655c962023-06-12 15:56:03 +0000202 generated_files
203 .iter()
204 .try_for_each(|file| write_output_file_realtive_to_dir(&dir, file))?;
Dennis Shen1dc9ad42023-05-12 00:21:55 +0000205 }
206 Some(("create-cpp-lib", sub_matches)) => {
Mårten Kongstad403658f2023-06-14 09:51:56 +0200207 let cache = open_single_file(sub_matches, "cache")?;
Dennis Shen8d544f72023-06-29 00:45:42 +0000208 let mode = get_required_arg::<CodegenMode>(sub_matches, "mode")?;
Mårten Kongstadcd414d4c2023-07-27 14:25:33 +0200209 let generated_files =
210 commands::create_cpp_lib(cache, *mode).context("failed to create cpp lib")?;
Mårten Kongstad403658f2023-06-14 09:51:56 +0200211 let dir = PathBuf::from(get_required_arg::<String>(sub_matches, "out")?);
Dennis Shen8d544f72023-06-29 00:45:42 +0000212 generated_files
213 .iter()
214 .try_for_each(|file| write_output_file_realtive_to_dir(&dir, file))?;
Zhi Doueb744892023-05-10 04:02:33 +0000215 }
Mårten Kongstadf73b9632023-05-24 15:43:47 +0200216 Some(("create-rust-lib", sub_matches)) => {
Mårten Kongstad403658f2023-06-14 09:51:56 +0200217 let cache = open_single_file(sub_matches, "cache")?;
Dennis Shen3cfbcf52023-07-17 14:57:23 +0000218 let mode = get_required_arg::<CodegenMode>(sub_matches, "mode")?;
Mårten Kongstadcd414d4c2023-07-27 14:25:33 +0200219 let generated_file =
220 commands::create_rust_lib(cache, *mode).context("failed to create rust lib")?;
Mårten Kongstad403658f2023-06-14 09:51:56 +0200221 let dir = PathBuf::from(get_required_arg::<String>(sub_matches, "out")?);
Mårten Kongstadf73b9632023-05-24 15:43:47 +0200222 write_output_file_realtive_to_dir(&dir, &generated_file)?;
223 }
Mårten Kongstadf02734e2023-06-02 11:34:24 +0200224 Some(("create-device-config-defaults", sub_matches)) => {
Mårten Kongstad403658f2023-06-14 09:51:56 +0200225 let cache = open_single_file(sub_matches, "cache")?;
Mårten Kongstadcd414d4c2023-07-27 14:25:33 +0200226 let output = commands::create_device_config_defaults(cache)
227 .context("failed to create device config defaults")?;
Mårten Kongstadf02734e2023-06-02 11:34:24 +0200228 let path = get_required_arg::<String>(sub_matches, "out")?;
229 write_output_to_file_or_stdout(path, &output)?;
230 }
Mårten Kongstadc31a6ff2023-06-02 11:54:36 +0200231 Some(("create-device-config-sysprops", sub_matches)) => {
Mårten Kongstad403658f2023-06-14 09:51:56 +0200232 let cache = open_single_file(sub_matches, "cache")?;
Mårten Kongstadcd414d4c2023-07-27 14:25:33 +0200233 let output = commands::create_device_config_sysprops(cache)
234 .context("failed to create device config sysprops")?;
Mårten Kongstadc31a6ff2023-06-02 11:54:36 +0200235 let path = get_required_arg::<String>(sub_matches, "out")?;
236 write_output_to_file_or_stdout(path, &output)?;
237 }
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +0200238 Some(("dump", sub_matches)) => {
Mårten Kongstad403658f2023-06-14 09:51:56 +0200239 let input = open_zero_or_more_files(sub_matches, "cache")?;
Mårten Kongstadcd414d4c2023-07-27 14:25:33 +0200240 let format = get_required_arg::<DumpFormat>(sub_matches, "format")
241 .context("failed to dump previously parsed flags")?;
Mårten Kongstad403658f2023-06-14 09:51:56 +0200242 let output = commands::dump_parsed_flags(input, *format)?;
Mårten Kongstad6b9e3822023-05-16 11:19:58 +0200243 let path = get_required_arg::<String>(sub_matches, "out")?;
Mårten Kongstadf02734e2023-06-02 11:34:24 +0200244 write_output_to_file_or_stdout(path, &output)?;
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +0200245 }
246 _ => unreachable!(),
247 }
Mårten Kongstadbb520722023-04-26 13:16:41 +0200248 Ok(())
Mårten Kongstad867a3492023-04-25 15:06:30 +0200249}