summaryrefslogtreecommitdiff
path: root/pkgtools/pkglint/files/tools.go
blob: 24c759b50dab332555a44855015c6e9bf872c198 (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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
package pkglint

import (
	"sort"
	"strings"
)

// Tool is one of the many standard shell utilities that are typically
// provided by the operating system, or, if missing, are installed via
// pkgsrc.
//
// See `mk/tools/`.
type Tool struct {
	Name    string // e.g. "sed", "gzip"
	Varname string // e.g. "SED", "GZIP_CMD"

	// Some of the very simple tools (echo, printf, test) differ in their implementations.
	//
	// When bmake encounters a "simple" command line, it bypasses the
	// call to a shell (see devel/bmake/files/compat.c:/useShell/).
	// Therefore, sometimes the shell builtin is run, and sometimes the
	// native tool.
	//
	// In particular, this decision depends on PKG_DEBUG_LEVEL
	// since that variable adds a semicolon to the command line, which is
	// considered one of the characters that force the commands being
	// executed by the shell. As of December 2018, the list of special characters
	// is "~#=|^(){};&<>*?[]:$`\\\n".
	//
	// To work around this tricky situation, pkglint warns when these shell builtins
	// are used by their simple names (echo, test) instead of the variable form
	// (${ECHO}, ${TEST}).
	MustUseVarForm bool
	Validity       Validity
	Aliases        []string
}

func (tool *Tool) String() string {
	aliases := ""
	if len(tool.Aliases) > 0 {
		aliases = ":" + strings.Join(tool.Aliases, ",")
	}

	varForm := condStr(tool.MustUseVarForm, "var", "")

	return sprintf("%s:%s:%s:%s%s",
		tool.Name, tool.Varname, varForm, tool.Validity, aliases)
}

// UsableAtLoadTime means that the tool may be used by its variable
// name after bsd.prefs.mk has been included.
//
// Additionally, all allowed cases from UsableAtRunTime are allowed.
//
//  VAR:=   ${TOOL}           # Not allowed since bsd.prefs.mk is not
//                            # included yet.
//
//  .include "../../bsd.prefs.mk"
//
//  VAR:=   ${TOOL}           # Allowed.
//  VAR!=   ${TOOL}           # Allowed.
//
//  VAR=    ${${TOOL}:sh}     # Allowed; the :sh modifier is evaluated
//                            # lazily, but when VAR should ever be
//                            # evaluated at load time, this still means
//                            # load time.
//
//  .if ${TOOL:T} == "tool"   # Allowed.
//  .endif
func (tool *Tool) UsableAtLoadTime(seenPrefs bool) bool {
	return seenPrefs && tool.Validity == AfterPrefsMk
}

// UsableAtRunTime means that the tool may be used by its simple name
// in all {pre,do,post}-* targets, and by its variable name in all
// runtime contexts.
//
//  VAR:=   ${TOOL}           # Not allowed; TOOL might not be initialized yet.
//  VAR!=   ${TOOL}           # Not allowed; TOOL might not be initialized yet.
//
//  VAR=    ${${TOOL}:sh}     # Probably ok; the :sh modifier is evaluated at
//                            # run time. But if VAR should ever be evaluated
//                            # at load time (see the "Not allowed" cases
//                            # above), it doesn't work. As of January 2019,
//                            # pkglint cannot reliably distinguish these cases.
//
//  own-target:
//          ${TOOL}           # Allowed.
//          tool              # Not allowed because the PATH might not be set
//                            # up for this target.
//
//  pre-configure:
//          ${TOOL}           # Allowed.
//          tool              # Allowed.
func (tool *Tool) UsableAtRunTime() bool {
	return tool.Validity == AtRunTime || tool.Validity == AfterPrefsMk
}

// Tools collects all tools for a certain scope (global or file)
// and remembers whether these tools are defined at all,
// and whether they are declared to be used via USE_TOOLS.
type Tools struct {
	byName    map[string]*Tool // "sed" => tool
	byVarname map[string]*Tool // "GREP_CMD" => tool
	fallback  *Tools

	// Determines the effect of adding the tool to USE_TOOLS.
	//
	// As long as bsd.prefs.mk has definitely not been included by the current file,
	// tools added to USE_TOOLS are available at load time, but only after bsd.prefs.mk
	// has been included.
	//
	// Adding a tool to USE_TOOLS _after_ bsd.prefs.mk has been included, on the other
	// hand, only makes the tool available at run time.
	SeenPrefs bool

	// For example, "sed" is an alias of "gsed".
	//
	// This means when gsed is added to USE_TOOLS, sed is implicitly added as well.
	AliasOf map[string]string
}

func NewTools() *Tools {
	return &Tools{
		make(map[string]*Tool),
		make(map[string]*Tool),
		nil,
		false,
		make(map[string]string)}
}

// Define registers the tool by its name and the corresponding
// variable name (if nonempty). Depending on the given mkline,
// it may be added to USE_TOOLS automatically.
//
// After this tool is added to USE_TOOLS, it may be used by this name
// (e.g. "awk") or by its variable (e.g. ${AWK}).
func (tr *Tools) Define(name, varname string, mkline *MkLine) *Tool {
	if trace.Tracing {
		trace.Stepf("Tools.Define: %q %q in %s", name, varname, mkline)
	}

	if !tr.IsValidToolName(name) {
		mkline.Errorf("Invalid tool name %q.", name)
		return nil
	}

	validity := tr.validity(mkline.Basename, false)
	return tr.def(name, varname, false, validity, nil)
}

func (tr *Tools) def(name, varname string, mustUseVarForm bool, validity Validity, aliases []string) *Tool {
	assert(tr.IsValidToolName(name))

	fresh := Tool{name, varname, mustUseVarForm, validity, aliases}

	tool := tr.byName[name]
	if tool == nil {
		tool = &fresh
		tr.byName[name] = tool
	} else {
		tr.merge(tool, &fresh)
	}

	if tr.fallback != nil {
		if fallback := tr.fallback.ByName(name); fallback != nil {
			tr.merge(tool, fallback)
		}
	}

	if varname != "" {
		if existing := tr.byVarname[varname]; existing == nil || len(existing.Name) > len(name) {
			tr.byVarname[varname] = tool
		}
	}

	for _, alias := range aliases {
		tr.AliasOf[alias] = name
	}

	return tool
}

func (tr *Tools) merge(target, source *Tool) {
	if target.Varname == "" && source.Varname != "" {
		target.Varname = source.Varname
	}
	if !target.MustUseVarForm && source.MustUseVarForm {
		target.MustUseVarForm = true
	}
	if source.Validity > target.Validity {
		target.Validity = source.Validity
	}
}

func (tr *Tools) Trace() {
	if trace.Tracing {
		defer trace.Call0()()
	} else {
		return
	}

	var keys []string
	for k := range tr.byName {
		keys = append(keys, k)
	}
	sort.Strings(keys)

	for _, toolname := range keys {
		trace.Stepf("tool %+v", tr.byName[toolname])
	}

	if tr.fallback != nil {
		tr.fallback.Trace()
	}
}

// ParseToolLine updates the tool definitions according to the given
// line from a Makefile.
//
// If fromInfrastructure is true, the tool is defined even when it is only
// added to USE_TOOLS (which normally doesn't define anything). This way,
// pkglint also finds those tools whose definitions are too difficult to
// parse from the code.
//
// If addToUseTools is true, a USE_TOOLS line makes a tool immediately
// usable. This should only be done if the current line is unconditional.
func (tr *Tools) ParseToolLine(mklines *MkLines, mkline *MkLine, fromInfrastructure bool, addToUseTools bool) {
	switch {

	case mkline.IsVarassign():
		varparam := mkline.Varparam()
		value := mkline.Value()

		switch mkline.Varcanon() {
		case "TOOLS_CREATE":
			for _, name := range mkline.ValueFields(value) {
				if tr.IsValidToolName(name) {
					tr.def(name, "", false, AtRunTime, nil)
				}
			}

		case "_TOOLS_VARNAME.*":
			if !containsVarRef(varparam) {
				tr.Define(varparam, value, mkline)
			}

		case "TOOLS_PATH.*", "_TOOLS_DEPMETHOD.*":
			if !containsVarRef(varparam) {
				tr.Define(varparam, "", mkline)
			}

		case "TOOLS_ALIASES.*":
			if containsVarRef(varparam) {
				break
			}

			tool := tr.def(varparam, "", false, Nowhere, nil)

			for _, alias := range mkline.ValueFields(value) {
				if tr.IsValidToolName(alias) {
					tr.addAlias(tool, alias)
				} else {
					varUse := ToVarUse(alias)
					if varUse != nil {
						for _, subAlias := range mklines.ExpandLoopVar(varUse.varname) {
							if tr.IsValidToolName(subAlias) {
								tr.addAlias(tool, subAlias)
							}
						}
					}
				}
			}

		case "_TOOLS.*":
			if !containsVarRef(varparam) {
				tr.Define(varparam, "", mkline)
				for _, tool := range mkline.ValueFields(value) {
					tr.Define(tool, "", mkline)
				}
			}

		case "USE_TOOLS":
			tr.parseUseTools(mkline, fromInfrastructure, addToUseTools)
		}

	case mkline.IsInclude():
		if LoadsPrefs(mkline.IncludedFile()) {
			tr.SeenPrefs = true
		}
	}
}

func (tr *Tools) addAlias(tool *Tool, alias string) {
	tool.Aliases = append(tool.Aliases, alias)
	tr.AliasOf[alias] = tool.Name
}

// parseUseTools interprets a "USE_TOOLS+=" line from a Makefile fragment.
// It determines the validity of the tool, i.e. in which places it may be used.
//
// If createIfAbsent is true and the tool is unknown, it is registered.
// This can be done only in the pkgsrc infrastructure files, where the
// actual definition is assumed to be in some other file. In packages
// though, this assumption cannot be made and pkglint needs to be strict.
func (tr *Tools) parseUseTools(mkline *MkLine, createIfAbsent bool, addToUseTools bool) {
	value := mkline.Value()
	if containsVarRef(value) {
		return
	}

	validity := tr.validity(mkline.Basename, addToUseTools)
	for _, dep := range mkline.ValueFields(value) {
		name := strings.Split(dep, ":")[0]
		if createIfAbsent || tr.ByName(name) != nil {
			tr.def(name, "", false, validity, nil)
			for _, implicitName := range tr.implicitTools(name) {
				tr.def(implicitName, "", false, validity, nil)
			}
		}
	}
}

func (tr *Tools) implicitTools(toolName string) []string {

	// See mk/tools/autoconf.mk:/^\.if !defined/

	if toolName == "autoconf213" {
		return []string{
			"autoconf-2.13", "autoheader-2.13", "autoreconf-2.13",
			"autoscan-2.13", "autoupdate-2.13", "ifnames-2.13",
			"autoconf",
			"autoheader", "autom4te", "autoreconf",
			"autoscan", "autoupdate", "ifnames"}
	}

	if toolName == "autoconf" {
		return []string{
			"autoheader", "autom4te", "autoreconf",
			"autoscan", "autoupdate", "ifnames"}
	}

	return nil
}

func (tr *Tools) validity(basename string, useTools bool) Validity {
	switch {
	case LoadsPrefs(NewRelPathString(basename)): // LoadsPrefs is not 100% accurate here but good enough
		return AfterPrefsMk
	case basename == "Makefile" && !tr.SeenPrefs:
		return AfterPrefsMk
	case useTools:
		return AtRunTime
	default:
		return Nowhere
	}
}

func (tr *Tools) ByName(name string) *Tool {
	tool := tr.byName[name]
	if tool == nil && tr.fallback != nil {
		fallback := tr.fallback.ByName(name)
		if fallback != nil {
			tool = tr.def(fallback.Name, fallback.Varname, fallback.MustUseVarForm, fallback.Validity, fallback.Aliases)
		}
	}
	tr.adjustValidity(tool)
	return tool
}

func (tr *Tools) ByVarname(varname string) *Tool {
	tool := tr.byVarname[varname]
	if tool == nil && tr.fallback != nil {
		fallback := tr.fallback.ByVarname(varname)
		if fallback != nil {
			tool = tr.def(fallback.Name, fallback.Varname, fallback.MustUseVarForm, fallback.Validity, fallback.Aliases)
		}
	}
	tr.adjustValidity(tool)
	return tool
}

// TODO: Tools.ByCommand (name or ${VARNAME})

func (tr *Tools) Usable(tool *Tool, time ToolTime) bool {
	if time == LoadTime {
		return tool.UsableAtLoadTime(tr.SeenPrefs)
	} else {
		return tool.UsableAtRunTime()
	}
}

func (tr *Tools) Fallback(other *Tools) {
	assert(tr.fallback == nil) // Must only be called once.
	tr.fallback = other
}

func (tr *Tools) IsValidToolName(name string) bool {
	return name == "[" || name == "echo -n" || matches(name, `^[-0-9a-z.]+$`)
}

func (tr *Tools) adjustValidity(tool *Tool) {
	if tool == nil {
		return
	}

	aliasName := tr.AliasOf[tool.Name]
	if aliasName == "" {
		return
	}

	alias := tr.ByName(tr.AliasOf[tool.Name])
	if alias.Validity > tool.Validity {
		tool.Validity = alias.Validity
	}
}

func (tr *Tools) ExistsVar(varname string) bool { return tr.byVarname[varname] != nil }

type Validity uint8

const (
	// Nowhere means that the tool has not been added
	// to USE_TOOLS and therefore cannot be used at all.
	Nowhere Validity = iota

	// AtRunTime means that the tool has been added to USE_TOOLS
	// after including bsd.prefs.mk and therefore cannot be used
	// at load time.
	//
	// The tool may be used as ${TOOL} in all targets.
	// The tool may be used by its plain name in {pre,do,post}-* targets.
	AtRunTime

	// AfterPrefsMk means that the tool has been added to USE_TOOLS
	// before including bsd.prefs.mk and therefore can be used at
	// load time after bsd.prefs.mk has been included.
	//
	// The tool may be used as ${TOOL} everywhere.
	// The tool may be used by its plain name in {pre,do,post}-* targets.
	AfterPrefsMk
)

func (time Validity) String() string {
	return [...]string{"Nowhere", "AtRunTime", "AfterPrefsMk"}[time]
}

type ToolTime uint8

const (
	LoadTime ToolTime = iota
	RunTime
)

func (t ToolTime) String() string { return [...]string{"LoadTime", "RunTime"}[t] }