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