Mårten Kongstad | 867a349 | 2023-04-25 15:06:30 +0200 | [diff] [blame] | 1 | /* |
| 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 Kongstad | cd414d4c | 2023-07-27 14:25:33 +0200 | [diff] [blame] | 19 | use anyhow::{anyhow, bail, Context, Result}; |
Mårten Kongstad | 98b0eeb | 2023-05-08 11:25:30 +0200 | [diff] [blame] | 20 | use clap::{builder::ArgAction, builder::EnumValueParser, Arg, ArgMatches, Command}; |
Mårten Kongstad | 6b9e382 | 2023-05-16 11:19:58 +0200 | [diff] [blame] | 21 | use core::any::Any; |
Mårten Kongstad | 4d2b4b0 | 2023-04-27 16:05:58 +0200 | [diff] [blame] | 22 | use std::fs; |
Mårten Kongstad | a102909 | 2023-05-08 11:51:59 +0200 | [diff] [blame] | 23 | use std::io; |
| 24 | use std::io::Write; |
Mårten Kongstad | d42eeeb | 2023-05-12 10:01:00 +0200 | [diff] [blame] | 25 | use std::path::{Path, PathBuf}; |
Mårten Kongstad | 867a349 | 2023-04-25 15:06:30 +0200 | [diff] [blame] | 26 | |
Mårten Kongstad | 00cf045 | 2023-05-26 16:48:01 +0200 | [diff] [blame] | 27 | mod codegen; |
Dennis Shen | 1dc9ad4 | 2023-05-12 00:21:55 +0000 | [diff] [blame] | 28 | mod codegen_cpp; |
Zhi Dou | eb74489 | 2023-05-10 04:02:33 +0000 | [diff] [blame] | 29 | mod codegen_java; |
Mårten Kongstad | f73b963 | 2023-05-24 15:43:47 +0200 | [diff] [blame] | 30 | mod codegen_rust; |
Mårten Kongstad | 4d2b4b0 | 2023-04-27 16:05:58 +0200 | [diff] [blame] | 31 | mod commands; |
Mårten Kongstad | fe753f5 | 2023-04-26 09:13:03 +0200 | [diff] [blame] | 32 | mod protos; |
Mårten Kongstad | fe753f5 | 2023-04-26 09:13:03 +0200 | [diff] [blame] | 33 | |
Mårten Kongstad | 83a8760 | 2023-06-02 11:20:15 +0200 | [diff] [blame] | 34 | #[cfg(test)] |
| 35 | mod test; |
| 36 | |
Zhi Dou | 8ba6aa7 | 2023-06-26 21:03:40 +0000 | [diff] [blame] | 37 | use commands::{CodegenMode, DumpFormat, Input, OutputFile}; |
Mårten Kongstad | 4d2b4b0 | 2023-04-27 16:05:58 +0200 | [diff] [blame] | 38 | |
| 39 | fn cli() -> Command { |
| 40 | Command::new("aconfig") |
| 41 | .subcommand_required(true) |
| 42 | .subcommand( |
| 43 | Command::new("create-cache") |
Mårten Kongstad | 9fb5896 | 2023-05-31 13:02:13 +0200 | [diff] [blame] | 44 | .arg(Arg::new("package").long("package").required(true)) |
Mårten Kongstad | fa23d29 | 2023-05-11 14:47:02 +0200 | [diff] [blame] | 45 | .arg(Arg::new("declarations").long("declarations").action(ArgAction::Append)) |
| 46 | .arg(Arg::new("values").long("values").action(ArgAction::Append)) |
Mårten Kongstad | 4d2b4b0 | 2023-04-27 16:05:58 +0200 | [diff] [blame] | 47 | .arg(Arg::new("cache").long("cache").required(true)), |
| 48 | ) |
| 49 | .subcommand( |
Zhi Dou | eb74489 | 2023-05-10 04:02:33 +0000 | [diff] [blame] | 50 | Command::new("create-java-lib") |
| 51 | .arg(Arg::new("cache").long("cache").required(true)) |
Zhi Dou | 8ba6aa7 | 2023-06-26 21:03:40 +0000 | [diff] [blame] | 52 | .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 Dou | eb74489 | 2023-05-10 04:02:33 +0000 | [diff] [blame] | 59 | ) |
| 60 | .subcommand( |
Dennis Shen | 1dc9ad4 | 2023-05-12 00:21:55 +0000 | [diff] [blame] | 61 | Command::new("create-cpp-lib") |
| 62 | .arg(Arg::new("cache").long("cache").required(true)) |
Dennis Shen | 8d544f7 | 2023-06-29 00:45:42 +0000 | [diff] [blame] | 63 | .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 Shen | 1dc9ad4 | 2023-05-12 00:21:55 +0000 | [diff] [blame] | 70 | ) |
| 71 | .subcommand( |
Mårten Kongstad | f73b963 | 2023-05-24 15:43:47 +0200 | [diff] [blame] | 72 | Command::new("create-rust-lib") |
| 73 | .arg(Arg::new("cache").long("cache").required(true)) |
Dennis Shen | 3cfbcf5 | 2023-07-17 14:57:23 +0000 | [diff] [blame] | 74 | .arg(Arg::new("out").long("out").required(true)) |
| 75 | .arg( |
| 76 | Arg::new("mode") |
| 77 | .long("mode") |
| 78 | .value_parser(EnumValueParser::<commands::CodegenMode>::new()) |
| 79 | .default_value("production"), |
| 80 | ), |
Mårten Kongstad | f73b963 | 2023-05-24 15:43:47 +0200 | [diff] [blame] | 81 | ) |
| 82 | .subcommand( |
Mårten Kongstad | f02734e | 2023-06-02 11:34:24 +0200 | [diff] [blame] | 83 | Command::new("create-device-config-defaults") |
| 84 | .arg(Arg::new("cache").long("cache").action(ArgAction::Append).required(true)) |
| 85 | .arg(Arg::new("out").long("out").default_value("-")), |
| 86 | ) |
| 87 | .subcommand( |
Mårten Kongstad | c31a6ff | 2023-06-02 11:54:36 +0200 | [diff] [blame] | 88 | Command::new("create-device-config-sysprops") |
| 89 | .arg(Arg::new("cache").long("cache").action(ArgAction::Append).required(true)) |
| 90 | .arg(Arg::new("out").long("out").default_value("-")), |
| 91 | ) |
| 92 | .subcommand( |
Mårten Kongstad | a102909 | 2023-05-08 11:51:59 +0200 | [diff] [blame] | 93 | Command::new("dump") |
Mårten Kongstad | af67703 | 2023-05-17 16:18:25 +0200 | [diff] [blame] | 94 | .arg(Arg::new("cache").long("cache").action(ArgAction::Append).required(true)) |
Mårten Kongstad | a102909 | 2023-05-08 11:51:59 +0200 | [diff] [blame] | 95 | .arg( |
| 96 | Arg::new("format") |
| 97 | .long("format") |
Mårten Kongstad | ba94e6a | 2023-05-16 11:00:16 +0200 | [diff] [blame] | 98 | .value_parser(EnumValueParser::<commands::DumpFormat>::new()) |
Mårten Kongstad | a102909 | 2023-05-08 11:51:59 +0200 | [diff] [blame] | 99 | .default_value("text"), |
| 100 | ) |
| 101 | .arg(Arg::new("out").long("out").default_value("-")), |
Mårten Kongstad | 4d2b4b0 | 2023-04-27 16:05:58 +0200 | [diff] [blame] | 102 | ) |
| 103 | } |
Mårten Kongstad | 867a349 | 2023-04-25 15:06:30 +0200 | [diff] [blame] | 104 | |
Mårten Kongstad | 6b9e382 | 2023-05-16 11:19:58 +0200 | [diff] [blame] | 105 | fn get_required_arg<'a, T>(matches: &'a ArgMatches, arg_name: &str) -> Result<&'a T> |
| 106 | where |
| 107 | T: Any + Clone + Send + Sync + 'static, |
| 108 | { |
| 109 | matches |
| 110 | .get_one::<T>(arg_name) |
| 111 | .ok_or(anyhow!("internal error: required argument '{}' not found", arg_name)) |
| 112 | } |
| 113 | |
Mårten Kongstad | 98b0eeb | 2023-05-08 11:25:30 +0200 | [diff] [blame] | 114 | fn open_zero_or_more_files(matches: &ArgMatches, arg_name: &str) -> Result<Vec<Input>> { |
| 115 | let mut opened_files = vec![]; |
| 116 | for path in matches.get_many::<String>(arg_name).unwrap_or_default() { |
| 117 | let file = Box::new(fs::File::open(path)?); |
Mårten Kongstad | 403658f | 2023-06-14 09:51:56 +0200 | [diff] [blame] | 118 | opened_files.push(Input { source: path.to_string(), reader: file }); |
Mårten Kongstad | 98b0eeb | 2023-05-08 11:25:30 +0200 | [diff] [blame] | 119 | } |
| 120 | Ok(opened_files) |
| 121 | } |
| 122 | |
Mårten Kongstad | 403658f | 2023-06-14 09:51:56 +0200 | [diff] [blame] | 123 | fn open_single_file(matches: &ArgMatches, arg_name: &str) -> Result<Input> { |
| 124 | let Some(path) = matches.get_one::<String>(arg_name) else { |
| 125 | bail!("missing argument {}", arg_name); |
| 126 | }; |
| 127 | let file = Box::new(fs::File::open(path)?); |
| 128 | Ok(Input { source: path.to_string(), reader: file }) |
| 129 | } |
| 130 | |
Mårten Kongstad | d42eeeb | 2023-05-12 10:01:00 +0200 | [diff] [blame] | 131 | fn write_output_file_realtive_to_dir(root: &Path, output_file: &OutputFile) -> Result<()> { |
Mårten Kongstad | d42eeeb | 2023-05-12 10:01:00 +0200 | [diff] [blame] | 132 | let path = root.join(output_file.path.clone()); |
| 133 | let parent = path |
| 134 | .parent() |
| 135 | .ok_or(anyhow!("unable to locate parent of output file {}", path.display()))?; |
Mårten Kongstad | cd414d4c | 2023-07-27 14:25:33 +0200 | [diff] [blame] | 136 | fs::create_dir_all(parent) |
| 137 | .with_context(|| format!("failed to create directory {}", parent.display()))?; |
| 138 | let mut file = fs::File::create(path.clone()) |
| 139 | .with_context(|| format!("failed to open {}", path.display()))?; |
| 140 | file.write_all(&output_file.contents) |
| 141 | .with_context(|| format!("failed to write to {}", path.display()))?; |
Mårten Kongstad | d42eeeb | 2023-05-12 10:01:00 +0200 | [diff] [blame] | 142 | Ok(()) |
| 143 | } |
| 144 | |
Mårten Kongstad | f02734e | 2023-06-02 11:34:24 +0200 | [diff] [blame] | 145 | fn write_output_to_file_or_stdout(path: &str, data: &[u8]) -> Result<()> { |
| 146 | if path == "-" { |
Mårten Kongstad | cd414d4c | 2023-07-27 14:25:33 +0200 | [diff] [blame] | 147 | io::stdout().write_all(data).context("failed to write to stdout")?; |
Mårten Kongstad | f02734e | 2023-06-02 11:34:24 +0200 | [diff] [blame] | 148 | } else { |
Mårten Kongstad | cd414d4c | 2023-07-27 14:25:33 +0200 | [diff] [blame] | 149 | fs::File::create(path) |
| 150 | .with_context(|| format!("failed to open {}", path))? |
| 151 | .write_all(data) |
| 152 | .with_context(|| format!("failed to write to {}", path))?; |
Mårten Kongstad | f02734e | 2023-06-02 11:34:24 +0200 | [diff] [blame] | 153 | } |
| 154 | Ok(()) |
| 155 | } |
| 156 | |
Mårten Kongstad | bb52072 | 2023-04-26 13:16:41 +0200 | [diff] [blame] | 157 | fn main() -> Result<()> { |
Mårten Kongstad | 4d2b4b0 | 2023-04-27 16:05:58 +0200 | [diff] [blame] | 158 | let matches = cli().get_matches(); |
| 159 | match matches.subcommand() { |
| 160 | Some(("create-cache", sub_matches)) => { |
Mårten Kongstad | 9fb5896 | 2023-05-31 13:02:13 +0200 | [diff] [blame] | 161 | let package = get_required_arg::<String>(sub_matches, "package")?; |
Mårten Kongstad | fa23d29 | 2023-05-11 14:47:02 +0200 | [diff] [blame] | 162 | let declarations = open_zero_or_more_files(sub_matches, "declarations")?; |
| 163 | let values = open_zero_or_more_files(sub_matches, "values")?; |
Mårten Kongstad | cd414d4c | 2023-07-27 14:25:33 +0200 | [diff] [blame] | 164 | let output = commands::parse_flags(package, declarations, values) |
| 165 | .context("failed to create cache")?; |
Mårten Kongstad | 6b9e382 | 2023-05-16 11:19:58 +0200 | [diff] [blame] | 166 | let path = get_required_arg::<String>(sub_matches, "cache")?; |
Mårten Kongstad | 403658f | 2023-06-14 09:51:56 +0200 | [diff] [blame] | 167 | write_output_to_file_or_stdout(path, &output)?; |
Mårten Kongstad | 4d2b4b0 | 2023-04-27 16:05:58 +0200 | [diff] [blame] | 168 | } |
Zhi Dou | eb74489 | 2023-05-10 04:02:33 +0000 | [diff] [blame] | 169 | Some(("create-java-lib", sub_matches)) => { |
Mårten Kongstad | 403658f | 2023-06-14 09:51:56 +0200 | [diff] [blame] | 170 | let cache = open_single_file(sub_matches, "cache")?; |
Zhi Dou | 8ba6aa7 | 2023-06-26 21:03:40 +0000 | [diff] [blame] | 171 | let mode = get_required_arg::<CodegenMode>(sub_matches, "mode")?; |
Mårten Kongstad | cd414d4c | 2023-07-27 14:25:33 +0200 | [diff] [blame] | 172 | let generated_files = |
| 173 | commands::create_java_lib(cache, *mode).context("failed to create java lib")?; |
Mårten Kongstad | 403658f | 2023-06-14 09:51:56 +0200 | [diff] [blame] | 174 | let dir = PathBuf::from(get_required_arg::<String>(sub_matches, "out")?); |
Zhi Dou | 4655c96 | 2023-06-12 15:56:03 +0000 | [diff] [blame] | 175 | generated_files |
| 176 | .iter() |
| 177 | .try_for_each(|file| write_output_file_realtive_to_dir(&dir, file))?; |
Dennis Shen | 1dc9ad4 | 2023-05-12 00:21:55 +0000 | [diff] [blame] | 178 | } |
| 179 | Some(("create-cpp-lib", sub_matches)) => { |
Mårten Kongstad | 403658f | 2023-06-14 09:51:56 +0200 | [diff] [blame] | 180 | let cache = open_single_file(sub_matches, "cache")?; |
Dennis Shen | 8d544f7 | 2023-06-29 00:45:42 +0000 | [diff] [blame] | 181 | let mode = get_required_arg::<CodegenMode>(sub_matches, "mode")?; |
Mårten Kongstad | cd414d4c | 2023-07-27 14:25:33 +0200 | [diff] [blame] | 182 | let generated_files = |
| 183 | commands::create_cpp_lib(cache, *mode).context("failed to create cpp lib")?; |
Mårten Kongstad | 403658f | 2023-06-14 09:51:56 +0200 | [diff] [blame] | 184 | let dir = PathBuf::from(get_required_arg::<String>(sub_matches, "out")?); |
Dennis Shen | 8d544f7 | 2023-06-29 00:45:42 +0000 | [diff] [blame] | 185 | generated_files |
| 186 | .iter() |
| 187 | .try_for_each(|file| write_output_file_realtive_to_dir(&dir, file))?; |
Zhi Dou | eb74489 | 2023-05-10 04:02:33 +0000 | [diff] [blame] | 188 | } |
Mårten Kongstad | f73b963 | 2023-05-24 15:43:47 +0200 | [diff] [blame] | 189 | Some(("create-rust-lib", sub_matches)) => { |
Mårten Kongstad | 403658f | 2023-06-14 09:51:56 +0200 | [diff] [blame] | 190 | let cache = open_single_file(sub_matches, "cache")?; |
Dennis Shen | 3cfbcf5 | 2023-07-17 14:57:23 +0000 | [diff] [blame] | 191 | let mode = get_required_arg::<CodegenMode>(sub_matches, "mode")?; |
Mårten Kongstad | cd414d4c | 2023-07-27 14:25:33 +0200 | [diff] [blame] | 192 | let generated_file = |
| 193 | commands::create_rust_lib(cache, *mode).context("failed to create rust lib")?; |
Mårten Kongstad | 403658f | 2023-06-14 09:51:56 +0200 | [diff] [blame] | 194 | let dir = PathBuf::from(get_required_arg::<String>(sub_matches, "out")?); |
Mårten Kongstad | f73b963 | 2023-05-24 15:43:47 +0200 | [diff] [blame] | 195 | write_output_file_realtive_to_dir(&dir, &generated_file)?; |
| 196 | } |
Mårten Kongstad | f02734e | 2023-06-02 11:34:24 +0200 | [diff] [blame] | 197 | Some(("create-device-config-defaults", sub_matches)) => { |
Mårten Kongstad | 403658f | 2023-06-14 09:51:56 +0200 | [diff] [blame] | 198 | let cache = open_single_file(sub_matches, "cache")?; |
Mårten Kongstad | cd414d4c | 2023-07-27 14:25:33 +0200 | [diff] [blame] | 199 | let output = commands::create_device_config_defaults(cache) |
| 200 | .context("failed to create device config defaults")?; |
Mårten Kongstad | f02734e | 2023-06-02 11:34:24 +0200 | [diff] [blame] | 201 | let path = get_required_arg::<String>(sub_matches, "out")?; |
| 202 | write_output_to_file_or_stdout(path, &output)?; |
| 203 | } |
Mårten Kongstad | c31a6ff | 2023-06-02 11:54:36 +0200 | [diff] [blame] | 204 | Some(("create-device-config-sysprops", sub_matches)) => { |
Mårten Kongstad | 403658f | 2023-06-14 09:51:56 +0200 | [diff] [blame] | 205 | let cache = open_single_file(sub_matches, "cache")?; |
Mårten Kongstad | cd414d4c | 2023-07-27 14:25:33 +0200 | [diff] [blame] | 206 | let output = commands::create_device_config_sysprops(cache) |
| 207 | .context("failed to create device config sysprops")?; |
Mårten Kongstad | c31a6ff | 2023-06-02 11:54:36 +0200 | [diff] [blame] | 208 | let path = get_required_arg::<String>(sub_matches, "out")?; |
| 209 | write_output_to_file_or_stdout(path, &output)?; |
| 210 | } |
Mårten Kongstad | 4d2b4b0 | 2023-04-27 16:05:58 +0200 | [diff] [blame] | 211 | Some(("dump", sub_matches)) => { |
Mårten Kongstad | 403658f | 2023-06-14 09:51:56 +0200 | [diff] [blame] | 212 | let input = open_zero_or_more_files(sub_matches, "cache")?; |
Mårten Kongstad | cd414d4c | 2023-07-27 14:25:33 +0200 | [diff] [blame] | 213 | let format = get_required_arg::<DumpFormat>(sub_matches, "format") |
| 214 | .context("failed to dump previously parsed flags")?; |
Mårten Kongstad | 403658f | 2023-06-14 09:51:56 +0200 | [diff] [blame] | 215 | let output = commands::dump_parsed_flags(input, *format)?; |
Mårten Kongstad | 6b9e382 | 2023-05-16 11:19:58 +0200 | [diff] [blame] | 216 | let path = get_required_arg::<String>(sub_matches, "out")?; |
Mårten Kongstad | f02734e | 2023-06-02 11:34:24 +0200 | [diff] [blame] | 217 | write_output_to_file_or_stdout(path, &output)?; |
Mårten Kongstad | 4d2b4b0 | 2023-04-27 16:05:58 +0200 | [diff] [blame] | 218 | } |
| 219 | _ => unreachable!(), |
| 220 | } |
Mårten Kongstad | bb52072 | 2023-04-26 13:16:41 +0200 | [diff] [blame] | 221 | Ok(()) |
Mårten Kongstad | 867a349 | 2023-04-25 15:06:30 +0200 | [diff] [blame] | 222 | } |