blob: 6c803705155ece9dc83a8d0549536d26d822f3d2 [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)
Tao Bao0ba5c942018-08-14 22:20:22 -070028
29 PreDepsMutators(func(ctx RegisterMutatorsContext) {
30 ctx.BottomUp("prebuilt_etc", prebuiltEtcMutator).Parallel()
31 })
Jiyong Parkc678ad32018-04-10 13:07:10 +090032}
33
34type prebuiltEtcProperties struct {
35 // Source file of this prebuilt.
Colin Cross27b922f2019-03-04 22:35:41 -080036 Src *string `android:"path,arch_variant"`
Jiyong Parkc678ad32018-04-10 13:07:10 +090037
38 // optional subdirectory under which this file is installed into
39 Sub_dir *string `android:"arch_variant"`
Tao Bao0ba5c942018-08-14 22:20:22 -070040
Jiyong Park139a2e62018-10-26 21:49:39 +090041 // optional name for the installed file. If unspecified, name of the module is used as the file name
42 Filename *string `android:"arch_variant"`
43
Jiyong Park1a7cf082018-11-13 11:59:12 +090044 // when set to true, and filename property is not set, the name for the installed file
45 // is the same as the file name of the source file.
46 Filename_from_src *bool `android:"arch_variant"`
47
Tao Bao0ba5c942018-08-14 22:20:22 -070048 // Make this module available when building for recovery.
49 Recovery_available *bool
50
51 InRecovery bool `blueprint:"mutated"`
Jiyong Parkad9ce042018-10-31 22:49:57 +090052
53 // Whether this module is directly installable to one of the partitions. Default: true.
54 Installable *bool
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
Tao Bao0ba5c942018-08-14 22:20:22 -070078func (p *PrebuiltEtc) inRecovery() bool {
79 return p.properties.InRecovery || p.ModuleBase.InstallInRecovery()
80}
81
82func (p *PrebuiltEtc) onlyInRecovery() bool {
83 return p.ModuleBase.InstallInRecovery()
84}
85
86func (p *PrebuiltEtc) InstallInRecovery() bool {
87 return p.inRecovery()
88}
89
Jiyong Park5a8d1be2018-04-25 22:57:34 +090090func (p *PrebuiltEtc) DepsMutator(ctx BottomUpMutatorContext) {
91 if p.properties.Src == nil {
92 ctx.PropertyErrorf("src", "missing prebuilt source file")
Jiyong Parkc678ad32018-04-10 13:07:10 +090093 }
Jiyong Parkc678ad32018-04-10 13:07:10 +090094}
95
Jiyong Park5a8d1be2018-04-25 22:57:34 +090096func (p *PrebuiltEtc) SourceFilePath(ctx ModuleContext) Path {
Colin Cross8a497952019-03-05 22:25:09 -080097 return PathForModuleSrc(ctx, String(p.properties.Src))
Jiyong Park5a8d1be2018-04-25 22:57:34 +090098}
99
Colin Cross70dda7e2019-10-01 22:05:35 -0700100func (p *PrebuiltEtc) InstallDirPath() InstallPath {
Jooyung Hana0171822019-07-22 15:48:36 +0900101 return p.installDirPath
102}
103
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900104// This allows other derivative modules (e.g. prebuilt_etc_xml) to perform
105// additional steps (like validating the src) before the file is installed.
106func (p *PrebuiltEtc) SetAdditionalDependencies(paths Paths) {
107 p.additionalDependencies = &paths
108}
109
Jiyong Parkc43e0ac2018-10-04 20:27:15 +0900110func (p *PrebuiltEtc) OutputFile() OutputPath {
111 return p.outputFilePath
112}
113
114func (p *PrebuiltEtc) SubDir() string {
115 return String(p.properties.Sub_dir)
116}
117
Jiyong Parkad9ce042018-10-31 22:49:57 +0900118func (p *PrebuiltEtc) Installable() bool {
119 return p.properties.Installable == nil || Bool(p.properties.Installable)
120}
121
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900122func (p *PrebuiltEtc) GenerateAndroidBuildActions(ctx ModuleContext) {
Colin Cross8a497952019-03-05 22:25:09 -0800123 p.sourceFilePath = PathForModuleSrc(ctx, String(p.properties.Src))
Jiyong Park139a2e62018-10-26 21:49:39 +0900124 filename := String(p.properties.Filename)
Jiyong Park1a7cf082018-11-13 11:59:12 +0900125 filename_from_src := Bool(p.properties.Filename_from_src)
Jiyong Park139a2e62018-10-26 21:49:39 +0900126 if filename == "" {
Jiyong Park1a7cf082018-11-13 11:59:12 +0900127 if filename_from_src {
128 filename = p.sourceFilePath.Base()
129 } else {
130 filename = ctx.ModuleName()
131 }
132 } else if filename_from_src {
133 ctx.PropertyErrorf("filename_from_src", "filename is set. filename_from_src can't be true")
134 return
Jiyong Park139a2e62018-10-26 21:49:39 +0900135 }
136 p.outputFilePath = PathForModuleOut(ctx, filename).OutputPath
Patrice Arruda057a8b12019-06-03 15:29:27 -0700137
138 // If soc install dir was specified and SOC specific is set, set the installDirPath to the specified
139 // socInstallDirBase.
140 installBaseDir := p.installDirBase
141 if ctx.SocSpecific() && p.socInstallDirBase != "" {
142 installBaseDir = p.socInstallDirBase
143 }
144 p.installDirPath = PathForModuleInstall(ctx, installBaseDir, String(p.properties.Sub_dir))
Jiyong Parkc43e0ac2018-10-04 20:27:15 +0900145
Dan Willemsenb0552672019-01-25 16:04:11 -0800146 // This ensures that outputFilePath has the correct name for others to
147 // use, as the source file may have a different name.
Jiyong Parkc43e0ac2018-10-04 20:27:15 +0900148 ctx.Build(pctx, BuildParams{
149 Rule: Cp,
150 Output: p.outputFilePath,
151 Input: p.sourceFilePath,
152 })
Jiyong Parkc678ad32018-04-10 13:07:10 +0900153}
154
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700155func (p *PrebuiltEtc) AndroidMkEntries() AndroidMkEntries {
156 nameSuffix := ""
157 if p.inRecovery() && !p.onlyInRecovery() {
158 nameSuffix = ".recovery"
159 }
160 return AndroidMkEntries{
161 Class: "ETC",
162 SubName: nameSuffix,
163 OutputFile: OptionalPathForPath(p.outputFilePath),
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700164 ExtraEntries: []AndroidMkExtraEntriesFunc{
165 func(entries *AndroidMkEntries) {
166 entries.SetString("LOCAL_MODULE_TAGS", "optional")
Colin Crossff6c33d2019-10-02 16:01:35 -0700167 entries.SetString("LOCAL_MODULE_PATH", p.installDirPath.ToMakePath().String())
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700168 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", p.outputFilePath.Base())
169 entries.SetString("LOCAL_UNINSTALLABLE_MODULE", strconv.FormatBool(!p.Installable()))
170 if p.additionalDependencies != nil {
171 for _, path := range *p.additionalDependencies {
172 entries.SetString("LOCAL_ADDITIONAL_DEPENDENCIES", path.String())
173 }
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900174 }
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700175 },
Jiyong Parkc678ad32018-04-10 13:07:10 +0900176 },
177 }
178}
179
Jooyung Hana0171822019-07-22 15:48:36 +0900180func InitPrebuiltEtcModule(p *PrebuiltEtc, dirBase string) {
181 p.installDirBase = dirBase
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900182 p.AddProperties(&p.properties)
183}
Jiyong Parkc678ad32018-04-10 13:07:10 +0900184
Patrice Arruda9e14b962019-03-11 15:58:50 -0700185// prebuilt_etc is for a prebuilt artifact that is installed in
186// <partition>/etc/<sub_dir> directory.
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900187func PrebuiltEtcFactory() Module {
Jooyung Hana0171822019-07-22 15:48:36 +0900188 module := &PrebuiltEtc{}
189 InitPrebuiltEtcModule(module, "etc")
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900190 // This module is device-only
Jaewoong Jungb9a11512019-01-15 10:47:05 -0800191 InitAndroidArchModule(module, DeviceSupported, MultilibFirst)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900192 return module
193}
Tao Bao0ba5c942018-08-14 22:20:22 -0700194
Patrice Arruda9e14b962019-03-11 15:58:50 -0700195// prebuilt_etc_host is for a host prebuilt artifact that is installed in
196// $(HOST_OUT)/etc/<sub_dir> directory.
Jaewoong Jung4b44fcd2019-02-07 08:28:03 -0800197func PrebuiltEtcHostFactory() Module {
Jooyung Hana0171822019-07-22 15:48:36 +0900198 module := &PrebuiltEtc{}
199 InitPrebuiltEtcModule(module, "etc")
Jaewoong Jung24788182019-02-04 14:34:10 -0800200 // This module is host-only
201 InitAndroidArchModule(module, HostSupported, MultilibCommon)
202 return module
203}
204
Patrice Arruda9e14b962019-03-11 15:58:50 -0700205// prebuilt_usr_share is for a prebuilt artifact that is installed in
206// <partition>/usr/share/<sub_dir> directory.
Jaewoong Jungc3fcdb42019-02-13 05:50:33 -0800207func PrebuiltUserShareFactory() Module {
Jooyung Hana0171822019-07-22 15:48:36 +0900208 module := &PrebuiltEtc{}
209 InitPrebuiltEtcModule(module, "usr/share")
Jaewoong Jungc3fcdb42019-02-13 05:50:33 -0800210 // This module is device-only
211 InitAndroidArchModule(module, DeviceSupported, MultilibFirst)
212 return module
213}
214
Patrice Arruda9e14b962019-03-11 15:58:50 -0700215// prebuild_usr_share_host is for a host prebuilt artifact that is installed in
216// $(HOST_OUT)/usr/share/<sub_dir> directory.
Patrice Arruda300cef92019-02-22 15:47:57 -0800217func PrebuiltUserShareHostFactory() Module {
Jooyung Hana0171822019-07-22 15:48:36 +0900218 module := &PrebuiltEtc{}
219 InitPrebuiltEtcModule(module, "usr/share")
Patrice Arruda300cef92019-02-22 15:47:57 -0800220 // This module is host-only
221 InitAndroidArchModule(module, HostSupported, MultilibCommon)
222 return module
223}
224
Tao Bao0ba5c942018-08-14 22:20:22 -0700225const (
226 // coreMode is the variant for modules to be installed to system.
227 coreMode = "core"
228
229 // recoveryMode means a module to be installed to recovery image.
230 recoveryMode = "recovery"
231)
232
233// prebuiltEtcMutator creates the needed variants to install the module to
234// system or recovery.
235func prebuiltEtcMutator(mctx BottomUpMutatorContext) {
236 m, ok := mctx.Module().(*PrebuiltEtc)
Jaewoong Jung24788182019-02-04 14:34:10 -0800237 if !ok || m.Host() {
Tao Bao0ba5c942018-08-14 22:20:22 -0700238 return
239 }
240
241 var coreVariantNeeded bool = true
242 var recoveryVariantNeeded bool = false
243 if Bool(m.properties.Recovery_available) {
244 recoveryVariantNeeded = true
245 }
246
247 if m.ModuleBase.InstallInRecovery() {
248 recoveryVariantNeeded = true
249 coreVariantNeeded = false
250 }
251
252 var variants []string
253 if coreVariantNeeded {
254 variants = append(variants, coreMode)
255 }
256 if recoveryVariantNeeded {
257 variants = append(variants, recoveryMode)
258 }
259 mod := mctx.CreateVariations(variants...)
260 for i, v := range variants {
261 if v == recoveryMode {
262 m := mod[i].(*PrebuiltEtc)
263 m.properties.InRecovery = true
264 }
265 }
266}
Patrice Arruda61583eb2019-05-14 08:20:45 -0700267
268// prebuilt_font installs a font in <partition>/fonts directory.
269func PrebuiltFontFactory() Module {
Jooyung Hana0171822019-07-22 15:48:36 +0900270 module := &PrebuiltEtc{}
271 InitPrebuiltEtcModule(module, "fonts")
Patrice Arruda61583eb2019-05-14 08:20:45 -0700272 // This module is device-only
273 InitAndroidArchModule(module, DeviceSupported, MultilibFirst)
274 return module
275}
Patrice Arruda057a8b12019-06-03 15:29:27 -0700276
277// prebuilt_firmware installs a firmware file to <partition>/etc/firmware directory for system image.
278// If soc_specific property is set to true, the firmware file is installed to the vendor <partition>/firmware
279// directory for vendor image.
280func PrebuiltFirmwareFactory() Module {
Jooyung Hana0171822019-07-22 15:48:36 +0900281 module := &PrebuiltEtc{}
282 module.socInstallDirBase = "firmware"
283 InitPrebuiltEtcModule(module, "etc/firmware")
Patrice Arruda057a8b12019-06-03 15:29:27 -0700284 // This module is device-only
285 InitAndroidArchModule(module, DeviceSupported, MultilibFirst)
286 return module
287}