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