blob: fbc9de2861fcd6695f4bef34702556770a7af30b [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
40 // Make this module available when building for recovery.
41 Recovery_available *bool
42
43 InRecovery bool `blueprint:"mutated"`
Jiyong Parkc678ad32018-04-10 13:07:10 +090044}
45
Jiyong Park5a8d1be2018-04-25 22:57:34 +090046type PrebuiltEtc struct {
Jiyong Parkc678ad32018-04-10 13:07:10 +090047 ModuleBase
Jiyong Parkc678ad32018-04-10 13:07:10 +090048
49 properties prebuiltEtcProperties
50
Jiyong Park5a8d1be2018-04-25 22:57:34 +090051 sourceFilePath Path
Jiyong Parkc43e0ac2018-10-04 20:27:15 +090052 outputFilePath OutputPath
Jiyong Park5a8d1be2018-04-25 22:57:34 +090053 installDirPath OutputPath
54 additionalDependencies *Paths
Jiyong Parkc678ad32018-04-10 13:07:10 +090055}
56
Tao Bao0ba5c942018-08-14 22:20:22 -070057func (p *PrebuiltEtc) inRecovery() bool {
58 return p.properties.InRecovery || p.ModuleBase.InstallInRecovery()
59}
60
61func (p *PrebuiltEtc) onlyInRecovery() bool {
62 return p.ModuleBase.InstallInRecovery()
63}
64
65func (p *PrebuiltEtc) InstallInRecovery() bool {
66 return p.inRecovery()
67}
68
Jiyong Park5a8d1be2018-04-25 22:57:34 +090069func (p *PrebuiltEtc) DepsMutator(ctx BottomUpMutatorContext) {
70 if p.properties.Src == nil {
71 ctx.PropertyErrorf("src", "missing prebuilt source file")
Jiyong Parkc678ad32018-04-10 13:07:10 +090072 }
73
74 // To support ":modulename" in src
Jiyong Park5a8d1be2018-04-25 22:57:34 +090075 ExtractSourceDeps(ctx, p.properties.Src)
Jiyong Parkc678ad32018-04-10 13:07:10 +090076}
77
Jiyong Park5a8d1be2018-04-25 22:57:34 +090078func (p *PrebuiltEtc) SourceFilePath(ctx ModuleContext) Path {
79 return ctx.ExpandSource(String(p.properties.Src), "src")
80}
81
82// This allows other derivative modules (e.g. prebuilt_etc_xml) to perform
83// additional steps (like validating the src) before the file is installed.
84func (p *PrebuiltEtc) SetAdditionalDependencies(paths Paths) {
85 p.additionalDependencies = &paths
86}
87
Jiyong Parkc43e0ac2018-10-04 20:27:15 +090088func (p *PrebuiltEtc) OutputFile() OutputPath {
89 return p.outputFilePath
90}
91
92func (p *PrebuiltEtc) SubDir() string {
93 return String(p.properties.Sub_dir)
94}
95
Jiyong Park5a8d1be2018-04-25 22:57:34 +090096func (p *PrebuiltEtc) GenerateAndroidBuildActions(ctx ModuleContext) {
97 p.sourceFilePath = ctx.ExpandSource(String(p.properties.Src), "src")
Jiyong Parkc43e0ac2018-10-04 20:27:15 +090098 p.outputFilePath = PathForModuleOut(ctx, ctx.ModuleName()).OutputPath
Jiyong Parkc678ad32018-04-10 13:07:10 +090099 p.installDirPath = PathForModuleInstall(ctx, "etc", String(p.properties.Sub_dir))
Jiyong Parkc43e0ac2018-10-04 20:27:15 +0900100
101 // This ensures that outputFilePath has the same name as this module.
102 ctx.Build(pctx, BuildParams{
103 Rule: Cp,
104 Output: p.outputFilePath,
105 Input: p.sourceFilePath,
106 })
Jiyong Parkc678ad32018-04-10 13:07:10 +0900107}
108
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900109func (p *PrebuiltEtc) AndroidMk() AndroidMkData {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900110 return AndroidMkData{
111 Custom: func(w io.Writer, name, prefix, moduleDir string, data AndroidMkData) {
Tao Bao0ba5c942018-08-14 22:20:22 -0700112 nameSuffix := ""
113 if p.inRecovery() && !p.onlyInRecovery() {
114 nameSuffix = ".recovery"
115 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900116 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
117 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
Tao Bao0ba5c942018-08-14 22:20:22 -0700118 fmt.Fprintln(w, "LOCAL_MODULE :=", name+nameSuffix)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900119 fmt.Fprintln(w, "LOCAL_MODULE_CLASS := ETC")
120 fmt.Fprintln(w, "LOCAL_MODULE_TAGS := optional")
Jiyong Parkc43e0ac2018-10-04 20:27:15 +0900121 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", p.outputFilePath.String())
Jiyong Parkc678ad32018-04-10 13:07:10 +0900122 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", "$(OUT_DIR)/"+p.installDirPath.RelPathString())
Tao Baoe1255322018-08-21 12:21:48 -0700123 fmt.Fprintln(w, "LOCAL_INSTALLED_MODULE_STEM :=", name)
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900124 if p.additionalDependencies != nil {
125 fmt.Fprint(w, "LOCAL_ADDITIONAL_DEPENDENCIES :=")
126 for _, path := range *p.additionalDependencies {
127 fmt.Fprint(w, " "+path.String())
128 }
129 fmt.Fprintln(w, "")
130 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900131 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
132 },
133 }
134}
135
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900136func InitPrebuiltEtcModule(p *PrebuiltEtc) {
137 p.AddProperties(&p.properties)
138}
Jiyong Parkc678ad32018-04-10 13:07:10 +0900139
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900140func PrebuiltEtcFactory() Module {
141 module := &PrebuiltEtc{}
142 InitPrebuiltEtcModule(module)
143 // This module is device-only
144 InitAndroidArchModule(module, DeviceSupported, MultilibCommon)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900145 return module
146}
Tao Bao0ba5c942018-08-14 22:20:22 -0700147
148const (
149 // coreMode is the variant for modules to be installed to system.
150 coreMode = "core"
151
152 // recoveryMode means a module to be installed to recovery image.
153 recoveryMode = "recovery"
154)
155
156// prebuiltEtcMutator creates the needed variants to install the module to
157// system or recovery.
158func prebuiltEtcMutator(mctx BottomUpMutatorContext) {
159 m, ok := mctx.Module().(*PrebuiltEtc)
160 if !ok {
161 return
162 }
163
164 var coreVariantNeeded bool = true
165 var recoveryVariantNeeded bool = false
166 if Bool(m.properties.Recovery_available) {
167 recoveryVariantNeeded = true
168 }
169
170 if m.ModuleBase.InstallInRecovery() {
171 recoveryVariantNeeded = true
172 coreVariantNeeded = false
173 }
174
175 var variants []string
176 if coreVariantNeeded {
177 variants = append(variants, coreMode)
178 }
179 if recoveryVariantNeeded {
180 variants = append(variants, recoveryMode)
181 }
182 mod := mctx.CreateVariations(variants...)
183 for i, v := range variants {
184 if v == recoveryMode {
185 m := mod[i].(*PrebuiltEtc)
186 m.properties.InRecovery = true
187 }
188 }
189}