Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame^] | 1 | // 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 | |
| 15 | package common |
| 16 | |
| 17 | import ( |
| 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. |
| 25 | type AndroidPackageContext struct { |
| 26 | blueprint.PackageContext |
| 27 | } |
| 28 | |
| 29 | func 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. |
| 39 | type configErrorWrapper struct { |
| 40 | config Config |
| 41 | errors []error |
| 42 | } |
| 43 | |
| 44 | var _ PathContext = &configErrorWrapper{} |
| 45 | var _ errorfContext = &configErrorWrapper{} |
| 46 | |
| 47 | func (e *configErrorWrapper) Config() interface{} { |
| 48 | return e.config |
| 49 | } |
| 50 | func (e *configErrorWrapper) Errorf(format string, args ...interface{}) { |
| 51 | e.errors = append(e.errors, fmt.Errorf(format, args...)) |
| 52 | } |
| 53 | |
| 54 | // SourcePathVariable returns a Variable whose value is the source directory |
| 55 | // appended with the supplied path. It may only be called during a Go package's |
| 56 | // initialization - either from the init() function or as part of a |
| 57 | // package-scoped variable's initialization. |
| 58 | func (p AndroidPackageContext) SourcePathVariable(name, path string) blueprint.Variable { |
| 59 | return p.VariableFunc(name, func(config interface{}) (string, error) { |
| 60 | ctx := &configErrorWrapper{config.(Config), []error{}} |
| 61 | p := safePathForSource(ctx, path) |
| 62 | if len(ctx.errors) > 0 { |
| 63 | return "", ctx.errors[0] |
| 64 | } |
| 65 | return p.String(), nil |
| 66 | }) |
| 67 | } |
| 68 | |
| 69 | // HostBinVariable returns a Variable whose value is the path to a host tool |
| 70 | // in the bin directory for host targets. It may only be called during a Go |
| 71 | // package's initialization - either from the init() function or as part of a |
| 72 | // package-scoped variable's initialization. |
| 73 | func (p AndroidPackageContext) HostBinToolVariable(name, path string) blueprint.Variable { |
| 74 | return p.VariableFunc(name, func(config interface{}) (string, error) { |
| 75 | ctx := &configErrorWrapper{config.(Config), []error{}} |
| 76 | p := PathForOutput(ctx, "host", ctx.config.PrebuiltOS(), "bin", path) |
| 77 | if len(ctx.errors) > 0 { |
| 78 | return "", ctx.errors[0] |
| 79 | } |
| 80 | return p.String(), nil |
| 81 | }) |
| 82 | } |
| 83 | |
| 84 | // HostJavaToolVariable returns a Variable whose value is the path to a host |
| 85 | // tool in the frameworks directory for host targets. It may only be called |
| 86 | // during a Go package's initialization - either from the init() function or as |
| 87 | // part of a package-scoped variable's initialization. |
| 88 | func (p AndroidPackageContext) HostJavaToolVariable(name, path string) blueprint.Variable { |
| 89 | return p.VariableFunc(name, func(config interface{}) (string, error) { |
| 90 | ctx := &configErrorWrapper{config.(Config), []error{}} |
| 91 | p := PathForOutput(ctx, "host", ctx.config.PrebuiltOS(), "framework", path) |
| 92 | if len(ctx.errors) > 0 { |
| 93 | return "", ctx.errors[0] |
| 94 | } |
| 95 | return p.String(), nil |
| 96 | }) |
| 97 | } |
| 98 | |
| 99 | // IntermediatesPathVariable returns a Variable whose value is the intermediate |
| 100 | // directory appended with the supplied path. It may only be called during a Go |
| 101 | // package's initialization - either from the init() function or as part of a |
| 102 | // package-scoped variable's initialization. |
| 103 | func (p AndroidPackageContext) IntermediatesPathVariable(name, path string) blueprint.Variable { |
| 104 | return p.VariableFunc(name, func(config interface{}) (string, error) { |
| 105 | ctx := &configErrorWrapper{config.(Config), []error{}} |
| 106 | p := PathForIntermediates(ctx, path) |
| 107 | if len(ctx.errors) > 0 { |
| 108 | return "", ctx.errors[0] |
| 109 | } |
| 110 | return p.String(), nil |
| 111 | }) |
| 112 | } |
| 113 | |
| 114 | // PrefixedPathsForSourceVariable returns a Variable whose value is the |
| 115 | // list of source paths prefixed with the supplied prefix. It may only be |
| 116 | // called during a Go package's initialization - either from the init() |
| 117 | // function or as part of a package-scoped variable's initialization. |
| 118 | func (p AndroidPackageContext) PrefixedPathsForSourceVariable(name, prefix string, paths []string) blueprint.Variable { |
| 119 | return p.VariableFunc(name, func(config interface{}) (string, error) { |
| 120 | ctx := &configErrorWrapper{config.(Config), []error{}} |
| 121 | paths := PathsForSource(ctx, paths) |
| 122 | if len(ctx.errors) > 0 { |
| 123 | return "", ctx.errors[0] |
| 124 | } |
| 125 | return JoinWithPrefix(paths.Strings(), prefix), nil |
| 126 | }) |
| 127 | } |