blob: 046512f2758b7ab75c70a35a097f0b685e2d223a [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())
Tao Baoe1255322018-08-21 12:21:48 -0700106 fmt.Fprintln(w, "LOCAL_INSTALLED_MODULE_STEM :=", name)
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900107 if p.additionalDependencies != nil {
108 fmt.Fprint(w, "LOCAL_ADDITIONAL_DEPENDENCIES :=")
109 for _, path := range *p.additionalDependencies {
110 fmt.Fprint(w, " "+path.String())
111 }
112 fmt.Fprintln(w, "")
113 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900114 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
115 },
116 }
117}
118
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900119func InitPrebuiltEtcModule(p *PrebuiltEtc) {
120 p.AddProperties(&p.properties)
121}
Jiyong Parkc678ad32018-04-10 13:07:10 +0900122
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900123func PrebuiltEtcFactory() Module {
124 module := &PrebuiltEtc{}
125 InitPrebuiltEtcModule(module)
126 // This module is device-only
127 InitAndroidArchModule(module, DeviceSupported, MultilibCommon)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900128 return module
129}
Tao Bao0ba5c942018-08-14 22:20:22 -0700130
131const (
132 // coreMode is the variant for modules to be installed to system.
133 coreMode = "core"
134
135 // recoveryMode means a module to be installed to recovery image.
136 recoveryMode = "recovery"
137)
138
139// prebuiltEtcMutator creates the needed variants to install the module to
140// system or recovery.
141func prebuiltEtcMutator(mctx BottomUpMutatorContext) {
142 m, ok := mctx.Module().(*PrebuiltEtc)
143 if !ok {
144 return
145 }
146
147 var coreVariantNeeded bool = true
148 var recoveryVariantNeeded bool = false
149 if Bool(m.properties.Recovery_available) {
150 recoveryVariantNeeded = true
151 }
152
153 if m.ModuleBase.InstallInRecovery() {
154 recoveryVariantNeeded = true
155 coreVariantNeeded = false
156 }
157
158 var variants []string
159 if coreVariantNeeded {
160 variants = append(variants, coreMode)
161 }
162 if recoveryVariantNeeded {
163 variants = append(variants, recoveryMode)
164 }
165 mod := mctx.CreateVariations(variants...)
166 for i, v := range variants {
167 if v == recoveryMode {
168 m := mod[i].(*PrebuiltEtc)
169 m.properties.InRecovery = true
170 }
171 }
172}