xref: /freebsd/contrib/bmake/unit-tests/deptgt-posix.mk (revision cc1a53bc1aea0675d64e9547cdca241612906592)
1# $NetBSD: deptgt-posix.mk,v 1.2 2022/04/18 15:59:39 sjg Exp $
2#
3# Tests for the special target '.POSIX', which enables POSIX mode.
4#
5# As of 2022-04-18, this only means that the variable '%POSIX' is defined and
6# that the variables and rules specified by POSIX replace the default ones.
7# This is done by loading <posix.mk>, if available.  That file is not included
8# in NetBSD, but only in the bmake distribution.  As of 2022-04-18, POSIX
9# support is not complete.
10#
11# Implementation node: this test needs to be isolated from the usual test
12# to prevent unit-tests/posix.mk from interfering with the posix.mk from the
13# system directory that this test uses.
14#
15# See also:
16#	https://pubs.opengroup.org/onlinepubs/9699919799/utilities/make.html
17
18TESTTMP=	${TMPDIR:U/tmp}/make.test.deptgt-posix
19SYSDIR=		${TESTTMP}/sysdir
20MAIN_MK=	${TESTTMP}/main.mk
21INCLUDED_MK=	${TESTTMP}/included.mk
22
23all: .PHONY
24.SILENT:
25
26set-up-sysdir: .USEBEFORE
27	mkdir -p ${SYSDIR}
28	printf '%s\n' > ${SYSDIR}/sys.mk \
29	    'CC=sys-cc' \
30	    'SEEN_SYS_MK=yes'
31	printf '%s\n' > ${SYSDIR}/posix.mk \
32	    'CC=posix-cc'
33
34check-is-posix: .USE
35	printf '%s\n' >> ${MAIN_MK} \
36		'.if $${CC} != "posix-cc"' \
37		'.  error' \
38		'.endif' \
39		'.if $${%POSIX} != "1003.2"' \
40		'.  error' \
41		'.endif' \
42		'all: .PHONY'
43
44check-not-posix: .USE
45	printf '%s\n' >> ${MAIN_MK} \
46		'.if $${CC} != "sys-cc"' \
47		'.  error' \
48		'.endif' \
49		'.if defined(%POSIX)' \
50		'.  error' \
51		'.endif' \
52		'all: .PHONY'
53
54check-not-seen-sys-mk: .USE
55	printf '%s\n' >> ${MAIN_MK} \
56	    '.if defined(SEEN_SYS_MK)' \
57	    '.  error' \
58	    '.endif'
59
60run: .USE
61	(cd "${TESTTMP}" && MAKEFLAGS=${MAKEFLAGS.${.TARGET}:Q} ${MAKE} \
62	    -m "${SYSDIR}" -f ${MAIN_MK:T})
63	rm -rf ${TESTTMP}
64
65# If the main makefile has a '.for' loop as its first non-comment line, a
66# strict reading of POSIX 2018 makes the makefile non-conforming.
67all: after-for
68after-for: .PHONY set-up-sysdir check-not-posix run
69	printf '%s\n' > ${MAIN_MK} \
70	    '# comment' \
71	    '' \
72	    '.for i in once' \
73	    '.POSIX:' \
74	    '.endfor'
75
76# If the main makefile has an '.if' conditional as its first non-comment line,
77# a strict reading of POSIX 2018 makes the makefile non-conforming.
78all: after-if
79after-if: .PHONY set-up-sysdir check-not-posix run
80	printf '%s\n' > ${MAIN_MK} \
81	    '# comment' \
82	    '' \
83	    '.if 1' \
84	    '.POSIX:' \
85	    '.endif'
86
87# If the main makefile first includes another makefile and that included
88# makefile tries to switch to POSIX mode, that's too late.
89all: in-included-file
90in-included-file: .PHONY set-up-sysdir check-not-posix run
91	printf 'include included.mk\n' > ${MAIN_MK}
92	printf '.POSIX:\n' > ${INCLUDED_MK}
93
94# If the main makefile switches to POSIX mode in its very first line, before
95# and comment lines or empty lines, that works.
96all: in-first-line
97in-first-line: .PHONY set-up-sysdir check-is-posix run
98	printf '%s\n' > ${MAIN_MK} \
99	    '.POSIX:'
100
101# The only allowed lines before switching to POSIX mode are comment lines.
102# POSIX defines that empty and blank lines are called comment lines as well.
103all: after-comment-lines
104after-comment-lines: .PHONY set-up-sysdir check-is-posix run
105	printf '%s\n' > ${MAIN_MK} \
106	    '# comment' \
107	    '' \
108	    '.POSIX:'
109
110# Running make with the option '-r' skips the builtin rules from <sys.mk>.
111# In that mode, '.POSIX:' just loads <posix.mk>, which works as well.
112MAKEFLAGS.no-builtins=	-r
113all: no-builtins
114no-builtins: .PHONY set-up-sysdir check-is-posix check-not-seen-sys-mk run
115	printf '%s\n' > ${MAIN_MK} \
116	    '.POSIX:'
117