blob: 85abf59989ec3914fc2387481d981062f0eb78b4 [file] [log] [blame]
Jiyong Parkc678ad32018-04-10 13:07:10 +09001// Copyright 2016 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
Jaewoong Jung4b79e982020-06-01 10:45:49 -070015package etc
Jiyong Parkc678ad32018-04-10 13:07:10 +090016
Yo Chiang803c40d2020-11-16 20:32:51 +080017// This file implements module types that install prebuilt artifacts.
18//
19// There exist two classes of prebuilt modules in the Android tree. The first class are the ones
20// based on `android.Prebuilt`, such as `cc_prebuilt_library` and `java_import`. This kind of
21// modules may exist both as prebuilts and source at the same time, though only one would be
22// installed and the other would be marked disabled. The `prebuilt_postdeps` mutator would select
23// the actual modules to be installed. More details in android/prebuilt.go.
24//
25// The second class is described in this file. Unlike `android.Prebuilt` based module types,
26// `prebuilt_etc` exist only as prebuilts and cannot have a same-named source module counterpart.
27// This makes the logic of `prebuilt_etc` to be much simpler as they don't need to go through the
28// various `prebuilt_*` mutators.
Jaewoong Jung4b79e982020-06-01 10:45:49 -070029
Yo Chiang803c40d2020-11-16 20:32:51 +080030import (
Kiyoung Kimae11c232021-07-19 11:38:04 +090031 "encoding/json"
Jiyong Park76a42f52021-02-16 06:50:37 +090032 "fmt"
Kiyoung Kimae11c232021-07-19 11:38:04 +090033 "path/filepath"
Inseob Kim27408bf2021-04-06 21:00:17 +090034 "strings"
Jiyong Park76a42f52021-02-16 06:50:37 +090035
Jaewoong Jung4b79e982020-06-01 10:45:49 -070036 "github.com/google/blueprint/proptools"
37
38 "android/soong/android"
Rupert Shuttleworth378fc1b2021-07-28 08:03:16 -040039 "android/soong/bazel"
Kiyoung Kimae11c232021-07-19 11:38:04 +090040 "android/soong/snapshot"
Jaewoong Jung4b79e982020-06-01 10:45:49 -070041)
42
43var pctx = android.NewPackageContext("android/soong/etc")
Jiyong Parkc678ad32018-04-10 13:07:10 +090044
Jaewoong Jungc3fcdb42019-02-13 05:50:33 -080045// TODO(jungw): Now that it handles more than the ones in etc/, consider renaming this file.
Jiyong Parkc678ad32018-04-10 13:07:10 +090046
47func init() {
Jaewoong Jung4b79e982020-06-01 10:45:49 -070048 pctx.Import("android/soong/android")
Jooyung Han0703fd82020-08-26 22:11:53 +090049 RegisterPrebuiltEtcBuildComponents(android.InitRegistrationContext)
Kiyoung Kimae11c232021-07-19 11:38:04 +090050 snapshot.RegisterSnapshotAction(generatePrebuiltSnapshot)
Jooyung Han0703fd82020-08-26 22:11:53 +090051}
Jaewoong Jung4b79e982020-06-01 10:45:49 -070052
Jooyung Han0703fd82020-08-26 22:11:53 +090053func RegisterPrebuiltEtcBuildComponents(ctx android.RegistrationContext) {
54 ctx.RegisterModuleType("prebuilt_etc", PrebuiltEtcFactory)
55 ctx.RegisterModuleType("prebuilt_etc_host", PrebuiltEtcHostFactory)
Inseob Kim27408bf2021-04-06 21:00:17 +090056 ctx.RegisterModuleType("prebuilt_root", PrebuiltRootFactory)
Jooyung Han0703fd82020-08-26 22:11:53 +090057 ctx.RegisterModuleType("prebuilt_usr_share", PrebuiltUserShareFactory)
58 ctx.RegisterModuleType("prebuilt_usr_share_host", PrebuiltUserShareHostFactory)
59 ctx.RegisterModuleType("prebuilt_font", PrebuiltFontFactory)
60 ctx.RegisterModuleType("prebuilt_firmware", PrebuiltFirmwareFactory)
61 ctx.RegisterModuleType("prebuilt_dsp", PrebuiltDSPFactory)
Colin Cross83ebf232021-04-09 09:41:23 -070062 ctx.RegisterModuleType("prebuilt_rfsa", PrebuiltRFSAFactory)
Inseob Kim1e27a142021-05-06 11:46:11 +000063
64 ctx.RegisterModuleType("prebuilt_defaults", defaultsFactory)
Rupert Shuttleworth378fc1b2021-07-28 08:03:16 -040065
66 android.RegisterBp2BuildMutator("prebuilt_etc", PrebuiltEtcBp2Build)
Jiyong Parkc678ad32018-04-10 13:07:10 +090067}
68
Paul Duffin1172fed2021-03-08 11:28:18 +000069var PrepareForTestWithPrebuiltEtc = android.FixtureRegisterWithContext(RegisterPrebuiltEtcBuildComponents)
70
Jiyong Parkc678ad32018-04-10 13:07:10 +090071type prebuiltEtcProperties struct {
Yo Chiang803c40d2020-11-16 20:32:51 +080072 // Source file of this prebuilt. Can reference a genrule type module with the ":module" syntax.
Colin Cross27b922f2019-03-04 22:35:41 -080073 Src *string `android:"path,arch_variant"`
Jiyong Parkc678ad32018-04-10 13:07:10 +090074
Yo Chiangf0e19fe2020-11-18 15:28:42 +080075 // Optional name for the installed file. If unspecified, name of the module is used as the file
76 // name.
Jiyong Park139a2e62018-10-26 21:49:39 +090077 Filename *string `android:"arch_variant"`
78
Yo Chiangf0e19fe2020-11-18 15:28:42 +080079 // When set to true, and filename property is not set, the name for the installed file
Jiyong Park1a7cf082018-11-13 11:59:12 +090080 // is the same as the file name of the source file.
81 Filename_from_src *bool `android:"arch_variant"`
82
Yifan Hong1b3348d2020-01-21 15:53:22 -080083 // Make this module available when building for ramdisk.
Yifan Hong39143a92020-10-26 12:43:12 -070084 // On device without a dedicated recovery partition, the module is only
85 // available after switching root into
86 // /first_stage_ramdisk. To expose the module before switching root, install
87 // the recovery variant instead.
Yifan Hong1b3348d2020-01-21 15:53:22 -080088 Ramdisk_available *bool
89
Yifan Hong60e0cfb2020-10-21 15:17:56 -070090 // Make this module available when building for vendor ramdisk.
Yifan Hong39143a92020-10-26 12:43:12 -070091 // On device without a dedicated recovery partition, the module is only
92 // available after switching root into
93 // /first_stage_ramdisk. To expose the module before switching root, install
94 // the recovery variant instead.
Yifan Hong60e0cfb2020-10-21 15:17:56 -070095 Vendor_ramdisk_available *bool
96
Inseob Kim08758f02021-04-08 21:13:22 +090097 // Make this module available when building for debug ramdisk.
98 Debug_ramdisk_available *bool
99
Tao Bao0ba5c942018-08-14 22:20:22 -0700100 // Make this module available when building for recovery.
101 Recovery_available *bool
102
Jiyong Parkad9ce042018-10-31 22:49:57 +0900103 // Whether this module is directly installable to one of the partitions. Default: true.
104 Installable *bool
Yo Chiang3d64d492020-05-27 17:56:39 +0800105
106 // Install symlinks to the installed file.
107 Symlinks []string `android:"arch_variant"`
Jiyong Parkc678ad32018-04-10 13:07:10 +0900108}
109
Inseob Kim27408bf2021-04-06 21:00:17 +0900110type prebuiltSubdirProperties struct {
111 // Optional subdirectory under which this file is installed into, cannot be specified with
112 // relative_install_path, prefer relative_install_path.
113 Sub_dir *string `android:"arch_variant"`
114
115 // Optional subdirectory under which this file is installed into, cannot be specified with
116 // sub_dir.
117 Relative_install_path *string `android:"arch_variant"`
118}
119
Jooyung Han39edb6c2019-11-06 16:53:07 +0900120type PrebuiltEtcModule interface {
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700121 android.Module
Yo Chiangf0e19fe2020-11-18 15:28:42 +0800122
123 // Returns the base install directory, such as "etc", "usr/share".
Jooyung Han0703fd82020-08-26 22:11:53 +0900124 BaseDir() string
Yo Chiangf0e19fe2020-11-18 15:28:42 +0800125
126 // Returns the sub install directory relative to BaseDir().
Jooyung Han39edb6c2019-11-06 16:53:07 +0900127 SubDir() string
Yo Chiangf0e19fe2020-11-18 15:28:42 +0800128
129 // Returns an android.OutputPath to the intermeidate file, which is the renamed prebuilt source
130 // file.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700131 OutputFile() android.OutputPath
Jooyung Han39edb6c2019-11-06 16:53:07 +0900132}
133
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900134type PrebuiltEtc struct {
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700135 android.ModuleBase
Inseob Kim1e27a142021-05-06 11:46:11 +0000136 android.DefaultableModuleBase
Rupert Shuttleworth378fc1b2021-07-28 08:03:16 -0400137 android.BazelModuleBase
Jiyong Parkc678ad32018-04-10 13:07:10 +0900138
Kiyoung Kimae11c232021-07-19 11:38:04 +0900139 snapshot.VendorSnapshotModuleInterface
140 snapshot.RecoverySnapshotModuleInterface
141
Inseob Kim27408bf2021-04-06 21:00:17 +0900142 properties prebuiltEtcProperties
143 subdirProperties prebuiltSubdirProperties
Jiyong Parkc678ad32018-04-10 13:07:10 +0900144
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700145 sourceFilePath android.Path
146 outputFilePath android.OutputPath
Jaewoong Jungc3fcdb42019-02-13 05:50:33 -0800147 // The base install location, e.g. "etc" for prebuilt_etc, "usr/share" for prebuilt_usr_share.
Patrice Arruda057a8b12019-06-03 15:29:27 -0700148 installDirBase string
Yo Chiangf0e19fe2020-11-18 15:28:42 +0800149 // The base install location when soc_specific property is set to true, e.g. "firmware" for
150 // prebuilt_firmware.
Patrice Arruda057a8b12019-06-03 15:29:27 -0700151 socInstallDirBase string
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700152 installDirPath android.InstallPath
153 additionalDependencies *android.Paths
Jiyong Parkc678ad32018-04-10 13:07:10 +0900154}
155
Inseob Kim1e27a142021-05-06 11:46:11 +0000156type Defaults struct {
157 android.ModuleBase
158 android.DefaultsModuleBase
159}
160
Yifan Hong1b3348d2020-01-21 15:53:22 -0800161func (p *PrebuiltEtc) inRamdisk() bool {
162 return p.ModuleBase.InRamdisk() || p.ModuleBase.InstallInRamdisk()
163}
164
165func (p *PrebuiltEtc) onlyInRamdisk() bool {
166 return p.ModuleBase.InstallInRamdisk()
167}
168
169func (p *PrebuiltEtc) InstallInRamdisk() bool {
170 return p.inRamdisk()
171}
172
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700173func (p *PrebuiltEtc) inVendorRamdisk() bool {
174 return p.ModuleBase.InVendorRamdisk() || p.ModuleBase.InstallInVendorRamdisk()
175}
176
177func (p *PrebuiltEtc) onlyInVendorRamdisk() bool {
178 return p.ModuleBase.InstallInVendorRamdisk()
179}
180
181func (p *PrebuiltEtc) InstallInVendorRamdisk() bool {
182 return p.inVendorRamdisk()
183}
184
Inseob Kim08758f02021-04-08 21:13:22 +0900185func (p *PrebuiltEtc) inDebugRamdisk() bool {
186 return p.ModuleBase.InDebugRamdisk() || p.ModuleBase.InstallInDebugRamdisk()
187}
188
189func (p *PrebuiltEtc) onlyInDebugRamdisk() bool {
190 return p.ModuleBase.InstallInDebugRamdisk()
191}
192
193func (p *PrebuiltEtc) InstallInDebugRamdisk() bool {
194 return p.inDebugRamdisk()
195}
196
Kiyoung Kimae11c232021-07-19 11:38:04 +0900197func (p *PrebuiltEtc) InRecovery() bool {
Colin Cross7228ecd2019-11-18 16:00:16 -0800198 return p.ModuleBase.InRecovery() || p.ModuleBase.InstallInRecovery()
Tao Bao0ba5c942018-08-14 22:20:22 -0700199}
200
201func (p *PrebuiltEtc) onlyInRecovery() bool {
202 return p.ModuleBase.InstallInRecovery()
203}
204
205func (p *PrebuiltEtc) InstallInRecovery() bool {
Kiyoung Kimae11c232021-07-19 11:38:04 +0900206 return p.InRecovery()
Tao Bao0ba5c942018-08-14 22:20:22 -0700207}
208
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700209var _ android.ImageInterface = (*PrebuiltEtc)(nil)
Colin Cross7228ecd2019-11-18 16:00:16 -0800210
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700211func (p *PrebuiltEtc) ImageMutatorBegin(ctx android.BaseModuleContext) {}
Colin Cross7228ecd2019-11-18 16:00:16 -0800212
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700213func (p *PrebuiltEtc) CoreVariantNeeded(ctx android.BaseModuleContext) bool {
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700214 return !p.ModuleBase.InstallInRecovery() && !p.ModuleBase.InstallInRamdisk() &&
Inseob Kim08758f02021-04-08 21:13:22 +0900215 !p.ModuleBase.InstallInVendorRamdisk() && !p.ModuleBase.InstallInDebugRamdisk()
Yifan Hong1b3348d2020-01-21 15:53:22 -0800216}
217
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700218func (p *PrebuiltEtc) RamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
219 return proptools.Bool(p.properties.Ramdisk_available) || p.ModuleBase.InstallInRamdisk()
Colin Cross7228ecd2019-11-18 16:00:16 -0800220}
221
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700222func (p *PrebuiltEtc) VendorRamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
223 return proptools.Bool(p.properties.Vendor_ramdisk_available) || p.ModuleBase.InstallInVendorRamdisk()
224}
225
Inseob Kim08758f02021-04-08 21:13:22 +0900226func (p *PrebuiltEtc) DebugRamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
227 return proptools.Bool(p.properties.Debug_ramdisk_available) || p.ModuleBase.InstallInDebugRamdisk()
228}
229
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700230func (p *PrebuiltEtc) RecoveryVariantNeeded(ctx android.BaseModuleContext) bool {
231 return proptools.Bool(p.properties.Recovery_available) || p.ModuleBase.InstallInRecovery()
Colin Cross7228ecd2019-11-18 16:00:16 -0800232}
233
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700234func (p *PrebuiltEtc) ExtraImageVariations(ctx android.BaseModuleContext) []string {
Colin Cross7228ecd2019-11-18 16:00:16 -0800235 return nil
236}
237
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700238func (p *PrebuiltEtc) SetImageVariation(ctx android.BaseModuleContext, variation string, module android.Module) {
Colin Cross7228ecd2019-11-18 16:00:16 -0800239}
240
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700241func (p *PrebuiltEtc) SourceFilePath(ctx android.ModuleContext) android.Path {
Yo Chiangf0e19fe2020-11-18 15:28:42 +0800242 return android.PathForModuleSrc(ctx, proptools.String(p.properties.Src))
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900243}
244
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700245func (p *PrebuiltEtc) InstallDirPath() android.InstallPath {
Jooyung Hana0171822019-07-22 15:48:36 +0900246 return p.installDirPath
247}
248
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900249// This allows other derivative modules (e.g. prebuilt_etc_xml) to perform
250// additional steps (like validating the src) before the file is installed.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700251func (p *PrebuiltEtc) SetAdditionalDependencies(paths android.Paths) {
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900252 p.additionalDependencies = &paths
253}
254
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700255func (p *PrebuiltEtc) OutputFile() android.OutputPath {
Jiyong Parkc43e0ac2018-10-04 20:27:15 +0900256 return p.outputFilePath
257}
258
Jiyong Park76a42f52021-02-16 06:50:37 +0900259var _ android.OutputFileProducer = (*PrebuiltEtc)(nil)
260
261func (p *PrebuiltEtc) OutputFiles(tag string) (android.Paths, error) {
262 switch tag {
263 case "":
264 return android.Paths{p.outputFilePath}, nil
265 default:
266 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
267 }
268}
269
Jiyong Parkc43e0ac2018-10-04 20:27:15 +0900270func (p *PrebuiltEtc) SubDir() string {
Inseob Kim27408bf2021-04-06 21:00:17 +0900271 if subDir := proptools.String(p.subdirProperties.Sub_dir); subDir != "" {
Liz Kammer0449a632020-06-26 10:12:36 -0700272 return subDir
273 }
Inseob Kim27408bf2021-04-06 21:00:17 +0900274 return proptools.String(p.subdirProperties.Relative_install_path)
Jiyong Parkc43e0ac2018-10-04 20:27:15 +0900275}
276
Jooyung Han0703fd82020-08-26 22:11:53 +0900277func (p *PrebuiltEtc) BaseDir() string {
Jooyung Han8e5685d2020-09-21 11:02:57 +0900278 return p.installDirBase
Jooyung Han0703fd82020-08-26 22:11:53 +0900279}
280
Jiyong Parkad9ce042018-10-31 22:49:57 +0900281func (p *PrebuiltEtc) Installable() bool {
Yo Chiangf0e19fe2020-11-18 15:28:42 +0800282 return p.properties.Installable == nil || proptools.Bool(p.properties.Installable)
Jiyong Parkad9ce042018-10-31 22:49:57 +0900283}
284
Kiyoung Kimae11c232021-07-19 11:38:04 +0900285func (p *PrebuiltEtc) InVendor() bool {
286 return p.ModuleBase.InstallInVendor()
287}
288
289func (p *PrebuiltEtc) ExcludeFromVendorSnapshot() bool {
290 return false
291}
292
293func (p *PrebuiltEtc) ExcludeFromRecoverySnapshot() bool {
294 return false
295}
296
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700297func (p *PrebuiltEtc) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Yo Chiangf0e19fe2020-11-18 15:28:42 +0800298 if p.properties.Src == nil {
299 ctx.PropertyErrorf("src", "missing prebuilt source file")
300 return
301 }
302 p.sourceFilePath = android.PathForModuleSrc(ctx, proptools.String(p.properties.Src))
Yo Chiang803c40d2020-11-16 20:32:51 +0800303
304 // Determine the output file basename.
305 // If Filename is set, use the name specified by the property.
306 // If Filename_from_src is set, use the source file name.
307 // Otherwise use the module name.
Yo Chiangf0e19fe2020-11-18 15:28:42 +0800308 filename := proptools.String(p.properties.Filename)
309 filenameFromSrc := proptools.Bool(p.properties.Filename_from_src)
310 if filename != "" {
311 if filenameFromSrc {
312 ctx.PropertyErrorf("filename_from_src", "filename is set. filename_from_src can't be true")
313 return
Jiyong Park1a7cf082018-11-13 11:59:12 +0900314 }
Yo Chiangf0e19fe2020-11-18 15:28:42 +0800315 } else if filenameFromSrc {
316 filename = p.sourceFilePath.Base()
317 } else {
318 filename = ctx.ModuleName()
Jiyong Park139a2e62018-10-26 21:49:39 +0900319 }
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700320 p.outputFilePath = android.PathForModuleOut(ctx, filename).OutputPath
Patrice Arruda057a8b12019-06-03 15:29:27 -0700321
Inseob Kim27408bf2021-04-06 21:00:17 +0900322 if strings.Contains(filename, "/") {
323 ctx.PropertyErrorf("filename", "filename cannot contain separator '/'")
324 return
325 }
326
Yo Chiangf0e19fe2020-11-18 15:28:42 +0800327 // Check that `sub_dir` and `relative_install_path` are not set at the same time.
Inseob Kim27408bf2021-04-06 21:00:17 +0900328 if p.subdirProperties.Sub_dir != nil && p.subdirProperties.Relative_install_path != nil {
Liz Kammer0449a632020-06-26 10:12:36 -0700329 ctx.PropertyErrorf("sub_dir", "relative_install_path is set. Cannot set sub_dir")
330 }
331
Yo Chiangf0e19fe2020-11-18 15:28:42 +0800332 // If soc install dir was specified and SOC specific is set, set the installDirPath to the
333 // specified socInstallDirBase.
Jooyung Han8e5685d2020-09-21 11:02:57 +0900334 installBaseDir := p.installDirBase
335 if p.SocSpecific() && p.socInstallDirBase != "" {
336 installBaseDir = p.socInstallDirBase
337 }
338 p.installDirPath = android.PathForModuleInstall(ctx, installBaseDir, p.SubDir())
Jiyong Parkc43e0ac2018-10-04 20:27:15 +0900339
Dan Willemsenb0552672019-01-25 16:04:11 -0800340 // This ensures that outputFilePath has the correct name for others to
341 // use, as the source file may have a different name.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700342 ctx.Build(pctx, android.BuildParams{
343 Rule: android.Cp,
Jiyong Parkc43e0ac2018-10-04 20:27:15 +0900344 Output: p.outputFilePath,
345 Input: p.sourceFilePath,
346 })
Jiyong Parkf9f68052020-09-29 20:15:08 +0900347
Inseob Kim916901e2021-02-17 15:48:53 +0900348 if !p.Installable() {
349 p.SkipInstall()
350 }
351
352 // Call InstallFile even when uninstallable to make the module included in the package
353 installPath := ctx.InstallFile(p.installDirPath, p.outputFilePath.Base(), p.outputFilePath)
354 for _, sl := range p.properties.Symlinks {
355 ctx.InstallSymlink(p.installDirPath, sl, installPath)
Jiyong Parkf9f68052020-09-29 20:15:08 +0900356 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900357}
358
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700359func (p *PrebuiltEtc) AndroidMkEntries() []android.AndroidMkEntries {
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700360 nameSuffix := ""
Yifan Hong1b3348d2020-01-21 15:53:22 -0800361 if p.inRamdisk() && !p.onlyInRamdisk() {
362 nameSuffix = ".ramdisk"
363 }
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700364 if p.inVendorRamdisk() && !p.onlyInVendorRamdisk() {
365 nameSuffix = ".vendor_ramdisk"
366 }
Inseob Kim08758f02021-04-08 21:13:22 +0900367 if p.inDebugRamdisk() && !p.onlyInDebugRamdisk() {
368 nameSuffix = ".debug_ramdisk"
369 }
Kiyoung Kimae11c232021-07-19 11:38:04 +0900370 if p.InRecovery() && !p.onlyInRecovery() {
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700371 nameSuffix = ".recovery"
372 }
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700373 return []android.AndroidMkEntries{android.AndroidMkEntries{
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700374 Class: "ETC",
375 SubName: nameSuffix,
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700376 OutputFile: android.OptionalPathForPath(p.outputFilePath),
377 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -0700378 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700379 entries.SetString("LOCAL_MODULE_TAGS", "optional")
Colin Crossff6c33d2019-10-02 16:01:35 -0700380 entries.SetString("LOCAL_MODULE_PATH", p.installDirPath.ToMakePath().String())
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700381 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", p.outputFilePath.Base())
Yo Chiang3d64d492020-05-27 17:56:39 +0800382 if len(p.properties.Symlinks) > 0 {
383 entries.AddStrings("LOCAL_MODULE_SYMLINKS", p.properties.Symlinks...)
384 }
Yo Chiang803c40d2020-11-16 20:32:51 +0800385 entries.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", !p.Installable())
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700386 if p.additionalDependencies != nil {
Yo Chiang803c40d2020-11-16 20:32:51 +0800387 entries.AddStrings("LOCAL_ADDITIONAL_DEPENDENCIES", p.additionalDependencies.Strings()...)
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900388 }
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700389 },
Jiyong Parkc678ad32018-04-10 13:07:10 +0900390 },
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900391 }}
Jiyong Parkc678ad32018-04-10 13:07:10 +0900392}
393
Jooyung Hana0171822019-07-22 15:48:36 +0900394func InitPrebuiltEtcModule(p *PrebuiltEtc, dirBase string) {
395 p.installDirBase = dirBase
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900396 p.AddProperties(&p.properties)
Inseob Kim27408bf2021-04-06 21:00:17 +0900397 p.AddProperties(&p.subdirProperties)
398}
399
400func InitPrebuiltRootModule(p *PrebuiltEtc) {
401 p.installDirBase = "."
402 p.AddProperties(&p.properties)
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900403}
Jiyong Parkc678ad32018-04-10 13:07:10 +0900404
Patrice Arruda9e14b962019-03-11 15:58:50 -0700405// prebuilt_etc is for a prebuilt artifact that is installed in
406// <partition>/etc/<sub_dir> directory.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700407func PrebuiltEtcFactory() android.Module {
Jooyung Hana0171822019-07-22 15:48:36 +0900408 module := &PrebuiltEtc{}
409 InitPrebuiltEtcModule(module, "etc")
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900410 // This module is device-only
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700411 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibFirst)
Inseob Kim1e27a142021-05-06 11:46:11 +0000412 android.InitDefaultableModule(module)
Rupert Shuttleworth378fc1b2021-07-28 08:03:16 -0400413 android.InitBazelModule(module)
Inseob Kim1e27a142021-05-06 11:46:11 +0000414 return module
415}
416
417func defaultsFactory() android.Module {
418 return DefaultsFactory()
419}
420
421func DefaultsFactory(props ...interface{}) android.Module {
422 module := &Defaults{}
423
424 module.AddProperties(props...)
425 module.AddProperties(
426 &prebuiltEtcProperties{},
427 &prebuiltSubdirProperties{},
428 )
429
430 android.InitDefaultsModule(module)
431
Jiyong Parkc678ad32018-04-10 13:07:10 +0900432 return module
433}
Tao Bao0ba5c942018-08-14 22:20:22 -0700434
Patrice Arruda9e14b962019-03-11 15:58:50 -0700435// prebuilt_etc_host is for a host prebuilt artifact that is installed in
436// $(HOST_OUT)/etc/<sub_dir> directory.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700437func PrebuiltEtcHostFactory() android.Module {
Jooyung Hana0171822019-07-22 15:48:36 +0900438 module := &PrebuiltEtc{}
439 InitPrebuiltEtcModule(module, "etc")
Jaewoong Jung24788182019-02-04 14:34:10 -0800440 // This module is host-only
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700441 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommon)
Jaewoong Jung24788182019-02-04 14:34:10 -0800442 return module
443}
444
Inseob Kim27408bf2021-04-06 21:00:17 +0900445// prebuilt_root is for a prebuilt artifact that is installed in
446// <partition>/ directory. Can't have any sub directories.
447func PrebuiltRootFactory() android.Module {
448 module := &PrebuiltEtc{}
449 InitPrebuiltRootModule(module)
450 // This module is device-only
451 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibFirst)
452 return module
453}
454
Patrice Arruda9e14b962019-03-11 15:58:50 -0700455// prebuilt_usr_share is for a prebuilt artifact that is installed in
456// <partition>/usr/share/<sub_dir> directory.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700457func PrebuiltUserShareFactory() android.Module {
Jooyung Hana0171822019-07-22 15:48:36 +0900458 module := &PrebuiltEtc{}
459 InitPrebuiltEtcModule(module, "usr/share")
Jaewoong Jungc3fcdb42019-02-13 05:50:33 -0800460 // This module is device-only
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700461 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibFirst)
Jaewoong Jungc3fcdb42019-02-13 05:50:33 -0800462 return module
463}
464
Patrice Arruda9e14b962019-03-11 15:58:50 -0700465// prebuild_usr_share_host is for a host prebuilt artifact that is installed in
466// $(HOST_OUT)/usr/share/<sub_dir> directory.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700467func PrebuiltUserShareHostFactory() android.Module {
Jooyung Hana0171822019-07-22 15:48:36 +0900468 module := &PrebuiltEtc{}
469 InitPrebuiltEtcModule(module, "usr/share")
Patrice Arruda300cef92019-02-22 15:47:57 -0800470 // This module is host-only
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700471 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommon)
Patrice Arruda300cef92019-02-22 15:47:57 -0800472 return module
473}
474
Patrice Arruda61583eb2019-05-14 08:20:45 -0700475// prebuilt_font installs a font in <partition>/fonts directory.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700476func PrebuiltFontFactory() android.Module {
Jooyung Hana0171822019-07-22 15:48:36 +0900477 module := &PrebuiltEtc{}
478 InitPrebuiltEtcModule(module, "fonts")
Patrice Arruda61583eb2019-05-14 08:20:45 -0700479 // This module is device-only
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700480 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibFirst)
Patrice Arruda61583eb2019-05-14 08:20:45 -0700481 return module
482}
Patrice Arruda057a8b12019-06-03 15:29:27 -0700483
Yo Chiangf0e19fe2020-11-18 15:28:42 +0800484// prebuilt_firmware installs a firmware file to <partition>/etc/firmware directory for system
485// image.
486// If soc_specific property is set to true, the firmware file is installed to the
487// vendor <partition>/firmware directory for vendor image.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700488func PrebuiltFirmwareFactory() android.Module {
Jooyung Hana0171822019-07-22 15:48:36 +0900489 module := &PrebuiltEtc{}
490 module.socInstallDirBase = "firmware"
491 InitPrebuiltEtcModule(module, "etc/firmware")
Patrice Arruda057a8b12019-06-03 15:29:27 -0700492 // This module is device-only
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700493 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibFirst)
Patrice Arruda057a8b12019-06-03 15:29:27 -0700494 return module
495}
Patrice Arruda0f688002020-06-08 21:40:25 +0000496
497// prebuilt_dsp installs a DSP related file to <partition>/etc/dsp directory for system image.
Yo Chiangf0e19fe2020-11-18 15:28:42 +0800498// If soc_specific property is set to true, the DSP related file is installed to the
499// vendor <partition>/dsp directory for vendor image.
Patrice Arruda0f688002020-06-08 21:40:25 +0000500func PrebuiltDSPFactory() android.Module {
501 module := &PrebuiltEtc{}
502 module.socInstallDirBase = "dsp"
503 InitPrebuiltEtcModule(module, "etc/dsp")
504 // This module is device-only
505 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibFirst)
506 return module
507}
Colin Cross83ebf232021-04-09 09:41:23 -0700508
509// prebuilt_rfsa installs a firmware file that will be available through Qualcomm's RFSA
510// to the <partition>/lib/rfsa directory.
511func PrebuiltRFSAFactory() android.Module {
512 module := &PrebuiltEtc{}
513 // Ideally these would go in /vendor/dsp, but the /vendor/lib/rfsa paths are hardcoded in too
514 // many places outside of the application processor. They could be moved to /vendor/dsp once
515 // that is cleaned up.
516 InitPrebuiltEtcModule(module, "lib/rfsa")
517 // This module is device-only
518 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibFirst)
519 return module
520}
Kiyoung Kimae11c232021-07-19 11:38:04 +0900521
Kiyoung Kimae11c232021-07-19 11:38:04 +0900522// Copy file into the snapshot
523func copyFile(ctx android.SingletonContext, path android.Path, out string, fake bool) android.OutputPath {
524 if fake {
525 // Create empty file instead for the fake snapshot
526 return snapshot.WriteStringToFileRule(ctx, "", out)
527 } else {
528 return snapshot.CopyFileRule(pctx, ctx, path, out)
529 }
530}
531
532// Check if the module is target of the snapshot
533func isSnapshotAware(ctx android.SingletonContext, m *PrebuiltEtc, image snapshot.SnapshotImage) bool {
534 if !m.Enabled() {
535 return false
536 }
537
538 // Skip if the module is not included in the image
539 if !image.InImage(m)() {
540 return false
541 }
542
543 // When android/prebuilt.go selects between source and prebuilt, it sets
544 // HideFromMake on the other one to avoid duplicate install rules in make.
545 if m.IsHideFromMake() {
546 return false
547 }
548
549 // There are some prebuilt_etc module with multiple definition of same name.
550 // Check if the target would be included from the build
551 if !m.ExportedToMake() {
552 return false
553 }
554
555 // Skip if the module is in the predefined path list to skip
556 if image.IsProprietaryPath(ctx.ModuleDir(m), ctx.DeviceConfig()) {
557 return false
558 }
559
560 // Skip if the module should be excluded
561 if image.ExcludeFromSnapshot(m) || image.ExcludeFromDirectedSnapshot(ctx.DeviceConfig(), m.BaseModuleName()) {
562 return false
563 }
564
565 // Skip from other exceptional cases
566 if m.Target().Os.Class != android.Device {
567 return false
568 }
569 if m.Target().NativeBridge == android.NativeBridgeEnabled {
570 return false
571 }
572
573 return true
574}
575
576func generatePrebuiltSnapshot(s snapshot.SnapshotSingleton, ctx android.SingletonContext, snapshotArchDir string) android.Paths {
577 /*
578 Snapshot zipped artifacts directory structure for etc modules:
579 {SNAPSHOT_ARCH}/
580 arch-{TARGET_ARCH}-{TARGET_ARCH_VARIANT}/
581 etc/
582 (prebuilt etc files)
583 arch-{TARGET_2ND_ARCH}-{TARGET_2ND_ARCH_VARIANT}/
584 etc/
585 (prebuilt etc files)
586 NOTICE_FILES/
587 (notice files)
588 */
589 var snapshotOutputs android.Paths
590 noticeDir := filepath.Join(snapshotArchDir, "NOTICE_FILES")
591 installedNotices := make(map[string]bool)
592
593 ctx.VisitAllModules(func(module android.Module) {
594 m, ok := module.(*PrebuiltEtc)
595 if !ok {
596 return
597 }
598
599 if !isSnapshotAware(ctx, m, s.Image) {
600 return
601 }
602
603 targetArch := "arch-" + m.Target().Arch.ArchType.String()
604
605 snapshotLibOut := filepath.Join(snapshotArchDir, targetArch, "etc", m.BaseModuleName())
606 snapshotOutputs = append(snapshotOutputs, copyFile(ctx, m.OutputFile(), snapshotLibOut, s.Fake))
607
Rob Seymour925aa092021-08-10 20:42:03 +0000608 prop := snapshot.SnapshotJsonFlags{}
Kiyoung Kimae11c232021-07-19 11:38:04 +0900609 propOut := snapshotLibOut + ".json"
610 prop.ModuleName = m.BaseModuleName()
611 if m.subdirProperties.Relative_install_path != nil {
612 prop.RelativeInstallPath = *m.subdirProperties.Relative_install_path
613 }
614
615 if m.properties.Filename != nil {
616 prop.Filename = *m.properties.Filename
617 }
618
619 j, err := json.Marshal(prop)
620 if err != nil {
621 ctx.Errorf("json marshal to %q failed: %#v", propOut, err)
622 return
623 }
624 snapshotOutputs = append(snapshotOutputs, snapshot.WriteStringToFileRule(ctx, string(j), propOut))
625
626 if len(m.EffectiveLicenseFiles()) > 0 {
627 noticeName := ctx.ModuleName(m) + ".txt"
628 noticeOut := filepath.Join(noticeDir, noticeName)
629 // skip already copied notice file
630 if !installedNotices[noticeOut] {
631 installedNotices[noticeOut] = true
632
633 noticeOutPath := android.PathForOutput(ctx, noticeOut)
634 ctx.Build(pctx, android.BuildParams{
635 Rule: android.Cat,
636 Inputs: m.EffectiveLicenseFiles(),
637 Output: noticeOutPath,
638 Description: "combine notices for " + noticeOut,
639 })
640 snapshotOutputs = append(snapshotOutputs, noticeOutPath)
641 }
642 }
643
644 })
645
646 return snapshotOutputs
647}
Rupert Shuttleworth378fc1b2021-07-28 08:03:16 -0400648
649// For Bazel / bp2build
650
651type bazelPrebuiltEtcAttributes struct {
652 Src bazel.LabelAttribute
653 Filename string
654 Sub_dir string
655 Installable bazel.BoolAttribute
656}
657
Rupert Shuttleworth378fc1b2021-07-28 08:03:16 -0400658func PrebuiltEtcBp2Build(ctx android.TopDownMutatorContext) {
659 module, ok := ctx.Module().(*PrebuiltEtc)
660 if !ok {
661 // Not an prebuilt_etc
662 return
663 }
664 if !module.ConvertWithBp2build(ctx) {
665 return
666 }
667 if ctx.ModuleType() != "prebuilt_etc" {
668 return
669 }
670
671 prebuiltEtcBp2BuildInternal(ctx, module)
672}
673
674func prebuiltEtcBp2BuildInternal(ctx android.TopDownMutatorContext, module *PrebuiltEtc) {
675 var srcLabelAttribute bazel.LabelAttribute
676 if module.properties.Src != nil {
677 srcLabelAttribute.SetValue(android.BazelLabelForModuleSrcSingle(ctx, *module.properties.Src))
678 }
679
680 var filename string
681 if module.properties.Filename != nil {
682 filename = *module.properties.Filename
683 }
684
685 var subDir string
686 if module.subdirProperties.Sub_dir != nil {
687 subDir = *module.subdirProperties.Sub_dir
688 }
689
690 var installableBoolAttribute bazel.BoolAttribute
691 if module.properties.Installable != nil {
692 installableBoolAttribute.Value = module.properties.Installable
693 }
694
695 attrs := &bazelPrebuiltEtcAttributes{
696 Src: srcLabelAttribute,
697 Filename: filename,
698 Sub_dir: subDir,
699 Installable: installableBoolAttribute,
700 }
701
702 props := bazel.BazelTargetModuleProperties{
703 Rule_class: "prebuilt_etc",
704 Bzl_load_location: "//build/bazel/rules:prebuilt_etc.bzl",
705 }
706
Liz Kammer2ada09a2021-08-11 00:17:36 -0400707 ctx.CreateBazelTargetModule(module.Name(), props, attrs)
Rupert Shuttleworth378fc1b2021-07-28 08:03:16 -0400708}