Colin Cross | 69452e1 | 2023-11-15 11:20:53 -0800 | [diff] [blame] | 1 | // Copyright 2015 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 ( |
Colin Cross | 69452e1 | 2023-11-15 11:20:53 -0800 | [diff] [blame] | 18 | "os" |
| 19 | "text/scanner" |
Cole Faust | 02987bd | 2024-03-21 17:58:43 -0700 | [diff] [blame] | 20 | |
| 21 | "github.com/google/blueprint" |
Colin Cross | 69452e1 | 2023-11-15 11:20:53 -0800 | [diff] [blame] | 22 | ) |
| 23 | |
Cole Faust | bda1816 | 2024-10-15 15:01:14 -0700 | [diff] [blame] | 24 | // ModuleErrorContext provides only methods to report errors about the current module. |
| 25 | type ModuleErrorContext interface { |
| 26 | // ModuleErrorf reports an error at the line number of the module type in the module definition. |
| 27 | ModuleErrorf(fmt string, args ...interface{}) |
| 28 | |
| 29 | // PropertyErrorf reports an error at the line number of a property in the module definition. |
| 30 | PropertyErrorf(property, fmt string, args ...interface{}) |
| 31 | } |
| 32 | |
Colin Cross | 69452e1 | 2023-11-15 11:20:53 -0800 | [diff] [blame] | 33 | // EarlyModuleContext provides methods that can be called early, as soon as the properties have |
| 34 | // been parsed into the module and before any mutators have run. |
| 35 | type EarlyModuleContext interface { |
Cole Faust | bda1816 | 2024-10-15 15:01:14 -0700 | [diff] [blame] | 36 | ModuleErrorContext |
| 37 | |
Colin Cross | 69452e1 | 2023-11-15 11:20:53 -0800 | [diff] [blame] | 38 | // Module returns the current module as a Module. It should rarely be necessary, as the module already has a |
| 39 | // reference to itself. |
| 40 | Module() Module |
| 41 | |
| 42 | // ModuleName returns the name of the module. This is generally the value that was returned by Module.Name() when |
Colin Cross | b2388e3 | 2024-10-07 15:05:23 -0700 | [diff] [blame] | 43 | // the module was created, but may have been modified by calls to BottomUpMutatorContext.Rename. |
Colin Cross | 69452e1 | 2023-11-15 11:20:53 -0800 | [diff] [blame] | 44 | ModuleName() string |
| 45 | |
| 46 | // ModuleDir returns the path to the directory that contains the definition of the module. |
| 47 | ModuleDir() string |
| 48 | |
| 49 | // ModuleType returns the name of the module type that was used to create the module, as specified in |
| 50 | // RegisterModuleType. |
| 51 | ModuleType() string |
| 52 | |
| 53 | // BlueprintFile returns the name of the blueprint file that contains the definition of this |
| 54 | // module. |
| 55 | BlueprintsFile() string |
| 56 | |
| 57 | // ContainsProperty returns true if the specified property name was set in the module definition. |
| 58 | ContainsProperty(name string) bool |
| 59 | |
| 60 | // Errorf reports an error at the specified position of the module definition file. |
| 61 | Errorf(pos scanner.Position, fmt string, args ...interface{}) |
| 62 | |
Cole Faust | 02987bd | 2024-03-21 17:58:43 -0700 | [diff] [blame] | 63 | // OtherModulePropertyErrorf reports an error at the line number of a property in the given module definition. |
| 64 | OtherModulePropertyErrorf(module Module, property, fmt string, args ...interface{}) |
| 65 | |
Colin Cross | 69452e1 | 2023-11-15 11:20:53 -0800 | [diff] [blame] | 66 | // Failed returns true if any errors have been reported. In most cases the module can continue with generating |
| 67 | // build rules after an error, allowing it to report additional errors in a single run, but in cases where the error |
| 68 | // has prevented the module from creating necessary data it can return early when Failed returns true. |
| 69 | Failed() bool |
| 70 | |
| 71 | // AddNinjaFileDeps adds dependencies on the specified files to the rule that creates the ninja manifest. The |
| 72 | // primary builder will be rerun whenever the specified files are modified. |
| 73 | AddNinjaFileDeps(deps ...string) |
| 74 | |
| 75 | DeviceSpecific() bool |
| 76 | SocSpecific() bool |
| 77 | ProductSpecific() bool |
| 78 | SystemExtSpecific() bool |
| 79 | Platform() bool |
| 80 | |
| 81 | Config() Config |
| 82 | DeviceConfig() DeviceConfig |
| 83 | |
| 84 | // Deprecated: use Config() |
| 85 | AConfig() Config |
| 86 | |
| 87 | // GlobWithDeps returns a list of files that match the specified pattern but do not match any |
| 88 | // of the patterns in excludes. It also adds efficient dependencies to rerun the primary |
| 89 | // builder whenever a file matching the pattern as added or removed, without rerunning if a |
| 90 | // file that does not match the pattern is added to a searched directory. |
| 91 | GlobWithDeps(pattern string, excludes []string) ([]string, error) |
| 92 | |
| 93 | Glob(globPattern string, excludes []string) Paths |
| 94 | GlobFiles(globPattern string, excludes []string) Paths |
| 95 | IsSymlink(path Path) bool |
| 96 | Readlink(path Path) string |
| 97 | |
| 98 | // Namespace returns the Namespace object provided by the NameInterface set by Context.SetNameInterface, or the |
| 99 | // default SimpleNameInterface if Context.SetNameInterface was not called. |
| 100 | Namespace() *Namespace |
Cole Faust | b1ccc2f | 2024-09-27 11:21:53 -0700 | [diff] [blame] | 101 | |
| 102 | // HasMutatorFinished returns true if the given mutator has finished running. |
| 103 | // It will panic if given an invalid mutator name. |
| 104 | HasMutatorFinished(mutatorName string) bool |
Colin Cross | 69452e1 | 2023-11-15 11:20:53 -0800 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | // Deprecated: use EarlyModuleContext instead |
| 108 | type BaseContext interface { |
| 109 | EarlyModuleContext |
| 110 | } |
| 111 | |
| 112 | type earlyModuleContext struct { |
| 113 | blueprint.EarlyModuleContext |
| 114 | |
| 115 | kind moduleKind |
| 116 | config Config |
| 117 | } |
| 118 | |
| 119 | func (e *earlyModuleContext) Glob(globPattern string, excludes []string) Paths { |
| 120 | return Glob(e, globPattern, excludes) |
| 121 | } |
| 122 | |
| 123 | func (e *earlyModuleContext) GlobFiles(globPattern string, excludes []string) Paths { |
| 124 | return GlobFiles(e, globPattern, excludes) |
| 125 | } |
| 126 | |
| 127 | func (e *earlyModuleContext) IsSymlink(path Path) bool { |
| 128 | fileInfo, err := e.config.fs.Lstat(path.String()) |
| 129 | if err != nil { |
| 130 | e.ModuleErrorf("os.Lstat(%q) failed: %s", path.String(), err) |
| 131 | } |
| 132 | return fileInfo.Mode()&os.ModeSymlink == os.ModeSymlink |
| 133 | } |
| 134 | |
| 135 | func (e *earlyModuleContext) Readlink(path Path) string { |
| 136 | dest, err := e.config.fs.Readlink(path.String()) |
| 137 | if err != nil { |
| 138 | e.ModuleErrorf("os.Readlink(%q) failed: %s", path.String(), err) |
| 139 | } |
| 140 | return dest |
| 141 | } |
| 142 | |
| 143 | func (e *earlyModuleContext) Module() Module { |
| 144 | module, _ := e.EarlyModuleContext.Module().(Module) |
| 145 | return module |
| 146 | } |
| 147 | |
| 148 | func (e *earlyModuleContext) Config() Config { |
| 149 | return e.EarlyModuleContext.Config().(Config) |
| 150 | } |
| 151 | |
| 152 | func (e *earlyModuleContext) AConfig() Config { |
| 153 | return e.config |
| 154 | } |
| 155 | |
| 156 | func (e *earlyModuleContext) DeviceConfig() DeviceConfig { |
| 157 | return DeviceConfig{e.config.deviceConfig} |
| 158 | } |
| 159 | |
| 160 | func (e *earlyModuleContext) Platform() bool { |
| 161 | return e.kind == platformModule |
| 162 | } |
| 163 | |
| 164 | func (e *earlyModuleContext) DeviceSpecific() bool { |
| 165 | return e.kind == deviceSpecificModule |
| 166 | } |
| 167 | |
| 168 | func (e *earlyModuleContext) SocSpecific() bool { |
| 169 | return e.kind == socSpecificModule |
| 170 | } |
| 171 | |
| 172 | func (e *earlyModuleContext) ProductSpecific() bool { |
| 173 | return e.kind == productSpecificModule |
| 174 | } |
| 175 | |
| 176 | func (e *earlyModuleContext) SystemExtSpecific() bool { |
| 177 | return e.kind == systemExtSpecificModule |
| 178 | } |
| 179 | |
| 180 | func (e *earlyModuleContext) Namespace() *Namespace { |
| 181 | return e.EarlyModuleContext.Namespace().(*Namespace) |
| 182 | } |
Cole Faust | 02987bd | 2024-03-21 17:58:43 -0700 | [diff] [blame] | 183 | |
| 184 | func (e *earlyModuleContext) OtherModulePropertyErrorf(module Module, property string, fmt string, args ...interface{}) { |
Cole Faust | a963b94 | 2024-04-11 17:43:00 -0700 | [diff] [blame] | 185 | e.EarlyModuleContext.OtherModulePropertyErrorf(module, property, fmt, args...) |
Cole Faust | 02987bd | 2024-03-21 17:58:43 -0700 | [diff] [blame] | 186 | } |
Cole Faust | b1ccc2f | 2024-09-27 11:21:53 -0700 | [diff] [blame] | 187 | |
| 188 | func (e *earlyModuleContext) HasMutatorFinished(mutatorName string) bool { |
| 189 | return e.EarlyModuleContext.HasMutatorFinished(mutatorName) |
| 190 | } |