blob: 849eb72a842beb651574fb23e68cf9e641ff42e7 [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 Cross5ab4e6d2017-11-22 16:20:45 -080019 "runtime"
Colin Crossc6bbef32017-08-14 14:16:06 -070020 "strings"
Dan Willemsen34cc69e2015-09-23 15:26:20 -070021
22 "github.com/google/blueprint"
Colin Cross294941b2017-02-01 14:10:36 -080023 "github.com/google/blueprint/pathtools"
Dan Willemsen34cc69e2015-09-23 15:26:20 -070024)
25
Colin Cross0875c522017-11-28 17:34:01 -080026// PackageContext is a wrapper for blueprint.PackageContext that adds
Dan Willemsen34cc69e2015-09-23 15:26:20 -070027// some android-specific helper functions.
Colin Cross0875c522017-11-28 17:34:01 -080028type PackageContext struct {
Dan Willemsen34cc69e2015-09-23 15:26:20 -070029 blueprint.PackageContext
30}
31
Colin Cross0875c522017-11-28 17:34:01 -080032func NewPackageContext(pkgPath string) PackageContext {
33 return PackageContext{blueprint.NewPackageContext(pkgPath)}
Dan Willemsen34cc69e2015-09-23 15:26:20 -070034}
35
36// configErrorWrapper can be used with Path functions when a Context is not
37// available. A Config can be provided, and errors are stored as a list for
38// later retrieval.
39//
40// The most common use here will be with VariableFunc, where only a config is
41// provided, and an error should be returned.
42type configErrorWrapper struct {
Colin Cross0875c522017-11-28 17:34:01 -080043 pctx PackageContext
Dan Willemsen34cc69e2015-09-23 15:26:20 -070044 config Config
45 errors []error
46}
47
48var _ PathContext = &configErrorWrapper{}
49var _ errorfContext = &configErrorWrapper{}
50
Colin Crossaabf6792017-11-29 00:27:14 -080051func (e *configErrorWrapper) Config() Config {
Dan Willemsen34cc69e2015-09-23 15:26:20 -070052 return e.config
53}
54func (e *configErrorWrapper) Errorf(format string, args ...interface{}) {
55 e.errors = append(e.errors, fmt.Errorf(format, args...))
56}
Dan Willemsen7b310ee2015-12-18 15:11:17 -080057func (e *configErrorWrapper) AddNinjaFileDeps(deps ...string) {
58 e.pctx.AddNinjaFileDeps(deps...)
59}
Dan Willemsen34cc69e2015-09-23 15:26:20 -070060
Colin Cross294941b2017-02-01 14:10:36 -080061func (e *configErrorWrapper) Fs() pathtools.FileSystem {
62 return nil
63}
64
Colin Cross0875c522017-11-28 17:34:01 -080065// VariableFunc wraps blueprint.PackageContext.VariableFunc, converting the interface{} config
66// argument to a Config.
67func (p PackageContext) VariableFunc(name string,
68 f func(Config) (string, error)) blueprint.Variable {
69
70 return p.PackageContext.VariableFunc(name, func(config interface{}) (string, error) {
71 return f(config.(Config))
72 })
73}
74
75// PoolFunc wraps blueprint.PackageContext.PoolFunc, converting the interface{} config
76// argument to a Config.
77func (p PackageContext) PoolFunc(name string,
78 f func(Config) (blueprint.PoolParams, error)) blueprint.Pool {
79
80 return p.PackageContext.PoolFunc(name, func(config interface{}) (blueprint.PoolParams, error) {
81 return f(config.(Config))
82 })
83}
84
85// RuleFunc wraps blueprint.PackageContext.RuleFunc, converting the interface{} config
86// argument to a Config.
87func (p PackageContext) RuleFunc(name string,
88 f func(Config) (blueprint.RuleParams, error), argNames ...string) blueprint.Rule {
89
90 return p.PackageContext.RuleFunc(name, func(config interface{}) (blueprint.RuleParams, error) {
91 return f(config.(Config))
92 }, argNames...)
93}
94
Dan Willemsen34cc69e2015-09-23 15:26:20 -070095// SourcePathVariable returns a Variable whose value is the source directory
96// appended with the supplied path. It may only be called during a Go package's
97// initialization - either from the init() function or as part of a
98// package-scoped variable's initialization.
Colin Cross0875c522017-11-28 17:34:01 -080099func (p PackageContext) SourcePathVariable(name, path string) blueprint.Variable {
100 return p.VariableFunc(name, func(config Config) (string, error) {
101 ctx := &configErrorWrapper{p, config, []error{}}
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700102 p := safePathForSource(ctx, path)
103 if len(ctx.errors) > 0 {
104 return "", ctx.errors[0]
105 }
106 return p.String(), nil
107 })
108}
109
Colin Crossc6bbef32017-08-14 14:16:06 -0700110// SourcePathsVariable returns a Variable whose value is the source directory
111// appended with the supplied paths, joined with separator. It may only be
112// called during a Go package's initialization - either from the init()
113// function or as part of a package-scoped variable's initialization.
Colin Cross0875c522017-11-28 17:34:01 -0800114func (p PackageContext) SourcePathsVariable(name, separator string, paths ...string) blueprint.Variable {
115 return p.VariableFunc(name, func(config Config) (string, error) {
116 ctx := &configErrorWrapper{p, config, []error{}}
Colin Crossc6bbef32017-08-14 14:16:06 -0700117 var ret []string
118 for _, path := range paths {
119 p := safePathForSource(ctx, path)
120 if len(ctx.errors) > 0 {
121 return "", ctx.errors[0]
122 }
123 ret = append(ret, p.String())
124 }
125 return strings.Join(ret, separator), nil
126 })
127}
128
Colin Cross64162712017-08-08 13:17:59 -0700129// SourcePathVariableWithEnvOverride returns a Variable whose value is the source directory
130// appended with the supplied path, or the value of the given environment variable if it is set.
131// The environment variable is not required to point to a path inside the source tree.
132// It may only be called during a Go package's initialization - either from the init() function or
133// as part of a package-scoped variable's initialization.
Colin Cross0875c522017-11-28 17:34:01 -0800134func (p PackageContext) SourcePathVariableWithEnvOverride(name, path, env string) blueprint.Variable {
135 return p.VariableFunc(name, func(config Config) (string, error) {
136 ctx := &configErrorWrapper{p, config, []error{}}
Colin Cross64162712017-08-08 13:17:59 -0700137 p := safePathForSource(ctx, path)
138 if len(ctx.errors) > 0 {
139 return "", ctx.errors[0]
140 }
Colin Cross0875c522017-11-28 17:34:01 -0800141 return config.GetenvWithDefault(env, p.String()), nil
Colin Cross64162712017-08-08 13:17:59 -0700142 })
143}
144
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800145// HostBinToolVariable returns a Variable whose value is the path to a host tool
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700146// in the bin directory for host targets. It may only be called during a Go
147// package's initialization - either from the init() function or as part of a
148// package-scoped variable's initialization.
Colin Cross0875c522017-11-28 17:34:01 -0800149func (p PackageContext) HostBinToolVariable(name, path string) blueprint.Variable {
150 return p.VariableFunc(name, func(config Config) (string, error) {
Alan Leung1d476fc2017-10-17 18:50:50 -0700151 po, err := p.HostBinToolPath(config, path)
152 if err != nil {
153 return "", err
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700154 }
Alan Leung1d476fc2017-10-17 18:50:50 -0700155 return po.String(), nil
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700156 })
157}
158
Colin Cross0875c522017-11-28 17:34:01 -0800159func (p PackageContext) HostBinToolPath(config Config, path string) (Path, error) {
160 ctx := &configErrorWrapper{p, config, []error{}}
Alan Leung1d476fc2017-10-17 18:50:50 -0700161 pa := PathForOutput(ctx, "host", ctx.config.PrebuiltOS(), "bin", path)
162 if len(ctx.errors) > 0 {
163 return nil, ctx.errors[0]
164 }
165 return pa, nil
166}
167
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800168// HostJNIToolVariable returns a Variable whose value is the path to a host tool
169// in the lib directory for host targets. It may only be called during a Go
170// package's initialization - either from the init() function or as part of a
171// package-scoped variable's initialization.
172func (p PackageContext) HostJNIToolVariable(name, path string) blueprint.Variable {
173 return p.VariableFunc(name, func(config Config) (string, error) {
174 po, err := p.HostJNIToolPath(config, path)
175 if err != nil {
176 return "", err
177 }
178 return po.String(), nil
179 })
180}
181
182func (p PackageContext) HostJNIToolPath(config Config, path string) (Path, error) {
183 ctx := &configErrorWrapper{p, config, []error{}}
184 ext := ".so"
185 if runtime.GOOS == "darwin" {
186 ext = ".dylib"
187 }
188 pa := PathForOutput(ctx, "host", ctx.config.PrebuiltOS(), "lib64", path+ext)
189 if len(ctx.errors) > 0 {
190 return nil, ctx.errors[0]
191 }
192 return pa, nil
193}
194
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700195// HostJavaToolVariable returns a Variable whose value is the path to a host
196// tool in the frameworks directory for host targets. It may only be called
197// during a Go package's initialization - either from the init() function or as
198// part of a package-scoped variable's initialization.
Colin Cross0875c522017-11-28 17:34:01 -0800199func (p PackageContext) HostJavaToolVariable(name, path string) blueprint.Variable {
200 return p.VariableFunc(name, func(config Config) (string, error) {
201 ctx := &configErrorWrapper{p, config, []error{}}
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700202 p := PathForOutput(ctx, "host", ctx.config.PrebuiltOS(), "framework", path)
203 if len(ctx.errors) > 0 {
204 return "", ctx.errors[0]
205 }
206 return p.String(), nil
207 })
208}
209
Colin Cross0875c522017-11-28 17:34:01 -0800210func (p PackageContext) HostJavaToolPath(config Config, path string) (Path, error) {
211 ctx := &configErrorWrapper{p, config, []error{}}
Nan Zhang9a364182017-10-25 11:11:37 -0700212 pa := PathForOutput(ctx, "host", ctx.config.PrebuiltOS(), "framework", path)
213 if len(ctx.errors) > 0 {
214 return nil, ctx.errors[0]
215 }
216 return pa, nil
217}
218
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700219// IntermediatesPathVariable returns a Variable whose value is the intermediate
220// directory appended with the supplied path. It may only be called during a Go
221// package's initialization - either from the init() function or as part of a
222// package-scoped variable's initialization.
Colin Cross0875c522017-11-28 17:34:01 -0800223func (p PackageContext) IntermediatesPathVariable(name, path string) blueprint.Variable {
224 return p.VariableFunc(name, func(config Config) (string, error) {
225 ctx := &configErrorWrapper{p, config, []error{}}
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700226 p := PathForIntermediates(ctx, path)
227 if len(ctx.errors) > 0 {
228 return "", ctx.errors[0]
229 }
230 return p.String(), nil
231 })
232}
233
Jeff Gaston734e3802017-04-10 15:47:24 -0700234// PrefixedExistentPathsForSourcesVariable returns a Variable whose value is the
Dan Willemsen7b310ee2015-12-18 15:11:17 -0800235// list of present source paths prefixed with the supplied prefix. It may only
236// be called during a Go package's initialization - either from the init()
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700237// function or as part of a package-scoped variable's initialization.
Colin Cross0875c522017-11-28 17:34:01 -0800238func (p PackageContext) PrefixedExistentPathsForSourcesVariable(
Dan Willemsen7b310ee2015-12-18 15:11:17 -0800239 name, prefix string, paths []string) blueprint.Variable {
240
Colin Cross0875c522017-11-28 17:34:01 -0800241 return p.VariableFunc(name, func(config Config) (string, error) {
242 ctx := &configErrorWrapper{p, config, []error{}}
Colin Cross32f38982018-02-22 11:47:25 -0800243 paths := ExistentPathsForSources(ctx, paths)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700244 if len(ctx.errors) > 0 {
245 return "", ctx.errors[0]
246 }
247 return JoinWithPrefix(paths.Strings(), prefix), nil
248 })
249}
Colin Cross9d45bb72016-08-29 16:14:13 -0700250
Colin Cross9d45bb72016-08-29 16:14:13 -0700251// AndroidStaticRule wraps blueprint.StaticRule and provides a default Pool if none is specified
Colin Cross0875c522017-11-28 17:34:01 -0800252func (p PackageContext) AndroidStaticRule(name string, params blueprint.RuleParams,
Colin Cross9d45bb72016-08-29 16:14:13 -0700253 argNames ...string) blueprint.Rule {
Colin Crosscf31fcf2017-11-20 12:14:08 -0800254 return p.AndroidRuleFunc(name, func(Config) (blueprint.RuleParams, error) {
Colin Cross9d45bb72016-08-29 16:14:13 -0700255 return params, nil
256 }, argNames...)
257}
258
259// AndroidGomaStaticRule wraps blueprint.StaticRule but uses goma's parallelism if goma is enabled
Colin Cross0875c522017-11-28 17:34:01 -0800260func (p PackageContext) AndroidGomaStaticRule(name string, params blueprint.RuleParams,
Colin Cross9d45bb72016-08-29 16:14:13 -0700261 argNames ...string) blueprint.Rule {
262 return p.StaticRule(name, params, argNames...)
263}
264
Colin Cross0875c522017-11-28 17:34:01 -0800265func (p PackageContext) AndroidRuleFunc(name string,
Colin Crosscf31fcf2017-11-20 12:14:08 -0800266 f func(Config) (blueprint.RuleParams, error), argNames ...string) blueprint.Rule {
Colin Cross0875c522017-11-28 17:34:01 -0800267 return p.RuleFunc(name, func(config Config) (blueprint.RuleParams, error) {
268 params, err := f(config)
269 if config.UseGoma() && params.Pool == nil {
Colin Cross9d45bb72016-08-29 16:14:13 -0700270 // When USE_GOMA=true is set and the rule is not supported by goma, restrict jobs to the
271 // local parallelism value
272 params.Pool = localPool
273 }
274 return params, err
275 }, argNames...)
276}