1281760Ssjg# $NetBSD: posix1.mk,v 1.3 2014/08/30 22:21:08 sjg Exp $
2281760Ssjg
3281760Ssjg# Keep the default suffixes from interfering, just in case.
4281760Ssjg.SUFFIXES:
5281760Ssjg
6281760Ssjgall:	line-continuations suffix-substitution localvars
7281760Ssjg
8281760Ssjg# we need to clean for repeatable results
9281760Ssjg.BEGIN: clean
10281760Ssjgclean:
11281760Ssjg	@rm -f lib.a dir/* dummy obj*
12281760Ssjg
13281760Ssjg#
14281760Ssjg# Line continuations
15281760Ssjg#
16281760Ssjg
17281760Ssjg# Escaped newlines and leading whitespace from the next line are replaced
18281760Ssjg# with single space, except in commands, where the escape and the newline
19281760Ssjg# are retained, but a single leading tab (if any) from the next line is
20281760Ssjg# removed. (PR 49085)
21281760Ssjg# Expect:
22281760Ssjg# ${VAR} = "foo  bar baz"
23281760Ssjg# a
24281760Ssjg# b
25281760Ssjg# c
26281760SsjgVAR = foo\
27281760Ssjg\
28281760Ssjg	  bar\
29281760Ssjg baz
30281760Ssjg
31281760Ssjgline-continuations:
32281760Ssjg	@echo '$${VAR} = "${VAR}"'
33281760Ssjg	@echo 'aXbXc' | sed -e 's/X/\
34281760Ssjg	/g'
35281760Ssjg
36281760Ssjg
37281760Ssjg#
38281760Ssjg# Suffix substitution
39281760Ssjg#
40281760Ssjg
41281760Ssjg# The only variable modifier accepted by POSIX.
42281760Ssjg# ${VAR:s1=s2}: replace s1, if found, with s2 at end of each word in
43281760Ssjg# ${VAR}.  s1 and s2 may contain macro expansions.
44281760Ssjg# Expect: foo baR baz, bar baz, foo bar baz, fooadd baradd bazadd
45281760Ssjgsuffix-substitution:
46281760Ssjg	@echo '${VAR:r=R}, ${VAR:foo=}, ${VAR:not_there=wrong}, ${VAR:=add}'
47281760Ssjg
48281760Ssjg
49281760Ssjg#
50281760Ssjg# Local variables: regular forms, D/F forms and suffix substitution.
51281760Ssjg#
52281760Ssjg
53281760Ssjg# In the past substitutions did not work with the D/F forms and those
54281760Ssjg# forms were not available for $?.  (PR 49085)
55281760Ssjg
56281760SsjgARFLAGS = -rcv
57281760Ssjg
58281760Ssjglocalvars: lib.a
59281760Ssjg
60281760Ssjg# $@ = target or archive name	$< = implied source
61281760Ssjg# $* = target without suffix 	$? = sources newer than target
62281760Ssjg# $% = archive member name
63281760SsjgLOCALS = \
64281760Ssjg	"Local variables\n\
65281760Ssjg	\$${@}=\"${@}\" \$${<}=\"${<}\"\n\
66281760Ssjg	\$${*}=\"${*}\" \$${?}=\"${?}\"\n\
67281760Ssjg	\$${%%}=\"${%}\"\n\n"
68281760Ssjg
69281760Ssjg# $XD = directory part of X	$XF = file part of X
70281760Ssjg# X is one of the local variables.
71281760SsjgLOCAL_ALTERNATIVES = \
72281760Ssjg	"Directory and filename parts of local variables\n\
73281760Ssjg	\$${@D}=\"${@D}\" \$${@F}=\"${@F}\"\n\
74281760Ssjg	\$${<D}=\"${<D}\" \$${<F}=\"${<F}\"\n\
75281760Ssjg	\$${*D}=\"${*D}\" \$${*F}=\"${*F}\"\n\
76281760Ssjg	\$${?D}=\"${?D}\" \$${?F}=\"${?F}\"\n\
77281760Ssjg	\$${%%D}=\"${%D}\" \$${%%F}=\"${%F}\"\n\n"
78281760Ssjg
79281760Ssjg# Do all kinds of meaningless substitutions on local variables to see
80281760Ssjg# if they work.  Add, remove and replace things.
81281760SsjgVAR2 = .o
82281760SsjgVAR3 = foo
83281760SsjgLOCAL_SUBSTITUTIONS = \
84281760Ssjg	"Local variable substitutions\n\
85281760Ssjg	\$${@:.o=}=\"${@:.o=}\" \$${<:.c=.C}=\"${<:.c=.C}\"\n\
86281760Ssjg	\$${*:=.h}=\"${*:=.h}\" \$${?:.h=.H}=\"${?:.h=.H}\"\n\
87281760Ssjg	\$${%%:=}=\"${%:=}\"\n\n"
88281760Ssjg
89281760SsjgLOCAL_ALTERNATIVE_SUBSTITUTIONS = \
90281760Ssjg	"Target with suffix transformations\n\
91281760Ssjg	\$${@D:=append}=\"${@D:=append}\"\n\
92281760Ssjg	\$${@F:.o=.O}=\"${@F:.o=.O}\"\n\
93281760Ssjg	\n\
94281760Ssjg	Implied source with suffix transformations\n\
95281760Ssjg	\$${<D:r=rr}=\"${<D:r=rr}\"\n\
96281760Ssjg	\$${<F:.c=.C}=\"${<F:.c=.C}\"\n\
97281760Ssjg	\n\
98281760Ssjg	Suffixless target with suffix transformations\n\
99281760Ssjg	\$${*D:.=dot}=\"${*D:.=dot}\"\n\
100281760Ssjg	\$${*F:.a=}=\"${*F:.a=}\"\n\
101281760Ssjg	\n\
102281760Ssjg	Out-of-date dependencies with suffix transformations\n\
103281760Ssjg	\$${?D:ir=}=\"${?D:ir=}\"\n\
104281760Ssjg	\$${?F:.h=.H}=\"${?F:.h=.H}\"\n\
105281760Ssjg	\n\
106281760Ssjg	Member with suffix transformations\n\
107281760Ssjg	\$${%%D:.=}=\"${%D:.=}\"\n\
108281760Ssjg	\$${%%F:\$${VAR2}=\$${VAR}}=\"${%F:${VAR2}=${VAR}}\"\n\n"
109281760Ssjg
110281760Ssjg.SUFFIXES: .c .o .a
111281760Ssjg
112281760Ssjg# The system makefiles make the .c.a rule .PRECIOUS with a special source,
113281760Ssjg# but such a thing is not POSIX compatible.  It's also somewhat useless
114281760Ssjg# in a test makefile.
115281760Ssjg.c.a:
116281760Ssjg	@printf ${LOCALS}
117281760Ssjg	@printf ${LOCAL_ALTERNATIVES}
118281760Ssjg	@printf ${LOCAL_SUBSTITUTIONS}
119281760Ssjg	@printf ${LOCAL_ALTERNATIVE_SUBSTITUTIONS}
120281760Ssjg	cc -c -o '${%}' '${<}'
121281760Ssjg	ar ${ARFLAGS} '${@}' '${%}'
122281760Ssjg	rm -f '${%}'
123281760Ssjg
124281760Ssjg.c.o:
125281760Ssjg	@printf ${LOCALS}
126281760Ssjg	@printf ${LOCAL_ALTERNATIVES}
127281760Ssjg	@printf ${LOCAL_SUBSTITUTIONS}
128281760Ssjg	@printf ${LOCAL_ALTERNATIVE_SUBSTITUTIONS}
129281760Ssjg	cc -c -o '${@}' '${<}'
130281760Ssjg
131281760Ssjg# Some of these rules are padded with useless extra dependencies just so
132281760Ssjg# that ${?} has more than one file.
133281760Ssjg
134281760Ssjglib.a: lib.a(obj1.o) lib.a(obj2.o) lib.a(obj3.o)
135281760Ssjg	@ar -s '${@}'
136281760Ssjg
137281760Ssjg# Explicit rule where the dependency is an inferred file.  The dependency
138281760Ssjg# object's name differs from the member's because there was a bug which
139281760Ssjg# forced a dependency on member even when no such dependency was specified
140281760Ssjg# (PR 49086).
141281760Ssjglib.a(obj1.o): dir/obj_1.o dummy
142281760Ssjg	@printf ${LOCALS}
143281760Ssjg	@printf ${LOCAL_ALTERNATIVES}
144281760Ssjg	@printf ${LOCAL_SUBSTITUTIONS}
145281760Ssjg	@printf ${LOCAL_ALTERNATIVE_SUBSTITUTIONS}
146281760Ssjg	cp 'dir/obj_1.o' '$%'
147281760Ssjg	ar ${ARFLAGS} '${@}' '$%'
148281760Ssjg	rm -f '$%'
149281760Ssjg
150281760Ssjg# Excplicit rule where the dependency also has an explicit rule.
151281760Ssjglib.a(obj2.o): obj2.o
152281760Ssjg	ar ${ARFLAGS} '${@}' '${%}'
153281760Ssjg
154281760Ssjg# Use .c.a inference with an extra dependency.
155281760Ssjglib.a(obj3.o): obj3.h dir/dummy
156281760Ssjg
157281760Ssjg# Use .c.o inference with an extra dependency.
158281760Ssjgdir/obj_1.o: dir/obj_1.h
159281760Ssjg
160281760Ssjg# According to POSIX, $* is only required for inference rules and $<'s
161281760Ssjg# value is unspecified outside of inference rules.  Strictly speaking
162281760Ssjg# we shouldn't be expanding them here but who cares.  At least we get
163281760Ssjg# to check that the program does nothing stupid (like crash) with them.
164281760Ssjg# The C file is named differently from the object file because there
165281760Ssjg# was a bug which forced dependencies based on inference rules on all
166281760Ssjg# applicable targets (PR 49086).
167281760Ssjgobj2.o: obj_2.c obj_2.h dir/obj_1.h
168281760Ssjg	@printf ${LOCALS}
169281760Ssjg	@printf ${LOCAL_ALTERNATIVES}
170281760Ssjg	@printf ${LOCAL_SUBSTITUTIONS}
171281760Ssjg	@printf ${LOCAL_ALTERNATIVE_SUBSTITUTIONS}
172281760Ssjg	cc -c -o '${@}' 'obj_2.c'
173281760Ssjg
174281760Ssjg# Hey, this is make, we can make our own test data setup!  obj1.c
175281760Ssjg# and obj2.c are not used, so they should not get created.  They're here
176281760Ssjg# as a bait for a regression into the forced dependencies discussed earlier.
177281760Ssjgobj1.c dir/obj_1.c obj2.c obj_2.c obj3.c:
178281760Ssjg	mkdir -p '${@D}'
179281760Ssjg	printf '#include "${@F:.c=.h}"\nconst char* ${@F:.c=} = "${@}";\n' \
180281760Ssjg	    >'${@}'
181281760Ssjg
182281760Ssjgdir/obj_1.h obj_2.h obj3.h dummy dir/dummy:
183281760Ssjg	mkdir -p '${@D}'
184281760Ssjg	touch '${@}'
185