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