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