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

import (
	"regexp"
	"strings"
)

func checklinesBuildlink3Mk(lines []*Line) {
	defer tracecall("checklinesBuildlink3Mk", lines[0].fname)()

	ParselinesMk(lines)
	ChecklinesMk(lines)

	exp := NewExpecter(lines)

	for exp.advanceIfMatches(`^#`) != nil {
		if hasPrefix(exp.previousLine().text, "# XXX") {
			exp.previousLine().notef("Please read this comment and remove it if appropriate.")
		}
	}

	exp.expectEmptyLine()

	if exp.advanceIfMatches(`^BUILDLINK_DEPMETHOD\.(\S+)\?=.*$`) != nil {
		exp.previousLine().warnf("This line belongs inside the .ifdef block.")
		for exp.advanceIfMatches(`^$`) != nil {
		}
	}

	pkgbaseLine, pkgbase := (*Line)(nil), ""
	pkgidLine, pkgid := exp.currentLine(), ""
	abiLine, abiPkg, abiVersion := (*Line)(nil), "", ""
	apiLine, apiPkg, apiVersion := (*Line)(nil), "", ""

	// First paragraph: Introduction of the package identifier
	if m := exp.advanceIfMatches(`^BUILDLINK_TREE\+=\s*(\S+)$`); m != nil {
		pkgid = m[1]
	} else {
		exp.currentLine().warnf("Expected a BUILDLINK_TREE line.")
		return
	}
	exp.expectEmptyLine()

	// Second paragraph: multiple inclusion protection and introduction
	// of the uppercase package identifier.
	if m := exp.advanceIfMatches(`^\.if !defined\((\S+)_BUILDLINK3_MK\)$`); m != nil {
		pkgbaseLine = exp.previousLine()
		pkgbase = m[1]
	} else {
		return
	}
	if !exp.expectText(pkgbase + "_BUILDLINK3_MK:=") {
		exp.currentLine().errorf("Expected the multiple-inclusion guard.")
		return
	}
	exp.expectEmptyLine()

	ucPkgid := strings.ToUpper(strings.Replace(pkgid, "-", "_", -1))
	if ucPkgid != pkgbase {
		pkgbaseLine.errorf("Package name mismatch between %q ...", pkgbase)
		pkgidLine.errorf("... and %q.", pkgid)
	}
	if G.pkgContext != nil {
		if mkbase := G.pkgContext.effectivePkgbase; mkbase != "" && mkbase != pkgid {
			pkgidLine.errorf("Package name mismatch between %q ...", pkgid)
			G.pkgContext.effectivePkgnameLine.errorf("... and %q.", mkbase)
		}
	}

	// Third paragraph: Package information.
	indentLevel := 1 // The first .if is from the second paragraph.
	for {
		if exp.eof() {
			exp.currentLine().warnf("Expected .endif")
			return
		}

		line := exp.currentLine()

		if m := exp.advanceIfMatches(reVarassign); m != nil {
			varname, value := m[1], m[3]
			doCheck := false

			if varname == "BUILDLINK_ABI_DEPENDS."+pkgid {
				abiLine = line
				if m, p, v := match2(value, reDependencyCmp); m {
					abiPkg, abiVersion = p, v
				} else if m, p := match1(value, reDependencyWildcard); m {
					abiPkg, abiVersion = p, ""
				} else {
					_ = G.opts.DebugUnchecked && line.debugf("Unchecked dependency pattern %q.", value)
				}
				doCheck = true
			}
			if varname == "BUILDLINK_API_DEPENDS."+pkgid {
				apiLine = line
				if m, p, v := match2(value, reDependencyCmp); m {
					apiPkg, apiVersion = p, v
				} else if m, p := match1(value, reDependencyWildcard); m {
					apiPkg, apiVersion = p, ""
				} else {
					_ = G.opts.DebugUnchecked && line.debugf("Unchecked dependency pattern %q.", value)
				}
				doCheck = true
			}
			if doCheck && abiPkg != "" && apiPkg != "" && abiPkg != apiPkg {
				abiLine.warnf("Package name mismatch between %q ...", abiPkg)
				apiLine.warnf("... and %q.", apiPkg)
			}
			if doCheck && abiVersion != "" && apiVersion != "" && pkgverCmp(abiVersion, apiVersion) < 0 {
				abiLine.warnf("ABI version (%s) should be at least ...", abiVersion)
				apiLine.warnf("... API version (%s).", apiVersion)
			}

			if m, varparam := match1(varname, `^BUILDLINK_[\w_]+\.(.*)$`); m {
				if varparam != pkgid {
					line.warnf("Only buildlink variables for %q, not %q may be set in this file.", pkgid, varparam)
				}
			}

			if varname == "pkgbase" {
				exp.advanceIfMatches(`^\.\s*include "../../mk/pkg-build-options\.mk"$`)
			}

		} else if exp.advanceIfMatches(`^(?:#.*)?$`) != nil {
			// Comments and empty lines are fine here.

		} else if exp.advanceIfMatches(`^\.\s*include "\.\./\.\./([^/]+/[^/]+)/buildlink3\.mk"$`) != nil ||
			exp.advanceIfMatches(`^\.\s*include "\.\./\.\./mk/(\S+)\.buildlink3\.mk"$`) != nil {
			// TODO: Maybe check dependency lines.

		} else if exp.advanceIfMatches(`^\.if\s`) != nil {
			indentLevel++

		} else if exp.advanceIfMatches(`^\.endif.*$`) != nil {
			indentLevel--
			if indentLevel == 0 {
				break
			}

		} else {
			_ = G.opts.DebugUnchecked && exp.currentLine().warnf("Unchecked line in third paragraph.")
			exp.advance()
		}
	}
	if apiLine == nil {
		exp.currentLine().warnf("Definition of BUILDLINK_API_DEPENDS is missing.")
	}
	exp.expectEmptyLine()

	// Fourth paragraph: Cleanup, corresponding to the first paragraph.
	if exp.advanceIfMatches(`^BUILDLINK_TREE\+=\s*-`+regexp.QuoteMeta(pkgid)+`$`) == nil {
		exp.currentLine().warnf("Expected BUILDLINK_TREE line.")
	}

	if !exp.eof() {
		exp.currentLine().warnf("The file should end here.")
	}

	checklinesBuildlink3Inclusion(lines)
}