[BACK]Return to alternatives.go CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / pkgsrc / pkgtools / pkglint / files

File: [cvs.NetBSD.org] / pkgsrc / pkgtools / pkglint / files / alternatives.go (download)

Revision 1.19, Sun Dec 8 00:06:38 2019 UTC (2 months, 1 week ago) by rillig
Branch: MAIN
CVS Tags: pkgsrc-2019Q4-base, pkgsrc-2019Q4
Changes since 1.18: +95 -64 lines

pkgtools/pkglint: update to 19.3.14

Changes since 19.3.13:

When pkglint suggests to replace !empty(VARNAME:Mfixed) with ${VARNAME}
== fixed, the exact suggested expression is now part of the diagnostic.
The check and the autofix have been improved. They now apply only to the
last modifier in the whole chain, everything else was a bug in pkglint.

Pkglint now knows the scope of variables better than before. It knows
the difference between variables from <sys.mk> like MACHINE_ARCH, which
are always in scope, and those from mk/defaults/mk.conf, which only come
into scope later, after bsd.prefs.mk has been included. It warns when
variables are used too early, for example in .if conditions.

The pathnames in ALTERNATIVES files are now checked for absolute
pathnames. This mistake doesn't happen in practice, but the code for
converting the different path types internally made it necessary to add
these checks. At least this prevents typos.

The special check for obsolete licenses has been removed since their
license files have been removed and that is checked as well.

Variables named *_AWK may be appended to.

The variables _PKG_SILENT and _PKG_DEBUG are no longer deprecated, they
are obsolete now. They are not used in main pkgsrc and pkgsrc-wip

When a package sets a default value for a user-settable variable (which
is something that should not happen anyway), it should .include
bsd.prefs.mk before, in order to not accidentally overwrite the
user-specified value.

Variable modifiers of the form :from=to are now parsed like in bmake.
They are greedy and eat up any following colons as well. This means that
${VAR:.c=.o:Q} replaces source.c with source.o:Q, instead of quoting it.
Pkglint now warns about such cases.

The handling of relative paths in diagnostics is now consistent. All
paths that are part of a diagnostic are relative to the line that issues
the diagnostic.

Fatal errors are no longer suppressed in --autofix mode.

Plus lots of refactoring, to prevent accidental mixing of incompatible
relative paths.

package pkglint

import (

func CheckFileAlternatives(filename CurrPath) {
	lines := Load(filename, NotEmpty|LogErrors)
	if lines == nil {

	var ck AlternativesChecker
	ck.Check(lines, G.Pkg)

type AlternativesChecker struct{}

func (ck *AlternativesChecker) Check(lines *Lines, pkg *Package) {
	var plistFiles map[RelPath]*PlistLine
	if pkg != nil {
		plistFiles = pkg.Plist.Files

	for _, line := range lines.Lines {
		ck.checkLine(line, plistFiles)

// checkLine checks a single line for the following format:
//  wrapper alternative [optional arguments]
func (ck *AlternativesChecker) checkLine(line *Line, plistFiles map[RelPath]*PlistLine) {
	// TODO: Add $ to the regex, just for confidence
	m, wrapper, space, alternative := match3(line.Text, `^([^\t ]+)([ \t]+)([^\t ]+)`)
	if !m {
		line.Errorf("Invalid line %q.", line.Text)
			sprintf("Run %q for more information.", bmakeHelp("alternatives")))

	if ck.checkWrapperAbs(line, NewPath(wrapper)) && plistFiles != nil {
		ck.checkWrapperPlist(line, NewRelPathString(wrapper), plistFiles)
	if plistFiles != nil {
		ck.checkAlternativePlist(line, alternative, plistFiles)

	ck.checkAlternativeAbs(alternative, line, space)

func (ck *AlternativesChecker) checkWrapperAbs(line *Line, wrapper Path) bool {
	if !wrapper.IsAbs() {
		return true

	line.Errorf("Alternative wrapper %q must be relative to PREFIX.", wrapper.String())
	return false

func (ck *AlternativesChecker) checkWrapperPlist(line *Line, wrapper RelPath,
	plistFiles map[RelPath]*PlistLine) {

	if plistFiles[wrapper] != nil {
		line.Errorf("Alternative wrapper %q must not appear in the PLIST.", wrapper)

func (ck *AlternativesChecker) checkAlternativeAbs(alternative string, line *Line, space string) {
	lex := textproc.NewLexer(alternative)

	if lex.SkipByte('/') || lex.SkipByte('@') {

	fix := line.Autofix()
	fix.Errorf("Alternative implementation %q must be an absolute path.", alternative)
		"It usually starts with @PREFIX@/... to refer to a path inside the installation prefix.")
	if lex.TestByteSet(textproc.Alnum) {
		fix.ReplaceAfter(space, alternative, "@PREFIX@/"+alternative)

func (ck *AlternativesChecker) checkAlternativePlist(line *Line, alternative string,
	plistFiles map[RelPath]*PlistLine) {

	relImplementation := strings.Replace(alternative, "@PREFIX@/", "", 1)
	plistName := replaceAll(relImplementation, `@(\w+)@`, "${$1}")
	if NewPath(plistName).IsAbs() {
		// It's possible but unusual to refer to a fixed absolute path.
		// These cannot be mentioned in the PLIST since they are not part of the package.

	rel := NewRelPathString(plistName)
	if plistFiles[rel] != nil || G.Pkg.vars.IsDefined("ALTERNATIVES_SRC") {
	if plistFiles[rel.Replace("${PKGMANDIR}", "man")] != nil {

	if plistName == alternative {
		line.Errorf("Alternative implementation %q must appear in the PLIST.",
	} else {
		line.Errorf("Alternative implementation %q must appear in the PLIST as %q.",
			alternative, plistName)