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