blob: 42c7c2c6f9193c7703828ca84d55ad8e06c46e4d [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")
148 fmt.Fprintln(w, "LOCAL_MODULE_TAGS := optional")
Jiyong Parkc43e0ac2018-10-04 20:27:15 +0900149 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", p.outputFilePath.String())
Jiyong Parkc678ad32018-04-10 13:07:10 +0900150 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", "$(OUT_DIR)/"+p.installDirPath.RelPathString())
Jiyong Park139a2e62018-10-26 21:49:39 +0900151 fmt.Fprintln(w, "LOCAL_INSTALLED_MODULE_STEM :=", p.outputFilePath.Base())
Jiyong Parkad9ce042018-10-31 22:49:57 +0900152 fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE :=", !p.Installable())
Anton Hansson4ca89892019-01-14 12:17:40 +0000153 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES :=", strings.Join(data.Required, " "))
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900154 if p.additionalDependencies != nil {
155 fmt.Fprint(w, "LOCAL_ADDITIONAL_DEPENDENCIES :=")
156 for _, path := range *p.additionalDependencies {
157 fmt.Fprint(w, " "+path.String())
158 }
159 fmt.Fprintln(w, "")
160 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900161 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
162 },
163 }
164}
165
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900166func InitPrebuiltEtcModule(p *PrebuiltEtc) {
167 p.AddProperties(&p.properties)
168}
Jiyong Parkc678ad32018-04-10 13:07:10 +0900169
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900170func PrebuiltEtcFactory() Module {
171 module := &PrebuiltEtc{}
172 InitPrebuiltEtcModule(module)
173 // This module is device-only
Jaewoong Jungb9a11512019-01-15 10:47:05 -0800174 InitAndroidArchModule(module, DeviceSupported, MultilibFirst)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900175 return module
176}
Tao Bao0ba5c942018-08-14 22:20:22 -0700177
178const (
179 // coreMode is the variant for modules to be installed to system.
180 coreMode = "core"
181
182 // recoveryMode means a module to be installed to recovery image.
183 recoveryMode = "recovery"
184)
185
186// prebuiltEtcMutator creates the needed variants to install the module to
187// system or recovery.
188func prebuiltEtcMutator(mctx BottomUpMutatorContext) {
189 m, ok := mctx.Module().(*PrebuiltEtc)
190 if !ok {
191 return
192 }
193
194 var coreVariantNeeded bool = true
195 var recoveryVariantNeeded bool = false
196 if Bool(m.properties.Recovery_available) {
197 recoveryVariantNeeded = true
198 }
199
200 if m.ModuleBase.InstallInRecovery() {
201 recoveryVariantNeeded = true
202 coreVariantNeeded = false
203 }
204
205 var variants []string
206 if coreVariantNeeded {
207 variants = append(variants, coreMode)
208 }
209 if recoveryVariantNeeded {
210 variants = append(variants, recoveryMode)
211 }
212 mod := mctx.CreateVariations(variants...)
213 for i, v := range variants {
214 if v == recoveryMode {
215 m := mod[i].(*PrebuiltEtc)
216 m.properties.InRecovery = true
217 }
218 }
219}