1# $Id: Makefile,v 1.115 2020/11/18 04:01:07 sjg Exp $ 2# 3# $NetBSD: Makefile,v 1.206 2020/11/18 01:12:00 sjg Exp $ 4# 5# Unit tests for make(1) 6# 7# The main targets are: 8# 9# all: 10# run all the tests 11# test: 12# run 'all', and compare to expected results 13# accept: 14# move generated output to expected results 15# 16# Settable variables 17# 18# TEST_MAKE 19# The make program to be tested. 20# 21# 22# Adding a test case 23# 24# Each feature should get its own set of tests in its own suitably 25# named makefile (*.mk), with its own set of expected results (*.exp), 26# and it should be added to the TESTS list. 27# 28# A few *.mk files are helper files for other tests (such as include-sub.mk) 29# and are thus not added to TESTS. Such files must be ignored in 30# src/tests/usr.bin/make/t_make.sh. 31# 32 33# Each test is in a sub-makefile. 34# Keep the list sorted. 35# Any test that is commented out must be ignored in 36# src/tests/usr.bin/make/t_make.sh as well. 37#TESTS+= archive 38TESTS+= archive-suffix 39TESTS+= cmd-errors 40TESTS+= cmd-errors-lint 41TESTS+= cmd-interrupt 42TESTS+= cmdline 43TESTS+= cmdline-undefined 44TESTS+= comment 45TESTS+= cond-cmp-numeric 46TESTS+= cond-cmp-numeric-eq 47TESTS+= cond-cmp-numeric-ge 48TESTS+= cond-cmp-numeric-gt 49TESTS+= cond-cmp-numeric-le 50TESTS+= cond-cmp-numeric-lt 51TESTS+= cond-cmp-numeric-ne 52TESTS+= cond-cmp-string 53TESTS+= cond-cmp-unary 54TESTS+= cond-func 55TESTS+= cond-func-commands 56TESTS+= cond-func-defined 57TESTS+= cond-func-empty 58TESTS+= cond-func-exists 59TESTS+= cond-func-make 60TESTS+= cond-func-target 61TESTS+= cond-late 62TESTS+= cond-op 63TESTS+= cond-op-and 64TESTS+= cond-op-and-lint 65TESTS+= cond-op-not 66TESTS+= cond-op-or 67TESTS+= cond-op-or-lint 68TESTS+= cond-op-parentheses 69TESTS+= cond-short 70TESTS+= cond-token-number 71TESTS+= cond-token-plain 72TESTS+= cond-token-string 73TESTS+= cond-token-var 74TESTS+= cond-undef-lint 75TESTS+= cond1 76TESTS+= counter 77TESTS+= counter-append 78TESTS+= dep 79TESTS+= dep-colon 80TESTS+= dep-colon-bug-cross-file 81TESTS+= dep-double-colon 82TESTS+= dep-double-colon-indep 83TESTS+= dep-exclam 84TESTS+= dep-none 85TESTS+= dep-percent 86TESTS+= dep-var 87TESTS+= dep-wildcards 88TESTS+= depsrc 89TESTS+= depsrc-end 90TESTS+= depsrc-exec 91TESTS+= depsrc-ignore 92TESTS+= depsrc-made 93TESTS+= depsrc-make 94TESTS+= depsrc-meta 95TESTS+= depsrc-nometa 96TESTS+= depsrc-nometa_cmp 97TESTS+= depsrc-nopath 98TESTS+= depsrc-notmain 99TESTS+= depsrc-optional 100TESTS+= depsrc-phony 101TESTS+= depsrc-precious 102TESTS+= depsrc-recursive 103TESTS+= depsrc-silent 104TESTS+= depsrc-use 105TESTS+= depsrc-usebefore 106TESTS+= depsrc-usebefore-double-colon 107TESTS+= depsrc-wait 108TESTS+= deptgt 109TESTS+= deptgt-begin 110TESTS+= deptgt-default 111TESTS+= deptgt-delete_on_error 112TESTS+= deptgt-end 113TESTS+= deptgt-end-jobs 114TESTS+= deptgt-error 115TESTS+= deptgt-ignore 116TESTS+= deptgt-interrupt 117TESTS+= deptgt-main 118TESTS+= deptgt-makeflags 119TESTS+= deptgt-no_parallel 120TESTS+= deptgt-nopath 121TESTS+= deptgt-notparallel 122TESTS+= deptgt-objdir 123TESTS+= deptgt-order 124TESTS+= deptgt-path 125TESTS+= deptgt-path-suffix 126TESTS+= deptgt-phony 127TESTS+= deptgt-precious 128TESTS+= deptgt-shell 129TESTS+= deptgt-silent 130TESTS+= deptgt-stale 131TESTS+= deptgt-suffixes 132TESTS+= dir 133TESTS+= dir-expand-path 134TESTS+= directive 135TESTS+= directive-dinclude 136TESTS+= directive-elif 137TESTS+= directive-elifdef 138TESTS+= directive-elifmake 139TESTS+= directive-elifndef 140TESTS+= directive-elifnmake 141TESTS+= directive-else 142TESTS+= directive-endif 143TESTS+= directive-error 144TESTS+= directive-export 145TESTS+= directive-export-env 146TESTS+= directive-export-gmake 147TESTS+= directive-export-literal 148TESTS+= directive-for 149TESTS+= directive-for-generating-endif 150TESTS+= directive-hyphen-include 151TESTS+= directive-if 152TESTS+= directive-if-nested 153TESTS+= directive-ifdef 154TESTS+= directive-ifmake 155TESTS+= directive-ifndef 156TESTS+= directive-ifnmake 157TESTS+= directive-include 158TESTS+= directive-include-fatal 159TESTS+= directive-info 160TESTS+= directive-sinclude 161TESTS+= directive-undef 162TESTS+= directive-unexport 163TESTS+= directive-unexport-env 164TESTS+= directive-warning 165TESTS+= dollar 166TESTS+= doterror 167TESTS+= dotwait 168TESTS+= envfirst 169TESTS+= error 170TESTS+= # escape # broken by reverting POSIX changes 171TESTS+= export 172TESTS+= export-all 173TESTS+= export-env 174TESTS+= export-variants 175TESTS+= forloop 176TESTS+= forsubst 177TESTS+= gnode-submake 178TESTS+= hanoi-include 179TESTS+= impsrc 180TESTS+= include-main 181TESTS+= job-flags 182#TESTS+= job-output-long-lines 183TESTS+= lint 184TESTS+= make-exported 185TESTS+= moderrs 186TESTS+= modmatch 187TESTS+= modmisc 188TESTS+= modts 189TESTS+= modword 190TESTS+= objdir-writable 191TESTS+= opt 192TESTS+= opt-backwards 193TESTS+= opt-chdir 194TESTS+= opt-debug 195TESTS+= opt-debug-all 196TESTS+= opt-debug-archive 197TESTS+= opt-debug-curdir 198TESTS+= opt-debug-cond 199TESTS+= opt-debug-dir 200TESTS+= opt-debug-errors 201TESTS+= opt-debug-file 202TESTS+= opt-debug-for 203TESTS+= opt-debug-graph1 204TESTS+= opt-debug-graph2 205TESTS+= opt-debug-graph3 206TESTS+= opt-debug-hash 207#TESTS+= opt-debug-jobs 208TESTS+= opt-debug-lint 209TESTS+= opt-debug-loud 210TESTS+= opt-debug-meta 211TESTS+= opt-debug-making 212TESTS+= opt-debug-no-rm 213TESTS+= opt-debug-parse 214TESTS+= opt-debug-suff 215TESTS+= opt-debug-targets 216TESTS+= opt-debug-varraw 217TESTS+= opt-debug-var 218TESTS+= opt-debug-x-trace 219TESTS+= opt-define 220TESTS+= opt-env 221TESTS+= opt-file 222TESTS+= opt-ignore 223TESTS+= opt-include-dir 224TESTS+= opt-jobs 225TESTS+= opt-jobs-internal 226TESTS+= opt-keep-going 227TESTS+= opt-m-include-dir 228TESTS+= opt-no-action 229TESTS+= opt-no-action-at-all 230TESTS+= opt-query 231TESTS+= opt-raw 232TESTS+= opt-silent 233TESTS+= opt-touch 234TESTS+= opt-touch-jobs 235TESTS+= opt-tracefile 236TESTS+= opt-var-expanded 237TESTS+= opt-var-literal 238TESTS+= opt-warnings-as-errors 239TESTS+= opt-where-am-i 240TESTS+= opt-x-reduce-exported 241TESTS+= order 242TESTS+= parse-var 243TESTS+= phony-end 244TESTS+= posix 245TESTS+= # posix1 # broken by reverting POSIX changes 246TESTS+= qequals 247TESTS+= recursive 248TESTS+= sh 249TESTS+= sh-dots 250TESTS+= sh-jobs 251TESTS+= sh-jobs-error 252TESTS+= sh-leading-at 253TESTS+= sh-leading-hyphen 254TESTS+= sh-leading-plus 255TESTS+= sh-meta-chars 256TESTS+= sh-multi-line 257TESTS+= sh-single-line 258TESTS+= shell-csh 259TESTS+= shell-custom 260.if exists(/bin/ksh) 261TESTS+= shell-ksh 262.endif 263TESTS+= shell-sh 264TESTS+= suff-add-later 265TESTS+= suff-clear-regular 266TESTS+= suff-clear-single 267TESTS+= suff-lookup 268TESTS+= suff-main 269TESTS+= suff-rebuild 270TESTS+= suff-self 271TESTS+= suff-transform-endless 272TESTS+= suff-transform-expand 273TESTS+= suff-transform-select 274TESTS+= sunshcmd 275TESTS+= ternary 276TESTS+= unexport 277TESTS+= unexport-env 278TESTS+= use-inference 279TESTS+= var-class 280TESTS+= var-class-cmdline 281TESTS+= var-class-env 282TESTS+= var-class-global 283TESTS+= var-class-local 284TESTS+= var-class-local-legacy 285TESTS+= var-op 286TESTS+= var-op-append 287TESTS+= var-op-assign 288TESTS+= var-op-default 289TESTS+= var-op-expand 290TESTS+= var-op-shell 291TESTS+= var-op-sunsh 292TESTS+= var-recursive 293TESTS+= varcmd 294TESTS+= vardebug 295TESTS+= varfind 296TESTS+= varmisc 297TESTS+= varmod 298TESTS+= varmod-assign 299TESTS+= varmod-defined 300TESTS+= varmod-edge 301TESTS+= varmod-exclam-shell 302TESTS+= varmod-extension 303TESTS+= varmod-gmtime 304TESTS+= varmod-hash 305TESTS+= varmod-head 306TESTS+= varmod-ifelse 307TESTS+= varmod-l-name-to-value 308TESTS+= varmod-localtime 309TESTS+= varmod-loop 310TESTS+= varmod-match 311TESTS+= varmod-match-escape 312TESTS+= varmod-no-match 313TESTS+= varmod-order 314TESTS+= varmod-order-reverse 315TESTS+= varmod-order-shuffle 316TESTS+= varmod-path 317TESTS+= varmod-quote 318TESTS+= varmod-quote-dollar 319TESTS+= varmod-range 320TESTS+= varmod-remember 321TESTS+= varmod-root 322TESTS+= varmod-select-words 323TESTS+= varmod-shell 324TESTS+= varmod-subst 325TESTS+= varmod-subst-regex 326TESTS+= varmod-sysv 327TESTS+= varmod-tail 328TESTS+= varmod-to-abs 329TESTS+= varmod-to-lower 330TESTS+= varmod-to-many-words 331TESTS+= varmod-to-one-word 332TESTS+= varmod-to-separator 333TESTS+= varmod-to-upper 334TESTS+= varmod-undefined 335TESTS+= varmod-unique 336TESTS+= varname 337TESTS+= varname-dollar 338TESTS+= varname-dot-alltargets 339TESTS+= varname-dot-curdir 340TESTS+= varname-dot-includes 341TESTS+= varname-dot-includedfromdir 342TESTS+= varname-dot-includedfromfile 343TESTS+= varname-dot-libs 344TESTS+= varname-dot-make-dependfile 345TESTS+= varname-dot-make-expand_variables 346TESTS+= varname-dot-make-exported 347TESTS+= varname-dot-make-jobs 348TESTS+= varname-dot-make-jobs-prefix 349TESTS+= varname-dot-make-level 350TESTS+= varname-dot-make-makefile_preference 351TESTS+= varname-dot-make-makefiles 352TESTS+= varname-dot-make-meta-bailiwick 353TESTS+= varname-dot-make-meta-created 354TESTS+= varname-dot-make-meta-files 355TESTS+= varname-dot-make-meta-ignore_filter 356TESTS+= varname-dot-make-meta-ignore_paths 357TESTS+= varname-dot-make-meta-ignore_patterns 358TESTS+= varname-dot-make-meta-prefix 359TESTS+= varname-dot-make-mode 360TESTS+= varname-dot-make-path_filemon 361TESTS+= varname-dot-make-pid 362TESTS+= varname-dot-make-ppid 363TESTS+= varname-dot-make-save_dollars 364TESTS+= varname-dot-makeoverrides 365TESTS+= varname-dot-newline 366TESTS+= varname-dot-objdir 367TESTS+= varname-dot-parsedir 368TESTS+= varname-dot-parsefile 369TESTS+= varname-dot-path 370TESTS+= varname-dot-shell 371TESTS+= varname-dot-targets 372TESTS+= varname-empty 373TESTS+= varname-make 374TESTS+= varname-make_print_var_on_error 375TESTS+= varname-make_print_var_on_error-jobs 376TESTS+= varname-makefile 377TESTS+= varname-makeflags 378TESTS+= varname-pwd 379TESTS+= varname-vpath 380TESTS+= varparse-dynamic 381TESTS+= varparse-errors 382TESTS+= varparse-mod 383TESTS+= varparse-undef-partial 384TESTS+= varquote 385 386# Ideas for more tests: 387# char-0020-space.mk 388# char-005C-backslash.mk 389# escape-cond-str.mk 390# escape-cond-func-arg.mk 391# escape-cond-func-arg.mk 392# escape-varmod.mk 393# escape-varmod-define.mk 394# escape-varmod-match.mk 395# escape-varname.mk 396# escape-varassign-varname.mk 397# escape-varassign-varname-cmdline.mk 398# escape-varassign-value.mk 399# escape-varassign-value-cmdline.mk 400# escape-dependency-source.mk 401# escape-dependency-target.mk 402# escape-for-varname.mk 403# escape-for-item.mk 404# posix-*.mk (see posix.mk and posix1.mk) 405 406.if ${.OBJDIR} != ${.CURDIR} 407RO_OBJDIR:= ${.OBJDIR}/roobj 408.else 409RO_OBJDIR:= ${TMPDIR:U/tmp}/roobj 410.endif 411# Additional environment variables for some of the tests. 412# The base environment is -i PATH="$PATH". 413ENV.depsrc-optional+= TZ=UTC 414ENV.envfirst= FROM_ENV=value-from-env 415ENV.objdir-writable+= RO_OBJDIR=${RO_OBJDIR} 416ENV.varmisc= FROM_ENV=env 417ENV.varmisc+= FROM_ENV_BEFORE=env 418ENV.varmisc+= FROM_ENV_AFTER=env 419ENV.varmod-localtime+= TZ=Europe/Berlin 420ENV.varname-vpath+= VPATH=varname-vpath.dir:varname-vpath.dir2 421 422# Override make flags for some of the tests; default is -k. 423# If possible, write ".MAKEFLAGS: -dv" in the test .mk file instead of 424# settings FLAGS.test=-dv here, since that is closer to the test code. 425FLAGS.cond-func-make= via-cmdline 426FLAGS.directive-ifmake= first second 427FLAGS.doterror= # none, especially not -k 428FLAGS.varname-empty= -dv '$${:U}=cmdline-u' '=cmdline-plain' 429 430# Some tests need extra postprocessing. 431SED_CMDS.export= \ 432 -e '/^[^=_A-Za-z0-9]*=/d' 433# these all share the same requirement 434.for t in export-all export-env 435SED_CMDS.$t= ${SED_CMDS.export} 436.endfor 437SED_CMDS.directive-export-gmake= \ 438 ${:D dash is a pain } \ 439 -e /non-zero/d 440SED_CMDS.job-output-long-lines= \ 441 ${:D Job separators on their own line are ok. } \ 442 -e '/^--- job-[ab] ---$$/d' \ 443 ${:D Plain output lines are ok as well. } \ 444 ${:D They may come in multiples of 1024 or as 10000. } \ 445 -e '/^aa*$$/d' \ 446 -e '/^bb*$$/d' \ 447 ${:D The following lines should rather not occur since the job } \ 448 ${:D marker should always be at the beginning of the line. } \ 449 -e '/^aa*--- job-b ---$$/d' \ 450 -e '/^bb*--- job-a ---$$/d' 451SED_CMDS.objdir-writable= -e 's,${RO_OBJDIR},OBJDIR/roobj,g' 452SED_CMDS.opt-debug-graph1= \ 453 -e 's,${.CURDIR},CURDIR,' 454SED_CMDS.opt-debug-graph1+= \ 455 -e '/Global Variables:/,/Suffixes:/d' 456SED_CMDS.sh-dots= -e 's,^.*\.\.\.:.*,<normalized: ...: not found>,' 457SED_CMDS.opt-debug-jobs= -e 's,([0-9][0-9]*),(<pid>),' 458SED_CMDS.opt-debug-jobs+= -e 's,pid [0-9][0-9]*,pid <pid>,' 459SED_CMDS.opt-debug-jobs+= -e 's,Process [0-9][0-9]*,Process <pid>,' 460SED_CMDS.opt-debug-jobs+= -e 's,JobFinish: [0-9][0-9]*,JobFinish: <pid>,' 461# The "-q" may be there or not, see jobs.c, variable shells. 462SED_CMDS.opt-debug-jobs+= -e 's,^\(.Command: sh\) -q,\1,' 463SED_CMDS.var-op-shell+= -e 's,^${.SHELL:T}: ,,' 464SED_CMDS.var-op-shell+= -e '/command/{ s,^[1-9]: ,,;s,No such.*,not found,; }' 465SED_CMDS.vardebug= \ 466 ${:D canonicalize .SHELL } \ 467 -e 's,${.SHELL},</path/to/shell>,' 468SED_CMDS.varmod-subst-regex+= \ 469 -e 's,\(Regex compilation error:\).*,\1 (details omitted),' 470SED_CMDS.varmod-edge+= -e 's, line [0-9]*:, line omitted:,' 471SED_CMDS.varname-dot-parsedir= -e '/in some cases/ s,^make: "[^"]*,make: "<normalized>,' 472SED_CMDS.varname-dot-parsefile= -e '/in some cases/ s,^make: "[^"]*,make: "<normalized>,' 473SED_CMDS.varname-dot-shell= -e 's, = /[^ ]*, = (details omitted),g' 474SED_CMDS.varname-dot-shell+= -e 's,"/[^" ]*","(details omitted)",g' 475SED_CMDS.varname-dot-shell+= -e 's,\[/[^] ]*\],[(details omitted)],g' 476 477# Some tests need an additional round of postprocessing. 478POSTPROC.deptgt-suffixes= \ 479 ${TOOL_SED} -n -e '/^\#\*\*\* Suffixes/,/^\#\*/p' 480POSTPROC.gnode-submake= awk '/Input graph/, /^$$/' 481POSTPROC.varname-empty= ${TOOL_SED} -n -e '/^Var_Set/p' -e '/^out:/p' 482 483# Some tests reuse other tests, which makes them unnecessarily fragile. 484export-all.rawout: export.mk 485unexport.rawout: export.mk 486unexport-env.rawout: export.mk 487 488# End of the configuration section. 489 490.MAIN: all 491 492.-include "Makefile.inc" 493.-include "Makefile.config" 494 495UNIT_TESTS:= ${.PARSEDIR} 496.PATH: ${UNIT_TESTS} 497 498.if ${USE_ABSOLUTE_TESTNAMES:Uno} == yes 499OUTFILES= ${TESTS:@test@${.CURDIR:tA}/${test}.out@} 500.else 501OUTFILES= ${TESTS:=.out} 502.endif 503 504all: ${OUTFILES} 505 506CLEANFILES= *.rawout *.out *.status *.tmp *.core *.tmp 507CLEANFILES+= obj*.[och] lib*.a # posix1.mk 508CLEANFILES+= issue* .[ab]* # suffixes.mk 509CLEANDIRS= dir dummy # posix1.mk 510 511clean: 512 rm -f ${CLEANFILES} 513 rm -rf ${CLEANDIRS} 514 515TEST_MAKE?= ${.MAKE} 516TOOL_SED?= sed 517TOOL_TR?= tr 518TOOL_DIFF?= diff 519DIFF_FLAGS?= -u 520 521.if defined(.PARSEDIR) 522# ensure consistent results from sort(1) 523LC_ALL= C 524LANG= C 525.export LANG LC_ALL 526.endif 527 528.if ${.MAKE.MODE:Unormal:Mmeta} != "" 529# we don't need the noise 530_MKMSG_TEST= : 531.endif 532 533MAKE_TEST_ENV?= MALLOC_OPTIONS="JA" # for jemalloc 534 535# Each test is run in a sub-make, to keep the tests for interfering with 536# each other, and because they use different environment variables and 537# command line options. 538.SUFFIXES: .mk .rawout .out 539.mk.rawout: 540 @${_MKMSG_TEST:Uecho '# test '} ${.PREFIX} 541 @set -eu; \ 542 cd ${.OBJDIR}; \ 543 env -i PATH="$$PATH" ${MAKE_TEST_ENV} ${ENV.${.PREFIX:T}} \ 544 ${TEST_MAKE} \ 545 -r -C ${.CURDIR} -f ${.IMPSRC} \ 546 ${FLAGS.${.PREFIX:T}:U-k} \ 547 > ${.TARGET}.tmp 2>&1 \ 548 && status=$$? || status=$$?; \ 549 echo $$status > ${.TARGET:R}.status 550 @mv ${.TARGET}.tmp ${.TARGET} 551 552# Postprocess the test output so that the results can be compared. 553# 554# always pretend .MAKE was called 'make' 555_SED_CMDS+= -e 's,^${TEST_MAKE:T:S,.,\\.,g}[][0-9]*:,make:,' 556_SED_CMDS+= -e 's,${TEST_MAKE:S,.,\\.,g},make,' 557_SED_CMDS+= -e 's,${TEST_MAKE:T:S,.,\\.,g}[][0-9]* warning,make warning,' 558_SED_CMDS+= -e 's,^usage: ${TEST_MAKE:T:S,.,\\.,g} ,usage: make ,' 559# replace anything after 'stopped in' with unit-tests 560_SED_CMDS+= -e '/stopped/s, /.*, unit-tests,' 561# strip ${.CURDIR}/ from the output 562_SED_CMDS+= -e 's,${.CURDIR:S,.,\\.,g}/,,g' 563_SED_CMDS+= -e 's,${UNIT_TESTS:S,.,\\.,g}/,,g' 564 565.rawout.out: 566 @${TOOL_SED} ${_SED_CMDS} ${SED_CMDS.${.PREFIX:T}} \ 567 < ${.IMPSRC} > ${.TARGET}.tmp1 568 @${POSTPROC.${.PREFIX:T}:Ucat} < ${.TARGET}.tmp1 > ${.TARGET}.tmp2 569 @rm ${.TARGET}.tmp1 570 @echo "exit status `cat ${.TARGET:R}.status`" >> ${.TARGET}.tmp2 571 @mv ${.TARGET}.tmp2 ${.TARGET} 572 573# Compare all output files 574test: ${OUTFILES} .PHONY 575 @failed= ; \ 576 for test in ${TESTS}; do \ 577 ${TOOL_DIFF} ${DIFF_FLAGS} ${UNIT_TESTS}/$${test}.exp $${test}.out \ 578 || failed="$${failed}$${failed:+ }$${test}" ; \ 579 done ; \ 580 if [ -n "$${failed}" ]; then \ 581 echo "Failed tests: $${failed}" ; false ; \ 582 else \ 583 echo "All tests passed" ; \ 584 fi 585 586accept: 587 @for test in ${TESTS}; do \ 588 cmp -s ${UNIT_TESTS}/$${test}.exp $${test}.out \ 589 || { echo "Replacing $${test}.exp" ; \ 590 cp $${test}.out ${UNIT_TESTS}/$${test}.exp ; } \ 591 done 592 593.if exists(${TEST_MAKE}) 594${TESTS:=.rawout}: ${TEST_MAKE} 595# in meta mode, we *know* if a target script is impacted 596# by a makefile change. 597.if ${.MAKE.MODE:Unormal:Mmeta} == "" 598${TESTS:=.rawout}: ${.PARSEDIR}/Makefile 599.endif 600.endif 601 602.-include <obj.mk> 603