blob: c2866abfaa205956c1d3be6230f1c250b6ec2a2d [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
Jiyong Parkc678ad32018-04-10 13:07:10 +090066}
67
Paul Duffin1172fed2021-03-08 11:28:18 +000068var PrepareForTestWithPrebuiltEtc = android.FixtureRegisterWithContext(RegisterPrebuiltEtcBuildComponents)
69
Jiyong Parkc678ad32018-04-10 13:07:10 +090070type prebuiltEtcProperties struct {
Yo Chiang803c40d2020-11-16 20:32:51 +080071 // Source file of this prebuilt. Can reference a genrule type module with the ":module" syntax.
Colin Cross27b922f2019-03-04 22:35:41 -080072 Src *string `android:"path,arch_variant"`
Jiyong Parkc678ad32018-04-10 13:07:10 +090073
Yo Chiangf0e19fe2020-11-18 15:28:42 +080074 // Optional name for the installed file. If unspecified, name of the module is used as the file
75 // name.
Jiyong Park139a2e62018-10-26 21:49:39 +090076 Filename *string `android:"arch_variant"`
77
Yo Chiangf0e19fe2020-11-18 15:28:42 +080078 // When set to true, and filename property is not set, the name for the installed file
Jiyong Park1a7cf082018-11-13 11:59:12 +090079 // is the same as the file name of the source file.
80 Filename_from_src *bool `android:"arch_variant"`
81
Yifan Hong1b3348d2020-01-21 15:53:22 -080082 // Make this module available when building for ramdisk.
Yifan Hong39143a92020-10-26 12:43:12 -070083 // On device without a dedicated recovery partition, the module is only
84 // available after switching root into
85 // /first_stage_ramdisk. To expose the module before switching root, install
86 // the recovery variant instead.
Yifan Hong1b3348d2020-01-21 15:53:22 -080087 Ramdisk_available *bool
88
Yifan Hong60e0cfb2020-10-21 15:17:56 -070089 // Make this module available when building for vendor ramdisk.
Yifan Hong39143a92020-10-26 12:43:12 -070090 // On device without a dedicated recovery partition, the module is only
91 // available after switching root into
92 // /first_stage_ramdisk. To expose the module before switching root, install
93 // the recovery variant instead.
Yifan Hong60e0cfb2020-10-21 15:17:56 -070094 Vendor_ramdisk_available *bool
95
Inseob Kim08758f02021-04-08 21:13:22 +090096 // Make this module available when building for debug ramdisk.
97 Debug_ramdisk_available *bool
98
Tao Bao0ba5c942018-08-14 22:20:22 -070099 // Make this module available when building for recovery.
100 Recovery_available *bool
101
Jiyong Parkad9ce042018-10-31 22:49:57 +0900102 // Whether this module is directly installable to one of the partitions. Default: true.
103 Installable *bool
Yo Chiang3d64d492020-05-27 17:56:39 +0800104
105 // Install symlinks to the installed file.
106 Symlinks []string `android:"arch_variant"`
Jiyong Parkc678ad32018-04-10 13:07:10 +0900107}
108
Inseob Kim27408bf2021-04-06 21:00:17 +0900109type prebuiltSubdirProperties struct {
110 // Optional subdirectory under which this file is installed into, cannot be specified with
111 // relative_install_path, prefer relative_install_path.
112 Sub_dir *string `android:"arch_variant"`
113
114 // Optional subdirectory under which this file is installed into, cannot be specified with
115 // sub_dir.
116 Relative_install_path *string `android:"arch_variant"`
117}
118
Jooyung Han39edb6c2019-11-06 16:53:07 +0900119type PrebuiltEtcModule interface {
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700120 android.Module
Yo Chiangf0e19fe2020-11-18 15:28:42 +0800121
122 // Returns the base install directory, such as "etc", "usr/share".
Jooyung Han0703fd82020-08-26 22:11:53 +0900123 BaseDir() string
Yo Chiangf0e19fe2020-11-18 15:28:42 +0800124
125 // Returns the sub install directory relative to BaseDir().
Jooyung Han39edb6c2019-11-06 16:53:07 +0900126 SubDir() string
Yo Chiangf0e19fe2020-11-18 15:28:42 +0800127
128 // Returns an android.OutputPath to the intermeidate file, which is the renamed prebuilt source
129 // file.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700130 OutputFile() android.OutputPath
Jooyung Han39edb6c2019-11-06 16:53:07 +0900131}
132
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900133type PrebuiltEtc struct {
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700134 android.ModuleBase
Inseob Kim1e27a142021-05-06 11:46:11 +0000135 android.DefaultableModuleBase
Rupert Shuttleworth378fc1b2021-07-28 08:03:16 -0400136 android.BazelModuleBase
Jiyong Parkc678ad32018-04-10 13:07:10 +0900137
Kiyoung Kimae11c232021-07-19 11:38:04 +0900138 snapshot.VendorSnapshotModuleInterface
139 snapshot.RecoverySnapshotModuleInterface
140
Inseob Kim27408bf2021-04-06 21:00:17 +0900141 properties prebuiltEtcProperties
142 subdirProperties prebuiltSubdirProperties
Jiyong Parkc678ad32018-04-10 13:07:10 +0900143
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700144 sourceFilePath android.Path
145 outputFilePath android.OutputPath
Jaewoong Jungc3fcdb42019-02-13 05:50:33 -0800146 // The base install location, e.g. "etc" for prebuilt_etc, "usr/share" for prebuilt_usr_share.
Patrice Arruda057a8b12019-06-03 15:29:27 -0700147 installDirBase string
Yo Chiangf0e19fe2020-11-18 15:28:42 +0800148 // The base install location when soc_specific property is set to true, e.g. "firmware" for
149 // prebuilt_firmware.
Patrice Arruda057a8b12019-06-03 15:29:27 -0700150 socInstallDirBase string
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700151 installDirPath android.InstallPath
152 additionalDependencies *android.Paths
Jiyong Parkc678ad32018-04-10 13:07:10 +0900153}
154
Inseob Kim1e27a142021-05-06 11:46:11 +0000155type Defaults struct {
156 android.ModuleBase
157 android.DefaultsModuleBase
158}
159
Yifan Hong1b3348d2020-01-21 15:53:22 -0800160func (p *PrebuiltEtc) inRamdisk() bool {
161 return p.ModuleBase.InRamdisk() || p.ModuleBase.InstallInRamdisk()
162}
163
164func (p *PrebuiltEtc) onlyInRamdisk() bool {
165 return p.ModuleBase.InstallInRamdisk()
166}
167
168func (p *PrebuiltEtc) InstallInRamdisk() bool {
169 return p.inRamdisk()
170}
171
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700172func (p *PrebuiltEtc) inVendorRamdisk() bool {
173 return p.ModuleBase.InVendorRamdisk() || p.ModuleBase.InstallInVendorRamdisk()
174}
175
176func (p *PrebuiltEtc) onlyInVendorRamdisk() bool {
177 return p.ModuleBase.InstallInVendorRamdisk()
178}
179
180func (p *PrebuiltEtc) InstallInVendorRamdisk() bool {
181 return p.inVendorRamdisk()
182}
183
Inseob Kim08758f02021-04-08 21:13:22 +0900184func (p *PrebuiltEtc) inDebugRamdisk() bool {
185 return p.ModuleBase.InDebugRamdisk() || p.ModuleBase.InstallInDebugRamdisk()
186}
187
188func (p *PrebuiltEtc) onlyInDebugRamdisk() bool {
189 return p.ModuleBase.InstallInDebugRamdisk()
190}
191
192func (p *PrebuiltEtc) InstallInDebugRamdisk() bool {
193 return p.inDebugRamdisk()
194}
195
Kiyoung Kimae11c232021-07-19 11:38:04 +0900196func (p *PrebuiltEtc) InRecovery() bool {
Colin Cross7228ecd2019-11-18 16:00:16 -0800197 return p.ModuleBase.InRecovery() || p.ModuleBase.InstallInRecovery()
Tao Bao0ba5c942018-08-14 22:20:22 -0700198}
199
200func (p *PrebuiltEtc) onlyInRecovery() bool {
201 return p.ModuleBase.InstallInRecovery()
202}
203
204func (p *PrebuiltEtc) InstallInRecovery() bool {
Kiyoung Kimae11c232021-07-19 11:38:04 +0900205 return p.InRecovery()
Tao Bao0ba5c942018-08-14 22:20:22 -0700206}
207
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700208var _ android.ImageInterface = (*PrebuiltEtc)(nil)
Colin Cross7228ecd2019-11-18 16:00:16 -0800209
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700210func (p *PrebuiltEtc) ImageMutatorBegin(ctx android.BaseModuleContext) {}
Colin Cross7228ecd2019-11-18 16:00:16 -0800211
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700212func (p *PrebuiltEtc) CoreVariantNeeded(ctx android.BaseModuleContext) bool {
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700213 return !p.ModuleBase.InstallInRecovery() && !p.ModuleBase.InstallInRamdisk() &&
Inseob Kim08758f02021-04-08 21:13:22 +0900214 !p.ModuleBase.InstallInVendorRamdisk() && !p.ModuleBase.InstallInDebugRamdisk()
Yifan Hong1b3348d2020-01-21 15:53:22 -0800215}
216
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700217func (p *PrebuiltEtc) RamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
218 return proptools.Bool(p.properties.Ramdisk_available) || p.ModuleBase.InstallInRamdisk()
Colin Cross7228ecd2019-11-18 16:00:16 -0800219}
220
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700221func (p *PrebuiltEtc) VendorRamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
222 return proptools.Bool(p.properties.Vendor_ramdisk_available) || p.ModuleBase.InstallInVendorRamdisk()
223}
224
Inseob Kim08758f02021-04-08 21:13:22 +0900225func (p *PrebuiltEtc) DebugRamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
226 return proptools.Bool(p.properties.Debug_ramdisk_available) || p.ModuleBase.InstallInDebugRamdisk()
227}
228
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700229func (p *PrebuiltEtc) RecoveryVariantNeeded(ctx android.BaseModuleContext) bool {
230 return proptools.Bool(p.properties.Recovery_available) || p.ModuleBase.InstallInRecovery()
Colin Cross7228ecd2019-11-18 16:00:16 -0800231}
232
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700233func (p *PrebuiltEtc) ExtraImageVariations(ctx android.BaseModuleContext) []string {
Colin Cross7228ecd2019-11-18 16:00:16 -0800234 return nil
235}
236
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700237func (p *PrebuiltEtc) SetImageVariation(ctx android.BaseModuleContext, variation string, module android.Module) {
Colin Cross7228ecd2019-11-18 16:00:16 -0800238}
239
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700240func (p *PrebuiltEtc) SourceFilePath(ctx android.ModuleContext) android.Path {
Yo Chiangf0e19fe2020-11-18 15:28:42 +0800241 return android.PathForModuleSrc(ctx, proptools.String(p.properties.Src))
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900242}
243
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700244func (p *PrebuiltEtc) InstallDirPath() android.InstallPath {
Jooyung Hana0171822019-07-22 15:48:36 +0900245 return p.installDirPath
246}
247
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900248// This allows other derivative modules (e.g. prebuilt_etc_xml) to perform
249// additional steps (like validating the src) before the file is installed.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700250func (p *PrebuiltEtc) SetAdditionalDependencies(paths android.Paths) {
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900251 p.additionalDependencies = &paths
252}
253
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700254func (p *PrebuiltEtc) OutputFile() android.OutputPath {
Jiyong Parkc43e0ac2018-10-04 20:27:15 +0900255 return p.outputFilePath
256}
257
Jiyong Park76a42f52021-02-16 06:50:37 +0900258var _ android.OutputFileProducer = (*PrebuiltEtc)(nil)
259
260func (p *PrebuiltEtc) OutputFiles(tag string) (android.Paths, error) {
261 switch tag {
262 case "":
263 return android.Paths{p.outputFilePath}, nil
264 default:
265 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
266 }
267}
268
Jiyong Parkc43e0ac2018-10-04 20:27:15 +0900269func (p *PrebuiltEtc) SubDir() string {
Inseob Kim27408bf2021-04-06 21:00:17 +0900270 if subDir := proptools.String(p.subdirProperties.Sub_dir); subDir != "" {
Liz Kammer0449a632020-06-26 10:12:36 -0700271 return subDir
272 }
Inseob Kim27408bf2021-04-06 21:00:17 +0900273 return proptools.String(p.subdirProperties.Relative_install_path)
Jiyong Parkc43e0ac2018-10-04 20:27:15 +0900274}
275
Jooyung Han0703fd82020-08-26 22:11:53 +0900276func (p *PrebuiltEtc) BaseDir() string {
Jooyung Han8e5685d2020-09-21 11:02:57 +0900277 return p.installDirBase
Jooyung Han0703fd82020-08-26 22:11:53 +0900278}
279
Jiyong Parkad9ce042018-10-31 22:49:57 +0900280func (p *PrebuiltEtc) Installable() bool {
Yo Chiangf0e19fe2020-11-18 15:28:42 +0800281 return p.properties.Installable == nil || proptools.Bool(p.properties.Installable)
Jiyong Parkad9ce042018-10-31 22:49:57 +0900282}
283
Kiyoung Kimae11c232021-07-19 11:38:04 +0900284func (p *PrebuiltEtc) InVendor() bool {
285 return p.ModuleBase.InstallInVendor()
286}
287
288func (p *PrebuiltEtc) ExcludeFromVendorSnapshot() bool {
289 return false
290}
291
292func (p *PrebuiltEtc) ExcludeFromRecoverySnapshot() bool {
293 return false
294}
295
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700296func (p *PrebuiltEtc) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Yo Chiangf0e19fe2020-11-18 15:28:42 +0800297 if p.properties.Src == nil {
298 ctx.PropertyErrorf("src", "missing prebuilt source file")
299 return
300 }
301 p.sourceFilePath = android.PathForModuleSrc(ctx, proptools.String(p.properties.Src))
Yo Chiang803c40d2020-11-16 20:32:51 +0800302
303 // Determine the output file basename.
304 // If Filename is set, use the name specified by the property.
305 // If Filename_from_src is set, use the source file name.
306 // Otherwise use the module name.
Yo Chiangf0e19fe2020-11-18 15:28:42 +0800307 filename := proptools.String(p.properties.Filename)
308 filenameFromSrc := proptools.Bool(p.properties.Filename_from_src)
309 if filename != "" {
310 if filenameFromSrc {
311 ctx.PropertyErrorf("filename_from_src", "filename is set. filename_from_src can't be true")
312 return
Jiyong Park1a7cf082018-11-13 11:59:12 +0900313 }
Yo Chiangf0e19fe2020-11-18 15:28:42 +0800314 } else if filenameFromSrc {
315 filename = p.sourceFilePath.Base()
316 } else {
317 filename = ctx.ModuleName()
Jiyong Park139a2e62018-10-26 21:49:39 +0900318 }
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700319 p.outputFilePath = android.PathForModuleOut(ctx, filename).OutputPath
Patrice Arruda057a8b12019-06-03 15:29:27 -0700320
Inseob Kim27408bf2021-04-06 21:00:17 +0900321 if strings.Contains(filename, "/") {
322 ctx.PropertyErrorf("filename", "filename cannot contain separator '/'")
323 return
324 }
325
Yo Chiangf0e19fe2020-11-18 15:28:42 +0800326 // Check that `sub_dir` and `relative_install_path` are not set at the same time.
Inseob Kim27408bf2021-04-06 21:00:17 +0900327 if p.subdirProperties.Sub_dir != nil && p.subdirProperties.Relative_install_path != nil {
Liz Kammer0449a632020-06-26 10:12:36 -0700328 ctx.PropertyErrorf("sub_dir", "relative_install_path is set. Cannot set sub_dir")
329 }
330
Yo Chiangf0e19fe2020-11-18 15:28:42 +0800331 // If soc install dir was specified and SOC specific is set, set the installDirPath to the
332 // specified socInstallDirBase.
Jooyung Han8e5685d2020-09-21 11:02:57 +0900333 installBaseDir := p.installDirBase
334 if p.SocSpecific() && p.socInstallDirBase != "" {
335 installBaseDir = p.socInstallDirBase
336 }
337 p.installDirPath = android.PathForModuleInstall(ctx, installBaseDir, p.SubDir())
Jiyong Parkc43e0ac2018-10-04 20:27:15 +0900338
Dan Willemsenb0552672019-01-25 16:04:11 -0800339 // This ensures that outputFilePath has the correct name for others to
340 // use, as the source file may have a different name.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700341 ctx.Build(pctx, android.BuildParams{
342 Rule: android.Cp,
Jiyong Parkc43e0ac2018-10-04 20:27:15 +0900343 Output: p.outputFilePath,
344 Input: p.sourceFilePath,
345 })
Jiyong Parkf9f68052020-09-29 20:15:08 +0900346
Inseob Kim916901e2021-02-17 15:48:53 +0900347 if !p.Installable() {
348 p.SkipInstall()
349 }
350
351 // Call InstallFile even when uninstallable to make the module included in the package
352 installPath := ctx.InstallFile(p.installDirPath, p.outputFilePath.Base(), p.outputFilePath)
353 for _, sl := range p.properties.Symlinks {
354 ctx.InstallSymlink(p.installDirPath, sl, installPath)
Jiyong Parkf9f68052020-09-29 20:15:08 +0900355 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900356}
357
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700358func (p *PrebuiltEtc) AndroidMkEntries() []android.AndroidMkEntries {
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700359 nameSuffix := ""
Yifan Hong1b3348d2020-01-21 15:53:22 -0800360 if p.inRamdisk() && !p.onlyInRamdisk() {
361 nameSuffix = ".ramdisk"
362 }
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700363 if p.inVendorRamdisk() && !p.onlyInVendorRamdisk() {
364 nameSuffix = ".vendor_ramdisk"
365 }
Inseob Kim08758f02021-04-08 21:13:22 +0900366 if p.inDebugRamdisk() && !p.onlyInDebugRamdisk() {
367 nameSuffix = ".debug_ramdisk"
368 }
Kiyoung Kimae11c232021-07-19 11:38:04 +0900369 if p.InRecovery() && !p.onlyInRecovery() {
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700370 nameSuffix = ".recovery"
371 }
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700372 return []android.AndroidMkEntries{android.AndroidMkEntries{
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700373 Class: "ETC",
374 SubName: nameSuffix,
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700375 OutputFile: android.OptionalPathForPath(p.outputFilePath),
376 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -0700377 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700378 entries.SetString("LOCAL_MODULE_TAGS", "optional")
Colin Crossc68db4b2021-11-11 18:59:15 -0800379 entries.SetString("LOCAL_MODULE_PATH", p.installDirPath.String())
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700380 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", p.outputFilePath.Base())
Yo Chiang3d64d492020-05-27 17:56:39 +0800381 if len(p.properties.Symlinks) > 0 {
382 entries.AddStrings("LOCAL_MODULE_SYMLINKS", p.properties.Symlinks...)
383 }
Yo Chiang803c40d2020-11-16 20:32:51 +0800384 entries.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", !p.Installable())
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700385 if p.additionalDependencies != nil {
Yo Chiang803c40d2020-11-16 20:32:51 +0800386 entries.AddStrings("LOCAL_ADDITIONAL_DEPENDENCIES", p.additionalDependencies.Strings()...)
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900387 }
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700388 },
Jiyong Parkc678ad32018-04-10 13:07:10 +0900389 },
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900390 }}
Jiyong Parkc678ad32018-04-10 13:07:10 +0900391}
392
Jooyung Hana0171822019-07-22 15:48:36 +0900393func InitPrebuiltEtcModule(p *PrebuiltEtc, dirBase string) {
394 p.installDirBase = dirBase
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900395 p.AddProperties(&p.properties)
Inseob Kim27408bf2021-04-06 21:00:17 +0900396 p.AddProperties(&p.subdirProperties)
397}
398
399func InitPrebuiltRootModule(p *PrebuiltEtc) {
400 p.installDirBase = "."
401 p.AddProperties(&p.properties)
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900402}
Jiyong Parkc678ad32018-04-10 13:07:10 +0900403
Patrice Arruda9e14b962019-03-11 15:58:50 -0700404// prebuilt_etc is for a prebuilt artifact that is installed in
405// <partition>/etc/<sub_dir> directory.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700406func PrebuiltEtcFactory() android.Module {
Jooyung Hana0171822019-07-22 15:48:36 +0900407 module := &PrebuiltEtc{}
408 InitPrebuiltEtcModule(module, "etc")
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900409 // This module is device-only
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700410 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibFirst)
Inseob Kim1e27a142021-05-06 11:46:11 +0000411 android.InitDefaultableModule(module)
Rupert Shuttleworth378fc1b2021-07-28 08:03:16 -0400412 android.InitBazelModule(module)
Inseob Kim1e27a142021-05-06 11:46:11 +0000413 return module
414}
415
416func defaultsFactory() android.Module {
417 return DefaultsFactory()
418}
419
420func DefaultsFactory(props ...interface{}) android.Module {
421 module := &Defaults{}
422
423 module.AddProperties(props...)
424 module.AddProperties(
425 &prebuiltEtcProperties{},
426 &prebuiltSubdirProperties{},
427 )
428
429 android.InitDefaultsModule(module)
430
Jiyong Parkc678ad32018-04-10 13:07:10 +0900431 return module
432}
Tao Bao0ba5c942018-08-14 22:20:22 -0700433
Patrice Arruda9e14b962019-03-11 15:58:50 -0700434// prebuilt_etc_host is for a host prebuilt artifact that is installed in
435// $(HOST_OUT)/etc/<sub_dir> directory.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700436func PrebuiltEtcHostFactory() android.Module {
Jooyung Hana0171822019-07-22 15:48:36 +0900437 module := &PrebuiltEtc{}
438 InitPrebuiltEtcModule(module, "etc")
Jaewoong Jung24788182019-02-04 14:34:10 -0800439 // This module is host-only
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700440 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommon)
Martin Stjernholmdc6525e2021-10-14 00:42:59 +0100441 android.InitDefaultableModule(module)
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)
Martin Stjernholmdc6525e2021-10-14 00:42:59 +0100452 android.InitDefaultableModule(module)
Inseob Kim27408bf2021-04-06 21:00:17 +0900453 return module
454}
455
Patrice Arruda9e14b962019-03-11 15:58:50 -0700456// prebuilt_usr_share is for a prebuilt artifact that is installed in
457// <partition>/usr/share/<sub_dir> directory.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700458func PrebuiltUserShareFactory() android.Module {
Jooyung Hana0171822019-07-22 15:48:36 +0900459 module := &PrebuiltEtc{}
460 InitPrebuiltEtcModule(module, "usr/share")
Jaewoong Jungc3fcdb42019-02-13 05:50:33 -0800461 // This module is device-only
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700462 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibFirst)
Martin Stjernholmdc6525e2021-10-14 00:42:59 +0100463 android.InitDefaultableModule(module)
Jaewoong Jungc3fcdb42019-02-13 05:50:33 -0800464 return module
465}
466
Patrice Arruda9e14b962019-03-11 15:58:50 -0700467// prebuild_usr_share_host is for a host prebuilt artifact that is installed in
468// $(HOST_OUT)/usr/share/<sub_dir> directory.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700469func PrebuiltUserShareHostFactory() android.Module {
Jooyung Hana0171822019-07-22 15:48:36 +0900470 module := &PrebuiltEtc{}
471 InitPrebuiltEtcModule(module, "usr/share")
Patrice Arruda300cef92019-02-22 15:47:57 -0800472 // This module is host-only
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700473 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommon)
Martin Stjernholmdc6525e2021-10-14 00:42:59 +0100474 android.InitDefaultableModule(module)
Patrice Arruda300cef92019-02-22 15:47:57 -0800475 return module
476}
477
Patrice Arruda61583eb2019-05-14 08:20:45 -0700478// prebuilt_font installs a font in <partition>/fonts directory.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700479func PrebuiltFontFactory() android.Module {
Jooyung Hana0171822019-07-22 15:48:36 +0900480 module := &PrebuiltEtc{}
481 InitPrebuiltEtcModule(module, "fonts")
Patrice Arruda61583eb2019-05-14 08:20:45 -0700482 // This module is device-only
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700483 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibFirst)
Martin Stjernholmdc6525e2021-10-14 00:42:59 +0100484 android.InitDefaultableModule(module)
Patrice Arruda61583eb2019-05-14 08:20:45 -0700485 return module
486}
Patrice Arruda057a8b12019-06-03 15:29:27 -0700487
Yo Chiangf0e19fe2020-11-18 15:28:42 +0800488// prebuilt_firmware installs a firmware file to <partition>/etc/firmware directory for system
489// image.
490// If soc_specific property is set to true, the firmware file is installed to the
491// vendor <partition>/firmware directory for vendor image.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700492func PrebuiltFirmwareFactory() android.Module {
Jooyung Hana0171822019-07-22 15:48:36 +0900493 module := &PrebuiltEtc{}
494 module.socInstallDirBase = "firmware"
495 InitPrebuiltEtcModule(module, "etc/firmware")
Patrice Arruda057a8b12019-06-03 15:29:27 -0700496 // This module is device-only
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700497 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibFirst)
Martin Stjernholmdc6525e2021-10-14 00:42:59 +0100498 android.InitDefaultableModule(module)
Patrice Arruda057a8b12019-06-03 15:29:27 -0700499 return module
500}
Patrice Arruda0f688002020-06-08 21:40:25 +0000501
502// prebuilt_dsp installs a DSP related file to <partition>/etc/dsp directory for system image.
Yo Chiangf0e19fe2020-11-18 15:28:42 +0800503// If soc_specific property is set to true, the DSP related file is installed to the
504// vendor <partition>/dsp directory for vendor image.
Patrice Arruda0f688002020-06-08 21:40:25 +0000505func PrebuiltDSPFactory() android.Module {
506 module := &PrebuiltEtc{}
507 module.socInstallDirBase = "dsp"
508 InitPrebuiltEtcModule(module, "etc/dsp")
509 // This module is device-only
510 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibFirst)
Martin Stjernholmdc6525e2021-10-14 00:42:59 +0100511 android.InitDefaultableModule(module)
Patrice Arruda0f688002020-06-08 21:40:25 +0000512 return module
513}
Colin Cross83ebf232021-04-09 09:41:23 -0700514
515// prebuilt_rfsa installs a firmware file that will be available through Qualcomm's RFSA
516// to the <partition>/lib/rfsa directory.
517func PrebuiltRFSAFactory() android.Module {
518 module := &PrebuiltEtc{}
519 // Ideally these would go in /vendor/dsp, but the /vendor/lib/rfsa paths are hardcoded in too
520 // many places outside of the application processor. They could be moved to /vendor/dsp once
521 // that is cleaned up.
522 InitPrebuiltEtcModule(module, "lib/rfsa")
523 // This module is device-only
524 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibFirst)
Martin Stjernholmdc6525e2021-10-14 00:42:59 +0100525 android.InitDefaultableModule(module)
Colin Cross83ebf232021-04-09 09:41:23 -0700526 return module
527}
Kiyoung Kimae11c232021-07-19 11:38:04 +0900528
Kiyoung Kimae11c232021-07-19 11:38:04 +0900529// Copy file into the snapshot
530func copyFile(ctx android.SingletonContext, path android.Path, out string, fake bool) android.OutputPath {
531 if fake {
532 // Create empty file instead for the fake snapshot
533 return snapshot.WriteStringToFileRule(ctx, "", out)
534 } else {
535 return snapshot.CopyFileRule(pctx, ctx, path, out)
536 }
537}
538
539// Check if the module is target of the snapshot
540func isSnapshotAware(ctx android.SingletonContext, m *PrebuiltEtc, image snapshot.SnapshotImage) bool {
541 if !m.Enabled() {
542 return false
543 }
544
545 // Skip if the module is not included in the image
546 if !image.InImage(m)() {
547 return false
548 }
549
550 // When android/prebuilt.go selects between source and prebuilt, it sets
551 // HideFromMake on the other one to avoid duplicate install rules in make.
552 if m.IsHideFromMake() {
553 return false
554 }
555
556 // There are some prebuilt_etc module with multiple definition of same name.
557 // Check if the target would be included from the build
558 if !m.ExportedToMake() {
559 return false
560 }
561
562 // Skip if the module is in the predefined path list to skip
563 if image.IsProprietaryPath(ctx.ModuleDir(m), ctx.DeviceConfig()) {
564 return false
565 }
566
567 // Skip if the module should be excluded
568 if image.ExcludeFromSnapshot(m) || image.ExcludeFromDirectedSnapshot(ctx.DeviceConfig(), m.BaseModuleName()) {
569 return false
570 }
571
572 // Skip from other exceptional cases
573 if m.Target().Os.Class != android.Device {
574 return false
575 }
576 if m.Target().NativeBridge == android.NativeBridgeEnabled {
577 return false
578 }
579
580 return true
581}
582
583func generatePrebuiltSnapshot(s snapshot.SnapshotSingleton, ctx android.SingletonContext, snapshotArchDir string) android.Paths {
584 /*
585 Snapshot zipped artifacts directory structure for etc modules:
586 {SNAPSHOT_ARCH}/
587 arch-{TARGET_ARCH}-{TARGET_ARCH_VARIANT}/
588 etc/
589 (prebuilt etc files)
590 arch-{TARGET_2ND_ARCH}-{TARGET_2ND_ARCH_VARIANT}/
591 etc/
592 (prebuilt etc files)
593 NOTICE_FILES/
594 (notice files)
595 */
596 var snapshotOutputs android.Paths
597 noticeDir := filepath.Join(snapshotArchDir, "NOTICE_FILES")
598 installedNotices := make(map[string]bool)
599
600 ctx.VisitAllModules(func(module android.Module) {
601 m, ok := module.(*PrebuiltEtc)
602 if !ok {
603 return
604 }
605
606 if !isSnapshotAware(ctx, m, s.Image) {
607 return
608 }
609
610 targetArch := "arch-" + m.Target().Arch.ArchType.String()
611
612 snapshotLibOut := filepath.Join(snapshotArchDir, targetArch, "etc", m.BaseModuleName())
613 snapshotOutputs = append(snapshotOutputs, copyFile(ctx, m.OutputFile(), snapshotLibOut, s.Fake))
614
Rob Seymour925aa092021-08-10 20:42:03 +0000615 prop := snapshot.SnapshotJsonFlags{}
Kiyoung Kimae11c232021-07-19 11:38:04 +0900616 propOut := snapshotLibOut + ".json"
617 prop.ModuleName = m.BaseModuleName()
618 if m.subdirProperties.Relative_install_path != nil {
619 prop.RelativeInstallPath = *m.subdirProperties.Relative_install_path
620 }
621
622 if m.properties.Filename != nil {
623 prop.Filename = *m.properties.Filename
624 }
625
626 j, err := json.Marshal(prop)
627 if err != nil {
628 ctx.Errorf("json marshal to %q failed: %#v", propOut, err)
629 return
630 }
631 snapshotOutputs = append(snapshotOutputs, snapshot.WriteStringToFileRule(ctx, string(j), propOut))
632
633 if len(m.EffectiveLicenseFiles()) > 0 {
634 noticeName := ctx.ModuleName(m) + ".txt"
635 noticeOut := filepath.Join(noticeDir, noticeName)
636 // skip already copied notice file
637 if !installedNotices[noticeOut] {
638 installedNotices[noticeOut] = true
639
640 noticeOutPath := android.PathForOutput(ctx, noticeOut)
641 ctx.Build(pctx, android.BuildParams{
642 Rule: android.Cat,
643 Inputs: m.EffectiveLicenseFiles(),
644 Output: noticeOutPath,
645 Description: "combine notices for " + noticeOut,
646 })
647 snapshotOutputs = append(snapshotOutputs, noticeOutPath)
648 }
649 }
650
651 })
652
653 return snapshotOutputs
654}
Rupert Shuttleworth378fc1b2021-07-28 08:03:16 -0400655
656// For Bazel / bp2build
657
658type bazelPrebuiltEtcAttributes struct {
659 Src bazel.LabelAttribute
660 Filename string
661 Sub_dir string
662 Installable bazel.BoolAttribute
663}
664
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400665// ConvertWithBp2build performs bp2build conversion of PrebuiltEtc
666func (p *PrebuiltEtc) ConvertWithBp2build(ctx android.TopDownMutatorContext) {
667 // All prebuilt_* modules are PrebuiltEtc, but at this time, we only convert prebuilt_etc modules.
Rupert Shuttleworth378fc1b2021-07-28 08:03:16 -0400668 if ctx.ModuleType() != "prebuilt_etc" {
669 return
670 }
671
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400672 prebuiltEtcBp2BuildInternal(ctx, p)
Rupert Shuttleworth378fc1b2021-07-28 08:03:16 -0400673}
674
675func prebuiltEtcBp2BuildInternal(ctx android.TopDownMutatorContext, module *PrebuiltEtc) {
676 var srcLabelAttribute bazel.LabelAttribute
Liz Kammerdff00ea2021-10-04 13:44:34 -0400677 for axis, configToProps := range module.GetArchVariantProperties(ctx, &prebuiltEtcProperties{}) {
678 for config, p := range configToProps {
679 props, ok := p.(*prebuiltEtcProperties)
680 if !ok {
681 continue
682 }
683 if props.Src != nil {
684 srcLabelAttribute.SetSelectValue(axis, config, android.BazelLabelForModuleSrcSingle(ctx, *props.Src))
685 }
686 }
Rupert Shuttleworth378fc1b2021-07-28 08:03:16 -0400687 }
688
689 var filename string
690 if module.properties.Filename != nil {
691 filename = *module.properties.Filename
692 }
693
694 var subDir string
695 if module.subdirProperties.Sub_dir != nil {
696 subDir = *module.subdirProperties.Sub_dir
697 }
698
699 var installableBoolAttribute bazel.BoolAttribute
700 if module.properties.Installable != nil {
701 installableBoolAttribute.Value = module.properties.Installable
702 }
703
704 attrs := &bazelPrebuiltEtcAttributes{
705 Src: srcLabelAttribute,
706 Filename: filename,
707 Sub_dir: subDir,
708 Installable: installableBoolAttribute,
709 }
710
711 props := bazel.BazelTargetModuleProperties{
712 Rule_class: "prebuilt_etc",
713 Bzl_load_location: "//build/bazel/rules:prebuilt_etc.bzl",
714 }
715
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +0000716 ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: module.Name()}, attrs)
Rupert Shuttleworth378fc1b2021-07-28 08:03:16 -0400717}