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