blob: 842d9eec16d821f2b89e83179ccce23ce122c642 [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 Jungfccad6b2020-06-01 10:45:49 -070015package etc
Jiyong Parkc678ad32018-04-10 13:07:10 +090016
Jaewoong Jungfccad6b2020-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 Jungfccad6b2020-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
Jiyong Parkc678ad32018-04-10 13:07:10 +090062}
63
Jooyung Han39edb6c2019-11-06 16:53:07 +090064type PrebuiltEtcModule interface {
Jaewoong Jungfccad6b2020-06-01 10:45:49 -070065 android.Module
Jooyung Han39edb6c2019-11-06 16:53:07 +090066 SubDir() string
Jaewoong Jungfccad6b2020-06-01 10:45:49 -070067 OutputFile() android.OutputPath
Jooyung Han39edb6c2019-11-06 16:53:07 +090068}
69
Jiyong Park5a8d1be2018-04-25 22:57:34 +090070type PrebuiltEtc struct {
Jaewoong Jungfccad6b2020-06-01 10:45:49 -070071 android.ModuleBase
Jiyong Parkc678ad32018-04-10 13:07:10 +090072
73 properties prebuiltEtcProperties
74
Jaewoong Jungfccad6b2020-06-01 10:45:49 -070075 sourceFilePath android.Path
76 outputFilePath android.OutputPath
Jaewoong Jungc3fcdb42019-02-13 05:50:33 -080077 // The base install location, e.g. "etc" for prebuilt_etc, "usr/share" for prebuilt_usr_share.
Patrice Arruda057a8b12019-06-03 15:29:27 -070078 installDirBase string
79 // The base install location when soc_specific property is set to true, e.g. "firmware" for prebuilt_firmware.
80 socInstallDirBase string
Jaewoong Jungfccad6b2020-06-01 10:45:49 -070081 installDirPath android.InstallPath
82 additionalDependencies *android.Paths
Jiyong Parkc678ad32018-04-10 13:07:10 +090083}
84
Yifan Hong1b3348d2020-01-21 15:53:22 -080085func (p *PrebuiltEtc) inRamdisk() bool {
86 return p.ModuleBase.InRamdisk() || p.ModuleBase.InstallInRamdisk()
87}
88
89func (p *PrebuiltEtc) onlyInRamdisk() bool {
90 return p.ModuleBase.InstallInRamdisk()
91}
92
93func (p *PrebuiltEtc) InstallInRamdisk() bool {
94 return p.inRamdisk()
95}
96
Tao Bao0ba5c942018-08-14 22:20:22 -070097func (p *PrebuiltEtc) inRecovery() bool {
Colin Cross7228ecd2019-11-18 16:00:16 -080098 return p.ModuleBase.InRecovery() || p.ModuleBase.InstallInRecovery()
Tao Bao0ba5c942018-08-14 22:20:22 -070099}
100
101func (p *PrebuiltEtc) onlyInRecovery() bool {
102 return p.ModuleBase.InstallInRecovery()
103}
104
105func (p *PrebuiltEtc) InstallInRecovery() bool {
106 return p.inRecovery()
107}
108
Jaewoong Jungfccad6b2020-06-01 10:45:49 -0700109var _ android.ImageInterface = (*PrebuiltEtc)(nil)
Colin Cross7228ecd2019-11-18 16:00:16 -0800110
Jaewoong Jungfccad6b2020-06-01 10:45:49 -0700111func (p *PrebuiltEtc) ImageMutatorBegin(ctx android.BaseModuleContext) {}
Colin Cross7228ecd2019-11-18 16:00:16 -0800112
Jaewoong Jungfccad6b2020-06-01 10:45:49 -0700113func (p *PrebuiltEtc) CoreVariantNeeded(ctx android.BaseModuleContext) bool {
Yifan Hong1b3348d2020-01-21 15:53:22 -0800114 return !p.ModuleBase.InstallInRecovery() && !p.ModuleBase.InstallInRamdisk()
115}
116
Jaewoong Jungfccad6b2020-06-01 10:45:49 -0700117func (p *PrebuiltEtc) RamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
118 return proptools.Bool(p.properties.Ramdisk_available) || p.ModuleBase.InstallInRamdisk()
Colin Cross7228ecd2019-11-18 16:00:16 -0800119}
120
Jaewoong Jungfccad6b2020-06-01 10:45:49 -0700121func (p *PrebuiltEtc) RecoveryVariantNeeded(ctx android.BaseModuleContext) bool {
122 return proptools.Bool(p.properties.Recovery_available) || p.ModuleBase.InstallInRecovery()
Colin Cross7228ecd2019-11-18 16:00:16 -0800123}
124
Jaewoong Jungfccad6b2020-06-01 10:45:49 -0700125func (p *PrebuiltEtc) ExtraImageVariations(ctx android.BaseModuleContext) []string {
Colin Cross7228ecd2019-11-18 16:00:16 -0800126 return nil
127}
128
Jaewoong Jungfccad6b2020-06-01 10:45:49 -0700129func (p *PrebuiltEtc) SetImageVariation(ctx android.BaseModuleContext, variation string, module android.Module) {
Colin Cross7228ecd2019-11-18 16:00:16 -0800130}
131
Jaewoong Jungfccad6b2020-06-01 10:45:49 -0700132func (p *PrebuiltEtc) DepsMutator(ctx android.BottomUpMutatorContext) {
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900133 if p.properties.Src == nil {
134 ctx.PropertyErrorf("src", "missing prebuilt source file")
Jiyong Parkc678ad32018-04-10 13:07:10 +0900135 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900136}
137
Jaewoong Jungfccad6b2020-06-01 10:45:49 -0700138func (p *PrebuiltEtc) SourceFilePath(ctx android.ModuleContext) android.Path {
139 return android.PathForModuleSrc(ctx, android.String(p.properties.Src))
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900140}
141
Jaewoong Jungfccad6b2020-06-01 10:45:49 -0700142func (p *PrebuiltEtc) InstallDirPath() android.InstallPath {
Jooyung Hana0171822019-07-22 15:48:36 +0900143 return p.installDirPath
144}
145
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900146// This allows other derivative modules (e.g. prebuilt_etc_xml) to perform
147// additional steps (like validating the src) before the file is installed.
Jaewoong Jungfccad6b2020-06-01 10:45:49 -0700148func (p *PrebuiltEtc) SetAdditionalDependencies(paths android.Paths) {
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900149 p.additionalDependencies = &paths
150}
151
Jaewoong Jungfccad6b2020-06-01 10:45:49 -0700152func (p *PrebuiltEtc) OutputFile() android.OutputPath {
Jiyong Parkc43e0ac2018-10-04 20:27:15 +0900153 return p.outputFilePath
154}
155
156func (p *PrebuiltEtc) SubDir() string {
Jaewoong Jungfccad6b2020-06-01 10:45:49 -0700157 return android.String(p.properties.Sub_dir)
Jiyong Parkc43e0ac2018-10-04 20:27:15 +0900158}
159
Jiyong Parkad9ce042018-10-31 22:49:57 +0900160func (p *PrebuiltEtc) Installable() bool {
Jaewoong Jungfccad6b2020-06-01 10:45:49 -0700161 return p.properties.Installable == nil || android.Bool(p.properties.Installable)
Jiyong Parkad9ce042018-10-31 22:49:57 +0900162}
163
Jaewoong Jungfccad6b2020-06-01 10:45:49 -0700164func (p *PrebuiltEtc) GenerateAndroidBuildActions(ctx android.ModuleContext) {
165 p.sourceFilePath = android.PathForModuleSrc(ctx, android.String(p.properties.Src))
166 filename := android.String(p.properties.Filename)
167 filename_from_src := android.Bool(p.properties.Filename_from_src)
Jiyong Park139a2e62018-10-26 21:49:39 +0900168 if filename == "" {
Jiyong Park1a7cf082018-11-13 11:59:12 +0900169 if filename_from_src {
170 filename = p.sourceFilePath.Base()
171 } else {
172 filename = ctx.ModuleName()
173 }
174 } else if filename_from_src {
175 ctx.PropertyErrorf("filename_from_src", "filename is set. filename_from_src can't be true")
176 return
Jiyong Park139a2e62018-10-26 21:49:39 +0900177 }
Jaewoong Jungfccad6b2020-06-01 10:45:49 -0700178 p.outputFilePath = android.PathForModuleOut(ctx, filename).OutputPath
Patrice Arruda057a8b12019-06-03 15:29:27 -0700179
180 // If soc install dir was specified and SOC specific is set, set the installDirPath to the specified
181 // socInstallDirBase.
182 installBaseDir := p.installDirBase
183 if ctx.SocSpecific() && p.socInstallDirBase != "" {
184 installBaseDir = p.socInstallDirBase
185 }
Jaewoong Jungfccad6b2020-06-01 10:45:49 -0700186 p.installDirPath = android.PathForModuleInstall(ctx, installBaseDir, proptools.String(p.properties.Sub_dir))
Jiyong Parkc43e0ac2018-10-04 20:27:15 +0900187
Dan Willemsenb0552672019-01-25 16:04:11 -0800188 // This ensures that outputFilePath has the correct name for others to
189 // use, as the source file may have a different name.
Jaewoong Jungfccad6b2020-06-01 10:45:49 -0700190 ctx.Build(pctx, android.BuildParams{
191 Rule: android.Cp,
Jiyong Parkc43e0ac2018-10-04 20:27:15 +0900192 Output: p.outputFilePath,
193 Input: p.sourceFilePath,
194 })
Jiyong Parkc678ad32018-04-10 13:07:10 +0900195}
196
Jaewoong Jungfccad6b2020-06-01 10:45:49 -0700197func (p *PrebuiltEtc) AndroidMkEntries() []android.AndroidMkEntries {
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700198 nameSuffix := ""
Yifan Hong1b3348d2020-01-21 15:53:22 -0800199 if p.inRamdisk() && !p.onlyInRamdisk() {
200 nameSuffix = ".ramdisk"
201 }
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700202 if p.inRecovery() && !p.onlyInRecovery() {
203 nameSuffix = ".recovery"
204 }
Jaewoong Jungfccad6b2020-06-01 10:45:49 -0700205 return []android.AndroidMkEntries{android.AndroidMkEntries{
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700206 Class: "ETC",
207 SubName: nameSuffix,
Jaewoong Jungfccad6b2020-06-01 10:45:49 -0700208 OutputFile: android.OptionalPathForPath(p.outputFilePath),
209 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
210 func(entries *android.AndroidMkEntries) {
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700211 entries.SetString("LOCAL_MODULE_TAGS", "optional")
Colin Crossff6c33d2019-10-02 16:01:35 -0700212 entries.SetString("LOCAL_MODULE_PATH", p.installDirPath.ToMakePath().String())
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700213 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", p.outputFilePath.Base())
214 entries.SetString("LOCAL_UNINSTALLABLE_MODULE", strconv.FormatBool(!p.Installable()))
215 if p.additionalDependencies != nil {
216 for _, path := range *p.additionalDependencies {
217 entries.SetString("LOCAL_ADDITIONAL_DEPENDENCIES", path.String())
218 }
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900219 }
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700220 },
Jiyong Parkc678ad32018-04-10 13:07:10 +0900221 },
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900222 }}
Jiyong Parkc678ad32018-04-10 13:07:10 +0900223}
224
Jooyung Hana0171822019-07-22 15:48:36 +0900225func InitPrebuiltEtcModule(p *PrebuiltEtc, dirBase string) {
226 p.installDirBase = dirBase
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900227 p.AddProperties(&p.properties)
228}
Jiyong Parkc678ad32018-04-10 13:07:10 +0900229
Patrice Arruda9e14b962019-03-11 15:58:50 -0700230// prebuilt_etc is for a prebuilt artifact that is installed in
231// <partition>/etc/<sub_dir> directory.
Jaewoong Jungfccad6b2020-06-01 10:45:49 -0700232func PrebuiltEtcFactory() android.Module {
Jooyung Hana0171822019-07-22 15:48:36 +0900233 module := &PrebuiltEtc{}
234 InitPrebuiltEtcModule(module, "etc")
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900235 // This module is device-only
Jaewoong Jungfccad6b2020-06-01 10:45:49 -0700236 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibFirst)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900237 return module
238}
Tao Bao0ba5c942018-08-14 22:20:22 -0700239
Patrice Arruda9e14b962019-03-11 15:58:50 -0700240// prebuilt_etc_host is for a host prebuilt artifact that is installed in
241// $(HOST_OUT)/etc/<sub_dir> directory.
Jaewoong Jungfccad6b2020-06-01 10:45:49 -0700242func PrebuiltEtcHostFactory() android.Module {
Jooyung Hana0171822019-07-22 15:48:36 +0900243 module := &PrebuiltEtc{}
244 InitPrebuiltEtcModule(module, "etc")
Jaewoong Jung24788182019-02-04 14:34:10 -0800245 // This module is host-only
Jaewoong Jungfccad6b2020-06-01 10:45:49 -0700246 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommon)
Jaewoong Jung24788182019-02-04 14:34:10 -0800247 return module
248}
249
Patrice Arruda9e14b962019-03-11 15:58:50 -0700250// prebuilt_usr_share is for a prebuilt artifact that is installed in
251// <partition>/usr/share/<sub_dir> directory.
Jaewoong Jungfccad6b2020-06-01 10:45:49 -0700252func PrebuiltUserShareFactory() android.Module {
Jooyung Hana0171822019-07-22 15:48:36 +0900253 module := &PrebuiltEtc{}
254 InitPrebuiltEtcModule(module, "usr/share")
Jaewoong Jungc3fcdb42019-02-13 05:50:33 -0800255 // This module is device-only
Jaewoong Jungfccad6b2020-06-01 10:45:49 -0700256 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibFirst)
Jaewoong Jungc3fcdb42019-02-13 05:50:33 -0800257 return module
258}
259
Patrice Arruda9e14b962019-03-11 15:58:50 -0700260// prebuild_usr_share_host is for a host prebuilt artifact that is installed in
261// $(HOST_OUT)/usr/share/<sub_dir> directory.
Jaewoong Jungfccad6b2020-06-01 10:45:49 -0700262func PrebuiltUserShareHostFactory() android.Module {
Jooyung Hana0171822019-07-22 15:48:36 +0900263 module := &PrebuiltEtc{}
264 InitPrebuiltEtcModule(module, "usr/share")
Patrice Arruda300cef92019-02-22 15:47:57 -0800265 // This module is host-only
Jaewoong Jungfccad6b2020-06-01 10:45:49 -0700266 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommon)
Patrice Arruda300cef92019-02-22 15:47:57 -0800267 return module
268}
269
Patrice Arruda61583eb2019-05-14 08:20:45 -0700270// prebuilt_font installs a font in <partition>/fonts directory.
Jaewoong Jungfccad6b2020-06-01 10:45:49 -0700271func PrebuiltFontFactory() android.Module {
Jooyung Hana0171822019-07-22 15:48:36 +0900272 module := &PrebuiltEtc{}
273 InitPrebuiltEtcModule(module, "fonts")
Patrice Arruda61583eb2019-05-14 08:20:45 -0700274 // This module is device-only
Jaewoong Jungfccad6b2020-06-01 10:45:49 -0700275 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibFirst)
Patrice Arruda61583eb2019-05-14 08:20:45 -0700276 return module
277}
Patrice Arruda057a8b12019-06-03 15:29:27 -0700278
279// prebuilt_firmware installs a firmware file to <partition>/etc/firmware directory for system image.
280// If soc_specific property is set to true, the firmware file is installed to the vendor <partition>/firmware
281// directory for vendor image.
Jaewoong Jungfccad6b2020-06-01 10:45:49 -0700282func PrebuiltFirmwareFactory() android.Module {
Jooyung Hana0171822019-07-22 15:48:36 +0900283 module := &PrebuiltEtc{}
284 module.socInstallDirBase = "firmware"
285 InitPrebuiltEtcModule(module, "etc/firmware")
Patrice Arruda057a8b12019-06-03 15:29:27 -0700286 // This module is device-only
Jaewoong Jungfccad6b2020-06-01 10:45:49 -0700287 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibFirst)
Patrice Arruda057a8b12019-06-03 15:29:27 -0700288 return module
289}