Commit graph

53 commits

Author SHA1 Message Date
Mårten Kongstad
a2e5ab82c7 aconfig: make proto fields optional
Change all required proto fields to optional. While the proto file is
supposed to be a backwards compatible API, and fields are not supposed
to be deprecated, this commit will allow for that option if needed.

Implementation wise this change doesn't matter much: any parsed data
needs additional verification outside what the protobuf crate's parser
provides anyway, so adding checks to verify that all required fields,
even though marked optional in the proto file, were found is a minor
increase in code complexity.

If in the future a proto field should no longer be used:

  - keep the field in the proto, still marked optional and clearly
    document that it is no longer in use
  - change protos.rs from checking struct.has_field() to explicitly
    dropping any value via struct.clear_field()

Bug: 286337317
Test: atest aconfig.test
Change-Id: Iad1ccfe50ecac286ff7a796aec909bec70b9520d
2023-06-19 16:53:22 +02:00
Mårten Kongstad
a2e152a139 aconfig: generate Java flag name constants
Generate Java constants for use in @FlaggedApi(flag = ...).

Also update the generated Java code to use the constants when reaching
out to DeviceConfig instead of hard-coding (duplicate) strings.

Bug: 285288440
Test: atest aconfig.test aconfig.test.java
Change-Id: I1127cacba650cc7a7896b1533e03631d7f5ec71b
2023-06-19 16:12:01 +02:00
Mårten Kongstad
403658f9cb aconfig: use proto struct directly
Remove the hand-crafted wrappers around the structures auto-generated
from protos/aconfig.proto, and use the auto-generated structs directly
intead. This gets rid of a lot of manual repetition, and its inherent
risk.

Also unify how individual fields read from text proto are verified (e.g.
is the flag.name field a valid identifier).

Also change the intermediate cache format from JSON to binary protobuf.

The concept of a 'cache' as an intermediate internal format to represent
parsed input stays. The command line interface still refers to caches.
At the moment a cache file is identical to a parsed_file protbuf, and
the code exploits this internally.

A couple of points regarding the auto-generated structs:

  - Vectors are named in the singular (e.g. parsed_flags.parsed_flag is
    a Vec<ProtoParsedFlag>) because this improves ergonomics for all
    devs working with aconfig input files

  - The auto-generated structs have fields that are of type Option<T>
    and convenience methods (named the same as the fields) to access T

Test: atest aconfig.test aconfig.test.java
Bug: 283910447
Change-Id: I512820cc4bc6c543dea9f6a4356f863120a10be3
2023-06-19 16:04:32 +02:00
Treehugger Robot
a99ac90eb5 Merge changes from topic "aconfig-prepare-for-proto-structs"
* changes:
  aconfig: fix incorrect source path in test cache
  aconfig: reduce number of #[cfg(feature = "cargo")] uses
2023-06-19 10:43:34 +00:00
Zhi Dou
af81e20653 aconfig: change java flag method name to camelCase
Before java code will directly use the flag name as the method name.
This change adds funciton to try the best to convert flag name to
camelCase, and then use the camelCase string as the method name in the
generated code.

Bug: 279483816
Test: atest aconfig.test aconfig.test.java
Change-Id: I45fc6df46c9d535cd38a657a41313202f9b660af
2023-06-16 12:40:40 +00:00
Mårten Kongstad
0cd8092376 aconfig: fix incorrect source path in test cache
Bug: 283910447
Test: atest aconfig.test
Change-Id: I9bc34e838a0945891f866e9788bcf9f4f6c23d3b
2023-06-15 11:43:33 +02:00
Mårten Kongstad
f94225266f aconfig: reduce number of #[cfg(feature = "cargo")] uses
Group statements with identical #[cfg(feature = "cargo")] attributes in
the same block. This reduces repetition and makes the code easier to
read and less error prone.

Bug: 284779868
Test: atest aconfig.test
Change-Id: Iebdcd20e7cd22cb641424a1af594f5c9ac57b623
2023-06-15 09:28:49 +02:00
Zhi Dou
06377d79ab Merge "aconfig: Java codegen iteration 1" 2023-06-14 13:21:16 +00:00
Zhi Dou
4655c967e1 aconfig: Java codegen iteration 1
This change includes
    - refactor generated java code to generate
        - Flags.java to support the static API
	- FeatureFlagsImpl.java to support injection API
	- FeatureFlags.java interface

Bug: 279483816
Test: atest aconfig.test aconfig.test.java
Change-Id: If0d4baf317b9174635cd0fff3832ab7091ee52ed
2023-06-13 14:45:53 +00:00
Mårten Kongstad
d18c978f31 aconfig: reject consecutive underscores in identifiers
The Java codegen may translate flag names (snake_case) to Java
camelCase, dropping the underscores. The flags a_b and a__b will
translate to the same camelCase form, which is ambiguous.

Circumvent this problem by disallowing consecutive underscores in flag
names, flag namespaces, and packages.

Bug: 284252015
Test: atest aconfig.test
Change-Id: I2586a38160723c06265a140193da8178655553e4
2023-06-13 13:30:58 +02:00
Dennis Shen
f1cd83bece Merge "aconfig: update cpp codegen to use static methods" 2023-06-12 12:57:57 +00:00
Dennis Shen
4f78f10015 aconfig: update cpp codegen to use static methods
Update c++ codegen to static methods interface.

Bug: b/279483801
Test: atest aconfig.test
Change-Id: I78da3bbca6240bee660c692807930d00f2242b0a
2023-06-12 12:57:38 +00:00
Joe Onorato
8b51859317 Fix build breakage
error: build/make/tools/aconfig/Android.bp:69:1: module "aconfig.test.java" variant "android_common": path dependency ":aconfig.test.flags{.srcjar}": unsupported device_config_definitions module reference tag ".srcjar"

Test: m aconfig.test.java
Change-Id: I3df980b06796c90dafee3940fe52809748305950
2023-06-09 13:39:41 -07:00
Mårten Kongstad
9c59c31499 aconfig: add Java integration tests
Add integration tests for Java. This test setup verifies that

  - the build system calls aconfig to generate a Java library
  - the Java test compiles against the auto-generated library
  - the auto-generated code returns expected values

Similar integration tests for C++ and Rust will be added in follow-up
CLs.

Note: the build does not currently support specifying that
tests/*.values should be applied, so the test flags will all be assigned
the defaults. A later CL will fix this.

Bug: b/283911467
Test: atest aconfig.test aconfig.test.java
Change-Id: Ia365e209261f4935a23e2dac9ef0ab5b60f76e52
2023-06-09 09:59:21 +02:00
Mårten Kongstad
202102f7be aconfig: include namespace in create-device-config-defaults
Update the output format of create-device-config-defaults to include the
flag's namespace. Also change the delimiters. The new format is

  <namespace>:<package>.<flag-name>=[enabled|disabled|

Bug: 285468565
Test: atest aconfig.test
Change-Id: I9b4ca1611cca8528dc341fc12812b614c86f6c08
2023-06-08 11:28:09 +02:00
Mårten Kongstad
b025507857 aconfig: improve code diffs in tests
Implement a helper function to make it easier for unit tests to diff
(and find the first difference) generated code and expected code.

Bug: 283910447
Test: atest aconfig.test
Change-Id: I460e8fbf05e8f33e8a62ecef67b2d9d77051e876
2023-06-08 11:27:59 +02:00
Mårten Kongstad
066575b95f aconfig: add namespace field to flag_declaration and parsed_flag
Add a new field to the proto messages flag_declaration and parsed_flag.

The new field will be used verbatim as a parameter when calling
DeviceConfig.getBoolean to read the value of a READ_WRITE flag. See the
DeviceConfig API for more info.

Note: not to be confused with the old namespace field, which has been
renamed to package.

Bug: 285211724
Test: atest aconfig.test
Change-Id: I2181be7b5e98fc334e5277fb5f7e386f1fe0b550
2023-06-08 11:27:57 +02:00
Mårten Kongstad
fbd71e2773 aconfig: allow dots in package fields
Allow package fields to include dots.

Update the generated code based on the package name: if the package name
is com.android.example:

  - java: package com.android.example; ...
  - C++: namespace com::android::example { ... }
  - Rust: mod com { mod android { mod example { ... } } }

Also, update examples to use dots in the package fields.

Also, remove unnecessary #include from the auto-generated C++ code: the
header should not include itself.

Bug: 285000854
Test: atest aconfig.test
Change-Id: I8a5352e25c64c34dee0725202a1b7c9957819de8
2023-06-08 11:25:43 +02:00
Mårten Kongstad
9fb58965af aconfig: rename namespace -> package
What used to be referred to as a namespace is now called a package.

This CL is a semantic change only.

Bug: 285000854
Test: m nothing
Test: atest aconfig.test
Change-Id: If3fca67c415af75b44f316e16666b97089407069
2023-06-07 14:53:04 +02:00
Treehugger Robot
2bb714ed2b Merge changes from topic "aconfig-gen-device-config-files"
* changes:
  aconfig: add create-device-config-sysprops command
  aconfig: add create-device-config-defaults command
  aconfig: add test utilities
  aconfig: cache.rs: remove unnecessary use statements
  aconfig: give commands ownership of all arguments
2023-06-07 09:50:23 +00:00
Mårten Kongstad
c31a6ff653 aconfig: add create-device-config-sysprops command
Add a new "create-device-config-sysprops" command that works like
"create-device-config-defaults" but for system properties.

DeviceConfig is a Java service, and will mirror (some of) its values by
setting system properties in the persist.device_config namespace. Native
code will access DeviceConfig (actually, the system properties) via the
server_configurable_flags library.

The new command writes a file that can be appended to /system/build.prop
to pre-populate persist.device_config before DeviceConfig has started.

Like create-device-config-defaults, the new command skips READ_ONLY
flags.

Bug: 285468565
Test: atest aconfig.test
Change-Id: I311c7c5e0b03dc897b09204137d43cc182324717
2023-06-02 16:47:04 +02:00
Mårten Kongstad
f02734e915 aconfig: add create-device-config-defaults command
DeviceConfig is the backend for READ_WRITE flags.

Add a new command "create-device-config-defaults" to create a file that
DeviceConfig will read to pre-populate its data store on first init.

This will be used to quickly switch flag values during CI tests:
rebuilding and reflashing a device would have the same effect, but would
be costlier. This feature is not expected to be used outside CI tests.

Note: because DeviceConfig only works with READ_WRITE flags, the
generated file excludes READ_ONLY flags.

Bug: 285468565
Test: atest aconfig.test
Change-Id: I4caff1a10647b8da0ce4e3615678993a957a92dd
2023-06-02 16:46:28 +02:00
Mårten Kongstad
83a8760bbc aconfig: add test utilities
Create a test utility function to create a Cache from the files in
testdata/*. A follow-up CL will update the unit tests to use this
instead of creating their own caches.

Bug: 283910447
Test: atest aconfig.test
Change-Id: Ice5064eadb0babde5eb38d292330d213ab136d96
2023-06-02 12:54:22 +02:00
Mårten Kongstad
1cd166cd31 aconfig: cache.rs: remove unnecessary use statements
Remove unnecessary use from the cache::test module: they already covered
by `use super:*;`.

Bug: 283910447
Test: atest aconfig.test
Change-Id: I9e03385629f38180c0f92080c7f097a8e0d9ef69
2023-06-02 12:52:48 +02:00
Mårten Kongstad
b27f2ce436 aconfig: give commands ownership of all arguments
Pass the Cache argument to command::create_<lang>_lib functions by value
instead of by reference, to align with other commands.

The intended ownership flow is as follows:

  - main creates objects based on command line arguments
  - main hands commands ownership of the objects
  - command processes the objects
  - command gives main ownership of any generated output
  - main writes the output to file

Rationale: commands.rs is a unit testable version of main, and to the
rest of aconfig, acts as the top level entry point; main.rs exists only
to parse command line arguments and perform I/O.

Bug: 283910447
Test: atest aconfig.test
Change-Id: I1e1dea7da8ecc2bb6e2f7ee4a6df64562c148959
2023-06-02 12:49:26 +02:00
Dennis Shen
6461673140 aconfig: update rust code gen to use libflags_rust
Bug: b/284096062, b/279483360
Test: atest aconfig.test
Change-Id: I4c96f35807de5cb2a745ba1c653513d9b22a5013
2023-05-31 14:35:59 +00:00
Mårten Kongstad
00cf045c85 aconfig: restrict valid namespace and flag names
The namespace and flag names will be used as identifiers in the
auto-generated code. Place restrictions on what constitutes a valid
name.

Valid identifiers are those that match /[a-z][a-z0-9_]/. aconfig
explicitly does not implement any automatic translation to make names
valid identifiers: this sidesteps potential conflicts such as "foo.bar"
and "foo_bar" mapping to the same name if dots were translated to
underscores.

Bug: b/284252015
Test: atest aconfig.test
Change-Id: I38d005a74311e5829e540063404d1565071e6e96
2023-05-26 17:09:10 +02:00
Mårten Kongstad
f73b963283 aconfig: first iteration of Rust codegen
Add a new `create-rust-lib` command to generate Rust code. The output is
a src/lib.rs file; the build system is assumed to set the generated
crate's name.

For READ_ONLY flags, the generated code returns a hard-coded true or false.

For READ_WRITE flags, the generated code reaches out to DeviceConfig via
the cc_library server_configurable_flags via the
libprofcollect_libflags_rust Rust bindings. The build system is assumed
to add this to the generated crate's dependencies.

Note: libprofcollect_libflags_rust seems generic enough that it should
be moved to an official Rust wrapper for server_configurable_flags. This
is tracked in b/284096062.

Summary of module the built system is assumed to wrap the auto-generated
code in:

  rust_library {
      name: "lib<namespace>_rs",
      crate_name: "<namespace>_rs",
      edition: "2021",
      clippy_lints: "none",
      no_stdlibs: true,
      lints: "none",
      srcs: ["src/lib.rs"],
      rustlibs: [
          "libprofcollect_libflags_rust",
      ],
  }

Also add a set of test input to be used in the unit tests for a more
coherent test strategy. A follow-up CL will migrate the code in
commands.rs, codegen_java.rs and codegen_cpp.rs.

Bug: 279483360
Bug: 283907905
Test: atest aconfig.test
Test: manual: create cache from files in testdata, create rust lib, add to module template above, verify the module builds
Change-Id: I02606aa3686eda921116e33f7e2df8fd1156a7aa
2023-05-25 16:18:59 +02:00
Mårten Kongstad
993111f91c aconfig: improve dump --format=debug output
Use Rust's {:#?} formatter for more readable output.

Bug: 279485059
Test: atest aconfig.test
Change-Id: I127f413e3d7aebfba96cad1dd58d9e261dd613a4
2023-05-24 14:56:45 +02:00
Mårten Kongstad
2f95444281 aconfig: sort items in cache by name
Introduce a builder pattern for constructing a cache from flag
declarations and flag values. Teach the builder to sort the flags by
name as the last step. This will ensure consistent dump output
regardless of the order flags are specified in the input files.

Bug: 279485059
Test: atest aconfig.test
Change-Id: Icdd62f51fa3761a469663f17581a83d9909e9ffe
2023-05-22 10:01:05 +02:00
Mårten Kongstad
af677038b3 aconfig: dump: support multiple caches
Teach `aconfig dump` to read multiple caches at the same time.

A cache represents a single namespace, and the Android build creates
multiple caches for different namespaces. By passing in all those cache
files to `aconfig dump`, aconfig will create a complete overview of all
flags in the Android tree.

Caches are traversed in order of namespace to produce the same output
regardless of the order the cache files are given on the command line.
If two caches use the same namespace, their order with respect to each
other is undefined.

Bug: 279485059
Test: atest aconfig.test
Change-Id: I54c3950bbb7b2be7d96c8928e78ae83aa626c2e2
2023-05-22 09:57:48 +02:00
Treehugger Robot
4266df817d Merge "aconfig: Add first iteration of cpp codegen to aconfig" 2023-05-17 19:37:28 +00:00
Dennis Shen
1dc9ad4662 aconfig: Add first iteration of cpp codegen to aconfig
The general idea to reuse java codegen's very neat tiny template idea.
For generated cpp code, it is in the form of a collection of classes
inside a namespace. The reason we choose a collection of classes rather
than a collection of static functions is because gmock test technology
only supports mocking virtual method.

Bug: b/279483801
Test: atest aconfig.test
Change-Id: I9ba00667437ff7c3e147ff2828171fc95528bebf
2023-05-17 14:57:58 +00:00
Joe Onorato
0c4ef0f88c Use the namepace as the java package name in aconfig and call the class "Flags"
Test: aconfig.test
Change-Id: I15032262aad2c80d1ec1bd8fa81664a846b307ec
2023-05-16 14:27:25 -07:00
Mårten Kongstad
e17ba5f423 aconfig: cache: reject empty namespace and name fields
Add invariant to struct Cache: all flag namespace and name fields added
to the cache are required to be non-empty strings.

Bug: 279485059
Test: atest aconfig.test
Change-Id: I5ff34ec8feccc19e52241d4221fc87699518f3ff
2023-05-16 12:52:43 +02:00
Mårten Kongstad
6b9e382eed aconfig: remove calls to unwrap (outside tests)
Do not call unwrap outside tests: replace existing uses with Result
return values or infallible alternatives.

Bug: 279485059
Test: atest aconfig.test
Change-Id: Ie5919b704b23a0f96bbef84ffbe9270d667cecd8
2023-05-16 11:19:58 +02:00
Mårten Kongstad
ba94e6a6b2 aconfig: rename enum Format -> enum DumpFormat
Rename enum Format to enum DumpFormat to make it more apparent what it
refers to.

Bug: 279485059
Test: atest aconfig.test
Change-Id: I869be020b69618b036fa05247f155d9e35ff85e2
2023-05-16 11:00:16 +02:00
Mårten Kongstad
e66b89f635 aconfig: use bail! and ensure! where applicable
The Android Rust style guide recommends anyhow's bail! and ensure!
macros where possible. Update the code accordingly.

Bug: 279485059
Test: atest aconfig.test
Change-Id: I7db7cc2d8ec15e1d3450657ad000e879adb19e08
2023-05-15 11:10:12 +02:00
Mårten Kongstad
d42eeeba3d aconfig: follow Java conventions for Java file paths
Update codegen_java to write the generated Java file(s) to
"java/package/File.java" instead of just "File.java".

Also generalize codegen_java::GeneratedFile to commands::OutputFile in
preparation for the upcoming C++ and Rust codegen.

Also change Java package name to 'com.android.internal.aconfig'.

Bug: 279485059
Test: atest aconfig.test
Change-Id: I13978697e35010fe6be8637aa495d4b852dbed7e
2023-05-12 10:01:00 +02:00
Mårten Kongstad
fa23d2993b aconfig: separate flag declarations and flag values
Simplify how aconfig configurations work: remove the ability to set flag
values based on build-id.

The aconfig files now some in two flavours:

  - flag declaration files: introduce new flags; aconfig will assign the
    flags a hard-coded default value (disabled, read-write)

  - flag value files: assign flags new values

`aconfig create-cache` expects flags to be declared exactly once, and
for their values to be reassigned zero or more times.

The flag value files are identical what used to be called override
files.

Also, remove the now obsolete build-id parameter: this was used to
calculate default values before applying overrides, and is no longer
needed.

Also rename a few more structs and functions to be closer to the .proto
names. This will make it easier to use the generated proto structs
directly, and get rid of the hand-crafter wrappers.

Bug: 279485059
Test: atest aconfig.test
Change-Id: I7bf881338b0567f932099ce419cac457abbe8df8
2023-05-12 08:50:49 +02:00
Zhi Dou
eb74489b3a aconfig: Add codegen for java
Add codegen for java skeleton

Bug: 279485059
Test: atest aconfig.test
Change-Id: Ia0481cec9c2e137e88e9a77d1b82412529b64adc
2023-05-11 03:08:53 +00:00
Mårten Kongstad
3095078981 aconfig: introduce namespace, rename proto messages
Flags belong to a namespace. Update the proto files to reflect this.

Config files can only refer to a single namespace. Override files can
refer to multiple namespaces; an override directive for a flag in a
different namespace than the one represented by the cache will be
silently ignored.

Rename the proto messages to make it more clear what they are. Propagate
this change through the wrappers in aconfig.rs and the rest of the code.

Also, settle on Tracepoint instead of TracePoint.

Bug: 279485059
Test: atest aconfig.test
Change-Id: I16e69dd14687bc498b2ba89d6a35879459903801
2023-05-10 17:17:20 +02:00
Mårten Kongstad
a102909e09 aconfig: add dump protobuf format
Introduce a new protobuf format to represent the all flags parsed by
aconfig. This data in this new format is similar to that of the internal
cache object, but the protobuf is a public API for other tools to
consume and any changes to the proto spec must be backwards compatible.

When aconfig has matured more, Cache can potentially be rewritten to
work with proto structs directly, removing some of the hand-written
wrapper structs and the logic to convert to and from the proto structs.
At this point, the intermediate json format can be replaced by the
protobuf dump.

Also, teach `aconfig dump` to accept an --out <file> argument (default:
stdout).

Also, teach `aconfig dump` to read more than once cache file.

Note: the new protobuf fields refer to existing fields. It would make
sense to split the .proto file in one for input and one for output
formats, and import the common messages, but the Android build system
and cargo will need different import paths. Keep the definitions in the
same file to circumvent this problem.

Bug: 279485059
Test: atest aconfig.test
Change-Id: I55ee4a52c0fb3369d91d61406867ae03a15805c3
2023-05-09 15:27:42 +02:00
Mårten Kongstad
98b0eeb2fc aconfig: simplify argument parsing in main
Improve readability by extracting how multiple input files are
transformed from a clap argument to an Input.

Bug: 279485059
Test: atest aconfig.test
Change-Id: I299179d39eca80e107342f6978984da79ee81e34
2023-05-08 12:47:34 +02:00
Mårten Kongstad
76adff2af0 aconfig: improve flag value tracing
Improve how the flag values (state, permission) are tracked when parsing
config and override files: migrate from a raw string to a proper struct,
as this will make it easier when aconfig will output this data in some
structured format for other tools to consume.

Also, rename Cache.debug to Cache.trace.

Bug: 279485059
Test: atest aconfig.test
Change-Id: Idad57c969515f697e9065429d8a44c38d8a512d2
2023-05-08 12:47:34 +02:00
Mårten Kongstad
c68c4eabea aconfig: change flag values to enabled/disabled enum
Change the underlying type of a flag's value from bool to an explicit
enum (Disabled, Enabled): this will hopefully reduce future confusion on
how flags are intended to be used.

Bug: 279485059
Test: atest aconfig.test
Change-Id: I9535f9b23baf93ad5916ca06fb7d21277b4573eb
2023-05-05 16:20:09 +02:00
Mårten Kongstad
416330b060 aconfig: add read/write permission
Introduce the concept of flag read/write permissions: a read-only flag
can only have its value set during the build; a writable flag can by
updated in runtime.

Bug: 279485059
Test: atest aconfig.test
Change-Id: I3ec5c9571faa54de5666120ccd60090d3db9e331
2023-05-05 13:45:28 +02:00
Mårten Kongstad
2937566c55 aconfig: Source: remove unnecessary #[derive(Serialize, Deserialize)]
The Source struct is no longer serialized/deserialized. Remove the
unused derives.

Bug: 279485059
Test: atest aconfig.test
Change-Id: Ifd78988ed8134ab43013314b4437e428a8927981
2023-05-05 10:57:19 +02:00
Mårten Kongstad
09c28d1689 aconfig: add support for changing flag value based on build
Teach aconfig about build IDs (continuously increasing integers). Extend
the aconfig file format to allow flags to say "by default, my value is
X, but starting from build ID A, it's Y, and from build ID B, it's Z".

Bug: 279485059
Test: atest aconfig.test
Change-Id: Idde03dee06f6cb9041c0dd4ca917c8b2f2faafdd
2023-05-04 14:34:55 +02:00
Mårten Kongstad
4d2b4b047b aconfig: introduce cache
Introduce the Cache struct to represent parsed and verified aconfig and
override content. Most commands in aconfig will work of an existing
cache file, eliminating the need to re-read the input every time.

Restructure main.rs to use clap to create a proper command line
interface with support for sub-commands. main.rs is responsible for
parsing the command line, performing disk I/O and calling the correct
subcommand implementation (in commands.rs).

To simplify unit tests, subcommands never perform explicit I/O; instead
they only work with Read and Write traits.

Also add dependencies on clap, serde and serde_json.

Bug: 279485059
Test: atest aconfig.test
Change-Id: Ib6abf2eabd264009804f253874b6fba924fc391b
2023-05-04 10:40:26 +02:00