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" |
Anton Hansson | 4ca8989 | 2019-01-14 12:17:40 +0000 | [diff] [blame] | 20 | "strings" |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 21 | ) |
| 22 | |
| 23 | // prebuilt_etc is for prebuilts that will be installed to |
| 24 | // <partition>/etc/<subdir> |
| 25 | |
| 26 | func init() { |
| 27 | RegisterModuleType("prebuilt_etc", PrebuiltEtcFactory) |
Jaewoong Jung | 2478818 | 2019-02-04 14:34:10 -0800 | [diff] [blame^] | 28 | RegisterModuleType("prebuilt_etc_host", prebuiltEtcHostFactory) |
Tao Bao | 0ba5c94 | 2018-08-14 22:20:22 -0700 | [diff] [blame] | 29 | |
| 30 | PreDepsMutators(func(ctx RegisterMutatorsContext) { |
| 31 | ctx.BottomUp("prebuilt_etc", prebuiltEtcMutator).Parallel() |
| 32 | }) |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | type prebuiltEtcProperties struct { |
| 36 | // Source file of this prebuilt. |
Jiyong Park | 5a8d1be | 2018-04-25 22:57:34 +0900 | [diff] [blame] | 37 | Src *string `android:"arch_variant"` |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 38 | |
| 39 | // optional subdirectory under which this file is installed into |
| 40 | Sub_dir *string `android:"arch_variant"` |
Tao Bao | 0ba5c94 | 2018-08-14 22:20:22 -0700 | [diff] [blame] | 41 | |
Jiyong Park | 139a2e6 | 2018-10-26 21:49:39 +0900 | [diff] [blame] | 42 | // optional name for the installed file. If unspecified, name of the module is used as the file name |
| 43 | Filename *string `android:"arch_variant"` |
| 44 | |
Jiyong Park | 1a7cf08 | 2018-11-13 11:59:12 +0900 | [diff] [blame] | 45 | // when set to true, and filename property is not set, the name for the installed file |
| 46 | // is the same as the file name of the source file. |
| 47 | Filename_from_src *bool `android:"arch_variant"` |
| 48 | |
Tao Bao | 0ba5c94 | 2018-08-14 22:20:22 -0700 | [diff] [blame] | 49 | // Make this module available when building for recovery. |
| 50 | Recovery_available *bool |
| 51 | |
| 52 | InRecovery bool `blueprint:"mutated"` |
Jiyong Park | ad9ce04 | 2018-10-31 22:49:57 +0900 | [diff] [blame] | 53 | |
| 54 | // Whether this module is directly installable to one of the partitions. Default: true. |
| 55 | Installable *bool |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 56 | } |
| 57 | |
Jiyong Park | 5a8d1be | 2018-04-25 22:57:34 +0900 | [diff] [blame] | 58 | type PrebuiltEtc struct { |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 59 | ModuleBase |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 60 | |
| 61 | properties prebuiltEtcProperties |
| 62 | |
Jiyong Park | 5a8d1be | 2018-04-25 22:57:34 +0900 | [diff] [blame] | 63 | sourceFilePath Path |
Jiyong Park | c43e0ac | 2018-10-04 20:27:15 +0900 | [diff] [blame] | 64 | outputFilePath OutputPath |
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 | } |
| 85 | |
| 86 | // To support ":modulename" in src |
Jiyong Park | 5a8d1be | 2018-04-25 22:57:34 +0900 | [diff] [blame] | 87 | ExtractSourceDeps(ctx, p.properties.Src) |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 88 | } |
| 89 | |
Jiyong Park | 5a8d1be | 2018-04-25 22:57:34 +0900 | [diff] [blame] | 90 | func (p *PrebuiltEtc) SourceFilePath(ctx ModuleContext) Path { |
| 91 | return ctx.ExpandSource(String(p.properties.Src), "src") |
| 92 | } |
| 93 | |
| 94 | // This allows other derivative modules (e.g. prebuilt_etc_xml) to perform |
| 95 | // additional steps (like validating the src) before the file is installed. |
| 96 | func (p *PrebuiltEtc) SetAdditionalDependencies(paths Paths) { |
| 97 | p.additionalDependencies = &paths |
| 98 | } |
| 99 | |
Jiyong Park | c43e0ac | 2018-10-04 20:27:15 +0900 | [diff] [blame] | 100 | func (p *PrebuiltEtc) OutputFile() OutputPath { |
| 101 | return p.outputFilePath |
| 102 | } |
| 103 | |
| 104 | func (p *PrebuiltEtc) SubDir() string { |
| 105 | return String(p.properties.Sub_dir) |
| 106 | } |
| 107 | |
Jiyong Park | ad9ce04 | 2018-10-31 22:49:57 +0900 | [diff] [blame] | 108 | func (p *PrebuiltEtc) Installable() bool { |
| 109 | return p.properties.Installable == nil || Bool(p.properties.Installable) |
| 110 | } |
| 111 | |
Jiyong Park | 5a8d1be | 2018-04-25 22:57:34 +0900 | [diff] [blame] | 112 | func (p *PrebuiltEtc) GenerateAndroidBuildActions(ctx ModuleContext) { |
| 113 | p.sourceFilePath = ctx.ExpandSource(String(p.properties.Src), "src") |
Jiyong Park | 139a2e6 | 2018-10-26 21:49:39 +0900 | [diff] [blame] | 114 | filename := String(p.properties.Filename) |
Jiyong Park | 1a7cf08 | 2018-11-13 11:59:12 +0900 | [diff] [blame] | 115 | filename_from_src := Bool(p.properties.Filename_from_src) |
Jiyong Park | 139a2e6 | 2018-10-26 21:49:39 +0900 | [diff] [blame] | 116 | if filename == "" { |
Jiyong Park | 1a7cf08 | 2018-11-13 11:59:12 +0900 | [diff] [blame] | 117 | if filename_from_src { |
| 118 | filename = p.sourceFilePath.Base() |
| 119 | } else { |
| 120 | filename = ctx.ModuleName() |
| 121 | } |
| 122 | } else if filename_from_src { |
| 123 | ctx.PropertyErrorf("filename_from_src", "filename is set. filename_from_src can't be true") |
| 124 | return |
Jiyong Park | 139a2e6 | 2018-10-26 21:49:39 +0900 | [diff] [blame] | 125 | } |
| 126 | p.outputFilePath = PathForModuleOut(ctx, filename).OutputPath |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 127 | p.installDirPath = PathForModuleInstall(ctx, "etc", String(p.properties.Sub_dir)) |
Jiyong Park | c43e0ac | 2018-10-04 20:27:15 +0900 | [diff] [blame] | 128 | |
Dan Willemsen | b055267 | 2019-01-25 16:04:11 -0800 | [diff] [blame] | 129 | // This ensures that outputFilePath has the correct name for others to |
| 130 | // use, as the source file may have a different name. |
Jiyong Park | c43e0ac | 2018-10-04 20:27:15 +0900 | [diff] [blame] | 131 | ctx.Build(pctx, BuildParams{ |
| 132 | Rule: Cp, |
| 133 | Output: p.outputFilePath, |
| 134 | Input: p.sourceFilePath, |
| 135 | }) |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 136 | } |
| 137 | |
Jiyong Park | 5a8d1be | 2018-04-25 22:57:34 +0900 | [diff] [blame] | 138 | func (p *PrebuiltEtc) AndroidMk() AndroidMkData { |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 139 | return AndroidMkData{ |
| 140 | Custom: func(w io.Writer, name, prefix, moduleDir string, data AndroidMkData) { |
Tao Bao | 0ba5c94 | 2018-08-14 22:20:22 -0700 | [diff] [blame] | 141 | nameSuffix := "" |
| 142 | if p.inRecovery() && !p.onlyInRecovery() { |
| 143 | nameSuffix = ".recovery" |
| 144 | } |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 145 | fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)") |
| 146 | fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir) |
Tao Bao | 0ba5c94 | 2018-08-14 22:20:22 -0700 | [diff] [blame] | 147 | fmt.Fprintln(w, "LOCAL_MODULE :=", name+nameSuffix) |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 148 | fmt.Fprintln(w, "LOCAL_MODULE_CLASS := ETC") |
Anton Hansson | ce0e258 | 2019-02-04 14:19:27 +0000 | [diff] [blame] | 149 | if p.commonProperties.Owner != nil { |
| 150 | fmt.Fprintln(w, "LOCAL_MODULE_OWNER :=", *p.commonProperties.Owner) |
| 151 | } |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 152 | fmt.Fprintln(w, "LOCAL_MODULE_TAGS := optional") |
Jaewoong Jung | 2478818 | 2019-02-04 14:34:10 -0800 | [diff] [blame^] | 153 | if p.Host() { |
| 154 | fmt.Fprintln(w, "LOCAL_IS_HOST_MODULE := true") |
| 155 | } |
Jiyong Park | c43e0ac | 2018-10-04 20:27:15 +0900 | [diff] [blame] | 156 | fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", p.outputFilePath.String()) |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 157 | fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", "$(OUT_DIR)/"+p.installDirPath.RelPathString()) |
Jiyong Park | 139a2e6 | 2018-10-26 21:49:39 +0900 | [diff] [blame] | 158 | fmt.Fprintln(w, "LOCAL_INSTALLED_MODULE_STEM :=", p.outputFilePath.Base()) |
Jiyong Park | ad9ce04 | 2018-10-31 22:49:57 +0900 | [diff] [blame] | 159 | fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE :=", !p.Installable()) |
Anton Hansson | 4ca8989 | 2019-01-14 12:17:40 +0000 | [diff] [blame] | 160 | fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES :=", strings.Join(data.Required, " ")) |
Jiyong Park | 5a8d1be | 2018-04-25 22:57:34 +0900 | [diff] [blame] | 161 | if p.additionalDependencies != nil { |
| 162 | fmt.Fprint(w, "LOCAL_ADDITIONAL_DEPENDENCIES :=") |
| 163 | for _, path := range *p.additionalDependencies { |
| 164 | fmt.Fprint(w, " "+path.String()) |
| 165 | } |
| 166 | fmt.Fprintln(w, "") |
| 167 | } |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 168 | fmt.Fprintln(w, "include $(BUILD_PREBUILT)") |
| 169 | }, |
| 170 | } |
| 171 | } |
| 172 | |
Jiyong Park | 5a8d1be | 2018-04-25 22:57:34 +0900 | [diff] [blame] | 173 | func InitPrebuiltEtcModule(p *PrebuiltEtc) { |
| 174 | p.AddProperties(&p.properties) |
| 175 | } |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 176 | |
Jiyong Park | 5a8d1be | 2018-04-25 22:57:34 +0900 | [diff] [blame] | 177 | func PrebuiltEtcFactory() Module { |
| 178 | module := &PrebuiltEtc{} |
| 179 | InitPrebuiltEtcModule(module) |
| 180 | // This module is device-only |
Jaewoong Jung | b9a1151 | 2019-01-15 10:47:05 -0800 | [diff] [blame] | 181 | InitAndroidArchModule(module, DeviceSupported, MultilibFirst) |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 182 | return module |
| 183 | } |
Tao Bao | 0ba5c94 | 2018-08-14 22:20:22 -0700 | [diff] [blame] | 184 | |
Jaewoong Jung | 2478818 | 2019-02-04 14:34:10 -0800 | [diff] [blame^] | 185 | func prebuiltEtcHostFactory() Module { |
| 186 | module := &PrebuiltEtc{} |
| 187 | InitPrebuiltEtcModule(module) |
| 188 | // This module is host-only |
| 189 | InitAndroidArchModule(module, HostSupported, MultilibCommon) |
| 190 | return module |
| 191 | } |
| 192 | |
Tao Bao | 0ba5c94 | 2018-08-14 22:20:22 -0700 | [diff] [blame] | 193 | const ( |
| 194 | // coreMode is the variant for modules to be installed to system. |
| 195 | coreMode = "core" |
| 196 | |
| 197 | // recoveryMode means a module to be installed to recovery image. |
| 198 | recoveryMode = "recovery" |
| 199 | ) |
| 200 | |
| 201 | // prebuiltEtcMutator creates the needed variants to install the module to |
| 202 | // system or recovery. |
| 203 | func prebuiltEtcMutator(mctx BottomUpMutatorContext) { |
| 204 | m, ok := mctx.Module().(*PrebuiltEtc) |
Jaewoong Jung | 2478818 | 2019-02-04 14:34:10 -0800 | [diff] [blame^] | 205 | if !ok || m.Host() { |
Tao Bao | 0ba5c94 | 2018-08-14 22:20:22 -0700 | [diff] [blame] | 206 | return |
| 207 | } |
| 208 | |
| 209 | var coreVariantNeeded bool = true |
| 210 | var recoveryVariantNeeded bool = false |
| 211 | if Bool(m.properties.Recovery_available) { |
| 212 | recoveryVariantNeeded = true |
| 213 | } |
| 214 | |
| 215 | if m.ModuleBase.InstallInRecovery() { |
| 216 | recoveryVariantNeeded = true |
| 217 | coreVariantNeeded = false |
| 218 | } |
| 219 | |
| 220 | var variants []string |
| 221 | if coreVariantNeeded { |
| 222 | variants = append(variants, coreMode) |
| 223 | } |
| 224 | if recoveryVariantNeeded { |
| 225 | variants = append(variants, recoveryMode) |
| 226 | } |
| 227 | mod := mctx.CreateVariations(variants...) |
| 228 | for i, v := range variants { |
| 229 | if v == recoveryMode { |
| 230 | m := mod[i].(*PrebuiltEtc) |
| 231 | m.properties.InRecovery = true |
| 232 | } |
| 233 | } |
| 234 | } |