xref: /freebsd/contrib/bmake/unit-tests/cmdline.mk (revision 4d846d26)
1# $NetBSD: cmdline.mk,v 1.4 2022/06/10 18:58:07 rillig Exp $
2#
3# Tests for command line parsing and related special variables.
4
5TMPBASE?=	${TMPDIR:U/tmp/uid${.MAKE.UID}}
6SUB1=		a7b41170-53f8-4cc2-bc5c-e4c3dd93ec45	# just a random UUID
7SUB2=		6a8899d2-d227-4b55-9b6b-f3c8eeb83fd5	# just a random UUID
8MAKE_CMD=	env TMPBASE=${TMPBASE}/${SUB1} ${.MAKE} -f ${MAKEFILE} -r
9DIR2=		${TMPBASE}/${SUB2}
10DIR12=		${TMPBASE}/${SUB1}/${SUB2}
11
12all: prepare-dirs
13all: makeobjdir-direct makeobjdir-indirect
14all: space-and-comment
15
16prepare-dirs:
17	@rm -rf ${DIR2} ${DIR12}
18	@mkdir -p ${DIR2} ${DIR12}
19
20# The .OBJDIR can be set via the MAKEOBJDIR command line variable.
21# It must be a command line variable; an environment variable would not work.
22makeobjdir-direct:
23	@echo $@:
24	@${MAKE_CMD} MAKEOBJDIR=${DIR2} show-objdir
25
26# The .OBJDIR can be set via the MAKEOBJDIR command line variable,
27# and that variable could even contain the usual modifiers.
28# Since the .OBJDIR=MAKEOBJDIR assignment happens very early,
29# the SUB2 variable in the modifier is not defined yet and is therefore empty.
30# The SUB1 in the resulting path comes from the environment variable TMPBASE,
31# see MAKE_CMD.
32makeobjdir-indirect:
33	@echo $@:
34	@${MAKE_CMD} MAKEOBJDIR='$${TMPBASE}/$${SUB2}' show-objdir
35
36show-objdir:
37	@echo $@: ${.OBJDIR:Q}
38
39
40# Variable assignments in the command line are handled differently from
41# variable assignments in makefiles.  In the command line, trailing whitespace
42# is preserved, and the '#' does not start a comment.  This is because the
43# low-level parsing from ParseRawLine does not take place.
44#
45# Preserving '#' and trailing whitespace has the benefit that when passing
46# such values to sub-makes via MAKEFLAGS, no special encoding is needed.
47# Leading whitespace in the variable value is discarded though, which makes
48# the behavior inconsistent.
49space-and-comment: .PHONY
50	@echo $@:
51
52	@env -i \
53	    ${MAKE} -r -f /dev/null ' VAR= value # no comment ' -v VAR \
54	| sed 's,$$,$$,'
55
56	@env -i MAKEFLAGS="' VAR= value # no comment '" \
57	    ${MAKE} -r -f /dev/null -v VAR \
58	| sed 's,$$,$$,'
59