xref: /freebsd/contrib/bmake/unit-tests/opt-define.mk (revision 954401e68e797868ab04a0147b94849feefbb199)
1*954401e6SSimon J. Gerraty# $NetBSD: opt-define.mk,v 1.4 2022/06/12 14:27:06 rillig Exp $
22c3632d1SSimon J. Gerraty#
39f45a3c8SSimon J. Gerraty# Tests for the -D command line option, which defines global variables to the
49f45a3c8SSimon J. Gerraty# value 1, like in the C preprocessor.
52c3632d1SSimon J. Gerraty
69f45a3c8SSimon J. Gerraty.MAKEFLAGS: -DVAR
72c3632d1SSimon J. Gerraty
89f45a3c8SSimon J. Gerraty# The variable has the exact value "1", not "1.0".
99f45a3c8SSimon J. Gerraty.if ${VAR} != "1"
109f45a3c8SSimon J. Gerraty.  error
119f45a3c8SSimon J. Gerraty.endif
129f45a3c8SSimon J. Gerraty
139f45a3c8SSimon J. Gerraty# The variable can be overwritten by assigning another value to it.  This
149f45a3c8SSimon J. Gerraty# would not be possible if the variable had been specified on the command line
159f45a3c8SSimon J. Gerraty# as 'VAR=1' instead of '-DVAR'.
169f45a3c8SSimon J. GerratyVAR=		overwritten
179f45a3c8SSimon J. Gerraty.if ${VAR} != "overwritten"
189f45a3c8SSimon J. Gerraty.  error
199f45a3c8SSimon J. Gerraty.endif
209f45a3c8SSimon J. Gerraty
219f45a3c8SSimon J. Gerraty# The variable can be undefined.  If the variable had been defined in the
22*954401e6SSimon J. Gerraty# "Internal" or in the "Command" scope instead, undefining it would have no
23*954401e6SSimon J. Gerraty# effect.
249f45a3c8SSimon J. Gerraty.undef VAR
259f45a3c8SSimon J. Gerraty.if defined(VAR)
269f45a3c8SSimon J. Gerraty.  error
279f45a3c8SSimon J. Gerraty.endif
289f45a3c8SSimon J. Gerraty
29*954401e6SSimon J. Gerraty# The C preprocessor allows to define a macro with a specific value.  Make
30*954401e6SSimon J. Gerraty# behaves differently, it defines a variable with the name 'VAR=value' and the
31*954401e6SSimon J. Gerraty# value 1.
32*954401e6SSimon J. Gerraty.MAKEFLAGS: -DVAR=value
33*954401e6SSimon J. Gerraty.if defined(VAR)
34*954401e6SSimon J. Gerraty.  error
35*954401e6SSimon J. Gerraty.endif
36*954401e6SSimon J. Gerraty.if ${VAR=value} != "1"
37*954401e6SSimon J. Gerraty.  error
38*954401e6SSimon J. Gerraty.endif
39*954401e6SSimon J. Gerraty
409f45a3c8SSimon J. Gerratyall: .PHONY
41