blob: d32e82b29f33d3e7846bc880e719c70716a81f5c [file] [log] [blame]
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001// 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 Cross635c3b02016-05-18 15:37:25 -070015package android
Dan Willemsen34cc69e2015-09-23 15:26:20 -070016
17import (
18 "fmt"
Colin Crossc6bbef32017-08-14 14:16:06 -070019 "strings"
Dan Willemsen34cc69e2015-09-23 15:26:20 -070020
21 "github.com/google/blueprint"
Colin Cross294941b2017-02-01 14:10:36 -080022 "github.com/google/blueprint/pathtools"
Dan Willemsen34cc69e2015-09-23 15:26:20 -070023)
24
25// AndroidPackageContext is a wrapper for blueprint.PackageContext that adds
26// some android-specific helper functions.
27type AndroidPackageContext struct {
28 blueprint.PackageContext
29}
30
31func NewPackageContext(pkgPath string) AndroidPackageContext {
32 return AndroidPackageContext{blueprint.NewPackageContext(pkgPath)}
33}
34
35// configErrorWrapper can be used with Path functions when a Context is not
36// available. A Config can be provided, and errors are stored as a list for
37// later retrieval.
38//
39// The most common use here will be with VariableFunc, where only a config is
40// provided, and an error should be returned.
41type configErrorWrapper struct {
Dan Willemsen7b310ee2015-12-18 15:11:17 -080042 pctx AndroidPackageContext
Dan Willemsen34cc69e2015-09-23 15:26:20 -070043 config Config
44 errors []error
45}
46
47var _ PathContext = &configErrorWrapper{}
48var _ errorfContext = &configErrorWrapper{}
49
50func (e *configErrorWrapper) Config() interface{} {
51 return e.config
52}
53func (e *configErrorWrapper) Errorf(format string, args ...interface{}) {
54 e.errors = append(e.errors, fmt.Errorf(format, args...))
55}
Dan Willemsen7b310ee2015-12-18 15:11:17 -080056func (e *configErrorWrapper) AddNinjaFileDeps(deps ...string) {
57 e.pctx.AddNinjaFileDeps(deps...)
58}
Dan Willemsen34cc69e2015-09-23 15:26:20 -070059
Colin Cross294941b2017-02-01 14:10:36 -080060func (e *configErrorWrapper) Fs() pathtools.FileSystem {
61 return nil
62}
63
Dan Willemsen34cc69e2015-09-23 15:26:20 -070064// SourcePathVariable returns a Variable whose value is the source directory
65// appended with the supplied path. It may only be called during a Go package's
66// initialization - either from the init() function or as part of a
67// package-scoped variable's initialization.
68func (p AndroidPackageContext) SourcePathVariable(name, path string) blueprint.Variable {
69 return p.VariableFunc(name, func(config interface{}) (string, error) {
Dan Willemsen7b310ee2015-12-18 15:11:17 -080070 ctx := &configErrorWrapper{p, config.(Config), []error{}}
Dan Willemsen34cc69e2015-09-23 15:26:20 -070071 p := safePathForSource(ctx, path)
72 if len(ctx.errors) > 0 {
73 return "", ctx.errors[0]
74 }
75 return p.String(), nil
76 })
77}
78
Colin Crossc6bbef32017-08-14 14:16:06 -070079// SourcePathsVariable returns a Variable whose value is the source directory
80// appended with the supplied paths, joined with separator. It may only be
81// called during a Go package's initialization - either from the init()
82// function or as part of a package-scoped variable's initialization.
83func (p AndroidPackageContext) SourcePathsVariable(name, separator string, paths ...string) blueprint.Variable {
84 return p.VariableFunc(name, func(config interface{}) (string, error) {
85 ctx := &configErrorWrapper{p, config.(Config), []error{}}
86 var ret []string
87 for _, path := range paths {
88 p := safePathForSource(ctx, path)
89 if len(ctx.errors) > 0 {
90 return "", ctx.errors[0]
91 }
92 ret = append(ret, p.String())
93 }
94 return strings.Join(ret, separator), nil
95 })
96}
97
Colin Cross64162712017-08-08 13:17:59 -070098// SourcePathVariableWithEnvOverride returns a Variable whose value is the source directory
99// appended with the supplied path, or the value of the given environment variable if it is set.
100// The environment variable is not required to point to a path inside the source tree.
101// It may only be called during a Go package's initialization - either from the init() function or
102// as part of a package-scoped variable's initialization.
103func (p AndroidPackageContext) SourcePathVariableWithEnvOverride(name, path, env string) blueprint.Variable {
104 return p.VariableFunc(name, func(config interface{}) (string, error) {
105 ctx := &configErrorWrapper{p, config.(Config), []error{}}
106 p := safePathForSource(ctx, path)
107 if len(ctx.errors) > 0 {
108 return "", ctx.errors[0]
109 }
110 return config.(Config).GetenvWithDefault(env, p.String()), nil
111 })
112}
113
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700114// HostBinVariable returns a Variable whose value is the path to a host tool
115// in the bin directory for host targets. It may only be called during a Go
116// package's initialization - either from the init() function or as part of a
117// package-scoped variable's initialization.
118func (p AndroidPackageContext) HostBinToolVariable(name, path string) blueprint.Variable {
119 return p.VariableFunc(name, func(config interface{}) (string, error) {
Alan Leung1d476fc2017-10-17 18:50:50 -0700120 po, err := p.HostBinToolPath(config, path)
121 if err != nil {
122 return "", err
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700123 }
Alan Leung1d476fc2017-10-17 18:50:50 -0700124 return po.String(), nil
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700125 })
126}
127
Alan Leung1d476fc2017-10-17 18:50:50 -0700128func (p AndroidPackageContext) HostBinToolPath(config interface{}, path string) (Path, error) {
129 ctx := &configErrorWrapper{p, config.(Config), []error{}}
130 pa := PathForOutput(ctx, "host", ctx.config.PrebuiltOS(), "bin", path)
131 if len(ctx.errors) > 0 {
132 return nil, ctx.errors[0]
133 }
134 return pa, nil
135}
136
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700137// HostJavaToolVariable returns a Variable whose value is the path to a host
138// tool in the frameworks directory for host targets. It may only be called
139// during a Go package's initialization - either from the init() function or as
140// part of a package-scoped variable's initialization.
141func (p AndroidPackageContext) HostJavaToolVariable(name, path string) blueprint.Variable {
142 return p.VariableFunc(name, func(config interface{}) (string, error) {
Dan Willemsen7b310ee2015-12-18 15:11:17 -0800143 ctx := &configErrorWrapper{p, config.(Config), []error{}}
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700144 p := PathForOutput(ctx, "host", ctx.config.PrebuiltOS(), "framework", path)
145 if len(ctx.errors) > 0 {
146 return "", ctx.errors[0]
147 }
148 return p.String(), nil
149 })
150}
151
Nan Zhang9a364182017-10-25 11:11:37 -0700152func (p AndroidPackageContext) HostJavaToolPath(config interface{}, path string) (Path, error) {
153 ctx := &configErrorWrapper{p, config.(Config), []error{}}
154 pa := PathForOutput(ctx, "host", ctx.config.PrebuiltOS(), "framework", path)
155 if len(ctx.errors) > 0 {
156 return nil, ctx.errors[0]
157 }
158 return pa, nil
159}
160
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700161// IntermediatesPathVariable returns a Variable whose value is the intermediate
162// directory appended with the supplied path. It may only be called during a Go
163// package's initialization - either from the init() function or as part of a
164// package-scoped variable's initialization.
165func (p AndroidPackageContext) IntermediatesPathVariable(name, path string) blueprint.Variable {
166 return p.VariableFunc(name, func(config interface{}) (string, error) {
Dan Willemsen7b310ee2015-12-18 15:11:17 -0800167 ctx := &configErrorWrapper{p, config.(Config), []error{}}
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700168 p := PathForIntermediates(ctx, path)
169 if len(ctx.errors) > 0 {
170 return "", ctx.errors[0]
171 }
172 return p.String(), nil
173 })
174}
175
Jeff Gaston734e3802017-04-10 15:47:24 -0700176// PrefixedExistentPathsForSourcesVariable returns a Variable whose value is the
Dan Willemsen7b310ee2015-12-18 15:11:17 -0800177// list of present source paths prefixed with the supplied prefix. It may only
178// be called during a Go package's initialization - either from the init()
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700179// function or as part of a package-scoped variable's initialization.
Jeff Gaston734e3802017-04-10 15:47:24 -0700180func (p AndroidPackageContext) PrefixedExistentPathsForSourcesVariable(
Dan Willemsen7b310ee2015-12-18 15:11:17 -0800181 name, prefix string, paths []string) blueprint.Variable {
182
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700183 return p.VariableFunc(name, func(config interface{}) (string, error) {
Dan Willemsen7b310ee2015-12-18 15:11:17 -0800184 ctx := &configErrorWrapper{p, config.(Config), []error{}}
Jeff Gaston734e3802017-04-10 15:47:24 -0700185 paths := ExistentPathsForSources(ctx, "", paths)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700186 if len(ctx.errors) > 0 {
187 return "", ctx.errors[0]
188 }
189 return JoinWithPrefix(paths.Strings(), prefix), nil
190 })
191}
Colin Cross9d45bb72016-08-29 16:14:13 -0700192
193type RuleParams struct {
194 blueprint.RuleParams
195 GomaSupported bool
196}
197
198// AndroidStaticRule wraps blueprint.StaticRule and provides a default Pool if none is specified
199func (p AndroidPackageContext) AndroidStaticRule(name string, params blueprint.RuleParams,
200 argNames ...string) blueprint.Rule {
Colin Crosscf31fcf2017-11-20 12:14:08 -0800201 return p.AndroidRuleFunc(name, func(Config) (blueprint.RuleParams, error) {
Colin Cross9d45bb72016-08-29 16:14:13 -0700202 return params, nil
203 }, argNames...)
204}
205
206// AndroidGomaStaticRule wraps blueprint.StaticRule but uses goma's parallelism if goma is enabled
207func (p AndroidPackageContext) AndroidGomaStaticRule(name string, params blueprint.RuleParams,
208 argNames ...string) blueprint.Rule {
209 return p.StaticRule(name, params, argNames...)
210}
211
212func (p AndroidPackageContext) AndroidRuleFunc(name string,
Colin Crosscf31fcf2017-11-20 12:14:08 -0800213 f func(Config) (blueprint.RuleParams, error), argNames ...string) blueprint.Rule {
Colin Cross9d45bb72016-08-29 16:14:13 -0700214 return p.PackageContext.RuleFunc(name, func(config interface{}) (blueprint.RuleParams, error) {
Colin Crosscf31fcf2017-11-20 12:14:08 -0800215 params, err := f(config.(Config))
Colin Cross9d45bb72016-08-29 16:14:13 -0700216 if config.(Config).UseGoma() && params.Pool == nil {
217 // When USE_GOMA=true is set and the rule is not supported by goma, restrict jobs to the
218 // local parallelism value
219 params.Pool = localPool
220 }
221 return params, err
222 }, argNames...)
223}