Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 1 | // 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 | |
| 15 | package android |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
| 19 | "io" |
| 20 | ) |
| 21 | |
| 22 | // prebuilt_etc is for prebuilts that will be installed to |
| 23 | // <partition>/etc/<subdir> |
| 24 | |
| 25 | func init() { |
| 26 | RegisterModuleType("prebuilt_etc", PrebuiltEtcFactory) |
Tao Bao | 0ba5c94 | 2018-08-14 22:20:22 -0700 | [diff] [blame] | 27 | |
| 28 | PreDepsMutators(func(ctx RegisterMutatorsContext) { |
| 29 | ctx.BottomUp("prebuilt_etc", prebuiltEtcMutator).Parallel() |
| 30 | }) |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | type prebuiltEtcProperties struct { |
| 34 | // Source file of this prebuilt. |
Jiyong Park | 5a8d1be | 2018-04-25 22:57:34 +0900 | [diff] [blame] | 35 | Src *string `android:"arch_variant"` |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 36 | |
| 37 | // optional subdirectory under which this file is installed into |
| 38 | Sub_dir *string `android:"arch_variant"` |
Tao Bao | 0ba5c94 | 2018-08-14 22:20:22 -0700 | [diff] [blame] | 39 | |
Jiyong Park | 139a2e6 | 2018-10-26 21:49:39 +0900 | [diff] [blame^] | 40 | // 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 Bao | 0ba5c94 | 2018-08-14 22:20:22 -0700 | [diff] [blame] | 43 | // Make this module available when building for recovery. |
| 44 | Recovery_available *bool |
| 45 | |
| 46 | InRecovery bool `blueprint:"mutated"` |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 47 | } |
| 48 | |
Jiyong Park | 5a8d1be | 2018-04-25 22:57:34 +0900 | [diff] [blame] | 49 | type PrebuiltEtc struct { |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 50 | ModuleBase |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 51 | |
| 52 | properties prebuiltEtcProperties |
| 53 | |
Jiyong Park | 5a8d1be | 2018-04-25 22:57:34 +0900 | [diff] [blame] | 54 | sourceFilePath Path |
Jiyong Park | c43e0ac | 2018-10-04 20:27:15 +0900 | [diff] [blame] | 55 | outputFilePath OutputPath |
Jiyong Park | 5a8d1be | 2018-04-25 22:57:34 +0900 | [diff] [blame] | 56 | installDirPath OutputPath |
| 57 | additionalDependencies *Paths |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 58 | } |
| 59 | |
Tao Bao | 0ba5c94 | 2018-08-14 22:20:22 -0700 | [diff] [blame] | 60 | func (p *PrebuiltEtc) inRecovery() bool { |
| 61 | return p.properties.InRecovery || p.ModuleBase.InstallInRecovery() |
| 62 | } |
| 63 | |
| 64 | func (p *PrebuiltEtc) onlyInRecovery() bool { |
| 65 | return p.ModuleBase.InstallInRecovery() |
| 66 | } |
| 67 | |
| 68 | func (p *PrebuiltEtc) InstallInRecovery() bool { |
| 69 | return p.inRecovery() |
| 70 | } |
| 71 | |
Jiyong Park | 5a8d1be | 2018-04-25 22:57:34 +0900 | [diff] [blame] | 72 | func (p *PrebuiltEtc) DepsMutator(ctx BottomUpMutatorContext) { |
| 73 | if p.properties.Src == nil { |
| 74 | ctx.PropertyErrorf("src", "missing prebuilt source file") |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | // To support ":modulename" in src |
Jiyong Park | 5a8d1be | 2018-04-25 22:57:34 +0900 | [diff] [blame] | 78 | ExtractSourceDeps(ctx, p.properties.Src) |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 79 | } |
| 80 | |
Jiyong Park | 5a8d1be | 2018-04-25 22:57:34 +0900 | [diff] [blame] | 81 | func (p *PrebuiltEtc) SourceFilePath(ctx ModuleContext) Path { |
| 82 | return ctx.ExpandSource(String(p.properties.Src), "src") |
| 83 | } |
| 84 | |
| 85 | // This allows other derivative modules (e.g. prebuilt_etc_xml) to perform |
| 86 | // additional steps (like validating the src) before the file is installed. |
| 87 | func (p *PrebuiltEtc) SetAdditionalDependencies(paths Paths) { |
| 88 | p.additionalDependencies = &paths |
| 89 | } |
| 90 | |
Jiyong Park | c43e0ac | 2018-10-04 20:27:15 +0900 | [diff] [blame] | 91 | func (p *PrebuiltEtc) OutputFile() OutputPath { |
| 92 | return p.outputFilePath |
| 93 | } |
| 94 | |
| 95 | func (p *PrebuiltEtc) SubDir() string { |
| 96 | return String(p.properties.Sub_dir) |
| 97 | } |
| 98 | |
Jiyong Park | 5a8d1be | 2018-04-25 22:57:34 +0900 | [diff] [blame] | 99 | func (p *PrebuiltEtc) GenerateAndroidBuildActions(ctx ModuleContext) { |
| 100 | p.sourceFilePath = ctx.ExpandSource(String(p.properties.Src), "src") |
Jiyong Park | 139a2e6 | 2018-10-26 21:49:39 +0900 | [diff] [blame^] | 101 | filename := String(p.properties.Filename) |
| 102 | if filename == "" { |
| 103 | filename = ctx.ModuleName() |
| 104 | } |
| 105 | p.outputFilePath = PathForModuleOut(ctx, filename).OutputPath |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 106 | p.installDirPath = PathForModuleInstall(ctx, "etc", String(p.properties.Sub_dir)) |
Jiyong Park | c43e0ac | 2018-10-04 20:27:15 +0900 | [diff] [blame] | 107 | |
| 108 | // This ensures that outputFilePath has the same name as this module. |
| 109 | ctx.Build(pctx, BuildParams{ |
| 110 | Rule: Cp, |
| 111 | Output: p.outputFilePath, |
| 112 | Input: p.sourceFilePath, |
| 113 | }) |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 114 | } |
| 115 | |
Jiyong Park | 5a8d1be | 2018-04-25 22:57:34 +0900 | [diff] [blame] | 116 | func (p *PrebuiltEtc) AndroidMk() AndroidMkData { |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 117 | return AndroidMkData{ |
| 118 | Custom: func(w io.Writer, name, prefix, moduleDir string, data AndroidMkData) { |
Tao Bao | 0ba5c94 | 2018-08-14 22:20:22 -0700 | [diff] [blame] | 119 | nameSuffix := "" |
| 120 | if p.inRecovery() && !p.onlyInRecovery() { |
| 121 | nameSuffix = ".recovery" |
| 122 | } |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 123 | fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)") |
| 124 | fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir) |
Tao Bao | 0ba5c94 | 2018-08-14 22:20:22 -0700 | [diff] [blame] | 125 | fmt.Fprintln(w, "LOCAL_MODULE :=", name+nameSuffix) |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 126 | fmt.Fprintln(w, "LOCAL_MODULE_CLASS := ETC") |
| 127 | fmt.Fprintln(w, "LOCAL_MODULE_TAGS := optional") |
Jiyong Park | c43e0ac | 2018-10-04 20:27:15 +0900 | [diff] [blame] | 128 | fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", p.outputFilePath.String()) |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 129 | fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", "$(OUT_DIR)/"+p.installDirPath.RelPathString()) |
Jiyong Park | 139a2e6 | 2018-10-26 21:49:39 +0900 | [diff] [blame^] | 130 | fmt.Fprintln(w, "LOCAL_INSTALLED_MODULE_STEM :=", p.outputFilePath.Base()) |
Jiyong Park | 5a8d1be | 2018-04-25 22:57:34 +0900 | [diff] [blame] | 131 | if p.additionalDependencies != nil { |
| 132 | fmt.Fprint(w, "LOCAL_ADDITIONAL_DEPENDENCIES :=") |
| 133 | for _, path := range *p.additionalDependencies { |
| 134 | fmt.Fprint(w, " "+path.String()) |
| 135 | } |
| 136 | fmt.Fprintln(w, "") |
| 137 | } |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 138 | fmt.Fprintln(w, "include $(BUILD_PREBUILT)") |
| 139 | }, |
| 140 | } |
| 141 | } |
| 142 | |
Jiyong Park | 5a8d1be | 2018-04-25 22:57:34 +0900 | [diff] [blame] | 143 | func InitPrebuiltEtcModule(p *PrebuiltEtc) { |
| 144 | p.AddProperties(&p.properties) |
| 145 | } |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 146 | |
Jiyong Park | 5a8d1be | 2018-04-25 22:57:34 +0900 | [diff] [blame] | 147 | func PrebuiltEtcFactory() Module { |
| 148 | module := &PrebuiltEtc{} |
| 149 | InitPrebuiltEtcModule(module) |
| 150 | // This module is device-only |
| 151 | InitAndroidArchModule(module, DeviceSupported, MultilibCommon) |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 152 | return module |
| 153 | } |
Tao Bao | 0ba5c94 | 2018-08-14 22:20:22 -0700 | [diff] [blame] | 154 | |
| 155 | const ( |
| 156 | // coreMode is the variant for modules to be installed to system. |
| 157 | coreMode = "core" |
| 158 | |
| 159 | // recoveryMode means a module to be installed to recovery image. |
| 160 | recoveryMode = "recovery" |
| 161 | ) |
| 162 | |
| 163 | // prebuiltEtcMutator creates the needed variants to install the module to |
| 164 | // system or recovery. |
| 165 | func prebuiltEtcMutator(mctx BottomUpMutatorContext) { |
| 166 | m, ok := mctx.Module().(*PrebuiltEtc) |
| 167 | if !ok { |
| 168 | return |
| 169 | } |
| 170 | |
| 171 | var coreVariantNeeded bool = true |
| 172 | var recoveryVariantNeeded bool = false |
| 173 | if Bool(m.properties.Recovery_available) { |
| 174 | recoveryVariantNeeded = true |
| 175 | } |
| 176 | |
| 177 | if m.ModuleBase.InstallInRecovery() { |
| 178 | recoveryVariantNeeded = true |
| 179 | coreVariantNeeded = false |
| 180 | } |
| 181 | |
| 182 | var variants []string |
| 183 | if coreVariantNeeded { |
| 184 | variants = append(variants, coreMode) |
| 185 | } |
| 186 | if recoveryVariantNeeded { |
| 187 | variants = append(variants, recoveryMode) |
| 188 | } |
| 189 | mod := mctx.CreateVariations(variants...) |
| 190 | for i, v := range variants { |
| 191 | if v == recoveryMode { |
| 192 | m := mod[i].(*PrebuiltEtc) |
| 193 | m.properties.InRecovery = true |
| 194 | } |
| 195 | } |
| 196 | } |