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

type Toplevel struct {
	dir            string
	previousSubdir string
	subdirs        []string
}

func CheckdirToplevel(dir string) {
	if trace.Tracing {
		defer trace.Call1(dir)()
	}

	ctx := Toplevel{dir, "", nil}
	filename := dir + "/Makefile"

	mklines := LoadMk(filename, NotEmpty|LogErrors)
	if mklines == nil {
		return
	}

	for _, mkline := range mklines.mklines {
		if mkline.IsVarassignMaybeCommented() && mkline.Varname() == "SUBDIR" {
			ctx.checkSubdir(mkline)
		}
	}

	mklines.Check()

	if G.Opts.Recursive {
		if G.Opts.CheckGlobal {
			G.InterPackage.Enable()
		}
		G.Todo.PushFront(ctx.subdirs...)
	}
}

func (ctx *Toplevel) checkSubdir(mkline *MkLine) {
	subdir := mkline.Value()

	if mkline.IsCommentedVarassign() {
		if !mkline.HasComment() || mkline.Comment() == "" {
			mkline.Warnf("%q commented out without giving a reason.", subdir)
		}
	}

	if containsVarRef(subdir) || !fileExists(joinPath(ctx.dir, subdir, "Makefile")) {
		return
	}

	prev := ctx.previousSubdir
	switch {
	case subdir > prev:
		// Correctly ordered
	case subdir == prev:
		mkline.Errorf("Each subdir must only appear once.")
	case subdir == "archivers" && prev == "x11":
		// This exception is documented in the top-level Makefile.
	default:
		mkline.Warnf("%s should come before %s.", subdir, prev)
	}
	ctx.previousSubdir = subdir

	if !mkline.IsCommentedVarassign() {
		ctx.subdirs = append(ctx.subdirs, joinPath(ctx.dir, subdir))
	}
}