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

import (
	"netbsd.org/pkglint/pkgver"
	"strings"
)

type Buildlink3Checker struct {
	mklines          *MkLines
	pkgbase          string
	pkgbaseLine      *MkLine
	abiLine, apiLine *MkLine
	abi, api         *DependencyPattern
}

func CheckLinesBuildlink3Mk(mklines *MkLines) {
	(&Buildlink3Checker{mklines: mklines}).Check()
}

func (ck *Buildlink3Checker) Check() {
	mklines := ck.mklines
	if trace.Tracing {
		defer trace.Call(mklines.lines.Filename)()
	}

	mklines.Check()

	llex := NewMkLinesLexer(mklines)

	for llex.SkipIf((*MkLine).IsComment) {
		line := llex.PreviousLine()
		// See pkgtools/createbuildlink/files/createbuildlink
		if hasPrefix(line.Text, "# XXX This file was created automatically") {
			line.Errorf("This comment indicates unfinished work (url2pkg).")
		}
	}

	llex.SkipEmptyOrNote()

	if llex.SkipRegexp(`^BUILDLINK_DEPMETHOD\.([^\t ]+)\?=.*$`) {
		llex.PreviousLine().Warnf("This line belongs inside the .ifdef block.")
		for llex.SkipText("") {
		}
	}

	if !ck.checkFirstParagraph(llex) {
		return
	}
	if !ck.checkSecondParagraph(llex) {
		return
	}
	if !ck.checkMainPart(llex) {
		return
	}

	// Fourth paragraph: Cleanup, corresponding to the first paragraph.
	if !llex.SkipTextOrWarn("BUILDLINK_TREE+=\t-" + ck.pkgbase) {
		return
	}

	if !llex.EOF() {
		llex.CurrentLine().Warnf("The file should end here.")
	}

	pkg := ck.mklines.pkg
	if pkg != nil {
		pkg.checkLinesBuildlink3Inclusion(mklines)
	}

	mklines.SaveAutofixChanges()
}

func (ck *Buildlink3Checker) checkFirstParagraph(mlex *MkLinesLexer) bool {

	for mlex.SkipPrefix("#") {
	}

	// First paragraph: Introduction of the package identifier
	m := mlex.NextRegexp(`^BUILDLINK_TREE\+=[\t ]*([^\t ]+)$`)
	if m == nil {
		mlex.CurrentLine().Warnf("Expected a BUILDLINK_TREE line.")
		return false
	}

	pkgbase := m[1]
	pkgbaseLine := mlex.PreviousMkLine()

	if containsVarUse(pkgbase) {
		ck.checkVaruseInPkgbase(pkgbaseLine)
	}

	ck.checkUniquePkgbase(pkgbase, pkgbaseLine)

	mlex.SkipEmptyOrNote()
	ck.pkgbase = pkgbase
	if pkg := ck.mklines.pkg; pkg != nil {
		pkg.buildlinkID = ck.pkgbase
	}
	ck.pkgbaseLine = pkgbaseLine
	return true
}

func (ck *Buildlink3Checker) checkUniquePkgbase(pkgbase string, mkline *MkLine) {
	prev := G.InterPackage.Bl3(pkgbase, &mkline.Location)
	if prev == nil {
		return
	}

	dirname := G.Pkgsrc.Rel(mkline.Filename().Dir()).Base()
	base, name := trimCommon(pkgbase, dirname.String())
	if base == "" && matches(name, `^(\d*|-cvs|-fossil|-git|-hg|-svn|-devel|-snapshot)$`) {
		return
	}

	mkline.Errorf("Duplicate package identifier %q already appeared in %s.",
		pkgbase, mkline.RelLocation(*prev))
	mkline.Explain(
		"Each buildlink3.mk file must have a unique identifier.",
		"These identifiers are used for multiple-inclusion guards,",
		"and using the same identifier for different packages",
		"(often by copy-and-paste) may change the dependencies",
		"of a package in subtle and unexpected ways.")
}

// checkSecondParagraph checks the multiple inclusion protection and
// introduces the uppercase package identifier.
func (ck *Buildlink3Checker) checkSecondParagraph(mlex *MkLinesLexer) bool {
	pkgbase := ck.pkgbase
	m := mlex.NextRegexp(`^\.if !defined\(([^\t ]+)_BUILDLINK3_MK\)$`)
	if m == nil {
		return false
	}
	pkgupperLine, pkgupper := mlex.PreviousMkLine(), m[1]

	if !mlex.SkipTextOrWarn(pkgupper + "_BUILDLINK3_MK:=") {
		return false
	}
	mlex.SkipEmptyOrNote()

	// See pkgtools/createbuildlink/files/createbuildlink, keyword PKGUPPER
	ucPkgbase := strings.ToUpper(strings.Replace(pkgbase, "-", "_", -1))
	if ucPkgbase != pkgupper && !containsVarUse(pkgbase) {
		pkgupperLine.Errorf("Package name mismatch between multiple-inclusion guard %q (expected %q) and package name %q (from %s).",
			pkgupper, ucPkgbase, pkgbase, pkgupperLine.RelMkLine(ck.pkgbaseLine))
	}
	ck.checkPkgbaseMismatch(pkgbase)

	return true
}

func (ck *Buildlink3Checker) checkPkgbaseMismatch(bl3base string) {
	pkg := ck.mklines.pkg
	if pkg == nil {
		return
	}

	mkbase := pkg.EffectivePkgbase
	if mkbase == "" || mkbase == bl3base || strings.TrimPrefix(mkbase, "lib") == bl3base {
		return
	}

	if hasPrefix(mkbase, bl3base) && matches(mkbase[len(bl3base):], `^\d+$`) {
		return
	}

	ck.pkgbaseLine.Errorf("Package name mismatch between %q in this file and %q from %s.",
		bl3base, mkbase, ck.pkgbaseLine.RelMkLine(pkg.EffectivePkgnameLine))
}

// Third paragraph: Package information.
func (ck *Buildlink3Checker) checkMainPart(mlex *MkLinesLexer) bool {
	pkgbase := ck.pkgbase

	// The first .if is from the second paragraph.
	indentLevel := 1

	for !mlex.EOF() && indentLevel > 0 {
		mkline := mlex.CurrentMkLine()
		mlex.Skip()

		switch {
		case mkline.IsVarassign():
			ck.checkVarassign(mkline, pkgbase)

		case mkline.IsDirective() && mkline.Directive() == "if":
			indentLevel++

		case mkline.IsDirective() && mkline.Directive() == "endif":
			indentLevel--
		}

		mkline.ForEachUsed(func(varUse *MkVarUse, time VucTime) {
			ck.checkVarUse(varUse, mkline)
		})
	}

	if indentLevel > 0 {
		return false
	}

	if ck.apiLine == nil {
		mlex.CurrentLine().Warnf("Definition of BUILDLINK_API_DEPENDS is missing.")
	}
	mlex.SkipEmptyOrNote()
	return true
}

func (ck *Buildlink3Checker) checkVarUse(varUse *MkVarUse, mkline *MkLine) {
	varname := varUse.varname
	if varname == "PKG_OPTIONS" {
		mkline.Errorf("PKG_OPTIONS is not available in buildlink3.mk files.")
		mkline.Explain(
			"The buildlink3.mk file of a package is only ever included",
			"by other packages, never by the package itself.",
			"Therefore it does not make sense to use the variable PKG_OPTIONS",
			"in this place since it contains the package options of a random",
			"package that happens to include this file.",
			"",
			"To access the options of this package, see mk/pkg-build-options.mk.")
	}

	if varnameBase(varname) == "PKG_BUILD_OPTIONS" {
		param := varnameParam(varname)
		if param != "" && param != ck.pkgbase {
			mkline.Warnf("Wrong PKG_BUILD_OPTIONS, expected %q instead of %q.",
				ck.pkgbase, param)
			mkline.Explain(
				"The variable parameter for PKG_BUILD_OPTIONS must correspond",
				"to the value of \"pkgbase\" above.")
		}
	}
}

func (ck *Buildlink3Checker) checkVarassign(mkline *MkLine, pkgbase string) {
	varname, value := mkline.Varname(), mkline.Value()
	doCheck := false

	if varname == "BUILDLINK_ABI_DEPENDS."+pkgbase {
		ck.abiLine = mkline
		parser := NewMkParser(nil, value)
		if dp := parser.DependencyPattern(); dp != nil && parser.EOF() {
			ck.abi = dp
		}
		doCheck = true
	}

	if varname == "BUILDLINK_API_DEPENDS."+pkgbase {
		ck.apiLine = mkline
		parser := NewMkParser(nil, value)
		if dp := parser.DependencyPattern(); dp != nil && parser.EOF() {
			ck.api = dp
		}
		doCheck = true
	}

	if doCheck && ck.abi != nil && ck.api != nil && ck.abi.Pkgbase != ck.api.Pkgbase {
		if !hasPrefix(ck.api.Pkgbase, "{") {
			ck.abiLine.Warnf("Package name mismatch between ABI %q and API %q (from %s).",
				ck.abi.Pkgbase, ck.api.Pkgbase, ck.abiLine.RelMkLine(ck.apiLine))
		}
	}

	if doCheck {
		if ck.abi != nil && ck.abi.Lower != "" && !containsVarUse(ck.abi.Lower) {
			if ck.api != nil && ck.api.Lower != "" && !containsVarUse(ck.api.Lower) {
				if pkgver.Compare(ck.abi.Lower, ck.api.Lower) < 0 {
					ck.abiLine.Warnf("ABI version %q should be at least API version %q (see %s).",
						ck.abi.Lower, ck.api.Lower, ck.abiLine.RelMkLine(ck.apiLine))
				}
			}
		}
	}

	if varparam := mkline.Varparam(); varparam != "" && varparam != pkgbase {
		if hasPrefix(varname, "BUILDLINK_") && mkline.Varcanon() != "BUILDLINK_API_DEPENDS.*" {
			mkline.Warnf("Only buildlink variables for %q, not %q may be set in this file.", pkgbase, varparam)
		}
	}

	if varname == "pkgbase" && value != ck.pkgbase {
		mkline.Errorf("A buildlink3.mk file must only query its own PKG_BUILD_OPTIONS.%s, not PKG_BUILD_OPTIONS.%s.",
			ck.pkgbase, value)
	}

	ck.checkVarassignPkgsrcdir(mkline, pkgbase, varname, value)
}

func (ck *Buildlink3Checker) checkVarassignPkgsrcdir(mkline *MkLine,
	pkgbase string, varname string, value string) {

	if varname != "BUILDLINK_PKGSRCDIR."+pkgbase {
		return
	}
	if containsVarUse(value) {
		return
	}

	pkgdir := mkline.Filename().Dir()
	expected := "../../" + G.Pkgsrc.Rel(pkgdir).String()
	if value == expected {
		return
	}

	mkline.Errorf("%s must be set to the package's own path (%s), not %s.",
		varname, expected, value)
}

func (ck *Buildlink3Checker) checkVaruseInPkgbase(pkgbaseLine *MkLine) {
	tokens, _ := pkgbaseLine.ValueTokens()
	for _, token := range tokens {
		if token.Varuse == nil {
			continue
		}

		replacement := ""
		switch token.Varuse.varname {
		case "PYPKGPREFIX":
			replacement = "py"
		case "RUBY_BASE", "RUBY_PKGPREFIX":
			replacement = "ruby"
		case "PHP_PKG_PREFIX":
			replacement = "php"
		}

		if replacement != "" {
			pkgbaseLine.Warnf("Please use %q instead of %q (also in other variables in this file).",
				replacement, token.Text)
		} else {
			pkgbaseLine.Warnf(
				"Please replace %q with a simple string (also in other variables in this file).",
				token.Text)
		}

		pkgbaseLine.Explain(
			"The identifiers in the BUILDLINK_TREE variable should be plain",
			"strings that do not refer to any variable.",
			"",
			"Even for packages that depend on a specific version of a",
			"programming language, the plain name is enough since",
			"the version number of the programming language is stored elsewhere.",
			"Furthermore, these package identifiers are only used at build time,",
			"after the specific version has been decided.")
	}
}

type Buildlink3Data struct {
	id             Buildlink3ID
	prefix         Path
	pkgsrcdir      PackagePath
	apiDepends     *DependencyPattern
	apiDependsLine *MkLine
	abiDepends     *DependencyPattern
	abiDependsLine *MkLine
}

// Buildlink3ID is the identifier that is used in the BUILDLINK_TREE
// for referring to a dependent package.
//
// It almost uniquely identifies a package.
// Packages that are alternatives to each other may use the same identifier.
type Buildlink3ID string

func LoadBuildlink3Data(mklines *MkLines) *Buildlink3Data {
	var data Buildlink3Data

	mklines.ForEach(func(mkline *MkLine) {
		if !mkline.IsVarassign() {
			return
		}

		varname := mkline.Varname()
		varbase := varnameBase(varname)
		varid := Buildlink3ID(varnameParam(varname))

		if varname == "BUILDLINK_TREE" {
			value := mkline.Value()
			if !hasPrefix(value, "-") {
				data.id = Buildlink3ID(mkline.Value())
			}
		}

		if varbase == "BUILDLINK_API_DEPENDS" && varid == data.id {
			p := NewMkParser(nil, mkline.Value())
			dep := p.DependencyPattern()
			if dep != nil && p.EOF() {
				data.apiDepends = dep
				data.apiDependsLine = mkline
			}
		}

		if varbase == "BUILDLINK_ABI_DEPENDS" && varid == data.id {
			p := NewMkParser(nil, mkline.Value())
			dep := p.DependencyPattern()
			if dep != nil && p.EOF() {
				data.abiDepends = dep
				data.abiDependsLine = mkline
			}
		}

		if varbase == "BUILDLINK_PREFIX" && varid == data.id {
			data.prefix = NewPath(mkline.Value())
		}
		if varbase == "BUILDLINK_PKGSRCDIR" && varid == data.id {
			data.pkgsrcdir = NewPackagePathString(mkline.Value())
		}
	})

	if data.id != "" {
		return &data
	}
	return nil
}