blob: 40da653c54b80d4cd1c724db0f61f449b2fc8076 [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
128 // This ensures that outputFilePath has the same name as this module.
129 ctx.Build(pctx, BuildParams{
130 Rule: Cp,
131 Output: p.outputFilePath,
132 Input: p.sourceFilePath,
133 })
Jiyong Parkc678ad32018-04-10 13:07:10 +0900134}
135
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900136func (p *PrebuiltEtc) AndroidMk() AndroidMkData {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900137 return AndroidMkData{
138 Custom: func(w io.Writer, name, prefix, moduleDir string, data AndroidMkData) {
Tao Bao0ba5c942018-08-14 22:20:22 -0700139 nameSuffix := ""
140 if p.inRecovery() && !p.onlyInRecovery() {
141 nameSuffix = ".recovery"
142 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900143 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
144 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
Tao Bao0ba5c942018-08-14 22:20:22 -0700145 fmt.Fprintln(w, "LOCAL_MODULE :=", name+nameSuffix)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900146 fmt.Fprintln(w, "LOCAL_MODULE_CLASS := ETC")
147 fmt.Fprintln(w, "LOCAL_MODULE_TAGS := optional")
Jiyong Parkc43e0ac2018-10-04 20:27:15 +0900148 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", p.outputFilePath.String())
Jiyong Parkc678ad32018-04-10 13:07:10 +0900149 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", "$(OUT_DIR)/"+p.installDirPath.RelPathString())
Jiyong Park139a2e62018-10-26 21:49:39 +0900150 fmt.Fprintln(w, "LOCAL_INSTALLED_MODULE_STEM :=", p.outputFilePath.Base())
Jiyong Parkad9ce042018-10-31 22:49:57 +0900151 fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE :=", !p.Installable())
Anton Hansson4ca89892019-01-14 12:17:40 +0000152 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES :=", strings.Join(data.Required, " "))
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900153 if p.additionalDependencies != nil {
154 fmt.Fprint(w, "LOCAL_ADDITIONAL_DEPENDENCIES :=")
155 for _, path := range *p.additionalDependencies {
156 fmt.Fprint(w, " "+path.String())
157 }
158 fmt.Fprintln(w, "")
159 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900160 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
161 },
162 }
163}
164
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900165func InitPrebuiltEtcModule(p *PrebuiltEtc) {
166 p.AddProperties(&p.properties)
167}
Jiyong Parkc678ad32018-04-10 13:07:10 +0900168
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900169func PrebuiltEtcFactory() Module {
170 module := &PrebuiltEtc{}
171 InitPrebuiltEtcModule(module)
172 // This module is device-only
173 InitAndroidArchModule(module, DeviceSupported, MultilibCommon)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900174 return module
175}
Tao Bao0ba5c942018-08-14 22:20:22 -0700176
177const (
178 // coreMode is the variant for modules to be installed to system.
179 coreMode = "core"
180
181 // recoveryMode means a module to be installed to recovery image.
182 recoveryMode = "recovery"
183)
184
185// prebuiltEtcMutator creates the needed variants to install the module to
186// system or recovery.
187func prebuiltEtcMutator(mctx BottomUpMutatorContext) {
188 m, ok := mctx.Module().(*PrebuiltEtc)
189 if !ok {
190 return
191 }
192
193 var coreVariantNeeded bool = true
194 var recoveryVariantNeeded bool = false
195 if Bool(m.properties.Recovery_available) {
196 recoveryVariantNeeded = true
197 }
198
199 if m.ModuleBase.InstallInRecovery() {
200 recoveryVariantNeeded = true
201 coreVariantNeeded = false
202 }
203
204 var variants []string
205 if coreVariantNeeded {
206 variants = append(variants, coreMode)
207 }
208 if recoveryVariantNeeded {
209 variants = append(variants, recoveryMode)
210 }
211 mod := mctx.CreateVariations(variants...)
212 for i, v := range variants {
213 if v == recoveryMode {
214 m := mod[i].(*PrebuiltEtc)
215 m.properties.InRecovery = true
216 }
217 }
218}