Colin Cross | 8e0c511 | 2015-01-23 14:15:10 -0800 | [diff] [blame] | 1 | // Copyright 2014 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 | |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 15 | package blueprint |
| 16 | |
| 17 | import ( |
| 18 | "errors" |
| 19 | "fmt" |
Jamie Gennis | 28a63fb | 2014-06-05 19:50:44 -0700 | [diff] [blame] | 20 | "reflect" |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 21 | "runtime" |
| 22 | "strings" |
Colin Cross | 691a60d | 2015-01-07 18:08:56 -0800 | [diff] [blame] | 23 | "sync" |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 24 | ) |
| 25 | |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 26 | // A PackageContext provides a way to create package-scoped Ninja pools, |
| 27 | // rules, and variables. A Go package should create a single unexported |
| 28 | // package-scoped PackageContext variable that it uses to create all package- |
| 29 | // scoped Ninja object definitions. This PackageContext object should then be |
| 30 | // passed to all calls to define module- or singleton-specific Ninja |
| 31 | // definitions. For example: |
| 32 | // |
| 33 | // package blah |
| 34 | // |
| 35 | // import ( |
| 36 | // "blueprint" |
| 37 | // ) |
| 38 | // |
| 39 | // var ( |
| 40 | // pctx = NewPackageContext("path/to/blah") |
| 41 | // |
| 42 | // myPrivateVar = pctx.StaticVariable("myPrivateVar", "abcdef") |
| 43 | // MyExportedVar = pctx.StaticVariable("MyExportedVar", "$myPrivateVar 123456!") |
| 44 | // |
| 45 | // SomeRule = pctx.StaticRule(...) |
| 46 | // ) |
| 47 | // |
| 48 | // // ... |
| 49 | // |
| 50 | // func (m *MyModule) GenerateBuildActions(ctx blueprint.Module) { |
| 51 | // ctx.Build(pctx, blueprint.BuildParams{ |
| 52 | // Rule: SomeRule, |
| 53 | // Outputs: []string{"$myPrivateVar"}, |
| 54 | // }) |
| 55 | // } |
Dan Willemsen | aeffbf7 | 2015-11-25 15:29:32 -0800 | [diff] [blame] | 56 | type PackageContext interface { |
| 57 | Import(pkgPath string) |
| 58 | ImportAs(as, pkgPath string) |
| 59 | |
| 60 | StaticVariable(name, value string) Variable |
| 61 | VariableFunc(name string, f func(config interface{}) (string, error)) Variable |
| 62 | VariableConfigMethod(name string, method interface{}) Variable |
| 63 | |
| 64 | StaticPool(name string, params PoolParams) Pool |
| 65 | PoolFunc(name string, f func(interface{}) (PoolParams, error)) Pool |
| 66 | |
| 67 | StaticRule(name string, params RuleParams, argNames ...string) Rule |
| 68 | RuleFunc(name string, f func(interface{}) (RuleParams, error), argNames ...string) Rule |
| 69 | |
| 70 | getScope() *basicScope |
| 71 | } |
| 72 | |
| 73 | type packageContext struct { |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 74 | fullName string |
| 75 | shortName string |
| 76 | pkgPath string |
Jamie Gennis | 48aed8c | 2014-06-13 18:25:54 -0700 | [diff] [blame] | 77 | scope *basicScope |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 78 | } |
Dan Willemsen | aeffbf7 | 2015-11-25 15:29:32 -0800 | [diff] [blame] | 79 | var _ PackageContext = &packageContext{} |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 80 | |
Dan Willemsen | aeffbf7 | 2015-11-25 15:29:32 -0800 | [diff] [blame] | 81 | func (p *packageContext) getScope() *basicScope { |
| 82 | return p.scope |
| 83 | } |
| 84 | |
| 85 | var packageContexts = map[string]*packageContext{} |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 86 | |
| 87 | // NewPackageContext creates a PackageContext object for a given package. The |
| 88 | // pkgPath argument should always be set to the full path used to import the |
| 89 | // package. This function may only be called from a Go package's init() |
| 90 | // function or as part of a package-scoped variable initialization. |
Dan Willemsen | aeffbf7 | 2015-11-25 15:29:32 -0800 | [diff] [blame] | 91 | func NewPackageContext(pkgPath string) PackageContext { |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 92 | checkCalledFromInit() |
| 93 | |
| 94 | if _, present := packageContexts[pkgPath]; present { |
| 95 | panic(fmt.Errorf("package %q already has a package context")) |
| 96 | } |
| 97 | |
| 98 | pkgName := pkgPathToName(pkgPath) |
| 99 | err := validateNinjaName(pkgName) |
| 100 | if err != nil { |
| 101 | panic(err) |
| 102 | } |
| 103 | |
| 104 | i := strings.LastIndex(pkgPath, "/") |
| 105 | shortName := pkgPath[i+1:] |
| 106 | |
Dan Willemsen | aeffbf7 | 2015-11-25 15:29:32 -0800 | [diff] [blame] | 107 | p := &packageContext{ |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 108 | fullName: pkgName, |
| 109 | shortName: shortName, |
| 110 | pkgPath: pkgPath, |
| 111 | scope: newScope(nil), |
| 112 | } |
| 113 | |
| 114 | packageContexts[pkgPath] = p |
| 115 | |
| 116 | return p |
| 117 | } |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 118 | |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 119 | var Phony Rule = &builtinRule{ |
| 120 | name_: "phony", |
| 121 | } |
| 122 | |
Ken Oslund | 4b9a051 | 2015-04-14 16:26:58 -0700 | [diff] [blame] | 123 | var Console Pool = &builtinPool{ |
| 124 | name_: "console", |
| 125 | } |
| 126 | |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 127 | var errRuleIsBuiltin = errors.New("the rule is a built-in") |
Ken Oslund | 4b9a051 | 2015-04-14 16:26:58 -0700 | [diff] [blame] | 128 | var errPoolIsBuiltin = errors.New("the pool is a built-in") |
Jamie Gennis | 28a63fb | 2014-06-05 19:50:44 -0700 | [diff] [blame] | 129 | var errVariableIsArg = errors.New("argument variables have no value") |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 130 | |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 131 | // checkCalledFromInit panics if a Go package's init function is not on the |
| 132 | // call stack. |
| 133 | func checkCalledFromInit() { |
| 134 | for skip := 3; ; skip++ { |
| 135 | _, funcName, ok := callerName(skip) |
| 136 | if !ok { |
| 137 | panic("not called from an init func") |
| 138 | } |
| 139 | |
| 140 | if funcName == "init" || strings.HasPrefix(funcName, "init·") { |
| 141 | return |
| 142 | } |
| 143 | } |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 144 | } |
| 145 | |
Jamie Gennis | 0ed63ef | 2014-06-30 18:07:17 -0700 | [diff] [blame] | 146 | // callerName returns the package path and function name of the calling |
| 147 | // function. The skip argument has the same meaning as the skip argument of |
| 148 | // runtime.Callers. |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 149 | func callerName(skip int) (pkgPath, funcName string, ok bool) { |
Jamie Gennis | 0ed63ef | 2014-06-30 18:07:17 -0700 | [diff] [blame] | 150 | var pc [1]uintptr |
| 151 | n := runtime.Callers(skip+1, pc[:]) |
| 152 | if n != 1 { |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 153 | return "", "", false |
Jamie Gennis | 0ed63ef | 2014-06-30 18:07:17 -0700 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | f := runtime.FuncForPC(pc[0]) |
| 157 | fullName := f.Name() |
| 158 | |
| 159 | lastDotIndex := strings.LastIndex(fullName, ".") |
| 160 | if lastDotIndex == -1 { |
| 161 | panic("unable to distinguish function name from package") |
| 162 | } |
| 163 | |
| 164 | if fullName[lastDotIndex-1] == ')' { |
| 165 | // The caller is a method on some type, so it's name looks like |
| 166 | // "pkg/path.(type).method". We need to go back one dot farther to get |
| 167 | // to the package name. |
| 168 | lastDotIndex = strings.LastIndex(fullName[:lastDotIndex], ".") |
| 169 | } |
| 170 | |
| 171 | pkgPath = fullName[:lastDotIndex] |
| 172 | funcName = fullName[lastDotIndex+1:] |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 173 | ok = true |
Jamie Gennis | 0ed63ef | 2014-06-30 18:07:17 -0700 | [diff] [blame] | 174 | return |
| 175 | } |
| 176 | |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 177 | // pkgPathToName makes a Ninja-friendly name out of a Go package name by |
| 178 | // replaceing all the '/' characters with '.'. We assume the results are |
| 179 | // unique, though this is not 100% guaranteed for Go package names that |
| 180 | // already contain '.' characters. Disallowing package names with '.' isn't |
| 181 | // reasonable since many package names contain the name of the hosting site |
| 182 | // (e.g. "code.google.com"). In practice this probably isn't really a |
| 183 | // problem. |
| 184 | func pkgPathToName(pkgPath string) string { |
| 185 | return strings.Replace(pkgPath, "/", ".", -1) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 186 | } |
| 187 | |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 188 | // Import enables access to the exported Ninja pools, rules, and variables |
| 189 | // that are defined at the package scope of another Go package. Go's |
| 190 | // visibility rules apply to these references - capitalized names indicate |
| 191 | // that something is exported. It may only be called from a Go package's |
| 192 | // init() function. The Go package path passed to Import must have already |
| 193 | // been imported into the Go package using a Go import statement. The |
| 194 | // imported variables may then be accessed from Ninja strings as |
| 195 | // "${pkg.Variable}", while the imported rules can simply be accessed as |
| 196 | // exported Go variables from the package. For example: |
Jamie Gennis | 28a63fb | 2014-06-05 19:50:44 -0700 | [diff] [blame] | 197 | // |
| 198 | // import ( |
| 199 | // "blueprint" |
| 200 | // "foo/bar" |
| 201 | // ) |
| 202 | // |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 203 | // var pctx = NewPackagePath("blah") |
| 204 | // |
Jamie Gennis | 28a63fb | 2014-06-05 19:50:44 -0700 | [diff] [blame] | 205 | // func init() { |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 206 | // pctx.Import("foo/bar") |
Jamie Gennis | 28a63fb | 2014-06-05 19:50:44 -0700 | [diff] [blame] | 207 | // } |
| 208 | // |
| 209 | // ... |
| 210 | // |
| 211 | // func (m *MyModule) GenerateBuildActions(ctx blueprint.Module) { |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 212 | // ctx.Build(pctx, blueprint.BuildParams{ |
Jamie Gennis | 28a63fb | 2014-06-05 19:50:44 -0700 | [diff] [blame] | 213 | // Rule: bar.SomeRule, |
| 214 | // Outputs: []string{"${bar.SomeVariable}"}, |
| 215 | // }) |
| 216 | // } |
Jamie Gennis | d4e1018 | 2014-06-12 20:06:50 -0700 | [diff] [blame] | 217 | // |
| 218 | // Note that the local name used to refer to the package in Ninja variable names |
| 219 | // is derived from pkgPath by extracting the last path component. This differs |
| 220 | // from Go's import declaration, which derives the local name from the package |
| 221 | // clause in the imported package. By convention these names are made to match, |
| 222 | // but this is not required. |
Dan Willemsen | aeffbf7 | 2015-11-25 15:29:32 -0800 | [diff] [blame] | 223 | func (p *packageContext) Import(pkgPath string) { |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 224 | checkCalledFromInit() |
| 225 | importPkg, ok := packageContexts[pkgPath] |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 226 | if !ok { |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 227 | panic(fmt.Errorf("package %q has no context", pkgPath)) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 228 | } |
| 229 | |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 230 | err := p.scope.AddImport(importPkg.shortName, importPkg.scope) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 231 | if err != nil { |
| 232 | panic(err) |
| 233 | } |
| 234 | } |
| 235 | |
Jamie Gennis | d4e1018 | 2014-06-12 20:06:50 -0700 | [diff] [blame] | 236 | // ImportAs provides the same functionality as Import, but it allows the local |
| 237 | // name that will be used to refer to the package to be specified explicitly. |
| 238 | // It may only be called from a Go package's init() function. |
Dan Willemsen | aeffbf7 | 2015-11-25 15:29:32 -0800 | [diff] [blame] | 239 | func (p *packageContext) ImportAs(as, pkgPath string) { |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 240 | checkCalledFromInit() |
| 241 | importPkg, ok := packageContexts[pkgPath] |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 242 | if !ok { |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 243 | panic(fmt.Errorf("package %q has no context", pkgPath)) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | err := validateNinjaName(as) |
| 247 | if err != nil { |
| 248 | panic(err) |
| 249 | } |
| 250 | |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 251 | err = p.scope.AddImport(as, importPkg.scope) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 252 | if err != nil { |
| 253 | panic(err) |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | type staticVariable struct { |
Dan Willemsen | aeffbf7 | 2015-11-25 15:29:32 -0800 | [diff] [blame] | 258 | pctx *packageContext |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 259 | name_ string |
| 260 | value_ string |
| 261 | } |
| 262 | |
Jamie Gennis | d4e1018 | 2014-06-12 20:06:50 -0700 | [diff] [blame] | 263 | // StaticVariable returns a Variable whose value does not depend on any |
| 264 | // configuration information. It may only be called during a Go package's |
| 265 | // initialization - either from the init() function or as part of a package- |
| 266 | // scoped variable's initialization. |
| 267 | // |
| 268 | // This function is usually used to initialize a package-scoped Go variable that |
| 269 | // represents a Ninja variable that will be output. The name argument should |
| 270 | // exactly match the Go variable name, and the value string may reference other |
| 271 | // Ninja variables that are visible within the calling Go package. |
Dan Willemsen | aeffbf7 | 2015-11-25 15:29:32 -0800 | [diff] [blame] | 272 | func (p *packageContext) StaticVariable(name, value string) Variable { |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 273 | checkCalledFromInit() |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 274 | err := validateNinjaName(name) |
| 275 | if err != nil { |
| 276 | panic(err) |
| 277 | } |
| 278 | |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 279 | v := &staticVariable{p, name, value} |
| 280 | err = p.scope.AddVariable(v) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 281 | if err != nil { |
| 282 | panic(err) |
| 283 | } |
| 284 | |
| 285 | return v |
| 286 | } |
| 287 | |
Dan Willemsen | aeffbf7 | 2015-11-25 15:29:32 -0800 | [diff] [blame] | 288 | func (v *staticVariable) packageContext() *packageContext { |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 289 | return v.pctx |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | func (v *staticVariable) name() string { |
| 293 | return v.name_ |
| 294 | } |
| 295 | |
Dan Willemsen | aeffbf7 | 2015-11-25 15:29:32 -0800 | [diff] [blame] | 296 | func (v *staticVariable) fullName(pkgNames map[*packageContext]string) string { |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 297 | return packageNamespacePrefix(pkgNames[v.pctx]) + v.name_ |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 298 | } |
| 299 | |
Jamie Gennis | 6eb4d24 | 2014-06-11 18:31:16 -0700 | [diff] [blame] | 300 | func (v *staticVariable) value(interface{}) (*ninjaString, error) { |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 301 | ninjaStr, err := parseNinjaString(v.pctx.scope, v.value_) |
Jamie Gennis | 71bd58a | 2014-06-13 18:19:16 -0700 | [diff] [blame] | 302 | if err != nil { |
Jamie Gennis | 48aed8c | 2014-06-13 18:25:54 -0700 | [diff] [blame] | 303 | err = fmt.Errorf("error parsing variable %s value: %s", v, err) |
Jamie Gennis | 71bd58a | 2014-06-13 18:19:16 -0700 | [diff] [blame] | 304 | panic(err) |
| 305 | } |
| 306 | return ninjaStr, nil |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 307 | } |
| 308 | |
Jamie Gennis | 48aed8c | 2014-06-13 18:25:54 -0700 | [diff] [blame] | 309 | func (v *staticVariable) String() string { |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 310 | return v.pctx.pkgPath + "." + v.name_ |
Jamie Gennis | 48aed8c | 2014-06-13 18:25:54 -0700 | [diff] [blame] | 311 | } |
| 312 | |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 313 | type variableFunc struct { |
Dan Willemsen | aeffbf7 | 2015-11-25 15:29:32 -0800 | [diff] [blame] | 314 | pctx *packageContext |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 315 | name_ string |
Jamie Gennis | 6eb4d24 | 2014-06-11 18:31:16 -0700 | [diff] [blame] | 316 | value_ func(interface{}) (string, error) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | // VariableFunc returns a Variable whose value is determined by a function that |
Jamie Gennis | d4e1018 | 2014-06-12 20:06:50 -0700 | [diff] [blame] | 320 | // takes a config object as input and returns either the variable value or an |
| 321 | // error. It may only be called during a Go package's initialization - either |
| 322 | // from the init() function or as part of a package-scoped variable's |
| 323 | // initialization. |
| 324 | // |
| 325 | // This function is usually used to initialize a package-scoped Go variable that |
| 326 | // represents a Ninja variable that will be output. The name argument should |
| 327 | // exactly match the Go variable name, and the value string returned by f may |
| 328 | // reference other Ninja variables that are visible within the calling Go |
| 329 | // package. |
Dan Willemsen | aeffbf7 | 2015-11-25 15:29:32 -0800 | [diff] [blame] | 330 | func (p *packageContext) VariableFunc(name string, |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 331 | f func(config interface{}) (string, error)) Variable { |
| 332 | |
| 333 | checkCalledFromInit() |
Jamie Gennis | d4e1018 | 2014-06-12 20:06:50 -0700 | [diff] [blame] | 334 | |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 335 | err := validateNinjaName(name) |
| 336 | if err != nil { |
| 337 | panic(err) |
| 338 | } |
| 339 | |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 340 | v := &variableFunc{p, name, f} |
| 341 | err = p.scope.AddVariable(v) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 342 | if err != nil { |
| 343 | panic(err) |
| 344 | } |
| 345 | |
| 346 | return v |
| 347 | } |
| 348 | |
Jamie Gennis | 28a63fb | 2014-06-05 19:50:44 -0700 | [diff] [blame] | 349 | // VariableConfigMethod returns a Variable whose value is determined by calling |
Jamie Gennis | d4e1018 | 2014-06-12 20:06:50 -0700 | [diff] [blame] | 350 | // a method on the config object. The method must take no arguments and return |
| 351 | // a single string that will be the variable's value. It may only be called |
| 352 | // during a Go package's initialization - either from the init() function or as |
| 353 | // part of a package-scoped variable's initialization. |
| 354 | // |
| 355 | // This function is usually used to initialize a package-scoped Go variable that |
| 356 | // represents a Ninja variable that will be output. The name argument should |
| 357 | // exactly match the Go variable name, and the value string returned by method |
| 358 | // may reference other Ninja variables that are visible within the calling Go |
| 359 | // package. |
Dan Willemsen | aeffbf7 | 2015-11-25 15:29:32 -0800 | [diff] [blame] | 360 | func (p *packageContext) VariableConfigMethod(name string, |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 361 | method interface{}) Variable { |
| 362 | |
| 363 | checkCalledFromInit() |
| 364 | |
Jamie Gennis | 28a63fb | 2014-06-05 19:50:44 -0700 | [diff] [blame] | 365 | err := validateNinjaName(name) |
| 366 | if err != nil { |
| 367 | panic(err) |
| 368 | } |
| 369 | |
Jamie Gennis | 28a63fb | 2014-06-05 19:50:44 -0700 | [diff] [blame] | 370 | methodValue := reflect.ValueOf(method) |
| 371 | validateVariableMethod(name, methodValue) |
| 372 | |
Jamie Gennis | 6eb4d24 | 2014-06-11 18:31:16 -0700 | [diff] [blame] | 373 | fun := func(config interface{}) (string, error) { |
Jamie Gennis | 28a63fb | 2014-06-05 19:50:44 -0700 | [diff] [blame] | 374 | result := methodValue.Call([]reflect.Value{reflect.ValueOf(config)}) |
| 375 | resultStr := result[0].Interface().(string) |
| 376 | return resultStr, nil |
| 377 | } |
| 378 | |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 379 | v := &variableFunc{p, name, fun} |
| 380 | err = p.scope.AddVariable(v) |
Jamie Gennis | 28a63fb | 2014-06-05 19:50:44 -0700 | [diff] [blame] | 381 | if err != nil { |
| 382 | panic(err) |
| 383 | } |
| 384 | |
| 385 | return v |
| 386 | } |
| 387 | |
Dan Willemsen | aeffbf7 | 2015-11-25 15:29:32 -0800 | [diff] [blame] | 388 | func (v *variableFunc) packageContext() *packageContext { |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 389 | return v.pctx |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | func (v *variableFunc) name() string { |
| 393 | return v.name_ |
| 394 | } |
| 395 | |
Dan Willemsen | aeffbf7 | 2015-11-25 15:29:32 -0800 | [diff] [blame] | 396 | func (v *variableFunc) fullName(pkgNames map[*packageContext]string) string { |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 397 | return packageNamespacePrefix(pkgNames[v.pctx]) + v.name_ |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 398 | } |
| 399 | |
Jamie Gennis | 6eb4d24 | 2014-06-11 18:31:16 -0700 | [diff] [blame] | 400 | func (v *variableFunc) value(config interface{}) (*ninjaString, error) { |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 401 | value, err := v.value_(config) |
| 402 | if err != nil { |
| 403 | return nil, err |
| 404 | } |
Jamie Gennis | 71bd58a | 2014-06-13 18:19:16 -0700 | [diff] [blame] | 405 | |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 406 | ninjaStr, err := parseNinjaString(v.pctx.scope, value) |
Jamie Gennis | 71bd58a | 2014-06-13 18:19:16 -0700 | [diff] [blame] | 407 | if err != nil { |
Jamie Gennis | 48aed8c | 2014-06-13 18:25:54 -0700 | [diff] [blame] | 408 | err = fmt.Errorf("error parsing variable %s value: %s", v, err) |
Jamie Gennis | 71bd58a | 2014-06-13 18:19:16 -0700 | [diff] [blame] | 409 | panic(err) |
| 410 | } |
| 411 | |
| 412 | return ninjaStr, nil |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 413 | } |
| 414 | |
Jamie Gennis | 48aed8c | 2014-06-13 18:25:54 -0700 | [diff] [blame] | 415 | func (v *variableFunc) String() string { |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 416 | return v.pctx.pkgPath + "." + v.name_ |
Jamie Gennis | 48aed8c | 2014-06-13 18:25:54 -0700 | [diff] [blame] | 417 | } |
| 418 | |
Jamie Gennis | 28a63fb | 2014-06-05 19:50:44 -0700 | [diff] [blame] | 419 | func validateVariableMethod(name string, methodValue reflect.Value) { |
| 420 | methodType := methodValue.Type() |
| 421 | if methodType.Kind() != reflect.Func { |
| 422 | panic(fmt.Errorf("method given for variable %s is not a function", |
| 423 | name)) |
| 424 | } |
| 425 | if n := methodType.NumIn(); n != 1 { |
| 426 | panic(fmt.Errorf("method for variable %s has %d inputs (should be 1)", |
| 427 | name, n)) |
| 428 | } |
| 429 | if n := methodType.NumOut(); n != 1 { |
| 430 | panic(fmt.Errorf("method for variable %s has %d outputs (should be 1)", |
| 431 | name, n)) |
| 432 | } |
| 433 | if kind := methodType.Out(0).Kind(); kind != reflect.String { |
| 434 | panic(fmt.Errorf("method for variable %s does not return a string", |
| 435 | name)) |
| 436 | } |
| 437 | } |
| 438 | |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 439 | // An argVariable is a Variable that exists only when it is set by a build |
| 440 | // statement to pass a value to the rule being invoked. It has no value, so it |
| 441 | // can never be used to create a Ninja assignment statement. It is inserted |
| 442 | // into the rule's scope, which is used for name lookups within the rule and |
| 443 | // when assigning argument values as part of a build statement. |
| 444 | type argVariable struct { |
| 445 | name_ string |
| 446 | } |
| 447 | |
Dan Willemsen | aeffbf7 | 2015-11-25 15:29:32 -0800 | [diff] [blame] | 448 | func (v *argVariable) packageContext() *packageContext { |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 449 | panic("this should not be called") |
| 450 | } |
| 451 | |
| 452 | func (v *argVariable) name() string { |
| 453 | return v.name_ |
| 454 | } |
| 455 | |
Dan Willemsen | aeffbf7 | 2015-11-25 15:29:32 -0800 | [diff] [blame] | 456 | func (v *argVariable) fullName(pkgNames map[*packageContext]string) string { |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 457 | return v.name_ |
| 458 | } |
| 459 | |
Jamie Gennis | 6eb4d24 | 2014-06-11 18:31:16 -0700 | [diff] [blame] | 460 | func (v *argVariable) value(config interface{}) (*ninjaString, error) { |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 461 | return nil, errVariableIsArg |
| 462 | } |
| 463 | |
Jamie Gennis | 48aed8c | 2014-06-13 18:25:54 -0700 | [diff] [blame] | 464 | func (v *argVariable) String() string { |
| 465 | return "<arg>:" + v.name_ |
| 466 | } |
| 467 | |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 468 | type staticPool struct { |
Dan Willemsen | aeffbf7 | 2015-11-25 15:29:32 -0800 | [diff] [blame] | 469 | pctx *packageContext |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 470 | name_ string |
| 471 | params PoolParams |
| 472 | } |
| 473 | |
Jamie Gennis | d4e1018 | 2014-06-12 20:06:50 -0700 | [diff] [blame] | 474 | // StaticPool returns a Pool whose value does not depend on any configuration |
| 475 | // information. It may only be called during a Go package's initialization - |
| 476 | // either from the init() function or as part of a package-scoped Go variable's |
| 477 | // initialization. |
| 478 | // |
| 479 | // This function is usually used to initialize a package-scoped Go variable that |
| 480 | // represents a Ninja pool that will be output. The name argument should |
| 481 | // exactly match the Go variable name, and the params fields may reference other |
| 482 | // Ninja variables that are visible within the calling Go package. |
Dan Willemsen | aeffbf7 | 2015-11-25 15:29:32 -0800 | [diff] [blame] | 483 | func (p *packageContext) StaticPool(name string, params PoolParams) Pool { |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 484 | checkCalledFromInit() |
| 485 | |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 486 | err := validateNinjaName(name) |
| 487 | if err != nil { |
| 488 | panic(err) |
| 489 | } |
| 490 | |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 491 | pool := &staticPool{p, name, params} |
| 492 | err = p.scope.AddPool(pool) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 493 | if err != nil { |
| 494 | panic(err) |
| 495 | } |
| 496 | |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 497 | return pool |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 498 | } |
| 499 | |
Dan Willemsen | aeffbf7 | 2015-11-25 15:29:32 -0800 | [diff] [blame] | 500 | func (p *staticPool) packageContext() *packageContext { |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 501 | return p.pctx |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | func (p *staticPool) name() string { |
| 505 | return p.name_ |
| 506 | } |
| 507 | |
Dan Willemsen | aeffbf7 | 2015-11-25 15:29:32 -0800 | [diff] [blame] | 508 | func (p *staticPool) fullName(pkgNames map[*packageContext]string) string { |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 509 | return packageNamespacePrefix(pkgNames[p.pctx]) + p.name_ |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 510 | } |
| 511 | |
Jamie Gennis | 6eb4d24 | 2014-06-11 18:31:16 -0700 | [diff] [blame] | 512 | func (p *staticPool) def(config interface{}) (*poolDef, error) { |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 513 | def, err := parsePoolParams(p.pctx.scope, &p.params) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 514 | if err != nil { |
Jamie Gennis | 48aed8c | 2014-06-13 18:25:54 -0700 | [diff] [blame] | 515 | panic(fmt.Errorf("error parsing PoolParams for %s: %s", p, err)) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 516 | } |
| 517 | return def, nil |
| 518 | } |
| 519 | |
Jamie Gennis | 48aed8c | 2014-06-13 18:25:54 -0700 | [diff] [blame] | 520 | func (p *staticPool) String() string { |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 521 | return p.pctx.pkgPath + "." + p.name_ |
Jamie Gennis | 48aed8c | 2014-06-13 18:25:54 -0700 | [diff] [blame] | 522 | } |
| 523 | |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 524 | type poolFunc struct { |
Dan Willemsen | aeffbf7 | 2015-11-25 15:29:32 -0800 | [diff] [blame] | 525 | pctx *packageContext |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 526 | name_ string |
Jamie Gennis | 6eb4d24 | 2014-06-11 18:31:16 -0700 | [diff] [blame] | 527 | paramsFunc func(interface{}) (PoolParams, error) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 528 | } |
| 529 | |
Jamie Gennis | d4e1018 | 2014-06-12 20:06:50 -0700 | [diff] [blame] | 530 | // PoolFunc returns a Pool whose value is determined by a function that takes a |
| 531 | // config object as input and returns either the pool parameters or an error. It |
| 532 | // may only be called during a Go package's initialization - either from the |
| 533 | // init() function or as part of a package-scoped variable's initialization. |
| 534 | // |
| 535 | // This function is usually used to initialize a package-scoped Go variable that |
| 536 | // represents a Ninja pool that will be output. The name argument should |
| 537 | // exactly match the Go variable name, and the string fields of the PoolParams |
| 538 | // returned by f may reference other Ninja variables that are visible within the |
| 539 | // calling Go package. |
Dan Willemsen | aeffbf7 | 2015-11-25 15:29:32 -0800 | [diff] [blame] | 540 | func (p *packageContext) PoolFunc(name string, f func(interface{}) (PoolParams, |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 541 | error)) Pool { |
| 542 | |
| 543 | checkCalledFromInit() |
| 544 | |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 545 | err := validateNinjaName(name) |
| 546 | if err != nil { |
| 547 | panic(err) |
| 548 | } |
| 549 | |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 550 | pool := &poolFunc{p, name, f} |
| 551 | err = p.scope.AddPool(pool) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 552 | if err != nil { |
| 553 | panic(err) |
| 554 | } |
| 555 | |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 556 | return pool |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 557 | } |
| 558 | |
Dan Willemsen | aeffbf7 | 2015-11-25 15:29:32 -0800 | [diff] [blame] | 559 | func (p *poolFunc) packageContext() *packageContext { |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 560 | return p.pctx |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 561 | } |
| 562 | |
| 563 | func (p *poolFunc) name() string { |
| 564 | return p.name_ |
| 565 | } |
| 566 | |
Dan Willemsen | aeffbf7 | 2015-11-25 15:29:32 -0800 | [diff] [blame] | 567 | func (p *poolFunc) fullName(pkgNames map[*packageContext]string) string { |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 568 | return packageNamespacePrefix(pkgNames[p.pctx]) + p.name_ |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 569 | } |
| 570 | |
Jamie Gennis | 6eb4d24 | 2014-06-11 18:31:16 -0700 | [diff] [blame] | 571 | func (p *poolFunc) def(config interface{}) (*poolDef, error) { |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 572 | params, err := p.paramsFunc(config) |
| 573 | if err != nil { |
| 574 | return nil, err |
| 575 | } |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 576 | def, err := parsePoolParams(p.pctx.scope, ¶ms) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 577 | if err != nil { |
Jamie Gennis | 48aed8c | 2014-06-13 18:25:54 -0700 | [diff] [blame] | 578 | panic(fmt.Errorf("error parsing PoolParams for %s: %s", p, err)) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 579 | } |
| 580 | return def, nil |
| 581 | } |
| 582 | |
Jamie Gennis | 48aed8c | 2014-06-13 18:25:54 -0700 | [diff] [blame] | 583 | func (p *poolFunc) String() string { |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 584 | return p.pctx.pkgPath + "." + p.name_ |
Jamie Gennis | 48aed8c | 2014-06-13 18:25:54 -0700 | [diff] [blame] | 585 | } |
| 586 | |
Ken Oslund | 4b9a051 | 2015-04-14 16:26:58 -0700 | [diff] [blame] | 587 | type builtinPool struct { |
Colin Cross | 63d5d4d | 2015-04-20 16:41:55 -0700 | [diff] [blame] | 588 | name_ string |
Ken Oslund | 4b9a051 | 2015-04-14 16:26:58 -0700 | [diff] [blame] | 589 | } |
| 590 | |
Dan Willemsen | aeffbf7 | 2015-11-25 15:29:32 -0800 | [diff] [blame] | 591 | func (p *builtinPool) packageContext() *packageContext { |
Ken Oslund | 4b9a051 | 2015-04-14 16:26:58 -0700 | [diff] [blame] | 592 | return nil |
| 593 | } |
| 594 | |
| 595 | func (p *builtinPool) name() string { |
| 596 | return p.name_ |
| 597 | } |
| 598 | |
Dan Willemsen | aeffbf7 | 2015-11-25 15:29:32 -0800 | [diff] [blame] | 599 | func (p *builtinPool) fullName(pkgNames map[*packageContext]string) string { |
Ken Oslund | 4b9a051 | 2015-04-14 16:26:58 -0700 | [diff] [blame] | 600 | return p.name_ |
| 601 | } |
| 602 | |
| 603 | func (p *builtinPool) def(config interface{}) (*poolDef, error) { |
| 604 | return nil, errPoolIsBuiltin |
| 605 | } |
| 606 | |
| 607 | func (p *builtinPool) String() string { |
| 608 | return "<builtin>:" + p.name_ |
| 609 | } |
| 610 | |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 611 | type staticRule struct { |
Dan Willemsen | aeffbf7 | 2015-11-25 15:29:32 -0800 | [diff] [blame] | 612 | pctx *packageContext |
Colin Cross | 691a60d | 2015-01-07 18:08:56 -0800 | [diff] [blame] | 613 | name_ string |
| 614 | params RuleParams |
| 615 | argNames map[string]bool |
| 616 | scope_ *basicScope |
| 617 | sync.Mutex // protects scope_ during lazy creation |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 618 | } |
| 619 | |
Jamie Gennis | d4e1018 | 2014-06-12 20:06:50 -0700 | [diff] [blame] | 620 | // StaticRule returns a Rule whose value does not depend on any configuration |
| 621 | // information. It may only be called during a Go package's initialization - |
| 622 | // either from the init() function or as part of a package-scoped Go variable's |
| 623 | // initialization. |
| 624 | // |
| 625 | // This function is usually used to initialize a package-scoped Go variable that |
| 626 | // represents a Ninja rule that will be output. The name argument should |
| 627 | // exactly match the Go variable name, and the params fields may reference other |
| 628 | // Ninja variables that are visible within the calling Go package. |
| 629 | // |
| 630 | // The argNames arguments list Ninja variables that may be overridden by Ninja |
| 631 | // build statements that invoke the rule. These arguments may be referenced in |
| 632 | // any of the string fields of params. Arguments can shadow package-scoped |
| 633 | // variables defined within the caller's Go package, but they may not shadow |
| 634 | // those defined in another package. Shadowing a package-scoped variable |
| 635 | // results in the package-scoped variable's value being used for build |
| 636 | // statements that do not override the argument. For argument names that do not |
| 637 | // shadow package-scoped variables the default value is an empty string. |
Dan Willemsen | aeffbf7 | 2015-11-25 15:29:32 -0800 | [diff] [blame] | 638 | func (p *packageContext) StaticRule(name string, params RuleParams, |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 639 | argNames ...string) Rule { |
| 640 | |
| 641 | checkCalledFromInit() |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 642 | |
| 643 | err := validateNinjaName(name) |
| 644 | if err != nil { |
| 645 | panic(err) |
| 646 | } |
| 647 | |
| 648 | err = validateArgNames(argNames) |
| 649 | if err != nil { |
| 650 | panic(fmt.Errorf("invalid argument name: %s", err)) |
| 651 | } |
| 652 | |
| 653 | argNamesSet := make(map[string]bool) |
| 654 | for _, argName := range argNames { |
| 655 | argNamesSet[argName] = true |
| 656 | } |
| 657 | |
Jamie Gennis | 48aed8c | 2014-06-13 18:25:54 -0700 | [diff] [blame] | 658 | ruleScope := (*basicScope)(nil) // This will get created lazily |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 659 | |
Colin Cross | 691a60d | 2015-01-07 18:08:56 -0800 | [diff] [blame] | 660 | r := &staticRule{ |
| 661 | pctx: p, |
| 662 | name_: name, |
| 663 | params: params, |
| 664 | argNames: argNamesSet, |
| 665 | scope_: ruleScope, |
| 666 | } |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 667 | err = p.scope.AddRule(r) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 668 | if err != nil { |
| 669 | panic(err) |
| 670 | } |
| 671 | |
| 672 | return r |
| 673 | } |
| 674 | |
Dan Willemsen | aeffbf7 | 2015-11-25 15:29:32 -0800 | [diff] [blame] | 675 | func (r *staticRule) packageContext() *packageContext { |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 676 | return r.pctx |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 677 | } |
| 678 | |
| 679 | func (r *staticRule) name() string { |
| 680 | return r.name_ |
| 681 | } |
| 682 | |
Dan Willemsen | aeffbf7 | 2015-11-25 15:29:32 -0800 | [diff] [blame] | 683 | func (r *staticRule) fullName(pkgNames map[*packageContext]string) string { |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 684 | return packageNamespacePrefix(pkgNames[r.pctx]) + r.name_ |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 685 | } |
| 686 | |
Jamie Gennis | 6eb4d24 | 2014-06-11 18:31:16 -0700 | [diff] [blame] | 687 | func (r *staticRule) def(interface{}) (*ruleDef, error) { |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 688 | def, err := parseRuleParams(r.scope(), &r.params) |
| 689 | if err != nil { |
Jamie Gennis | 48aed8c | 2014-06-13 18:25:54 -0700 | [diff] [blame] | 690 | panic(fmt.Errorf("error parsing RuleParams for %s: %s", r, err)) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 691 | } |
| 692 | return def, nil |
| 693 | } |
| 694 | |
Jamie Gennis | 48aed8c | 2014-06-13 18:25:54 -0700 | [diff] [blame] | 695 | func (r *staticRule) scope() *basicScope { |
| 696 | // We lazily create the scope so that all the package-scoped variables get |
| 697 | // declared before the args are created. Otherwise we could incorrectly |
| 698 | // shadow a package-scoped variable with an arg variable. |
Colin Cross | 691a60d | 2015-01-07 18:08:56 -0800 | [diff] [blame] | 699 | r.Lock() |
| 700 | defer r.Unlock() |
| 701 | |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 702 | if r.scope_ == nil { |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 703 | r.scope_ = makeRuleScope(r.pctx.scope, r.argNames) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 704 | } |
| 705 | return r.scope_ |
| 706 | } |
| 707 | |
| 708 | func (r *staticRule) isArg(argName string) bool { |
| 709 | return r.argNames[argName] |
| 710 | } |
| 711 | |
Jamie Gennis | 48aed8c | 2014-06-13 18:25:54 -0700 | [diff] [blame] | 712 | func (r *staticRule) String() string { |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 713 | return r.pctx.pkgPath + "." + r.name_ |
Jamie Gennis | 48aed8c | 2014-06-13 18:25:54 -0700 | [diff] [blame] | 714 | } |
| 715 | |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 716 | type ruleFunc struct { |
Dan Willemsen | aeffbf7 | 2015-11-25 15:29:32 -0800 | [diff] [blame] | 717 | pctx *packageContext |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 718 | name_ string |
Jamie Gennis | 6eb4d24 | 2014-06-11 18:31:16 -0700 | [diff] [blame] | 719 | paramsFunc func(interface{}) (RuleParams, error) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 720 | argNames map[string]bool |
Jamie Gennis | 48aed8c | 2014-06-13 18:25:54 -0700 | [diff] [blame] | 721 | scope_ *basicScope |
Colin Cross | 691a60d | 2015-01-07 18:08:56 -0800 | [diff] [blame] | 722 | sync.Mutex // protects scope_ during lazy creation |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 723 | } |
| 724 | |
Jamie Gennis | d4e1018 | 2014-06-12 20:06:50 -0700 | [diff] [blame] | 725 | // RuleFunc returns a Rule whose value is determined by a function that takes a |
| 726 | // config object as input and returns either the rule parameters or an error. It |
| 727 | // may only be called during a Go package's initialization - either from the |
| 728 | // init() function or as part of a package-scoped variable's initialization. |
| 729 | // |
| 730 | // This function is usually used to initialize a package-scoped Go variable that |
| 731 | // represents a Ninja rule that will be output. The name argument should |
| 732 | // exactly match the Go variable name, and the string fields of the RuleParams |
| 733 | // returned by f may reference other Ninja variables that are visible within the |
| 734 | // calling Go package. |
| 735 | // |
| 736 | // The argNames arguments list Ninja variables that may be overridden by Ninja |
| 737 | // build statements that invoke the rule. These arguments may be referenced in |
| 738 | // any of the string fields of the RuleParams returned by f. Arguments can |
| 739 | // shadow package-scoped variables defined within the caller's Go package, but |
| 740 | // they may not shadow those defined in another package. Shadowing a package- |
| 741 | // scoped variable results in the package-scoped variable's value being used for |
| 742 | // build statements that do not override the argument. For argument names that |
| 743 | // do not shadow package-scoped variables the default value is an empty string. |
Dan Willemsen | aeffbf7 | 2015-11-25 15:29:32 -0800 | [diff] [blame] | 744 | func (p *packageContext) RuleFunc(name string, f func(interface{}) (RuleParams, |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 745 | error), argNames ...string) Rule { |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 746 | |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 747 | checkCalledFromInit() |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 748 | |
| 749 | err := validateNinjaName(name) |
| 750 | if err != nil { |
| 751 | panic(err) |
| 752 | } |
| 753 | |
| 754 | err = validateArgNames(argNames) |
| 755 | if err != nil { |
| 756 | panic(fmt.Errorf("invalid argument name: %s", err)) |
| 757 | } |
| 758 | |
| 759 | argNamesSet := make(map[string]bool) |
| 760 | for _, argName := range argNames { |
| 761 | argNamesSet[argName] = true |
| 762 | } |
| 763 | |
Jamie Gennis | 48aed8c | 2014-06-13 18:25:54 -0700 | [diff] [blame] | 764 | ruleScope := (*basicScope)(nil) // This will get created lazily |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 765 | |
Colin Cross | 691a60d | 2015-01-07 18:08:56 -0800 | [diff] [blame] | 766 | rule := &ruleFunc{ |
| 767 | pctx: p, |
| 768 | name_: name, |
| 769 | paramsFunc: f, |
| 770 | argNames: argNamesSet, |
| 771 | scope_: ruleScope, |
| 772 | } |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 773 | err = p.scope.AddRule(rule) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 774 | if err != nil { |
| 775 | panic(err) |
| 776 | } |
| 777 | |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 778 | return rule |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 779 | } |
| 780 | |
Dan Willemsen | aeffbf7 | 2015-11-25 15:29:32 -0800 | [diff] [blame] | 781 | func (r *ruleFunc) packageContext() *packageContext { |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 782 | return r.pctx |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 783 | } |
| 784 | |
| 785 | func (r *ruleFunc) name() string { |
| 786 | return r.name_ |
| 787 | } |
| 788 | |
Dan Willemsen | aeffbf7 | 2015-11-25 15:29:32 -0800 | [diff] [blame] | 789 | func (r *ruleFunc) fullName(pkgNames map[*packageContext]string) string { |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 790 | return packageNamespacePrefix(pkgNames[r.pctx]) + r.name_ |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 791 | } |
| 792 | |
Jamie Gennis | 6eb4d24 | 2014-06-11 18:31:16 -0700 | [diff] [blame] | 793 | func (r *ruleFunc) def(config interface{}) (*ruleDef, error) { |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 794 | params, err := r.paramsFunc(config) |
| 795 | if err != nil { |
| 796 | return nil, err |
| 797 | } |
| 798 | def, err := parseRuleParams(r.scope(), ¶ms) |
| 799 | if err != nil { |
Jamie Gennis | 48aed8c | 2014-06-13 18:25:54 -0700 | [diff] [blame] | 800 | panic(fmt.Errorf("error parsing RuleParams for %s: %s", r, err)) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 801 | } |
| 802 | return def, nil |
| 803 | } |
| 804 | |
Jamie Gennis | 48aed8c | 2014-06-13 18:25:54 -0700 | [diff] [blame] | 805 | func (r *ruleFunc) scope() *basicScope { |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 806 | // We lazily create the scope so that all the global variables get declared |
| 807 | // before the args are created. Otherwise we could incorrectly shadow a |
| 808 | // global variable with an arg variable. |
Colin Cross | 691a60d | 2015-01-07 18:08:56 -0800 | [diff] [blame] | 809 | r.Lock() |
| 810 | defer r.Unlock() |
| 811 | |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 812 | if r.scope_ == nil { |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 813 | r.scope_ = makeRuleScope(r.pctx.scope, r.argNames) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 814 | } |
| 815 | return r.scope_ |
| 816 | } |
| 817 | |
| 818 | func (r *ruleFunc) isArg(argName string) bool { |
| 819 | return r.argNames[argName] |
| 820 | } |
| 821 | |
Jamie Gennis | 48aed8c | 2014-06-13 18:25:54 -0700 | [diff] [blame] | 822 | func (r *ruleFunc) String() string { |
Jamie Gennis | 2fb2095 | 2014-10-03 02:49:58 -0700 | [diff] [blame] | 823 | return r.pctx.pkgPath + "." + r.name_ |
Jamie Gennis | 48aed8c | 2014-06-13 18:25:54 -0700 | [diff] [blame] | 824 | } |
| 825 | |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 826 | type builtinRule struct { |
Colin Cross | 691a60d | 2015-01-07 18:08:56 -0800 | [diff] [blame] | 827 | name_ string |
| 828 | scope_ *basicScope |
| 829 | sync.Mutex // protects scope_ during lazy creation |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 830 | } |
| 831 | |
Dan Willemsen | aeffbf7 | 2015-11-25 15:29:32 -0800 | [diff] [blame] | 832 | func (r *builtinRule) packageContext() *packageContext { |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 833 | return nil |
| 834 | } |
| 835 | |
| 836 | func (r *builtinRule) name() string { |
| 837 | return r.name_ |
| 838 | } |
| 839 | |
Dan Willemsen | aeffbf7 | 2015-11-25 15:29:32 -0800 | [diff] [blame] | 840 | func (r *builtinRule) fullName(pkgNames map[*packageContext]string) string { |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 841 | return r.name_ |
| 842 | } |
| 843 | |
Jamie Gennis | 6eb4d24 | 2014-06-11 18:31:16 -0700 | [diff] [blame] | 844 | func (r *builtinRule) def(config interface{}) (*ruleDef, error) { |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 845 | return nil, errRuleIsBuiltin |
| 846 | } |
| 847 | |
Jamie Gennis | 48aed8c | 2014-06-13 18:25:54 -0700 | [diff] [blame] | 848 | func (r *builtinRule) scope() *basicScope { |
Colin Cross | 691a60d | 2015-01-07 18:08:56 -0800 | [diff] [blame] | 849 | r.Lock() |
| 850 | defer r.Unlock() |
| 851 | |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 852 | if r.scope_ == nil { |
| 853 | r.scope_ = makeRuleScope(nil, nil) |
| 854 | } |
| 855 | return r.scope_ |
| 856 | } |
| 857 | |
| 858 | func (r *builtinRule) isArg(argName string) bool { |
| 859 | return false |
| 860 | } |
| 861 | |
Jamie Gennis | 48aed8c | 2014-06-13 18:25:54 -0700 | [diff] [blame] | 862 | func (r *builtinRule) String() string { |
| 863 | return "<builtin>:" + r.name_ |
| 864 | } |