xref: /freebsd/contrib/bmake/unit-tests/comment.mk (revision 5ca8e32633c4ffbbcd6762e5888b6a4ba0708c6c)
1# $NetBSD: comment.mk,v 1.6 2023/11/19 21:47:52 rillig Exp $
2#
3# Demonstrate how comments are written in makefiles.
4
5# This is a comment.
6
7#\
8This is a multiline comment.
9
10# Another multiline comment \
11that \
12goes \
13on and on.
14
15 # Comments can be indented with spaces, but that is rather unusual.
16
17	# Comments can be indented with a tab.
18	# Since parse.c 1.127 from 2007-01-01, these are not shell commands,
19	# they are just makefile comments.  Before that commit, these comments
20	# triggered the error message "Unassociated shell command".
21
22.if 1			# There can be comments after conditions.
23.endif			# And after the closing directive.
24
25VAR=			# This comment makes the variable value empty.
26			# ParseRawLine removes any whitespace before the
27			# comment.
28.if ${VAR} != ""
29.  error
30.endif
31
32# The comment does not need to start at the beginning of a word (as in the
33# shell), it can start anywhere.
34VAR=# defined but empty
35
36# The space before the comment is always trimmed.
37VAR=	value
38.if ${VAR} != "value"
39.  error
40.endif
41
42# This comment ends with 2 backslashes.  An even number of backslashes does
43# not count as a line continuation, therefore the variable assignment that
44# follows is actively interpreted. \\
45VAR=	not part of the comment
46.if ${VAR} != "not part of the comment"
47.  error
48.endif
49
50# To escape a comment sign, precede it with a backslash.
51VAR=	\#		# Both in the assignment.
52.if ${VAR} != "\#"	# And in the comparison.
53.  error
54.endif
55
56# Since 2012-03-24 the variable modifier :[#] does not need to be escaped.
57# To keep the parsing code simple, any "[#" does not start a comment, even
58# outside of an expression.
59WORDS=	${VAR:[#]} [#
60.if ${WORDS} != "1 [#"
61.  error
62.endif
63
64# An odd number of backslashes makes a line continuation, \\\
65no matter if it is 3 or 5 \\\\\
66or 9 backslashes. \\\\\\\\\
67This is the last line of the comment.
68VAR=	no comment anymore
69.if ${VAR} != "no comment anymore"
70.  error
71.endif
72
73all:
74# In the commands associated with a target, the '#' does not start a makefile
75# comment.  The '#' is just passed to the shell, like any ordinary character.
76	echo This is a shell comment: # comment
77# If the '#' were to start a makefile comment, the following shell command
78# would have unbalanced quotes.
79	echo This is not a shell comment: '# comment'
80	@echo A shell comment can#not start in the middle of a word.
81