blob: 5a820d9e4e862dbf9a7cb42269f630854acf19dd [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 Kongstadd42eeeb2023-05-12 10:01:00 +020019use anyhow::{anyhow, 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 Kongstadbb520722023-04-26 13:16:41 +020027mod aconfig;
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +020028mod cache;
Mårten Kongstad00cf0452023-05-26 16:48:01 +020029mod codegen;
Dennis Shen1dc9ad42023-05-12 00:21:55 +000030mod codegen_cpp;
Zhi Doueb744892023-05-10 04:02:33 +000031mod codegen_java;
Mårten Kongstadf73b9632023-05-24 15:43:47 +020032mod codegen_rust;
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +020033mod commands;
Mårten Kongstadfe753f52023-04-26 09:13:03 +020034mod protos;
Mårten Kongstadfe753f52023-04-26 09:13:03 +020035
Mårten Kongstad83a87602023-06-02 11:20:15 +020036#[cfg(test)]
37mod test;
38
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +020039use crate::cache::Cache;
Mårten Kongstad6b9e3822023-05-16 11:19:58 +020040use commands::{DumpFormat, Input, OutputFile, Source};
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +020041
42fn cli() -> Command {
43 Command::new("aconfig")
44 .subcommand_required(true)
45 .subcommand(
46 Command::new("create-cache")
Mårten Kongstad9fb58962023-05-31 13:02:13 +020047 .arg(Arg::new("package").long("package").required(true))
Mårten Kongstadfa23d292023-05-11 14:47:02 +020048 .arg(Arg::new("declarations").long("declarations").action(ArgAction::Append))
49 .arg(Arg::new("values").long("values").action(ArgAction::Append))
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +020050 .arg(Arg::new("cache").long("cache").required(true)),
51 )
52 .subcommand(
Zhi Doueb744892023-05-10 04:02:33 +000053 Command::new("create-java-lib")
54 .arg(Arg::new("cache").long("cache").required(true))
55 .arg(Arg::new("out").long("out").required(true)),
56 )
57 .subcommand(
Dennis Shen1dc9ad42023-05-12 00:21:55 +000058 Command::new("create-cpp-lib")
59 .arg(Arg::new("cache").long("cache").required(true))
60 .arg(Arg::new("out").long("out").required(true)),
61 )
62 .subcommand(
Mårten Kongstadf73b9632023-05-24 15:43:47 +020063 Command::new("create-rust-lib")
64 .arg(Arg::new("cache").long("cache").required(true))
65 .arg(Arg::new("out").long("out").required(true)),
66 )
67 .subcommand(
Mårten Kongstadf02734e2023-06-02 11:34:24 +020068 Command::new("create-device-config-defaults")
69 .arg(Arg::new("cache").long("cache").action(ArgAction::Append).required(true))
70 .arg(Arg::new("out").long("out").default_value("-")),
71 )
72 .subcommand(
Mårten Kongstadc31a6ff2023-06-02 11:54:36 +020073 Command::new("create-device-config-sysprops")
74 .arg(Arg::new("cache").long("cache").action(ArgAction::Append).required(true))
75 .arg(Arg::new("out").long("out").default_value("-")),
76 )
77 .subcommand(
Mårten Kongstada1029092023-05-08 11:51:59 +020078 Command::new("dump")
Mårten Kongstadaf677032023-05-17 16:18:25 +020079 .arg(Arg::new("cache").long("cache").action(ArgAction::Append).required(true))
Mårten Kongstada1029092023-05-08 11:51:59 +020080 .arg(
81 Arg::new("format")
82 .long("format")
Mårten Kongstadba94e6a2023-05-16 11:00:16 +020083 .value_parser(EnumValueParser::<commands::DumpFormat>::new())
Mårten Kongstada1029092023-05-08 11:51:59 +020084 .default_value("text"),
85 )
86 .arg(Arg::new("out").long("out").default_value("-")),
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +020087 )
88}
Mårten Kongstad867a3492023-04-25 15:06:30 +020089
Mårten Kongstad6b9e3822023-05-16 11:19:58 +020090fn get_required_arg<'a, T>(matches: &'a ArgMatches, arg_name: &str) -> Result<&'a T>
91where
92 T: Any + Clone + Send + Sync + 'static,
93{
94 matches
95 .get_one::<T>(arg_name)
96 .ok_or(anyhow!("internal error: required argument '{}' not found", arg_name))
97}
98
Mårten Kongstad98b0eeb2023-05-08 11:25:30 +020099fn open_zero_or_more_files(matches: &ArgMatches, arg_name: &str) -> Result<Vec<Input>> {
100 let mut opened_files = vec![];
101 for path in matches.get_many::<String>(arg_name).unwrap_or_default() {
102 let file = Box::new(fs::File::open(path)?);
103 opened_files.push(Input { source: Source::File(path.to_string()), reader: file });
104 }
105 Ok(opened_files)
106}
107
Mårten Kongstadd42eeeb2023-05-12 10:01:00 +0200108fn write_output_file_realtive_to_dir(root: &Path, output_file: &OutputFile) -> Result<()> {
109 ensure!(
110 root.is_dir(),
111 "output directory {} does not exist or is not a directory",
112 root.display()
113 );
114 let path = root.join(output_file.path.clone());
115 let parent = path
116 .parent()
117 .ok_or(anyhow!("unable to locate parent of output file {}", path.display()))?;
118 fs::create_dir_all(parent)?;
119 let mut file = fs::File::create(path)?;
120 file.write_all(&output_file.contents)?;
121 Ok(())
122}
123
Mårten Kongstadf02734e2023-06-02 11:34:24 +0200124fn write_output_to_file_or_stdout(path: &str, data: &[u8]) -> Result<()> {
125 if path == "-" {
126 io::stdout().write_all(data)?;
127 } else {
128 fs::File::create(path)?.write_all(data)?;
129 }
130 Ok(())
131}
132
Mårten Kongstadbb520722023-04-26 13:16:41 +0200133fn main() -> Result<()> {
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +0200134 let matches = cli().get_matches();
135 match matches.subcommand() {
136 Some(("create-cache", sub_matches)) => {
Mårten Kongstad9fb58962023-05-31 13:02:13 +0200137 let package = get_required_arg::<String>(sub_matches, "package")?;
Mårten Kongstadfa23d292023-05-11 14:47:02 +0200138 let declarations = open_zero_or_more_files(sub_matches, "declarations")?;
139 let values = open_zero_or_more_files(sub_matches, "values")?;
Mårten Kongstad9fb58962023-05-31 13:02:13 +0200140 let cache = commands::create_cache(package, declarations, values)?;
Mårten Kongstad6b9e3822023-05-16 11:19:58 +0200141 let path = get_required_arg::<String>(sub_matches, "cache")?;
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +0200142 let file = fs::File::create(path)?;
143 cache.write_to_writer(file)?;
144 }
Zhi Doueb744892023-05-10 04:02:33 +0000145 Some(("create-java-lib", sub_matches)) => {
Mårten Kongstad6b9e3822023-05-16 11:19:58 +0200146 let path = get_required_arg::<String>(sub_matches, "cache")?;
Zhi Doueb744892023-05-10 04:02:33 +0000147 let file = fs::File::open(path)?;
148 let cache = Cache::read_from_reader(file)?;
Mårten Kongstad6b9e3822023-05-16 11:19:58 +0200149 let dir = PathBuf::from(get_required_arg::<String>(sub_matches, "out")?);
Mårten Kongstadb27f2ce2023-06-02 11:43:21 +0200150 let generated_file = commands::create_java_lib(cache)?;
Dennis Shen1dc9ad42023-05-12 00:21:55 +0000151 write_output_file_realtive_to_dir(&dir, &generated_file)?;
152 }
153 Some(("create-cpp-lib", sub_matches)) => {
154 let path = get_required_arg::<String>(sub_matches, "cache")?;
155 let file = fs::File::open(path)?;
156 let cache = Cache::read_from_reader(file)?;
157 let dir = PathBuf::from(get_required_arg::<String>(sub_matches, "out")?);
Mårten Kongstadb27f2ce2023-06-02 11:43:21 +0200158 let generated_file = commands::create_cpp_lib(cache)?;
Mårten Kongstadd42eeeb2023-05-12 10:01:00 +0200159 write_output_file_realtive_to_dir(&dir, &generated_file)?;
Zhi Doueb744892023-05-10 04:02:33 +0000160 }
Mårten Kongstadf73b9632023-05-24 15:43:47 +0200161 Some(("create-rust-lib", sub_matches)) => {
162 let path = get_required_arg::<String>(sub_matches, "cache")?;
163 let file = fs::File::open(path)?;
164 let cache = Cache::read_from_reader(file)?;
165 let dir = PathBuf::from(get_required_arg::<String>(sub_matches, "out")?);
Mårten Kongstadb27f2ce2023-06-02 11:43:21 +0200166 let generated_file = commands::create_rust_lib(cache)?;
Mårten Kongstadf73b9632023-05-24 15:43:47 +0200167 write_output_file_realtive_to_dir(&dir, &generated_file)?;
168 }
Mårten Kongstadf02734e2023-06-02 11:34:24 +0200169 Some(("create-device-config-defaults", sub_matches)) => {
170 let mut caches = Vec::new();
171 for path in sub_matches.get_many::<String>("cache").unwrap_or_default() {
172 let file = fs::File::open(path)?;
173 let cache = Cache::read_from_reader(file)?;
174 caches.push(cache);
175 }
176 let output = commands::create_device_config_defaults(caches)?;
177 let path = get_required_arg::<String>(sub_matches, "out")?;
178 write_output_to_file_or_stdout(path, &output)?;
179 }
Mårten Kongstadc31a6ff2023-06-02 11:54:36 +0200180 Some(("create-device-config-sysprops", sub_matches)) => {
181 let mut caches = Vec::new();
182 for path in sub_matches.get_many::<String>("cache").unwrap_or_default() {
183 let file = fs::File::open(path)?;
184 let cache = Cache::read_from_reader(file)?;
185 caches.push(cache);
186 }
187 let output = commands::create_device_config_sysprops(caches)?;
188 let path = get_required_arg::<String>(sub_matches, "out")?;
189 write_output_to_file_or_stdout(path, &output)?;
190 }
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +0200191 Some(("dump", sub_matches)) => {
Mårten Kongstadaf677032023-05-17 16:18:25 +0200192 let mut caches = Vec::new();
193 for path in sub_matches.get_many::<String>("cache").unwrap_or_default() {
194 let file = fs::File::open(path)?;
195 let cache = Cache::read_from_reader(file)?;
196 caches.push(cache);
197 }
Mårten Kongstad6b9e3822023-05-16 11:19:58 +0200198 let format = get_required_arg::<DumpFormat>(sub_matches, "format")?;
Mårten Kongstadaf677032023-05-17 16:18:25 +0200199 let output = commands::dump_cache(caches, *format)?;
Mårten Kongstad6b9e3822023-05-16 11:19:58 +0200200 let path = get_required_arg::<String>(sub_matches, "out")?;
Mårten Kongstadf02734e2023-06-02 11:34:24 +0200201 write_output_to_file_or_stdout(path, &output)?;
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +0200202 }
203 _ => unreachable!(),
204 }
Mårten Kongstadbb520722023-04-26 13:16:41 +0200205 Ok(())
Mårten Kongstad867a3492023-04-25 15:06:30 +0200206}