blob: b3b6ac408ff3932402d1be41c419ec4f6d648501 [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 Kongstada1029092023-05-08 11:51:59 +020068 Command::new("dump")
Mårten Kongstadaf677032023-05-17 16:18:25 +020069 .arg(Arg::new("cache").long("cache").action(ArgAction::Append).required(true))
Mårten Kongstada1029092023-05-08 11:51:59 +020070 .arg(
71 Arg::new("format")
72 .long("format")
Mårten Kongstadba94e6a2023-05-16 11:00:16 +020073 .value_parser(EnumValueParser::<commands::DumpFormat>::new())
Mårten Kongstada1029092023-05-08 11:51:59 +020074 .default_value("text"),
75 )
76 .arg(Arg::new("out").long("out").default_value("-")),
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +020077 )
78}
Mårten Kongstad867a3492023-04-25 15:06:30 +020079
Mårten Kongstad6b9e3822023-05-16 11:19:58 +020080fn get_required_arg<'a, T>(matches: &'a ArgMatches, arg_name: &str) -> Result<&'a T>
81where
82 T: Any + Clone + Send + Sync + 'static,
83{
84 matches
85 .get_one::<T>(arg_name)
86 .ok_or(anyhow!("internal error: required argument '{}' not found", arg_name))
87}
88
Mårten Kongstad98b0eeb2023-05-08 11:25:30 +020089fn open_zero_or_more_files(matches: &ArgMatches, arg_name: &str) -> Result<Vec<Input>> {
90 let mut opened_files = vec![];
91 for path in matches.get_many::<String>(arg_name).unwrap_or_default() {
92 let file = Box::new(fs::File::open(path)?);
93 opened_files.push(Input { source: Source::File(path.to_string()), reader: file });
94 }
95 Ok(opened_files)
96}
97
Mårten Kongstadd42eeeb2023-05-12 10:01:00 +020098fn write_output_file_realtive_to_dir(root: &Path, output_file: &OutputFile) -> Result<()> {
99 ensure!(
100 root.is_dir(),
101 "output directory {} does not exist or is not a directory",
102 root.display()
103 );
104 let path = root.join(output_file.path.clone());
105 let parent = path
106 .parent()
107 .ok_or(anyhow!("unable to locate parent of output file {}", path.display()))?;
108 fs::create_dir_all(parent)?;
109 let mut file = fs::File::create(path)?;
110 file.write_all(&output_file.contents)?;
111 Ok(())
112}
113
Mårten Kongstadbb520722023-04-26 13:16:41 +0200114fn main() -> Result<()> {
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +0200115 let matches = cli().get_matches();
116 match matches.subcommand() {
117 Some(("create-cache", sub_matches)) => {
Mårten Kongstad6b9e3822023-05-16 11:19:58 +0200118 let namespace = get_required_arg::<String>(sub_matches, "namespace")?;
Mårten Kongstadfa23d292023-05-11 14:47:02 +0200119 let declarations = open_zero_or_more_files(sub_matches, "declarations")?;
120 let values = open_zero_or_more_files(sub_matches, "values")?;
121 let cache = commands::create_cache(namespace, declarations, values)?;
Mårten Kongstad6b9e3822023-05-16 11:19:58 +0200122 let path = get_required_arg::<String>(sub_matches, "cache")?;
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +0200123 let file = fs::File::create(path)?;
124 cache.write_to_writer(file)?;
125 }
Zhi Doueb744892023-05-10 04:02:33 +0000126 Some(("create-java-lib", sub_matches)) => {
Mårten Kongstad6b9e3822023-05-16 11:19:58 +0200127 let path = get_required_arg::<String>(sub_matches, "cache")?;
Zhi Doueb744892023-05-10 04:02:33 +0000128 let file = fs::File::open(path)?;
129 let cache = Cache::read_from_reader(file)?;
Mårten Kongstad6b9e3822023-05-16 11:19:58 +0200130 let dir = PathBuf::from(get_required_arg::<String>(sub_matches, "out")?);
Mårten Kongstadb27f2ce2023-06-02 11:43:21 +0200131 let generated_file = commands::create_java_lib(cache)?;
Dennis Shen1dc9ad42023-05-12 00:21:55 +0000132 write_output_file_realtive_to_dir(&dir, &generated_file)?;
133 }
134 Some(("create-cpp-lib", sub_matches)) => {
135 let path = get_required_arg::<String>(sub_matches, "cache")?;
136 let file = fs::File::open(path)?;
137 let cache = Cache::read_from_reader(file)?;
138 let dir = PathBuf::from(get_required_arg::<String>(sub_matches, "out")?);
Mårten Kongstadb27f2ce2023-06-02 11:43:21 +0200139 let generated_file = commands::create_cpp_lib(cache)?;
Mårten Kongstadd42eeeb2023-05-12 10:01:00 +0200140 write_output_file_realtive_to_dir(&dir, &generated_file)?;
Zhi Doueb744892023-05-10 04:02:33 +0000141 }
Mårten Kongstadf73b9632023-05-24 15:43:47 +0200142 Some(("create-rust-lib", sub_matches)) => {
143 let path = get_required_arg::<String>(sub_matches, "cache")?;
144 let file = fs::File::open(path)?;
145 let cache = Cache::read_from_reader(file)?;
146 let dir = PathBuf::from(get_required_arg::<String>(sub_matches, "out")?);
Mårten Kongstadb27f2ce2023-06-02 11:43:21 +0200147 let generated_file = commands::create_rust_lib(cache)?;
Mårten Kongstadf73b9632023-05-24 15:43:47 +0200148 write_output_file_realtive_to_dir(&dir, &generated_file)?;
149 }
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +0200150 Some(("dump", sub_matches)) => {
Mårten Kongstadaf677032023-05-17 16:18:25 +0200151 let mut caches = Vec::new();
152 for path in sub_matches.get_many::<String>("cache").unwrap_or_default() {
153 let file = fs::File::open(path)?;
154 let cache = Cache::read_from_reader(file)?;
155 caches.push(cache);
156 }
Mårten Kongstad6b9e3822023-05-16 11:19:58 +0200157 let format = get_required_arg::<DumpFormat>(sub_matches, "format")?;
Mårten Kongstadaf677032023-05-17 16:18:25 +0200158 let output = commands::dump_cache(caches, *format)?;
Mårten Kongstad6b9e3822023-05-16 11:19:58 +0200159 let path = get_required_arg::<String>(sub_matches, "out")?;
160 let mut file: Box<dyn Write> = if *path == "-" {
Mårten Kongstada1029092023-05-08 11:51:59 +0200161 Box::new(io::stdout())
162 } else {
163 Box::new(fs::File::create(path)?)
164 };
165 file.write_all(&output)?;
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +0200166 }
167 _ => unreachable!(),
168 }
Mårten Kongstadbb520722023-04-26 13:16:41 +0200169 Ok(())
Mårten Kongstad867a3492023-04-25 15:06:30 +0200170}