deptgt-posix.mk revision 1.1 1 # $NetBSD: deptgt-posix.mk,v 1.1 2022/04/18 15:06:28 rillig 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
18 TMPDIR?= /tmp/make.test.deptgt-posix
19 SYSDIR= ${TMPDIR}/sysdir
20 MAIN_MK= ${TMPDIR}/main.mk
21 INCLUDED_MK= ${TMPDIR}/included.mk
22
23 all: .PHONY
24 .SILENT:
25
26 set-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
34 check-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
44 check-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
54 check-not-seen-sys-mk: .USE
55 printf '%s\n' >> ${MAIN_MK} \
56 '.if defined(SEEN_SYS_MK)' \
57 '. error' \
58 '.endif'
59
60 run: .USE
61 (cd "${TMPDIR}" && MAKEFLAGS=${MAKEFLAGS.${.TARGET}:Q} ${MAKE} \
62 -m "${SYSDIR}" -f ${MAIN_MK:T})
63 rm -rf ${TMPDIR}
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.
67 all: after-for
68 after-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.
78 all: after-if
79 after-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.
89 all: in-included-file
90 in-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.
96 all: in-first-line
97 in-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.
103 all: after-comment-lines
104 after-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.
112 MAKEFLAGS.no-builtins= -r
113 all: no-builtins
114 no-builtins: .PHONY set-up-sysdir check-is-posix check-not-seen-sys-mk run
115 printf '%s\n' > ${MAIN_MK} \
116 '.POSIX:'
117