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