blob: d02307d7027995b5668b00231604c4acdc19b450 [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 Kongstad4d2b4b02023-04-27 16:05:58 +020031mod commands;
Mårten Kongstadfe753f52023-04-26 09:13:03 +020032mod protos;
Mårten Kongstadfe753f52023-04-26 09:13:03 +020033
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +020034use crate::cache::Cache;
Mårten Kongstad6b9e3822023-05-16 11:19:58 +020035use commands::{DumpFormat, Input, OutputFile, Source};
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +020036
37fn cli() -> Command {
38 Command::new("aconfig")
39 .subcommand_required(true)
40 .subcommand(
41 Command::new("create-cache")
Mårten Kongstad30950782023-05-09 13:31:29 +020042 .arg(Arg::new("namespace").long("namespace").required(true))
Mårten Kongstadfa23d292023-05-11 14:47:02 +020043 .arg(Arg::new("declarations").long("declarations").action(ArgAction::Append))
44 .arg(Arg::new("values").long("values").action(ArgAction::Append))
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +020045 .arg(Arg::new("cache").long("cache").required(true)),
46 )
47 .subcommand(
Zhi Doueb744892023-05-10 04:02:33 +000048 Command::new("create-java-lib")
49 .arg(Arg::new("cache").long("cache").required(true))
50 .arg(Arg::new("out").long("out").required(true)),
51 )
52 .subcommand(
Dennis Shen1dc9ad42023-05-12 00:21:55 +000053 Command::new("create-cpp-lib")
54 .arg(Arg::new("cache").long("cache").required(true))
55 .arg(Arg::new("out").long("out").required(true)),
56 )
57 .subcommand(
Mårten Kongstada1029092023-05-08 11:51:59 +020058 Command::new("dump")
Mårten Kongstadaf677032023-05-17 16:18:25 +020059 .arg(Arg::new("cache").long("cache").action(ArgAction::Append).required(true))
Mårten Kongstada1029092023-05-08 11:51:59 +020060 .arg(
61 Arg::new("format")
62 .long("format")
Mårten Kongstadba94e6a2023-05-16 11:00:16 +020063 .value_parser(EnumValueParser::<commands::DumpFormat>::new())
Mårten Kongstada1029092023-05-08 11:51:59 +020064 .default_value("text"),
65 )
66 .arg(Arg::new("out").long("out").default_value("-")),
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +020067 )
68}
Mårten Kongstad867a3492023-04-25 15:06:30 +020069
Mårten Kongstad6b9e3822023-05-16 11:19:58 +020070fn get_required_arg<'a, T>(matches: &'a ArgMatches, arg_name: &str) -> Result<&'a T>
71where
72 T: Any + Clone + Send + Sync + 'static,
73{
74 matches
75 .get_one::<T>(arg_name)
76 .ok_or(anyhow!("internal error: required argument '{}' not found", arg_name))
77}
78
Mårten Kongstad98b0eeb2023-05-08 11:25:30 +020079fn open_zero_or_more_files(matches: &ArgMatches, arg_name: &str) -> Result<Vec<Input>> {
80 let mut opened_files = vec![];
81 for path in matches.get_many::<String>(arg_name).unwrap_or_default() {
82 let file = Box::new(fs::File::open(path)?);
83 opened_files.push(Input { source: Source::File(path.to_string()), reader: file });
84 }
85 Ok(opened_files)
86}
87
Mårten Kongstadd42eeeb2023-05-12 10:01:00 +020088fn write_output_file_realtive_to_dir(root: &Path, output_file: &OutputFile) -> Result<()> {
89 ensure!(
90 root.is_dir(),
91 "output directory {} does not exist or is not a directory",
92 root.display()
93 );
94 let path = root.join(output_file.path.clone());
95 let parent = path
96 .parent()
97 .ok_or(anyhow!("unable to locate parent of output file {}", path.display()))?;
98 fs::create_dir_all(parent)?;
99 let mut file = fs::File::create(path)?;
100 file.write_all(&output_file.contents)?;
101 Ok(())
102}
103
Mårten Kongstadbb520722023-04-26 13:16:41 +0200104fn main() -> Result<()> {
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +0200105 let matches = cli().get_matches();
106 match matches.subcommand() {
107 Some(("create-cache", sub_matches)) => {
Mårten Kongstad6b9e3822023-05-16 11:19:58 +0200108 let namespace = get_required_arg::<String>(sub_matches, "namespace")?;
Mårten Kongstadfa23d292023-05-11 14:47:02 +0200109 let declarations = open_zero_or_more_files(sub_matches, "declarations")?;
110 let values = open_zero_or_more_files(sub_matches, "values")?;
111 let cache = commands::create_cache(namespace, declarations, values)?;
Mårten Kongstad6b9e3822023-05-16 11:19:58 +0200112 let path = get_required_arg::<String>(sub_matches, "cache")?;
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +0200113 let file = fs::File::create(path)?;
114 cache.write_to_writer(file)?;
115 }
Zhi Doueb744892023-05-10 04:02:33 +0000116 Some(("create-java-lib", sub_matches)) => {
Mårten Kongstad6b9e3822023-05-16 11:19:58 +0200117 let path = get_required_arg::<String>(sub_matches, "cache")?;
Zhi Doueb744892023-05-10 04:02:33 +0000118 let file = fs::File::open(path)?;
119 let cache = Cache::read_from_reader(file)?;
Mårten Kongstad6b9e3822023-05-16 11:19:58 +0200120 let dir = PathBuf::from(get_required_arg::<String>(sub_matches, "out")?);
Dennis Shen1dc9ad42023-05-12 00:21:55 +0000121 let generated_file = commands::create_java_lib(&cache)?;
122 write_output_file_realtive_to_dir(&dir, &generated_file)?;
123 }
124 Some(("create-cpp-lib", sub_matches)) => {
125 let path = get_required_arg::<String>(sub_matches, "cache")?;
126 let file = fs::File::open(path)?;
127 let cache = Cache::read_from_reader(file)?;
128 let dir = PathBuf::from(get_required_arg::<String>(sub_matches, "out")?);
129 let generated_file = commands::create_cpp_lib(&cache)?;
Mårten Kongstadd42eeeb2023-05-12 10:01:00 +0200130 write_output_file_realtive_to_dir(&dir, &generated_file)?;
Zhi Doueb744892023-05-10 04:02:33 +0000131 }
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +0200132 Some(("dump", sub_matches)) => {
Mårten Kongstadaf677032023-05-17 16:18:25 +0200133 let mut caches = Vec::new();
134 for path in sub_matches.get_many::<String>("cache").unwrap_or_default() {
135 let file = fs::File::open(path)?;
136 let cache = Cache::read_from_reader(file)?;
137 caches.push(cache);
138 }
Mårten Kongstad6b9e3822023-05-16 11:19:58 +0200139 let format = get_required_arg::<DumpFormat>(sub_matches, "format")?;
Mårten Kongstadaf677032023-05-17 16:18:25 +0200140 let output = commands::dump_cache(caches, *format)?;
Mårten Kongstad6b9e3822023-05-16 11:19:58 +0200141 let path = get_required_arg::<String>(sub_matches, "out")?;
142 let mut file: Box<dyn Write> = if *path == "-" {
Mårten Kongstada1029092023-05-08 11:51:59 +0200143 Box::new(io::stdout())
144 } else {
145 Box::new(fs::File::create(path)?)
146 };
147 file.write_all(&output)?;
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +0200148 }
149 _ => unreachable!(),
150 }
Mårten Kongstadbb520722023-04-26 13:16:41 +0200151 Ok(())
Mårten Kongstad867a3492023-04-25 15:06:30 +0200152}