blob: 6743fb3feaad4dede87bf55223519056fda02533 [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) {
Dan Willemsen7b310ee2015-12-18 15:11:17 -0800120 ctx := &configErrorWrapper{p, config.(Config), []error{}}
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700121 p := PathForOutput(ctx, "host", ctx.config.PrebuiltOS(), "bin", path)
122 if len(ctx.errors) > 0 {
123 return "", ctx.errors[0]
124 }
125 return p.String(), nil
126 })
127}
128
129// HostJavaToolVariable returns a Variable whose value is the path to a host
130// tool in the frameworks directory for host targets. It may only be called
131// during a Go package's initialization - either from the init() function or as
132// part of a package-scoped variable's initialization.
133func (p AndroidPackageContext) HostJavaToolVariable(name, path string) blueprint.Variable {
134 return p.VariableFunc(name, func(config interface{}) (string, error) {
Dan Willemsen7b310ee2015-12-18 15:11:17 -0800135 ctx := &configErrorWrapper{p, config.(Config), []error{}}
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700136 p := PathForOutput(ctx, "host", ctx.config.PrebuiltOS(), "framework", path)
137 if len(ctx.errors) > 0 {
138 return "", ctx.errors[0]
139 }
140 return p.String(), nil
141 })
142}
143
144// IntermediatesPathVariable returns a Variable whose value is the intermediate
145// directory appended with the supplied path. 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.
148func (p AndroidPackageContext) IntermediatesPathVariable(name, path string) blueprint.Variable {
149 return p.VariableFunc(name, func(config interface{}) (string, error) {
Dan Willemsen7b310ee2015-12-18 15:11:17 -0800150 ctx := &configErrorWrapper{p, config.(Config), []error{}}
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700151 p := PathForIntermediates(ctx, path)
152 if len(ctx.errors) > 0 {
153 return "", ctx.errors[0]
154 }
155 return p.String(), nil
156 })
157}
158
Jeff Gaston734e3802017-04-10 15:47:24 -0700159// PrefixedExistentPathsForSourcesVariable returns a Variable whose value is the
Dan Willemsen7b310ee2015-12-18 15:11:17 -0800160// list of present source paths prefixed with the supplied prefix. It may only
161// be called during a Go package's initialization - either from the init()
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700162// function or as part of a package-scoped variable's initialization.
Jeff Gaston734e3802017-04-10 15:47:24 -0700163func (p AndroidPackageContext) PrefixedExistentPathsForSourcesVariable(
Dan Willemsen7b310ee2015-12-18 15:11:17 -0800164 name, prefix string, paths []string) blueprint.Variable {
165
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700166 return p.VariableFunc(name, func(config interface{}) (string, error) {
Dan Willemsen7b310ee2015-12-18 15:11:17 -0800167 ctx := &configErrorWrapper{p, config.(Config), []error{}}
Jeff Gaston734e3802017-04-10 15:47:24 -0700168 paths := ExistentPathsForSources(ctx, "", paths)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700169 if len(ctx.errors) > 0 {
170 return "", ctx.errors[0]
171 }
172 return JoinWithPrefix(paths.Strings(), prefix), nil
173 })
174}
Colin Cross9d45bb72016-08-29 16:14:13 -0700175
176type RuleParams struct {
177 blueprint.RuleParams
178 GomaSupported bool
179}
180
181// AndroidStaticRule wraps blueprint.StaticRule and provides a default Pool if none is specified
182func (p AndroidPackageContext) AndroidStaticRule(name string, params blueprint.RuleParams,
183 argNames ...string) blueprint.Rule {
184 return p.AndroidRuleFunc(name, func(interface{}) (blueprint.RuleParams, error) {
185 return params, nil
186 }, argNames...)
187}
188
189// AndroidGomaStaticRule wraps blueprint.StaticRule but uses goma's parallelism if goma is enabled
190func (p AndroidPackageContext) AndroidGomaStaticRule(name string, params blueprint.RuleParams,
191 argNames ...string) blueprint.Rule {
192 return p.StaticRule(name, params, argNames...)
193}
194
195func (p AndroidPackageContext) AndroidRuleFunc(name string,
196 f func(interface{}) (blueprint.RuleParams, error), argNames ...string) blueprint.Rule {
197 return p.PackageContext.RuleFunc(name, func(config interface{}) (blueprint.RuleParams, error) {
198 params, err := f(config)
199 if config.(Config).UseGoma() && params.Pool == nil {
200 // When USE_GOMA=true is set and the rule is not supported by goma, restrict jobs to the
201 // local parallelism value
202 params.Pool = localPool
203 }
204 return params, err
205 }, argNames...)
206}