diff options
Diffstat (limited to 'tools/finalization/finalization-test/build_flags.rs')
| -rw-r--r-- | tools/finalization/finalization-test/build_flags.rs | 96 |
1 files changed, 96 insertions, 0 deletions
diff --git a/tools/finalization/finalization-test/build_flags.rs b/tools/finalization/finalization-test/build_flags.rs new file mode 100644 index 0000000000..816fd40879 --- /dev/null +++ b/tools/finalization/finalization-test/build_flags.rs @@ -0,0 +1,96 @@ +/* + * Copyright (C) 2025 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 all_release_configs_proto::build_flags_out::{ReleaseConfigArtifact, ReleaseConfigsArtifact}; +use protobuf::Message; +use std::collections::HashMap; +use std::fs; + +#[allow(dead_code)] +pub const FLAGS_WE_CARE_ABOUT: [&str; 5] = [ + "RELEASE_PLATFORM_SDK_VERSION", + "RELEASE_PLATFORM_SDK_VERSION_FULL", + "RELEASE_PLATFORM_VERSION", + "RELEASE_PLATFORM_VERSION_CODENAME", + "RELEASE_HIDDEN_API_EXPORTABLE_STUBS", +]; + +// A map of release-config -name -> map of flag-name -> flag-value +// +// Example access: +// +// assert_eq!(BUILD_FLAGS["trunk"]["RELEASE_PLATFORM_SDK_VERSION"], 36) +#[allow(dead_code)] +pub type BuildFlagMap = HashMap<String, HashMap<String, String>>; + +#[allow(dead_code)] +pub struct ReleaseConfigs { + pub flags: BuildFlagMap, + pub next: String, +} + +impl ReleaseConfigs { + #[allow(dead_code)] + pub fn init() -> Self { + let protobuf = + fs::read("all_release_configs.pb").expect("Could not read all_release_configs.pb"); + let all_release_configs = ReleaseConfigsArtifact::parse_from_bytes(&protobuf[..]) + .expect("failed to parse protobuf as ReleaseConfigArtifact"); + + let mut flags = HashMap::new(); + let mut next: Option<String> = None; + + // parse currently active release config + parse_release_config(&mut flags, &all_release_configs.release_config); + + // parse the other release configs + for release_config in all_release_configs.other_release_configs { + parse_release_config(&mut flags, &release_config); + if release_config.other_names.contains(&"next".to_string()) { + assert!(next.is_none(), "next: multiple aliases"); + next = Some(release_config.name().to_string()); + } + } + + ReleaseConfigs { flags, next: next.expect("next: missing alias") } + } +} + +fn parse_release_config(build_flag_map: &mut BuildFlagMap, release_config: &ReleaseConfigArtifact) { + let x: HashMap<String, String> = release_config + .flags + .iter() + .filter(|flag| FLAGS_WE_CARE_ABOUT.contains(&flag.flag_declaration.name())) + .map(|flag| { + // Flag values are expected to be strings or bools, or not set. In this tool, we + // represent all types as strings (for simplicity). + let value = if flag.value.val.is_none() { + // value not set -> "" + String::new() + } else if flag.value.has_string_value() { + // already a string, use as is + flag.value.string_value().to_string() + } else if flag.value.has_bool_value() { + // convert bool to "true" or "false" + format!("{}", flag.value.bool_value()) + } else { + panic!("unexpected protobuf value type: {:?}", flag.value); + }; + (flag.flag_declaration.name().to_string(), value) + }) + .collect(); + build_flag_map.insert(release_config.name().to_string(), x); +} |
