blob: f0c0767b6a1d064d7b940e68873940bdb9b12b3a [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
15package android
16
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -070017import "strconv"
Jiyong Parkc678ad32018-04-10 13:07:10 +090018
Jaewoong Jungc3fcdb42019-02-13 05:50:33 -080019// TODO(jungw): Now that it handles more than the ones in etc/, consider renaming this file.
Jiyong Parkc678ad32018-04-10 13:07:10 +090020
21func init() {
22 RegisterModuleType("prebuilt_etc", PrebuiltEtcFactory)
Jaewoong Jung4b44fcd2019-02-07 08:28:03 -080023 RegisterModuleType("prebuilt_etc_host", PrebuiltEtcHostFactory)
Jaewoong Jungc3fcdb42019-02-13 05:50:33 -080024 RegisterModuleType("prebuilt_usr_share", PrebuiltUserShareFactory)
Patrice Arruda300cef92019-02-22 15:47:57 -080025 RegisterModuleType("prebuilt_usr_share_host", PrebuiltUserShareHostFactory)
Patrice Arruda61583eb2019-05-14 08:20:45 -070026 RegisterModuleType("prebuilt_font", PrebuiltFontFactory)
Patrice Arruda057a8b12019-06-03 15:29:27 -070027 RegisterModuleType("prebuilt_firmware", PrebuiltFirmwareFactory)
Jiyong Parkc678ad32018-04-10 13:07:10 +090028}
29
30type prebuiltEtcProperties struct {
31 // Source file of this prebuilt.
Colin Cross27b922f2019-03-04 22:35:41 -080032 Src *string `android:"path,arch_variant"`
Jiyong Parkc678ad32018-04-10 13:07:10 +090033
34 // optional subdirectory under which this file is installed into
35 Sub_dir *string `android:"arch_variant"`
Tao Bao0ba5c942018-08-14 22:20:22 -070036
Jiyong Park139a2e62018-10-26 21:49:39 +090037 // optional name for the installed file. If unspecified, name of the module is used as the file name
38 Filename *string `android:"arch_variant"`
39
Jiyong Park1a7cf082018-11-13 11:59:12 +090040 // when set to true, and filename property is not set, the name for the installed file
41 // is the same as the file name of the source file.
42 Filename_from_src *bool `android:"arch_variant"`
43
Yifan Hong1b3348d2020-01-21 15:53:22 -080044 // Make this module available when building for ramdisk.
45 Ramdisk_available *bool
46
Tao Bao0ba5c942018-08-14 22:20:22 -070047 // Make this module available when building for recovery.
48 Recovery_available *bool
49
Jiyong Parkad9ce042018-10-31 22:49:57 +090050 // Whether this module is directly installable to one of the partitions. Default: true.
51 Installable *bool
Yo Chiang3d64d492020-05-27 17:56:39 +080052
53 // Install symlinks to the installed file.
54 Symlinks []string `android:"arch_variant"`
Jiyong Parkc678ad32018-04-10 13:07:10 +090055}
56
Jooyung Han39edb6c2019-11-06 16:53:07 +090057type PrebuiltEtcModule interface {
58 Module
59 SubDir() string
60 OutputFile() OutputPath
61}
62
Jiyong Park5a8d1be2018-04-25 22:57:34 +090063type PrebuiltEtc struct {
Jiyong Parkc678ad32018-04-10 13:07:10 +090064 ModuleBase
Jiyong Parkc678ad32018-04-10 13:07:10 +090065
66 properties prebuiltEtcProperties
67
Jaewoong Jungc3fcdb42019-02-13 05:50:33 -080068 sourceFilePath Path
69 outputFilePath OutputPath
70 // The base install location, e.g. "etc" for prebuilt_etc, "usr/share" for prebuilt_usr_share.
Patrice Arruda057a8b12019-06-03 15:29:27 -070071 installDirBase string
72 // The base install location when soc_specific property is set to true, e.g. "firmware" for prebuilt_firmware.
73 socInstallDirBase string
Colin Cross70dda7e2019-10-01 22:05:35 -070074 installDirPath InstallPath
Jiyong Park5a8d1be2018-04-25 22:57:34 +090075 additionalDependencies *Paths
Jiyong Parkc678ad32018-04-10 13:07:10 +090076}
77
Yifan Hong1b3348d2020-01-21 15:53:22 -080078func (p *PrebuiltEtc) inRamdisk() bool {
79 return p.ModuleBase.InRamdisk() || p.ModuleBase.InstallInRamdisk()
80}
81
82func (p *PrebuiltEtc) onlyInRamdisk() bool {
83 return p.ModuleBase.InstallInRamdisk()
84}
85
86func (p *PrebuiltEtc) InstallInRamdisk() bool {
87 return p.inRamdisk()
88}
89
Tao Bao0ba5c942018-08-14 22:20:22 -070090func (p *PrebuiltEtc) inRecovery() bool {
Colin Cross7228ecd2019-11-18 16:00:16 -080091 return p.ModuleBase.InRecovery() || p.ModuleBase.InstallInRecovery()
Tao Bao0ba5c942018-08-14 22:20:22 -070092}
93
94func (p *PrebuiltEtc) onlyInRecovery() bool {
95 return p.ModuleBase.InstallInRecovery()
96}
97
98func (p *PrebuiltEtc) InstallInRecovery() bool {
99 return p.inRecovery()
100}
101
Colin Cross7228ecd2019-11-18 16:00:16 -0800102var _ ImageInterface = (*PrebuiltEtc)(nil)
103
104func (p *PrebuiltEtc) ImageMutatorBegin(ctx BaseModuleContext) {}
105
106func (p *PrebuiltEtc) CoreVariantNeeded(ctx BaseModuleContext) bool {
Yifan Hong1b3348d2020-01-21 15:53:22 -0800107 return !p.ModuleBase.InstallInRecovery() && !p.ModuleBase.InstallInRamdisk()
108}
109
110func (p *PrebuiltEtc) RamdiskVariantNeeded(ctx BaseModuleContext) bool {
111 return Bool(p.properties.Ramdisk_available) || p.ModuleBase.InstallInRamdisk()
Colin Cross7228ecd2019-11-18 16:00:16 -0800112}
113
114func (p *PrebuiltEtc) RecoveryVariantNeeded(ctx BaseModuleContext) bool {
115 return Bool(p.properties.Recovery_available) || p.ModuleBase.InstallInRecovery()
116}
117
118func (p *PrebuiltEtc) ExtraImageVariations(ctx BaseModuleContext) []string {
119 return nil
120}
121
122func (p *PrebuiltEtc) SetImageVariation(ctx BaseModuleContext, variation string, module Module) {
123}
124
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900125func (p *PrebuiltEtc) DepsMutator(ctx BottomUpMutatorContext) {
126 if p.properties.Src == nil {
127 ctx.PropertyErrorf("src", "missing prebuilt source file")
Jiyong Parkc678ad32018-04-10 13:07:10 +0900128 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900129}
130
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900131func (p *PrebuiltEtc) SourceFilePath(ctx ModuleContext) Path {
Colin Cross8a497952019-03-05 22:25:09 -0800132 return PathForModuleSrc(ctx, String(p.properties.Src))
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900133}
134
Colin Cross70dda7e2019-10-01 22:05:35 -0700135func (p *PrebuiltEtc) InstallDirPath() InstallPath {
Jooyung Hana0171822019-07-22 15:48:36 +0900136 return p.installDirPath
137}
138
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900139// This allows other derivative modules (e.g. prebuilt_etc_xml) to perform
140// additional steps (like validating the src) before the file is installed.
141func (p *PrebuiltEtc) SetAdditionalDependencies(paths Paths) {
142 p.additionalDependencies = &paths
143}
144
Jiyong Parkc43e0ac2018-10-04 20:27:15 +0900145func (p *PrebuiltEtc) OutputFile() OutputPath {
146 return p.outputFilePath
147}
148
149func (p *PrebuiltEtc) SubDir() string {
150 return String(p.properties.Sub_dir)
151}
152
Jiyong Parkad9ce042018-10-31 22:49:57 +0900153func (p *PrebuiltEtc) Installable() bool {
154 return p.properties.Installable == nil || Bool(p.properties.Installable)
155}
156
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900157func (p *PrebuiltEtc) GenerateAndroidBuildActions(ctx ModuleContext) {
Colin Cross8a497952019-03-05 22:25:09 -0800158 p.sourceFilePath = PathForModuleSrc(ctx, String(p.properties.Src))
Jiyong Park139a2e62018-10-26 21:49:39 +0900159 filename := String(p.properties.Filename)
Jiyong Park1a7cf082018-11-13 11:59:12 +0900160 filename_from_src := Bool(p.properties.Filename_from_src)
Jiyong Park139a2e62018-10-26 21:49:39 +0900161 if filename == "" {
Jiyong Park1a7cf082018-11-13 11:59:12 +0900162 if filename_from_src {
163 filename = p.sourceFilePath.Base()
164 } else {
165 filename = ctx.ModuleName()
166 }
167 } else if filename_from_src {
168 ctx.PropertyErrorf("filename_from_src", "filename is set. filename_from_src can't be true")
169 return
Jiyong Park139a2e62018-10-26 21:49:39 +0900170 }
171 p.outputFilePath = PathForModuleOut(ctx, filename).OutputPath
Patrice Arruda057a8b12019-06-03 15:29:27 -0700172
173 // If soc install dir was specified and SOC specific is set, set the installDirPath to the specified
174 // socInstallDirBase.
175 installBaseDir := p.installDirBase
176 if ctx.SocSpecific() && p.socInstallDirBase != "" {
177 installBaseDir = p.socInstallDirBase
178 }
179 p.installDirPath = PathForModuleInstall(ctx, installBaseDir, String(p.properties.Sub_dir))
Jiyong Parkc43e0ac2018-10-04 20:27:15 +0900180
Dan Willemsenb0552672019-01-25 16:04:11 -0800181 // This ensures that outputFilePath has the correct name for others to
182 // use, as the source file may have a different name.
Jiyong Parkc43e0ac2018-10-04 20:27:15 +0900183 ctx.Build(pctx, BuildParams{
184 Rule: Cp,
185 Output: p.outputFilePath,
186 Input: p.sourceFilePath,
187 })
Jiyong Parkc678ad32018-04-10 13:07:10 +0900188}
189
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900190func (p *PrebuiltEtc) AndroidMkEntries() []AndroidMkEntries {
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700191 nameSuffix := ""
Yifan Hong1b3348d2020-01-21 15:53:22 -0800192 if p.inRamdisk() && !p.onlyInRamdisk() {
193 nameSuffix = ".ramdisk"
194 }
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700195 if p.inRecovery() && !p.onlyInRecovery() {
196 nameSuffix = ".recovery"
197 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900198 return []AndroidMkEntries{AndroidMkEntries{
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700199 Class: "ETC",
200 SubName: nameSuffix,
201 OutputFile: OptionalPathForPath(p.outputFilePath),
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700202 ExtraEntries: []AndroidMkExtraEntriesFunc{
203 func(entries *AndroidMkEntries) {
204 entries.SetString("LOCAL_MODULE_TAGS", "optional")
Colin Crossff6c33d2019-10-02 16:01:35 -0700205 entries.SetString("LOCAL_MODULE_PATH", p.installDirPath.ToMakePath().String())
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700206 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", p.outputFilePath.Base())
Yo Chiang3d64d492020-05-27 17:56:39 +0800207 if len(p.properties.Symlinks) > 0 {
208 entries.AddStrings("LOCAL_MODULE_SYMLINKS", p.properties.Symlinks...)
209 }
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700210 entries.SetString("LOCAL_UNINSTALLABLE_MODULE", strconv.FormatBool(!p.Installable()))
211 if p.additionalDependencies != nil {
212 for _, path := range *p.additionalDependencies {
Yo Chiang3d64d492020-05-27 17:56:39 +0800213 entries.AddStrings("LOCAL_ADDITIONAL_DEPENDENCIES", path.String())
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700214 }
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900215 }
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700216 },
Jiyong Parkc678ad32018-04-10 13:07:10 +0900217 },
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900218 }}
Jiyong Parkc678ad32018-04-10 13:07:10 +0900219}
220
Jooyung Hana0171822019-07-22 15:48:36 +0900221func InitPrebuiltEtcModule(p *PrebuiltEtc, dirBase string) {
222 p.installDirBase = dirBase
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900223 p.AddProperties(&p.properties)
224}
Jiyong Parkc678ad32018-04-10 13:07:10 +0900225
Patrice Arruda9e14b962019-03-11 15:58:50 -0700226// prebuilt_etc is for a prebuilt artifact that is installed in
227// <partition>/etc/<sub_dir> directory.
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900228func PrebuiltEtcFactory() Module {
Jooyung Hana0171822019-07-22 15:48:36 +0900229 module := &PrebuiltEtc{}
230 InitPrebuiltEtcModule(module, "etc")
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900231 // This module is device-only
Jaewoong Jungb9a11512019-01-15 10:47:05 -0800232 InitAndroidArchModule(module, DeviceSupported, MultilibFirst)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900233 return module
234}
Tao Bao0ba5c942018-08-14 22:20:22 -0700235
Patrice Arruda9e14b962019-03-11 15:58:50 -0700236// prebuilt_etc_host is for a host prebuilt artifact that is installed in
237// $(HOST_OUT)/etc/<sub_dir> directory.
Jaewoong Jung4b44fcd2019-02-07 08:28:03 -0800238func PrebuiltEtcHostFactory() Module {
Jooyung Hana0171822019-07-22 15:48:36 +0900239 module := &PrebuiltEtc{}
240 InitPrebuiltEtcModule(module, "etc")
Jaewoong Jung24788182019-02-04 14:34:10 -0800241 // This module is host-only
242 InitAndroidArchModule(module, HostSupported, MultilibCommon)
243 return module
244}
245
Patrice Arruda9e14b962019-03-11 15:58:50 -0700246// prebuilt_usr_share is for a prebuilt artifact that is installed in
247// <partition>/usr/share/<sub_dir> directory.
Jaewoong Jungc3fcdb42019-02-13 05:50:33 -0800248func PrebuiltUserShareFactory() Module {
Jooyung Hana0171822019-07-22 15:48:36 +0900249 module := &PrebuiltEtc{}
250 InitPrebuiltEtcModule(module, "usr/share")
Jaewoong Jungc3fcdb42019-02-13 05:50:33 -0800251 // This module is device-only
252 InitAndroidArchModule(module, DeviceSupported, MultilibFirst)
253 return module
254}
255
Patrice Arruda9e14b962019-03-11 15:58:50 -0700256// prebuild_usr_share_host is for a host prebuilt artifact that is installed in
257// $(HOST_OUT)/usr/share/<sub_dir> directory.
Patrice Arruda300cef92019-02-22 15:47:57 -0800258func PrebuiltUserShareHostFactory() Module {
Jooyung Hana0171822019-07-22 15:48:36 +0900259 module := &PrebuiltEtc{}
260 InitPrebuiltEtcModule(module, "usr/share")
Patrice Arruda300cef92019-02-22 15:47:57 -0800261 // This module is host-only
262 InitAndroidArchModule(module, HostSupported, MultilibCommon)
263 return module
264}
265
Patrice Arruda61583eb2019-05-14 08:20:45 -0700266// prebuilt_font installs a font in <partition>/fonts directory.
267func PrebuiltFontFactory() Module {
Jooyung Hana0171822019-07-22 15:48:36 +0900268 module := &PrebuiltEtc{}
269 InitPrebuiltEtcModule(module, "fonts")
Patrice Arruda61583eb2019-05-14 08:20:45 -0700270 // This module is device-only
271 InitAndroidArchModule(module, DeviceSupported, MultilibFirst)
272 return module
273}
Patrice Arruda057a8b12019-06-03 15:29:27 -0700274
275// prebuilt_firmware installs a firmware file to <partition>/etc/firmware directory for system image.
276// If soc_specific property is set to true, the firmware file is installed to the vendor <partition>/firmware
277// directory for vendor image.
278func PrebuiltFirmwareFactory() Module {
Jooyung Hana0171822019-07-22 15:48:36 +0900279 module := &PrebuiltEtc{}
280 module.socInstallDirBase = "firmware"
281 InitPrebuiltEtcModule(module, "etc/firmware")
Patrice Arruda057a8b12019-06-03 15:29:27 -0700282 // This module is device-only
283 InitAndroidArchModule(module, DeviceSupported, MultilibFirst)
284 return module
285}