• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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	RecordFinalizedFlagsRule = pctx.AndroidStaticRule("RecordFinalizedFlagsRule",
74		blueprint.RuleParams{
75			Command: `${record-finalized-flags} ${parsed_flags_file} ${finalized_flags_file} ${api_signature_files} > ${out}`,
76			CommandDeps: []string{
77				"${record-finalized-flags}",
78			},
79		}, "api_signature_files", "finalized_flags_file", "parsed_flags_file")
80	ExportedFlagCheckRule = pctx.AndroidStaticRule("ExportedFlagCheckRule",
81		blueprint.RuleParams{
82			Command: `${exported-flag-check} ${parsed_flags_file} ${finalized_flags_file} ${api_signature_files} > ${out}`,
83			CommandDeps: []string{
84				"${exported-flag-check}",
85			},
86		}, "api_signature_files", "finalized_flags_file", "parsed_flags_file")
87
88	CreateStorageRule = pctx.AndroidStaticRule("aconfig_create_storage",
89		blueprint.RuleParams{
90			Command: `${aconfig} create-storage --container ${container} --file ${file_type} --out ${out} ${cache_files} --version ${version}`,
91			CommandDeps: []string{
92				"${aconfig}",
93			},
94		}, "container", "file_type", "cache_files", "version")
95
96	// For exported_java_aconfig_library: Generate a JAR from all
97	// java_aconfig_libraries to be consumed by apps built outside the
98	// platform
99	exportedJavaRule = pctx.AndroidStaticRule("exported_java_aconfig_library",
100		// For each aconfig cache file, if the cache contains any
101		// exported flags, generate Java flag lookup code for the
102		// exported flags (only). Finally collect all generated code
103		// into the ${out} JAR file.
104		blueprint.RuleParams{
105			// LINT.IfChange
106			Command: `rm -rf ${out}.tmp` +
107				`&& for cache in ${cache_files}; do ` +
108				`  if [ -n "$$(${aconfig} dump-cache --dedup --cache $$cache --filter=is_exported:true --format='{fully_qualified_name}')" ]; then ` +
109				`    ${aconfig} create-java-lib` +
110				`        --cache $$cache` +
111				`        --mode=exported` +
112				`        --allow-instrumentation ${use_new_storage}` +
113				`        --new-exported ${use_new_exported}` +
114				`        --single-exported-file true` +
115				`        --check-api-level ${check_api_level}` +
116				`        --out ${out}.tmp; ` +
117				`  fi ` +
118				`done` +
119				`&& $soong_zip -write_if_changed -jar -o ${out} -C ${out}.tmp -D ${out}.tmp` +
120				`&& rm -rf ${out}.tmp`,
121			// LINT.ThenChange(/aconfig/codegen/init.go)
122			CommandDeps: []string{
123				"$aconfig",
124				"$soong_zip",
125			},
126		}, "cache_files", "use_new_storage", "use_new_exported", "check_api_level")
127)
128
129func init() {
130	RegisterBuildComponents(android.InitRegistrationContext)
131	pctx.HostBinToolVariable("aconfig", "aconfig")
132	pctx.HostBinToolVariable("soong_zip", "soong_zip")
133	pctx.HostBinToolVariable("record-finalized-flags", "record-finalized-flags")
134	pctx.HostBinToolVariable("exported-flag-check", "exported-flag-check")
135}
136
137func RegisterBuildComponents(ctx android.RegistrationContext) {
138	ctx.RegisterModuleType("aconfig_declarations", DeclarationsFactory)
139	ctx.RegisterModuleType("aconfig_values", ValuesFactory)
140	ctx.RegisterModuleType("aconfig_value_set", ValueSetFactory)
141	ctx.RegisterSingletonModuleType("all_aconfig_declarations", AllAconfigDeclarationsFactory)
142	ctx.RegisterParallelSingletonType("exported_java_aconfig_library", ExportedJavaDeclarationsLibraryFactory)
143	ctx.RegisterModuleType("all_aconfig_declarations_extension", AllAconfigDeclarationsExtensionFactory)
144}
145