xref: /freebsd/contrib/bmake/unit-tests/opt-file.mk (revision 548bfc56eb0b2cefa0fb8dc2478240bfef610309)
1*548bfc56SSimon J. Gerraty# $NetBSD: opt-file.mk,v 1.16 2024/04/01 12:26:02 rillig Exp $
22c3632d1SSimon J. Gerraty#
312904384SSimon J. Gerraty# Tests for the -f command line option, which adds a makefile to the list of
412904384SSimon J. Gerraty# files that are parsed.
52c3632d1SSimon J. Gerraty
62c3632d1SSimon J. Gerraty# TODO: Implementation
72c3632d1SSimon J. Gerraty
806b9b3e0SSimon J. Gerratyall: .PHONY
906b9b3e0SSimon J. Gerratyall: file-ending-in-backslash
1006b9b3e0SSimon J. Gerratyall: file-ending-in-backslash-mmap
1106b9b3e0SSimon J. Gerratyall: line-with-trailing-whitespace
1206b9b3e0SSimon J. Gerratyall: file-containing-null-byte
1306b9b3e0SSimon J. Gerraty
1412904384SSimon J. Gerraty# When the filename is '-', the input comes from stdin.  This is unusual but
1512904384SSimon J. Gerraty# possible.
1606b9b3e0SSimon J. Gerraty#
1706b9b3e0SSimon J. Gerraty# In the unlikely case where a file ends in a backslash instead of a newline,
181d3f2ddcSSimon J. Gerraty# that backslash is trimmed.  See ReadLowLevelLine.
1906b9b3e0SSimon J. Gerraty#
2006b9b3e0SSimon J. Gerraty# make-2014.01.01.00.00.00 invoked undefined behavior, reading text from
2106b9b3e0SSimon J. Gerraty# outside of the file buffer.
2206b9b3e0SSimon J. Gerraty#
2306b9b3e0SSimon J. Gerraty#	printf '%s' 'VAR=value\' \
2412904384SSimon J. Gerraty#	| MALLOC_OPTIONS="JA" \
2512904384SSimon J. Gerraty#	  MALLOC_CONF="junk:true" \
2612904384SSimon J. Gerraty#	  make-2014.01.01.00.00.00 -r -f - -V VAR -dA 2>&1 \
2706b9b3e0SSimon J. Gerraty#	| less
2806b9b3e0SSimon J. Gerraty#
2906b9b3e0SSimon J. Gerraty# The debug output shows how make happily uses freshly allocated memory (the
3006b9b3e0SSimon J. Gerraty# <A5>) and already freed memory ('Z').
3106b9b3e0SSimon J. Gerraty#
3206b9b3e0SSimon J. Gerraty#	ParseReadLine (1): 'VAR=value\<A5><A5><A5><A5><A5><A5>'
3306b9b3e0SSimon J. Gerraty#	Global:VAR = value\<A5><A5><A5><A5><A5><A5>value\<A5><A5><A5><A5><A5><A5>
3406b9b3e0SSimon J. Gerraty#	ParseReadLine (2): 'alue\<A5><A5><A5><A5><A5><A5>'
35b0c40a00SSimon J. Gerraty#	ParseDependency(alue\<A5><A5><A5><A5><A5><A5>)
3606b9b3e0SSimon J. Gerraty#	make-2014.01.01.00.00.00: "(stdin)" line 2: Need an operator
3706b9b3e0SSimon J. Gerraty#	ParseReadLine (3): '<A5><A5><A5>ZZZZZZZZZZZZZZZZ'
38b0c40a00SSimon J. Gerraty#	ParseDependency(<A5><A5><A5>ZZZZZZZZZZZZZZZZ)
3906b9b3e0SSimon J. Gerraty#
4006b9b3e0SSimon J. Gerratyfile-ending-in-backslash: .PHONY
4106b9b3e0SSimon J. Gerraty	@printf '%s' 'VAR=value\' \
4206b9b3e0SSimon J. Gerraty	| ${MAKE} -r -f - -V VAR
4306b9b3e0SSimon J. Gerraty
4406b9b3e0SSimon J. Gerraty# Between parse.c 1.170 from 2010-12-25 and parse.c 1.511 from 2020-12-22,
4506b9b3e0SSimon J. Gerraty# there was an out-of-bounds write in ParseGetLine, where line_end pointed at
4606b9b3e0SSimon J. Gerraty# the end of the allocated buffer, in the special case where loadedfile_mmap
4706b9b3e0SSimon J. Gerraty# had not added the final newline character.
4806b9b3e0SSimon J. Gerratyfile-ending-in-backslash-mmap: .PHONY
4906b9b3e0SSimon J. Gerraty	@printf '%s' 'VAR=value\' > opt-file-backslash
5006b9b3e0SSimon J. Gerraty	@${MAKE} -r -f opt-file-backslash -V VAR
5106b9b3e0SSimon J. Gerraty	@rm opt-file-backslash
5206b9b3e0SSimon J. Gerraty
5306b9b3e0SSimon J. Gerraty# Since parse.c 1.511 from 2020-12-22, an assertion in ParseGetLine failed
5406b9b3e0SSimon J. Gerraty# for lines that contained trailing whitespace.  Worked around in parse.c
551d3f2ddcSSimon J. Gerraty# 1.513, properly fixed in parse.c 1.514 from 2020-12-22.
5606b9b3e0SSimon J. Gerratyline-with-trailing-whitespace: .PHONY
5706b9b3e0SSimon J. Gerraty	@printf '%s' 'VAR=$@ ' > opt-file-trailing-whitespace
5806b9b3e0SSimon J. Gerraty	@${MAKE} -r -f opt-file-trailing-whitespace -V VAR
5906b9b3e0SSimon J. Gerraty	@rm opt-file-trailing-whitespace
6006b9b3e0SSimon J. Gerraty
6106b9b3e0SSimon J. Gerraty# If a makefile contains null bytes, it is an error.  Throughout the history
6206b9b3e0SSimon J. Gerraty# of make, the behavior has changed several times, sometimes intentionally,
6306b9b3e0SSimon J. Gerraty# sometimes by accident.
6406b9b3e0SSimon J. Gerraty#
6506b9b3e0SSimon J. Gerraty#	echo 'VAR=value' | tr 'l' '\0' > zero-byte.in
6606b9b3e0SSimon J. Gerraty#	printf '%s\n' 'all:' ': VAR=${VAR:Q}' >> zero-byte.in
6706b9b3e0SSimon J. Gerraty#
6806b9b3e0SSimon J. Gerraty#	for year in $(seq 2003 2020); do
6906b9b3e0SSimon J. Gerraty#	  echo $year:
7006b9b3e0SSimon J. Gerraty#	  make-$year.01.01.00.00.00 -r -f zero-byte.in
7106b9b3e0SSimon J. Gerraty#	  echo "exit status $?"
7206b9b3e0SSimon J. Gerraty#	  echo
7306b9b3e0SSimon J. Gerraty#	done 2>&1 \
7406b9b3e0SSimon J. Gerraty#	| sed "s,$PWD/,.,"
7506b9b3e0SSimon J. Gerraty#
7606b9b3e0SSimon J. Gerraty# This program generated the following output:
7706b9b3e0SSimon J. Gerraty#
7806b9b3e0SSimon J. Gerraty#	2003 to 2007:
7906b9b3e0SSimon J. Gerraty#	exit status 0
8006b9b3e0SSimon J. Gerraty#
8106b9b3e0SSimon J. Gerraty#	2008 to 2010:
82*548bfc56SSimon J. Gerraty#	make: "(stdin)" line 1: Zero byte read from file
8306b9b3e0SSimon J. Gerraty#	make: Fatal errors encountered -- cannot continue
8406b9b3e0SSimon J. Gerraty#
8506b9b3e0SSimon J. Gerraty#	make: stopped in .
8606b9b3e0SSimon J. Gerraty#	exit status 1
8706b9b3e0SSimon J. Gerraty#
8806b9b3e0SSimon J. Gerraty#	2011 to 2013:
8906b9b3e0SSimon J. Gerraty#	make: no target to make.
9006b9b3e0SSimon J. Gerraty#
9106b9b3e0SSimon J. Gerraty#	make: stopped in .
9206b9b3e0SSimon J. Gerraty#	exit status 2
9306b9b3e0SSimon J. Gerraty#
9406b9b3e0SSimon J. Gerraty#	2014 to 2020-12-06:
95*548bfc56SSimon J. Gerraty#	make: "(stdin)" line 1: warning: Zero byte read from file, skipping rest of line.
9606b9b3e0SSimon J. Gerraty#	exit status 0
9706b9b3e0SSimon J. Gerraty#
9806b9b3e0SSimon J. Gerraty#	Since 2020-12-07:
99*548bfc56SSimon J. Gerraty#	make: "(stdin)" line 1: Zero byte read from file
10006b9b3e0SSimon J. Gerraty#	make: Fatal errors encountered -- cannot continue
10106b9b3e0SSimon J. Gerraty#	make: stopped in .
10206b9b3e0SSimon J. Gerraty#	exit status 1
103*548bfc56SSimon J. Gerraty#
104*548bfc56SSimon J. Gerraty#	Since 2024-04-01:
105*548bfc56SSimon J. Gerraty#	make: "(stdin)" line 1: Zero byte read from file
106*548bfc56SSimon J. Gerraty#	*** Error code 2 (continuing)
10706b9b3e0SSimon J. Gerratyfile-containing-null-byte: .PHONY
10806b9b3e0SSimon J. Gerraty	@printf '%s\n' 'VAR=value' 'VAR2=VALUE2' \
10906b9b3e0SSimon J. Gerraty	| tr 'l' '\0' \
11006b9b3e0SSimon J. Gerraty	| ${MAKE} -r -f - -V VAR -V VAR2
11106b9b3e0SSimon J. Gerraty
1122c3632d1SSimon J. Gerratyall:
11306b9b3e0SSimon J. Gerraty	: Making ${.TARGET}
114