blob: e1803427c4a670add16866b5cd8a3f091dd17b65 [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
23// prebuilt_etc is for prebuilts that will be installed to
24// <partition>/etc/<subdir>
25
26func init() {
27 RegisterModuleType("prebuilt_etc", PrebuiltEtcFactory)
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.
Jiyong Park5a8d1be2018-04-25 22:57:34 +090036 Src *string `android:"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
Jiyong Park5a8d1be2018-04-25 22:57:34 +090057type PrebuiltEtc struct {
Jiyong Parkc678ad32018-04-10 13:07:10 +090058 ModuleBase
Jiyong Parkc678ad32018-04-10 13:07:10 +090059
60 properties prebuiltEtcProperties
61
Jiyong Park5a8d1be2018-04-25 22:57:34 +090062 sourceFilePath Path
Jiyong Parkc43e0ac2018-10-04 20:27:15 +090063 outputFilePath OutputPath
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 }
84
85 // To support ":modulename" in src
Jiyong Park5a8d1be2018-04-25 22:57:34 +090086 ExtractSourceDeps(ctx, p.properties.Src)
Jiyong Parkc678ad32018-04-10 13:07:10 +090087}
88
Jiyong Park5a8d1be2018-04-25 22:57:34 +090089func (p *PrebuiltEtc) SourceFilePath(ctx ModuleContext) Path {
90 return ctx.ExpandSource(String(p.properties.Src), "src")
91}
92
93// This allows other derivative modules (e.g. prebuilt_etc_xml) to perform
94// additional steps (like validating the src) before the file is installed.
95func (p *PrebuiltEtc) SetAdditionalDependencies(paths Paths) {
96 p.additionalDependencies = &paths
97}
98
Jiyong Parkc43e0ac2018-10-04 20:27:15 +090099func (p *PrebuiltEtc) OutputFile() OutputPath {
100 return p.outputFilePath
101}
102
103func (p *PrebuiltEtc) SubDir() string {
104 return String(p.properties.Sub_dir)
105}
106
Jiyong Parkad9ce042018-10-31 22:49:57 +0900107func (p *PrebuiltEtc) Installable() bool {
108 return p.properties.Installable == nil || Bool(p.properties.Installable)
109}
110
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900111func (p *PrebuiltEtc) GenerateAndroidBuildActions(ctx ModuleContext) {
112 p.sourceFilePath = ctx.ExpandSource(String(p.properties.Src), "src")
Jiyong Park139a2e62018-10-26 21:49:39 +0900113 filename := String(p.properties.Filename)
Jiyong Park1a7cf082018-11-13 11:59:12 +0900114 filename_from_src := Bool(p.properties.Filename_from_src)
Jiyong Park139a2e62018-10-26 21:49:39 +0900115 if filename == "" {
Jiyong Park1a7cf082018-11-13 11:59:12 +0900116 if filename_from_src {
117 filename = p.sourceFilePath.Base()
118 } else {
119 filename = ctx.ModuleName()
120 }
121 } else if filename_from_src {
122 ctx.PropertyErrorf("filename_from_src", "filename is set. filename_from_src can't be true")
123 return
Jiyong Park139a2e62018-10-26 21:49:39 +0900124 }
125 p.outputFilePath = PathForModuleOut(ctx, filename).OutputPath
Jiyong Parkc678ad32018-04-10 13:07:10 +0900126 p.installDirPath = PathForModuleInstall(ctx, "etc", String(p.properties.Sub_dir))
Jiyong Parkc43e0ac2018-10-04 20:27:15 +0900127
Dan Willemsenb0552672019-01-25 16:04:11 -0800128 // This ensures that outputFilePath has the correct name for others to
129 // use, as the source file may have a different name.
Jiyong Parkc43e0ac2018-10-04 20:27:15 +0900130 ctx.Build(pctx, BuildParams{
131 Rule: Cp,
132 Output: p.outputFilePath,
133 Input: p.sourceFilePath,
134 })
Jiyong Parkc678ad32018-04-10 13:07:10 +0900135}
136
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900137func (p *PrebuiltEtc) AndroidMk() AndroidMkData {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900138 return AndroidMkData{
139 Custom: func(w io.Writer, name, prefix, moduleDir string, data AndroidMkData) {
Tao Bao0ba5c942018-08-14 22:20:22 -0700140 nameSuffix := ""
141 if p.inRecovery() && !p.onlyInRecovery() {
142 nameSuffix = ".recovery"
143 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900144 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
145 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
Tao Bao0ba5c942018-08-14 22:20:22 -0700146 fmt.Fprintln(w, "LOCAL_MODULE :=", name+nameSuffix)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900147 fmt.Fprintln(w, "LOCAL_MODULE_CLASS := ETC")
Anton Hanssonce0e2582019-02-04 14:19:27 +0000148 if p.commonProperties.Owner != nil {
149 fmt.Fprintln(w, "LOCAL_MODULE_OWNER :=", *p.commonProperties.Owner)
150 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900151 fmt.Fprintln(w, "LOCAL_MODULE_TAGS := optional")
Jiyong Parkc43e0ac2018-10-04 20:27:15 +0900152 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", p.outputFilePath.String())
Jiyong Parkc678ad32018-04-10 13:07:10 +0900153 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", "$(OUT_DIR)/"+p.installDirPath.RelPathString())
Jiyong Park139a2e62018-10-26 21:49:39 +0900154 fmt.Fprintln(w, "LOCAL_INSTALLED_MODULE_STEM :=", p.outputFilePath.Base())
Jiyong Parkad9ce042018-10-31 22:49:57 +0900155 fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE :=", !p.Installable())
Anton Hansson4ca89892019-01-14 12:17:40 +0000156 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES :=", strings.Join(data.Required, " "))
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900157 if p.additionalDependencies != nil {
158 fmt.Fprint(w, "LOCAL_ADDITIONAL_DEPENDENCIES :=")
159 for _, path := range *p.additionalDependencies {
160 fmt.Fprint(w, " "+path.String())
161 }
162 fmt.Fprintln(w, "")
163 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900164 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
165 },
166 }
167}
168
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900169func InitPrebuiltEtcModule(p *PrebuiltEtc) {
170 p.AddProperties(&p.properties)
171}
Jiyong Parkc678ad32018-04-10 13:07:10 +0900172
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900173func PrebuiltEtcFactory() Module {
174 module := &PrebuiltEtc{}
175 InitPrebuiltEtcModule(module)
176 // This module is device-only
Jaewoong Jungb9a11512019-01-15 10:47:05 -0800177 InitAndroidArchModule(module, DeviceSupported, MultilibFirst)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900178 return module
179}
Tao Bao0ba5c942018-08-14 22:20:22 -0700180
181const (
182 // coreMode is the variant for modules to be installed to system.
183 coreMode = "core"
184
185 // recoveryMode means a module to be installed to recovery image.
186 recoveryMode = "recovery"
187)
188
189// prebuiltEtcMutator creates the needed variants to install the module to
190// system or recovery.
191func prebuiltEtcMutator(mctx BottomUpMutatorContext) {
192 m, ok := mctx.Module().(*PrebuiltEtc)
193 if !ok {
194 return
195 }
196
197 var coreVariantNeeded bool = true
198 var recoveryVariantNeeded bool = false
199 if Bool(m.properties.Recovery_available) {
200 recoveryVariantNeeded = true
201 }
202
203 if m.ModuleBase.InstallInRecovery() {
204 recoveryVariantNeeded = true
205 coreVariantNeeded = false
206 }
207
208 var variants []string
209 if coreVariantNeeded {
210 variants = append(variants, coreMode)
211 }
212 if recoveryVariantNeeded {
213 variants = append(variants, recoveryMode)
214 }
215 mod := mctx.CreateVariations(variants...)
216 for i, v := range variants {
217 if v == recoveryMode {
218 m := mod[i].(*PrebuiltEtc)
219 m.properties.InRecovery = true
220 }
221 }
222}