blob: d6eb008e407fe1c68d04d58197dfe6988a57b900 [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")
31
32 android.RegisterModuleType("prebuilt_etc", PrebuiltEtcFactory)
33 android.RegisterModuleType("prebuilt_etc_host", PrebuiltEtcHostFactory)
34 android.RegisterModuleType("prebuilt_usr_share", PrebuiltUserShareFactory)
35 android.RegisterModuleType("prebuilt_usr_share_host", PrebuiltUserShareHostFactory)
36 android.RegisterModuleType("prebuilt_font", PrebuiltFontFactory)
37 android.RegisterModuleType("prebuilt_firmware", PrebuiltFirmwareFactory)
Jiyong Parkc678ad32018-04-10 13:07:10 +090038}
39
40type prebuiltEtcProperties struct {
41 // Source file of this prebuilt.
Colin Cross27b922f2019-03-04 22:35:41 -080042 Src *string `android:"path,arch_variant"`
Jiyong Parkc678ad32018-04-10 13:07:10 +090043
44 // optional subdirectory under which this file is installed into
45 Sub_dir *string `android:"arch_variant"`
Tao Bao0ba5c942018-08-14 22:20:22 -070046
Jiyong Park139a2e62018-10-26 21:49:39 +090047 // optional name for the installed file. If unspecified, name of the module is used as the file name
48 Filename *string `android:"arch_variant"`
49
Jiyong Park1a7cf082018-11-13 11:59:12 +090050 // when set to true, and filename property is not set, the name for the installed file
51 // is the same as the file name of the source file.
52 Filename_from_src *bool `android:"arch_variant"`
53
Yifan Hong1b3348d2020-01-21 15:53:22 -080054 // Make this module available when building for ramdisk.
55 Ramdisk_available *bool
56
Tao Bao0ba5c942018-08-14 22:20:22 -070057 // Make this module available when building for recovery.
58 Recovery_available *bool
59
Jiyong Parkad9ce042018-10-31 22:49:57 +090060 // Whether this module is directly installable to one of the partitions. Default: true.
61 Installable *bool
Yo Chiang3d64d492020-05-27 17:56:39 +080062
63 // Install symlinks to the installed file.
64 Symlinks []string `android:"arch_variant"`
Jiyong Parkc678ad32018-04-10 13:07:10 +090065}
66
Jooyung Han39edb6c2019-11-06 16:53:07 +090067type PrebuiltEtcModule interface {
Jaewoong Jung4b79e982020-06-01 10:45:49 -070068 android.Module
Jooyung Han39edb6c2019-11-06 16:53:07 +090069 SubDir() string
Jaewoong Jung4b79e982020-06-01 10:45:49 -070070 OutputFile() android.OutputPath
Jooyung Han39edb6c2019-11-06 16:53:07 +090071}
72
Jiyong Park5a8d1be2018-04-25 22:57:34 +090073type PrebuiltEtc struct {
Jaewoong Jung4b79e982020-06-01 10:45:49 -070074 android.ModuleBase
Jiyong Parkc678ad32018-04-10 13:07:10 +090075
76 properties prebuiltEtcProperties
77
Jaewoong Jung4b79e982020-06-01 10:45:49 -070078 sourceFilePath android.Path
79 outputFilePath android.OutputPath
Jaewoong Jungc3fcdb42019-02-13 05:50:33 -080080 // The base install location, e.g. "etc" for prebuilt_etc, "usr/share" for prebuilt_usr_share.
Patrice Arruda057a8b12019-06-03 15:29:27 -070081 installDirBase string
82 // The base install location when soc_specific property is set to true, e.g. "firmware" for prebuilt_firmware.
83 socInstallDirBase string
Jaewoong Jung4b79e982020-06-01 10:45:49 -070084 installDirPath android.InstallPath
85 additionalDependencies *android.Paths
Jiyong Parkc678ad32018-04-10 13:07:10 +090086}
87
Yifan Hong1b3348d2020-01-21 15:53:22 -080088func (p *PrebuiltEtc) inRamdisk() bool {
89 return p.ModuleBase.InRamdisk() || p.ModuleBase.InstallInRamdisk()
90}
91
92func (p *PrebuiltEtc) onlyInRamdisk() bool {
93 return p.ModuleBase.InstallInRamdisk()
94}
95
96func (p *PrebuiltEtc) InstallInRamdisk() bool {
97 return p.inRamdisk()
98}
99
Tao Bao0ba5c942018-08-14 22:20:22 -0700100func (p *PrebuiltEtc) inRecovery() bool {
Colin Cross7228ecd2019-11-18 16:00:16 -0800101 return p.ModuleBase.InRecovery() || p.ModuleBase.InstallInRecovery()
Tao Bao0ba5c942018-08-14 22:20:22 -0700102}
103
104func (p *PrebuiltEtc) onlyInRecovery() bool {
105 return p.ModuleBase.InstallInRecovery()
106}
107
108func (p *PrebuiltEtc) InstallInRecovery() bool {
109 return p.inRecovery()
110}
111
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700112var _ android.ImageInterface = (*PrebuiltEtc)(nil)
Colin Cross7228ecd2019-11-18 16:00:16 -0800113
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700114func (p *PrebuiltEtc) ImageMutatorBegin(ctx android.BaseModuleContext) {}
Colin Cross7228ecd2019-11-18 16:00:16 -0800115
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700116func (p *PrebuiltEtc) CoreVariantNeeded(ctx android.BaseModuleContext) bool {
Yifan Hong1b3348d2020-01-21 15:53:22 -0800117 return !p.ModuleBase.InstallInRecovery() && !p.ModuleBase.InstallInRamdisk()
118}
119
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700120func (p *PrebuiltEtc) RamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
121 return proptools.Bool(p.properties.Ramdisk_available) || p.ModuleBase.InstallInRamdisk()
Colin Cross7228ecd2019-11-18 16:00:16 -0800122}
123
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700124func (p *PrebuiltEtc) RecoveryVariantNeeded(ctx android.BaseModuleContext) bool {
125 return proptools.Bool(p.properties.Recovery_available) || p.ModuleBase.InstallInRecovery()
Colin Cross7228ecd2019-11-18 16:00:16 -0800126}
127
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700128func (p *PrebuiltEtc) ExtraImageVariations(ctx android.BaseModuleContext) []string {
Colin Cross7228ecd2019-11-18 16:00:16 -0800129 return nil
130}
131
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700132func (p *PrebuiltEtc) SetImageVariation(ctx android.BaseModuleContext, variation string, module android.Module) {
Colin Cross7228ecd2019-11-18 16:00:16 -0800133}
134
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700135func (p *PrebuiltEtc) DepsMutator(ctx android.BottomUpMutatorContext) {
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900136 if p.properties.Src == nil {
137 ctx.PropertyErrorf("src", "missing prebuilt source file")
Jiyong Parkc678ad32018-04-10 13:07:10 +0900138 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900139}
140
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700141func (p *PrebuiltEtc) SourceFilePath(ctx android.ModuleContext) android.Path {
142 return android.PathForModuleSrc(ctx, android.String(p.properties.Src))
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900143}
144
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700145func (p *PrebuiltEtc) InstallDirPath() android.InstallPath {
Jooyung Hana0171822019-07-22 15:48:36 +0900146 return p.installDirPath
147}
148
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900149// This allows other derivative modules (e.g. prebuilt_etc_xml) to perform
150// additional steps (like validating the src) before the file is installed.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700151func (p *PrebuiltEtc) SetAdditionalDependencies(paths android.Paths) {
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900152 p.additionalDependencies = &paths
153}
154
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700155func (p *PrebuiltEtc) OutputFile() android.OutputPath {
Jiyong Parkc43e0ac2018-10-04 20:27:15 +0900156 return p.outputFilePath
157}
158
159func (p *PrebuiltEtc) SubDir() string {
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700160 return android.String(p.properties.Sub_dir)
Jiyong Parkc43e0ac2018-10-04 20:27:15 +0900161}
162
Jiyong Parkad9ce042018-10-31 22:49:57 +0900163func (p *PrebuiltEtc) Installable() bool {
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700164 return p.properties.Installable == nil || android.Bool(p.properties.Installable)
Jiyong Parkad9ce042018-10-31 22:49:57 +0900165}
166
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700167func (p *PrebuiltEtc) GenerateAndroidBuildActions(ctx android.ModuleContext) {
168 p.sourceFilePath = android.PathForModuleSrc(ctx, android.String(p.properties.Src))
169 filename := android.String(p.properties.Filename)
170 filename_from_src := android.Bool(p.properties.Filename_from_src)
Jiyong Park139a2e62018-10-26 21:49:39 +0900171 if filename == "" {
Jiyong Park1a7cf082018-11-13 11:59:12 +0900172 if filename_from_src {
173 filename = p.sourceFilePath.Base()
174 } else {
175 filename = ctx.ModuleName()
176 }
177 } else if filename_from_src {
178 ctx.PropertyErrorf("filename_from_src", "filename is set. filename_from_src can't be true")
179 return
Jiyong Park139a2e62018-10-26 21:49:39 +0900180 }
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700181 p.outputFilePath = android.PathForModuleOut(ctx, filename).OutputPath
Patrice Arruda057a8b12019-06-03 15:29:27 -0700182
183 // If soc install dir was specified and SOC specific is set, set the installDirPath to the specified
184 // socInstallDirBase.
185 installBaseDir := p.installDirBase
186 if ctx.SocSpecific() && p.socInstallDirBase != "" {
187 installBaseDir = p.socInstallDirBase
188 }
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700189 p.installDirPath = android.PathForModuleInstall(ctx, installBaseDir, proptools.String(p.properties.Sub_dir))
Jiyong Parkc43e0ac2018-10-04 20:27:15 +0900190
Dan Willemsenb0552672019-01-25 16:04:11 -0800191 // This ensures that outputFilePath has the correct name for others to
192 // use, as the source file may have a different name.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700193 ctx.Build(pctx, android.BuildParams{
194 Rule: android.Cp,
Jiyong Parkc43e0ac2018-10-04 20:27:15 +0900195 Output: p.outputFilePath,
196 Input: p.sourceFilePath,
197 })
Jiyong Parkc678ad32018-04-10 13:07:10 +0900198}
199
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700200func (p *PrebuiltEtc) AndroidMkEntries() []android.AndroidMkEntries {
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700201 nameSuffix := ""
Yifan Hong1b3348d2020-01-21 15:53:22 -0800202 if p.inRamdisk() && !p.onlyInRamdisk() {
203 nameSuffix = ".ramdisk"
204 }
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700205 if p.inRecovery() && !p.onlyInRecovery() {
206 nameSuffix = ".recovery"
207 }
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700208 return []android.AndroidMkEntries{android.AndroidMkEntries{
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700209 Class: "ETC",
210 SubName: nameSuffix,
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700211 OutputFile: android.OptionalPathForPath(p.outputFilePath),
212 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
213 func(entries *android.AndroidMkEntries) {
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700214 entries.SetString("LOCAL_MODULE_TAGS", "optional")
Colin Crossff6c33d2019-10-02 16:01:35 -0700215 entries.SetString("LOCAL_MODULE_PATH", p.installDirPath.ToMakePath().String())
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700216 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", p.outputFilePath.Base())
Yo Chiang3d64d492020-05-27 17:56:39 +0800217 if len(p.properties.Symlinks) > 0 {
218 entries.AddStrings("LOCAL_MODULE_SYMLINKS", p.properties.Symlinks...)
219 }
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700220 entries.SetString("LOCAL_UNINSTALLABLE_MODULE", strconv.FormatBool(!p.Installable()))
221 if p.additionalDependencies != nil {
222 for _, path := range *p.additionalDependencies {
Yo Chiang3d64d492020-05-27 17:56:39 +0800223 entries.AddStrings("LOCAL_ADDITIONAL_DEPENDENCIES", path.String())
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700224 }
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900225 }
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700226 },
Jiyong Parkc678ad32018-04-10 13:07:10 +0900227 },
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900228 }}
Jiyong Parkc678ad32018-04-10 13:07:10 +0900229}
230
Jooyung Hana0171822019-07-22 15:48:36 +0900231func InitPrebuiltEtcModule(p *PrebuiltEtc, dirBase string) {
232 p.installDirBase = dirBase
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900233 p.AddProperties(&p.properties)
234}
Jiyong Parkc678ad32018-04-10 13:07:10 +0900235
Patrice Arruda9e14b962019-03-11 15:58:50 -0700236// prebuilt_etc is for a prebuilt artifact that is installed in
237// <partition>/etc/<sub_dir> directory.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700238func PrebuiltEtcFactory() android.Module {
Jooyung Hana0171822019-07-22 15:48:36 +0900239 module := &PrebuiltEtc{}
240 InitPrebuiltEtcModule(module, "etc")
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900241 // This module is device-only
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700242 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibFirst)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900243 return module
244}
Tao Bao0ba5c942018-08-14 22:20:22 -0700245
Patrice Arruda9e14b962019-03-11 15:58:50 -0700246// prebuilt_etc_host is for a host prebuilt artifact that is installed in
247// $(HOST_OUT)/etc/<sub_dir> directory.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700248func PrebuiltEtcHostFactory() android.Module {
Jooyung Hana0171822019-07-22 15:48:36 +0900249 module := &PrebuiltEtc{}
250 InitPrebuiltEtcModule(module, "etc")
Jaewoong Jung24788182019-02-04 14:34:10 -0800251 // This module is host-only
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700252 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommon)
Jaewoong Jung24788182019-02-04 14:34:10 -0800253 return module
254}
255
Patrice Arruda9e14b962019-03-11 15:58:50 -0700256// prebuilt_usr_share is for a prebuilt artifact that is installed in
257// <partition>/usr/share/<sub_dir> directory.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700258func PrebuiltUserShareFactory() android.Module {
Jooyung Hana0171822019-07-22 15:48:36 +0900259 module := &PrebuiltEtc{}
260 InitPrebuiltEtcModule(module, "usr/share")
Jaewoong Jungc3fcdb42019-02-13 05:50:33 -0800261 // This module is device-only
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700262 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibFirst)
Jaewoong Jungc3fcdb42019-02-13 05:50:33 -0800263 return module
264}
265
Patrice Arruda9e14b962019-03-11 15:58:50 -0700266// prebuild_usr_share_host is for a host prebuilt artifact that is installed in
267// $(HOST_OUT)/usr/share/<sub_dir> directory.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700268func PrebuiltUserShareHostFactory() android.Module {
Jooyung Hana0171822019-07-22 15:48:36 +0900269 module := &PrebuiltEtc{}
270 InitPrebuiltEtcModule(module, "usr/share")
Patrice Arruda300cef92019-02-22 15:47:57 -0800271 // This module is host-only
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700272 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommon)
Patrice Arruda300cef92019-02-22 15:47:57 -0800273 return module
274}
275
Patrice Arruda61583eb2019-05-14 08:20:45 -0700276// prebuilt_font installs a font in <partition>/fonts directory.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700277func PrebuiltFontFactory() android.Module {
Jooyung Hana0171822019-07-22 15:48:36 +0900278 module := &PrebuiltEtc{}
279 InitPrebuiltEtcModule(module, "fonts")
Patrice Arruda61583eb2019-05-14 08:20:45 -0700280 // This module is device-only
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700281 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibFirst)
Patrice Arruda61583eb2019-05-14 08:20:45 -0700282 return module
283}
Patrice Arruda057a8b12019-06-03 15:29:27 -0700284
285// prebuilt_firmware installs a firmware file to <partition>/etc/firmware directory for system image.
286// If soc_specific property is set to true, the firmware file is installed to the vendor <partition>/firmware
287// directory for vendor image.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700288func PrebuiltFirmwareFactory() android.Module {
Jooyung Hana0171822019-07-22 15:48:36 +0900289 module := &PrebuiltEtc{}
290 module.socInstallDirBase = "firmware"
291 InitPrebuiltEtcModule(module, "etc/firmware")
Patrice Arruda057a8b12019-06-03 15:29:27 -0700292 // This module is device-only
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700293 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibFirst)
Patrice Arruda057a8b12019-06-03 15:29:27 -0700294 return module
295}