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" |
| 19 | |
| 20 | "github.com/google/blueprint" |
Colin Cross | 294941b | 2017-02-01 14:10:36 -0800 | [diff] [blame] | 21 | "github.com/google/blueprint/pathtools" |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 22 | ) |
| 23 | |
| 24 | // AndroidPackageContext is a wrapper for blueprint.PackageContext that adds |
| 25 | // some android-specific helper functions. |
| 26 | type AndroidPackageContext struct { |
| 27 | blueprint.PackageContext |
| 28 | } |
| 29 | |
| 30 | func NewPackageContext(pkgPath string) AndroidPackageContext { |
| 31 | return AndroidPackageContext{blueprint.NewPackageContext(pkgPath)} |
| 32 | } |
| 33 | |
| 34 | // configErrorWrapper can be used with Path functions when a Context is not |
| 35 | // available. A Config can be provided, and errors are stored as a list for |
| 36 | // later retrieval. |
| 37 | // |
| 38 | // The most common use here will be with VariableFunc, where only a config is |
| 39 | // provided, and an error should be returned. |
| 40 | type configErrorWrapper struct { |
Dan Willemsen | 7b310ee | 2015-12-18 15:11:17 -0800 | [diff] [blame] | 41 | pctx AndroidPackageContext |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 42 | config Config |
| 43 | errors []error |
| 44 | } |
| 45 | |
| 46 | var _ PathContext = &configErrorWrapper{} |
| 47 | var _ errorfContext = &configErrorWrapper{} |
| 48 | |
| 49 | func (e *configErrorWrapper) Config() interface{} { |
| 50 | return e.config |
| 51 | } |
| 52 | func (e *configErrorWrapper) Errorf(format string, args ...interface{}) { |
| 53 | e.errors = append(e.errors, fmt.Errorf(format, args...)) |
| 54 | } |
Dan Willemsen | 7b310ee | 2015-12-18 15:11:17 -0800 | [diff] [blame] | 55 | func (e *configErrorWrapper) AddNinjaFileDeps(deps ...string) { |
| 56 | e.pctx.AddNinjaFileDeps(deps...) |
| 57 | } |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 58 | |
Colin Cross | 294941b | 2017-02-01 14:10:36 -0800 | [diff] [blame] | 59 | func (e *configErrorWrapper) Fs() pathtools.FileSystem { |
| 60 | return nil |
| 61 | } |
| 62 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 63 | // SourcePathVariable returns a Variable whose value is the source directory |
| 64 | // appended with the supplied path. It may only be called during a Go package's |
| 65 | // initialization - either from the init() function or as part of a |
| 66 | // package-scoped variable's initialization. |
| 67 | func (p AndroidPackageContext) SourcePathVariable(name, path string) blueprint.Variable { |
| 68 | return p.VariableFunc(name, func(config interface{}) (string, error) { |
Dan Willemsen | 7b310ee | 2015-12-18 15:11:17 -0800 | [diff] [blame] | 69 | ctx := &configErrorWrapper{p, config.(Config), []error{}} |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 70 | p := safePathForSource(ctx, path) |
| 71 | if len(ctx.errors) > 0 { |
| 72 | return "", ctx.errors[0] |
| 73 | } |
| 74 | return p.String(), nil |
| 75 | }) |
| 76 | } |
| 77 | |
Colin Cross | 6416271 | 2017-08-08 13:17:59 -0700 | [diff] [blame^] | 78 | // SourcePathVariableWithEnvOverride returns a Variable whose value is the source directory |
| 79 | // appended with the supplied path, or the value of the given environment variable if it is set. |
| 80 | // The environment variable is not required to point to a path inside the source tree. |
| 81 | // It may only be called during a Go package's initialization - either from the init() function or |
| 82 | // as part of a package-scoped variable's initialization. |
| 83 | func (p AndroidPackageContext) SourcePathVariableWithEnvOverride(name, path, env string) blueprint.Variable { |
| 84 | return p.VariableFunc(name, func(config interface{}) (string, error) { |
| 85 | ctx := &configErrorWrapper{p, config.(Config), []error{}} |
| 86 | p := safePathForSource(ctx, path) |
| 87 | if len(ctx.errors) > 0 { |
| 88 | return "", ctx.errors[0] |
| 89 | } |
| 90 | return config.(Config).GetenvWithDefault(env, p.String()), nil |
| 91 | }) |
| 92 | } |
| 93 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 94 | // HostBinVariable returns a Variable whose value is the path to a host tool |
| 95 | // in the bin directory for host targets. It may only be called during a Go |
| 96 | // package's initialization - either from the init() function or as part of a |
| 97 | // package-scoped variable's initialization. |
| 98 | func (p AndroidPackageContext) HostBinToolVariable(name, path string) blueprint.Variable { |
| 99 | return p.VariableFunc(name, func(config interface{}) (string, error) { |
Dan Willemsen | 7b310ee | 2015-12-18 15:11:17 -0800 | [diff] [blame] | 100 | ctx := &configErrorWrapper{p, config.(Config), []error{}} |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 101 | p := PathForOutput(ctx, "host", ctx.config.PrebuiltOS(), "bin", path) |
| 102 | if len(ctx.errors) > 0 { |
| 103 | return "", ctx.errors[0] |
| 104 | } |
| 105 | return p.String(), nil |
| 106 | }) |
| 107 | } |
| 108 | |
| 109 | // HostJavaToolVariable returns a Variable whose value is the path to a host |
| 110 | // tool in the frameworks directory for host targets. It may only be called |
| 111 | // during a Go package's initialization - either from the init() function or as |
| 112 | // part of a package-scoped variable's initialization. |
| 113 | func (p AndroidPackageContext) HostJavaToolVariable(name, path string) blueprint.Variable { |
| 114 | return p.VariableFunc(name, func(config interface{}) (string, error) { |
Dan Willemsen | 7b310ee | 2015-12-18 15:11:17 -0800 | [diff] [blame] | 115 | ctx := &configErrorWrapper{p, config.(Config), []error{}} |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 116 | p := PathForOutput(ctx, "host", ctx.config.PrebuiltOS(), "framework", path) |
| 117 | if len(ctx.errors) > 0 { |
| 118 | return "", ctx.errors[0] |
| 119 | } |
| 120 | return p.String(), nil |
| 121 | }) |
| 122 | } |
| 123 | |
| 124 | // IntermediatesPathVariable returns a Variable whose value is the intermediate |
| 125 | // directory appended with the supplied path. It may only be called during a Go |
| 126 | // package's initialization - either from the init() function or as part of a |
| 127 | // package-scoped variable's initialization. |
| 128 | func (p AndroidPackageContext) IntermediatesPathVariable(name, path string) blueprint.Variable { |
| 129 | return p.VariableFunc(name, func(config interface{}) (string, error) { |
Dan Willemsen | 7b310ee | 2015-12-18 15:11:17 -0800 | [diff] [blame] | 130 | ctx := &configErrorWrapper{p, config.(Config), []error{}} |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 131 | p := PathForIntermediates(ctx, path) |
| 132 | if len(ctx.errors) > 0 { |
| 133 | return "", ctx.errors[0] |
| 134 | } |
| 135 | return p.String(), nil |
| 136 | }) |
| 137 | } |
| 138 | |
Jeff Gaston | 734e380 | 2017-04-10 15:47:24 -0700 | [diff] [blame] | 139 | // PrefixedExistentPathsForSourcesVariable returns a Variable whose value is the |
Dan Willemsen | 7b310ee | 2015-12-18 15:11:17 -0800 | [diff] [blame] | 140 | // list of present source paths prefixed with the supplied prefix. It may only |
| 141 | // be called during a Go package's initialization - either from the init() |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 142 | // function or as part of a package-scoped variable's initialization. |
Jeff Gaston | 734e380 | 2017-04-10 15:47:24 -0700 | [diff] [blame] | 143 | func (p AndroidPackageContext) PrefixedExistentPathsForSourcesVariable( |
Dan Willemsen | 7b310ee | 2015-12-18 15:11:17 -0800 | [diff] [blame] | 144 | name, prefix string, paths []string) blueprint.Variable { |
| 145 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 146 | return p.VariableFunc(name, func(config interface{}) (string, error) { |
Dan Willemsen | 7b310ee | 2015-12-18 15:11:17 -0800 | [diff] [blame] | 147 | ctx := &configErrorWrapper{p, config.(Config), []error{}} |
Jeff Gaston | 734e380 | 2017-04-10 15:47:24 -0700 | [diff] [blame] | 148 | paths := ExistentPathsForSources(ctx, "", paths) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 149 | if len(ctx.errors) > 0 { |
| 150 | return "", ctx.errors[0] |
| 151 | } |
| 152 | return JoinWithPrefix(paths.Strings(), prefix), nil |
| 153 | }) |
| 154 | } |
Colin Cross | 9d45bb7 | 2016-08-29 16:14:13 -0700 | [diff] [blame] | 155 | |
| 156 | type RuleParams struct { |
| 157 | blueprint.RuleParams |
| 158 | GomaSupported bool |
| 159 | } |
| 160 | |
| 161 | // AndroidStaticRule wraps blueprint.StaticRule and provides a default Pool if none is specified |
| 162 | func (p AndroidPackageContext) AndroidStaticRule(name string, params blueprint.RuleParams, |
| 163 | argNames ...string) blueprint.Rule { |
| 164 | return p.AndroidRuleFunc(name, func(interface{}) (blueprint.RuleParams, error) { |
| 165 | return params, nil |
| 166 | }, argNames...) |
| 167 | } |
| 168 | |
| 169 | // AndroidGomaStaticRule wraps blueprint.StaticRule but uses goma's parallelism if goma is enabled |
| 170 | func (p AndroidPackageContext) AndroidGomaStaticRule(name string, params blueprint.RuleParams, |
| 171 | argNames ...string) blueprint.Rule { |
| 172 | return p.StaticRule(name, params, argNames...) |
| 173 | } |
| 174 | |
| 175 | func (p AndroidPackageContext) AndroidRuleFunc(name string, |
| 176 | f func(interface{}) (blueprint.RuleParams, error), argNames ...string) blueprint.Rule { |
| 177 | return p.PackageContext.RuleFunc(name, func(config interface{}) (blueprint.RuleParams, error) { |
| 178 | params, err := f(config) |
| 179 | if config.(Config).UseGoma() && params.Pool == nil { |
| 180 | // When USE_GOMA=true is set and the rule is not supported by goma, restrict jobs to the |
| 181 | // local parallelism value |
| 182 | params.Pool = localPool |
| 183 | } |
| 184 | return params, err |
| 185 | }, argNames...) |
| 186 | } |