blob: 3dea6d8f6d76e3f49fd34929665882d086d556c4 [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
Jiyong Parkc678ad32018-04-10 13:07:10 +090052}
53
Jooyung Han39edb6c2019-11-06 16:53:07 +090054type PrebuiltEtcModule interface {
55 Module
56 SubDir() string
57 OutputFile() OutputPath
58}
59
Jiyong Park5a8d1be2018-04-25 22:57:34 +090060type PrebuiltEtc struct {
Jiyong Parkc678ad32018-04-10 13:07:10 +090061 ModuleBase
Jiyong Parkc678ad32018-04-10 13:07:10 +090062
63 properties prebuiltEtcProperties
64
Jaewoong Jungc3fcdb42019-02-13 05:50:33 -080065 sourceFilePath Path
66 outputFilePath OutputPath
67 // The base install location, e.g. "etc" for prebuilt_etc, "usr/share" for prebuilt_usr_share.
Patrice Arruda057a8b12019-06-03 15:29:27 -070068 installDirBase string
69 // The base install location when soc_specific property is set to true, e.g. "firmware" for prebuilt_firmware.
70 socInstallDirBase string
Colin Cross70dda7e2019-10-01 22:05:35 -070071 installDirPath InstallPath
Jiyong Park5a8d1be2018-04-25 22:57:34 +090072 additionalDependencies *Paths
Jiyong Parkc678ad32018-04-10 13:07:10 +090073}
74
Yifan Hong1b3348d2020-01-21 15:53:22 -080075func (p *PrebuiltEtc) inRamdisk() bool {
76 return p.ModuleBase.InRamdisk() || p.ModuleBase.InstallInRamdisk()
77}
78
79func (p *PrebuiltEtc) onlyInRamdisk() bool {
80 return p.ModuleBase.InstallInRamdisk()
81}
82
83func (p *PrebuiltEtc) InstallInRamdisk() bool {
84 return p.inRamdisk()
85}
86
Tao Bao0ba5c942018-08-14 22:20:22 -070087func (p *PrebuiltEtc) inRecovery() bool {
Colin Cross7228ecd2019-11-18 16:00:16 -080088 return p.ModuleBase.InRecovery() || p.ModuleBase.InstallInRecovery()
Tao Bao0ba5c942018-08-14 22:20:22 -070089}
90
91func (p *PrebuiltEtc) onlyInRecovery() bool {
92 return p.ModuleBase.InstallInRecovery()
93}
94
95func (p *PrebuiltEtc) InstallInRecovery() bool {
96 return p.inRecovery()
97}
98
Colin Cross7228ecd2019-11-18 16:00:16 -080099var _ ImageInterface = (*PrebuiltEtc)(nil)
100
101func (p *PrebuiltEtc) ImageMutatorBegin(ctx BaseModuleContext) {}
102
103func (p *PrebuiltEtc) CoreVariantNeeded(ctx BaseModuleContext) bool {
Yifan Hong1b3348d2020-01-21 15:53:22 -0800104 return !p.ModuleBase.InstallInRecovery() && !p.ModuleBase.InstallInRamdisk()
105}
106
107func (p *PrebuiltEtc) RamdiskVariantNeeded(ctx BaseModuleContext) bool {
108 return Bool(p.properties.Ramdisk_available) || p.ModuleBase.InstallInRamdisk()
Colin Cross7228ecd2019-11-18 16:00:16 -0800109}
110
111func (p *PrebuiltEtc) RecoveryVariantNeeded(ctx BaseModuleContext) bool {
112 return Bool(p.properties.Recovery_available) || p.ModuleBase.InstallInRecovery()
113}
114
115func (p *PrebuiltEtc) ExtraImageVariations(ctx BaseModuleContext) []string {
116 return nil
117}
118
119func (p *PrebuiltEtc) SetImageVariation(ctx BaseModuleContext, variation string, module Module) {
120}
121
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900122func (p *PrebuiltEtc) DepsMutator(ctx BottomUpMutatorContext) {
123 if p.properties.Src == nil {
124 ctx.PropertyErrorf("src", "missing prebuilt source file")
Jiyong Parkc678ad32018-04-10 13:07:10 +0900125 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900126}
127
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900128func (p *PrebuiltEtc) SourceFilePath(ctx ModuleContext) Path {
Colin Cross8a497952019-03-05 22:25:09 -0800129 return PathForModuleSrc(ctx, String(p.properties.Src))
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900130}
131
Colin Cross70dda7e2019-10-01 22:05:35 -0700132func (p *PrebuiltEtc) InstallDirPath() InstallPath {
Jooyung Hana0171822019-07-22 15:48:36 +0900133 return p.installDirPath
134}
135
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900136// This allows other derivative modules (e.g. prebuilt_etc_xml) to perform
137// additional steps (like validating the src) before the file is installed.
138func (p *PrebuiltEtc) SetAdditionalDependencies(paths Paths) {
139 p.additionalDependencies = &paths
140}
141
Jiyong Parkc43e0ac2018-10-04 20:27:15 +0900142func (p *PrebuiltEtc) OutputFile() OutputPath {
143 return p.outputFilePath
144}
145
146func (p *PrebuiltEtc) SubDir() string {
147 return String(p.properties.Sub_dir)
148}
149
Jiyong Parkad9ce042018-10-31 22:49:57 +0900150func (p *PrebuiltEtc) Installable() bool {
151 return p.properties.Installable == nil || Bool(p.properties.Installable)
152}
153
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900154func (p *PrebuiltEtc) GenerateAndroidBuildActions(ctx ModuleContext) {
Colin Cross8a497952019-03-05 22:25:09 -0800155 p.sourceFilePath = PathForModuleSrc(ctx, String(p.properties.Src))
Jiyong Park139a2e62018-10-26 21:49:39 +0900156 filename := String(p.properties.Filename)
Jiyong Park1a7cf082018-11-13 11:59:12 +0900157 filename_from_src := Bool(p.properties.Filename_from_src)
Jiyong Park139a2e62018-10-26 21:49:39 +0900158 if filename == "" {
Jiyong Park1a7cf082018-11-13 11:59:12 +0900159 if filename_from_src {
160 filename = p.sourceFilePath.Base()
161 } else {
162 filename = ctx.ModuleName()
163 }
164 } else if filename_from_src {
165 ctx.PropertyErrorf("filename_from_src", "filename is set. filename_from_src can't be true")
166 return
Jiyong Park139a2e62018-10-26 21:49:39 +0900167 }
168 p.outputFilePath = PathForModuleOut(ctx, filename).OutputPath
Patrice Arruda057a8b12019-06-03 15:29:27 -0700169
170 // If soc install dir was specified and SOC specific is set, set the installDirPath to the specified
171 // socInstallDirBase.
172 installBaseDir := p.installDirBase
173 if ctx.SocSpecific() && p.socInstallDirBase != "" {
174 installBaseDir = p.socInstallDirBase
175 }
176 p.installDirPath = PathForModuleInstall(ctx, installBaseDir, String(p.properties.Sub_dir))
Jiyong Parkc43e0ac2018-10-04 20:27:15 +0900177
Dan Willemsenb0552672019-01-25 16:04:11 -0800178 // This ensures that outputFilePath has the correct name for others to
179 // use, as the source file may have a different name.
Jiyong Parkc43e0ac2018-10-04 20:27:15 +0900180 ctx.Build(pctx, BuildParams{
181 Rule: Cp,
182 Output: p.outputFilePath,
183 Input: p.sourceFilePath,
184 })
Jiyong Parkc678ad32018-04-10 13:07:10 +0900185}
186
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900187func (p *PrebuiltEtc) AndroidMkEntries() []AndroidMkEntries {
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700188 nameSuffix := ""
Yifan Hong1b3348d2020-01-21 15:53:22 -0800189 if p.inRamdisk() && !p.onlyInRamdisk() {
190 nameSuffix = ".ramdisk"
191 }
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700192 if p.inRecovery() && !p.onlyInRecovery() {
193 nameSuffix = ".recovery"
194 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900195 return []AndroidMkEntries{AndroidMkEntries{
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700196 Class: "ETC",
197 SubName: nameSuffix,
198 OutputFile: OptionalPathForPath(p.outputFilePath),
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700199 ExtraEntries: []AndroidMkExtraEntriesFunc{
200 func(entries *AndroidMkEntries) {
201 entries.SetString("LOCAL_MODULE_TAGS", "optional")
Colin Crossff6c33d2019-10-02 16:01:35 -0700202 entries.SetString("LOCAL_MODULE_PATH", p.installDirPath.ToMakePath().String())
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700203 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", p.outputFilePath.Base())
204 entries.SetString("LOCAL_UNINSTALLABLE_MODULE", strconv.FormatBool(!p.Installable()))
205 if p.additionalDependencies != nil {
206 for _, path := range *p.additionalDependencies {
207 entries.SetString("LOCAL_ADDITIONAL_DEPENDENCIES", path.String())
208 }
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900209 }
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700210 },
Jiyong Parkc678ad32018-04-10 13:07:10 +0900211 },
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900212 }}
Jiyong Parkc678ad32018-04-10 13:07:10 +0900213}
214
Jooyung Hana0171822019-07-22 15:48:36 +0900215func InitPrebuiltEtcModule(p *PrebuiltEtc, dirBase string) {
216 p.installDirBase = dirBase
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900217 p.AddProperties(&p.properties)
218}
Jiyong Parkc678ad32018-04-10 13:07:10 +0900219
Patrice Arruda9e14b962019-03-11 15:58:50 -0700220// prebuilt_etc is for a prebuilt artifact that is installed in
221// <partition>/etc/<sub_dir> directory.
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900222func PrebuiltEtcFactory() Module {
Jooyung Hana0171822019-07-22 15:48:36 +0900223 module := &PrebuiltEtc{}
224 InitPrebuiltEtcModule(module, "etc")
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900225 // This module is device-only
Jaewoong Jungb9a11512019-01-15 10:47:05 -0800226 InitAndroidArchModule(module, DeviceSupported, MultilibFirst)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900227 return module
228}
Tao Bao0ba5c942018-08-14 22:20:22 -0700229
Patrice Arruda9e14b962019-03-11 15:58:50 -0700230// prebuilt_etc_host is for a host prebuilt artifact that is installed in
231// $(HOST_OUT)/etc/<sub_dir> directory.
Jaewoong Jung4b44fcd2019-02-07 08:28:03 -0800232func PrebuiltEtcHostFactory() Module {
Jooyung Hana0171822019-07-22 15:48:36 +0900233 module := &PrebuiltEtc{}
234 InitPrebuiltEtcModule(module, "etc")
Jaewoong Jung24788182019-02-04 14:34:10 -0800235 // This module is host-only
236 InitAndroidArchModule(module, HostSupported, MultilibCommon)
237 return module
238}
239
Patrice Arruda9e14b962019-03-11 15:58:50 -0700240// prebuilt_usr_share is for a prebuilt artifact that is installed in
241// <partition>/usr/share/<sub_dir> directory.
Jaewoong Jungc3fcdb42019-02-13 05:50:33 -0800242func PrebuiltUserShareFactory() Module {
Jooyung Hana0171822019-07-22 15:48:36 +0900243 module := &PrebuiltEtc{}
244 InitPrebuiltEtcModule(module, "usr/share")
Jaewoong Jungc3fcdb42019-02-13 05:50:33 -0800245 // This module is device-only
246 InitAndroidArchModule(module, DeviceSupported, MultilibFirst)
247 return module
248}
249
Patrice Arruda9e14b962019-03-11 15:58:50 -0700250// prebuild_usr_share_host is for a host prebuilt artifact that is installed in
251// $(HOST_OUT)/usr/share/<sub_dir> directory.
Patrice Arruda300cef92019-02-22 15:47:57 -0800252func PrebuiltUserShareHostFactory() Module {
Jooyung Hana0171822019-07-22 15:48:36 +0900253 module := &PrebuiltEtc{}
254 InitPrebuiltEtcModule(module, "usr/share")
Patrice Arruda300cef92019-02-22 15:47:57 -0800255 // This module is host-only
256 InitAndroidArchModule(module, HostSupported, MultilibCommon)
257 return module
258}
259
Patrice Arruda61583eb2019-05-14 08:20:45 -0700260// prebuilt_font installs a font in <partition>/fonts directory.
261func PrebuiltFontFactory() Module {
Jooyung Hana0171822019-07-22 15:48:36 +0900262 module := &PrebuiltEtc{}
263 InitPrebuiltEtcModule(module, "fonts")
Patrice Arruda61583eb2019-05-14 08:20:45 -0700264 // This module is device-only
265 InitAndroidArchModule(module, DeviceSupported, MultilibFirst)
266 return module
267}
Patrice Arruda057a8b12019-06-03 15:29:27 -0700268
269// prebuilt_firmware installs a firmware file to <partition>/etc/firmware directory for system image.
270// If soc_specific property is set to true, the firmware file is installed to the vendor <partition>/firmware
271// directory for vendor image.
272func PrebuiltFirmwareFactory() Module {
Jooyung Hana0171822019-07-22 15:48:36 +0900273 module := &PrebuiltEtc{}
274 module.socInstallDirBase = "firmware"
275 InitPrebuiltEtcModule(module, "etc/firmware")
Patrice Arruda057a8b12019-06-03 15:29:27 -0700276 // This module is device-only
277 InitAndroidArchModule(module, DeviceSupported, MultilibFirst)
278 return module
279}