aconfig: add new codegen mode force_read_only

The "force_read_only" mode is designed for libraries shared among
multiple containers for the following two scenarios where:
1. The library must be built with read_only flags or
2. Different containers requires different values for the same flags.

In this mode, the library includes only non-exported flags, adhering to
the concept that exported flags should maintain consistent values across
containers. Additionally, regardless of their original permissions, all
flags within this mode will be set to read_only.

Bug: 316357759
Test: atest aconfig.test
Change-Id: I65df39416c96404d84044a46bbcfe1bc8ce9ba8f
This commit is contained in:
Zhi Dou 2023-12-20 21:04:10 +00:00
parent 2e137a9963
commit 1a8281ccc2
3 changed files with 36 additions and 0 deletions

View file

@ -56,6 +56,7 @@ pub fn create_device_config_ident(package: &str, flag_name: &str) -> Result<Stri
#[derive(Copy, Clone, Debug, PartialEq, Eq, ValueEnum)]
pub enum CodegenMode {
Exported,
ForceReadOnly,
Production,
Test,
}
@ -64,6 +65,7 @@ impl std::fmt::Display for CodegenMode {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
CodegenMode::Exported => write!(f, "exported"),
CodegenMode::ForceReadOnly => write!(f, "force-read-only"),
CodegenMode::Production => write!(f, "production"),
CodegenMode::Test => write!(f, "test"),
}

View file

@ -47,6 +47,7 @@ where
CodegenMode::Production => include_str!("../../templates/rust_prod.template"),
CodegenMode::Test => include_str!("../../templates/rust_test.template"),
CodegenMode::Exported => include_str!("../../templates/rust_exported.template"),
CodegenMode::ForceReadOnly => todo!(),
},
)?;
let contents = template.render("rust_code_gen", &context)?;
@ -569,6 +570,7 @@ pub fn enabled_ro_exported() -> bool {
CodegenMode::Production => PROD_EXPECTED,
CodegenMode::Test => TEST_EXPECTED,
CodegenMode::Exported => EXPORTED_EXPECTED,
codegen::CodegenMode::ForceReadOnly => todo!(),
},
&String::from_utf8(generated.contents).unwrap()
)

View file

@ -332,6 +332,11 @@ pub fn modify_parsed_flags_based_on_mode(
parsed_flag
}
fn force_read_only_mode_flag_modifier(mut parsed_flag: ProtoParsedFlag) -> ProtoParsedFlag {
parsed_flag.set_permission(ProtoFlagPermission::READ_ONLY);
parsed_flag
}
let modified_parsed_flags: Vec<_> = match codegen_mode {
CodegenMode::Exported => parsed_flags
.parsed_flag
@ -339,6 +344,12 @@ pub fn modify_parsed_flags_based_on_mode(
.filter(|pf| pf.is_exported())
.map(exported_mode_flag_modifier)
.collect(),
CodegenMode::ForceReadOnly => parsed_flags
.parsed_flag
.into_iter()
.filter(|pf| !pf.is_exported())
.map(force_read_only_mode_flag_modifier)
.collect(),
CodegenMode::Production | CodegenMode::Test => {
parsed_flags.parsed_flag.into_iter().collect()
}
@ -694,4 +705,25 @@ mod tests {
]);
assert_eq!(flag_ids, expected_flag_ids);
}
#[test]
fn test_modify_parsed_flags_based_on_mode_force_read_only() {
let parsed_flags = crate::test::parse_test_flags();
let p_parsed_flags =
modify_parsed_flags_based_on_mode(parsed_flags.clone(), CodegenMode::ForceReadOnly)
.unwrap();
assert_eq!(6, p_parsed_flags.len());
for pf in p_parsed_flags {
assert_eq!(ProtoFlagPermission::READ_ONLY, pf.permission());
}
let mut parsed_flags = crate::test::parse_test_flags();
parsed_flags.parsed_flag.retain_mut(|pf| pf.is_exported());
let error = modify_parsed_flags_based_on_mode(parsed_flags, CodegenMode::ForceReadOnly)
.unwrap_err();
assert_eq!(
"force-read-only library contains no force-read-only flags",
format!("{:?}", error)
);
}
}