blob: 2bc98aaf982bd70129e95d339628b8eeca351a3e [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"
19
20 "github.com/google/blueprint"
Colin Cross294941b2017-02-01 14:10:36 -080021 "github.com/google/blueprint/pathtools"
Dan Willemsen34cc69e2015-09-23 15:26:20 -070022)
23
24// AndroidPackageContext is a wrapper for blueprint.PackageContext that adds
25// some android-specific helper functions.
26type AndroidPackageContext struct {
27 blueprint.PackageContext
28}
29
30func NewPackageContext(pkgPath string) AndroidPackageContext {
31 return AndroidPackageContext{blueprint.NewPackageContext(pkgPath)}
32}
33
34// configErrorWrapper can be used with Path functions when a Context is not
35// available. A Config can be provided, and errors are stored as a list for
36// later retrieval.
37//
38// The most common use here will be with VariableFunc, where only a config is
39// provided, and an error should be returned.
40type configErrorWrapper struct {
Dan Willemsen7b310ee2015-12-18 15:11:17 -080041 pctx AndroidPackageContext
Dan Willemsen34cc69e2015-09-23 15:26:20 -070042 config Config
43 errors []error
44}
45
46var _ PathContext = &configErrorWrapper{}
47var _ errorfContext = &configErrorWrapper{}
48
49func (e *configErrorWrapper) Config() interface{} {
50 return e.config
51}
52func (e *configErrorWrapper) Errorf(format string, args ...interface{}) {
53 e.errors = append(e.errors, fmt.Errorf(format, args...))
54}
Dan Willemsen7b310ee2015-12-18 15:11:17 -080055func (e *configErrorWrapper) AddNinjaFileDeps(deps ...string) {
56 e.pctx.AddNinjaFileDeps(deps...)
57}
Dan Willemsen34cc69e2015-09-23 15:26:20 -070058
Colin Cross294941b2017-02-01 14:10:36 -080059func (e *configErrorWrapper) Fs() pathtools.FileSystem {
60 return nil
61}
62
Dan Willemsen34cc69e2015-09-23 15:26:20 -070063// SourcePathVariable returns a Variable whose value is the source directory
64// appended with the supplied path. It may only be called during a Go package's
65// initialization - either from the init() function or as part of a
66// package-scoped variable's initialization.
67func (p AndroidPackageContext) SourcePathVariable(name, path string) blueprint.Variable {
68 return p.VariableFunc(name, func(config interface{}) (string, error) {
Dan Willemsen7b310ee2015-12-18 15:11:17 -080069 ctx := &configErrorWrapper{p, config.(Config), []error{}}
Dan Willemsen34cc69e2015-09-23 15:26:20 -070070 p := safePathForSource(ctx, path)
71 if len(ctx.errors) > 0 {
72 return "", ctx.errors[0]
73 }
74 return p.String(), nil
75 })
76}
77
Colin Cross64162712017-08-08 13:17:59 -070078// SourcePathVariableWithEnvOverride returns a Variable whose value is the source directory
79// appended with the supplied path, or the value of the given environment variable if it is set.
80// The environment variable is not required to point to a path inside the source tree.
81// It may only be called during a Go package's initialization - either from the init() function or
82// as part of a package-scoped variable's initialization.
83func (p AndroidPackageContext) SourcePathVariableWithEnvOverride(name, path, env string) blueprint.Variable {
84 return p.VariableFunc(name, func(config interface{}) (string, error) {
85 ctx := &configErrorWrapper{p, config.(Config), []error{}}
86 p := safePathForSource(ctx, path)
87 if len(ctx.errors) > 0 {
88 return "", ctx.errors[0]
89 }
90 return config.(Config).GetenvWithDefault(env, p.String()), nil
91 })
92}
93
Dan Willemsen34cc69e2015-09-23 15:26:20 -070094// HostBinVariable returns a Variable whose value is the path to a host tool
95// in the bin directory for host targets. It may only be called during a Go
96// package's initialization - either from the init() function or as part of a
97// package-scoped variable's initialization.
98func (p AndroidPackageContext) HostBinToolVariable(name, path string) blueprint.Variable {
99 return p.VariableFunc(name, func(config interface{}) (string, error) {
Dan Willemsen7b310ee2015-12-18 15:11:17 -0800100 ctx := &configErrorWrapper{p, config.(Config), []error{}}
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700101 p := PathForOutput(ctx, "host", ctx.config.PrebuiltOS(), "bin", path)
102 if len(ctx.errors) > 0 {
103 return "", ctx.errors[0]
104 }
105 return p.String(), nil
106 })
107}
108
109// HostJavaToolVariable returns a Variable whose value is the path to a host
110// tool in the frameworks directory for host targets. It may only be called
111// during a Go package's initialization - either from the init() function or as
112// part of a package-scoped variable's initialization.
113func (p AndroidPackageContext) HostJavaToolVariable(name, path string) blueprint.Variable {
114 return p.VariableFunc(name, func(config interface{}) (string, error) {
Dan Willemsen7b310ee2015-12-18 15:11:17 -0800115 ctx := &configErrorWrapper{p, config.(Config), []error{}}
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700116 p := PathForOutput(ctx, "host", ctx.config.PrebuiltOS(), "framework", path)
117 if len(ctx.errors) > 0 {
118 return "", ctx.errors[0]
119 }
120 return p.String(), nil
121 })
122}
123
124// IntermediatesPathVariable returns a Variable whose value is the intermediate
125// directory appended with the supplied path. It may only be called during a Go
126// package's initialization - either from the init() function or as part of a
127// package-scoped variable's initialization.
128func (p AndroidPackageContext) IntermediatesPathVariable(name, path string) blueprint.Variable {
129 return p.VariableFunc(name, func(config interface{}) (string, error) {
Dan Willemsen7b310ee2015-12-18 15:11:17 -0800130 ctx := &configErrorWrapper{p, config.(Config), []error{}}
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700131 p := PathForIntermediates(ctx, path)
132 if len(ctx.errors) > 0 {
133 return "", ctx.errors[0]
134 }
135 return p.String(), nil
136 })
137}
138
Jeff Gaston734e3802017-04-10 15:47:24 -0700139// PrefixedExistentPathsForSourcesVariable returns a Variable whose value is the
Dan Willemsen7b310ee2015-12-18 15:11:17 -0800140// list of present source paths prefixed with the supplied prefix. It may only
141// be called during a Go package's initialization - either from the init()
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700142// function or as part of a package-scoped variable's initialization.
Jeff Gaston734e3802017-04-10 15:47:24 -0700143func (p AndroidPackageContext) PrefixedExistentPathsForSourcesVariable(
Dan Willemsen7b310ee2015-12-18 15:11:17 -0800144 name, prefix string, paths []string) blueprint.Variable {
145
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700146 return p.VariableFunc(name, func(config interface{}) (string, error) {
Dan Willemsen7b310ee2015-12-18 15:11:17 -0800147 ctx := &configErrorWrapper{p, config.(Config), []error{}}
Jeff Gaston734e3802017-04-10 15:47:24 -0700148 paths := ExistentPathsForSources(ctx, "", paths)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700149 if len(ctx.errors) > 0 {
150 return "", ctx.errors[0]
151 }
152 return JoinWithPrefix(paths.Strings(), prefix), nil
153 })
154}
Colin Cross9d45bb72016-08-29 16:14:13 -0700155
156type RuleParams struct {
157 blueprint.RuleParams
158 GomaSupported bool
159}
160
161// AndroidStaticRule wraps blueprint.StaticRule and provides a default Pool if none is specified
162func (p AndroidPackageContext) AndroidStaticRule(name string, params blueprint.RuleParams,
163 argNames ...string) blueprint.Rule {
164 return p.AndroidRuleFunc(name, func(interface{}) (blueprint.RuleParams, error) {
165 return params, nil
166 }, argNames...)
167}
168
169// AndroidGomaStaticRule wraps blueprint.StaticRule but uses goma's parallelism if goma is enabled
170func (p AndroidPackageContext) AndroidGomaStaticRule(name string, params blueprint.RuleParams,
171 argNames ...string) blueprint.Rule {
172 return p.StaticRule(name, params, argNames...)
173}
174
175func (p AndroidPackageContext) AndroidRuleFunc(name string,
176 f func(interface{}) (blueprint.RuleParams, error), argNames ...string) blueprint.Rule {
177 return p.PackageContext.RuleFunc(name, func(config interface{}) (blueprint.RuleParams, error) {
178 params, err := f(config)
179 if config.(Config).UseGoma() && params.Pool == nil {
180 // When USE_GOMA=true is set and the rule is not supported by goma, restrict jobs to the
181 // local parallelism value
182 params.Pool = localPool
183 }
184 return params, err
185 }, argNames...)
186}