blob: 0c68b0cce30a48f163ece27ce19cfa0b36e1df6b [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
17import (
18 "fmt"
19 "io"
Anton Hansson4ca89892019-01-14 12:17:40 +000020 "strings"
Jiyong Parkc678ad32018-04-10 13:07:10 +090021)
22
Jaewoong Jungc3fcdb42019-02-13 05:50:33 -080023// TODO(jungw): Now that it handles more than the ones in etc/, consider renaming this file.
Jiyong Parkc678ad32018-04-10 13:07:10 +090024
25func init() {
26 RegisterModuleType("prebuilt_etc", PrebuiltEtcFactory)
Jaewoong Jung4b44fcd2019-02-07 08:28:03 -080027 RegisterModuleType("prebuilt_etc_host", PrebuiltEtcHostFactory)
Jaewoong Jungc3fcdb42019-02-13 05:50:33 -080028 RegisterModuleType("prebuilt_usr_share", PrebuiltUserShareFactory)
Patrice Arruda300cef92019-02-22 15:47:57 -080029 RegisterModuleType("prebuilt_usr_share_host", PrebuiltUserShareHostFactory)
Tao Bao0ba5c942018-08-14 22:20:22 -070030
31 PreDepsMutators(func(ctx RegisterMutatorsContext) {
32 ctx.BottomUp("prebuilt_etc", prebuiltEtcMutator).Parallel()
33 })
Jiyong Parkc678ad32018-04-10 13:07:10 +090034}
35
36type prebuiltEtcProperties struct {
37 // Source file of this prebuilt.
Colin Cross27b922f2019-03-04 22:35:41 -080038 Src *string `android:"path,arch_variant"`
Jiyong Parkc678ad32018-04-10 13:07:10 +090039
40 // optional subdirectory under which this file is installed into
41 Sub_dir *string `android:"arch_variant"`
Tao Bao0ba5c942018-08-14 22:20:22 -070042
Jiyong Park139a2e62018-10-26 21:49:39 +090043 // optional name for the installed file. If unspecified, name of the module is used as the file name
44 Filename *string `android:"arch_variant"`
45
Jiyong Park1a7cf082018-11-13 11:59:12 +090046 // when set to true, and filename property is not set, the name for the installed file
47 // is the same as the file name of the source file.
48 Filename_from_src *bool `android:"arch_variant"`
49
Tao Bao0ba5c942018-08-14 22:20:22 -070050 // Make this module available when building for recovery.
51 Recovery_available *bool
52
53 InRecovery bool `blueprint:"mutated"`
Jiyong Parkad9ce042018-10-31 22:49:57 +090054
55 // Whether this module is directly installable to one of the partitions. Default: true.
56 Installable *bool
Jiyong Parkc678ad32018-04-10 13:07:10 +090057}
58
Jiyong Park5a8d1be2018-04-25 22:57:34 +090059type PrebuiltEtc struct {
Jiyong Parkc678ad32018-04-10 13:07:10 +090060 ModuleBase
Jiyong Parkc678ad32018-04-10 13:07:10 +090061
62 properties prebuiltEtcProperties
63
Jaewoong Jungc3fcdb42019-02-13 05:50:33 -080064 sourceFilePath Path
65 outputFilePath OutputPath
66 // The base install location, e.g. "etc" for prebuilt_etc, "usr/share" for prebuilt_usr_share.
67 installDirBase string
Jiyong Park5a8d1be2018-04-25 22:57:34 +090068 installDirPath OutputPath
69 additionalDependencies *Paths
Jiyong Parkc678ad32018-04-10 13:07:10 +090070}
71
Tao Bao0ba5c942018-08-14 22:20:22 -070072func (p *PrebuiltEtc) inRecovery() bool {
73 return p.properties.InRecovery || p.ModuleBase.InstallInRecovery()
74}
75
76func (p *PrebuiltEtc) onlyInRecovery() bool {
77 return p.ModuleBase.InstallInRecovery()
78}
79
80func (p *PrebuiltEtc) InstallInRecovery() bool {
81 return p.inRecovery()
82}
83
Jiyong Park5a8d1be2018-04-25 22:57:34 +090084func (p *PrebuiltEtc) DepsMutator(ctx BottomUpMutatorContext) {
85 if p.properties.Src == nil {
86 ctx.PropertyErrorf("src", "missing prebuilt source file")
Jiyong Parkc678ad32018-04-10 13:07:10 +090087 }
Jiyong Parkc678ad32018-04-10 13:07:10 +090088}
89
Jiyong Park5a8d1be2018-04-25 22:57:34 +090090func (p *PrebuiltEtc) SourceFilePath(ctx ModuleContext) Path {
91 return ctx.ExpandSource(String(p.properties.Src), "src")
92}
93
94// This allows other derivative modules (e.g. prebuilt_etc_xml) to perform
95// additional steps (like validating the src) before the file is installed.
96func (p *PrebuiltEtc) SetAdditionalDependencies(paths Paths) {
97 p.additionalDependencies = &paths
98}
99
Jiyong Parkc43e0ac2018-10-04 20:27:15 +0900100func (p *PrebuiltEtc) OutputFile() OutputPath {
101 return p.outputFilePath
102}
103
104func (p *PrebuiltEtc) SubDir() string {
105 return String(p.properties.Sub_dir)
106}
107
Jiyong Parkad9ce042018-10-31 22:49:57 +0900108func (p *PrebuiltEtc) Installable() bool {
109 return p.properties.Installable == nil || Bool(p.properties.Installable)
110}
111
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900112func (p *PrebuiltEtc) GenerateAndroidBuildActions(ctx ModuleContext) {
113 p.sourceFilePath = ctx.ExpandSource(String(p.properties.Src), "src")
Jiyong Park139a2e62018-10-26 21:49:39 +0900114 filename := String(p.properties.Filename)
Jiyong Park1a7cf082018-11-13 11:59:12 +0900115 filename_from_src := Bool(p.properties.Filename_from_src)
Jiyong Park139a2e62018-10-26 21:49:39 +0900116 if filename == "" {
Jiyong Park1a7cf082018-11-13 11:59:12 +0900117 if filename_from_src {
118 filename = p.sourceFilePath.Base()
119 } else {
120 filename = ctx.ModuleName()
121 }
122 } else if filename_from_src {
123 ctx.PropertyErrorf("filename_from_src", "filename is set. filename_from_src can't be true")
124 return
Jiyong Park139a2e62018-10-26 21:49:39 +0900125 }
126 p.outputFilePath = PathForModuleOut(ctx, filename).OutputPath
Jaewoong Jungc3fcdb42019-02-13 05:50:33 -0800127 p.installDirPath = PathForModuleInstall(ctx, p.installDirBase, String(p.properties.Sub_dir))
Jiyong Parkc43e0ac2018-10-04 20:27:15 +0900128
Dan Willemsenb0552672019-01-25 16:04:11 -0800129 // This ensures that outputFilePath has the correct name for others to
130 // use, as the source file may have a different name.
Jiyong Parkc43e0ac2018-10-04 20:27:15 +0900131 ctx.Build(pctx, BuildParams{
132 Rule: Cp,
133 Output: p.outputFilePath,
134 Input: p.sourceFilePath,
135 })
Jiyong Parkc678ad32018-04-10 13:07:10 +0900136}
137
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900138func (p *PrebuiltEtc) AndroidMk() AndroidMkData {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900139 return AndroidMkData{
140 Custom: func(w io.Writer, name, prefix, moduleDir string, data AndroidMkData) {
Tao Bao0ba5c942018-08-14 22:20:22 -0700141 nameSuffix := ""
142 if p.inRecovery() && !p.onlyInRecovery() {
143 nameSuffix = ".recovery"
144 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900145 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
146 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
Tao Bao0ba5c942018-08-14 22:20:22 -0700147 fmt.Fprintln(w, "LOCAL_MODULE :=", name+nameSuffix)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900148 fmt.Fprintln(w, "LOCAL_MODULE_CLASS := ETC")
Anton Hanssonce0e2582019-02-04 14:19:27 +0000149 if p.commonProperties.Owner != nil {
150 fmt.Fprintln(w, "LOCAL_MODULE_OWNER :=", *p.commonProperties.Owner)
151 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900152 fmt.Fprintln(w, "LOCAL_MODULE_TAGS := optional")
Jaewoong Jung24788182019-02-04 14:34:10 -0800153 if p.Host() {
154 fmt.Fprintln(w, "LOCAL_IS_HOST_MODULE := true")
155 }
Jiyong Parkc43e0ac2018-10-04 20:27:15 +0900156 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", p.outputFilePath.String())
Jiyong Parkc678ad32018-04-10 13:07:10 +0900157 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", "$(OUT_DIR)/"+p.installDirPath.RelPathString())
Jiyong Park139a2e62018-10-26 21:49:39 +0900158 fmt.Fprintln(w, "LOCAL_INSTALLED_MODULE_STEM :=", p.outputFilePath.Base())
Jiyong Parkad9ce042018-10-31 22:49:57 +0900159 fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE :=", !p.Installable())
Anton Hansson4ca89892019-01-14 12:17:40 +0000160 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES :=", strings.Join(data.Required, " "))
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900161 if p.additionalDependencies != nil {
162 fmt.Fprint(w, "LOCAL_ADDITIONAL_DEPENDENCIES :=")
163 for _, path := range *p.additionalDependencies {
164 fmt.Fprint(w, " "+path.String())
165 }
166 fmt.Fprintln(w, "")
167 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900168 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
169 },
170 }
171}
172
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900173func InitPrebuiltEtcModule(p *PrebuiltEtc) {
174 p.AddProperties(&p.properties)
175}
Jiyong Parkc678ad32018-04-10 13:07:10 +0900176
Patrice Arruda9e14b962019-03-11 15:58:50 -0700177// prebuilt_etc is for a prebuilt artifact that is installed in
178// <partition>/etc/<sub_dir> directory.
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900179func PrebuiltEtcFactory() Module {
Jaewoong Jungc3fcdb42019-02-13 05:50:33 -0800180 module := &PrebuiltEtc{installDirBase: "etc"}
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900181 InitPrebuiltEtcModule(module)
182 // This module is device-only
Jaewoong Jungb9a11512019-01-15 10:47:05 -0800183 InitAndroidArchModule(module, DeviceSupported, MultilibFirst)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900184 return module
185}
Tao Bao0ba5c942018-08-14 22:20:22 -0700186
Patrice Arruda9e14b962019-03-11 15:58:50 -0700187// prebuilt_etc_host is for a host prebuilt artifact that is installed in
188// $(HOST_OUT)/etc/<sub_dir> directory.
Jaewoong Jung4b44fcd2019-02-07 08:28:03 -0800189func PrebuiltEtcHostFactory() Module {
Jaewoong Jungc3fcdb42019-02-13 05:50:33 -0800190 module := &PrebuiltEtc{installDirBase: "etc"}
Jaewoong Jung24788182019-02-04 14:34:10 -0800191 InitPrebuiltEtcModule(module)
192 // This module is host-only
193 InitAndroidArchModule(module, HostSupported, MultilibCommon)
194 return module
195}
196
Patrice Arruda9e14b962019-03-11 15:58:50 -0700197// prebuilt_usr_share is for a prebuilt artifact that is installed in
198// <partition>/usr/share/<sub_dir> directory.
Jaewoong Jungc3fcdb42019-02-13 05:50:33 -0800199func PrebuiltUserShareFactory() Module {
200 module := &PrebuiltEtc{installDirBase: "usr/share"}
201 InitPrebuiltEtcModule(module)
202 // This module is device-only
203 InitAndroidArchModule(module, DeviceSupported, MultilibFirst)
204 return module
205}
206
Patrice Arruda9e14b962019-03-11 15:58:50 -0700207// prebuild_usr_share_host is for a host prebuilt artifact that is installed in
208// $(HOST_OUT)/usr/share/<sub_dir> directory.
Patrice Arruda300cef92019-02-22 15:47:57 -0800209func PrebuiltUserShareHostFactory() Module {
210 module := &PrebuiltEtc{installDirBase: "usr/share"}
211 InitPrebuiltEtcModule(module)
212 // This module is host-only
213 InitAndroidArchModule(module, HostSupported, MultilibCommon)
214 return module
215}
216
Tao Bao0ba5c942018-08-14 22:20:22 -0700217const (
218 // coreMode is the variant for modules to be installed to system.
219 coreMode = "core"
220
221 // recoveryMode means a module to be installed to recovery image.
222 recoveryMode = "recovery"
223)
224
225// prebuiltEtcMutator creates the needed variants to install the module to
226// system or recovery.
227func prebuiltEtcMutator(mctx BottomUpMutatorContext) {
228 m, ok := mctx.Module().(*PrebuiltEtc)
Jaewoong Jung24788182019-02-04 14:34:10 -0800229 if !ok || m.Host() {
Tao Bao0ba5c942018-08-14 22:20:22 -0700230 return
231 }
232
233 var coreVariantNeeded bool = true
234 var recoveryVariantNeeded bool = false
235 if Bool(m.properties.Recovery_available) {
236 recoveryVariantNeeded = true
237 }
238
239 if m.ModuleBase.InstallInRecovery() {
240 recoveryVariantNeeded = true
241 coreVariantNeeded = false
242 }
243
244 var variants []string
245 if coreVariantNeeded {
246 variants = append(variants, coreMode)
247 }
248 if recoveryVariantNeeded {
249 variants = append(variants, recoveryMode)
250 }
251 mod := mctx.CreateVariations(variants...)
252 for i, v := range variants {
253 if v == recoveryMode {
254 m := mod[i].(*PrebuiltEtc)
255 m.properties.InRecovery = true
256 }
257 }
258}