blob: 12fefab7faaf939fbc6ce582ec87517fb4668156 [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
52 installDirPath OutputPath
53 additionalDependencies *Paths
Jiyong Parkc678ad32018-04-10 13:07:10 +090054}
55
Tao Bao0ba5c942018-08-14 22:20:22 -070056func (p *PrebuiltEtc) inRecovery() bool {
57 return p.properties.InRecovery || p.ModuleBase.InstallInRecovery()
58}
59
60func (p *PrebuiltEtc) onlyInRecovery() bool {
61 return p.ModuleBase.InstallInRecovery()
62}
63
64func (p *PrebuiltEtc) InstallInRecovery() bool {
65 return p.inRecovery()
66}
67
Jiyong Park5a8d1be2018-04-25 22:57:34 +090068func (p *PrebuiltEtc) DepsMutator(ctx BottomUpMutatorContext) {
69 if p.properties.Src == nil {
70 ctx.PropertyErrorf("src", "missing prebuilt source file")
Jiyong Parkc678ad32018-04-10 13:07:10 +090071 }
72
73 // To support ":modulename" in src
Jiyong Park5a8d1be2018-04-25 22:57:34 +090074 ExtractSourceDeps(ctx, p.properties.Src)
Jiyong Parkc678ad32018-04-10 13:07:10 +090075}
76
Jiyong Park5a8d1be2018-04-25 22:57:34 +090077func (p *PrebuiltEtc) SourceFilePath(ctx ModuleContext) Path {
78 return ctx.ExpandSource(String(p.properties.Src), "src")
79}
80
81// This allows other derivative modules (e.g. prebuilt_etc_xml) to perform
82// additional steps (like validating the src) before the file is installed.
83func (p *PrebuiltEtc) SetAdditionalDependencies(paths Paths) {
84 p.additionalDependencies = &paths
85}
86
87func (p *PrebuiltEtc) GenerateAndroidBuildActions(ctx ModuleContext) {
88 p.sourceFilePath = ctx.ExpandSource(String(p.properties.Src), "src")
Jiyong Parkc678ad32018-04-10 13:07:10 +090089 p.installDirPath = PathForModuleInstall(ctx, "etc", String(p.properties.Sub_dir))
90}
91
Jiyong Park5a8d1be2018-04-25 22:57:34 +090092func (p *PrebuiltEtc) AndroidMk() AndroidMkData {
Jiyong Parkc678ad32018-04-10 13:07:10 +090093 return AndroidMkData{
94 Custom: func(w io.Writer, name, prefix, moduleDir string, data AndroidMkData) {
Tao Bao0ba5c942018-08-14 22:20:22 -070095 nameSuffix := ""
96 if p.inRecovery() && !p.onlyInRecovery() {
97 nameSuffix = ".recovery"
98 }
Jiyong Parkc678ad32018-04-10 13:07:10 +090099 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
100 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
Tao Bao0ba5c942018-08-14 22:20:22 -0700101 fmt.Fprintln(w, "LOCAL_MODULE :=", name+nameSuffix)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900102 fmt.Fprintln(w, "LOCAL_MODULE_CLASS := ETC")
103 fmt.Fprintln(w, "LOCAL_MODULE_TAGS := optional")
104 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", p.sourceFilePath.String())
105 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", "$(OUT_DIR)/"+p.installDirPath.RelPathString())
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900106 if p.additionalDependencies != nil {
107 fmt.Fprint(w, "LOCAL_ADDITIONAL_DEPENDENCIES :=")
108 for _, path := range *p.additionalDependencies {
109 fmt.Fprint(w, " "+path.String())
110 }
111 fmt.Fprintln(w, "")
112 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900113 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
114 },
115 }
116}
117
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900118func InitPrebuiltEtcModule(p *PrebuiltEtc) {
119 p.AddProperties(&p.properties)
120}
Jiyong Parkc678ad32018-04-10 13:07:10 +0900121
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900122func PrebuiltEtcFactory() Module {
123 module := &PrebuiltEtc{}
124 InitPrebuiltEtcModule(module)
125 // This module is device-only
126 InitAndroidArchModule(module, DeviceSupported, MultilibCommon)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900127 return module
128}
Tao Bao0ba5c942018-08-14 22:20:22 -0700129
130const (
131 // coreMode is the variant for modules to be installed to system.
132 coreMode = "core"
133
134 // recoveryMode means a module to be installed to recovery image.
135 recoveryMode = "recovery"
136)
137
138// prebuiltEtcMutator creates the needed variants to install the module to
139// system or recovery.
140func prebuiltEtcMutator(mctx BottomUpMutatorContext) {
141 m, ok := mctx.Module().(*PrebuiltEtc)
142 if !ok {
143 return
144 }
145
146 var coreVariantNeeded bool = true
147 var recoveryVariantNeeded bool = false
148 if Bool(m.properties.Recovery_available) {
149 recoveryVariantNeeded = true
150 }
151
152 if m.ModuleBase.InstallInRecovery() {
153 recoveryVariantNeeded = true
154 coreVariantNeeded = false
155 }
156
157 var variants []string
158 if coreVariantNeeded {
159 variants = append(variants, coreMode)
160 }
161 if recoveryVariantNeeded {
162 variants = append(variants, recoveryMode)
163 }
164 mod := mctx.CreateVariations(variants...)
165 for i, v := range variants {
166 if v == recoveryMode {
167 m := mod[i].(*PrebuiltEtc)
168 m.properties.InRecovery = true
169 }
170 }
171}