blob: 7520f5875b1cf204ee9643916e94ab0f61a84be3 [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"
Alixbbfd5382022-06-09 18:52:05 +000034 "reflect"
Inseob Kim27408bf2021-04-06 21:00:17 +090035 "strings"
Jiyong Park76a42f52021-02-16 06:50:37 +090036
Jaewoong Jung4b79e982020-06-01 10:45:49 -070037 "github.com/google/blueprint/proptools"
38
39 "android/soong/android"
Rupert Shuttleworth378fc1b2021-07-28 08:03:16 -040040 "android/soong/bazel"
Kiyoung Kimae11c232021-07-19 11:38:04 +090041 "android/soong/snapshot"
Jaewoong Jung4b79e982020-06-01 10:45:49 -070042)
43
44var pctx = android.NewPackageContext("android/soong/etc")
Jiyong Parkc678ad32018-04-10 13:07:10 +090045
Jaewoong Jungc3fcdb42019-02-13 05:50:33 -080046// TODO(jungw): Now that it handles more than the ones in etc/, consider renaming this file.
Jiyong Parkc678ad32018-04-10 13:07:10 +090047
48func init() {
Jaewoong Jung4b79e982020-06-01 10:45:49 -070049 pctx.Import("android/soong/android")
Jooyung Han0703fd82020-08-26 22:11:53 +090050 RegisterPrebuiltEtcBuildComponents(android.InitRegistrationContext)
Kiyoung Kimae11c232021-07-19 11:38:04 +090051 snapshot.RegisterSnapshotAction(generatePrebuiltSnapshot)
Jooyung Han0703fd82020-08-26 22:11:53 +090052}
Jaewoong Jung4b79e982020-06-01 10:45:49 -070053
Jooyung Han0703fd82020-08-26 22:11:53 +090054func RegisterPrebuiltEtcBuildComponents(ctx android.RegistrationContext) {
55 ctx.RegisterModuleType("prebuilt_etc", PrebuiltEtcFactory)
56 ctx.RegisterModuleType("prebuilt_etc_host", PrebuiltEtcHostFactory)
Inseob Kim27408bf2021-04-06 21:00:17 +090057 ctx.RegisterModuleType("prebuilt_root", PrebuiltRootFactory)
Liz Kammere9ecddc2022-01-04 17:27:52 -050058 ctx.RegisterModuleType("prebuilt_root_host", PrebuiltRootHostFactory)
Jooyung Han0703fd82020-08-26 22:11:53 +090059 ctx.RegisterModuleType("prebuilt_usr_share", PrebuiltUserShareFactory)
60 ctx.RegisterModuleType("prebuilt_usr_share_host", PrebuiltUserShareHostFactory)
61 ctx.RegisterModuleType("prebuilt_font", PrebuiltFontFactory)
62 ctx.RegisterModuleType("prebuilt_firmware", PrebuiltFirmwareFactory)
63 ctx.RegisterModuleType("prebuilt_dsp", PrebuiltDSPFactory)
Colin Cross83ebf232021-04-09 09:41:23 -070064 ctx.RegisterModuleType("prebuilt_rfsa", PrebuiltRFSAFactory)
Inseob Kim1e27a142021-05-06 11:46:11 +000065
66 ctx.RegisterModuleType("prebuilt_defaults", defaultsFactory)
Rupert Shuttleworth378fc1b2021-07-28 08:03:16 -040067
Jiyong Parkc678ad32018-04-10 13:07:10 +090068}
69
Paul Duffin1172fed2021-03-08 11:28:18 +000070var PrepareForTestWithPrebuiltEtc = android.FixtureRegisterWithContext(RegisterPrebuiltEtcBuildComponents)
71
Jiyong Parkc678ad32018-04-10 13:07:10 +090072type prebuiltEtcProperties struct {
Yo Chiang803c40d2020-11-16 20:32:51 +080073 // Source file of this prebuilt. Can reference a genrule type module with the ":module" syntax.
Colin Cross27b922f2019-03-04 22:35:41 -080074 Src *string `android:"path,arch_variant"`
Jiyong Parkc678ad32018-04-10 13:07:10 +090075
Yo Chiangf0e19fe2020-11-18 15:28:42 +080076 // Optional name for the installed file. If unspecified, name of the module is used as the file
77 // name.
Jiyong Park139a2e62018-10-26 21:49:39 +090078 Filename *string `android:"arch_variant"`
79
Yo Chiangf0e19fe2020-11-18 15:28:42 +080080 // When set to true, and filename property is not set, the name for the installed file
Jiyong Park1a7cf082018-11-13 11:59:12 +090081 // is the same as the file name of the source file.
82 Filename_from_src *bool `android:"arch_variant"`
83
Yifan Hong1b3348d2020-01-21 15:53:22 -080084 // Make this module available when building for ramdisk.
Yifan Hong39143a92020-10-26 12:43:12 -070085 // On device without a dedicated recovery partition, the module is only
86 // available after switching root into
87 // /first_stage_ramdisk. To expose the module before switching root, install
88 // the recovery variant instead.
Yifan Hong1b3348d2020-01-21 15:53:22 -080089 Ramdisk_available *bool
90
Yifan Hong60e0cfb2020-10-21 15:17:56 -070091 // Make this module available when building for vendor ramdisk.
Yifan Hong39143a92020-10-26 12:43:12 -070092 // On device without a dedicated recovery partition, the module is only
93 // available after switching root into
94 // /first_stage_ramdisk. To expose the module before switching root, install
95 // the recovery variant instead.
Yifan Hong60e0cfb2020-10-21 15:17:56 -070096 Vendor_ramdisk_available *bool
97
Inseob Kim08758f02021-04-08 21:13:22 +090098 // Make this module available when building for debug ramdisk.
99 Debug_ramdisk_available *bool
100
Tao Bao0ba5c942018-08-14 22:20:22 -0700101 // Make this module available when building for recovery.
102 Recovery_available *bool
103
Jiyong Parkad9ce042018-10-31 22:49:57 +0900104 // Whether this module is directly installable to one of the partitions. Default: true.
105 Installable *bool
Yo Chiang3d64d492020-05-27 17:56:39 +0800106
107 // Install symlinks to the installed file.
108 Symlinks []string `android:"arch_variant"`
Jiyong Parkc678ad32018-04-10 13:07:10 +0900109}
110
Inseob Kim27408bf2021-04-06 21:00:17 +0900111type prebuiltSubdirProperties struct {
112 // Optional subdirectory under which this file is installed into, cannot be specified with
113 // relative_install_path, prefer relative_install_path.
114 Sub_dir *string `android:"arch_variant"`
115
116 // Optional subdirectory under which this file is installed into, cannot be specified with
117 // sub_dir.
118 Relative_install_path *string `android:"arch_variant"`
119}
120
Jooyung Han39edb6c2019-11-06 16:53:07 +0900121type PrebuiltEtcModule interface {
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700122 android.Module
Yo Chiangf0e19fe2020-11-18 15:28:42 +0800123
124 // Returns the base install directory, such as "etc", "usr/share".
Jooyung Han0703fd82020-08-26 22:11:53 +0900125 BaseDir() string
Yo Chiangf0e19fe2020-11-18 15:28:42 +0800126
127 // Returns the sub install directory relative to BaseDir().
Jooyung Han39edb6c2019-11-06 16:53:07 +0900128 SubDir() string
Yo Chiangf0e19fe2020-11-18 15:28:42 +0800129
130 // Returns an android.OutputPath to the intermeidate file, which is the renamed prebuilt source
131 // file.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700132 OutputFile() android.OutputPath
Jooyung Han39edb6c2019-11-06 16:53:07 +0900133}
134
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900135type PrebuiltEtc struct {
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700136 android.ModuleBase
Inseob Kim1e27a142021-05-06 11:46:11 +0000137 android.DefaultableModuleBase
Rupert Shuttleworth378fc1b2021-07-28 08:03:16 -0400138 android.BazelModuleBase
Jiyong Parkc678ad32018-04-10 13:07:10 +0900139
Kiyoung Kimae11c232021-07-19 11:38:04 +0900140 snapshot.VendorSnapshotModuleInterface
141 snapshot.RecoverySnapshotModuleInterface
142
Inseob Kim27408bf2021-04-06 21:00:17 +0900143 properties prebuiltEtcProperties
144 subdirProperties prebuiltSubdirProperties
Jiyong Parkc678ad32018-04-10 13:07:10 +0900145
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700146 sourceFilePath android.Path
147 outputFilePath android.OutputPath
Jaewoong Jungc3fcdb42019-02-13 05:50:33 -0800148 // The base install location, e.g. "etc" for prebuilt_etc, "usr/share" for prebuilt_usr_share.
Patrice Arruda057a8b12019-06-03 15:29:27 -0700149 installDirBase string
Yo Chiangf0e19fe2020-11-18 15:28:42 +0800150 // The base install location when soc_specific property is set to true, e.g. "firmware" for
151 // prebuilt_firmware.
Patrice Arruda057a8b12019-06-03 15:29:27 -0700152 socInstallDirBase string
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700153 installDirPath android.InstallPath
154 additionalDependencies *android.Paths
Jiyong Parkc678ad32018-04-10 13:07:10 +0900155}
156
Inseob Kim1e27a142021-05-06 11:46:11 +0000157type Defaults struct {
158 android.ModuleBase
159 android.DefaultsModuleBase
160}
161
Yifan Hong1b3348d2020-01-21 15:53:22 -0800162func (p *PrebuiltEtc) inRamdisk() bool {
163 return p.ModuleBase.InRamdisk() || p.ModuleBase.InstallInRamdisk()
164}
165
166func (p *PrebuiltEtc) onlyInRamdisk() bool {
167 return p.ModuleBase.InstallInRamdisk()
168}
169
170func (p *PrebuiltEtc) InstallInRamdisk() bool {
171 return p.inRamdisk()
172}
173
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700174func (p *PrebuiltEtc) inVendorRamdisk() bool {
175 return p.ModuleBase.InVendorRamdisk() || p.ModuleBase.InstallInVendorRamdisk()
176}
177
178func (p *PrebuiltEtc) onlyInVendorRamdisk() bool {
179 return p.ModuleBase.InstallInVendorRamdisk()
180}
181
182func (p *PrebuiltEtc) InstallInVendorRamdisk() bool {
183 return p.inVendorRamdisk()
184}
185
Inseob Kim08758f02021-04-08 21:13:22 +0900186func (p *PrebuiltEtc) inDebugRamdisk() bool {
187 return p.ModuleBase.InDebugRamdisk() || p.ModuleBase.InstallInDebugRamdisk()
188}
189
190func (p *PrebuiltEtc) onlyInDebugRamdisk() bool {
191 return p.ModuleBase.InstallInDebugRamdisk()
192}
193
194func (p *PrebuiltEtc) InstallInDebugRamdisk() bool {
195 return p.inDebugRamdisk()
196}
197
Kiyoung Kimae11c232021-07-19 11:38:04 +0900198func (p *PrebuiltEtc) InRecovery() bool {
Colin Cross7228ecd2019-11-18 16:00:16 -0800199 return p.ModuleBase.InRecovery() || p.ModuleBase.InstallInRecovery()
Tao Bao0ba5c942018-08-14 22:20:22 -0700200}
201
202func (p *PrebuiltEtc) onlyInRecovery() bool {
203 return p.ModuleBase.InstallInRecovery()
204}
205
206func (p *PrebuiltEtc) InstallInRecovery() bool {
Kiyoung Kimae11c232021-07-19 11:38:04 +0900207 return p.InRecovery()
Tao Bao0ba5c942018-08-14 22:20:22 -0700208}
209
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700210var _ android.ImageInterface = (*PrebuiltEtc)(nil)
Colin Cross7228ecd2019-11-18 16:00:16 -0800211
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700212func (p *PrebuiltEtc) ImageMutatorBegin(ctx android.BaseModuleContext) {}
Colin Cross7228ecd2019-11-18 16:00:16 -0800213
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700214func (p *PrebuiltEtc) CoreVariantNeeded(ctx android.BaseModuleContext) bool {
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700215 return !p.ModuleBase.InstallInRecovery() && !p.ModuleBase.InstallInRamdisk() &&
Inseob Kim08758f02021-04-08 21:13:22 +0900216 !p.ModuleBase.InstallInVendorRamdisk() && !p.ModuleBase.InstallInDebugRamdisk()
Yifan Hong1b3348d2020-01-21 15:53:22 -0800217}
218
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700219func (p *PrebuiltEtc) RamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
220 return proptools.Bool(p.properties.Ramdisk_available) || p.ModuleBase.InstallInRamdisk()
Colin Cross7228ecd2019-11-18 16:00:16 -0800221}
222
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700223func (p *PrebuiltEtc) VendorRamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
224 return proptools.Bool(p.properties.Vendor_ramdisk_available) || p.ModuleBase.InstallInVendorRamdisk()
225}
226
Inseob Kim08758f02021-04-08 21:13:22 +0900227func (p *PrebuiltEtc) DebugRamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
228 return proptools.Bool(p.properties.Debug_ramdisk_available) || p.ModuleBase.InstallInDebugRamdisk()
229}
230
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700231func (p *PrebuiltEtc) RecoveryVariantNeeded(ctx android.BaseModuleContext) bool {
232 return proptools.Bool(p.properties.Recovery_available) || p.ModuleBase.InstallInRecovery()
Colin Cross7228ecd2019-11-18 16:00:16 -0800233}
234
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700235func (p *PrebuiltEtc) ExtraImageVariations(ctx android.BaseModuleContext) []string {
Colin Cross7228ecd2019-11-18 16:00:16 -0800236 return nil
237}
238
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700239func (p *PrebuiltEtc) SetImageVariation(ctx android.BaseModuleContext, variation string, module android.Module) {
Colin Cross7228ecd2019-11-18 16:00:16 -0800240}
241
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700242func (p *PrebuiltEtc) SourceFilePath(ctx android.ModuleContext) android.Path {
Yo Chiangf0e19fe2020-11-18 15:28:42 +0800243 return android.PathForModuleSrc(ctx, proptools.String(p.properties.Src))
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900244}
245
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700246func (p *PrebuiltEtc) InstallDirPath() android.InstallPath {
Jooyung Hana0171822019-07-22 15:48:36 +0900247 return p.installDirPath
248}
249
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900250// This allows other derivative modules (e.g. prebuilt_etc_xml) to perform
251// additional steps (like validating the src) before the file is installed.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700252func (p *PrebuiltEtc) SetAdditionalDependencies(paths android.Paths) {
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900253 p.additionalDependencies = &paths
254}
255
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700256func (p *PrebuiltEtc) OutputFile() android.OutputPath {
Jiyong Parkc43e0ac2018-10-04 20:27:15 +0900257 return p.outputFilePath
258}
259
Jiyong Park76a42f52021-02-16 06:50:37 +0900260var _ android.OutputFileProducer = (*PrebuiltEtc)(nil)
261
262func (p *PrebuiltEtc) OutputFiles(tag string) (android.Paths, error) {
263 switch tag {
264 case "":
265 return android.Paths{p.outputFilePath}, nil
266 default:
267 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
268 }
269}
270
Jiyong Parkc43e0ac2018-10-04 20:27:15 +0900271func (p *PrebuiltEtc) SubDir() string {
Inseob Kim27408bf2021-04-06 21:00:17 +0900272 if subDir := proptools.String(p.subdirProperties.Sub_dir); subDir != "" {
Liz Kammer0449a632020-06-26 10:12:36 -0700273 return subDir
274 }
Inseob Kim27408bf2021-04-06 21:00:17 +0900275 return proptools.String(p.subdirProperties.Relative_install_path)
Jiyong Parkc43e0ac2018-10-04 20:27:15 +0900276}
277
Jooyung Han0703fd82020-08-26 22:11:53 +0900278func (p *PrebuiltEtc) BaseDir() string {
Jooyung Han8e5685d2020-09-21 11:02:57 +0900279 return p.installDirBase
Jooyung Han0703fd82020-08-26 22:11:53 +0900280}
281
Jiyong Parkad9ce042018-10-31 22:49:57 +0900282func (p *PrebuiltEtc) Installable() bool {
Yo Chiangf0e19fe2020-11-18 15:28:42 +0800283 return p.properties.Installable == nil || proptools.Bool(p.properties.Installable)
Jiyong Parkad9ce042018-10-31 22:49:57 +0900284}
285
Kiyoung Kimae11c232021-07-19 11:38:04 +0900286func (p *PrebuiltEtc) InVendor() bool {
287 return p.ModuleBase.InstallInVendor()
288}
289
290func (p *PrebuiltEtc) ExcludeFromVendorSnapshot() bool {
291 return false
292}
293
294func (p *PrebuiltEtc) ExcludeFromRecoverySnapshot() bool {
295 return false
296}
297
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700298func (p *PrebuiltEtc) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Yo Chiangf0e19fe2020-11-18 15:28:42 +0800299 if p.properties.Src == nil {
300 ctx.PropertyErrorf("src", "missing prebuilt source file")
301 return
302 }
303 p.sourceFilePath = android.PathForModuleSrc(ctx, proptools.String(p.properties.Src))
Yo Chiang803c40d2020-11-16 20:32:51 +0800304
305 // Determine the output file basename.
306 // If Filename is set, use the name specified by the property.
307 // If Filename_from_src is set, use the source file name.
308 // Otherwise use the module name.
Yo Chiangf0e19fe2020-11-18 15:28:42 +0800309 filename := proptools.String(p.properties.Filename)
310 filenameFromSrc := proptools.Bool(p.properties.Filename_from_src)
311 if filename != "" {
312 if filenameFromSrc {
313 ctx.PropertyErrorf("filename_from_src", "filename is set. filename_from_src can't be true")
314 return
Jiyong Park1a7cf082018-11-13 11:59:12 +0900315 }
Yo Chiangf0e19fe2020-11-18 15:28:42 +0800316 } else if filenameFromSrc {
317 filename = p.sourceFilePath.Base()
318 } else {
319 filename = ctx.ModuleName()
Jiyong Park139a2e62018-10-26 21:49:39 +0900320 }
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700321 p.outputFilePath = android.PathForModuleOut(ctx, filename).OutputPath
Patrice Arruda057a8b12019-06-03 15:29:27 -0700322
Inseob Kim27408bf2021-04-06 21:00:17 +0900323 if strings.Contains(filename, "/") {
324 ctx.PropertyErrorf("filename", "filename cannot contain separator '/'")
325 return
326 }
327
Yo Chiangf0e19fe2020-11-18 15:28:42 +0800328 // Check that `sub_dir` and `relative_install_path` are not set at the same time.
Inseob Kim27408bf2021-04-06 21:00:17 +0900329 if p.subdirProperties.Sub_dir != nil && p.subdirProperties.Relative_install_path != nil {
Liz Kammer0449a632020-06-26 10:12:36 -0700330 ctx.PropertyErrorf("sub_dir", "relative_install_path is set. Cannot set sub_dir")
331 }
332
Yo Chiangf0e19fe2020-11-18 15:28:42 +0800333 // If soc install dir was specified and SOC specific is set, set the installDirPath to the
334 // specified socInstallDirBase.
Jooyung Han8e5685d2020-09-21 11:02:57 +0900335 installBaseDir := p.installDirBase
336 if p.SocSpecific() && p.socInstallDirBase != "" {
337 installBaseDir = p.socInstallDirBase
338 }
339 p.installDirPath = android.PathForModuleInstall(ctx, installBaseDir, p.SubDir())
Jiyong Parkc43e0ac2018-10-04 20:27:15 +0900340
Dan Willemsenb0552672019-01-25 16:04:11 -0800341 // This ensures that outputFilePath has the correct name for others to
342 // use, as the source file may have a different name.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700343 ctx.Build(pctx, android.BuildParams{
344 Rule: android.Cp,
Jiyong Parkc43e0ac2018-10-04 20:27:15 +0900345 Output: p.outputFilePath,
346 Input: p.sourceFilePath,
347 })
Jiyong Parkf9f68052020-09-29 20:15:08 +0900348
Inseob Kim916901e2021-02-17 15:48:53 +0900349 if !p.Installable() {
350 p.SkipInstall()
351 }
352
353 // Call InstallFile even when uninstallable to make the module included in the package
354 installPath := ctx.InstallFile(p.installDirPath, p.outputFilePath.Base(), p.outputFilePath)
355 for _, sl := range p.properties.Symlinks {
356 ctx.InstallSymlink(p.installDirPath, sl, installPath)
Jiyong Parkf9f68052020-09-29 20:15:08 +0900357 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900358}
359
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700360func (p *PrebuiltEtc) AndroidMkEntries() []android.AndroidMkEntries {
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700361 nameSuffix := ""
Yifan Hong1b3348d2020-01-21 15:53:22 -0800362 if p.inRamdisk() && !p.onlyInRamdisk() {
363 nameSuffix = ".ramdisk"
364 }
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700365 if p.inVendorRamdisk() && !p.onlyInVendorRamdisk() {
366 nameSuffix = ".vendor_ramdisk"
367 }
Inseob Kim08758f02021-04-08 21:13:22 +0900368 if p.inDebugRamdisk() && !p.onlyInDebugRamdisk() {
369 nameSuffix = ".debug_ramdisk"
370 }
Kiyoung Kimae11c232021-07-19 11:38:04 +0900371 if p.InRecovery() && !p.onlyInRecovery() {
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700372 nameSuffix = ".recovery"
373 }
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700374 return []android.AndroidMkEntries{android.AndroidMkEntries{
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700375 Class: "ETC",
376 SubName: nameSuffix,
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700377 OutputFile: android.OptionalPathForPath(p.outputFilePath),
378 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -0700379 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700380 entries.SetString("LOCAL_MODULE_TAGS", "optional")
Colin Crossc68db4b2021-11-11 18:59:15 -0800381 entries.SetString("LOCAL_MODULE_PATH", p.installDirPath.String())
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700382 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", p.outputFilePath.Base())
Yo Chiang3d64d492020-05-27 17:56:39 +0800383 if len(p.properties.Symlinks) > 0 {
384 entries.AddStrings("LOCAL_MODULE_SYMLINKS", p.properties.Symlinks...)
385 }
Yo Chiang803c40d2020-11-16 20:32:51 +0800386 entries.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", !p.Installable())
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700387 if p.additionalDependencies != nil {
Yo Chiang803c40d2020-11-16 20:32:51 +0800388 entries.AddStrings("LOCAL_ADDITIONAL_DEPENDENCIES", p.additionalDependencies.Strings()...)
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900389 }
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700390 },
Jiyong Parkc678ad32018-04-10 13:07:10 +0900391 },
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900392 }}
Jiyong Parkc678ad32018-04-10 13:07:10 +0900393}
394
Jooyung Hana0171822019-07-22 15:48:36 +0900395func InitPrebuiltEtcModule(p *PrebuiltEtc, dirBase string) {
396 p.installDirBase = dirBase
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900397 p.AddProperties(&p.properties)
Inseob Kim27408bf2021-04-06 21:00:17 +0900398 p.AddProperties(&p.subdirProperties)
399}
400
401func InitPrebuiltRootModule(p *PrebuiltEtc) {
402 p.installDirBase = "."
403 p.AddProperties(&p.properties)
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900404}
Jiyong Parkc678ad32018-04-10 13:07:10 +0900405
Patrice Arruda9e14b962019-03-11 15:58:50 -0700406// prebuilt_etc is for a prebuilt artifact that is installed in
407// <partition>/etc/<sub_dir> directory.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700408func PrebuiltEtcFactory() android.Module {
Jooyung Hana0171822019-07-22 15:48:36 +0900409 module := &PrebuiltEtc{}
410 InitPrebuiltEtcModule(module, "etc")
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900411 // This module is device-only
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700412 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibFirst)
Inseob Kim1e27a142021-05-06 11:46:11 +0000413 android.InitDefaultableModule(module)
Rupert Shuttleworth378fc1b2021-07-28 08:03:16 -0400414 android.InitBazelModule(module)
Inseob Kim1e27a142021-05-06 11:46:11 +0000415 return module
416}
417
418func defaultsFactory() android.Module {
419 return DefaultsFactory()
420}
421
422func DefaultsFactory(props ...interface{}) android.Module {
423 module := &Defaults{}
424
425 module.AddProperties(props...)
426 module.AddProperties(
427 &prebuiltEtcProperties{},
428 &prebuiltSubdirProperties{},
429 )
430
431 android.InitDefaultsModule(module)
432
Jiyong Parkc678ad32018-04-10 13:07:10 +0900433 return module
434}
Tao Bao0ba5c942018-08-14 22:20:22 -0700435
Patrice Arruda9e14b962019-03-11 15:58:50 -0700436// prebuilt_etc_host is for a host prebuilt artifact that is installed in
437// $(HOST_OUT)/etc/<sub_dir> directory.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700438func PrebuiltEtcHostFactory() android.Module {
Jooyung Hana0171822019-07-22 15:48:36 +0900439 module := &PrebuiltEtc{}
440 InitPrebuiltEtcModule(module, "etc")
Jaewoong Jung24788182019-02-04 14:34:10 -0800441 // This module is host-only
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700442 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommon)
Martin Stjernholmdc6525e2021-10-14 00:42:59 +0100443 android.InitDefaultableModule(module)
Liz Kammera9234422021-12-22 15:32:18 -0500444 android.InitBazelModule(module)
Jaewoong Jung24788182019-02-04 14:34:10 -0800445 return module
446}
447
Inseob Kim27408bf2021-04-06 21:00:17 +0900448// prebuilt_root is for a prebuilt artifact that is installed in
449// <partition>/ directory. Can't have any sub directories.
450func PrebuiltRootFactory() android.Module {
451 module := &PrebuiltEtc{}
452 InitPrebuiltRootModule(module)
453 // This module is device-only
454 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibFirst)
Martin Stjernholmdc6525e2021-10-14 00:42:59 +0100455 android.InitDefaultableModule(module)
Inseob Kim27408bf2021-04-06 21:00:17 +0900456 return module
457}
458
Liz Kammere9ecddc2022-01-04 17:27:52 -0500459// prebuilt_root_host is for a host prebuilt artifact that is installed in $(HOST_OUT)/<sub_dir>
460// directory.
461func PrebuiltRootHostFactory() android.Module {
462 module := &PrebuiltEtc{}
463 InitPrebuiltEtcModule(module, ".")
464 // This module is host-only
465 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommon)
466 android.InitDefaultableModule(module)
467 return module
468}
469
Patrice Arruda9e14b962019-03-11 15:58:50 -0700470// prebuilt_usr_share is for a prebuilt artifact that is installed in
471// <partition>/usr/share/<sub_dir> directory.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700472func PrebuiltUserShareFactory() android.Module {
Jooyung Hana0171822019-07-22 15:48:36 +0900473 module := &PrebuiltEtc{}
474 InitPrebuiltEtcModule(module, "usr/share")
Jaewoong Jungc3fcdb42019-02-13 05:50:33 -0800475 // This module is device-only
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700476 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibFirst)
Martin Stjernholmdc6525e2021-10-14 00:42:59 +0100477 android.InitDefaultableModule(module)
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxc78604e2022-02-28 18:22:59 -0500478 android.InitBazelModule(module)
Jaewoong Jungc3fcdb42019-02-13 05:50:33 -0800479 return module
480}
481
Patrice Arruda9e14b962019-03-11 15:58:50 -0700482// prebuild_usr_share_host is for a host prebuilt artifact that is installed in
483// $(HOST_OUT)/usr/share/<sub_dir> directory.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700484func PrebuiltUserShareHostFactory() android.Module {
Jooyung Hana0171822019-07-22 15:48:36 +0900485 module := &PrebuiltEtc{}
486 InitPrebuiltEtcModule(module, "usr/share")
Patrice Arruda300cef92019-02-22 15:47:57 -0800487 // This module is host-only
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700488 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommon)
Martin Stjernholmdc6525e2021-10-14 00:42:59 +0100489 android.InitDefaultableModule(module)
Patrice Arruda300cef92019-02-22 15:47:57 -0800490 return module
491}
492
Patrice Arruda61583eb2019-05-14 08:20:45 -0700493// prebuilt_font installs a font in <partition>/fonts directory.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700494func PrebuiltFontFactory() android.Module {
Jooyung Hana0171822019-07-22 15:48:36 +0900495 module := &PrebuiltEtc{}
496 InitPrebuiltEtcModule(module, "fonts")
Patrice Arruda61583eb2019-05-14 08:20:45 -0700497 // This module is device-only
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700498 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibFirst)
Martin Stjernholmdc6525e2021-10-14 00:42:59 +0100499 android.InitDefaultableModule(module)
Patrice Arruda61583eb2019-05-14 08:20:45 -0700500 return module
501}
Patrice Arruda057a8b12019-06-03 15:29:27 -0700502
Yo Chiangf0e19fe2020-11-18 15:28:42 +0800503// prebuilt_firmware installs a firmware file to <partition>/etc/firmware directory for system
504// image.
505// If soc_specific property is set to true, the firmware file is installed to the
506// vendor <partition>/firmware directory for vendor image.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700507func PrebuiltFirmwareFactory() android.Module {
Jooyung Hana0171822019-07-22 15:48:36 +0900508 module := &PrebuiltEtc{}
509 module.socInstallDirBase = "firmware"
510 InitPrebuiltEtcModule(module, "etc/firmware")
Patrice Arruda057a8b12019-06-03 15:29:27 -0700511 // This module is device-only
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700512 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibFirst)
Martin Stjernholmdc6525e2021-10-14 00:42:59 +0100513 android.InitDefaultableModule(module)
Patrice Arruda057a8b12019-06-03 15:29:27 -0700514 return module
515}
Patrice Arruda0f688002020-06-08 21:40:25 +0000516
517// prebuilt_dsp installs a DSP related file to <partition>/etc/dsp directory for system image.
Yo Chiangf0e19fe2020-11-18 15:28:42 +0800518// If soc_specific property is set to true, the DSP related file is installed to the
519// vendor <partition>/dsp directory for vendor image.
Patrice Arruda0f688002020-06-08 21:40:25 +0000520func PrebuiltDSPFactory() android.Module {
521 module := &PrebuiltEtc{}
522 module.socInstallDirBase = "dsp"
523 InitPrebuiltEtcModule(module, "etc/dsp")
524 // This module is device-only
525 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibFirst)
Martin Stjernholmdc6525e2021-10-14 00:42:59 +0100526 android.InitDefaultableModule(module)
Patrice Arruda0f688002020-06-08 21:40:25 +0000527 return module
528}
Colin Cross83ebf232021-04-09 09:41:23 -0700529
530// prebuilt_rfsa installs a firmware file that will be available through Qualcomm's RFSA
531// to the <partition>/lib/rfsa directory.
532func PrebuiltRFSAFactory() android.Module {
533 module := &PrebuiltEtc{}
534 // Ideally these would go in /vendor/dsp, but the /vendor/lib/rfsa paths are hardcoded in too
535 // many places outside of the application processor. They could be moved to /vendor/dsp once
536 // that is cleaned up.
537 InitPrebuiltEtcModule(module, "lib/rfsa")
538 // This module is device-only
539 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibFirst)
Martin Stjernholmdc6525e2021-10-14 00:42:59 +0100540 android.InitDefaultableModule(module)
Colin Cross83ebf232021-04-09 09:41:23 -0700541 return module
542}
Kiyoung Kimae11c232021-07-19 11:38:04 +0900543
Kiyoung Kimae11c232021-07-19 11:38:04 +0900544// Copy file into the snapshot
545func copyFile(ctx android.SingletonContext, path android.Path, out string, fake bool) android.OutputPath {
546 if fake {
547 // Create empty file instead for the fake snapshot
548 return snapshot.WriteStringToFileRule(ctx, "", out)
549 } else {
550 return snapshot.CopyFileRule(pctx, ctx, path, out)
551 }
552}
553
554// Check if the module is target of the snapshot
555func isSnapshotAware(ctx android.SingletonContext, m *PrebuiltEtc, image snapshot.SnapshotImage) bool {
556 if !m.Enabled() {
557 return false
558 }
559
560 // Skip if the module is not included in the image
561 if !image.InImage(m)() {
562 return false
563 }
564
565 // When android/prebuilt.go selects between source and prebuilt, it sets
566 // HideFromMake on the other one to avoid duplicate install rules in make.
567 if m.IsHideFromMake() {
568 return false
569 }
570
571 // There are some prebuilt_etc module with multiple definition of same name.
572 // Check if the target would be included from the build
573 if !m.ExportedToMake() {
574 return false
575 }
576
577 // Skip if the module is in the predefined path list to skip
578 if image.IsProprietaryPath(ctx.ModuleDir(m), ctx.DeviceConfig()) {
579 return false
580 }
581
582 // Skip if the module should be excluded
583 if image.ExcludeFromSnapshot(m) || image.ExcludeFromDirectedSnapshot(ctx.DeviceConfig(), m.BaseModuleName()) {
584 return false
585 }
586
587 // Skip from other exceptional cases
588 if m.Target().Os.Class != android.Device {
589 return false
590 }
591 if m.Target().NativeBridge == android.NativeBridgeEnabled {
592 return false
593 }
594
595 return true
596}
597
598func generatePrebuiltSnapshot(s snapshot.SnapshotSingleton, ctx android.SingletonContext, snapshotArchDir string) android.Paths {
599 /*
600 Snapshot zipped artifacts directory structure for etc modules:
601 {SNAPSHOT_ARCH}/
602 arch-{TARGET_ARCH}-{TARGET_ARCH_VARIANT}/
603 etc/
604 (prebuilt etc files)
605 arch-{TARGET_2ND_ARCH}-{TARGET_2ND_ARCH_VARIANT}/
606 etc/
607 (prebuilt etc files)
608 NOTICE_FILES/
609 (notice files)
610 */
611 var snapshotOutputs android.Paths
612 noticeDir := filepath.Join(snapshotArchDir, "NOTICE_FILES")
613 installedNotices := make(map[string]bool)
614
615 ctx.VisitAllModules(func(module android.Module) {
616 m, ok := module.(*PrebuiltEtc)
617 if !ok {
618 return
619 }
620
621 if !isSnapshotAware(ctx, m, s.Image) {
622 return
623 }
624
625 targetArch := "arch-" + m.Target().Arch.ArchType.String()
626
627 snapshotLibOut := filepath.Join(snapshotArchDir, targetArch, "etc", m.BaseModuleName())
628 snapshotOutputs = append(snapshotOutputs, copyFile(ctx, m.OutputFile(), snapshotLibOut, s.Fake))
629
Rob Seymour925aa092021-08-10 20:42:03 +0000630 prop := snapshot.SnapshotJsonFlags{}
Kiyoung Kimae11c232021-07-19 11:38:04 +0900631 propOut := snapshotLibOut + ".json"
632 prop.ModuleName = m.BaseModuleName()
633 if m.subdirProperties.Relative_install_path != nil {
634 prop.RelativeInstallPath = *m.subdirProperties.Relative_install_path
635 }
636
637 if m.properties.Filename != nil {
638 prop.Filename = *m.properties.Filename
639 }
640
641 j, err := json.Marshal(prop)
642 if err != nil {
643 ctx.Errorf("json marshal to %q failed: %#v", propOut, err)
644 return
645 }
646 snapshotOutputs = append(snapshotOutputs, snapshot.WriteStringToFileRule(ctx, string(j), propOut))
647
648 if len(m.EffectiveLicenseFiles()) > 0 {
649 noticeName := ctx.ModuleName(m) + ".txt"
650 noticeOut := filepath.Join(noticeDir, noticeName)
651 // skip already copied notice file
652 if !installedNotices[noticeOut] {
653 installedNotices[noticeOut] = true
654
655 noticeOutPath := android.PathForOutput(ctx, noticeOut)
656 ctx.Build(pctx, android.BuildParams{
657 Rule: android.Cat,
658 Inputs: m.EffectiveLicenseFiles(),
659 Output: noticeOutPath,
660 Description: "combine notices for " + noticeOut,
661 })
662 snapshotOutputs = append(snapshotOutputs, noticeOutPath)
663 }
664 }
665
666 })
667
668 return snapshotOutputs
669}
Rupert Shuttleworth378fc1b2021-07-28 08:03:16 -0400670
671// For Bazel / bp2build
672
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxc78604e2022-02-28 18:22:59 -0500673type bazelPrebuiltFileAttributes struct {
Alix993872a2022-06-15 17:42:14 +0000674 Src bazel.LabelAttribute
675 Filename bazel.LabelAttribute
676 Dir string
677 Installable bazel.BoolAttribute
678 Filename_from_src bazel.BoolAttribute
Rupert Shuttleworth378fc1b2021-07-28 08:03:16 -0400679}
680
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400681// ConvertWithBp2build performs bp2build conversion of PrebuiltEtc
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxc78604e2022-02-28 18:22:59 -0500682// All prebuilt_* modules are PrebuiltEtc, which we treat uniformily as *PrebuiltFile*
683func (module *PrebuiltEtc) ConvertWithBp2build(ctx android.TopDownMutatorContext) {
684 var src bazel.LabelAttribute
Liz Kammerdff00ea2021-10-04 13:44:34 -0400685 for axis, configToProps := range module.GetArchVariantProperties(ctx, &prebuiltEtcProperties{}) {
686 for config, p := range configToProps {
687 props, ok := p.(*prebuiltEtcProperties)
688 if !ok {
689 continue
690 }
691 if props.Src != nil {
Chris Parsons58852a02021-12-09 18:10:18 -0500692 label := android.BazelLabelForModuleSrcSingle(ctx, *props.Src)
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxc78604e2022-02-28 18:22:59 -0500693 src.SetSelectValue(axis, config, label)
Liz Kammerdff00ea2021-10-04 13:44:34 -0400694 }
695 }
Alixbbfd5382022-06-09 18:52:05 +0000696
697 for propName, productConfigProps := range android.ProductVariableProperties(ctx) {
698 for configProp, propVal := range productConfigProps {
699 if propName == "Src" {
700 props, ok := propVal.(*string)
701 if !ok {
702 ctx.PropertyErrorf(" Expected Property to have type string, but was %s\n", reflect.TypeOf(propVal).String())
703 continue
704 }
705 if props != nil {
706 label := android.BazelLabelForModuleSrcSingle(ctx, *props)
707 src.SetSelectValue(configProp.ConfigurationAxis(), configProp.SelectKey(), label)
708 }
709 }
710 }
711 }
Rupert Shuttleworth378fc1b2021-07-28 08:03:16 -0400712 }
713
714 var filename string
Alix993872a2022-06-15 17:42:14 +0000715 var filenameFromSrc bool
716 moduleProps := module.properties
717
718 if moduleProps.Filename != nil && *moduleProps.Filename != "" {
719 filename = *moduleProps.Filename
720 } else if moduleProps.Filename_from_src != nil && *moduleProps.Filename_from_src {
721 if moduleProps.Src != nil {
722 filename = *moduleProps.Src
723 }
724 filenameFromSrc = true
725 } else {
726 filename = ctx.ModuleName()
Rupert Shuttleworth378fc1b2021-07-28 08:03:16 -0400727 }
728
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxc78604e2022-02-28 18:22:59 -0500729 var dir = module.installDirBase
730 // prebuilt_file supports only `etc` or `usr/share`
731 if !(dir == "etc" || dir == "usr/share") {
732 return
733 }
734 if subDir := module.subdirProperties.Sub_dir; subDir != nil {
735 dir = dir + "/" + *subDir
Rupert Shuttleworth378fc1b2021-07-28 08:03:16 -0400736 }
737
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxc78604e2022-02-28 18:22:59 -0500738 var installable bazel.BoolAttribute
739 if install := module.properties.Installable; install != nil {
740 installable.Value = install
Rupert Shuttleworth378fc1b2021-07-28 08:03:16 -0400741 }
742
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxc78604e2022-02-28 18:22:59 -0500743 attrs := &bazelPrebuiltFileAttributes{
744 Src: src,
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxc78604e2022-02-28 18:22:59 -0500745 Dir: dir,
746 Installable: installable,
Rupert Shuttleworth378fc1b2021-07-28 08:03:16 -0400747 }
748
Alix993872a2022-06-15 17:42:14 +0000749 if filename != "" {
750 attrs.Filename = bazel.LabelAttribute{Value: &bazel.Label{Label: filename}}
751 } else if filenameFromSrc {
752 attrs.Filename_from_src = bazel.BoolAttribute{Value: moduleProps.Filename_from_src}
753 }
754
Rupert Shuttleworth378fc1b2021-07-28 08:03:16 -0400755 props := bazel.BazelTargetModuleProperties{
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxb81f77e2022-02-28 17:38:34 -0500756 Rule_class: "prebuilt_file",
757 Bzl_load_location: "//build/bazel/rules:prebuilt_file.bzl",
Rupert Shuttleworth378fc1b2021-07-28 08:03:16 -0400758 }
759
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +0000760 ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: module.Name()}, attrs)
Rupert Shuttleworth378fc1b2021-07-28 08:03:16 -0400761}