blob: 4107916a7a2b0837c380ef03ffce34c9da733d9b [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"
Kiyoung Kimae11c232021-07-19 11:38:04 +090039 "android/soong/snapshot"
Jaewoong Jung4b79e982020-06-01 10:45:49 -070040)
41
42var pctx = android.NewPackageContext("android/soong/etc")
Jiyong Parkc678ad32018-04-10 13:07:10 +090043
Jaewoong Jungc3fcdb42019-02-13 05:50:33 -080044// TODO(jungw): Now that it handles more than the ones in etc/, consider renaming this file.
Jiyong Parkc678ad32018-04-10 13:07:10 +090045
46func init() {
Jaewoong Jung4b79e982020-06-01 10:45:49 -070047 pctx.Import("android/soong/android")
Jooyung Han0703fd82020-08-26 22:11:53 +090048 RegisterPrebuiltEtcBuildComponents(android.InitRegistrationContext)
Kiyoung Kimae11c232021-07-19 11:38:04 +090049 snapshot.RegisterSnapshotAction(generatePrebuiltSnapshot)
Jooyung Han0703fd82020-08-26 22:11:53 +090050}
Jaewoong Jung4b79e982020-06-01 10:45:49 -070051
Jooyung Han0703fd82020-08-26 22:11:53 +090052func RegisterPrebuiltEtcBuildComponents(ctx android.RegistrationContext) {
53 ctx.RegisterModuleType("prebuilt_etc", PrebuiltEtcFactory)
54 ctx.RegisterModuleType("prebuilt_etc_host", PrebuiltEtcHostFactory)
Inseob Kim27408bf2021-04-06 21:00:17 +090055 ctx.RegisterModuleType("prebuilt_root", PrebuiltRootFactory)
Jooyung Han0703fd82020-08-26 22:11:53 +090056 ctx.RegisterModuleType("prebuilt_usr_share", PrebuiltUserShareFactory)
57 ctx.RegisterModuleType("prebuilt_usr_share_host", PrebuiltUserShareHostFactory)
58 ctx.RegisterModuleType("prebuilt_font", PrebuiltFontFactory)
59 ctx.RegisterModuleType("prebuilt_firmware", PrebuiltFirmwareFactory)
60 ctx.RegisterModuleType("prebuilt_dsp", PrebuiltDSPFactory)
Colin Cross83ebf232021-04-09 09:41:23 -070061 ctx.RegisterModuleType("prebuilt_rfsa", PrebuiltRFSAFactory)
Inseob Kim1e27a142021-05-06 11:46:11 +000062
63 ctx.RegisterModuleType("prebuilt_defaults", defaultsFactory)
Jiyong Parkc678ad32018-04-10 13:07:10 +090064}
65
Paul Duffin1172fed2021-03-08 11:28:18 +000066var PrepareForTestWithPrebuiltEtc = android.FixtureRegisterWithContext(RegisterPrebuiltEtcBuildComponents)
67
Jiyong Parkc678ad32018-04-10 13:07:10 +090068type prebuiltEtcProperties struct {
Yo Chiang803c40d2020-11-16 20:32:51 +080069 // Source file of this prebuilt. Can reference a genrule type module with the ":module" syntax.
Colin Cross27b922f2019-03-04 22:35:41 -080070 Src *string `android:"path,arch_variant"`
Jiyong Parkc678ad32018-04-10 13:07:10 +090071
Yo Chiangf0e19fe2020-11-18 15:28:42 +080072 // Optional name for the installed file. If unspecified, name of the module is used as the file
73 // name.
Jiyong Park139a2e62018-10-26 21:49:39 +090074 Filename *string `android:"arch_variant"`
75
Yo Chiangf0e19fe2020-11-18 15:28:42 +080076 // When set to true, and filename property is not set, the name for the installed file
Jiyong Park1a7cf082018-11-13 11:59:12 +090077 // is the same as the file name of the source file.
78 Filename_from_src *bool `android:"arch_variant"`
79
Yifan Hong1b3348d2020-01-21 15:53:22 -080080 // Make this module available when building for ramdisk.
Yifan Hong39143a92020-10-26 12:43:12 -070081 // On device without a dedicated recovery partition, the module is only
82 // available after switching root into
83 // /first_stage_ramdisk. To expose the module before switching root, install
84 // the recovery variant instead.
Yifan Hong1b3348d2020-01-21 15:53:22 -080085 Ramdisk_available *bool
86
Yifan Hong60e0cfb2020-10-21 15:17:56 -070087 // Make this module available when building for vendor ramdisk.
Yifan Hong39143a92020-10-26 12:43:12 -070088 // On device without a dedicated recovery partition, the module is only
89 // available after switching root into
90 // /first_stage_ramdisk. To expose the module before switching root, install
91 // the recovery variant instead.
Yifan Hong60e0cfb2020-10-21 15:17:56 -070092 Vendor_ramdisk_available *bool
93
Inseob Kim08758f02021-04-08 21:13:22 +090094 // Make this module available when building for debug ramdisk.
95 Debug_ramdisk_available *bool
96
Tao Bao0ba5c942018-08-14 22:20:22 -070097 // Make this module available when building for recovery.
98 Recovery_available *bool
99
Jiyong Parkad9ce042018-10-31 22:49:57 +0900100 // Whether this module is directly installable to one of the partitions. Default: true.
101 Installable *bool
Yo Chiang3d64d492020-05-27 17:56:39 +0800102
103 // Install symlinks to the installed file.
104 Symlinks []string `android:"arch_variant"`
Jiyong Parkc678ad32018-04-10 13:07:10 +0900105}
106
Inseob Kim27408bf2021-04-06 21:00:17 +0900107type prebuiltSubdirProperties struct {
108 // Optional subdirectory under which this file is installed into, cannot be specified with
109 // relative_install_path, prefer relative_install_path.
110 Sub_dir *string `android:"arch_variant"`
111
112 // Optional subdirectory under which this file is installed into, cannot be specified with
113 // sub_dir.
114 Relative_install_path *string `android:"arch_variant"`
115}
116
Jooyung Han39edb6c2019-11-06 16:53:07 +0900117type PrebuiltEtcModule interface {
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700118 android.Module
Yo Chiangf0e19fe2020-11-18 15:28:42 +0800119
120 // Returns the base install directory, such as "etc", "usr/share".
Jooyung Han0703fd82020-08-26 22:11:53 +0900121 BaseDir() string
Yo Chiangf0e19fe2020-11-18 15:28:42 +0800122
123 // Returns the sub install directory relative to BaseDir().
Jooyung Han39edb6c2019-11-06 16:53:07 +0900124 SubDir() string
Yo Chiangf0e19fe2020-11-18 15:28:42 +0800125
126 // Returns an android.OutputPath to the intermeidate file, which is the renamed prebuilt source
127 // file.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700128 OutputFile() android.OutputPath
Jooyung Han39edb6c2019-11-06 16:53:07 +0900129}
130
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900131type PrebuiltEtc struct {
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700132 android.ModuleBase
Inseob Kim1e27a142021-05-06 11:46:11 +0000133 android.DefaultableModuleBase
Jiyong Parkc678ad32018-04-10 13:07:10 +0900134
Kiyoung Kimae11c232021-07-19 11:38:04 +0900135 snapshot.VendorSnapshotModuleInterface
136 snapshot.RecoverySnapshotModuleInterface
137
Inseob Kim27408bf2021-04-06 21:00:17 +0900138 properties prebuiltEtcProperties
139 subdirProperties prebuiltSubdirProperties
Jiyong Parkc678ad32018-04-10 13:07:10 +0900140
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700141 sourceFilePath android.Path
142 outputFilePath android.OutputPath
Jaewoong Jungc3fcdb42019-02-13 05:50:33 -0800143 // The base install location, e.g. "etc" for prebuilt_etc, "usr/share" for prebuilt_usr_share.
Patrice Arruda057a8b12019-06-03 15:29:27 -0700144 installDirBase string
Yo Chiangf0e19fe2020-11-18 15:28:42 +0800145 // The base install location when soc_specific property is set to true, e.g. "firmware" for
146 // prebuilt_firmware.
Patrice Arruda057a8b12019-06-03 15:29:27 -0700147 socInstallDirBase string
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700148 installDirPath android.InstallPath
149 additionalDependencies *android.Paths
Jiyong Parkc678ad32018-04-10 13:07:10 +0900150}
151
Inseob Kim1e27a142021-05-06 11:46:11 +0000152type Defaults struct {
153 android.ModuleBase
154 android.DefaultsModuleBase
155}
156
Yifan Hong1b3348d2020-01-21 15:53:22 -0800157func (p *PrebuiltEtc) inRamdisk() bool {
158 return p.ModuleBase.InRamdisk() || p.ModuleBase.InstallInRamdisk()
159}
160
161func (p *PrebuiltEtc) onlyInRamdisk() bool {
162 return p.ModuleBase.InstallInRamdisk()
163}
164
165func (p *PrebuiltEtc) InstallInRamdisk() bool {
166 return p.inRamdisk()
167}
168
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700169func (p *PrebuiltEtc) inVendorRamdisk() bool {
170 return p.ModuleBase.InVendorRamdisk() || p.ModuleBase.InstallInVendorRamdisk()
171}
172
173func (p *PrebuiltEtc) onlyInVendorRamdisk() bool {
174 return p.ModuleBase.InstallInVendorRamdisk()
175}
176
177func (p *PrebuiltEtc) InstallInVendorRamdisk() bool {
178 return p.inVendorRamdisk()
179}
180
Inseob Kim08758f02021-04-08 21:13:22 +0900181func (p *PrebuiltEtc) inDebugRamdisk() bool {
182 return p.ModuleBase.InDebugRamdisk() || p.ModuleBase.InstallInDebugRamdisk()
183}
184
185func (p *PrebuiltEtc) onlyInDebugRamdisk() bool {
186 return p.ModuleBase.InstallInDebugRamdisk()
187}
188
189func (p *PrebuiltEtc) InstallInDebugRamdisk() bool {
190 return p.inDebugRamdisk()
191}
192
Kiyoung Kimae11c232021-07-19 11:38:04 +0900193func (p *PrebuiltEtc) InRecovery() bool {
Colin Cross7228ecd2019-11-18 16:00:16 -0800194 return p.ModuleBase.InRecovery() || p.ModuleBase.InstallInRecovery()
Tao Bao0ba5c942018-08-14 22:20:22 -0700195}
196
197func (p *PrebuiltEtc) onlyInRecovery() bool {
198 return p.ModuleBase.InstallInRecovery()
199}
200
201func (p *PrebuiltEtc) InstallInRecovery() bool {
Kiyoung Kimae11c232021-07-19 11:38:04 +0900202 return p.InRecovery()
Tao Bao0ba5c942018-08-14 22:20:22 -0700203}
204
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700205var _ android.ImageInterface = (*PrebuiltEtc)(nil)
Colin Cross7228ecd2019-11-18 16:00:16 -0800206
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700207func (p *PrebuiltEtc) ImageMutatorBegin(ctx android.BaseModuleContext) {}
Colin Cross7228ecd2019-11-18 16:00:16 -0800208
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700209func (p *PrebuiltEtc) CoreVariantNeeded(ctx android.BaseModuleContext) bool {
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700210 return !p.ModuleBase.InstallInRecovery() && !p.ModuleBase.InstallInRamdisk() &&
Inseob Kim08758f02021-04-08 21:13:22 +0900211 !p.ModuleBase.InstallInVendorRamdisk() && !p.ModuleBase.InstallInDebugRamdisk()
Yifan Hong1b3348d2020-01-21 15:53:22 -0800212}
213
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700214func (p *PrebuiltEtc) RamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
215 return proptools.Bool(p.properties.Ramdisk_available) || p.ModuleBase.InstallInRamdisk()
Colin Cross7228ecd2019-11-18 16:00:16 -0800216}
217
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700218func (p *PrebuiltEtc) VendorRamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
219 return proptools.Bool(p.properties.Vendor_ramdisk_available) || p.ModuleBase.InstallInVendorRamdisk()
220}
221
Inseob Kim08758f02021-04-08 21:13:22 +0900222func (p *PrebuiltEtc) DebugRamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
223 return proptools.Bool(p.properties.Debug_ramdisk_available) || p.ModuleBase.InstallInDebugRamdisk()
224}
225
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700226func (p *PrebuiltEtc) RecoveryVariantNeeded(ctx android.BaseModuleContext) bool {
227 return proptools.Bool(p.properties.Recovery_available) || p.ModuleBase.InstallInRecovery()
Colin Cross7228ecd2019-11-18 16:00:16 -0800228}
229
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700230func (p *PrebuiltEtc) ExtraImageVariations(ctx android.BaseModuleContext) []string {
Colin Cross7228ecd2019-11-18 16:00:16 -0800231 return nil
232}
233
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700234func (p *PrebuiltEtc) SetImageVariation(ctx android.BaseModuleContext, variation string, module android.Module) {
Colin Cross7228ecd2019-11-18 16:00:16 -0800235}
236
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700237func (p *PrebuiltEtc) SourceFilePath(ctx android.ModuleContext) android.Path {
Yo Chiangf0e19fe2020-11-18 15:28:42 +0800238 return android.PathForModuleSrc(ctx, proptools.String(p.properties.Src))
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900239}
240
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700241func (p *PrebuiltEtc) InstallDirPath() android.InstallPath {
Jooyung Hana0171822019-07-22 15:48:36 +0900242 return p.installDirPath
243}
244
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900245// This allows other derivative modules (e.g. prebuilt_etc_xml) to perform
246// additional steps (like validating the src) before the file is installed.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700247func (p *PrebuiltEtc) SetAdditionalDependencies(paths android.Paths) {
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900248 p.additionalDependencies = &paths
249}
250
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700251func (p *PrebuiltEtc) OutputFile() android.OutputPath {
Jiyong Parkc43e0ac2018-10-04 20:27:15 +0900252 return p.outputFilePath
253}
254
Jiyong Park76a42f52021-02-16 06:50:37 +0900255var _ android.OutputFileProducer = (*PrebuiltEtc)(nil)
256
257func (p *PrebuiltEtc) OutputFiles(tag string) (android.Paths, error) {
258 switch tag {
259 case "":
260 return android.Paths{p.outputFilePath}, nil
261 default:
262 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
263 }
264}
265
Jiyong Parkc43e0ac2018-10-04 20:27:15 +0900266func (p *PrebuiltEtc) SubDir() string {
Inseob Kim27408bf2021-04-06 21:00:17 +0900267 if subDir := proptools.String(p.subdirProperties.Sub_dir); subDir != "" {
Liz Kammer0449a632020-06-26 10:12:36 -0700268 return subDir
269 }
Inseob Kim27408bf2021-04-06 21:00:17 +0900270 return proptools.String(p.subdirProperties.Relative_install_path)
Jiyong Parkc43e0ac2018-10-04 20:27:15 +0900271}
272
Jooyung Han0703fd82020-08-26 22:11:53 +0900273func (p *PrebuiltEtc) BaseDir() string {
Jooyung Han8e5685d2020-09-21 11:02:57 +0900274 return p.installDirBase
Jooyung Han0703fd82020-08-26 22:11:53 +0900275}
276
Jiyong Parkad9ce042018-10-31 22:49:57 +0900277func (p *PrebuiltEtc) Installable() bool {
Yo Chiangf0e19fe2020-11-18 15:28:42 +0800278 return p.properties.Installable == nil || proptools.Bool(p.properties.Installable)
Jiyong Parkad9ce042018-10-31 22:49:57 +0900279}
280
Kiyoung Kimae11c232021-07-19 11:38:04 +0900281func (p *PrebuiltEtc) InVendor() bool {
282 return p.ModuleBase.InstallInVendor()
283}
284
285func (p *PrebuiltEtc) ExcludeFromVendorSnapshot() bool {
286 return false
287}
288
289func (p *PrebuiltEtc) ExcludeFromRecoverySnapshot() bool {
290 return false
291}
292
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700293func (p *PrebuiltEtc) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Yo Chiangf0e19fe2020-11-18 15:28:42 +0800294 if p.properties.Src == nil {
295 ctx.PropertyErrorf("src", "missing prebuilt source file")
296 return
297 }
298 p.sourceFilePath = android.PathForModuleSrc(ctx, proptools.String(p.properties.Src))
Yo Chiang803c40d2020-11-16 20:32:51 +0800299
300 // Determine the output file basename.
301 // If Filename is set, use the name specified by the property.
302 // If Filename_from_src is set, use the source file name.
303 // Otherwise use the module name.
Yo Chiangf0e19fe2020-11-18 15:28:42 +0800304 filename := proptools.String(p.properties.Filename)
305 filenameFromSrc := proptools.Bool(p.properties.Filename_from_src)
306 if filename != "" {
307 if filenameFromSrc {
308 ctx.PropertyErrorf("filename_from_src", "filename is set. filename_from_src can't be true")
309 return
Jiyong Park1a7cf082018-11-13 11:59:12 +0900310 }
Yo Chiangf0e19fe2020-11-18 15:28:42 +0800311 } else if filenameFromSrc {
312 filename = p.sourceFilePath.Base()
313 } else {
314 filename = ctx.ModuleName()
Jiyong Park139a2e62018-10-26 21:49:39 +0900315 }
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700316 p.outputFilePath = android.PathForModuleOut(ctx, filename).OutputPath
Patrice Arruda057a8b12019-06-03 15:29:27 -0700317
Inseob Kim27408bf2021-04-06 21:00:17 +0900318 if strings.Contains(filename, "/") {
319 ctx.PropertyErrorf("filename", "filename cannot contain separator '/'")
320 return
321 }
322
Yo Chiangf0e19fe2020-11-18 15:28:42 +0800323 // Check that `sub_dir` and `relative_install_path` are not set at the same time.
Inseob Kim27408bf2021-04-06 21:00:17 +0900324 if p.subdirProperties.Sub_dir != nil && p.subdirProperties.Relative_install_path != nil {
Liz Kammer0449a632020-06-26 10:12:36 -0700325 ctx.PropertyErrorf("sub_dir", "relative_install_path is set. Cannot set sub_dir")
326 }
327
Yo Chiangf0e19fe2020-11-18 15:28:42 +0800328 // If soc install dir was specified and SOC specific is set, set the installDirPath to the
329 // specified socInstallDirBase.
Jooyung Han8e5685d2020-09-21 11:02:57 +0900330 installBaseDir := p.installDirBase
331 if p.SocSpecific() && p.socInstallDirBase != "" {
332 installBaseDir = p.socInstallDirBase
333 }
334 p.installDirPath = android.PathForModuleInstall(ctx, installBaseDir, p.SubDir())
Jiyong Parkc43e0ac2018-10-04 20:27:15 +0900335
Dan Willemsenb0552672019-01-25 16:04:11 -0800336 // This ensures that outputFilePath has the correct name for others to
337 // use, as the source file may have a different name.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700338 ctx.Build(pctx, android.BuildParams{
339 Rule: android.Cp,
Jiyong Parkc43e0ac2018-10-04 20:27:15 +0900340 Output: p.outputFilePath,
341 Input: p.sourceFilePath,
342 })
Jiyong Parkf9f68052020-09-29 20:15:08 +0900343
Inseob Kim916901e2021-02-17 15:48:53 +0900344 if !p.Installable() {
345 p.SkipInstall()
346 }
347
348 // Call InstallFile even when uninstallable to make the module included in the package
349 installPath := ctx.InstallFile(p.installDirPath, p.outputFilePath.Base(), p.outputFilePath)
350 for _, sl := range p.properties.Symlinks {
351 ctx.InstallSymlink(p.installDirPath, sl, installPath)
Jiyong Parkf9f68052020-09-29 20:15:08 +0900352 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900353}
354
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700355func (p *PrebuiltEtc) AndroidMkEntries() []android.AndroidMkEntries {
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700356 nameSuffix := ""
Yifan Hong1b3348d2020-01-21 15:53:22 -0800357 if p.inRamdisk() && !p.onlyInRamdisk() {
358 nameSuffix = ".ramdisk"
359 }
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700360 if p.inVendorRamdisk() && !p.onlyInVendorRamdisk() {
361 nameSuffix = ".vendor_ramdisk"
362 }
Inseob Kim08758f02021-04-08 21:13:22 +0900363 if p.inDebugRamdisk() && !p.onlyInDebugRamdisk() {
364 nameSuffix = ".debug_ramdisk"
365 }
Kiyoung Kimae11c232021-07-19 11:38:04 +0900366 if p.InRecovery() && !p.onlyInRecovery() {
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700367 nameSuffix = ".recovery"
368 }
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700369 return []android.AndroidMkEntries{android.AndroidMkEntries{
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700370 Class: "ETC",
371 SubName: nameSuffix,
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700372 OutputFile: android.OptionalPathForPath(p.outputFilePath),
373 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -0700374 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700375 entries.SetString("LOCAL_MODULE_TAGS", "optional")
Colin Crossff6c33d2019-10-02 16:01:35 -0700376 entries.SetString("LOCAL_MODULE_PATH", p.installDirPath.ToMakePath().String())
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700377 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", p.outputFilePath.Base())
Yo Chiang3d64d492020-05-27 17:56:39 +0800378 if len(p.properties.Symlinks) > 0 {
379 entries.AddStrings("LOCAL_MODULE_SYMLINKS", p.properties.Symlinks...)
380 }
Yo Chiang803c40d2020-11-16 20:32:51 +0800381 entries.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", !p.Installable())
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700382 if p.additionalDependencies != nil {
Yo Chiang803c40d2020-11-16 20:32:51 +0800383 entries.AddStrings("LOCAL_ADDITIONAL_DEPENDENCIES", p.additionalDependencies.Strings()...)
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900384 }
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700385 },
Jiyong Parkc678ad32018-04-10 13:07:10 +0900386 },
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900387 }}
Jiyong Parkc678ad32018-04-10 13:07:10 +0900388}
389
Jooyung Hana0171822019-07-22 15:48:36 +0900390func InitPrebuiltEtcModule(p *PrebuiltEtc, dirBase string) {
391 p.installDirBase = dirBase
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900392 p.AddProperties(&p.properties)
Inseob Kim27408bf2021-04-06 21:00:17 +0900393 p.AddProperties(&p.subdirProperties)
394}
395
396func InitPrebuiltRootModule(p *PrebuiltEtc) {
397 p.installDirBase = "."
398 p.AddProperties(&p.properties)
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900399}
Jiyong Parkc678ad32018-04-10 13:07:10 +0900400
Patrice Arruda9e14b962019-03-11 15:58:50 -0700401// prebuilt_etc is for a prebuilt artifact that is installed in
402// <partition>/etc/<sub_dir> directory.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700403func PrebuiltEtcFactory() android.Module {
Jooyung Hana0171822019-07-22 15:48:36 +0900404 module := &PrebuiltEtc{}
405 InitPrebuiltEtcModule(module, "etc")
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900406 // This module is device-only
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700407 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibFirst)
Inseob Kim1e27a142021-05-06 11:46:11 +0000408 android.InitDefaultableModule(module)
409 return module
410}
411
412func defaultsFactory() android.Module {
413 return DefaultsFactory()
414}
415
416func DefaultsFactory(props ...interface{}) android.Module {
417 module := &Defaults{}
418
419 module.AddProperties(props...)
420 module.AddProperties(
421 &prebuiltEtcProperties{},
422 &prebuiltSubdirProperties{},
423 )
424
425 android.InitDefaultsModule(module)
426
Jiyong Parkc678ad32018-04-10 13:07:10 +0900427 return module
428}
Tao Bao0ba5c942018-08-14 22:20:22 -0700429
Patrice Arruda9e14b962019-03-11 15:58:50 -0700430// prebuilt_etc_host is for a host prebuilt artifact that is installed in
431// $(HOST_OUT)/etc/<sub_dir> directory.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700432func PrebuiltEtcHostFactory() android.Module {
Jooyung Hana0171822019-07-22 15:48:36 +0900433 module := &PrebuiltEtc{}
434 InitPrebuiltEtcModule(module, "etc")
Jaewoong Jung24788182019-02-04 14:34:10 -0800435 // This module is host-only
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700436 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommon)
Jaewoong Jung24788182019-02-04 14:34:10 -0800437 return module
438}
439
Inseob Kim27408bf2021-04-06 21:00:17 +0900440// prebuilt_root is for a prebuilt artifact that is installed in
441// <partition>/ directory. Can't have any sub directories.
442func PrebuiltRootFactory() android.Module {
443 module := &PrebuiltEtc{}
444 InitPrebuiltRootModule(module)
445 // This module is device-only
446 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibFirst)
447 return module
448}
449
Patrice Arruda9e14b962019-03-11 15:58:50 -0700450// prebuilt_usr_share is for a prebuilt artifact that is installed in
451// <partition>/usr/share/<sub_dir> directory.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700452func PrebuiltUserShareFactory() android.Module {
Jooyung Hana0171822019-07-22 15:48:36 +0900453 module := &PrebuiltEtc{}
454 InitPrebuiltEtcModule(module, "usr/share")
Jaewoong Jungc3fcdb42019-02-13 05:50:33 -0800455 // This module is device-only
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700456 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibFirst)
Jaewoong Jungc3fcdb42019-02-13 05:50:33 -0800457 return module
458}
459
Patrice Arruda9e14b962019-03-11 15:58:50 -0700460// prebuild_usr_share_host is for a host prebuilt artifact that is installed in
461// $(HOST_OUT)/usr/share/<sub_dir> directory.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700462func PrebuiltUserShareHostFactory() android.Module {
Jooyung Hana0171822019-07-22 15:48:36 +0900463 module := &PrebuiltEtc{}
464 InitPrebuiltEtcModule(module, "usr/share")
Patrice Arruda300cef92019-02-22 15:47:57 -0800465 // This module is host-only
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700466 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommon)
Patrice Arruda300cef92019-02-22 15:47:57 -0800467 return module
468}
469
Patrice Arruda61583eb2019-05-14 08:20:45 -0700470// prebuilt_font installs a font in <partition>/fonts directory.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700471func PrebuiltFontFactory() android.Module {
Jooyung Hana0171822019-07-22 15:48:36 +0900472 module := &PrebuiltEtc{}
473 InitPrebuiltEtcModule(module, "fonts")
Patrice Arruda61583eb2019-05-14 08:20:45 -0700474 // This module is device-only
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700475 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibFirst)
Patrice Arruda61583eb2019-05-14 08:20:45 -0700476 return module
477}
Patrice Arruda057a8b12019-06-03 15:29:27 -0700478
Yo Chiangf0e19fe2020-11-18 15:28:42 +0800479// prebuilt_firmware installs a firmware file to <partition>/etc/firmware directory for system
480// image.
481// If soc_specific property is set to true, the firmware file is installed to the
482// vendor <partition>/firmware directory for vendor image.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700483func PrebuiltFirmwareFactory() android.Module {
Jooyung Hana0171822019-07-22 15:48:36 +0900484 module := &PrebuiltEtc{}
485 module.socInstallDirBase = "firmware"
486 InitPrebuiltEtcModule(module, "etc/firmware")
Patrice Arruda057a8b12019-06-03 15:29:27 -0700487 // This module is device-only
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700488 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibFirst)
Patrice Arruda057a8b12019-06-03 15:29:27 -0700489 return module
490}
Patrice Arruda0f688002020-06-08 21:40:25 +0000491
492// prebuilt_dsp installs a DSP related file to <partition>/etc/dsp directory for system image.
Yo Chiangf0e19fe2020-11-18 15:28:42 +0800493// If soc_specific property is set to true, the DSP related file is installed to the
494// vendor <partition>/dsp directory for vendor image.
Patrice Arruda0f688002020-06-08 21:40:25 +0000495func PrebuiltDSPFactory() android.Module {
496 module := &PrebuiltEtc{}
497 module.socInstallDirBase = "dsp"
498 InitPrebuiltEtcModule(module, "etc/dsp")
499 // This module is device-only
500 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibFirst)
501 return module
502}
Colin Cross83ebf232021-04-09 09:41:23 -0700503
504// prebuilt_rfsa installs a firmware file that will be available through Qualcomm's RFSA
505// to the <partition>/lib/rfsa directory.
506func PrebuiltRFSAFactory() android.Module {
507 module := &PrebuiltEtc{}
508 // Ideally these would go in /vendor/dsp, but the /vendor/lib/rfsa paths are hardcoded in too
509 // many places outside of the application processor. They could be moved to /vendor/dsp once
510 // that is cleaned up.
511 InitPrebuiltEtcModule(module, "lib/rfsa")
512 // This module is device-only
513 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibFirst)
514 return module
515}
Kiyoung Kimae11c232021-07-19 11:38:04 +0900516
517// Flags to be included in the snapshot
518type snapshotJsonFlags struct {
519 ModuleName string `json:",omitempty"`
520 Filename string `json:",omitempty"`
521 RelativeInstallPath string `json:",omitempty"`
522}
523
524// Copy file into the snapshot
525func copyFile(ctx android.SingletonContext, path android.Path, out string, fake bool) android.OutputPath {
526 if fake {
527 // Create empty file instead for the fake snapshot
528 return snapshot.WriteStringToFileRule(ctx, "", out)
529 } else {
530 return snapshot.CopyFileRule(pctx, ctx, path, out)
531 }
532}
533
534// Check if the module is target of the snapshot
535func isSnapshotAware(ctx android.SingletonContext, m *PrebuiltEtc, image snapshot.SnapshotImage) bool {
536 if !m.Enabled() {
537 return false
538 }
539
540 // Skip if the module is not included in the image
541 if !image.InImage(m)() {
542 return false
543 }
544
545 // When android/prebuilt.go selects between source and prebuilt, it sets
546 // HideFromMake on the other one to avoid duplicate install rules in make.
547 if m.IsHideFromMake() {
548 return false
549 }
550
551 // There are some prebuilt_etc module with multiple definition of same name.
552 // Check if the target would be included from the build
553 if !m.ExportedToMake() {
554 return false
555 }
556
557 // Skip if the module is in the predefined path list to skip
558 if image.IsProprietaryPath(ctx.ModuleDir(m), ctx.DeviceConfig()) {
559 return false
560 }
561
562 // Skip if the module should be excluded
563 if image.ExcludeFromSnapshot(m) || image.ExcludeFromDirectedSnapshot(ctx.DeviceConfig(), m.BaseModuleName()) {
564 return false
565 }
566
567 // Skip from other exceptional cases
568 if m.Target().Os.Class != android.Device {
569 return false
570 }
571 if m.Target().NativeBridge == android.NativeBridgeEnabled {
572 return false
573 }
574
575 return true
576}
577
578func generatePrebuiltSnapshot(s snapshot.SnapshotSingleton, ctx android.SingletonContext, snapshotArchDir string) android.Paths {
579 /*
580 Snapshot zipped artifacts directory structure for etc modules:
581 {SNAPSHOT_ARCH}/
582 arch-{TARGET_ARCH}-{TARGET_ARCH_VARIANT}/
583 etc/
584 (prebuilt etc files)
585 arch-{TARGET_2ND_ARCH}-{TARGET_2ND_ARCH_VARIANT}/
586 etc/
587 (prebuilt etc files)
588 NOTICE_FILES/
589 (notice files)
590 */
591 var snapshotOutputs android.Paths
592 noticeDir := filepath.Join(snapshotArchDir, "NOTICE_FILES")
593 installedNotices := make(map[string]bool)
594
595 ctx.VisitAllModules(func(module android.Module) {
596 m, ok := module.(*PrebuiltEtc)
597 if !ok {
598 return
599 }
600
601 if !isSnapshotAware(ctx, m, s.Image) {
602 return
603 }
604
605 targetArch := "arch-" + m.Target().Arch.ArchType.String()
606
607 snapshotLibOut := filepath.Join(snapshotArchDir, targetArch, "etc", m.BaseModuleName())
608 snapshotOutputs = append(snapshotOutputs, copyFile(ctx, m.OutputFile(), snapshotLibOut, s.Fake))
609
610 prop := snapshotJsonFlags{}
611 propOut := snapshotLibOut + ".json"
612 prop.ModuleName = m.BaseModuleName()
613 if m.subdirProperties.Relative_install_path != nil {
614 prop.RelativeInstallPath = *m.subdirProperties.Relative_install_path
615 }
616
617 if m.properties.Filename != nil {
618 prop.Filename = *m.properties.Filename
619 }
620
621 j, err := json.Marshal(prop)
622 if err != nil {
623 ctx.Errorf("json marshal to %q failed: %#v", propOut, err)
624 return
625 }
626 snapshotOutputs = append(snapshotOutputs, snapshot.WriteStringToFileRule(ctx, string(j), propOut))
627
628 if len(m.EffectiveLicenseFiles()) > 0 {
629 noticeName := ctx.ModuleName(m) + ".txt"
630 noticeOut := filepath.Join(noticeDir, noticeName)
631 // skip already copied notice file
632 if !installedNotices[noticeOut] {
633 installedNotices[noticeOut] = true
634
635 noticeOutPath := android.PathForOutput(ctx, noticeOut)
636 ctx.Build(pctx, android.BuildParams{
637 Rule: android.Cat,
638 Inputs: m.EffectiveLicenseFiles(),
639 Output: noticeOutPath,
640 Description: "combine notices for " + noticeOut,
641 })
642 snapshotOutputs = append(snapshotOutputs, noticeOutPath)
643 }
644 }
645
646 })
647
648 return snapshotOutputs
649}