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