blob: 1626f766c3f07ff931c447fec97c77cea24db071 [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
Colin Cross0875c522017-11-28 17:34:01 -080025// PackageContext is a wrapper for blueprint.PackageContext that adds
Dan Willemsen34cc69e2015-09-23 15:26:20 -070026// some android-specific helper functions.
Colin Cross0875c522017-11-28 17:34:01 -080027type PackageContext struct {
Dan Willemsen34cc69e2015-09-23 15:26:20 -070028 blueprint.PackageContext
29}
30
Colin Cross0875c522017-11-28 17:34:01 -080031func NewPackageContext(pkgPath string) PackageContext {
32 return PackageContext{blueprint.NewPackageContext(pkgPath)}
Dan Willemsen34cc69e2015-09-23 15:26:20 -070033}
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 {
Colin Cross0875c522017-11-28 17:34:01 -080042 pctx PackageContext
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
Colin Cross0875c522017-11-28 17:34:01 -080064// VariableFunc wraps blueprint.PackageContext.VariableFunc, converting the interface{} config
65// argument to a Config.
66func (p PackageContext) VariableFunc(name string,
67 f func(Config) (string, error)) blueprint.Variable {
68
69 return p.PackageContext.VariableFunc(name, func(config interface{}) (string, error) {
70 return f(config.(Config))
71 })
72}
73
74// PoolFunc wraps blueprint.PackageContext.PoolFunc, converting the interface{} config
75// argument to a Config.
76func (p PackageContext) PoolFunc(name string,
77 f func(Config) (blueprint.PoolParams, error)) blueprint.Pool {
78
79 return p.PackageContext.PoolFunc(name, func(config interface{}) (blueprint.PoolParams, error) {
80 return f(config.(Config))
81 })
82}
83
84// RuleFunc wraps blueprint.PackageContext.RuleFunc, converting the interface{} config
85// argument to a Config.
86func (p PackageContext) RuleFunc(name string,
87 f func(Config) (blueprint.RuleParams, error), argNames ...string) blueprint.Rule {
88
89 return p.PackageContext.RuleFunc(name, func(config interface{}) (blueprint.RuleParams, error) {
90 return f(config.(Config))
91 }, argNames...)
92}
93
Dan Willemsen34cc69e2015-09-23 15:26:20 -070094// SourcePathVariable returns a Variable whose value is the source directory
95// appended with the supplied path. It may only be called during a Go package's
96// initialization - either from the init() function or as part of a
97// package-scoped variable's initialization.
Colin Cross0875c522017-11-28 17:34:01 -080098func (p PackageContext) SourcePathVariable(name, path string) blueprint.Variable {
99 return p.VariableFunc(name, func(config Config) (string, error) {
100 ctx := &configErrorWrapper{p, config, []error{}}
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700101 p := safePathForSource(ctx, path)
102 if len(ctx.errors) > 0 {
103 return "", ctx.errors[0]
104 }
105 return p.String(), nil
106 })
107}
108
Colin Crossc6bbef32017-08-14 14:16:06 -0700109// SourcePathsVariable returns a Variable whose value is the source directory
110// appended with the supplied paths, joined with separator. It may only be
111// called during a Go package's initialization - either from the init()
112// function or as part of a package-scoped variable's initialization.
Colin Cross0875c522017-11-28 17:34:01 -0800113func (p PackageContext) SourcePathsVariable(name, separator string, paths ...string) blueprint.Variable {
114 return p.VariableFunc(name, func(config Config) (string, error) {
115 ctx := &configErrorWrapper{p, config, []error{}}
Colin Crossc6bbef32017-08-14 14:16:06 -0700116 var ret []string
117 for _, path := range paths {
118 p := safePathForSource(ctx, path)
119 if len(ctx.errors) > 0 {
120 return "", ctx.errors[0]
121 }
122 ret = append(ret, p.String())
123 }
124 return strings.Join(ret, separator), nil
125 })
126}
127
Colin Cross64162712017-08-08 13:17:59 -0700128// SourcePathVariableWithEnvOverride returns a Variable whose value is the source directory
129// appended with the supplied path, or the value of the given environment variable if it is set.
130// The environment variable is not required to point to a path inside the source tree.
131// It may only be called during a Go package's initialization - either from the init() function or
132// as part of a package-scoped variable's initialization.
Colin Cross0875c522017-11-28 17:34:01 -0800133func (p PackageContext) SourcePathVariableWithEnvOverride(name, path, env string) blueprint.Variable {
134 return p.VariableFunc(name, func(config Config) (string, error) {
135 ctx := &configErrorWrapper{p, config, []error{}}
Colin Cross64162712017-08-08 13:17:59 -0700136 p := safePathForSource(ctx, path)
137 if len(ctx.errors) > 0 {
138 return "", ctx.errors[0]
139 }
Colin Cross0875c522017-11-28 17:34:01 -0800140 return config.GetenvWithDefault(env, p.String()), nil
Colin Cross64162712017-08-08 13:17:59 -0700141 })
142}
143
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700144// HostBinVariable returns a Variable whose value is the path to a host tool
145// in the bin directory for host targets. It may only be called during a Go
146// package's initialization - either from the init() function or as part of a
147// package-scoped variable's initialization.
Colin Cross0875c522017-11-28 17:34:01 -0800148func (p PackageContext) HostBinToolVariable(name, path string) blueprint.Variable {
149 return p.VariableFunc(name, func(config Config) (string, error) {
Alan Leung1d476fc2017-10-17 18:50:50 -0700150 po, err := p.HostBinToolPath(config, path)
151 if err != nil {
152 return "", err
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700153 }
Alan Leung1d476fc2017-10-17 18:50:50 -0700154 return po.String(), nil
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700155 })
156}
157
Colin Cross0875c522017-11-28 17:34:01 -0800158func (p PackageContext) HostBinToolPath(config Config, path string) (Path, error) {
159 ctx := &configErrorWrapper{p, config, []error{}}
Alan Leung1d476fc2017-10-17 18:50:50 -0700160 pa := PathForOutput(ctx, "host", ctx.config.PrebuiltOS(), "bin", path)
161 if len(ctx.errors) > 0 {
162 return nil, ctx.errors[0]
163 }
164 return pa, nil
165}
166
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700167// HostJavaToolVariable returns a Variable whose value is the path to a host
168// tool in the frameworks directory for host targets. It may only be called
169// during a Go package's initialization - either from the init() function or as
170// part of a package-scoped variable's initialization.
Colin Cross0875c522017-11-28 17:34:01 -0800171func (p PackageContext) HostJavaToolVariable(name, path string) blueprint.Variable {
172 return p.VariableFunc(name, func(config Config) (string, error) {
173 ctx := &configErrorWrapper{p, config, []error{}}
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700174 p := PathForOutput(ctx, "host", ctx.config.PrebuiltOS(), "framework", path)
175 if len(ctx.errors) > 0 {
176 return "", ctx.errors[0]
177 }
178 return p.String(), nil
179 })
180}
181
Colin Cross0875c522017-11-28 17:34:01 -0800182func (p PackageContext) HostJavaToolPath(config Config, path string) (Path, error) {
183 ctx := &configErrorWrapper{p, config, []error{}}
Nan Zhang9a364182017-10-25 11:11:37 -0700184 pa := PathForOutput(ctx, "host", ctx.config.PrebuiltOS(), "framework", path)
185 if len(ctx.errors) > 0 {
186 return nil, ctx.errors[0]
187 }
188 return pa, nil
189}
190
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700191// IntermediatesPathVariable returns a Variable whose value is the intermediate
192// directory appended with the supplied path. It may only be called during a Go
193// package's initialization - either from the init() function or as part of a
194// package-scoped variable's initialization.
Colin Cross0875c522017-11-28 17:34:01 -0800195func (p PackageContext) IntermediatesPathVariable(name, path string) blueprint.Variable {
196 return p.VariableFunc(name, func(config Config) (string, error) {
197 ctx := &configErrorWrapper{p, config, []error{}}
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700198 p := PathForIntermediates(ctx, path)
199 if len(ctx.errors) > 0 {
200 return "", ctx.errors[0]
201 }
202 return p.String(), nil
203 })
204}
205
Jeff Gaston734e3802017-04-10 15:47:24 -0700206// PrefixedExistentPathsForSourcesVariable returns a Variable whose value is the
Dan Willemsen7b310ee2015-12-18 15:11:17 -0800207// list of present source paths prefixed with the supplied prefix. It may only
208// be called during a Go package's initialization - either from the init()
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700209// function or as part of a package-scoped variable's initialization.
Colin Cross0875c522017-11-28 17:34:01 -0800210func (p PackageContext) PrefixedExistentPathsForSourcesVariable(
Dan Willemsen7b310ee2015-12-18 15:11:17 -0800211 name, prefix string, paths []string) blueprint.Variable {
212
Colin Cross0875c522017-11-28 17:34:01 -0800213 return p.VariableFunc(name, func(config Config) (string, error) {
214 ctx := &configErrorWrapper{p, config, []error{}}
Jeff Gaston734e3802017-04-10 15:47:24 -0700215 paths := ExistentPathsForSources(ctx, "", paths)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700216 if len(ctx.errors) > 0 {
217 return "", ctx.errors[0]
218 }
219 return JoinWithPrefix(paths.Strings(), prefix), nil
220 })
221}
Colin Cross9d45bb72016-08-29 16:14:13 -0700222
223type RuleParams struct {
224 blueprint.RuleParams
225 GomaSupported bool
226}
227
228// AndroidStaticRule wraps blueprint.StaticRule and provides a default Pool if none is specified
Colin Cross0875c522017-11-28 17:34:01 -0800229func (p PackageContext) AndroidStaticRule(name string, params blueprint.RuleParams,
Colin Cross9d45bb72016-08-29 16:14:13 -0700230 argNames ...string) blueprint.Rule {
Colin Crosscf31fcf2017-11-20 12:14:08 -0800231 return p.AndroidRuleFunc(name, func(Config) (blueprint.RuleParams, error) {
Colin Cross9d45bb72016-08-29 16:14:13 -0700232 return params, nil
233 }, argNames...)
234}
235
236// AndroidGomaStaticRule wraps blueprint.StaticRule but uses goma's parallelism if goma is enabled
Colin Cross0875c522017-11-28 17:34:01 -0800237func (p PackageContext) AndroidGomaStaticRule(name string, params blueprint.RuleParams,
Colin Cross9d45bb72016-08-29 16:14:13 -0700238 argNames ...string) blueprint.Rule {
239 return p.StaticRule(name, params, argNames...)
240}
241
Colin Cross0875c522017-11-28 17:34:01 -0800242func (p PackageContext) AndroidRuleFunc(name string,
Colin Crosscf31fcf2017-11-20 12:14:08 -0800243 f func(Config) (blueprint.RuleParams, error), argNames ...string) blueprint.Rule {
Colin Cross0875c522017-11-28 17:34:01 -0800244 return p.RuleFunc(name, func(config Config) (blueprint.RuleParams, error) {
245 params, err := f(config)
246 if config.UseGoma() && params.Pool == nil {
Colin Cross9d45bb72016-08-29 16:14:13 -0700247 // When USE_GOMA=true is set and the rule is not supported by goma, restrict jobs to the
248 // local parallelism value
249 params.Pool = localPool
250 }
251 return params, err
252 }, argNames...)
253}