blob: ee826c819549702d252e3da2b3b1097396ac3852 [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"
21)
22
23// AndroidPackageContext is a wrapper for blueprint.PackageContext that adds
24// some android-specific helper functions.
25type AndroidPackageContext struct {
26 blueprint.PackageContext
27}
28
29func NewPackageContext(pkgPath string) AndroidPackageContext {
30 return AndroidPackageContext{blueprint.NewPackageContext(pkgPath)}
31}
32
33// configErrorWrapper can be used with Path functions when a Context is not
34// available. A Config can be provided, and errors are stored as a list for
35// later retrieval.
36//
37// The most common use here will be with VariableFunc, where only a config is
38// provided, and an error should be returned.
39type configErrorWrapper struct {
Dan Willemsen7b310ee2015-12-18 15:11:17 -080040 pctx AndroidPackageContext
Dan Willemsen34cc69e2015-09-23 15:26:20 -070041 config Config
42 errors []error
43}
44
45var _ PathContext = &configErrorWrapper{}
46var _ errorfContext = &configErrorWrapper{}
47
48func (e *configErrorWrapper) Config() interface{} {
49 return e.config
50}
51func (e *configErrorWrapper) Errorf(format string, args ...interface{}) {
52 e.errors = append(e.errors, fmt.Errorf(format, args...))
53}
Dan Willemsen7b310ee2015-12-18 15:11:17 -080054func (e *configErrorWrapper) AddNinjaFileDeps(deps ...string) {
55 e.pctx.AddNinjaFileDeps(deps...)
56}
Dan Willemsen34cc69e2015-09-23 15:26:20 -070057
58// SourcePathVariable returns a Variable whose value is the source directory
59// appended with the supplied path. It may only be called during a Go package's
60// initialization - either from the init() function or as part of a
61// package-scoped variable's initialization.
62func (p AndroidPackageContext) SourcePathVariable(name, path string) blueprint.Variable {
63 return p.VariableFunc(name, func(config interface{}) (string, error) {
Dan Willemsen7b310ee2015-12-18 15:11:17 -080064 ctx := &configErrorWrapper{p, config.(Config), []error{}}
Dan Willemsen34cc69e2015-09-23 15:26:20 -070065 p := safePathForSource(ctx, path)
66 if len(ctx.errors) > 0 {
67 return "", ctx.errors[0]
68 }
69 return p.String(), nil
70 })
71}
72
73// HostBinVariable returns a Variable whose value is the path to a host tool
74// in the bin directory for host targets. It may only be called during a Go
75// package's initialization - either from the init() function or as part of a
76// package-scoped variable's initialization.
77func (p AndroidPackageContext) HostBinToolVariable(name, path string) blueprint.Variable {
78 return p.VariableFunc(name, func(config interface{}) (string, error) {
Dan Willemsen7b310ee2015-12-18 15:11:17 -080079 ctx := &configErrorWrapper{p, config.(Config), []error{}}
Dan Willemsen34cc69e2015-09-23 15:26:20 -070080 p := PathForOutput(ctx, "host", ctx.config.PrebuiltOS(), "bin", path)
81 if len(ctx.errors) > 0 {
82 return "", ctx.errors[0]
83 }
84 return p.String(), nil
85 })
86}
87
88// HostJavaToolVariable returns a Variable whose value is the path to a host
89// tool in the frameworks directory for host targets. It may only be called
90// during a Go package's initialization - either from the init() function or as
91// part of a package-scoped variable's initialization.
92func (p AndroidPackageContext) HostJavaToolVariable(name, path string) blueprint.Variable {
93 return p.VariableFunc(name, func(config interface{}) (string, error) {
Dan Willemsen7b310ee2015-12-18 15:11:17 -080094 ctx := &configErrorWrapper{p, config.(Config), []error{}}
Dan Willemsen34cc69e2015-09-23 15:26:20 -070095 p := PathForOutput(ctx, "host", ctx.config.PrebuiltOS(), "framework", path)
96 if len(ctx.errors) > 0 {
97 return "", ctx.errors[0]
98 }
99 return p.String(), nil
100 })
101}
102
103// IntermediatesPathVariable returns a Variable whose value is the intermediate
104// directory appended with the supplied path. It may only be called during a Go
105// package's initialization - either from the init() function or as part of a
106// package-scoped variable's initialization.
107func (p AndroidPackageContext) IntermediatesPathVariable(name, path string) blueprint.Variable {
108 return p.VariableFunc(name, func(config interface{}) (string, error) {
Dan Willemsen7b310ee2015-12-18 15:11:17 -0800109 ctx := &configErrorWrapper{p, config.(Config), []error{}}
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700110 p := PathForIntermediates(ctx, path)
111 if len(ctx.errors) > 0 {
112 return "", ctx.errors[0]
113 }
114 return p.String(), nil
115 })
116}
117
Dan Willemsen7b310ee2015-12-18 15:11:17 -0800118// PrefixedPathsForOptionalSourceVariable returns a Variable whose value is the
119// list of present source paths prefixed with the supplied prefix. It may only
120// be called during a Go package's initialization - either from the init()
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700121// function or as part of a package-scoped variable's initialization.
Dan Willemsen7b310ee2015-12-18 15:11:17 -0800122func (p AndroidPackageContext) PrefixedPathsForOptionalSourceVariable(
123 name, prefix string, paths []string) blueprint.Variable {
124
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700125 return p.VariableFunc(name, func(config interface{}) (string, error) {
Dan Willemsen7b310ee2015-12-18 15:11:17 -0800126 ctx := &configErrorWrapper{p, config.(Config), []error{}}
127 paths := PathsForOptionalSource(ctx, "", paths)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700128 if len(ctx.errors) > 0 {
129 return "", ctx.errors[0]
130 }
131 return JoinWithPrefix(paths.Strings(), prefix), nil
132 })
133}
Colin Cross9d45bb72016-08-29 16:14:13 -0700134
135type RuleParams struct {
136 blueprint.RuleParams
137 GomaSupported bool
138}
139
140// AndroidStaticRule wraps blueprint.StaticRule and provides a default Pool if none is specified
141func (p AndroidPackageContext) AndroidStaticRule(name string, params blueprint.RuleParams,
142 argNames ...string) blueprint.Rule {
143 return p.AndroidRuleFunc(name, func(interface{}) (blueprint.RuleParams, error) {
144 return params, nil
145 }, argNames...)
146}
147
148// AndroidGomaStaticRule wraps blueprint.StaticRule but uses goma's parallelism if goma is enabled
149func (p AndroidPackageContext) AndroidGomaStaticRule(name string, params blueprint.RuleParams,
150 argNames ...string) blueprint.Rule {
151 return p.StaticRule(name, params, argNames...)
152}
153
154func (p AndroidPackageContext) AndroidRuleFunc(name string,
155 f func(interface{}) (blueprint.RuleParams, error), argNames ...string) blueprint.Rule {
156 return p.PackageContext.RuleFunc(name, func(config interface{}) (blueprint.RuleParams, error) {
157 params, err := f(config)
158 if config.(Config).UseGoma() && params.Pool == nil {
159 // When USE_GOMA=true is set and the rule is not supported by goma, restrict jobs to the
160 // local parallelism value
161 params.Pool = localPool
162 }
163 return params, err
164 }, argNames...)
165}