blob: 664cb51e24349c82e8a4f4379e62e3d4d0974c22 [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
Jaewoong Jung4b79e982020-06-01 10:45:49 -070017import (
18 "strconv"
19
20 "github.com/google/blueprint/proptools"
21
22 "android/soong/android"
23)
24
25var pctx = android.NewPackageContext("android/soong/etc")
Jiyong Parkc678ad32018-04-10 13:07:10 +090026
Jaewoong Jungc3fcdb42019-02-13 05:50:33 -080027// TODO(jungw): Now that it handles more than the ones in etc/, consider renaming this file.
Jiyong Parkc678ad32018-04-10 13:07:10 +090028
29func init() {
Jaewoong Jung4b79e982020-06-01 10:45:49 -070030 pctx.Import("android/soong/android")
Jooyung Han0703fd82020-08-26 22:11:53 +090031 RegisterPrebuiltEtcBuildComponents(android.InitRegistrationContext)
32}
Jaewoong Jung4b79e982020-06-01 10:45:49 -070033
Jooyung Han0703fd82020-08-26 22:11:53 +090034func RegisterPrebuiltEtcBuildComponents(ctx android.RegistrationContext) {
35 ctx.RegisterModuleType("prebuilt_etc", PrebuiltEtcFactory)
36 ctx.RegisterModuleType("prebuilt_etc_host", PrebuiltEtcHostFactory)
37 ctx.RegisterModuleType("prebuilt_usr_share", PrebuiltUserShareFactory)
38 ctx.RegisterModuleType("prebuilt_usr_share_host", PrebuiltUserShareHostFactory)
39 ctx.RegisterModuleType("prebuilt_font", PrebuiltFontFactory)
40 ctx.RegisterModuleType("prebuilt_firmware", PrebuiltFirmwareFactory)
41 ctx.RegisterModuleType("prebuilt_dsp", PrebuiltDSPFactory)
Jiyong Parkc678ad32018-04-10 13:07:10 +090042}
43
44type prebuiltEtcProperties struct {
45 // Source file of this prebuilt.
Colin Cross27b922f2019-03-04 22:35:41 -080046 Src *string `android:"path,arch_variant"`
Jiyong Parkc678ad32018-04-10 13:07:10 +090047
Liz Kammer0449a632020-06-26 10:12:36 -070048 // optional subdirectory under which this file is installed into, cannot be specified with relative_install_path, prefer relative_install_path
Jiyong Parkc678ad32018-04-10 13:07:10 +090049 Sub_dir *string `android:"arch_variant"`
Tao Bao0ba5c942018-08-14 22:20:22 -070050
Liz Kammer0449a632020-06-26 10:12:36 -070051 // optional subdirectory under which this file is installed into, cannot be specified with sub_dir
52 Relative_install_path *string `android:"arch_variant"`
53
Jiyong Park139a2e62018-10-26 21:49:39 +090054 // optional name for the installed file. If unspecified, name of the module is used as the file name
55 Filename *string `android:"arch_variant"`
56
Jiyong Park1a7cf082018-11-13 11:59:12 +090057 // when set to true, and filename property is not set, the name for the installed file
58 // is the same as the file name of the source file.
59 Filename_from_src *bool `android:"arch_variant"`
60
Yifan Hong1b3348d2020-01-21 15:53:22 -080061 // Make this module available when building for ramdisk.
62 Ramdisk_available *bool
63
Tao Bao0ba5c942018-08-14 22:20:22 -070064 // Make this module available when building for recovery.
65 Recovery_available *bool
66
Jiyong Parkad9ce042018-10-31 22:49:57 +090067 // Whether this module is directly installable to one of the partitions. Default: true.
68 Installable *bool
Yo Chiang3d64d492020-05-27 17:56:39 +080069
70 // Install symlinks to the installed file.
71 Symlinks []string `android:"arch_variant"`
Jiyong Parkc678ad32018-04-10 13:07:10 +090072}
73
Jooyung Han39edb6c2019-11-06 16:53:07 +090074type PrebuiltEtcModule interface {
Jaewoong Jung4b79e982020-06-01 10:45:49 -070075 android.Module
Jooyung Han0703fd82020-08-26 22:11:53 +090076 BaseDir() string
Jooyung Han39edb6c2019-11-06 16:53:07 +090077 SubDir() string
Jaewoong Jung4b79e982020-06-01 10:45:49 -070078 OutputFile() android.OutputPath
Jooyung Han39edb6c2019-11-06 16:53:07 +090079}
80
Jiyong Park5a8d1be2018-04-25 22:57:34 +090081type PrebuiltEtc struct {
Jaewoong Jung4b79e982020-06-01 10:45:49 -070082 android.ModuleBase
Jiyong Parkc678ad32018-04-10 13:07:10 +090083
84 properties prebuiltEtcProperties
85
Jaewoong Jung4b79e982020-06-01 10:45:49 -070086 sourceFilePath android.Path
87 outputFilePath android.OutputPath
Jaewoong Jungc3fcdb42019-02-13 05:50:33 -080088 // The base install location, e.g. "etc" for prebuilt_etc, "usr/share" for prebuilt_usr_share.
Patrice Arruda057a8b12019-06-03 15:29:27 -070089 installDirBase string
90 // The base install location when soc_specific property is set to true, e.g. "firmware" for prebuilt_firmware.
91 socInstallDirBase string
Jaewoong Jung4b79e982020-06-01 10:45:49 -070092 installDirPath android.InstallPath
93 additionalDependencies *android.Paths
Jiyong Parkc678ad32018-04-10 13:07:10 +090094}
95
Yifan Hong1b3348d2020-01-21 15:53:22 -080096func (p *PrebuiltEtc) inRamdisk() bool {
97 return p.ModuleBase.InRamdisk() || p.ModuleBase.InstallInRamdisk()
98}
99
100func (p *PrebuiltEtc) onlyInRamdisk() bool {
101 return p.ModuleBase.InstallInRamdisk()
102}
103
104func (p *PrebuiltEtc) InstallInRamdisk() bool {
105 return p.inRamdisk()
106}
107
Tao Bao0ba5c942018-08-14 22:20:22 -0700108func (p *PrebuiltEtc) inRecovery() bool {
Colin Cross7228ecd2019-11-18 16:00:16 -0800109 return p.ModuleBase.InRecovery() || p.ModuleBase.InstallInRecovery()
Tao Bao0ba5c942018-08-14 22:20:22 -0700110}
111
112func (p *PrebuiltEtc) onlyInRecovery() bool {
113 return p.ModuleBase.InstallInRecovery()
114}
115
116func (p *PrebuiltEtc) InstallInRecovery() bool {
117 return p.inRecovery()
118}
119
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700120var _ android.ImageInterface = (*PrebuiltEtc)(nil)
Colin Cross7228ecd2019-11-18 16:00:16 -0800121
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700122func (p *PrebuiltEtc) ImageMutatorBegin(ctx android.BaseModuleContext) {}
Colin Cross7228ecd2019-11-18 16:00:16 -0800123
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700124func (p *PrebuiltEtc) CoreVariantNeeded(ctx android.BaseModuleContext) bool {
Yifan Hong1b3348d2020-01-21 15:53:22 -0800125 return !p.ModuleBase.InstallInRecovery() && !p.ModuleBase.InstallInRamdisk()
126}
127
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700128func (p *PrebuiltEtc) RamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
129 return proptools.Bool(p.properties.Ramdisk_available) || p.ModuleBase.InstallInRamdisk()
Colin Cross7228ecd2019-11-18 16:00:16 -0800130}
131
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700132func (p *PrebuiltEtc) RecoveryVariantNeeded(ctx android.BaseModuleContext) bool {
133 return proptools.Bool(p.properties.Recovery_available) || p.ModuleBase.InstallInRecovery()
Colin Cross7228ecd2019-11-18 16:00:16 -0800134}
135
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700136func (p *PrebuiltEtc) ExtraImageVariations(ctx android.BaseModuleContext) []string {
Colin Cross7228ecd2019-11-18 16:00:16 -0800137 return nil
138}
139
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700140func (p *PrebuiltEtc) SetImageVariation(ctx android.BaseModuleContext, variation string, module android.Module) {
Colin Cross7228ecd2019-11-18 16:00:16 -0800141}
142
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700143func (p *PrebuiltEtc) DepsMutator(ctx android.BottomUpMutatorContext) {
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900144 if p.properties.Src == nil {
145 ctx.PropertyErrorf("src", "missing prebuilt source file")
Jiyong Parkc678ad32018-04-10 13:07:10 +0900146 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900147}
148
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700149func (p *PrebuiltEtc) SourceFilePath(ctx android.ModuleContext) android.Path {
150 return android.PathForModuleSrc(ctx, android.String(p.properties.Src))
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900151}
152
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700153func (p *PrebuiltEtc) InstallDirPath() android.InstallPath {
Jooyung Hana0171822019-07-22 15:48:36 +0900154 return p.installDirPath
155}
156
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900157// This allows other derivative modules (e.g. prebuilt_etc_xml) to perform
158// additional steps (like validating the src) before the file is installed.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700159func (p *PrebuiltEtc) SetAdditionalDependencies(paths android.Paths) {
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900160 p.additionalDependencies = &paths
161}
162
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700163func (p *PrebuiltEtc) OutputFile() android.OutputPath {
Jiyong Parkc43e0ac2018-10-04 20:27:15 +0900164 return p.outputFilePath
165}
166
167func (p *PrebuiltEtc) SubDir() string {
Liz Kammer0449a632020-06-26 10:12:36 -0700168 if subDir := proptools.String(p.properties.Sub_dir); subDir != "" {
169 return subDir
170 }
171 return proptools.String(p.properties.Relative_install_path)
Jiyong Parkc43e0ac2018-10-04 20:27:15 +0900172}
173
Jooyung Han0703fd82020-08-26 22:11:53 +0900174func (p *PrebuiltEtc) BaseDir() string {
Jooyung Han8e5685d2020-09-21 11:02:57 +0900175 return p.installDirBase
Jooyung Han0703fd82020-08-26 22:11:53 +0900176}
177
Jiyong Parkad9ce042018-10-31 22:49:57 +0900178func (p *PrebuiltEtc) Installable() bool {
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700179 return p.properties.Installable == nil || android.Bool(p.properties.Installable)
Jiyong Parkad9ce042018-10-31 22:49:57 +0900180}
181
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700182func (p *PrebuiltEtc) GenerateAndroidBuildActions(ctx android.ModuleContext) {
183 p.sourceFilePath = android.PathForModuleSrc(ctx, android.String(p.properties.Src))
184 filename := android.String(p.properties.Filename)
185 filename_from_src := android.Bool(p.properties.Filename_from_src)
Jiyong Park139a2e62018-10-26 21:49:39 +0900186 if filename == "" {
Jiyong Park1a7cf082018-11-13 11:59:12 +0900187 if filename_from_src {
188 filename = p.sourceFilePath.Base()
189 } else {
190 filename = ctx.ModuleName()
191 }
192 } else if filename_from_src {
193 ctx.PropertyErrorf("filename_from_src", "filename is set. filename_from_src can't be true")
194 return
Jiyong Park139a2e62018-10-26 21:49:39 +0900195 }
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700196 p.outputFilePath = android.PathForModuleOut(ctx, filename).OutputPath
Patrice Arruda057a8b12019-06-03 15:29:27 -0700197
Liz Kammer0449a632020-06-26 10:12:36 -0700198 if p.properties.Sub_dir != nil && p.properties.Relative_install_path != nil {
199 ctx.PropertyErrorf("sub_dir", "relative_install_path is set. Cannot set sub_dir")
200 }
201
Jooyung Han8e5685d2020-09-21 11:02:57 +0900202 // If soc install dir was specified and SOC specific is set, set the installDirPath to the specified
203 // socInstallDirBase.
204 installBaseDir := p.installDirBase
205 if p.SocSpecific() && p.socInstallDirBase != "" {
206 installBaseDir = p.socInstallDirBase
207 }
208 p.installDirPath = android.PathForModuleInstall(ctx, installBaseDir, p.SubDir())
Jiyong Parkc43e0ac2018-10-04 20:27:15 +0900209
Dan Willemsenb0552672019-01-25 16:04:11 -0800210 // This ensures that outputFilePath has the correct name for others to
211 // use, as the source file may have a different name.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700212 ctx.Build(pctx, android.BuildParams{
213 Rule: android.Cp,
Jiyong Parkc43e0ac2018-10-04 20:27:15 +0900214 Output: p.outputFilePath,
215 Input: p.sourceFilePath,
216 })
Jiyong Parkf9f68052020-09-29 20:15:08 +0900217
218 if p.Installable() {
219 installPath := ctx.InstallFile(p.installDirPath, p.outputFilePath.Base(), p.outputFilePath)
220 for _, sl := range p.properties.Symlinks {
221 ctx.InstallSymlink(p.installDirPath, sl, installPath)
222 }
223 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900224}
225
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700226func (p *PrebuiltEtc) AndroidMkEntries() []android.AndroidMkEntries {
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700227 nameSuffix := ""
Yifan Hong1b3348d2020-01-21 15:53:22 -0800228 if p.inRamdisk() && !p.onlyInRamdisk() {
229 nameSuffix = ".ramdisk"
230 }
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700231 if p.inRecovery() && !p.onlyInRecovery() {
232 nameSuffix = ".recovery"
233 }
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700234 return []android.AndroidMkEntries{android.AndroidMkEntries{
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700235 Class: "ETC",
236 SubName: nameSuffix,
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700237 OutputFile: android.OptionalPathForPath(p.outputFilePath),
238 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
239 func(entries *android.AndroidMkEntries) {
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700240 entries.SetString("LOCAL_MODULE_TAGS", "optional")
Colin Crossff6c33d2019-10-02 16:01:35 -0700241 entries.SetString("LOCAL_MODULE_PATH", p.installDirPath.ToMakePath().String())
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700242 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", p.outputFilePath.Base())
Yo Chiang3d64d492020-05-27 17:56:39 +0800243 if len(p.properties.Symlinks) > 0 {
244 entries.AddStrings("LOCAL_MODULE_SYMLINKS", p.properties.Symlinks...)
245 }
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700246 entries.SetString("LOCAL_UNINSTALLABLE_MODULE", strconv.FormatBool(!p.Installable()))
247 if p.additionalDependencies != nil {
248 for _, path := range *p.additionalDependencies {
Yo Chiang3d64d492020-05-27 17:56:39 +0800249 entries.AddStrings("LOCAL_ADDITIONAL_DEPENDENCIES", path.String())
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700250 }
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900251 }
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700252 },
Jiyong Parkc678ad32018-04-10 13:07:10 +0900253 },
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900254 }}
Jiyong Parkc678ad32018-04-10 13:07:10 +0900255}
256
Jooyung Hana0171822019-07-22 15:48:36 +0900257func InitPrebuiltEtcModule(p *PrebuiltEtc, dirBase string) {
258 p.installDirBase = dirBase
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900259 p.AddProperties(&p.properties)
260}
Jiyong Parkc678ad32018-04-10 13:07:10 +0900261
Patrice Arruda9e14b962019-03-11 15:58:50 -0700262// prebuilt_etc is for a prebuilt artifact that is installed in
263// <partition>/etc/<sub_dir> directory.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700264func PrebuiltEtcFactory() android.Module {
Jooyung Hana0171822019-07-22 15:48:36 +0900265 module := &PrebuiltEtc{}
266 InitPrebuiltEtcModule(module, "etc")
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900267 // This module is device-only
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700268 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibFirst)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900269 return module
270}
Tao Bao0ba5c942018-08-14 22:20:22 -0700271
Patrice Arruda9e14b962019-03-11 15:58:50 -0700272// prebuilt_etc_host is for a host prebuilt artifact that is installed in
273// $(HOST_OUT)/etc/<sub_dir> directory.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700274func PrebuiltEtcHostFactory() android.Module {
Jooyung Hana0171822019-07-22 15:48:36 +0900275 module := &PrebuiltEtc{}
276 InitPrebuiltEtcModule(module, "etc")
Jaewoong Jung24788182019-02-04 14:34:10 -0800277 // This module is host-only
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700278 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommon)
Jaewoong Jung24788182019-02-04 14:34:10 -0800279 return module
280}
281
Patrice Arruda9e14b962019-03-11 15:58:50 -0700282// prebuilt_usr_share is for a prebuilt artifact that is installed in
283// <partition>/usr/share/<sub_dir> directory.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700284func PrebuiltUserShareFactory() android.Module {
Jooyung Hana0171822019-07-22 15:48:36 +0900285 module := &PrebuiltEtc{}
286 InitPrebuiltEtcModule(module, "usr/share")
Jaewoong Jungc3fcdb42019-02-13 05:50:33 -0800287 // This module is device-only
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700288 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibFirst)
Jaewoong Jungc3fcdb42019-02-13 05:50:33 -0800289 return module
290}
291
Patrice Arruda9e14b962019-03-11 15:58:50 -0700292// prebuild_usr_share_host is for a host prebuilt artifact that is installed in
293// $(HOST_OUT)/usr/share/<sub_dir> directory.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700294func PrebuiltUserShareHostFactory() android.Module {
Jooyung Hana0171822019-07-22 15:48:36 +0900295 module := &PrebuiltEtc{}
296 InitPrebuiltEtcModule(module, "usr/share")
Patrice Arruda300cef92019-02-22 15:47:57 -0800297 // This module is host-only
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700298 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommon)
Patrice Arruda300cef92019-02-22 15:47:57 -0800299 return module
300}
301
Patrice Arruda61583eb2019-05-14 08:20:45 -0700302// prebuilt_font installs a font in <partition>/fonts directory.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700303func PrebuiltFontFactory() android.Module {
Jooyung Hana0171822019-07-22 15:48:36 +0900304 module := &PrebuiltEtc{}
305 InitPrebuiltEtcModule(module, "fonts")
Patrice Arruda61583eb2019-05-14 08:20:45 -0700306 // This module is device-only
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700307 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibFirst)
Patrice Arruda61583eb2019-05-14 08:20:45 -0700308 return module
309}
Patrice Arruda057a8b12019-06-03 15:29:27 -0700310
311// prebuilt_firmware installs a firmware file to <partition>/etc/firmware directory for system image.
312// If soc_specific property is set to true, the firmware file is installed to the vendor <partition>/firmware
313// directory for vendor image.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700314func PrebuiltFirmwareFactory() android.Module {
Jooyung Hana0171822019-07-22 15:48:36 +0900315 module := &PrebuiltEtc{}
316 module.socInstallDirBase = "firmware"
317 InitPrebuiltEtcModule(module, "etc/firmware")
Patrice Arruda057a8b12019-06-03 15:29:27 -0700318 // This module is device-only
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700319 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibFirst)
Patrice Arruda057a8b12019-06-03 15:29:27 -0700320 return module
321}
Patrice Arruda0f688002020-06-08 21:40:25 +0000322
323// prebuilt_dsp installs a DSP related file to <partition>/etc/dsp directory for system image.
324// If soc_specific property is set to true, the DSP related file is installed to the vendor <partition>/dsp
325// directory for vendor image.
326func PrebuiltDSPFactory() android.Module {
327 module := &PrebuiltEtc{}
328 module.socInstallDirBase = "dsp"
329 InitPrebuiltEtcModule(module, "etc/dsp")
330 // This module is device-only
331 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibFirst)
332 return module
333}