blob: 5a4bb90089ddca474b8228df6171e782d094af73 [file] [log] [blame]
Joe Onoratofee845a2023-05-09 08:14:14 -07001// Copyright 2023 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
Joe Onorato981c9262023-06-21 15:16:23 -070015package aconfig
Joe Onoratofee845a2023-05-09 08:14:14 -070016
17import (
18 "android/soong/android"
Vinh Tran457ddef2023-08-02 13:50:26 -040019
Joe Onoratofee845a2023-05-09 08:14:14 -070020 "github.com/google/blueprint"
21)
22
Jihoon Kang2a43e562024-02-12 19:05:12 +000023type CodegenInfo struct {
24 // AconfigDeclarations is the name of the aconfig_declarations modules that
25 // the codegen module is associated with
26 AconfigDeclarations []string
27
28 // Paths to the cache files of the associated aconfig_declaration modules
29 IntermediateCacheOutputPaths android.Paths
30
31 // Paths to the srcjar files generated from the java_aconfig_library modules
32 Srcjars android.Paths
33}
34
35var CodegenInfoProvider = blueprint.NewProvider[CodegenInfo]()
36
Joe Onoratofee845a2023-05-09 08:14:14 -070037var (
Joe Onorato981c9262023-06-21 15:16:23 -070038 pctx = android.NewPackageContext("android/soong/aconfig")
Joe Onoratofee845a2023-05-09 08:14:14 -070039
Joe Onorato981c9262023-06-21 15:16:23 -070040 // For aconfig_declarations: Generate cache file
Joe Onoratofee845a2023-05-09 08:14:14 -070041 aconfigRule = pctx.AndroidStaticRule("aconfig",
42 blueprint.RuleParams{
43 Command: `${aconfig} create-cache` +
Joe Onorato81b25ed2023-06-21 13:49:37 -070044 ` --package ${package}` +
Zhi Dou8a35a6f2023-07-19 18:04:24 +000045 ` ${declarations}` +
Joe Onoratofee845a2023-05-09 08:14:14 -070046 ` ${values}` +
Zhi Dou3f65a412023-08-10 21:47:40 +000047 ` ${default-permission}` +
Joe Onoratofee845a2023-05-09 08:14:14 -070048 ` --cache ${out}.tmp` +
Zhi Dou9dc24512023-07-24 21:19:55 +000049 ` && ( if cmp -s ${out}.tmp ${out} ; then rm ${out}.tmp ; else mv ${out}.tmp ${out} ; fi )`,
Joe Onoratofee845a2023-05-09 08:14:14 -070050 // ` --build-id ${release_version}` +
51 CommandDeps: []string{
52 "${aconfig}",
53 },
54 Restat: true,
Zhi Dou3f65a412023-08-10 21:47:40 +000055 }, "release_version", "package", "declarations", "values", "default-permission")
Joe Onoratofee845a2023-05-09 08:14:14 -070056
Jihoon Kangcca3e0c2023-11-29 19:35:29 +000057 // For create-device-config-sysprops: Generate aconfig flag value map text file
58 aconfigTextRule = pctx.AndroidStaticRule("aconfig_text",
59 blueprint.RuleParams{
Yu Liu748ade22024-02-08 19:11:39 +000060 Command: `${aconfig} dump-cache --dedup --format='{fully_qualified_name}={state:bool}'` +
Jihoon Kangcca3e0c2023-11-29 19:35:29 +000061 ` --cache ${in}` +
62 ` --out ${out}.tmp` +
63 ` && ( if cmp -s ${out}.tmp ${out} ; then rm ${out}.tmp ; else mv ${out}.tmp ${out} ; fi )`,
64 CommandDeps: []string{
65 "${aconfig}",
66 },
67 Restat: true,
68 })
69
Mårten Kongstadfacb8d62023-11-20 14:03:29 +010070 // For all_aconfig_declarations: Combine all parsed_flags proto files
Yu Liueae7b362023-11-16 17:05:47 -080071 AllDeclarationsRule = pctx.AndroidStaticRule("All_aconfig_declarations_dump",
Joe Onorato2f99c472023-06-21 18:10:28 -070072 blueprint.RuleParams{
Yu Liu748ade22024-02-08 19:11:39 +000073 Command: `${aconfig} dump-cache --dedup --format protobuf --out ${out} ${cache_files}`,
Joe Onorato2f99c472023-06-21 18:10:28 -070074 CommandDeps: []string{
75 "${aconfig}",
76 },
77 }, "cache_files")
Mårten Kongstad2a1adcc2024-02-20 12:43:21 +010078 AllDeclarationsRuleTextProto = pctx.AndroidStaticRule("All_aconfig_declarations_dump_textproto",
79 blueprint.RuleParams{
80 Command: `${aconfig} dump-cache --dedup --format textproto --out ${out} ${cache_files}`,
81 CommandDeps: []string{
82 "${aconfig}",
83 },
84 }, "cache_files")
Colin Crossd788b3e2023-11-28 13:14:56 -080085
Mårten Kongstadc82f1182023-11-20 15:04:30 +010086 // For exported_java_aconfig_library: Generate a JAR from all
87 // java_aconfig_libraries to be consumed by apps built outside the
88 // platform
89 exportedJavaRule = pctx.AndroidStaticRule("exported_java_aconfig_library",
Mårten Kongstad1258acf2023-12-18 13:47:57 +010090 // For each aconfig cache file, if the cache contains any
91 // exported flags, generate Java flag lookup code for the
92 // exported flags (only). Finally collect all generated code
93 // into the ${out} JAR file.
Mårten Kongstadc82f1182023-11-20 15:04:30 +010094 blueprint.RuleParams{
95 Command: `rm -rf ${out}.tmp` +
Mårten Kongstad1258acf2023-12-18 13:47:57 +010096 `&& for cache in ${cache_files}; do ` +
Yu Liu748ade22024-02-08 19:11:39 +000097 ` if [ -n "$$(${aconfig} dump-cache --dedup --cache $$cache --filter=is_exported:true --format='{fully_qualified_name}')" ]; then ` +
Mårten Kongstad1258acf2023-12-18 13:47:57 +010098 ` ${aconfig} create-java-lib --cache $$cache --mode=exported --out ${out}.tmp; ` +
99 ` fi ` +
100 `done` +
Mårten Kongstadc82f1182023-11-20 15:04:30 +0100101 `&& $soong_zip -write_if_changed -jar -o ${out} -C ${out}.tmp -D ${out}.tmp` +
102 `&& rm -rf ${out}.tmp`,
103 CommandDeps: []string{
104 "$aconfig",
105 "$soong_zip",
106 },
107 }, "cache_files")
Joe Onoratofee845a2023-05-09 08:14:14 -0700108)
109
110func init() {
Yu Liu2cc802a2023-09-05 17:19:45 -0700111 RegisterBuildComponents(android.InitRegistrationContext)
Joe Onorato175073c2023-06-01 14:42:59 -0700112 pctx.HostBinToolVariable("aconfig", "aconfig")
113 pctx.HostBinToolVariable("soong_zip", "soong_zip")
Joe Onoratofee845a2023-05-09 08:14:14 -0700114}
115
Yu Liu2cc802a2023-09-05 17:19:45 -0700116func RegisterBuildComponents(ctx android.RegistrationContext) {
Joe Onorato981c9262023-06-21 15:16:23 -0700117 ctx.RegisterModuleType("aconfig_declarations", DeclarationsFactory)
118 ctx.RegisterModuleType("aconfig_values", ValuesFactory)
119 ctx.RegisterModuleType("aconfig_value_set", ValueSetFactory)
Joe Onorato2f99c472023-06-21 18:10:28 -0700120 ctx.RegisterParallelSingletonType("all_aconfig_declarations", AllAconfigDeclarationsFactory)
Mårten Kongstadc82f1182023-11-20 15:04:30 +0100121 ctx.RegisterParallelSingletonType("exported_java_aconfig_library", ExportedJavaDeclarationsLibraryFactory)
Joe Onoratofee845a2023-05-09 08:14:14 -0700122}