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