blob: 83c38db07cc7cc1ce6e3a18a3265dbf99bf47fe1 [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"
20)
21
22// prebuilt_etc is for prebuilts that will be installed to
23// <partition>/etc/<subdir>
24
25func init() {
26 RegisterModuleType("prebuilt_etc", PrebuiltEtcFactory)
Tao Bao0ba5c942018-08-14 22:20:22 -070027
28 PreDepsMutators(func(ctx RegisterMutatorsContext) {
29 ctx.BottomUp("prebuilt_etc", prebuiltEtcMutator).Parallel()
30 })
Jiyong Parkc678ad32018-04-10 13:07:10 +090031}
32
33type prebuiltEtcProperties struct {
34 // Source file of this prebuilt.
Jiyong Park5a8d1be2018-04-25 22:57:34 +090035 Src *string `android:"arch_variant"`
Jiyong Parkc678ad32018-04-10 13:07:10 +090036
37 // optional subdirectory under which this file is installed into
38 Sub_dir *string `android:"arch_variant"`
Tao Bao0ba5c942018-08-14 22:20:22 -070039
Jiyong Park139a2e62018-10-26 21:49:39 +090040 // optional name for the installed file. If unspecified, name of the module is used as the file name
41 Filename *string `android:"arch_variant"`
42
Tao Bao0ba5c942018-08-14 22:20:22 -070043 // Make this module available when building for recovery.
44 Recovery_available *bool
45
46 InRecovery bool `blueprint:"mutated"`
Jiyong Parkad9ce042018-10-31 22:49:57 +090047
48 // Whether this module is directly installable to one of the partitions. Default: true.
49 Installable *bool
Jiyong Parkc678ad32018-04-10 13:07:10 +090050}
51
Jiyong Park5a8d1be2018-04-25 22:57:34 +090052type PrebuiltEtc struct {
Jiyong Parkc678ad32018-04-10 13:07:10 +090053 ModuleBase
Jiyong Parkc678ad32018-04-10 13:07:10 +090054
55 properties prebuiltEtcProperties
56
Jiyong Park5a8d1be2018-04-25 22:57:34 +090057 sourceFilePath Path
Jiyong Parkc43e0ac2018-10-04 20:27:15 +090058 outputFilePath OutputPath
Jiyong Park5a8d1be2018-04-25 22:57:34 +090059 installDirPath OutputPath
60 additionalDependencies *Paths
Jiyong Parkc678ad32018-04-10 13:07:10 +090061}
62
Tao Bao0ba5c942018-08-14 22:20:22 -070063func (p *PrebuiltEtc) inRecovery() bool {
64 return p.properties.InRecovery || p.ModuleBase.InstallInRecovery()
65}
66
67func (p *PrebuiltEtc) onlyInRecovery() bool {
68 return p.ModuleBase.InstallInRecovery()
69}
70
71func (p *PrebuiltEtc) InstallInRecovery() bool {
72 return p.inRecovery()
73}
74
Jiyong Park5a8d1be2018-04-25 22:57:34 +090075func (p *PrebuiltEtc) DepsMutator(ctx BottomUpMutatorContext) {
76 if p.properties.Src == nil {
77 ctx.PropertyErrorf("src", "missing prebuilt source file")
Jiyong Parkc678ad32018-04-10 13:07:10 +090078 }
79
80 // To support ":modulename" in src
Jiyong Park5a8d1be2018-04-25 22:57:34 +090081 ExtractSourceDeps(ctx, p.properties.Src)
Jiyong Parkc678ad32018-04-10 13:07:10 +090082}
83
Jiyong Park5a8d1be2018-04-25 22:57:34 +090084func (p *PrebuiltEtc) SourceFilePath(ctx ModuleContext) Path {
85 return ctx.ExpandSource(String(p.properties.Src), "src")
86}
87
88// This allows other derivative modules (e.g. prebuilt_etc_xml) to perform
89// additional steps (like validating the src) before the file is installed.
90func (p *PrebuiltEtc) SetAdditionalDependencies(paths Paths) {
91 p.additionalDependencies = &paths
92}
93
Jiyong Parkc43e0ac2018-10-04 20:27:15 +090094func (p *PrebuiltEtc) OutputFile() OutputPath {
95 return p.outputFilePath
96}
97
98func (p *PrebuiltEtc) SubDir() string {
99 return String(p.properties.Sub_dir)
100}
101
Jiyong Parkad9ce042018-10-31 22:49:57 +0900102func (p *PrebuiltEtc) Installable() bool {
103 return p.properties.Installable == nil || Bool(p.properties.Installable)
104}
105
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900106func (p *PrebuiltEtc) GenerateAndroidBuildActions(ctx ModuleContext) {
107 p.sourceFilePath = ctx.ExpandSource(String(p.properties.Src), "src")
Jiyong Park139a2e62018-10-26 21:49:39 +0900108 filename := String(p.properties.Filename)
109 if filename == "" {
110 filename = ctx.ModuleName()
111 }
112 p.outputFilePath = PathForModuleOut(ctx, filename).OutputPath
Jiyong Parkc678ad32018-04-10 13:07:10 +0900113 p.installDirPath = PathForModuleInstall(ctx, "etc", String(p.properties.Sub_dir))
Jiyong Parkc43e0ac2018-10-04 20:27:15 +0900114
115 // This ensures that outputFilePath has the same name as this module.
116 ctx.Build(pctx, BuildParams{
117 Rule: Cp,
118 Output: p.outputFilePath,
119 Input: p.sourceFilePath,
120 })
Jiyong Parkc678ad32018-04-10 13:07:10 +0900121}
122
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900123func (p *PrebuiltEtc) AndroidMk() AndroidMkData {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900124 return AndroidMkData{
125 Custom: func(w io.Writer, name, prefix, moduleDir string, data AndroidMkData) {
Tao Bao0ba5c942018-08-14 22:20:22 -0700126 nameSuffix := ""
127 if p.inRecovery() && !p.onlyInRecovery() {
128 nameSuffix = ".recovery"
129 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900130 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
131 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
Tao Bao0ba5c942018-08-14 22:20:22 -0700132 fmt.Fprintln(w, "LOCAL_MODULE :=", name+nameSuffix)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900133 fmt.Fprintln(w, "LOCAL_MODULE_CLASS := ETC")
134 fmt.Fprintln(w, "LOCAL_MODULE_TAGS := optional")
Jiyong Parkc43e0ac2018-10-04 20:27:15 +0900135 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", p.outputFilePath.String())
Jiyong Parkc678ad32018-04-10 13:07:10 +0900136 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", "$(OUT_DIR)/"+p.installDirPath.RelPathString())
Jiyong Park139a2e62018-10-26 21:49:39 +0900137 fmt.Fprintln(w, "LOCAL_INSTALLED_MODULE_STEM :=", p.outputFilePath.Base())
Jiyong Parkad9ce042018-10-31 22:49:57 +0900138 fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE :=", !p.Installable())
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900139 if p.additionalDependencies != nil {
140 fmt.Fprint(w, "LOCAL_ADDITIONAL_DEPENDENCIES :=")
141 for _, path := range *p.additionalDependencies {
142 fmt.Fprint(w, " "+path.String())
143 }
144 fmt.Fprintln(w, "")
145 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900146 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
147 },
148 }
149}
150
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900151func InitPrebuiltEtcModule(p *PrebuiltEtc) {
152 p.AddProperties(&p.properties)
153}
Jiyong Parkc678ad32018-04-10 13:07:10 +0900154
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900155func PrebuiltEtcFactory() Module {
156 module := &PrebuiltEtc{}
157 InitPrebuiltEtcModule(module)
158 // This module is device-only
159 InitAndroidArchModule(module, DeviceSupported, MultilibCommon)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900160 return module
161}
Tao Bao0ba5c942018-08-14 22:20:22 -0700162
163const (
164 // coreMode is the variant for modules to be installed to system.
165 coreMode = "core"
166
167 // recoveryMode means a module to be installed to recovery image.
168 recoveryMode = "recovery"
169)
170
171// prebuiltEtcMutator creates the needed variants to install the module to
172// system or recovery.
173func prebuiltEtcMutator(mctx BottomUpMutatorContext) {
174 m, ok := mctx.Module().(*PrebuiltEtc)
175 if !ok {
176 return
177 }
178
179 var coreVariantNeeded bool = true
180 var recoveryVariantNeeded bool = false
181 if Bool(m.properties.Recovery_available) {
182 recoveryVariantNeeded = true
183 }
184
185 if m.ModuleBase.InstallInRecovery() {
186 recoveryVariantNeeded = true
187 coreVariantNeeded = false
188 }
189
190 var variants []string
191 if coreVariantNeeded {
192 variants = append(variants, coreMode)
193 }
194 if recoveryVariantNeeded {
195 variants = append(variants, recoveryMode)
196 }
197 mod := mctx.CreateVariations(variants...)
198 for i, v := range variants {
199 if v == recoveryMode {
200 m := mod[i].(*PrebuiltEtc)
201 m.properties.InRecovery = true
202 }
203 }
204}