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