blob: f632ce7efc79462dc434098541a08aca22ac5fc1 [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 Kongstad30950782023-05-09 13:31:29 +020047 .arg(Arg::new("namespace").long("namespace").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 Kongstada1029092023-05-08 11:51:59 +020073 Command::new("dump")
Mårten Kongstadaf677032023-05-17 16:18:25 +020074 .arg(Arg::new("cache").long("cache").action(ArgAction::Append).required(true))
Mårten Kongstada1029092023-05-08 11:51:59 +020075 .arg(
76 Arg::new("format")
77 .long("format")
Mårten Kongstadba94e6a2023-05-16 11:00:16 +020078 .value_parser(EnumValueParser::<commands::DumpFormat>::new())
Mårten Kongstada1029092023-05-08 11:51:59 +020079 .default_value("text"),
80 )
81 .arg(Arg::new("out").long("out").default_value("-")),
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +020082 )
83}
Mårten Kongstad867a3492023-04-25 15:06:30 +020084
Mårten Kongstad6b9e3822023-05-16 11:19:58 +020085fn get_required_arg<'a, T>(matches: &'a ArgMatches, arg_name: &str) -> Result<&'a T>
86where
87 T: Any + Clone + Send + Sync + 'static,
88{
89 matches
90 .get_one::<T>(arg_name)
91 .ok_or(anyhow!("internal error: required argument '{}' not found", arg_name))
92}
93
Mårten Kongstad98b0eeb2023-05-08 11:25:30 +020094fn open_zero_or_more_files(matches: &ArgMatches, arg_name: &str) -> Result<Vec<Input>> {
95 let mut opened_files = vec![];
96 for path in matches.get_many::<String>(arg_name).unwrap_or_default() {
97 let file = Box::new(fs::File::open(path)?);
98 opened_files.push(Input { source: Source::File(path.to_string()), reader: file });
99 }
100 Ok(opened_files)
101}
102
Mårten Kongstadd42eeeb2023-05-12 10:01:00 +0200103fn write_output_file_realtive_to_dir(root: &Path, output_file: &OutputFile) -> Result<()> {
104 ensure!(
105 root.is_dir(),
106 "output directory {} does not exist or is not a directory",
107 root.display()
108 );
109 let path = root.join(output_file.path.clone());
110 let parent = path
111 .parent()
112 .ok_or(anyhow!("unable to locate parent of output file {}", path.display()))?;
113 fs::create_dir_all(parent)?;
114 let mut file = fs::File::create(path)?;
115 file.write_all(&output_file.contents)?;
116 Ok(())
117}
118
Mårten Kongstadf02734e2023-06-02 11:34:24 +0200119fn write_output_to_file_or_stdout(path: &str, data: &[u8]) -> Result<()> {
120 if path == "-" {
121 io::stdout().write_all(data)?;
122 } else {
123 fs::File::create(path)?.write_all(data)?;
124 }
125 Ok(())
126}
127
Mårten Kongstadbb520722023-04-26 13:16:41 +0200128fn main() -> Result<()> {
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +0200129 let matches = cli().get_matches();
130 match matches.subcommand() {
131 Some(("create-cache", sub_matches)) => {
Mårten Kongstad6b9e3822023-05-16 11:19:58 +0200132 let namespace = get_required_arg::<String>(sub_matches, "namespace")?;
Mårten Kongstadfa23d292023-05-11 14:47:02 +0200133 let declarations = open_zero_or_more_files(sub_matches, "declarations")?;
134 let values = open_zero_or_more_files(sub_matches, "values")?;
135 let cache = commands::create_cache(namespace, declarations, values)?;
Mårten Kongstad6b9e3822023-05-16 11:19:58 +0200136 let path = get_required_arg::<String>(sub_matches, "cache")?;
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +0200137 let file = fs::File::create(path)?;
138 cache.write_to_writer(file)?;
139 }
Zhi Doueb744892023-05-10 04:02:33 +0000140 Some(("create-java-lib", sub_matches)) => {
Mårten Kongstad6b9e3822023-05-16 11:19:58 +0200141 let path = get_required_arg::<String>(sub_matches, "cache")?;
Zhi Doueb744892023-05-10 04:02:33 +0000142 let file = fs::File::open(path)?;
143 let cache = Cache::read_from_reader(file)?;
Mårten Kongstad6b9e3822023-05-16 11:19:58 +0200144 let dir = PathBuf::from(get_required_arg::<String>(sub_matches, "out")?);
Mårten Kongstadb27f2ce2023-06-02 11:43:21 +0200145 let generated_file = commands::create_java_lib(cache)?;
Dennis Shen1dc9ad42023-05-12 00:21:55 +0000146 write_output_file_realtive_to_dir(&dir, &generated_file)?;
147 }
148 Some(("create-cpp-lib", sub_matches)) => {
149 let path = get_required_arg::<String>(sub_matches, "cache")?;
150 let file = fs::File::open(path)?;
151 let cache = Cache::read_from_reader(file)?;
152 let dir = PathBuf::from(get_required_arg::<String>(sub_matches, "out")?);
Mårten Kongstadb27f2ce2023-06-02 11:43:21 +0200153 let generated_file = commands::create_cpp_lib(cache)?;
Mårten Kongstadd42eeeb2023-05-12 10:01:00 +0200154 write_output_file_realtive_to_dir(&dir, &generated_file)?;
Zhi Doueb744892023-05-10 04:02:33 +0000155 }
Mårten Kongstadf73b9632023-05-24 15:43:47 +0200156 Some(("create-rust-lib", sub_matches)) => {
157 let path = get_required_arg::<String>(sub_matches, "cache")?;
158 let file = fs::File::open(path)?;
159 let cache = Cache::read_from_reader(file)?;
160 let dir = PathBuf::from(get_required_arg::<String>(sub_matches, "out")?);
Mårten Kongstadb27f2ce2023-06-02 11:43:21 +0200161 let generated_file = commands::create_rust_lib(cache)?;
Mårten Kongstadf73b9632023-05-24 15:43:47 +0200162 write_output_file_realtive_to_dir(&dir, &generated_file)?;
163 }
Mårten Kongstadf02734e2023-06-02 11:34:24 +0200164 Some(("create-device-config-defaults", sub_matches)) => {
165 let mut caches = Vec::new();
166 for path in sub_matches.get_many::<String>("cache").unwrap_or_default() {
167 let file = fs::File::open(path)?;
168 let cache = Cache::read_from_reader(file)?;
169 caches.push(cache);
170 }
171 let output = commands::create_device_config_defaults(caches)?;
172 let path = get_required_arg::<String>(sub_matches, "out")?;
173 write_output_to_file_or_stdout(path, &output)?;
174 }
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +0200175 Some(("dump", sub_matches)) => {
Mårten Kongstadaf677032023-05-17 16:18:25 +0200176 let mut caches = Vec::new();
177 for path in sub_matches.get_many::<String>("cache").unwrap_or_default() {
178 let file = fs::File::open(path)?;
179 let cache = Cache::read_from_reader(file)?;
180 caches.push(cache);
181 }
Mårten Kongstad6b9e3822023-05-16 11:19:58 +0200182 let format = get_required_arg::<DumpFormat>(sub_matches, "format")?;
Mårten Kongstadaf677032023-05-17 16:18:25 +0200183 let output = commands::dump_cache(caches, *format)?;
Mårten Kongstad6b9e3822023-05-16 11:19:58 +0200184 let path = get_required_arg::<String>(sub_matches, "out")?;
Mårten Kongstadf02734e2023-06-02 11:34:24 +0200185 write_output_to_file_or_stdout(path, &output)?;
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +0200186 }
187 _ => unreachable!(),
188 }
Mårten Kongstadbb520722023-04-26 13:16:41 +0200189 Ok(())
Mårten Kongstad867a3492023-04-25 15:06:30 +0200190}