blob: b60909b377a406d562e3e660c7f83c984f8da60b [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;
Dennis Shen1dc9ad42023-05-12 00:21:55 +000029mod codegen_cpp;
Zhi Doueb744892023-05-10 04:02:33 +000030mod codegen_java;
Mårten Kongstadf73b9632023-05-24 15:43:47 +020031mod codegen_rust;
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +020032mod commands;
Mårten Kongstadfe753f52023-04-26 09:13:03 +020033mod protos;
Mårten Kongstadfe753f52023-04-26 09:13:03 +020034
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +020035use crate::cache::Cache;
Mårten Kongstad6b9e3822023-05-16 11:19:58 +020036use commands::{DumpFormat, Input, OutputFile, Source};
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +020037
38fn cli() -> Command {
39 Command::new("aconfig")
40 .subcommand_required(true)
41 .subcommand(
42 Command::new("create-cache")
Mårten Kongstad30950782023-05-09 13:31:29 +020043 .arg(Arg::new("namespace").long("namespace").required(true))
Mårten Kongstadfa23d292023-05-11 14:47:02 +020044 .arg(Arg::new("declarations").long("declarations").action(ArgAction::Append))
45 .arg(Arg::new("values").long("values").action(ArgAction::Append))
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +020046 .arg(Arg::new("cache").long("cache").required(true)),
47 )
48 .subcommand(
Zhi Doueb744892023-05-10 04:02:33 +000049 Command::new("create-java-lib")
50 .arg(Arg::new("cache").long("cache").required(true))
51 .arg(Arg::new("out").long("out").required(true)),
52 )
53 .subcommand(
Dennis Shen1dc9ad42023-05-12 00:21:55 +000054 Command::new("create-cpp-lib")
55 .arg(Arg::new("cache").long("cache").required(true))
56 .arg(Arg::new("out").long("out").required(true)),
57 )
58 .subcommand(
Mårten Kongstadf73b9632023-05-24 15:43:47 +020059 Command::new("create-rust-lib")
60 .arg(Arg::new("cache").long("cache").required(true))
61 .arg(Arg::new("out").long("out").required(true)),
62 )
63 .subcommand(
Mårten Kongstada1029092023-05-08 11:51:59 +020064 Command::new("dump")
Mårten Kongstadaf677032023-05-17 16:18:25 +020065 .arg(Arg::new("cache").long("cache").action(ArgAction::Append).required(true))
Mårten Kongstada1029092023-05-08 11:51:59 +020066 .arg(
67 Arg::new("format")
68 .long("format")
Mårten Kongstadba94e6a2023-05-16 11:00:16 +020069 .value_parser(EnumValueParser::<commands::DumpFormat>::new())
Mårten Kongstada1029092023-05-08 11:51:59 +020070 .default_value("text"),
71 )
72 .arg(Arg::new("out").long("out").default_value("-")),
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +020073 )
74}
Mårten Kongstad867a3492023-04-25 15:06:30 +020075
Mårten Kongstad6b9e3822023-05-16 11:19:58 +020076fn get_required_arg<'a, T>(matches: &'a ArgMatches, arg_name: &str) -> Result<&'a T>
77where
78 T: Any + Clone + Send + Sync + 'static,
79{
80 matches
81 .get_one::<T>(arg_name)
82 .ok_or(anyhow!("internal error: required argument '{}' not found", arg_name))
83}
84
Mårten Kongstad98b0eeb2023-05-08 11:25:30 +020085fn open_zero_or_more_files(matches: &ArgMatches, arg_name: &str) -> Result<Vec<Input>> {
86 let mut opened_files = vec![];
87 for path in matches.get_many::<String>(arg_name).unwrap_or_default() {
88 let file = Box::new(fs::File::open(path)?);
89 opened_files.push(Input { source: Source::File(path.to_string()), reader: file });
90 }
91 Ok(opened_files)
92}
93
Mårten Kongstadd42eeeb2023-05-12 10:01:00 +020094fn write_output_file_realtive_to_dir(root: &Path, output_file: &OutputFile) -> Result<()> {
95 ensure!(
96 root.is_dir(),
97 "output directory {} does not exist or is not a directory",
98 root.display()
99 );
100 let path = root.join(output_file.path.clone());
101 let parent = path
102 .parent()
103 .ok_or(anyhow!("unable to locate parent of output file {}", path.display()))?;
104 fs::create_dir_all(parent)?;
105 let mut file = fs::File::create(path)?;
106 file.write_all(&output_file.contents)?;
107 Ok(())
108}
109
Mårten Kongstadbb520722023-04-26 13:16:41 +0200110fn main() -> Result<()> {
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +0200111 let matches = cli().get_matches();
112 match matches.subcommand() {
113 Some(("create-cache", sub_matches)) => {
Mårten Kongstad6b9e3822023-05-16 11:19:58 +0200114 let namespace = get_required_arg::<String>(sub_matches, "namespace")?;
Mårten Kongstadfa23d292023-05-11 14:47:02 +0200115 let declarations = open_zero_or_more_files(sub_matches, "declarations")?;
116 let values = open_zero_or_more_files(sub_matches, "values")?;
117 let cache = commands::create_cache(namespace, declarations, values)?;
Mårten Kongstad6b9e3822023-05-16 11:19:58 +0200118 let path = get_required_arg::<String>(sub_matches, "cache")?;
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +0200119 let file = fs::File::create(path)?;
120 cache.write_to_writer(file)?;
121 }
Zhi Doueb744892023-05-10 04:02:33 +0000122 Some(("create-java-lib", sub_matches)) => {
Mårten Kongstad6b9e3822023-05-16 11:19:58 +0200123 let path = get_required_arg::<String>(sub_matches, "cache")?;
Zhi Doueb744892023-05-10 04:02:33 +0000124 let file = fs::File::open(path)?;
125 let cache = Cache::read_from_reader(file)?;
Mårten Kongstad6b9e3822023-05-16 11:19:58 +0200126 let dir = PathBuf::from(get_required_arg::<String>(sub_matches, "out")?);
Dennis Shen1dc9ad42023-05-12 00:21:55 +0000127 let generated_file = commands::create_java_lib(&cache)?;
128 write_output_file_realtive_to_dir(&dir, &generated_file)?;
129 }
130 Some(("create-cpp-lib", sub_matches)) => {
131 let path = get_required_arg::<String>(sub_matches, "cache")?;
132 let file = fs::File::open(path)?;
133 let cache = Cache::read_from_reader(file)?;
134 let dir = PathBuf::from(get_required_arg::<String>(sub_matches, "out")?);
135 let generated_file = commands::create_cpp_lib(&cache)?;
Mårten Kongstadd42eeeb2023-05-12 10:01:00 +0200136 write_output_file_realtive_to_dir(&dir, &generated_file)?;
Zhi Doueb744892023-05-10 04:02:33 +0000137 }
Mårten Kongstadf73b9632023-05-24 15:43:47 +0200138 Some(("create-rust-lib", sub_matches)) => {
139 let path = get_required_arg::<String>(sub_matches, "cache")?;
140 let file = fs::File::open(path)?;
141 let cache = Cache::read_from_reader(file)?;
142 let dir = PathBuf::from(get_required_arg::<String>(sub_matches, "out")?);
143 let generated_file = commands::create_rust_lib(&cache)?;
144 write_output_file_realtive_to_dir(&dir, &generated_file)?;
145 }
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +0200146 Some(("dump", sub_matches)) => {
Mårten Kongstadaf677032023-05-17 16:18:25 +0200147 let mut caches = Vec::new();
148 for path in sub_matches.get_many::<String>("cache").unwrap_or_default() {
149 let file = fs::File::open(path)?;
150 let cache = Cache::read_from_reader(file)?;
151 caches.push(cache);
152 }
Mårten Kongstad6b9e3822023-05-16 11:19:58 +0200153 let format = get_required_arg::<DumpFormat>(sub_matches, "format")?;
Mårten Kongstadaf677032023-05-17 16:18:25 +0200154 let output = commands::dump_cache(caches, *format)?;
Mårten Kongstad6b9e3822023-05-16 11:19:58 +0200155 let path = get_required_arg::<String>(sub_matches, "out")?;
156 let mut file: Box<dyn Write> = if *path == "-" {
Mårten Kongstada1029092023-05-08 11:51:59 +0200157 Box::new(io::stdout())
158 } else {
159 Box::new(fs::File::create(path)?)
160 };
161 file.write_all(&output)?;
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +0200162 }
163 _ => unreachable!(),
164 }
Mårten Kongstadbb520722023-04-26 13:16:41 +0200165 Ok(())
Mårten Kongstad867a3492023-04-25 15:06:30 +0200166}