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{} |
| 49 | |
| 50 | func (e *configErrorWrapper) Config() interface{} { |
| 51 | return e.config |
| 52 | } |
| 53 | func (e *configErrorWrapper) Errorf(format string, args ...interface{}) { |
| 54 | e.errors = append(e.errors, fmt.Errorf(format, args...)) |
| 55 | } |
Dan Willemsen | 7b310ee | 2015-12-18 15:11:17 -0800 | [diff] [blame] | 56 | func (e *configErrorWrapper) AddNinjaFileDeps(deps ...string) { |
| 57 | e.pctx.AddNinjaFileDeps(deps...) |
| 58 | } |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 59 | |
Colin Cross | 294941b | 2017-02-01 14:10:36 -0800 | [diff] [blame] | 60 | func (e *configErrorWrapper) Fs() pathtools.FileSystem { |
| 61 | return nil |
| 62 | } |
| 63 | |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame^] | 64 | // VariableFunc wraps blueprint.PackageContext.VariableFunc, converting the interface{} config |
| 65 | // argument to a Config. |
| 66 | func (p PackageContext) VariableFunc(name string, |
| 67 | f func(Config) (string, error)) blueprint.Variable { |
| 68 | |
| 69 | return p.PackageContext.VariableFunc(name, func(config interface{}) (string, error) { |
| 70 | return f(config.(Config)) |
| 71 | }) |
| 72 | } |
| 73 | |
| 74 | // PoolFunc wraps blueprint.PackageContext.PoolFunc, converting the interface{} config |
| 75 | // argument to a Config. |
| 76 | func (p PackageContext) PoolFunc(name string, |
| 77 | f func(Config) (blueprint.PoolParams, error)) blueprint.Pool { |
| 78 | |
| 79 | return p.PackageContext.PoolFunc(name, func(config interface{}) (blueprint.PoolParams, error) { |
| 80 | return f(config.(Config)) |
| 81 | }) |
| 82 | } |
| 83 | |
| 84 | // RuleFunc wraps blueprint.PackageContext.RuleFunc, converting the interface{} config |
| 85 | // argument to a Config. |
| 86 | func (p PackageContext) RuleFunc(name string, |
| 87 | f func(Config) (blueprint.RuleParams, error), argNames ...string) blueprint.Rule { |
| 88 | |
| 89 | return p.PackageContext.RuleFunc(name, func(config interface{}) (blueprint.RuleParams, error) { |
| 90 | return f(config.(Config)) |
| 91 | }, argNames...) |
| 92 | } |
| 93 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 94 | // SourcePathVariable returns a Variable whose value is the source directory |
| 95 | // appended with the supplied path. It may only be called during a Go package's |
| 96 | // initialization - either from the init() function or as part of a |
| 97 | // package-scoped variable's initialization. |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame^] | 98 | func (p PackageContext) SourcePathVariable(name, path string) blueprint.Variable { |
| 99 | return p.VariableFunc(name, func(config Config) (string, error) { |
| 100 | ctx := &configErrorWrapper{p, config, []error{}} |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 101 | p := safePathForSource(ctx, path) |
| 102 | if len(ctx.errors) > 0 { |
| 103 | return "", ctx.errors[0] |
| 104 | } |
| 105 | return p.String(), nil |
| 106 | }) |
| 107 | } |
| 108 | |
Colin Cross | c6bbef3 | 2017-08-14 14:16:06 -0700 | [diff] [blame] | 109 | // SourcePathsVariable returns a Variable whose value is the source directory |
| 110 | // appended with the supplied paths, joined with separator. It may only be |
| 111 | // called during a Go package's initialization - either from the init() |
| 112 | // function or as part of a package-scoped variable's initialization. |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame^] | 113 | func (p PackageContext) SourcePathsVariable(name, separator string, paths ...string) blueprint.Variable { |
| 114 | return p.VariableFunc(name, func(config Config) (string, error) { |
| 115 | ctx := &configErrorWrapper{p, config, []error{}} |
Colin Cross | c6bbef3 | 2017-08-14 14:16:06 -0700 | [diff] [blame] | 116 | var ret []string |
| 117 | for _, path := range paths { |
| 118 | p := safePathForSource(ctx, path) |
| 119 | if len(ctx.errors) > 0 { |
| 120 | return "", ctx.errors[0] |
| 121 | } |
| 122 | ret = append(ret, p.String()) |
| 123 | } |
| 124 | return strings.Join(ret, separator), nil |
| 125 | }) |
| 126 | } |
| 127 | |
Colin Cross | 6416271 | 2017-08-08 13:17:59 -0700 | [diff] [blame] | 128 | // SourcePathVariableWithEnvOverride returns a Variable whose value is the source directory |
| 129 | // appended with the supplied path, or the value of the given environment variable if it is set. |
| 130 | // The environment variable is not required to point to a path inside the source tree. |
| 131 | // It may only be called during a Go package's initialization - either from the init() function or |
| 132 | // as part of a package-scoped variable's initialization. |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame^] | 133 | func (p PackageContext) SourcePathVariableWithEnvOverride(name, path, env string) blueprint.Variable { |
| 134 | return p.VariableFunc(name, func(config Config) (string, error) { |
| 135 | ctx := &configErrorWrapper{p, config, []error{}} |
Colin Cross | 6416271 | 2017-08-08 13:17:59 -0700 | [diff] [blame] | 136 | p := safePathForSource(ctx, path) |
| 137 | if len(ctx.errors) > 0 { |
| 138 | return "", ctx.errors[0] |
| 139 | } |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame^] | 140 | return config.GetenvWithDefault(env, p.String()), nil |
Colin Cross | 6416271 | 2017-08-08 13:17:59 -0700 | [diff] [blame] | 141 | }) |
| 142 | } |
| 143 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 144 | // HostBinVariable returns a Variable whose value is the path to a host tool |
| 145 | // in the bin directory for host targets. It may only be called during a Go |
| 146 | // package's initialization - either from the init() function or as part of a |
| 147 | // package-scoped variable's initialization. |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame^] | 148 | func (p PackageContext) HostBinToolVariable(name, path string) blueprint.Variable { |
| 149 | return p.VariableFunc(name, func(config Config) (string, error) { |
Alan Leung | 1d476fc | 2017-10-17 18:50:50 -0700 | [diff] [blame] | 150 | po, err := p.HostBinToolPath(config, path) |
| 151 | if err != nil { |
| 152 | return "", err |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 153 | } |
Alan Leung | 1d476fc | 2017-10-17 18:50:50 -0700 | [diff] [blame] | 154 | return po.String(), nil |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 155 | }) |
| 156 | } |
| 157 | |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame^] | 158 | func (p PackageContext) HostBinToolPath(config Config, path string) (Path, error) { |
| 159 | ctx := &configErrorWrapper{p, config, []error{}} |
Alan Leung | 1d476fc | 2017-10-17 18:50:50 -0700 | [diff] [blame] | 160 | pa := PathForOutput(ctx, "host", ctx.config.PrebuiltOS(), "bin", path) |
| 161 | if len(ctx.errors) > 0 { |
| 162 | return nil, ctx.errors[0] |
| 163 | } |
| 164 | return pa, nil |
| 165 | } |
| 166 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 167 | // HostJavaToolVariable returns a Variable whose value is the path to a host |
| 168 | // tool in the frameworks directory for host targets. It may only be called |
| 169 | // during a Go package's initialization - either from the init() function or as |
| 170 | // part of a package-scoped variable's initialization. |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame^] | 171 | func (p PackageContext) HostJavaToolVariable(name, path string) blueprint.Variable { |
| 172 | return p.VariableFunc(name, func(config Config) (string, error) { |
| 173 | ctx := &configErrorWrapper{p, config, []error{}} |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 174 | p := PathForOutput(ctx, "host", ctx.config.PrebuiltOS(), "framework", path) |
| 175 | if len(ctx.errors) > 0 { |
| 176 | return "", ctx.errors[0] |
| 177 | } |
| 178 | return p.String(), nil |
| 179 | }) |
| 180 | } |
| 181 | |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame^] | 182 | func (p PackageContext) HostJavaToolPath(config Config, path string) (Path, error) { |
| 183 | ctx := &configErrorWrapper{p, config, []error{}} |
Nan Zhang | 9a36418 | 2017-10-25 11:11:37 -0700 | [diff] [blame] | 184 | pa := PathForOutput(ctx, "host", ctx.config.PrebuiltOS(), "framework", path) |
| 185 | if len(ctx.errors) > 0 { |
| 186 | return nil, ctx.errors[0] |
| 187 | } |
| 188 | return pa, nil |
| 189 | } |
| 190 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 191 | // IntermediatesPathVariable returns a Variable whose value is the intermediate |
| 192 | // directory appended with the supplied path. It may only be called during a Go |
| 193 | // package's initialization - either from the init() function or as part of a |
| 194 | // package-scoped variable's initialization. |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame^] | 195 | func (p PackageContext) IntermediatesPathVariable(name, path string) blueprint.Variable { |
| 196 | return p.VariableFunc(name, func(config Config) (string, error) { |
| 197 | ctx := &configErrorWrapper{p, config, []error{}} |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 198 | p := PathForIntermediates(ctx, path) |
| 199 | if len(ctx.errors) > 0 { |
| 200 | return "", ctx.errors[0] |
| 201 | } |
| 202 | return p.String(), nil |
| 203 | }) |
| 204 | } |
| 205 | |
Jeff Gaston | 734e380 | 2017-04-10 15:47:24 -0700 | [diff] [blame] | 206 | // PrefixedExistentPathsForSourcesVariable returns a Variable whose value is the |
Dan Willemsen | 7b310ee | 2015-12-18 15:11:17 -0800 | [diff] [blame] | 207 | // list of present source paths prefixed with the supplied prefix. It may only |
| 208 | // be called during a Go package's initialization - either from the init() |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 209 | // function or as part of a package-scoped variable's initialization. |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame^] | 210 | func (p PackageContext) PrefixedExistentPathsForSourcesVariable( |
Dan Willemsen | 7b310ee | 2015-12-18 15:11:17 -0800 | [diff] [blame] | 211 | name, prefix string, paths []string) blueprint.Variable { |
| 212 | |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame^] | 213 | return p.VariableFunc(name, func(config Config) (string, error) { |
| 214 | ctx := &configErrorWrapper{p, config, []error{}} |
Jeff Gaston | 734e380 | 2017-04-10 15:47:24 -0700 | [diff] [blame] | 215 | paths := ExistentPathsForSources(ctx, "", paths) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 216 | if len(ctx.errors) > 0 { |
| 217 | return "", ctx.errors[0] |
| 218 | } |
| 219 | return JoinWithPrefix(paths.Strings(), prefix), nil |
| 220 | }) |
| 221 | } |
Colin Cross | 9d45bb7 | 2016-08-29 16:14:13 -0700 | [diff] [blame] | 222 | |
| 223 | type RuleParams struct { |
| 224 | blueprint.RuleParams |
| 225 | GomaSupported bool |
| 226 | } |
| 227 | |
| 228 | // AndroidStaticRule wraps blueprint.StaticRule and provides a default Pool if none is specified |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame^] | 229 | func (p PackageContext) AndroidStaticRule(name string, params blueprint.RuleParams, |
Colin Cross | 9d45bb7 | 2016-08-29 16:14:13 -0700 | [diff] [blame] | 230 | argNames ...string) blueprint.Rule { |
Colin Cross | cf31fcf | 2017-11-20 12:14:08 -0800 | [diff] [blame] | 231 | return p.AndroidRuleFunc(name, func(Config) (blueprint.RuleParams, error) { |
Colin Cross | 9d45bb7 | 2016-08-29 16:14:13 -0700 | [diff] [blame] | 232 | return params, nil |
| 233 | }, argNames...) |
| 234 | } |
| 235 | |
| 236 | // AndroidGomaStaticRule wraps blueprint.StaticRule but uses goma's parallelism if goma is enabled |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame^] | 237 | func (p PackageContext) AndroidGomaStaticRule(name string, params blueprint.RuleParams, |
Colin Cross | 9d45bb7 | 2016-08-29 16:14:13 -0700 | [diff] [blame] | 238 | argNames ...string) blueprint.Rule { |
| 239 | return p.StaticRule(name, params, argNames...) |
| 240 | } |
| 241 | |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame^] | 242 | func (p PackageContext) AndroidRuleFunc(name string, |
Colin Cross | cf31fcf | 2017-11-20 12:14:08 -0800 | [diff] [blame] | 243 | f func(Config) (blueprint.RuleParams, error), argNames ...string) blueprint.Rule { |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame^] | 244 | return p.RuleFunc(name, func(config Config) (blueprint.RuleParams, error) { |
| 245 | params, err := f(config) |
| 246 | if config.UseGoma() && params.Pool == nil { |
Colin Cross | 9d45bb7 | 2016-08-29 16:14:13 -0700 | [diff] [blame] | 247 | // When USE_GOMA=true is set and the rule is not supported by goma, restrict jobs to the |
| 248 | // local parallelism value |
| 249 | params.Pool = localPool |
| 250 | } |
| 251 | return params, err |
| 252 | }, argNames...) |
| 253 | } |