| 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 |  | 
| Jaewoong Jung | 9aa3ab1 | 2019-04-03 15:47:29 -0700 | [diff] [blame] | 17 | import "strconv" | 
| Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 18 |  | 
| Jaewoong Jung | c3fcdb4 | 2019-02-13 05:50:33 -0800 | [diff] [blame] | 19 | // TODO(jungw): Now that it handles more than the ones in etc/, consider renaming this file. | 
| Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 20 |  | 
|  | 21 | func init() { | 
|  | 22 | RegisterModuleType("prebuilt_etc", PrebuiltEtcFactory) | 
| Jaewoong Jung | 4b44fcd | 2019-02-07 08:28:03 -0800 | [diff] [blame] | 23 | RegisterModuleType("prebuilt_etc_host", PrebuiltEtcHostFactory) | 
| Jaewoong Jung | c3fcdb4 | 2019-02-13 05:50:33 -0800 | [diff] [blame] | 24 | RegisterModuleType("prebuilt_usr_share", PrebuiltUserShareFactory) | 
| Patrice Arruda | 300cef9 | 2019-02-22 15:47:57 -0800 | [diff] [blame] | 25 | RegisterModuleType("prebuilt_usr_share_host", PrebuiltUserShareHostFactory) | 
| Patrice Arruda | 61583eb | 2019-05-14 08:20:45 -0700 | [diff] [blame^] | 26 | RegisterModuleType("prebuilt_font", PrebuiltFontFactory) | 
| 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. | 
| Colin Cross | 27b922f | 2019-03-04 22:35:41 -0800 | [diff] [blame] | 35 | Src *string `android:"path,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 |  | 
| Jaewoong Jung | c3fcdb4 | 2019-02-13 05:50:33 -0800 | [diff] [blame] | 61 | sourceFilePath Path | 
|  | 62 | outputFilePath OutputPath | 
|  | 63 | // The base install location, e.g. "etc" for prebuilt_etc, "usr/share" for prebuilt_usr_share. | 
|  | 64 | installDirBase         string | 
| Jiyong Park | 5a8d1be | 2018-04-25 22:57:34 +0900 | [diff] [blame] | 65 | installDirPath         OutputPath | 
|  | 66 | additionalDependencies *Paths | 
| Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 67 | } | 
|  | 68 |  | 
| Tao Bao | 0ba5c94 | 2018-08-14 22:20:22 -0700 | [diff] [blame] | 69 | func (p *PrebuiltEtc) inRecovery() bool { | 
|  | 70 | return p.properties.InRecovery || p.ModuleBase.InstallInRecovery() | 
|  | 71 | } | 
|  | 72 |  | 
|  | 73 | func (p *PrebuiltEtc) onlyInRecovery() bool { | 
|  | 74 | return p.ModuleBase.InstallInRecovery() | 
|  | 75 | } | 
|  | 76 |  | 
|  | 77 | func (p *PrebuiltEtc) InstallInRecovery() bool { | 
|  | 78 | return p.inRecovery() | 
|  | 79 | } | 
|  | 80 |  | 
| Jiyong Park | 5a8d1be | 2018-04-25 22:57:34 +0900 | [diff] [blame] | 81 | func (p *PrebuiltEtc) DepsMutator(ctx BottomUpMutatorContext) { | 
|  | 82 | if p.properties.Src == nil { | 
|  | 83 | ctx.PropertyErrorf("src", "missing prebuilt source file") | 
| Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 84 | } | 
| Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 85 | } | 
|  | 86 |  | 
| Jiyong Park | 5a8d1be | 2018-04-25 22:57:34 +0900 | [diff] [blame] | 87 | func (p *PrebuiltEtc) SourceFilePath(ctx ModuleContext) Path { | 
| Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 88 | return PathForModuleSrc(ctx, String(p.properties.Src)) | 
| Jiyong Park | 5a8d1be | 2018-04-25 22:57:34 +0900 | [diff] [blame] | 89 | } | 
|  | 90 |  | 
|  | 91 | // This allows other derivative modules (e.g. prebuilt_etc_xml) to perform | 
|  | 92 | // additional steps (like validating the src) before the file is installed. | 
|  | 93 | func (p *PrebuiltEtc) SetAdditionalDependencies(paths Paths) { | 
|  | 94 | p.additionalDependencies = &paths | 
|  | 95 | } | 
|  | 96 |  | 
| Jiyong Park | c43e0ac | 2018-10-04 20:27:15 +0900 | [diff] [blame] | 97 | func (p *PrebuiltEtc) OutputFile() OutputPath { | 
|  | 98 | return p.outputFilePath | 
|  | 99 | } | 
|  | 100 |  | 
|  | 101 | func (p *PrebuiltEtc) SubDir() string { | 
|  | 102 | return String(p.properties.Sub_dir) | 
|  | 103 | } | 
|  | 104 |  | 
| Jiyong Park | ad9ce04 | 2018-10-31 22:49:57 +0900 | [diff] [blame] | 105 | func (p *PrebuiltEtc) Installable() bool { | 
|  | 106 | return p.properties.Installable == nil || Bool(p.properties.Installable) | 
|  | 107 | } | 
|  | 108 |  | 
| Jiyong Park | 5a8d1be | 2018-04-25 22:57:34 +0900 | [diff] [blame] | 109 | func (p *PrebuiltEtc) GenerateAndroidBuildActions(ctx ModuleContext) { | 
| Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 110 | p.sourceFilePath = PathForModuleSrc(ctx, String(p.properties.Src)) | 
| Jiyong Park | 139a2e6 | 2018-10-26 21:49:39 +0900 | [diff] [blame] | 111 | filename := String(p.properties.Filename) | 
| Jiyong Park | 1a7cf08 | 2018-11-13 11:59:12 +0900 | [diff] [blame] | 112 | filename_from_src := Bool(p.properties.Filename_from_src) | 
| Jiyong Park | 139a2e6 | 2018-10-26 21:49:39 +0900 | [diff] [blame] | 113 | if filename == "" { | 
| Jiyong Park | 1a7cf08 | 2018-11-13 11:59:12 +0900 | [diff] [blame] | 114 | if filename_from_src { | 
|  | 115 | filename = p.sourceFilePath.Base() | 
|  | 116 | } else { | 
|  | 117 | filename = ctx.ModuleName() | 
|  | 118 | } | 
|  | 119 | } else if filename_from_src { | 
|  | 120 | ctx.PropertyErrorf("filename_from_src", "filename is set. filename_from_src can't be true") | 
|  | 121 | return | 
| Jiyong Park | 139a2e6 | 2018-10-26 21:49:39 +0900 | [diff] [blame] | 122 | } | 
|  | 123 | p.outputFilePath = PathForModuleOut(ctx, filename).OutputPath | 
| Jaewoong Jung | c3fcdb4 | 2019-02-13 05:50:33 -0800 | [diff] [blame] | 124 | p.installDirPath = PathForModuleInstall(ctx, p.installDirBase, String(p.properties.Sub_dir)) | 
| Jiyong Park | c43e0ac | 2018-10-04 20:27:15 +0900 | [diff] [blame] | 125 |  | 
| Dan Willemsen | b055267 | 2019-01-25 16:04:11 -0800 | [diff] [blame] | 126 | // This ensures that outputFilePath has the correct name for others to | 
|  | 127 | // use, as the source file may have a different name. | 
| Jiyong Park | c43e0ac | 2018-10-04 20:27:15 +0900 | [diff] [blame] | 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 |  | 
| Jaewoong Jung | 9aa3ab1 | 2019-04-03 15:47:29 -0700 | [diff] [blame] | 135 | func (p *PrebuiltEtc) AndroidMkEntries() AndroidMkEntries { | 
|  | 136 | nameSuffix := "" | 
|  | 137 | if p.inRecovery() && !p.onlyInRecovery() { | 
|  | 138 | nameSuffix = ".recovery" | 
|  | 139 | } | 
|  | 140 | return AndroidMkEntries{ | 
|  | 141 | Class:      "ETC", | 
|  | 142 | SubName:    nameSuffix, | 
|  | 143 | OutputFile: OptionalPathForPath(p.outputFilePath), | 
|  | 144 | AddCustomEntries: func(name, prefix, moduleDir string, entries *AndroidMkEntries) { | 
|  | 145 | entries.SetString("LOCAL_MODULE_TAGS", "optional") | 
|  | 146 | entries.SetString("LOCAL_MODULE_PATH", "$(OUT_DIR)/"+p.installDirPath.RelPathString()) | 
|  | 147 | entries.SetString("LOCAL_INSTALLED_MODULE_STEM", p.outputFilePath.Base()) | 
|  | 148 | entries.SetString("LOCAL_UNINSTALLABLE_MODULE", strconv.FormatBool(!p.Installable())) | 
| Jiyong Park | 5a8d1be | 2018-04-25 22:57:34 +0900 | [diff] [blame] | 149 | if p.additionalDependencies != nil { | 
| Jiyong Park | 5a8d1be | 2018-04-25 22:57:34 +0900 | [diff] [blame] | 150 | for _, path := range *p.additionalDependencies { | 
| Jaewoong Jung | 9aa3ab1 | 2019-04-03 15:47:29 -0700 | [diff] [blame] | 151 | entries.SetString("LOCAL_ADDITIONAL_DEPENDENCIES", path.String()) | 
| Jiyong Park | 5a8d1be | 2018-04-25 22:57:34 +0900 | [diff] [blame] | 152 | } | 
| Jiyong Park | 5a8d1be | 2018-04-25 22:57:34 +0900 | [diff] [blame] | 153 | } | 
| Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 154 | }, | 
|  | 155 | } | 
|  | 156 | } | 
|  | 157 |  | 
| Jiyong Park | 5a8d1be | 2018-04-25 22:57:34 +0900 | [diff] [blame] | 158 | func InitPrebuiltEtcModule(p *PrebuiltEtc) { | 
|  | 159 | p.AddProperties(&p.properties) | 
|  | 160 | } | 
| Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 161 |  | 
| Patrice Arruda | 9e14b96 | 2019-03-11 15:58:50 -0700 | [diff] [blame] | 162 | // prebuilt_etc is for a prebuilt artifact that is installed in | 
|  | 163 | // <partition>/etc/<sub_dir> directory. | 
| Jiyong Park | 5a8d1be | 2018-04-25 22:57:34 +0900 | [diff] [blame] | 164 | func PrebuiltEtcFactory() Module { | 
| Jaewoong Jung | c3fcdb4 | 2019-02-13 05:50:33 -0800 | [diff] [blame] | 165 | module := &PrebuiltEtc{installDirBase: "etc"} | 
| Jiyong Park | 5a8d1be | 2018-04-25 22:57:34 +0900 | [diff] [blame] | 166 | InitPrebuiltEtcModule(module) | 
|  | 167 | // This module is device-only | 
| Jaewoong Jung | b9a1151 | 2019-01-15 10:47:05 -0800 | [diff] [blame] | 168 | InitAndroidArchModule(module, DeviceSupported, MultilibFirst) | 
| Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 169 | return module | 
|  | 170 | } | 
| Tao Bao | 0ba5c94 | 2018-08-14 22:20:22 -0700 | [diff] [blame] | 171 |  | 
| Patrice Arruda | 9e14b96 | 2019-03-11 15:58:50 -0700 | [diff] [blame] | 172 | // prebuilt_etc_host is for a host prebuilt artifact that is installed in | 
|  | 173 | // $(HOST_OUT)/etc/<sub_dir> directory. | 
| Jaewoong Jung | 4b44fcd | 2019-02-07 08:28:03 -0800 | [diff] [blame] | 174 | func PrebuiltEtcHostFactory() Module { | 
| Jaewoong Jung | c3fcdb4 | 2019-02-13 05:50:33 -0800 | [diff] [blame] | 175 | module := &PrebuiltEtc{installDirBase: "etc"} | 
| Jaewoong Jung | 2478818 | 2019-02-04 14:34:10 -0800 | [diff] [blame] | 176 | InitPrebuiltEtcModule(module) | 
|  | 177 | // This module is host-only | 
|  | 178 | InitAndroidArchModule(module, HostSupported, MultilibCommon) | 
|  | 179 | return module | 
|  | 180 | } | 
|  | 181 |  | 
| Patrice Arruda | 9e14b96 | 2019-03-11 15:58:50 -0700 | [diff] [blame] | 182 | // prebuilt_usr_share is for a prebuilt artifact that is installed in | 
|  | 183 | // <partition>/usr/share/<sub_dir> directory. | 
| Jaewoong Jung | c3fcdb4 | 2019-02-13 05:50:33 -0800 | [diff] [blame] | 184 | func PrebuiltUserShareFactory() Module { | 
|  | 185 | module := &PrebuiltEtc{installDirBase: "usr/share"} | 
|  | 186 | InitPrebuiltEtcModule(module) | 
|  | 187 | // This module is device-only | 
|  | 188 | InitAndroidArchModule(module, DeviceSupported, MultilibFirst) | 
|  | 189 | return module | 
|  | 190 | } | 
|  | 191 |  | 
| Patrice Arruda | 9e14b96 | 2019-03-11 15:58:50 -0700 | [diff] [blame] | 192 | // prebuild_usr_share_host is for a host prebuilt artifact that is installed in | 
|  | 193 | // $(HOST_OUT)/usr/share/<sub_dir> directory. | 
| Patrice Arruda | 300cef9 | 2019-02-22 15:47:57 -0800 | [diff] [blame] | 194 | func PrebuiltUserShareHostFactory() Module { | 
|  | 195 | module := &PrebuiltEtc{installDirBase: "usr/share"} | 
|  | 196 | InitPrebuiltEtcModule(module) | 
|  | 197 | // This module is host-only | 
|  | 198 | InitAndroidArchModule(module, HostSupported, MultilibCommon) | 
|  | 199 | return module | 
|  | 200 | } | 
|  | 201 |  | 
| Tao Bao | 0ba5c94 | 2018-08-14 22:20:22 -0700 | [diff] [blame] | 202 | const ( | 
|  | 203 | // coreMode is the variant for modules to be installed to system. | 
|  | 204 | coreMode = "core" | 
|  | 205 |  | 
|  | 206 | // recoveryMode means a module to be installed to recovery image. | 
|  | 207 | recoveryMode = "recovery" | 
|  | 208 | ) | 
|  | 209 |  | 
|  | 210 | // prebuiltEtcMutator creates the needed variants to install the module to | 
|  | 211 | // system or recovery. | 
|  | 212 | func prebuiltEtcMutator(mctx BottomUpMutatorContext) { | 
|  | 213 | m, ok := mctx.Module().(*PrebuiltEtc) | 
| Jaewoong Jung | 2478818 | 2019-02-04 14:34:10 -0800 | [diff] [blame] | 214 | if !ok || m.Host() { | 
| Tao Bao | 0ba5c94 | 2018-08-14 22:20:22 -0700 | [diff] [blame] | 215 | return | 
|  | 216 | } | 
|  | 217 |  | 
|  | 218 | var coreVariantNeeded bool = true | 
|  | 219 | var recoveryVariantNeeded bool = false | 
|  | 220 | if Bool(m.properties.Recovery_available) { | 
|  | 221 | recoveryVariantNeeded = true | 
|  | 222 | } | 
|  | 223 |  | 
|  | 224 | if m.ModuleBase.InstallInRecovery() { | 
|  | 225 | recoveryVariantNeeded = true | 
|  | 226 | coreVariantNeeded = false | 
|  | 227 | } | 
|  | 228 |  | 
|  | 229 | var variants []string | 
|  | 230 | if coreVariantNeeded { | 
|  | 231 | variants = append(variants, coreMode) | 
|  | 232 | } | 
|  | 233 | if recoveryVariantNeeded { | 
|  | 234 | variants = append(variants, recoveryMode) | 
|  | 235 | } | 
|  | 236 | mod := mctx.CreateVariations(variants...) | 
|  | 237 | for i, v := range variants { | 
|  | 238 | if v == recoveryMode { | 
|  | 239 | m := mod[i].(*PrebuiltEtc) | 
|  | 240 | m.properties.InRecovery = true | 
|  | 241 | } | 
|  | 242 | } | 
|  | 243 | } | 
| Patrice Arruda | 61583eb | 2019-05-14 08:20:45 -0700 | [diff] [blame^] | 244 |  | 
|  | 245 | // prebuilt_font installs a font in <partition>/fonts directory. | 
|  | 246 | func PrebuiltFontFactory() Module { | 
|  | 247 | module := &PrebuiltEtc{installDirBase: "fonts"} | 
|  | 248 | InitPrebuiltEtcModule(module) | 
|  | 249 | // This module is device-only | 
|  | 250 | InitAndroidArchModule(module, DeviceSupported, MultilibFirst) | 
|  | 251 | return module | 
|  | 252 | } |