Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [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 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 15 | package android |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 16 | |
| 17 | import ( |
| 18 | "fmt" |
Colin Cross | c6bbef3 | 2017-08-14 14:16:06 -0700 | [diff] [blame] | 19 | "strings" |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 20 | |
| 21 | "github.com/google/blueprint" |
Colin Cross | 294941b | 2017-02-01 14:10:36 -0800 | [diff] [blame] | 22 | "github.com/google/blueprint/pathtools" |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 23 | ) |
| 24 | |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 25 | // PackageContext is a wrapper for blueprint.PackageContext that adds |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 26 | // some android-specific helper functions. |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 27 | type PackageContext struct { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 28 | blueprint.PackageContext |
| 29 | } |
| 30 | |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 31 | func NewPackageContext(pkgPath string) PackageContext { |
| 32 | return PackageContext{blueprint.NewPackageContext(pkgPath)} |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | // configErrorWrapper can be used with Path functions when a Context is not |
| 36 | // available. A Config can be provided, and errors are stored as a list for |
| 37 | // later retrieval. |
| 38 | // |
| 39 | // The most common use here will be with VariableFunc, where only a config is |
| 40 | // provided, and an error should be returned. |
| 41 | type configErrorWrapper struct { |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 42 | pctx PackageContext |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 43 | config Config |
| 44 | errors []error |
| 45 | } |
| 46 | |
| 47 | var _ PathContext = &configErrorWrapper{} |
| 48 | var _ errorfContext = &configErrorWrapper{} |
Dan Willemsen | 54daaf0 | 2018-03-12 13:24:09 -0700 | [diff] [blame] | 49 | var _ PackageVarContext = &configErrorWrapper{} |
| 50 | var _ PackagePoolContext = &configErrorWrapper{} |
| 51 | var _ PackageRuleContext = &configErrorWrapper{} |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 52 | |
Colin Cross | aabf679 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 53 | func (e *configErrorWrapper) Config() Config { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 54 | return e.config |
| 55 | } |
| 56 | func (e *configErrorWrapper) Errorf(format string, args ...interface{}) { |
| 57 | e.errors = append(e.errors, fmt.Errorf(format, args...)) |
| 58 | } |
Dan Willemsen | 7b310ee | 2015-12-18 15:11:17 -0800 | [diff] [blame] | 59 | func (e *configErrorWrapper) AddNinjaFileDeps(deps ...string) { |
| 60 | e.pctx.AddNinjaFileDeps(deps...) |
| 61 | } |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 62 | |
Colin Cross | 294941b | 2017-02-01 14:10:36 -0800 | [diff] [blame] | 63 | func (e *configErrorWrapper) Fs() pathtools.FileSystem { |
| 64 | return nil |
| 65 | } |
| 66 | |
Dan Willemsen | 54daaf0 | 2018-03-12 13:24:09 -0700 | [diff] [blame] | 67 | type PackageVarContext interface { |
| 68 | PathContext |
| 69 | errorfContext |
| 70 | } |
| 71 | |
| 72 | type PackagePoolContext PackageVarContext |
| 73 | type PackageRuleContext PackageVarContext |
| 74 | |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 75 | // VariableFunc wraps blueprint.PackageContext.VariableFunc, converting the interface{} config |
Dan Willemsen | 54daaf0 | 2018-03-12 13:24:09 -0700 | [diff] [blame] | 76 | // argument to a PackageVarContext. |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 77 | func (p PackageContext) VariableFunc(name string, |
Dan Willemsen | 54daaf0 | 2018-03-12 13:24:09 -0700 | [diff] [blame] | 78 | f func(PackageVarContext) string) blueprint.Variable { |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 79 | |
| 80 | return p.PackageContext.VariableFunc(name, func(config interface{}) (string, error) { |
Dan Willemsen | 54daaf0 | 2018-03-12 13:24:09 -0700 | [diff] [blame] | 81 | ctx := &configErrorWrapper{p, config.(Config), nil} |
| 82 | ret := f(ctx) |
| 83 | if len(ctx.errors) > 0 { |
| 84 | return "", ctx.errors[0] |
| 85 | } |
| 86 | return ret, nil |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 87 | }) |
| 88 | } |
| 89 | |
| 90 | // PoolFunc wraps blueprint.PackageContext.PoolFunc, converting the interface{} config |
Dan Willemsen | 54daaf0 | 2018-03-12 13:24:09 -0700 | [diff] [blame] | 91 | // argument to a Context that supports Config(). |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 92 | func (p PackageContext) PoolFunc(name string, |
Dan Willemsen | 54daaf0 | 2018-03-12 13:24:09 -0700 | [diff] [blame] | 93 | f func(PackagePoolContext) blueprint.PoolParams) blueprint.Pool { |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 94 | |
| 95 | return p.PackageContext.PoolFunc(name, func(config interface{}) (blueprint.PoolParams, error) { |
Dan Willemsen | 54daaf0 | 2018-03-12 13:24:09 -0700 | [diff] [blame] | 96 | ctx := &configErrorWrapper{p, config.(Config), nil} |
| 97 | params := f(ctx) |
| 98 | if len(ctx.errors) > 0 { |
| 99 | return params, ctx.errors[0] |
| 100 | } |
| 101 | return params, nil |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 102 | }) |
| 103 | } |
| 104 | |
| 105 | // RuleFunc wraps blueprint.PackageContext.RuleFunc, converting the interface{} config |
Colin Cross | 2e2dbc2 | 2019-09-25 13:31:46 -0700 | [diff] [blame] | 106 | // argument to a Context that supports Config(), and provides a default Pool if none is |
| 107 | // specified. |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 108 | func (p PackageContext) RuleFunc(name string, |
Dan Willemsen | 54daaf0 | 2018-03-12 13:24:09 -0700 | [diff] [blame] | 109 | f func(PackageRuleContext) blueprint.RuleParams, argNames ...string) blueprint.Rule { |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 110 | |
| 111 | return p.PackageContext.RuleFunc(name, func(config interface{}) (blueprint.RuleParams, error) { |
Dan Willemsen | 54daaf0 | 2018-03-12 13:24:09 -0700 | [diff] [blame] | 112 | ctx := &configErrorWrapper{p, config.(Config), nil} |
| 113 | params := f(ctx) |
| 114 | if len(ctx.errors) > 0 { |
| 115 | return params, ctx.errors[0] |
| 116 | } |
Ramy Medhat | dd0418a | 2019-11-04 18:16:11 -0500 | [diff] [blame] | 117 | if (ctx.Config().UseGoma() || ctx.Config().UseRBE()) && params.Pool == nil { |
| 118 | // When USE_GOMA=true or USE_RBE=true are set and the rule is not supported by |
| 119 | // goma/RBE, restrict jobs to the local parallelism value |
Colin Cross | 2e2dbc2 | 2019-09-25 13:31:46 -0700 | [diff] [blame] | 120 | params.Pool = localPool |
| 121 | } |
Dan Willemsen | 54daaf0 | 2018-03-12 13:24:09 -0700 | [diff] [blame] | 122 | return params, nil |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 123 | }, argNames...) |
| 124 | } |
| 125 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 126 | // SourcePathVariable returns a Variable whose value is the source directory |
| 127 | // appended with the supplied path. It may only be called during a Go package's |
| 128 | // initialization - either from the init() function or as part of a |
| 129 | // package-scoped variable's initialization. |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 130 | func (p PackageContext) SourcePathVariable(name, path string) blueprint.Variable { |
Dan Willemsen | 54daaf0 | 2018-03-12 13:24:09 -0700 | [diff] [blame] | 131 | return p.VariableFunc(name, func(ctx PackageVarContext) string { |
Colin Cross | fe4bc36 | 2018-09-12 10:02:13 -0700 | [diff] [blame] | 132 | p, err := safePathForSource(ctx, path) |
| 133 | if err != nil { |
| 134 | ctx.Errorf("%s", err.Error()) |
| 135 | } |
| 136 | return p.String() |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 137 | }) |
| 138 | } |
| 139 | |
Colin Cross | c6bbef3 | 2017-08-14 14:16:06 -0700 | [diff] [blame] | 140 | // SourcePathsVariable returns a Variable whose value is the source directory |
| 141 | // appended with the supplied paths, joined with separator. It may only be |
| 142 | // called during a Go package's initialization - either from the init() |
| 143 | // function or as part of a package-scoped variable's initialization. |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 144 | func (p PackageContext) SourcePathsVariable(name, separator string, paths ...string) blueprint.Variable { |
Dan Willemsen | 54daaf0 | 2018-03-12 13:24:09 -0700 | [diff] [blame] | 145 | return p.VariableFunc(name, func(ctx PackageVarContext) string { |
Colin Cross | c6bbef3 | 2017-08-14 14:16:06 -0700 | [diff] [blame] | 146 | var ret []string |
| 147 | for _, path := range paths { |
Colin Cross | fe4bc36 | 2018-09-12 10:02:13 -0700 | [diff] [blame] | 148 | p, err := safePathForSource(ctx, path) |
| 149 | if err != nil { |
| 150 | ctx.Errorf("%s", err.Error()) |
| 151 | } |
Colin Cross | c6bbef3 | 2017-08-14 14:16:06 -0700 | [diff] [blame] | 152 | ret = append(ret, p.String()) |
| 153 | } |
Dan Willemsen | 54daaf0 | 2018-03-12 13:24:09 -0700 | [diff] [blame] | 154 | return strings.Join(ret, separator) |
Colin Cross | c6bbef3 | 2017-08-14 14:16:06 -0700 | [diff] [blame] | 155 | }) |
| 156 | } |
| 157 | |
Colin Cross | 6416271 | 2017-08-08 13:17:59 -0700 | [diff] [blame] | 158 | // SourcePathVariableWithEnvOverride returns a Variable whose value is the source directory |
| 159 | // appended with the supplied path, or the value of the given environment variable if it is set. |
| 160 | // The environment variable is not required to point to a path inside the source tree. |
| 161 | // It may only be called during a Go package's initialization - either from the init() function or |
| 162 | // as part of a package-scoped variable's initialization. |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 163 | func (p PackageContext) SourcePathVariableWithEnvOverride(name, path, env string) blueprint.Variable { |
Dan Willemsen | 54daaf0 | 2018-03-12 13:24:09 -0700 | [diff] [blame] | 164 | return p.VariableFunc(name, func(ctx PackageVarContext) string { |
Colin Cross | fe4bc36 | 2018-09-12 10:02:13 -0700 | [diff] [blame] | 165 | p, err := safePathForSource(ctx, path) |
| 166 | if err != nil { |
| 167 | ctx.Errorf("%s", err.Error()) |
| 168 | } |
Dan Willemsen | 54daaf0 | 2018-03-12 13:24:09 -0700 | [diff] [blame] | 169 | return ctx.Config().GetenvWithDefault(env, p.String()) |
Colin Cross | 6416271 | 2017-08-08 13:17:59 -0700 | [diff] [blame] | 170 | }) |
| 171 | } |
| 172 | |
Colin Cross | 5ab4e6d | 2017-11-22 16:20:45 -0800 | [diff] [blame] | 173 | // HostBinToolVariable returns a Variable whose value is the path to a host tool |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 174 | // in the bin directory for host targets. It may only be called during a Go |
| 175 | // package's initialization - either from the init() function or as part of a |
| 176 | // package-scoped variable's initialization. |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 177 | func (p PackageContext) HostBinToolVariable(name, path string) blueprint.Variable { |
Dan Willemsen | 54daaf0 | 2018-03-12 13:24:09 -0700 | [diff] [blame] | 178 | return p.VariableFunc(name, func(ctx PackageVarContext) string { |
Martin Stjernholm | 7260d06 | 2019-12-09 21:47:14 +0000 | [diff] [blame^] | 179 | return ctx.Config().HostToolPath(ctx, path).String() |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 180 | }) |
| 181 | } |
| 182 | |
Colin Cross | 5ab4e6d | 2017-11-22 16:20:45 -0800 | [diff] [blame] | 183 | // HostJNIToolVariable returns a Variable whose value is the path to a host tool |
| 184 | // in the lib directory for host targets. It may only be called during a Go |
| 185 | // package's initialization - either from the init() function or as part of a |
| 186 | // package-scoped variable's initialization. |
| 187 | func (p PackageContext) HostJNIToolVariable(name, path string) blueprint.Variable { |
Dan Willemsen | 54daaf0 | 2018-03-12 13:24:09 -0700 | [diff] [blame] | 188 | return p.VariableFunc(name, func(ctx PackageVarContext) string { |
Martin Stjernholm | 7260d06 | 2019-12-09 21:47:14 +0000 | [diff] [blame^] | 189 | return ctx.Config().HostJNIToolPath(ctx, path).String() |
Colin Cross | 5ab4e6d | 2017-11-22 16:20:45 -0800 | [diff] [blame] | 190 | }) |
| 191 | } |
| 192 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 193 | // HostJavaToolVariable returns a Variable whose value is the path to a host |
| 194 | // tool in the frameworks directory for host targets. It may only be called |
| 195 | // during a Go package's initialization - either from the init() function or as |
| 196 | // part of a package-scoped variable's initialization. |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 197 | func (p PackageContext) HostJavaToolVariable(name, path string) blueprint.Variable { |
Dan Willemsen | 54daaf0 | 2018-03-12 13:24:09 -0700 | [diff] [blame] | 198 | return p.VariableFunc(name, func(ctx PackageVarContext) string { |
Martin Stjernholm | 7260d06 | 2019-12-09 21:47:14 +0000 | [diff] [blame^] | 199 | return ctx.Config().HostJavaToolPath(ctx, path).String() |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 200 | }) |
| 201 | } |
| 202 | |
| 203 | // IntermediatesPathVariable returns a Variable whose value is the intermediate |
| 204 | // directory appended with the supplied path. It may only be called during a Go |
| 205 | // package's initialization - either from the init() function or as part of a |
| 206 | // package-scoped variable's initialization. |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 207 | func (p PackageContext) IntermediatesPathVariable(name, path string) blueprint.Variable { |
Dan Willemsen | 54daaf0 | 2018-03-12 13:24:09 -0700 | [diff] [blame] | 208 | return p.VariableFunc(name, func(ctx PackageVarContext) string { |
| 209 | return PathForIntermediates(ctx, path).String() |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 210 | }) |
| 211 | } |
| 212 | |
Jeff Gaston | 734e380 | 2017-04-10 15:47:24 -0700 | [diff] [blame] | 213 | // PrefixedExistentPathsForSourcesVariable returns a Variable whose value is the |
Dan Willemsen | 7b310ee | 2015-12-18 15:11:17 -0800 | [diff] [blame] | 214 | // list of present source paths prefixed with the supplied prefix. It may only |
| 215 | // be called during a Go package's initialization - either from the init() |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 216 | // function or as part of a package-scoped variable's initialization. |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 217 | func (p PackageContext) PrefixedExistentPathsForSourcesVariable( |
Dan Willemsen | 7b310ee | 2015-12-18 15:11:17 -0800 | [diff] [blame] | 218 | name, prefix string, paths []string) blueprint.Variable { |
| 219 | |
Dan Willemsen | 54daaf0 | 2018-03-12 13:24:09 -0700 | [diff] [blame] | 220 | return p.VariableFunc(name, func(ctx PackageVarContext) string { |
Colin Cross | 32f3898 | 2018-02-22 11:47:25 -0800 | [diff] [blame] | 221 | paths := ExistentPathsForSources(ctx, paths) |
Dan Willemsen | 54daaf0 | 2018-03-12 13:24:09 -0700 | [diff] [blame] | 222 | return JoinWithPrefix(paths.Strings(), prefix) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 223 | }) |
| 224 | } |
Colin Cross | 9d45bb7 | 2016-08-29 16:14:13 -0700 | [diff] [blame] | 225 | |
Colin Cross | 2e2dbc2 | 2019-09-25 13:31:46 -0700 | [diff] [blame] | 226 | // AndroidStaticRule is an alias for StaticRule. |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 227 | func (p PackageContext) AndroidStaticRule(name string, params blueprint.RuleParams, |
Colin Cross | 9d45bb7 | 2016-08-29 16:14:13 -0700 | [diff] [blame] | 228 | argNames ...string) blueprint.Rule { |
Colin Cross | 2e2dbc2 | 2019-09-25 13:31:46 -0700 | [diff] [blame] | 229 | return p.StaticRule(name, params, argNames...) |
| 230 | } |
| 231 | |
| 232 | // StaticRule wraps blueprint.StaticRule and provides a default Pool if none is specified. |
| 233 | func (p PackageContext) StaticRule(name string, params blueprint.RuleParams, |
| 234 | argNames ...string) blueprint.Rule { |
| 235 | return p.RuleFunc(name, func(PackageRuleContext) blueprint.RuleParams { |
Dan Willemsen | 54daaf0 | 2018-03-12 13:24:09 -0700 | [diff] [blame] | 236 | return params |
Colin Cross | 9d45bb7 | 2016-08-29 16:14:13 -0700 | [diff] [blame] | 237 | }, argNames...) |
| 238 | } |
| 239 | |
Ramy Medhat | dd0418a | 2019-11-04 18:16:11 -0500 | [diff] [blame] | 240 | // RemoteRuleSupports selects if a AndroidRemoteStaticRule supports goma, RBE, or both. |
| 241 | type RemoteRuleSupports int |
| 242 | |
| 243 | const ( |
| 244 | SUPPORTS_NONE = 0 |
| 245 | SUPPORTS_GOMA = 1 << iota |
| 246 | SUPPORTS_RBE = 1 << iota |
| 247 | SUPPORTS_BOTH = SUPPORTS_GOMA | SUPPORTS_RBE |
| 248 | ) |
| 249 | |
| 250 | // AndroidRemoteStaticRule wraps blueprint.StaticRule but uses goma or RBE's parallelism if goma or RBE are enabled |
| 251 | // and the appropriate SUPPORTS_* flag is set. |
| 252 | func (p PackageContext) AndroidRemoteStaticRule(name string, supports RemoteRuleSupports, params blueprint.RuleParams, |
Colin Cross | 9d45bb7 | 2016-08-29 16:14:13 -0700 | [diff] [blame] | 253 | argNames ...string) blueprint.Rule { |
Ramy Medhat | dd0418a | 2019-11-04 18:16:11 -0500 | [diff] [blame] | 254 | |
| 255 | return p.PackageContext.RuleFunc(name, func(config interface{}) (blueprint.RuleParams, error) { |
| 256 | ctx := &configErrorWrapper{p, config.(Config), nil} |
| 257 | if ctx.Config().UseGoma() && supports&SUPPORTS_GOMA == 0 { |
| 258 | // When USE_GOMA=true is set and the rule is not supported by goma, restrict jobs to the |
| 259 | // local parallelism value |
| 260 | params.Pool = localPool |
| 261 | } |
| 262 | |
| 263 | if ctx.Config().UseRBE() && supports&SUPPORTS_RBE == 0 { |
| 264 | // When USE_RBE=true is set and the rule is not supported by RBE, restrict jobs to the |
| 265 | // local parallelism value |
| 266 | params.Pool = localPool |
| 267 | } |
| 268 | |
| 269 | return params, nil |
| 270 | }, argNames...) |
Colin Cross | 9d45bb7 | 2016-08-29 16:14:13 -0700 | [diff] [blame] | 271 | } |