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