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

import "netbsd.org/pkglint/textproc"

func CheckdirCategory(dir CurrPath) {
	if trace.Tracing {
		defer trace.Call(dir)()
	}

	mklines := LoadMk(dir.JoinNoClean("Makefile"), NotEmpty|LogErrors) // TODO: Remove the "./" here already
	if mklines == nil {
		return
	}

	mklines.Check()

	mlex := NewMkLinesLexer(mklines)
	for mlex.SkipPrefix("#") {
	}
	mlex.SkipEmptyOrNote()

	if mlex.SkipIf(func(mkline *MkLine) bool { return mkline.IsVarassign() && mkline.Varname() == "COMMENT" }) {
		mkline := mlex.PreviousMkLine()

		valid := textproc.NewByteSet("--- '(),/0-9A-Za-z")
		invalid := invalidCharacters(mkline.Value(), valid)
		if invalid != "" {
			mkline.Warnf("%s contains invalid characters (%s).",
				mkline.Varname(), invalid)
		}

	} else {
		mlex.CurrentLine().Errorf("COMMENT= line expected.")
	}
	mlex.SkipEmptyOrNote()

	type subdir struct {
		name RelPath
		line *MkLine
	}

	// 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(dir)
	var mSubdirs []subdir

	seen := make(map[string]*MkLine)
	for !mlex.EOF() {
		mkline := mlex.CurrentMkLine()

		if mkline.IsVarassignMaybeCommented() && mkline.Varname() == "SUBDIR" {
			mlex.Skip()

			name := mkline.Value() // TODO: Maybe NewPath here already
			if mkline.IsCommentedVarassign() && !mkline.HasComment() {
				mkline.Warnf("%q commented out without giving a reason.", name)
			}

			if prev := seen[name]; prev != nil {
				mkline.Errorf("%q must only appear once, already seen in %s.", name, mkline.RelMkLine(prev))
			}
			seen[name] = mkline

			if len(mSubdirs) > 0 {
				if prev := mSubdirs[len(mSubdirs)-1].name; name < prev.String() {
					mkline.Warnf("%q should come before %q.", name, prev)
				}
			}

			mSubdirs = append(mSubdirs, subdir{NewRelPathString(name), mkline})

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

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

	fRest := fSubdirs[:]
	mRest := mSubdirs[:]

	for len(mRest) > 0 || len(fRest) > 0 {

		if len(fRest) > 0 && (len(mRest) == 0 || fRest[0] < mRest[0].name) {
			fCurrent := fRest[0]
			if !mCheck[fCurrent] {
				var line *Line
				if len(mRest) > 0 {
					line = mRest[0].line.Line
				} else {
					line = mlex.CurrentLine()
				}

				fix := line.Autofix()
				fix.Errorf("%q exists in the file system but not in the Makefile.", fCurrent)
				fix.InsertBefore("SUBDIR+=\t" + fCurrent.String())
				fix.Apply()
			}
			fRest = fRest[1:]

		} else if len(fRest) == 0 || mRest[0].name < fRest[0] {
			if !fCheck[mRest[0].name] {
				fix := mRest[0].line.Autofix()
				fix.Errorf("%q exists in the Makefile but not in the file system.", mRest[0].name)
				fix.Delete()
				fix.Apply()
			}
			mRest = mRest[1:]

		} else {
			fRest = fRest[1:]
			mRest = mRest[1:]
		}
	}

	// the pkgsrc-wip category Makefile defines its own targets for
	// generating indexes and READMEs. Just skip them.
	if !G.Wip {
		mlex.SkipEmptyOrNote()
		mlex.SkipContainsOrWarn(".include \"../mk/misc/category.mk\"")
		if !mlex.EOF() {
			mlex.CurrentLine().Errorf("The file must end here.")
		}
	}

	mklines.SaveAutofixChanges()

	if G.Opts.Recursive {
		var recurseInto []CurrPath
		for _, msub := range mSubdirs {
			if !msub.line.IsCommentedVarassign() {
				recurseInto = append(recurseInto, dir.JoinNoClean(msub.name))
			}
		}
		G.Todo.PushFront(recurseInto...)
	}
}