summaryrefslogtreecommitdiff
path: root/pkgtools/pkglint/files/mkcontext.go
blob: 5385e7f615120b1597f14c67f0627756070bdf53 (plain)
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
package main

// MkContext contains data for the Makefile (or *.mk) that is currently checked.
type MkContext struct {
	forVars     map[string]bool  // The variables currently used in .for loops
	indentation []int            // Indentation depth of preprocessing directives
	target      string           // Current make(1) target
	vardef      map[string]*Line // varname => line; for all variables that are defined in the current file
	varuse      map[string]*Line // varname => line; for all variables that are used in the current file
	buildDefs   map[string]bool  // Variables that are registered in BUILD_DEFS, to ensure that all user-defined variables are added to it.
	plistVars   map[string]bool  // Variables that are registered in PLIST_VARS, to ensure that all user-defined variables are added to it.
	tools       map[string]bool  // Set of tools that are declared to be used.
}

func newMkContext() *MkContext {
	forVars := make(map[string]bool)
	indentation := make([]int, 1)
	vardef := make(map[string]*Line)
	varuse := make(map[string]*Line)
	buildDefs := make(map[string]bool)
	plistVars := make(map[string]bool)
	tools := make(map[string]bool)
	for tool := range G.globalData.predefinedTools {
		tools[tool] = true
	}
	return &MkContext{forVars, indentation, "", vardef, varuse, buildDefs, plistVars, tools}
}

func (ctx *MkContext) indentDepth() int {
	return ctx.indentation[len(ctx.indentation)-1]
}
func (ctx *MkContext) popIndent() {
	ctx.indentation = ctx.indentation[:len(ctx.indentation)-1]
}
func (ctx *MkContext) pushIndent(indent int) {
	ctx.indentation = append(ctx.indentation, indent)
}

func (ctx *MkContext) defineVar(line *Line, varname string) {
	if line.extra["value"] == nil {
		line.errorf("Internal pkglint error: novalue")
		return
	}
	if ctx.vardef[varname] == nil {
		ctx.vardef[varname] = line
	}
	varcanon := varnameCanon(varname)
	if ctx.vardef[varcanon] == nil {
		ctx.vardef[varcanon] = line
	}
}

func (ctx *MkContext) varValue(varname string) (value string, found bool) {
	if line := ctx.vardef[varname]; line != nil {
		if value := line.extra["value"]; value != nil {
			return value.(string), true
		} else {
			line.errorf("Internal pkglint error: novalue")
		}
	}
	return "", false
}