xref: /aosp_15_r20/build/soong/aconfig/init.go (revision 333d2b3687b3a337dbcca9d65000bca186795e39)
1// 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
15package aconfig
16
17import (
18	"android/soong/android"
19
20	"github.com/google/blueprint"
21)
22
23var (
24	pctx = android.NewPackageContext("android/soong/aconfig")
25
26	// For aconfig_declarations: Generate cache file
27	aconfigRule = pctx.AndroidStaticRule("aconfig",
28		blueprint.RuleParams{
29			Command: `${aconfig} create-cache` +
30				` --package ${package}` +
31				` ${container}` +
32				` ${declarations}` +
33				` ${values}` +
34				` ${default-permission}` +
35				` ${allow-read-write}` +
36				` --cache ${out}.tmp` +
37				` && ( if cmp -s ${out}.tmp ${out} ; then rm ${out}.tmp ; else mv ${out}.tmp ${out} ; fi )`,
38			//				` --build-id ${release_version}` +
39			CommandDeps: []string{
40				"${aconfig}",
41			},
42			Restat: true,
43		}, "release_version", "package", "container", "declarations", "values", "default-permission", "allow-read-write")
44
45	// For create-device-config-sysprops: Generate aconfig flag value map text file
46	aconfigTextRule = pctx.AndroidStaticRule("aconfig_text",
47		blueprint.RuleParams{
48			Command: `${aconfig} dump-cache --dedup --format='{fully_qualified_name}:{permission}={state:bool}'` +
49				` --cache ${in}` +
50				` --out ${out}.tmp` +
51				` && ( if cmp -s ${out}.tmp ${out} ; then rm ${out}.tmp ; else mv ${out}.tmp ${out} ; fi )`,
52			CommandDeps: []string{
53				"${aconfig}",
54			},
55			Restat: true,
56		})
57
58	// For all_aconfig_declarations: Combine all parsed_flags proto files
59	AllDeclarationsRule = pctx.AndroidStaticRule("All_aconfig_declarations_dump",
60		blueprint.RuleParams{
61			Command: `${aconfig} dump-cache --dedup --format protobuf --out ${out} ${cache_files}`,
62			CommandDeps: []string{
63				"${aconfig}",
64			},
65		}, "cache_files")
66	AllDeclarationsRuleTextProto = pctx.AndroidStaticRule("All_aconfig_declarations_dump_textproto",
67		blueprint.RuleParams{
68			Command: `${aconfig} dump-cache --dedup --format textproto --out ${out} ${cache_files}`,
69			CommandDeps: []string{
70				"${aconfig}",
71			},
72		}, "cache_files")
73
74	CreateStorageRule = pctx.AndroidStaticRule("aconfig_create_storage",
75		blueprint.RuleParams{
76			Command: `${aconfig} create-storage --container ${container} --file ${file_type} --out ${out} ${cache_files}`,
77			CommandDeps: []string{
78				"${aconfig}",
79			},
80		}, "container", "file_type", "cache_files")
81
82	// For exported_java_aconfig_library: Generate a JAR from all
83	// java_aconfig_libraries to be consumed by apps built outside the
84	// platform
85	exportedJavaRule = pctx.AndroidStaticRule("exported_java_aconfig_library",
86		// For each aconfig cache file, if the cache contains any
87		// exported flags, generate Java flag lookup code for the
88		// exported flags (only). Finally collect all generated code
89		// into the ${out} JAR file.
90		blueprint.RuleParams{
91			Command: `rm -rf ${out}.tmp` +
92				`&& for cache in ${cache_files}; do ` +
93				`  if [ -n "$$(${aconfig} dump-cache --dedup --cache $$cache --filter=is_exported:true --format='{fully_qualified_name}')" ]; then ` +
94				`    ${aconfig} create-java-lib --cache $$cache --mode=exported --out ${out}.tmp; ` +
95				`  fi ` +
96				`done` +
97				`&& $soong_zip -write_if_changed -jar -o ${out} -C ${out}.tmp -D ${out}.tmp` +
98				`&& rm -rf ${out}.tmp`,
99			CommandDeps: []string{
100				"$aconfig",
101				"$soong_zip",
102			},
103		}, "cache_files")
104)
105
106func init() {
107	RegisterBuildComponents(android.InitRegistrationContext)
108	pctx.HostBinToolVariable("aconfig", "aconfig")
109	pctx.HostBinToolVariable("soong_zip", "soong_zip")
110}
111
112func RegisterBuildComponents(ctx android.RegistrationContext) {
113	ctx.RegisterModuleType("aconfig_declarations", DeclarationsFactory)
114	ctx.RegisterModuleType("aconfig_values", ValuesFactory)
115	ctx.RegisterModuleType("aconfig_value_set", ValueSetFactory)
116	ctx.RegisterParallelSingletonType("all_aconfig_declarations", AllAconfigDeclarationsFactory)
117	ctx.RegisterParallelSingletonType("exported_java_aconfig_library", ExportedJavaDeclarationsLibraryFactory)
118}
119