blob: 5e971ef1e4a58fd36d18d1b1e7ede362bd1a3585 [file] [log] [blame]
Colin Cross69452e12023-11-15 11:20:53 -08001// 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
15package android
16
17import (
Colin Cross69452e12023-11-15 11:20:53 -080018 "os"
19 "text/scanner"
Cole Faust02987bd2024-03-21 17:58:43 -070020
21 "github.com/google/blueprint"
Colin Cross69452e12023-11-15 11:20:53 -080022)
23
Cole Faustbda18162024-10-15 15:01:14 -070024// ModuleErrorContext provides only methods to report errors about the current module.
25type 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 Cross69452e12023-11-15 11:20:53 -080033// 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.
35type EarlyModuleContext interface {
Cole Faustbda18162024-10-15 15:01:14 -070036 ModuleErrorContext
37
Colin Cross69452e12023-11-15 11:20:53 -080038 // 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 Crossb2388e32024-10-07 15:05:23 -070043 // the module was created, but may have been modified by calls to BottomUpMutatorContext.Rename.
Colin Cross69452e12023-11-15 11:20:53 -080044 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 Faust02987bd2024-03-21 17:58:43 -070063 // 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 Cross69452e12023-11-15 11:20:53 -080066 // 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 Faustb1ccc2f2024-09-27 11:21:53 -0700101
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 Cross69452e12023-11-15 11:20:53 -0800105}
106
107// Deprecated: use EarlyModuleContext instead
108type BaseContext interface {
109 EarlyModuleContext
110}
111
112type earlyModuleContext struct {
113 blueprint.EarlyModuleContext
114
115 kind moduleKind
116 config Config
117}
118
119func (e *earlyModuleContext) Glob(globPattern string, excludes []string) Paths {
120 return Glob(e, globPattern, excludes)
121}
122
123func (e *earlyModuleContext) GlobFiles(globPattern string, excludes []string) Paths {
124 return GlobFiles(e, globPattern, excludes)
125}
126
127func (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
135func (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
143func (e *earlyModuleContext) Module() Module {
144 module, _ := e.EarlyModuleContext.Module().(Module)
145 return module
146}
147
148func (e *earlyModuleContext) Config() Config {
149 return e.EarlyModuleContext.Config().(Config)
150}
151
152func (e *earlyModuleContext) AConfig() Config {
153 return e.config
154}
155
156func (e *earlyModuleContext) DeviceConfig() DeviceConfig {
157 return DeviceConfig{e.config.deviceConfig}
158}
159
160func (e *earlyModuleContext) Platform() bool {
161 return e.kind == platformModule
162}
163
164func (e *earlyModuleContext) DeviceSpecific() bool {
165 return e.kind == deviceSpecificModule
166}
167
168func (e *earlyModuleContext) SocSpecific() bool {
169 return e.kind == socSpecificModule
170}
171
172func (e *earlyModuleContext) ProductSpecific() bool {
173 return e.kind == productSpecificModule
174}
175
176func (e *earlyModuleContext) SystemExtSpecific() bool {
177 return e.kind == systemExtSpecificModule
178}
179
180func (e *earlyModuleContext) Namespace() *Namespace {
181 return e.EarlyModuleContext.Namespace().(*Namespace)
182}
Cole Faust02987bd2024-03-21 17:58:43 -0700183
184func (e *earlyModuleContext) OtherModulePropertyErrorf(module Module, property string, fmt string, args ...interface{}) {
Cole Fausta963b942024-04-11 17:43:00 -0700185 e.EarlyModuleContext.OtherModulePropertyErrorf(module, property, fmt, args...)
Cole Faust02987bd2024-03-21 17:58:43 -0700186}
Cole Faustb1ccc2f2024-09-27 11:21:53 -0700187
188func (e *earlyModuleContext) HasMutatorFinished(mutatorName string) bool {
189 return e.EarlyModuleContext.HasMutatorFinished(mutatorName)
190}