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

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

Revision 1.28, Sun Dec 8 00:06:38 2019 UTC (2 months, 2 weeks ago) by rillig
Branch: MAIN
Changes since 1.27: +5 -5 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
anymore.

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 (
	"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.SkipContainsOrWarn("BUILDLINK_TREE+=\t-" + ck.pkgbase) {
		return
	}

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

	if G.Pkg != nil {
		G.Pkg.checkLinesBuildlink3Inclusion(mklines)
	}

	mklines.SaveAutofixChanges()
}

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

	// 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 containsVarRef(pkgbase) {
		ck.checkVaruseInPkgbase(pkgbase, pkgbaseLine)
	}

	ck.checkUniquePkgbase(pkgbase, pkgbaseLine)

	mlex.SkipEmptyOrNote()
	ck.pkgbase = 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.ToRel(mkline.Filename.DirNoClean()).Base()
	base, name := trimCommon(pkgbase, dirname)
	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.SkipContainsOrWarn(pkgupper + "_BUILDLINK3_MK:=") {
		return false
	}
	mlex.SkipEmptyOrNote()

	// See pkgtools/createbuildlink/files/createbuildlink, keyword PKGUPPER
	ucPkgbase := strings.ToUpper(strings.Replace(pkgbase, "-", "_", -1))
	if ucPkgbase != pkgupper && !containsVarRef(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) {
	if G.Pkg == nil {
		return
	}

	mkbase := G.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(G.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(mlex, mkline, pkgbase)

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

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

	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) checkVarassign(mlex *MkLinesLexer, 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.Dependency(); 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.Dependency(); 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 != "" && !containsVarRef(ck.abi.Lower) {
			if ck.api != nil && ck.api.Lower != "" && !containsVarRef(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)
		}
	}
}

func (ck *Buildlink3Checker) checkVaruseInPkgbase(pkgbase string, 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.")
	}
}