blob: 72feb9406f9fc3335a79eafd12fd49a94493dd98 [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 Kongstad403658f2023-06-14 09:51:56 +020019use anyhow::{anyhow, bail, ensure, 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))
Mårten Kongstadfa23d292023-05-11 14:47:02 +020045 .arg(Arg::new("declarations").long("declarations").action(ArgAction::Append))
46 .arg(Arg::new("values").long("values").action(ArgAction::Append))
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +020047 .arg(Arg::new("cache").long("cache").required(true)),
48 )
49 .subcommand(
Zhi Doueb744892023-05-10 04:02:33 +000050 Command::new("create-java-lib")
51 .arg(Arg::new("cache").long("cache").required(true))
Zhi Dou8ba6aa72023-06-26 21:03:40 +000052 .arg(Arg::new("out").long("out").required(true))
53 .arg(
54 Arg::new("mode")
55 .long("mode")
56 .value_parser(EnumValueParser::<commands::CodegenMode>::new())
57 .default_value("production"),
58 ),
Zhi Doueb744892023-05-10 04:02:33 +000059 )
60 .subcommand(
Dennis Shen1dc9ad42023-05-12 00:21:55 +000061 Command::new("create-cpp-lib")
62 .arg(Arg::new("cache").long("cache").required(true))
Dennis Shen8d544f72023-06-29 00:45:42 +000063 .arg(Arg::new("out").long("out").required(true))
64 .arg(
65 Arg::new("mode")
66 .long("mode")
67 .value_parser(EnumValueParser::<commands::CodegenMode>::new())
68 .default_value("production"),
69 ),
Dennis Shen1dc9ad42023-05-12 00:21:55 +000070 )
71 .subcommand(
Mårten Kongstadf73b9632023-05-24 15:43:47 +020072 Command::new("create-rust-lib")
73 .arg(Arg::new("cache").long("cache").required(true))
74 .arg(Arg::new("out").long("out").required(true)),
75 )
76 .subcommand(
Mårten Kongstadf02734e2023-06-02 11:34:24 +020077 Command::new("create-device-config-defaults")
78 .arg(Arg::new("cache").long("cache").action(ArgAction::Append).required(true))
79 .arg(Arg::new("out").long("out").default_value("-")),
80 )
81 .subcommand(
Mårten Kongstadc31a6ff2023-06-02 11:54:36 +020082 Command::new("create-device-config-sysprops")
83 .arg(Arg::new("cache").long("cache").action(ArgAction::Append).required(true))
84 .arg(Arg::new("out").long("out").default_value("-")),
85 )
86 .subcommand(
Mårten Kongstada1029092023-05-08 11:51:59 +020087 Command::new("dump")
Mårten Kongstadaf677032023-05-17 16:18:25 +020088 .arg(Arg::new("cache").long("cache").action(ArgAction::Append).required(true))
Mårten Kongstada1029092023-05-08 11:51:59 +020089 .arg(
90 Arg::new("format")
91 .long("format")
Mårten Kongstadba94e6a2023-05-16 11:00:16 +020092 .value_parser(EnumValueParser::<commands::DumpFormat>::new())
Mårten Kongstada1029092023-05-08 11:51:59 +020093 .default_value("text"),
94 )
95 .arg(Arg::new("out").long("out").default_value("-")),
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +020096 )
97}
Mårten Kongstad867a3492023-04-25 15:06:30 +020098
Mårten Kongstad6b9e3822023-05-16 11:19:58 +020099fn get_required_arg<'a, T>(matches: &'a ArgMatches, arg_name: &str) -> Result<&'a T>
100where
101 T: Any + Clone + Send + Sync + 'static,
102{
103 matches
104 .get_one::<T>(arg_name)
105 .ok_or(anyhow!("internal error: required argument '{}' not found", arg_name))
106}
107
Mårten Kongstad98b0eeb2023-05-08 11:25:30 +0200108fn open_zero_or_more_files(matches: &ArgMatches, arg_name: &str) -> Result<Vec<Input>> {
109 let mut opened_files = vec![];
110 for path in matches.get_many::<String>(arg_name).unwrap_or_default() {
111 let file = Box::new(fs::File::open(path)?);
Mårten Kongstad403658f2023-06-14 09:51:56 +0200112 opened_files.push(Input { source: path.to_string(), reader: file });
Mårten Kongstad98b0eeb2023-05-08 11:25:30 +0200113 }
114 Ok(opened_files)
115}
116
Mårten Kongstad403658f2023-06-14 09:51:56 +0200117fn open_single_file(matches: &ArgMatches, arg_name: &str) -> Result<Input> {
118 let Some(path) = matches.get_one::<String>(arg_name) else {
119 bail!("missing argument {}", arg_name);
120 };
121 let file = Box::new(fs::File::open(path)?);
122 Ok(Input { source: path.to_string(), reader: file })
123}
124
Mårten Kongstadd42eeeb2023-05-12 10:01:00 +0200125fn write_output_file_realtive_to_dir(root: &Path, output_file: &OutputFile) -> Result<()> {
126 ensure!(
127 root.is_dir(),
128 "output directory {} does not exist or is not a directory",
129 root.display()
130 );
131 let path = root.join(output_file.path.clone());
132 let parent = path
133 .parent()
134 .ok_or(anyhow!("unable to locate parent of output file {}", path.display()))?;
135 fs::create_dir_all(parent)?;
136 let mut file = fs::File::create(path)?;
137 file.write_all(&output_file.contents)?;
138 Ok(())
139}
140
Mårten Kongstadf02734e2023-06-02 11:34:24 +0200141fn write_output_to_file_or_stdout(path: &str, data: &[u8]) -> Result<()> {
142 if path == "-" {
143 io::stdout().write_all(data)?;
144 } else {
145 fs::File::create(path)?.write_all(data)?;
146 }
147 Ok(())
148}
149
Mårten Kongstadbb520722023-04-26 13:16:41 +0200150fn main() -> Result<()> {
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +0200151 let matches = cli().get_matches();
152 match matches.subcommand() {
153 Some(("create-cache", sub_matches)) => {
Mårten Kongstad9fb58962023-05-31 13:02:13 +0200154 let package = get_required_arg::<String>(sub_matches, "package")?;
Mårten Kongstadfa23d292023-05-11 14:47:02 +0200155 let declarations = open_zero_or_more_files(sub_matches, "declarations")?;
156 let values = open_zero_or_more_files(sub_matches, "values")?;
Mårten Kongstad403658f2023-06-14 09:51:56 +0200157 let output = commands::parse_flags(package, declarations, values)?;
Mårten Kongstad6b9e3822023-05-16 11:19:58 +0200158 let path = get_required_arg::<String>(sub_matches, "cache")?;
Mårten Kongstad403658f2023-06-14 09:51:56 +0200159 write_output_to_file_or_stdout(path, &output)?;
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +0200160 }
Zhi Doueb744892023-05-10 04:02:33 +0000161 Some(("create-java-lib", sub_matches)) => {
Mårten Kongstad403658f2023-06-14 09:51:56 +0200162 let cache = open_single_file(sub_matches, "cache")?;
Zhi Dou8ba6aa72023-06-26 21:03:40 +0000163 let mode = get_required_arg::<CodegenMode>(sub_matches, "mode")?;
164 let generated_files = commands::create_java_lib(cache, *mode)?;
Mårten Kongstad403658f2023-06-14 09:51:56 +0200165 let dir = PathBuf::from(get_required_arg::<String>(sub_matches, "out")?);
Zhi Dou4655c962023-06-12 15:56:03 +0000166 generated_files
167 .iter()
168 .try_for_each(|file| write_output_file_realtive_to_dir(&dir, file))?;
Dennis Shen1dc9ad42023-05-12 00:21:55 +0000169 }
170 Some(("create-cpp-lib", sub_matches)) => {
Mårten Kongstad403658f2023-06-14 09:51:56 +0200171 let cache = open_single_file(sub_matches, "cache")?;
Dennis Shen8d544f72023-06-29 00:45:42 +0000172 let mode = get_required_arg::<CodegenMode>(sub_matches, "mode")?;
173 let generated_files = commands::create_cpp_lib(cache, *mode)?;
Mårten Kongstad403658f2023-06-14 09:51:56 +0200174 let dir = PathBuf::from(get_required_arg::<String>(sub_matches, "out")?);
Dennis Shen8d544f72023-06-29 00:45:42 +0000175 generated_files
176 .iter()
177 .try_for_each(|file| write_output_file_realtive_to_dir(&dir, file))?;
Zhi Doueb744892023-05-10 04:02:33 +0000178 }
Mårten Kongstadf73b9632023-05-24 15:43:47 +0200179 Some(("create-rust-lib", sub_matches)) => {
Mårten Kongstad403658f2023-06-14 09:51:56 +0200180 let cache = open_single_file(sub_matches, "cache")?;
Mårten Kongstadb27f2ce2023-06-02 11:43:21 +0200181 let generated_file = commands::create_rust_lib(cache)?;
Mårten Kongstad403658f2023-06-14 09:51:56 +0200182 let dir = PathBuf::from(get_required_arg::<String>(sub_matches, "out")?);
Mårten Kongstadf73b9632023-05-24 15:43:47 +0200183 write_output_file_realtive_to_dir(&dir, &generated_file)?;
184 }
Mårten Kongstadf02734e2023-06-02 11:34:24 +0200185 Some(("create-device-config-defaults", sub_matches)) => {
Mårten Kongstad403658f2023-06-14 09:51:56 +0200186 let cache = open_single_file(sub_matches, "cache")?;
187 let output = commands::create_device_config_defaults(cache)?;
Mårten Kongstadf02734e2023-06-02 11:34:24 +0200188 let path = get_required_arg::<String>(sub_matches, "out")?;
189 write_output_to_file_or_stdout(path, &output)?;
190 }
Mårten Kongstadc31a6ff2023-06-02 11:54:36 +0200191 Some(("create-device-config-sysprops", sub_matches)) => {
Mårten Kongstad403658f2023-06-14 09:51:56 +0200192 let cache = open_single_file(sub_matches, "cache")?;
193 let output = commands::create_device_config_sysprops(cache)?;
Mårten Kongstadc31a6ff2023-06-02 11:54:36 +0200194 let path = get_required_arg::<String>(sub_matches, "out")?;
195 write_output_to_file_or_stdout(path, &output)?;
196 }
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +0200197 Some(("dump", sub_matches)) => {
Mårten Kongstad403658f2023-06-14 09:51:56 +0200198 let input = open_zero_or_more_files(sub_matches, "cache")?;
Mårten Kongstad6b9e3822023-05-16 11:19:58 +0200199 let format = get_required_arg::<DumpFormat>(sub_matches, "format")?;
Mårten Kongstad403658f2023-06-14 09:51:56 +0200200 let output = commands::dump_parsed_flags(input, *format)?;
Mårten Kongstad6b9e3822023-05-16 11:19:58 +0200201 let path = get_required_arg::<String>(sub_matches, "out")?;
Mårten Kongstadf02734e2023-06-02 11:34:24 +0200202 write_output_to_file_or_stdout(path, &output)?;
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +0200203 }
204 _ => unreachable!(),
205 }
Mårten Kongstadbb520722023-04-26 13:16:41 +0200206 Ok(())
Mårten Kongstad867a3492023-04-25 15:06:30 +0200207}