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