summaryrefslogtreecommitdiff
path: root/pkgtools/pkglint/files/category.go
blob: 6f2fa691238f1a258774bd5ac59bf4441aaa049b (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
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
package main

import (
	"sort"
)

func CheckdirCategory() {
	if G.opts.Debug {
		defer tracecall1(G.CurrentDir)()
	}

	lines := LoadNonemptyLines(G.CurrentDir+"/Makefile", true)
	if lines == nil {
		return
	}

	mklines := NewMkLines(lines)
	mklines.Check()

	exp := NewExpecter(lines)
	for exp.AdvanceIfPrefix("#") {
	}
	exp.ExpectEmptyLine()

	if exp.AdvanceIfMatches(`^COMMENT=\t*(.*)`) {
		mklines.mklines[exp.index-1].CheckValidCharactersInValue(`[- '(),/0-9A-Za-z]`)
	} else {
		exp.CurrentLine().Errorf("COMMENT= line expected.")
	}
	exp.ExpectEmptyLine()

	type subdir struct {
		name   string
		line   *Line
		active bool
	}

	// And now to the most complicated part of the category Makefiles,
	// the (hopefully) sorted list of SUBDIRs. The first step is to
	// collect the SUBDIRs in the Makefile and in the file system.

	fSubdirs := getSubdirs(G.CurrentDir)
	sort.Sort(sort.StringSlice(fSubdirs))
	var mSubdirs []subdir

	prevSubdir := ""
	for !exp.EOF() {
		line := exp.CurrentLine()
		text := line.Text

		if m, commentFlag, indentation, name, comment := match4(text, `^(#?)SUBDIR\+=(\s*)(\S+)\s*(?:#\s*(.*?)\s*|)$`); m {
			commentedOut := commentFlag == "#"
			if commentedOut && comment == "" {
				line.Warnf("%q commented out without giving a reason.", name)
			}

			if indentation != "\t" {
				line.Warnf("Indentation should be a single tab character.")
			}

			if name == prevSubdir {
				line.Errorf("%q must only appear once.", name)
			} else if name < prevSubdir {
				line.Warnf("%q should come before %q.", name, prevSubdir)
			} else {
				// correctly ordered
			}

			mSubdirs = append(mSubdirs, subdir{name, line, !commentedOut})
			prevSubdir = name
			exp.Advance()

		} else {
			if line.Text != "" {
				line.Errorf("SUBDIR+= line or empty line expected.")
			}
			break
		}
	}

	// To prevent unnecessary warnings about subdirectories that are
	// in one list, but not in the other, we generate the sets of
	// subdirs of each list.
	fCheck := make(map[string]bool)
	mCheck := make(map[string]bool)
	for _, fsub := range fSubdirs {
		fCheck[fsub] = true
	}
	for _, msub := range mSubdirs {
		mCheck[msub.name] = true
	}

	fIndex, fAtend, fNeednext, fCurrent := 0, false, true, ""
	mIndex, mAtend, mNeednext, mCurrent := 0, false, true, ""

	var subdirs []string

	var line *Line
	mActive := false

	for !(mAtend && fAtend) {
		if !mAtend && mNeednext {
			mNeednext = false
			if mIndex >= len(mSubdirs) {
				mAtend = true
				line = exp.CurrentLine()
				continue
			} else {
				mCurrent = mSubdirs[mIndex].name
				line = mSubdirs[mIndex].line
				mActive = mSubdirs[mIndex].active
				mIndex++
			}
		}

		if !fAtend && fNeednext {
			fNeednext = false
			if fIndex >= len(fSubdirs) {
				fAtend = true
				continue
			} else {
				fCurrent = fSubdirs[fIndex]
				fIndex++
			}
		}

		if !fAtend && (mAtend || fCurrent < mCurrent) {
			if !mCheck[fCurrent] {
				if !line.AutofixInsertBefore("SUBDIR+=\t" + fCurrent) {
					line.Errorf("%q exists in the file system, but not in the Makefile.", fCurrent)
				}
			}
			fNeednext = true

		} else if !mAtend && (fAtend || mCurrent < fCurrent) {
			if !fCheck[mCurrent] {
				if !line.AutofixDelete() {
					line.Errorf("%q exists in the Makefile, but not in the file system.", mCurrent)
				}
			}
			mNeednext = true

		} else { // f_current == m_current
			fNeednext = true
			mNeednext = true
			if mActive {
				subdirs = append(subdirs, G.CurrentDir+"/"+mCurrent)
			}
		}
	}

	// the pkgsrc-wip category Makefile defines its own targets for
	// generating indexes and READMEs. Just skip them.
	if G.Wip {
		exp.index = len(exp.lines) - 2
	}

	exp.ExpectEmptyLine()
	exp.ExpectText(".include \"../mk/misc/category.mk\"")
	if !exp.EOF() {
		exp.CurrentLine().Errorf("The file should end here.")
	}

	SaveAutofixChanges(lines)

	if G.opts.Recursive {
		G.Todo = append(append([]string(nil), subdirs...), G.Todo...)
	}
}