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