blob: bec24c7771ad59daf69bd425f349924ae5e55ea9 [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)
Tao Bao0ba5c942018-08-14 22:20:22 -070026
27 PreDepsMutators(func(ctx RegisterMutatorsContext) {
28 ctx.BottomUp("prebuilt_etc", prebuiltEtcMutator).Parallel()
29 })
Jiyong Parkc678ad32018-04-10 13:07:10 +090030}
31
32type prebuiltEtcProperties struct {
33 // Source file of this prebuilt.
Colin Cross27b922f2019-03-04 22:35:41 -080034 Src *string `android:"path,arch_variant"`
Jiyong Parkc678ad32018-04-10 13:07:10 +090035
36 // optional subdirectory under which this file is installed into
37 Sub_dir *string `android:"arch_variant"`
Tao Bao0ba5c942018-08-14 22:20:22 -070038
Jiyong Park139a2e62018-10-26 21:49:39 +090039 // optional name for the installed file. If unspecified, name of the module is used as the file name
40 Filename *string `android:"arch_variant"`
41
Jiyong Park1a7cf082018-11-13 11:59:12 +090042 // when set to true, and filename property is not set, the name for the installed file
43 // is the same as the file name of the source file.
44 Filename_from_src *bool `android:"arch_variant"`
45
Tao Bao0ba5c942018-08-14 22:20:22 -070046 // Make this module available when building for recovery.
47 Recovery_available *bool
48
49 InRecovery bool `blueprint:"mutated"`
Jiyong Parkad9ce042018-10-31 22:49:57 +090050
51 // Whether this module is directly installable to one of the partitions. Default: true.
52 Installable *bool
Jiyong Parkc678ad32018-04-10 13:07:10 +090053}
54
Jiyong Park5a8d1be2018-04-25 22:57:34 +090055type PrebuiltEtc struct {
Jiyong Parkc678ad32018-04-10 13:07:10 +090056 ModuleBase
Jiyong Parkc678ad32018-04-10 13:07:10 +090057
58 properties prebuiltEtcProperties
59
Jaewoong Jungc3fcdb42019-02-13 05:50:33 -080060 sourceFilePath Path
61 outputFilePath OutputPath
62 // The base install location, e.g. "etc" for prebuilt_etc, "usr/share" for prebuilt_usr_share.
63 installDirBase string
Jiyong Park5a8d1be2018-04-25 22:57:34 +090064 installDirPath OutputPath
65 additionalDependencies *Paths
Jiyong Parkc678ad32018-04-10 13:07:10 +090066}
67
Tao Bao0ba5c942018-08-14 22:20:22 -070068func (p *PrebuiltEtc) inRecovery() bool {
69 return p.properties.InRecovery || p.ModuleBase.InstallInRecovery()
70}
71
72func (p *PrebuiltEtc) onlyInRecovery() bool {
73 return p.ModuleBase.InstallInRecovery()
74}
75
76func (p *PrebuiltEtc) InstallInRecovery() bool {
77 return p.inRecovery()
78}
79
Jiyong Park5a8d1be2018-04-25 22:57:34 +090080func (p *PrebuiltEtc) DepsMutator(ctx BottomUpMutatorContext) {
81 if p.properties.Src == nil {
82 ctx.PropertyErrorf("src", "missing prebuilt source file")
Jiyong Parkc678ad32018-04-10 13:07:10 +090083 }
Jiyong Parkc678ad32018-04-10 13:07:10 +090084}
85
Jiyong Park5a8d1be2018-04-25 22:57:34 +090086func (p *PrebuiltEtc) SourceFilePath(ctx ModuleContext) Path {
Colin Cross8a497952019-03-05 22:25:09 -080087 return PathForModuleSrc(ctx, String(p.properties.Src))
Jiyong Park5a8d1be2018-04-25 22:57:34 +090088}
89
90// This allows other derivative modules (e.g. prebuilt_etc_xml) to perform
91// additional steps (like validating the src) before the file is installed.
92func (p *PrebuiltEtc) SetAdditionalDependencies(paths Paths) {
93 p.additionalDependencies = &paths
94}
95
Jiyong Parkc43e0ac2018-10-04 20:27:15 +090096func (p *PrebuiltEtc) OutputFile() OutputPath {
97 return p.outputFilePath
98}
99
100func (p *PrebuiltEtc) SubDir() string {
101 return String(p.properties.Sub_dir)
102}
103
Jiyong Parkad9ce042018-10-31 22:49:57 +0900104func (p *PrebuiltEtc) Installable() bool {
105 return p.properties.Installable == nil || Bool(p.properties.Installable)
106}
107
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900108func (p *PrebuiltEtc) GenerateAndroidBuildActions(ctx ModuleContext) {
Colin Cross8a497952019-03-05 22:25:09 -0800109 p.sourceFilePath = PathForModuleSrc(ctx, String(p.properties.Src))
Jiyong Park139a2e62018-10-26 21:49:39 +0900110 filename := String(p.properties.Filename)
Jiyong Park1a7cf082018-11-13 11:59:12 +0900111 filename_from_src := Bool(p.properties.Filename_from_src)
Jiyong Park139a2e62018-10-26 21:49:39 +0900112 if filename == "" {
Jiyong Park1a7cf082018-11-13 11:59:12 +0900113 if filename_from_src {
114 filename = p.sourceFilePath.Base()
115 } else {
116 filename = ctx.ModuleName()
117 }
118 } else if filename_from_src {
119 ctx.PropertyErrorf("filename_from_src", "filename is set. filename_from_src can't be true")
120 return
Jiyong Park139a2e62018-10-26 21:49:39 +0900121 }
122 p.outputFilePath = PathForModuleOut(ctx, filename).OutputPath
Jaewoong Jungc3fcdb42019-02-13 05:50:33 -0800123 p.installDirPath = PathForModuleInstall(ctx, p.installDirBase, String(p.properties.Sub_dir))
Jiyong Parkc43e0ac2018-10-04 20:27:15 +0900124
Dan Willemsenb0552672019-01-25 16:04:11 -0800125 // This ensures that outputFilePath has the correct name for others to
126 // use, as the source file may have a different name.
Jiyong Parkc43e0ac2018-10-04 20:27:15 +0900127 ctx.Build(pctx, BuildParams{
128 Rule: Cp,
129 Output: p.outputFilePath,
130 Input: p.sourceFilePath,
131 })
Jiyong Parkc678ad32018-04-10 13:07:10 +0900132}
133
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700134func (p *PrebuiltEtc) AndroidMkEntries() AndroidMkEntries {
135 nameSuffix := ""
136 if p.inRecovery() && !p.onlyInRecovery() {
137 nameSuffix = ".recovery"
138 }
139 return AndroidMkEntries{
140 Class: "ETC",
141 SubName: nameSuffix,
142 OutputFile: OptionalPathForPath(p.outputFilePath),
143 AddCustomEntries: func(name, prefix, moduleDir string, entries *AndroidMkEntries) {
144 entries.SetString("LOCAL_MODULE_TAGS", "optional")
145 entries.SetString("LOCAL_MODULE_PATH", "$(OUT_DIR)/"+p.installDirPath.RelPathString())
146 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", p.outputFilePath.Base())
147 entries.SetString("LOCAL_UNINSTALLABLE_MODULE", strconv.FormatBool(!p.Installable()))
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900148 if p.additionalDependencies != nil {
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900149 for _, path := range *p.additionalDependencies {
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700150 entries.SetString("LOCAL_ADDITIONAL_DEPENDENCIES", path.String())
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900151 }
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900152 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900153 },
154 }
155}
156
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900157func InitPrebuiltEtcModule(p *PrebuiltEtc) {
158 p.AddProperties(&p.properties)
159}
Jiyong Parkc678ad32018-04-10 13:07:10 +0900160
Patrice Arruda9e14b962019-03-11 15:58:50 -0700161// prebuilt_etc is for a prebuilt artifact that is installed in
162// <partition>/etc/<sub_dir> directory.
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900163func PrebuiltEtcFactory() Module {
Jaewoong Jungc3fcdb42019-02-13 05:50:33 -0800164 module := &PrebuiltEtc{installDirBase: "etc"}
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900165 InitPrebuiltEtcModule(module)
166 // This module is device-only
Jaewoong Jungb9a11512019-01-15 10:47:05 -0800167 InitAndroidArchModule(module, DeviceSupported, MultilibFirst)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900168 return module
169}
Tao Bao0ba5c942018-08-14 22:20:22 -0700170
Patrice Arruda9e14b962019-03-11 15:58:50 -0700171// prebuilt_etc_host is for a host prebuilt artifact that is installed in
172// $(HOST_OUT)/etc/<sub_dir> directory.
Jaewoong Jung4b44fcd2019-02-07 08:28:03 -0800173func PrebuiltEtcHostFactory() Module {
Jaewoong Jungc3fcdb42019-02-13 05:50:33 -0800174 module := &PrebuiltEtc{installDirBase: "etc"}
Jaewoong Jung24788182019-02-04 14:34:10 -0800175 InitPrebuiltEtcModule(module)
176 // This module is host-only
177 InitAndroidArchModule(module, HostSupported, MultilibCommon)
178 return module
179}
180
Patrice Arruda9e14b962019-03-11 15:58:50 -0700181// prebuilt_usr_share is for a prebuilt artifact that is installed in
182// <partition>/usr/share/<sub_dir> directory.
Jaewoong Jungc3fcdb42019-02-13 05:50:33 -0800183func PrebuiltUserShareFactory() Module {
184 module := &PrebuiltEtc{installDirBase: "usr/share"}
185 InitPrebuiltEtcModule(module)
186 // This module is device-only
187 InitAndroidArchModule(module, DeviceSupported, MultilibFirst)
188 return module
189}
190
Patrice Arruda9e14b962019-03-11 15:58:50 -0700191// prebuild_usr_share_host is for a host prebuilt artifact that is installed in
192// $(HOST_OUT)/usr/share/<sub_dir> directory.
Patrice Arruda300cef92019-02-22 15:47:57 -0800193func PrebuiltUserShareHostFactory() Module {
194 module := &PrebuiltEtc{installDirBase: "usr/share"}
195 InitPrebuiltEtcModule(module)
196 // This module is host-only
197 InitAndroidArchModule(module, HostSupported, MultilibCommon)
198 return module
199}
200
Tao Bao0ba5c942018-08-14 22:20:22 -0700201const (
202 // coreMode is the variant for modules to be installed to system.
203 coreMode = "core"
204
205 // recoveryMode means a module to be installed to recovery image.
206 recoveryMode = "recovery"
207)
208
209// prebuiltEtcMutator creates the needed variants to install the module to
210// system or recovery.
211func prebuiltEtcMutator(mctx BottomUpMutatorContext) {
212 m, ok := mctx.Module().(*PrebuiltEtc)
Jaewoong Jung24788182019-02-04 14:34:10 -0800213 if !ok || m.Host() {
Tao Bao0ba5c942018-08-14 22:20:22 -0700214 return
215 }
216
217 var coreVariantNeeded bool = true
218 var recoveryVariantNeeded bool = false
219 if Bool(m.properties.Recovery_available) {
220 recoveryVariantNeeded = true
221 }
222
223 if m.ModuleBase.InstallInRecovery() {
224 recoveryVariantNeeded = true
225 coreVariantNeeded = false
226 }
227
228 var variants []string
229 if coreVariantNeeded {
230 variants = append(variants, coreMode)
231 }
232 if recoveryVariantNeeded {
233 variants = append(variants, recoveryMode)
234 }
235 mod := mctx.CreateVariations(variants...)
236 for i, v := range variants {
237 if v == recoveryMode {
238 m := mod[i].(*PrebuiltEtc)
239 m.properties.InRecovery = true
240 }
241 }
242}