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