Merge changes from topic "aconfig-create-rust-lib" am: aa977fb083
Original change: https://android-review.googlesource.com/c/platform/build/+/2602726 Change-Id: I2e708156312d2274c430a698b983b5cfe73c20ab Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
This commit is contained in:
commit
5c2756c13a
7 changed files with 229 additions and 1 deletions
129
tools/aconfig/src/codegen_rust.rs
Normal file
129
tools/aconfig/src/codegen_rust.rs
Normal file
|
@ -0,0 +1,129 @@
|
|||
/*
|
||||
* Copyright (C) 2023 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
use anyhow::Result;
|
||||
use serde::Serialize;
|
||||
use tinytemplate::TinyTemplate;
|
||||
|
||||
use crate::aconfig::{FlagState, Permission};
|
||||
use crate::cache::{Cache, Item};
|
||||
use crate::commands::OutputFile;
|
||||
|
||||
pub fn generate_rust_code(cache: &Cache) -> Result<OutputFile> {
|
||||
let namespace = cache.namespace().to_lowercase();
|
||||
let parsed_flags: Vec<TemplateParsedFlag> =
|
||||
cache.iter().map(|item| create_template_parsed_flag(&namespace, item)).collect();
|
||||
let context = TemplateContext { namespace, parsed_flags };
|
||||
let mut template = TinyTemplate::new();
|
||||
template.add_template("rust_code_gen", include_str!("../templates/rust.template"))?;
|
||||
let contents = template.render("rust_code_gen", &context)?;
|
||||
let path = ["src", "lib.rs"].iter().collect();
|
||||
Ok(OutputFile { contents: contents.into(), path })
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct TemplateContext {
|
||||
pub namespace: String,
|
||||
pub parsed_flags: Vec<TemplateParsedFlag>,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct TemplateParsedFlag {
|
||||
pub name: String,
|
||||
pub fn_name: String,
|
||||
|
||||
// TinyTemplate's conditionals are limited to single <bool> expressions; list all options here
|
||||
// Invariant: exactly one of these fields will be true
|
||||
pub is_read_only_enabled: bool,
|
||||
pub is_read_only_disabled: bool,
|
||||
pub is_read_write: bool,
|
||||
}
|
||||
|
||||
#[allow(clippy::nonminimal_bool)]
|
||||
fn create_template_parsed_flag(namespace: &str, item: &Item) -> TemplateParsedFlag {
|
||||
let template = TemplateParsedFlag {
|
||||
name: item.name.clone(),
|
||||
fn_name: format!("{}_{}", namespace, item.name.replace('-', "_").to_lowercase()),
|
||||
is_read_only_enabled: item.permission == Permission::ReadOnly
|
||||
&& item.state == FlagState::Enabled,
|
||||
is_read_only_disabled: item.permission == Permission::ReadOnly
|
||||
&& item.state == FlagState::Disabled,
|
||||
is_read_write: item.permission == Permission::ReadWrite,
|
||||
};
|
||||
#[rustfmt::skip]
|
||||
debug_assert!(
|
||||
(template.is_read_only_enabled && !template.is_read_only_disabled && !template.is_read_write) ||
|
||||
(!template.is_read_only_enabled && template.is_read_only_disabled && !template.is_read_write) ||
|
||||
(!template.is_read_only_enabled && !template.is_read_only_disabled && template.is_read_write),
|
||||
"TemplateParsedFlag invariant failed: {} {} {}",
|
||||
template.is_read_only_enabled,
|
||||
template.is_read_only_disabled,
|
||||
template.is_read_write,
|
||||
);
|
||||
template
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::commands::{create_cache, Input, Source};
|
||||
|
||||
#[test]
|
||||
fn test_generate_rust_code() {
|
||||
let cache = create_cache(
|
||||
"test",
|
||||
vec![Input {
|
||||
source: Source::File("testdata/test.aconfig".to_string()),
|
||||
reader: Box::new(include_bytes!("../testdata/test.aconfig").as_slice()),
|
||||
}],
|
||||
vec![
|
||||
Input {
|
||||
source: Source::File("testdata/first.values".to_string()),
|
||||
reader: Box::new(include_bytes!("../testdata/first.values").as_slice()),
|
||||
},
|
||||
Input {
|
||||
source: Source::File("testdata/test.aconfig".to_string()),
|
||||
reader: Box::new(include_bytes!("../testdata/second.values").as_slice()),
|
||||
},
|
||||
],
|
||||
)
|
||||
.unwrap();
|
||||
let generated = generate_rust_code(&cache).unwrap();
|
||||
assert_eq!("src/lib.rs", format!("{}", generated.path.display()));
|
||||
let expected = r#"
|
||||
#[inline(always)]
|
||||
pub const fn r#test_disabled_ro() -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn r#test_disabled_rw() -> bool {
|
||||
profcollect_libflags_rust::GetServerConfigurableFlag("test", "disabled-rw", "false") == "true"
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub const fn r#test_enabled_ro() -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn r#test_enabled_rw() -> bool {
|
||||
profcollect_libflags_rust::GetServerConfigurableFlag("test", "enabled-rw", "false") == "true"
|
||||
}
|
||||
"#;
|
||||
assert_eq!(expected.trim(), String::from_utf8(generated.contents).unwrap().trim());
|
||||
}
|
||||
}
|
|
@ -26,6 +26,7 @@ use crate::aconfig::{FlagDeclarations, FlagValue};
|
|||
use crate::cache::{Cache, CacheBuilder};
|
||||
use crate::codegen_cpp::generate_cpp_code;
|
||||
use crate::codegen_java::generate_java_code;
|
||||
use crate::codegen_rust::generate_rust_code;
|
||||
use crate::protos::ProtoParsedFlags;
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone, Debug)]
|
||||
|
@ -100,6 +101,10 @@ pub fn create_cpp_lib(cache: &Cache) -> Result<OutputFile> {
|
|||
generate_cpp_code(cache)
|
||||
}
|
||||
|
||||
pub fn create_rust_lib(cache: &Cache) -> Result<OutputFile> {
|
||||
generate_rust_code(cache)
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, PartialEq, Eq, ValueEnum)]
|
||||
pub enum DumpFormat {
|
||||
Text,
|
||||
|
@ -125,7 +130,7 @@ pub fn dump_cache(mut caches: Vec<Cache>, format: DumpFormat) -> Result<Vec<u8>>
|
|||
DumpFormat::Debug => {
|
||||
let mut lines = vec![];
|
||||
for item in cache.iter() {
|
||||
lines.push(format!("{:?}\n", item));
|
||||
lines.push(format!("{:#?}\n", item));
|
||||
}
|
||||
output.append(&mut lines.concat().into());
|
||||
}
|
||||
|
|
|
@ -28,6 +28,7 @@ mod aconfig;
|
|||
mod cache;
|
||||
mod codegen_cpp;
|
||||
mod codegen_java;
|
||||
mod codegen_rust;
|
||||
mod commands;
|
||||
mod protos;
|
||||
|
||||
|
@ -54,6 +55,11 @@ fn cli() -> Command {
|
|||
.arg(Arg::new("cache").long("cache").required(true))
|
||||
.arg(Arg::new("out").long("out").required(true)),
|
||||
)
|
||||
.subcommand(
|
||||
Command::new("create-rust-lib")
|
||||
.arg(Arg::new("cache").long("cache").required(true))
|
||||
.arg(Arg::new("out").long("out").required(true)),
|
||||
)
|
||||
.subcommand(
|
||||
Command::new("dump")
|
||||
.arg(Arg::new("cache").long("cache").action(ArgAction::Append).required(true))
|
||||
|
@ -129,6 +135,14 @@ fn main() -> Result<()> {
|
|||
let generated_file = commands::create_cpp_lib(&cache)?;
|
||||
write_output_file_realtive_to_dir(&dir, &generated_file)?;
|
||||
}
|
||||
Some(("create-rust-lib", sub_matches)) => {
|
||||
let path = get_required_arg::<String>(sub_matches, "cache")?;
|
||||
let file = fs::File::open(path)?;
|
||||
let cache = Cache::read_from_reader(file)?;
|
||||
let dir = PathBuf::from(get_required_arg::<String>(sub_matches, "out")?);
|
||||
let generated_file = commands::create_rust_lib(&cache)?;
|
||||
write_output_file_realtive_to_dir(&dir, &generated_file)?;
|
||||
}
|
||||
Some(("dump", sub_matches)) => {
|
||||
let mut caches = Vec::new();
|
||||
for path in sub_matches.get_many::<String>("cache").unwrap_or_default() {
|
||||
|
|
23
tools/aconfig/templates/rust.template
Normal file
23
tools/aconfig/templates/rust.template
Normal file
|
@ -0,0 +1,23 @@
|
|||
{{- for parsed_flag in parsed_flags -}}
|
||||
{{- if parsed_flag.is_read_only_disabled -}}
|
||||
#[inline(always)]
|
||||
pub const fn r#{parsed_flag.fn_name}() -> bool \{
|
||||
false
|
||||
}
|
||||
|
||||
{{ endif -}}
|
||||
{{- if parsed_flag.is_read_only_enabled -}}
|
||||
#[inline(always)]
|
||||
pub const fn r#{parsed_flag.fn_name}() -> bool \{
|
||||
true
|
||||
}
|
||||
|
||||
{{ endif -}}
|
||||
{{- if parsed_flag.is_read_write -}}
|
||||
#[inline(always)]
|
||||
pub fn r#{parsed_flag.fn_name}() -> bool \{
|
||||
profcollect_libflags_rust::GetServerConfigurableFlag("{namespace}", "{parsed_flag.name}", "false") == "true"
|
||||
}
|
||||
|
||||
{{ endif -}}
|
||||
{{- endfor -}}
|
18
tools/aconfig/testdata/first.values
vendored
Normal file
18
tools/aconfig/testdata/first.values
vendored
Normal file
|
@ -0,0 +1,18 @@
|
|||
flag_value {
|
||||
namespace: "test"
|
||||
name: "disabled-ro"
|
||||
state: DISABLED
|
||||
permission: READ_ONLY
|
||||
}
|
||||
flag_value {
|
||||
namespace: "test"
|
||||
name: "enabled-ro"
|
||||
state: DISABLED
|
||||
permission: READ_WRITE
|
||||
}
|
||||
flag_value {
|
||||
namespace: "test"
|
||||
name: "enabled-rw"
|
||||
state: ENABLED
|
||||
permission: READ_WRITE
|
||||
}
|
6
tools/aconfig/testdata/second.values
vendored
Normal file
6
tools/aconfig/testdata/second.values
vendored
Normal file
|
@ -0,0 +1,6 @@
|
|||
flag_value {
|
||||
namespace: "test"
|
||||
name: "enabled-ro"
|
||||
state: ENABLED
|
||||
permission: READ_ONLY
|
||||
}
|
33
tools/aconfig/testdata/test.aconfig
vendored
Normal file
33
tools/aconfig/testdata/test.aconfig
vendored
Normal file
|
@ -0,0 +1,33 @@
|
|||
namespace: "test"
|
||||
|
||||
# This flag's final value is calculated from:
|
||||
# - test.aconfig: DISABLED + READ_WRITE (default)
|
||||
# - first.values: DISABLED + READ_ONLY
|
||||
flag {
|
||||
name: "disabled-ro"
|
||||
description: "This flag is DISABLED + READ_ONLY"
|
||||
}
|
||||
|
||||
# This flag's final value is calculated from:
|
||||
# - test.aconfig: DISABLED + READ_WRITE (default)
|
||||
flag {
|
||||
name: "disabled-rw"
|
||||
description: "This flag is DISABLED + READ_WRITE"
|
||||
}
|
||||
|
||||
# This flag's final value is calculated from:
|
||||
# - test.aconfig: DISABLED + READ_WRITE (default)
|
||||
# - first.values: DISABLED + READ_WRITE
|
||||
# - second.values: ENABLED + READ_ONLY
|
||||
flag {
|
||||
name: "enabled-ro"
|
||||
description: "This flag is ENABLED + READ_ONLY"
|
||||
}
|
||||
|
||||
# This flag's final value is calculated from:
|
||||
# - test.aconfig: DISABLED + READ_WRITE (default)
|
||||
# - first.values: ENABLED + READ_WRITE
|
||||
flag {
|
||||
name: "enabled-rw"
|
||||
description: "This flag is ENABLED + READ_WRITE"
|
||||
}
|
Loading…
Reference in a new issue