blob: e228bba56a425912762b264c3a3a90a3e72d1a02 [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{}
Dan Willemsen54daaf02018-03-12 13:24:09 -070050var _ PackageVarContext = &configErrorWrapper{}
51var _ PackagePoolContext = &configErrorWrapper{}
52var _ PackageRuleContext = &configErrorWrapper{}
Dan Willemsen34cc69e2015-09-23 15:26:20 -070053
Colin Crossaabf6792017-11-29 00:27:14 -080054func (e *configErrorWrapper) Config() Config {
Dan Willemsen34cc69e2015-09-23 15:26:20 -070055 return e.config
56}
57func (e *configErrorWrapper) Errorf(format string, args ...interface{}) {
58 e.errors = append(e.errors, fmt.Errorf(format, args...))
59}
Dan Willemsen7b310ee2015-12-18 15:11:17 -080060func (e *configErrorWrapper) AddNinjaFileDeps(deps ...string) {
61 e.pctx.AddNinjaFileDeps(deps...)
62}
Dan Willemsen34cc69e2015-09-23 15:26:20 -070063
Colin Cross294941b2017-02-01 14:10:36 -080064func (e *configErrorWrapper) Fs() pathtools.FileSystem {
65 return nil
66}
67
Dan Willemsen54daaf02018-03-12 13:24:09 -070068type PackageVarContext interface {
69 PathContext
70 errorfContext
71}
72
73type PackagePoolContext PackageVarContext
74type PackageRuleContext PackageVarContext
75
Colin Cross0875c522017-11-28 17:34:01 -080076// VariableFunc wraps blueprint.PackageContext.VariableFunc, converting the interface{} config
Dan Willemsen54daaf02018-03-12 13:24:09 -070077// argument to a PackageVarContext.
Colin Cross0875c522017-11-28 17:34:01 -080078func (p PackageContext) VariableFunc(name string,
Dan Willemsen54daaf02018-03-12 13:24:09 -070079 f func(PackageVarContext) string) blueprint.Variable {
Colin Cross0875c522017-11-28 17:34:01 -080080
81 return p.PackageContext.VariableFunc(name, func(config interface{}) (string, error) {
Dan Willemsen54daaf02018-03-12 13:24:09 -070082 ctx := &configErrorWrapper{p, config.(Config), nil}
83 ret := f(ctx)
84 if len(ctx.errors) > 0 {
85 return "", ctx.errors[0]
86 }
87 return ret, nil
Colin Cross0875c522017-11-28 17:34:01 -080088 })
89}
90
91// PoolFunc wraps blueprint.PackageContext.PoolFunc, converting the interface{} config
Dan Willemsen54daaf02018-03-12 13:24:09 -070092// argument to a Context that supports Config().
Colin Cross0875c522017-11-28 17:34:01 -080093func (p PackageContext) PoolFunc(name string,
Dan Willemsen54daaf02018-03-12 13:24:09 -070094 f func(PackagePoolContext) blueprint.PoolParams) blueprint.Pool {
Colin Cross0875c522017-11-28 17:34:01 -080095
96 return p.PackageContext.PoolFunc(name, func(config interface{}) (blueprint.PoolParams, error) {
Dan Willemsen54daaf02018-03-12 13:24:09 -070097 ctx := &configErrorWrapper{p, config.(Config), nil}
98 params := f(ctx)
99 if len(ctx.errors) > 0 {
100 return params, ctx.errors[0]
101 }
102 return params, nil
Colin Cross0875c522017-11-28 17:34:01 -0800103 })
104}
105
106// RuleFunc wraps blueprint.PackageContext.RuleFunc, converting the interface{} config
Dan Willemsen54daaf02018-03-12 13:24:09 -0700107// argument to a Context that supports Config().
Colin Cross0875c522017-11-28 17:34:01 -0800108func (p PackageContext) RuleFunc(name string,
Dan Willemsen54daaf02018-03-12 13:24:09 -0700109 f func(PackageRuleContext) blueprint.RuleParams, argNames ...string) blueprint.Rule {
Colin Cross0875c522017-11-28 17:34:01 -0800110
111 return p.PackageContext.RuleFunc(name, func(config interface{}) (blueprint.RuleParams, error) {
Dan Willemsen54daaf02018-03-12 13:24:09 -0700112 ctx := &configErrorWrapper{p, config.(Config), nil}
113 params := f(ctx)
114 if len(ctx.errors) > 0 {
115 return params, ctx.errors[0]
116 }
117 return params, nil
Colin Cross0875c522017-11-28 17:34:01 -0800118 }, argNames...)
119}
120
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700121// SourcePathVariable returns a Variable whose value is the source directory
122// appended with the supplied path. It may only be called during a Go package's
123// initialization - either from the init() function or as part of a
124// package-scoped variable's initialization.
Colin Cross0875c522017-11-28 17:34:01 -0800125func (p PackageContext) SourcePathVariable(name, path string) blueprint.Variable {
Dan Willemsen54daaf02018-03-12 13:24:09 -0700126 return p.VariableFunc(name, func(ctx PackageVarContext) string {
127 return safePathForSource(ctx, path).String()
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700128 })
129}
130
Colin Crossc6bbef32017-08-14 14:16:06 -0700131// SourcePathsVariable returns a Variable whose value is the source directory
132// appended with the supplied paths, joined with separator. It may only be
133// called during a Go package's initialization - either from the init()
134// function or as part of a package-scoped variable's initialization.
Colin Cross0875c522017-11-28 17:34:01 -0800135func (p PackageContext) SourcePathsVariable(name, separator string, paths ...string) blueprint.Variable {
Dan Willemsen54daaf02018-03-12 13:24:09 -0700136 return p.VariableFunc(name, func(ctx PackageVarContext) string {
Colin Crossc6bbef32017-08-14 14:16:06 -0700137 var ret []string
138 for _, path := range paths {
139 p := safePathForSource(ctx, path)
Colin Crossc6bbef32017-08-14 14:16:06 -0700140 ret = append(ret, p.String())
141 }
Dan Willemsen54daaf02018-03-12 13:24:09 -0700142 return strings.Join(ret, separator)
Colin Crossc6bbef32017-08-14 14:16:06 -0700143 })
144}
145
Colin Cross64162712017-08-08 13:17:59 -0700146// SourcePathVariableWithEnvOverride returns a Variable whose value is the source directory
147// appended with the supplied path, or the value of the given environment variable if it is set.
148// The environment variable is not required to point to a path inside the source tree.
149// It may only be called during a Go package's initialization - either from the init() function or
150// as part of a package-scoped variable's initialization.
Colin Cross0875c522017-11-28 17:34:01 -0800151func (p PackageContext) SourcePathVariableWithEnvOverride(name, path, env string) blueprint.Variable {
Dan Willemsen54daaf02018-03-12 13:24:09 -0700152 return p.VariableFunc(name, func(ctx PackageVarContext) string {
Colin Cross64162712017-08-08 13:17:59 -0700153 p := safePathForSource(ctx, path)
Dan Willemsen54daaf02018-03-12 13:24:09 -0700154 return ctx.Config().GetenvWithDefault(env, p.String())
Colin Cross64162712017-08-08 13:17:59 -0700155 })
156}
157
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800158// HostBinToolVariable returns a Variable whose value is the path to a host tool
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700159// in the bin directory for host targets. It may only be called during a Go
160// package's initialization - either from the init() function or as part of a
161// package-scoped variable's initialization.
Colin Cross0875c522017-11-28 17:34:01 -0800162func (p PackageContext) HostBinToolVariable(name, path string) blueprint.Variable {
Dan Willemsen54daaf02018-03-12 13:24:09 -0700163 return p.VariableFunc(name, func(ctx PackageVarContext) string {
164 return p.HostBinToolPath(ctx, path).String()
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700165 })
166}
167
Dan Willemsen54daaf02018-03-12 13:24:09 -0700168func (p PackageContext) HostBinToolPath(ctx PackageVarContext, path string) Path {
169 return PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "bin", path)
Alan Leung1d476fc2017-10-17 18:50:50 -0700170}
171
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800172// HostJNIToolVariable returns a Variable whose value is the path to a host tool
173// in the lib directory for host targets. It may only be called during a Go
174// package's initialization - either from the init() function or as part of a
175// package-scoped variable's initialization.
176func (p PackageContext) HostJNIToolVariable(name, path string) blueprint.Variable {
Dan Willemsen54daaf02018-03-12 13:24:09 -0700177 return p.VariableFunc(name, func(ctx PackageVarContext) string {
178 return p.HostJNIToolPath(ctx, path).String()
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800179 })
180}
181
Dan Willemsen54daaf02018-03-12 13:24:09 -0700182func (p PackageContext) HostJNIToolPath(ctx PackageVarContext, path string) Path {
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800183 ext := ".so"
184 if runtime.GOOS == "darwin" {
185 ext = ".dylib"
186 }
Dan Willemsen54daaf02018-03-12 13:24:09 -0700187 return PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "lib64", path+ext)
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800188}
189
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700190// HostJavaToolVariable returns a Variable whose value is the path to a host
191// tool in the frameworks directory for host targets. It may only be called
192// during a Go package's initialization - either from the init() function or as
193// part of a package-scoped variable's initialization.
Colin Cross0875c522017-11-28 17:34:01 -0800194func (p PackageContext) HostJavaToolVariable(name, path string) blueprint.Variable {
Dan Willemsen54daaf02018-03-12 13:24:09 -0700195 return p.VariableFunc(name, func(ctx PackageVarContext) string {
196 return p.HostJavaToolPath(ctx, path).String()
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700197 })
198}
199
Dan Willemsen54daaf02018-03-12 13:24:09 -0700200func (p PackageContext) HostJavaToolPath(ctx PackageVarContext, path string) Path {
201 return PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "framework", path)
Nan Zhang9a364182017-10-25 11:11:37 -0700202}
203
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700204// IntermediatesPathVariable returns a Variable whose value is the intermediate
205// directory appended with the supplied path. It may only be called during a Go
206// package's initialization - either from the init() function or as part of a
207// package-scoped variable's initialization.
Colin Cross0875c522017-11-28 17:34:01 -0800208func (p PackageContext) IntermediatesPathVariable(name, path string) blueprint.Variable {
Dan Willemsen54daaf02018-03-12 13:24:09 -0700209 return p.VariableFunc(name, func(ctx PackageVarContext) string {
210 return PathForIntermediates(ctx, path).String()
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700211 })
212}
213
Jeff Gaston734e3802017-04-10 15:47:24 -0700214// PrefixedExistentPathsForSourcesVariable returns a Variable whose value is the
Dan Willemsen7b310ee2015-12-18 15:11:17 -0800215// list of present source paths prefixed with the supplied prefix. It may only
216// be called during a Go package's initialization - either from the init()
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700217// function or as part of a package-scoped variable's initialization.
Colin Cross0875c522017-11-28 17:34:01 -0800218func (p PackageContext) PrefixedExistentPathsForSourcesVariable(
Dan Willemsen7b310ee2015-12-18 15:11:17 -0800219 name, prefix string, paths []string) blueprint.Variable {
220
Dan Willemsen54daaf02018-03-12 13:24:09 -0700221 return p.VariableFunc(name, func(ctx PackageVarContext) string {
Colin Cross32f38982018-02-22 11:47:25 -0800222 paths := ExistentPathsForSources(ctx, paths)
Dan Willemsen54daaf02018-03-12 13:24:09 -0700223 return JoinWithPrefix(paths.Strings(), prefix)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700224 })
225}
Colin Cross9d45bb72016-08-29 16:14:13 -0700226
Colin Cross9d45bb72016-08-29 16:14:13 -0700227// AndroidStaticRule wraps blueprint.StaticRule and provides a default Pool if none is specified
Colin Cross0875c522017-11-28 17:34:01 -0800228func (p PackageContext) AndroidStaticRule(name string, params blueprint.RuleParams,
Colin Cross9d45bb72016-08-29 16:14:13 -0700229 argNames ...string) blueprint.Rule {
Dan Willemsen54daaf02018-03-12 13:24:09 -0700230 return p.AndroidRuleFunc(name, func(PackageRuleContext) blueprint.RuleParams {
231 return params
Colin Cross9d45bb72016-08-29 16:14:13 -0700232 }, argNames...)
233}
234
235// AndroidGomaStaticRule wraps blueprint.StaticRule but uses goma's parallelism if goma is enabled
Colin Cross0875c522017-11-28 17:34:01 -0800236func (p PackageContext) AndroidGomaStaticRule(name string, params blueprint.RuleParams,
Colin Cross9d45bb72016-08-29 16:14:13 -0700237 argNames ...string) blueprint.Rule {
238 return p.StaticRule(name, params, argNames...)
239}
240
Colin Cross0875c522017-11-28 17:34:01 -0800241func (p PackageContext) AndroidRuleFunc(name string,
Dan Willemsen54daaf02018-03-12 13:24:09 -0700242 f func(PackageRuleContext) blueprint.RuleParams, argNames ...string) blueprint.Rule {
243 return p.RuleFunc(name, func(ctx PackageRuleContext) blueprint.RuleParams {
244 params := f(ctx)
245 if ctx.Config().UseGoma() && params.Pool == nil {
Colin Cross9d45bb72016-08-29 16:14:13 -0700246 // When USE_GOMA=true is set and the rule is not supported by goma, restrict jobs to the
247 // local parallelism value
248 params.Pool = localPool
249 }
Dan Willemsen54daaf02018-03-12 13:24:09 -0700250 return params
Colin Cross9d45bb72016-08-29 16:14:13 -0700251 }, argNames...)
252}