diff --git a/tools/aconfig/aconfig_flags/Android.bp b/tools/aconfig/aconfig_flags/Android.bp new file mode 100644 index 0000000000..e327ced26c --- /dev/null +++ b/tools/aconfig/aconfig_flags/Android.bp @@ -0,0 +1,46 @@ +/* + * Copyright (C) 2024 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. + */ + +rust_library { + name: "libaconfig_flags", + crate_name: "aconfig_flags", + srcs: [ + "src/lib.rs", + ], + rustlibs: [ + "libaconfig_flags_rust", + ], + host_supported: true, +} + +aconfig_declarations { + name: "aconfig_flags", + package: "com.android.aconfig.flags", + container: "system", + srcs: ["flags.aconfig"], +} + +rust_aconfig_library { + name: "libaconfig_flags_rust", + crate_name: "aconfig_flags_rust", + aconfig_declarations: "aconfig_flags", + host_supported: true, +} + +cc_aconfig_library { + name: "libaconfig_flags_cc", + aconfig_declarations: "aconfig_flags", +} diff --git a/tools/aconfig/aconfig_flags/Cargo.toml b/tools/aconfig/aconfig_flags/Cargo.toml new file mode 100644 index 0000000000..6eb9f14058 --- /dev/null +++ b/tools/aconfig/aconfig_flags/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "aconfig_flags" +version = "0.1.0" +edition = "2021" + +[features] +default = ["cargo"] +cargo = [] + +[dependencies] \ No newline at end of file diff --git a/tools/aconfig/aconfig_flags/flags.aconfig b/tools/aconfig/aconfig_flags/flags.aconfig new file mode 100644 index 0000000000..db8b1b7904 --- /dev/null +++ b/tools/aconfig/aconfig_flags/flags.aconfig @@ -0,0 +1,9 @@ +package: "com.android.aconfig.flags" +container: "system" + +flag { + name: "enable_only_new_storage" + namespace: "core_experiments_team_internal" + bug: "312235596" + description: "When enabled, aconfig flags are read from the new aconfig storage only." +} diff --git a/tools/aconfig/aconfig_flags/src/lib.rs b/tools/aconfig/aconfig_flags/src/lib.rs new file mode 100644 index 0000000000..a607efb7d4 --- /dev/null +++ b/tools/aconfig/aconfig_flags/src/lib.rs @@ -0,0 +1,47 @@ +/* + * Copyright (C) 2024 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. + */ + +//! `aconfig_flags` is a crate for reading aconfig flags from Rust +// When building with the Android tool-chain +// +// - the flag functions will read from aconfig_flags_inner +// - the feature "cargo" will be disabled +// +// When building with cargo +// +// - the flag functions will all return some trivial value, like true +// - the feature "cargo" will be enabled +// +// This module hides these differences from the rest of aconfig. + +/// Module used when building with the Android tool-chain +#[cfg(not(feature = "cargo"))] +pub mod auto_generated { + /// Returns the value for the enable_only_new_storage flag. + pub fn enable_only_new_storage() -> bool { + aconfig_flags_rust::enable_only_new_storage() + } +} + +/// Module used when building with cargo +#[cfg(feature = "cargo")] +pub mod auto_generated { + /// Returns a placeholder value for the enable_only_new_storage flag. + pub fn enable_only_new_storage() -> bool { + // Used only to enable typechecking and testing with cargo + true + } +} diff --git a/tools/aconfig/aflags/Android.bp b/tools/aconfig/aflags/Android.bp index c48585aed9..2040cc635b 100644 --- a/tools/aconfig/aflags/Android.bp +++ b/tools/aconfig/aflags/Android.bp @@ -10,6 +10,7 @@ rust_defaults { srcs: ["src/main.rs"], rustlibs: [ "libaconfig_device_paths", + "libaconfig_flags", "libaconfig_protos", "libaconfigd_protos", "libaconfig_storage_read_api", @@ -24,6 +25,7 @@ rust_defaults { rust_binary { name: "aflags", + host_supported: true, defaults: ["aflags.defaults"], } diff --git a/tools/aconfig/aflags/Cargo.toml b/tools/aconfig/aflags/Cargo.toml index 7dc343668d..7efce6dc96 100644 --- a/tools/aconfig/aflags/Cargo.toml +++ b/tools/aconfig/aflags/Cargo.toml @@ -15,3 +15,4 @@ aconfig_storage_file = { version = "0.1.0", path = "../aconfig_storage_file" } aconfig_storage_read_api = { version = "0.1.0", path = "../aconfig_storage_read_api" } clap = {version = "4.5.2" } aconfig_device_paths = { version = "0.1.0", path = "../aconfig_device_paths" } +aconfig_flags = { version = "0.1.0", path = "../aconfig_flags" } \ No newline at end of file diff --git a/tools/aconfig/aflags/src/main.rs b/tools/aconfig/aflags/src/main.rs index d8912a946e..a726cc0369 100644 --- a/tools/aconfig/aflags/src/main.rs +++ b/tools/aconfig/aflags/src/main.rs @@ -164,10 +164,6 @@ struct Cli { enum Command { /// List all aconfig flags on this device. List { - /// Read from the new flag storage. - #[clap(long)] - use_new_storage: bool, - /// Optionally filter by container name. #[clap(short = 'c', long = "container")] container: Option, @@ -184,6 +180,9 @@ enum Command { /// . qualified_name: String, }, + + /// Display which flag storage backs aconfig flags. + WhichBacking, } struct PaddingInfo { @@ -282,21 +281,31 @@ fn list(source_type: FlagSourceType, container: Option) -> Result String { + if aconfig_flags::auto_generated::enable_only_new_storage() { + "aconfig_storage".to_string() + } else { + "device_config".to_string() + } +} + fn main() -> Result<()> { ensure!(nix::unistd::Uid::current().is_root(), "must be root"); let cli = Cli::parse(); let output = match cli.command { - Command::List { use_new_storage: true, container } => { - list(FlagSourceType::AconfigStorage, container) - .map_err(|err| anyhow!("storage may not be enabled: {err}")) - .map(Some) - } - Command::List { use_new_storage: false, container } => { - list(FlagSourceType::DeviceConfig, container).map(Some) + Command::List { container } => { + if aconfig_flags::auto_generated::enable_only_new_storage() { + list(FlagSourceType::AconfigStorage, container) + .map_err(|err| anyhow!("storage may not be enabled: {err}")) + .map(Some) + } else { + list(FlagSourceType::DeviceConfig, container).map(Some) + } } Command::Enable { qualified_name } => set_flag(&qualified_name, "true").map(|_| None), Command::Disable { qualified_name } => set_flag(&qualified_name, "false").map(|_| None), + Command::WhichBacking => Ok(Some(display_which_backing())), }; match output { Ok(Some(text)) => println!("{text}"),