github.com/westcoastroms/westcoastroms-build@v0.0.0-20190928114312-2350e5a73030/build/soong/android/package_ctx.go (about) 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 android 16 17 import ( 18 "fmt" 19 "runtime" 20 "strings" 21 22 "github.com/google/blueprint" 23 "github.com/google/blueprint/pathtools" 24 ) 25 26 // PackageContext is a wrapper for blueprint.PackageContext that adds 27 // some android-specific helper functions. 28 type PackageContext struct { 29 blueprint.PackageContext 30 } 31 32 func NewPackageContext(pkgPath string) PackageContext { 33 return PackageContext{blueprint.NewPackageContext(pkgPath)} 34 } 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. 42 type configErrorWrapper struct { 43 pctx PackageContext 44 config Config 45 errors []error 46 } 47 48 var _ PathContext = &configErrorWrapper{} 49 var _ errorfContext = &configErrorWrapper{} 50 var _ PackageVarContext = &configErrorWrapper{} 51 var _ PackagePoolContext = &configErrorWrapper{} 52 var _ PackageRuleContext = &configErrorWrapper{} 53 54 func (e *configErrorWrapper) Config() Config { 55 return e.config 56 } 57 func (e *configErrorWrapper) Errorf(format string, args ...interface{}) { 58 e.errors = append(e.errors, fmt.Errorf(format, args...)) 59 } 60 func (e *configErrorWrapper) AddNinjaFileDeps(deps ...string) { 61 e.pctx.AddNinjaFileDeps(deps...) 62 } 63 64 func (e *configErrorWrapper) Fs() pathtools.FileSystem { 65 return nil 66 } 67 68 type PackageVarContext interface { 69 PathContext 70 errorfContext 71 } 72 73 type PackagePoolContext PackageVarContext 74 type PackageRuleContext PackageVarContext 75 76 // VariableFunc wraps blueprint.PackageContext.VariableFunc, converting the interface{} config 77 // argument to a PackageVarContext. 78 func (p PackageContext) VariableFunc(name string, 79 f func(PackageVarContext) string) blueprint.Variable { 80 81 return p.PackageContext.VariableFunc(name, func(config interface{}) (string, error) { 82 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 88 }) 89 } 90 91 // PoolFunc wraps blueprint.PackageContext.PoolFunc, converting the interface{} config 92 // argument to a Context that supports Config(). 93 func (p PackageContext) PoolFunc(name string, 94 f func(PackagePoolContext) blueprint.PoolParams) blueprint.Pool { 95 96 return p.PackageContext.PoolFunc(name, func(config interface{}) (blueprint.PoolParams, error) { 97 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 103 }) 104 } 105 106 // RuleFunc wraps blueprint.PackageContext.RuleFunc, converting the interface{} config 107 // argument to a Context that supports Config(). 108 func (p PackageContext) RuleFunc(name string, 109 f func(PackageRuleContext) blueprint.RuleParams, argNames ...string) blueprint.Rule { 110 111 return p.PackageContext.RuleFunc(name, func(config interface{}) (blueprint.RuleParams, error) { 112 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 118 }, argNames...) 119 } 120 121 // 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. 125 func (p PackageContext) SourcePathVariable(name, path string) blueprint.Variable { 126 return p.VariableFunc(name, func(ctx PackageVarContext) string { 127 return safePathForSource(ctx, path).String() 128 }) 129 } 130 131 // 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. 135 func (p PackageContext) SourcePathsVariable(name, separator string, paths ...string) blueprint.Variable { 136 return p.VariableFunc(name, func(ctx PackageVarContext) string { 137 var ret []string 138 for _, path := range paths { 139 p := safePathForSource(ctx, path) 140 ret = append(ret, p.String()) 141 } 142 return strings.Join(ret, separator) 143 }) 144 } 145 146 // 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. 151 func (p PackageContext) SourcePathVariableWithEnvOverride(name, path, env string) blueprint.Variable { 152 return p.VariableFunc(name, func(ctx PackageVarContext) string { 153 p := safePathForSource(ctx, path) 154 return ctx.Config().GetenvWithDefault(env, p.String()) 155 }) 156 } 157 158 // HostBinToolVariable returns a Variable whose value is the path to a host tool 159 // 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. 162 func (p PackageContext) HostBinToolVariable(name, path string) blueprint.Variable { 163 return p.VariableFunc(name, func(ctx PackageVarContext) string { 164 return p.HostBinToolPath(ctx, path).String() 165 }) 166 } 167 168 func (p PackageContext) HostBinToolPath(ctx PackageVarContext, path string) Path { 169 return PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "bin", path) 170 } 171 172 // 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. 176 func (p PackageContext) HostJNIToolVariable(name, path string) blueprint.Variable { 177 return p.VariableFunc(name, func(ctx PackageVarContext) string { 178 return p.HostJNIToolPath(ctx, path).String() 179 }) 180 } 181 182 func (p PackageContext) HostJNIToolPath(ctx PackageVarContext, path string) Path { 183 ext := ".so" 184 if runtime.GOOS == "darwin" { 185 ext = ".dylib" 186 } 187 return PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "lib64", path+ext) 188 } 189 190 // 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. 194 func (p PackageContext) HostJavaToolVariable(name, path string) blueprint.Variable { 195 return p.VariableFunc(name, func(ctx PackageVarContext) string { 196 return p.HostJavaToolPath(ctx, path).String() 197 }) 198 } 199 200 func (p PackageContext) HostJavaToolPath(ctx PackageVarContext, path string) Path { 201 return PathForOutput(ctx, "host", ctx.Config().PrebuiltOS(), "framework", path) 202 } 203 204 // 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. 208 func (p PackageContext) IntermediatesPathVariable(name, path string) blueprint.Variable { 209 return p.VariableFunc(name, func(ctx PackageVarContext) string { 210 return PathForIntermediates(ctx, path).String() 211 }) 212 } 213 214 // PrefixedExistentPathsForSourcesVariable returns a Variable whose value is the 215 // 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() 217 // function or as part of a package-scoped variable's initialization. 218 func (p PackageContext) PrefixedExistentPathsForSourcesVariable( 219 name, prefix string, paths []string) blueprint.Variable { 220 221 return p.VariableFunc(name, func(ctx PackageVarContext) string { 222 paths := ExistentPathsForSources(ctx, paths) 223 return JoinWithPrefix(paths.Strings(), prefix) 224 }) 225 } 226 227 // AndroidStaticRule wraps blueprint.StaticRule and provides a default Pool if none is specified 228 func (p PackageContext) AndroidStaticRule(name string, params blueprint.RuleParams, 229 argNames ...string) blueprint.Rule { 230 return p.AndroidRuleFunc(name, func(PackageRuleContext) blueprint.RuleParams { 231 return params 232 }, argNames...) 233 } 234 235 // AndroidGomaStaticRule wraps blueprint.StaticRule but uses goma's parallelism if goma is enabled 236 func (p PackageContext) AndroidGomaStaticRule(name string, params blueprint.RuleParams, 237 argNames ...string) blueprint.Rule { 238 return p.StaticRule(name, params, argNames...) 239 } 240 241 func (p PackageContext) AndroidRuleFunc(name string, 242 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 { 246 // 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 } 250 return params 251 }, argNames...) 252 }