1// Copyright 2020 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 java
16
17// This file contains the module implementations for runtime_resource_overlay and
18// override_runtime_resource_overlay.
19
20import "android/soong/android"
21
22func init() {
23	RegisterRuntimeResourceOverlayBuildComponents(android.InitRegistrationContext)
24}
25
26func RegisterRuntimeResourceOverlayBuildComponents(ctx android.RegistrationContext) {
27	ctx.RegisterModuleType("runtime_resource_overlay", RuntimeResourceOverlayFactory)
28	ctx.RegisterModuleType("override_runtime_resource_overlay", OverrideRuntimeResourceOverlayModuleFactory)
29}
30
31type RuntimeResourceOverlay struct {
32	android.ModuleBase
33	android.DefaultableModuleBase
34	android.OverridableModuleBase
35	aapt
36
37	properties            RuntimeResourceOverlayProperties
38	overridableProperties OverridableRuntimeResourceOverlayProperties
39
40	certificate Certificate
41
42	outputFile android.Path
43	installDir android.InstallPath
44}
45
46type RuntimeResourceOverlayProperties struct {
47	// the name of a certificate in the default certificate directory or an android_app_certificate
48	// module name in the form ":module".
49	Certificate *string
50
51	// Name of the signing certificate lineage file.
52	Lineage *string
53
54	// optional theme name. If specified, the overlay package will be applied
55	// only when the ro.boot.vendor.overlay.theme system property is set to the same value.
56	Theme *string
57
58	// If not blank, set to the version of the sdk to compile against. This
59	// can be either an API version (e.g. "29" for API level 29 AKA Android 10)
60	// or special subsets of the current platform, for example "none", "current",
61	// "core", "system", "test". See build/soong/java/sdk.go for the full and
62	// up-to-date list of possible values.
63	// Defaults to compiling against the current platform.
64	Sdk_version *string
65
66	// if not blank, set the minimum version of the sdk that the compiled artifacts will run against.
67	// Defaults to sdk_version if not set.
68	Min_sdk_version *string
69
70	// list of android_library modules whose resources are extracted and linked against statically
71	Static_libs []string
72
73	// list of android_app modules whose resources are extracted and linked against
74	Resource_libs []string
75
76	// Names of modules to be overridden. Listed modules can only be other overlays
77	// (in Make or Soong).
78	// This does not completely prevent installation of the overridden overlays, but if both
79	// overlays would be installed by default (in PRODUCT_PACKAGES) the other overlay will be removed
80	// from PRODUCT_PACKAGES.
81	Overrides []string
82}
83
84// RuntimeResourceOverlayModule interface is used by the apex package to gather information from
85// a RuntimeResourceOverlay module.
86type RuntimeResourceOverlayModule interface {
87	android.Module
88	OutputFile() android.Path
89	Certificate() Certificate
90	Theme() string
91}
92
93func (r *RuntimeResourceOverlay) DepsMutator(ctx android.BottomUpMutatorContext) {
94	sdkDep := decodeSdkDep(ctx, android.SdkContext(r))
95	if sdkDep.hasFrameworkLibs() {
96		r.aapt.deps(ctx, sdkDep)
97	}
98
99	cert := android.SrcIsModule(String(r.properties.Certificate))
100	if cert != "" {
101		ctx.AddDependency(ctx.Module(), certificateTag, cert)
102	}
103
104	ctx.AddVariationDependencies(nil, staticLibTag, r.properties.Static_libs...)
105	ctx.AddVariationDependencies(nil, libTag, r.properties.Resource_libs...)
106}
107
108func (r *RuntimeResourceOverlay) GenerateAndroidBuildActions(ctx android.ModuleContext) {
109	// Compile and link resources
110	r.aapt.hasNoCode = true
111	// Do not remove resources without default values nor dedupe resource configurations with the same value
112	aaptLinkFlags := []string{"--no-resource-deduping", "--no-resource-removal"}
113	// Allow the override of "package name" and "overlay target package name"
114	manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(ctx.ModuleName())
115	if overridden || r.overridableProperties.Package_name != nil {
116		// The product override variable has a priority over the package_name property.
117		if !overridden {
118			manifestPackageName = *r.overridableProperties.Package_name
119		}
120		aaptLinkFlags = append(aaptLinkFlags, generateAaptRenamePackageFlags(manifestPackageName, false)...)
121	}
122	if r.overridableProperties.Target_package_name != nil {
123		aaptLinkFlags = append(aaptLinkFlags,
124			"--rename-overlay-target-package "+*r.overridableProperties.Target_package_name)
125	}
126	r.aapt.buildActions(ctx, r, nil, aaptLinkFlags...)
127
128	// Sign the built package
129	_, certificates := collectAppDeps(ctx, r, false, false)
130	certificates = processMainCert(r.ModuleBase, String(r.properties.Certificate), certificates, ctx)
131	signed := android.PathForModuleOut(ctx, "signed", r.Name()+".apk")
132	var lineageFile android.Path
133	if lineage := String(r.properties.Lineage); lineage != "" {
134		lineageFile = android.PathForModuleSrc(ctx, lineage)
135	}
136	SignAppPackage(ctx, signed, r.aapt.exportPackage, certificates, nil, lineageFile)
137	r.certificate = certificates[0]
138
139	r.outputFile = signed
140	r.installDir = android.PathForModuleInstall(ctx, "overlay", String(r.properties.Theme))
141	ctx.InstallFile(r.installDir, r.outputFile.Base(), r.outputFile)
142}
143
144func (r *RuntimeResourceOverlay) SdkVersion(ctx android.EarlyModuleContext) android.SdkSpec {
145	return android.SdkSpecFrom(ctx, String(r.properties.Sdk_version))
146}
147
148func (r *RuntimeResourceOverlay) SystemModules() string {
149	return ""
150}
151
152func (r *RuntimeResourceOverlay) MinSdkVersion(ctx android.EarlyModuleContext) android.SdkSpec {
153	if r.properties.Min_sdk_version != nil {
154		return android.SdkSpecFrom(ctx, *r.properties.Min_sdk_version)
155	}
156	return r.SdkVersion(ctx)
157}
158
159func (r *RuntimeResourceOverlay) TargetSdkVersion(ctx android.EarlyModuleContext) android.SdkSpec {
160	return r.SdkVersion(ctx)
161}
162
163func (r *RuntimeResourceOverlay) Certificate() Certificate {
164	return r.certificate
165}
166
167func (r *RuntimeResourceOverlay) OutputFile() android.Path {
168	return r.outputFile
169}
170
171func (r *RuntimeResourceOverlay) Theme() string {
172	return String(r.properties.Theme)
173}
174
175// runtime_resource_overlay generates a resource-only apk file that can overlay application and
176// system resources at run time.
177func RuntimeResourceOverlayFactory() android.Module {
178	module := &RuntimeResourceOverlay{}
179	module.AddProperties(
180		&module.properties,
181		&module.aaptProperties,
182		&module.overridableProperties)
183
184	android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
185	android.InitDefaultableModule(module)
186	android.InitOverridableModule(module, &module.properties.Overrides)
187	return module
188}
189
190// runtime_resource_overlay properties that can be overridden by override_runtime_resource_overlay
191type OverridableRuntimeResourceOverlayProperties struct {
192	// the package name of this app. The package name in the manifest file is used if one was not given.
193	Package_name *string
194
195	// the target package name of this overlay app. The target package name in the manifest file is used if one was not given.
196	Target_package_name *string
197}
198
199type OverrideRuntimeResourceOverlay struct {
200	android.ModuleBase
201	android.OverrideModuleBase
202}
203
204func (i *OverrideRuntimeResourceOverlay) GenerateAndroidBuildActions(_ android.ModuleContext) {
205	// All the overrides happen in the base module.
206	// TODO(jungjw): Check the base module type.
207}
208
209// override_runtime_resource_overlay is used to create a module based on another
210// runtime_resource_overlay module by overriding some of its properties.
211func OverrideRuntimeResourceOverlayModuleFactory() android.Module {
212	m := &OverrideRuntimeResourceOverlay{}
213	m.AddProperties(&OverridableRuntimeResourceOverlayProperties{})
214
215	android.InitAndroidMultiTargetsArchModule(m, android.DeviceSupported, android.MultilibCommon)
216	android.InitOverrideModule(m)
217	return m
218}
219