1# $NetBSD: posix1.mk,v 1.3 2014/08/30 22:21:08 sjg Exp $
2
3# Keep the default suffixes from interfering, just in case.
4.SUFFIXES:
5
6all:	line-continuations suffix-substitution localvars
7
8# we need to clean for repeatable results
9.BEGIN: clean
10clean:
11	@rm -f lib.a dir/* dummy obj*
12
13#
14# Line continuations
15#
16
17# Escaped newlines and leading whitespace from the next line are replaced
18# with single space, except in commands, where the escape and the newline
19# are retained, but a single leading tab (if any) from the next line is
20# removed. (PR 49085)
21# Expect:
22# ${VAR} = "foo  bar baz"
23# a
24# b
25# c
26VAR = foo\
27\
28	  bar\
29 baz
30
31line-continuations:
32	@echo '$${VAR} = "${VAR}"'
33	@echo 'aXbXc' | sed -e 's/X/\
34	/g'
35
36
37#
38# Suffix substitution
39#
40
41# The only variable modifier accepted by POSIX.
42# ${VAR:s1=s2}: replace s1, if found, with s2 at end of each word in
43# ${VAR}.  s1 and s2 may contain macro expansions.
44# Expect: foo baR baz, bar baz, foo bar baz, fooadd baradd bazadd
45suffix-substitution:
46	@echo '${VAR:r=R}, ${VAR:foo=}, ${VAR:not_there=wrong}, ${VAR:=add}'
47
48
49#
50# Local variables: regular forms, D/F forms and suffix substitution.
51#
52
53# In the past substitutions did not work with the D/F forms and those
54# forms were not available for $?.  (PR 49085)
55
56ARFLAGS = -rcv
57
58localvars: lib.a
59
60# $@ = target or archive name	$< = implied source
61# $* = target without suffix 	$? = sources newer than target
62# $% = archive member name
63LOCALS = \
64	"Local variables\n\
65	\$${@}=\"${@}\" \$${<}=\"${<}\"\n\
66	\$${*}=\"${*}\" \$${?}=\"${?}\"\n\
67	\$${%%}=\"${%}\"\n\n"
68
69# $XD = directory part of X	$XF = file part of X
70# X is one of the local variables.
71LOCAL_ALTERNATIVES = \
72	"Directory and filename parts of local variables\n\
73	\$${@D}=\"${@D}\" \$${@F}=\"${@F}\"\n\
74	\$${<D}=\"${<D}\" \$${<F}=\"${<F}\"\n\
75	\$${*D}=\"${*D}\" \$${*F}=\"${*F}\"\n\
76	\$${?D}=\"${?D}\" \$${?F}=\"${?F}\"\n\
77	\$${%%D}=\"${%D}\" \$${%%F}=\"${%F}\"\n\n"
78
79# Do all kinds of meaningless substitutions on local variables to see
80# if they work.  Add, remove and replace things.
81VAR2 = .o
82VAR3 = foo
83LOCAL_SUBSTITUTIONS = \
84	"Local variable substitutions\n\
85	\$${@:.o=}=\"${@:.o=}\" \$${<:.c=.C}=\"${<:.c=.C}\"\n\
86	\$${*:=.h}=\"${*:=.h}\" \$${?:.h=.H}=\"${?:.h=.H}\"\n\
87	\$${%%:=}=\"${%:=}\"\n\n"
88
89LOCAL_ALTERNATIVE_SUBSTITUTIONS = \
90	"Target with suffix transformations\n\
91	\$${@D:=append}=\"${@D:=append}\"\n\
92	\$${@F:.o=.O}=\"${@F:.o=.O}\"\n\
93	\n\
94	Implied source with suffix transformations\n\
95	\$${<D:r=rr}=\"${<D:r=rr}\"\n\
96	\$${<F:.c=.C}=\"${<F:.c=.C}\"\n\
97	\n\
98	Suffixless target with suffix transformations\n\
99	\$${*D:.=dot}=\"${*D:.=dot}\"\n\
100	\$${*F:.a=}=\"${*F:.a=}\"\n\
101	\n\
102	Out-of-date dependencies with suffix transformations\n\
103	\$${?D:ir=}=\"${?D:ir=}\"\n\
104	\$${?F:.h=.H}=\"${?F:.h=.H}\"\n\
105	\n\
106	Member with suffix transformations\n\
107	\$${%%D:.=}=\"${%D:.=}\"\n\
108	\$${%%F:\$${VAR2}=\$${VAR}}=\"${%F:${VAR2}=${VAR}}\"\n\n"
109
110.SUFFIXES: .c .o .a
111
112# The system makefiles make the .c.a rule .PRECIOUS with a special source,
113# but such a thing is not POSIX compatible.  It's also somewhat useless
114# in a test makefile.
115.c.a:
116	@printf ${LOCALS}
117	@printf ${LOCAL_ALTERNATIVES}
118	@printf ${LOCAL_SUBSTITUTIONS}
119	@printf ${LOCAL_ALTERNATIVE_SUBSTITUTIONS}
120	cc -c -o '${%}' '${<}'
121	ar ${ARFLAGS} '${@}' '${%}'
122	rm -f '${%}'
123
124.c.o:
125	@printf ${LOCALS}
126	@printf ${LOCAL_ALTERNATIVES}
127	@printf ${LOCAL_SUBSTITUTIONS}
128	@printf ${LOCAL_ALTERNATIVE_SUBSTITUTIONS}
129	cc -c -o '${@}' '${<}'
130
131# Some of these rules are padded with useless extra dependencies just so
132# that ${?} has more than one file.
133
134lib.a: lib.a(obj1.o) lib.a(obj2.o) lib.a(obj3.o)
135	@ar -s '${@}'
136
137# Explicit rule where the dependency is an inferred file.  The dependency
138# object's name differs from the member's because there was a bug which
139# forced a dependency on member even when no such dependency was specified
140# (PR 49086).
141lib.a(obj1.o): dir/obj_1.o dummy
142	@printf ${LOCALS}
143	@printf ${LOCAL_ALTERNATIVES}
144	@printf ${LOCAL_SUBSTITUTIONS}
145	@printf ${LOCAL_ALTERNATIVE_SUBSTITUTIONS}
146	cp 'dir/obj_1.o' '$%'
147	ar ${ARFLAGS} '${@}' '$%'
148	rm -f '$%'
149
150# Excplicit rule where the dependency also has an explicit rule.
151lib.a(obj2.o): obj2.o
152	ar ${ARFLAGS} '${@}' '${%}'
153
154# Use .c.a inference with an extra dependency.
155lib.a(obj3.o): obj3.h dir/dummy
156
157# Use .c.o inference with an extra dependency.
158dir/obj_1.o: dir/obj_1.h
159
160# According to POSIX, $* is only required for inference rules and $<'s
161# value is unspecified outside of inference rules.  Strictly speaking
162# we shouldn't be expanding them here but who cares.  At least we get
163# to check that the program does nothing stupid (like crash) with them.
164# The C file is named differently from the object file because there
165# was a bug which forced dependencies based on inference rules on all
166# applicable targets (PR 49086).
167obj2.o: obj_2.c obj_2.h dir/obj_1.h
168	@printf ${LOCALS}
169	@printf ${LOCAL_ALTERNATIVES}
170	@printf ${LOCAL_SUBSTITUTIONS}
171	@printf ${LOCAL_ALTERNATIVE_SUBSTITUTIONS}
172	cc -c -o '${@}' 'obj_2.c'
173
174# Hey, this is make, we can make our own test data setup!  obj1.c
175# and obj2.c are not used, so they should not get created.  They're here
176# as a bait for a regression into the forced dependencies discussed earlier.
177obj1.c dir/obj_1.c obj2.c obj_2.c obj3.c:
178	mkdir -p '${@D}'
179	printf '#include "${@F:.c=.h}"\nconst char* ${@F:.c=} = "${@}";\n' \
180	    >'${@}'
181
182dir/obj_1.h obj_2.h obj3.h dummy dir/dummy:
183	mkdir -p '${@D}'
184	touch '${@}'
185