Makefile revision 1.112
1#	$NetBSD: Makefile,v 1.112 2021/01/30 16:05:45 rillig Exp $
2#	@(#)Makefile	5.2 (Berkeley) 12/28/90
3
4PROG=	make
5SRCS=	arch.c
6SRCS+=  buf.c
7SRCS+=  compat.c
8SRCS+=  cond.c
9SRCS+=  dir.c
10SRCS+=  enum.c
11SRCS+=  for.c
12SRCS+=  hash.c
13SRCS+=  job.c
14SRCS+=  lst.c
15SRCS+=  main.c
16SRCS+=	make.c
17SRCS+=  make_malloc.c
18SRCS+=  metachar.c
19SRCS+=  parse.c
20SRCS+=	str.c
21SRCS+=  suff.c
22SRCS+=  targ.c
23SRCS+=  trace.c
24SRCS+=  var.c
25SRCS+=  util.c
26HDRS=   buf.h
27HDRS+=  config.h
28HDRS+=  dir.h
29HDRS+=  enum.h
30HDRS+=  hash.h
31HDRS+=  job.h
32HDRS+=  lst.h
33HDRS+=  make.h
34HDRS+=  make_malloc.h
35HDRS+=  meta.h
36HDRS+=  metachar.h
37HDRS+=  nonints.h
38HDRS+=  pathnames.h
39HDRS+=  trace.h
40
41# Whether to generate a coverage report after running the tests.
42USE_COVERAGE?=	no		# works only with gcc; clang9 fails to link
43.if ${USE_COVERAGE} == "yes"
44GCOV?=		gcov
45COPTS+=		--coverage -O0 -ggdb
46GCOV_PERL=	if (/^File '(?:.*\/)?(\S+)'/) {
47GCOV_PERL+=		$$file = $$1; $$func = "";
48GCOV_PERL+=	} elsif (/^Function '(\S+)'/) {
49GCOV_PERL+=		$$func = $$1;
50GCOV_PERL+=	} elsif (/^Lines executed:(\d+\.\d+)% of (\d+)/ && defined($$file)) {
51GCOV_PERL+=		my ($$percent, $$lines) = ($$1, $$2);
52GCOV_PERL+=		my $$uncovered = `grep -c '\#\#\#\#\#:' < \$$(basename $$file.gcov)`;
53GCOV_PERL+=		printf("%7.2f  %4d/%4d  %s%s\n",
54GCOV_PERL+=		    $$percent, $$uncovered, $$lines, $$file, $$func);
55GCOV_PERL+=		$$file = undef;
56GCOV_PERL+=	}
57LDADD+=		--coverage
58.endif
59CLEANFILES+=	*.gcda *.gcno *.gcov
60
61# Whether to compile using the Undefined Behavior Sanitizer (GCC, Clang).
62USE_UBSAN?=	no
63.if ${USE_UBSAN} == "yes"
64COPTS+=		-fsanitize=undefined
65LDADD+=		-fsanitize=undefined
66.endif
67
68# Whether to compile with GCC 10 from pkgsrc, during development.
69USE_GCC10?=	no
70.if ${USE_GCC10} == "yes"
71# CC is set further down in this file
72COPTS+=		-Wno-attributes	# for abs and labs
73COPTS.arch.c+=	-Wno-error=format-truncation
74COPTS.dir.c+=	-Wno-error=format-truncation
75COPTS.main.c+=	-Wno-error=format-truncation
76COPTS.meta.c+=	-Wno-error=format-truncation
77.endif
78
79# Whether to compile with GCC 9 from pkgsrc, during development.
80USE_GCC9?=	no
81.if ${USE_GCC9} == "yes"
82# CC is set further down in this file
83COPTS+=		-Wno-attributes	# for abs and labs
84COPTS.arch.c+=	-Wno-error=format-truncation
85COPTS.dir.c+=	-Wno-error=format-truncation
86COPTS.main.c+=	-Wno-error=format-truncation
87COPTS.meta.c+=	-Wno-error=format-truncation
88.endif
89
90# Whether to compile with GCC 8 from pkgsrc, during development.
91USE_GCC8?=	no
92.if ${USE_GCC8} == "yes"
93# CC is set further down in this file
94COPTS+=		-Wno-attributes	# for abs and labs
95COPTS.arch.c+=	-Wno-error=format-truncation
96COPTS.dir.c+=	-Wno-error=format-truncation
97COPTS.main.c+=	-Wno-error=format-truncation
98COPTS.meta.c+=	-Wno-error=format-truncation
99.endif
100
101USE_META?=	yes
102.if ${USE_META:tl} != "no"
103
104SRCS+=		meta.c
105CPPFLAGS+=	-DUSE_META
106
107USE_FILEMON?=	ktrace
108.  if ${USE_FILEMON:tl} != "no"
109
110.PATH:	${.CURDIR}/filemon
111SRCS+=		filemon_${USE_FILEMON}.c
112CPPFLAGS+=	-DUSE_FILEMON
113CPPFLAGS+=	-DUSE_FILEMON_${USE_FILEMON:tu}
114
115.    if ${USE_FILEMON} == "dev"
116FILEMON_H?=	/usr/include/dev/filemon/filemon.h
117.      if exists(${FILEMON_H}) && ${FILEMON_H:T} == "filemon.h"
118COPTS.filemon_dev.c+= \
119		-DHAVE_FILEMON_H -I${FILEMON_H:H}
120.      endif
121.    endif
122.  endif
123.endif
124
125SUBDIR.roff+=	PSD.doc
126.if make(obj) || make(clean)
127SUBDIR+=	unit-tests
128.endif
129
130LINTFLAGS+=	-T	# strict bool mode, available since 2021-01-11
131
132${SRCS:M*.c:.c=.o}: ${HDRS}
133CLEANFILES+=	*.o
134
135COPTS.arch.c+=	${GCC_NO_FORMAT_TRUNCATION}
136COPTS.dir.c+=	${GCC_NO_FORMAT_TRUNCATION}
137COPTS.job.c+=	-Wno-format-nonliteral
138COPTS.main.c+=	${GCC_NO_FORMAT_TRUNCATION} ${GCC_NO_STRINGOP_TRUNCATION}
139COPTS.meta.c+=	${GCC_NO_FORMAT_TRUNCATION}
140COPTS.parse.c+=	-Wno-format-nonliteral
141COPTS.var.c+=	-Wno-format-nonliteral
142
143CPPFLAGS+=	-DMAKE_NATIVE
144
145.if ${USE_GCC10} == "yes"
146GCC10BASE?=	/usr/pkg/gcc10
147LATE_CC=	${GCC10BASE}/bin/gcc
148GCOV=		${GCC10BASE}/bin/gcov
149.endif
150
151.if ${USE_GCC9} == "yes"
152GCC9BASE?=	/usr/pkg/gcc9
153LATE_CC=	${GCC9BASE}/bin/gcc
154GCOV=		${GCC9BASE}/bin/gcov
155.endif
156
157.if ${USE_GCC8} == "yes"
158GCC8BASE?=	/usr/pkg/gcc8
159LATE_CC=	${GCC8BASE}/bin/gcc
160GCOV=		${GCC8BASE}/bin/gcov
161.endif
162
163.if defined(TOOLDIR)
164# This is a native NetBSD build, use libutil rather than the local emalloc etc.
165CPPFLAGS+=	-DUSE_EMALLOC
166LDADD+=		-lutil
167DPADD+=		${LIBUTIL}
168.endif
169
170COPTS+=		-Wdeclaration-after-statement
171
172# A simple unit-test driver to help catch regressions
173TEST_MAKE ?= ${.OBJDIR}/${PROG:T}
174test: .MAKE
175	cd ${.CURDIR}/unit-tests \
176	&& MAKEFLAGS= ${TEST_MAKE} -r -m / TEST_MAKE=${TEST_MAKE} ${TESTS:DTESTS=${TESTS:Q}} ${.TARGET}
177.if ${USE_COVERAGE} == yes
178	${MAKE} report-coverage
179.endif
180
181accept sync-mi: .MAKE
182	cd ${.CURDIR}/unit-tests && ${.MAKE} ${.TARGET}
183
184retest:
185	${.MAKE} -C ${.CURDIR}/unit-tests cleandir
186.if ${USE_COVERAGE} == yes
187	rm -f *.gcov *.gcda
188.endif
189	${.MAKE} test
190
191# Just out of curiosity, during development.
192.SUFFIXES: .cpre .casm
193.c.cpre:
194	${COMPILE.c:S,^-c$,-E,} ${COPTS.${.IMPSRC}} ${.IMPSRC} -o ${.TARGET}
195.c.casm:
196	${COMPILE.c:S,^-c$,-S,} ${COPTS.${.IMPSRC}} ${.IMPSRC} -o ${.TARGET}
197
198test-coverage: .PHONY
199	@make -s clean cleandir
200	@env USE_COVERAGE=yes COPTS="-O0 -ggdb" USER_CPPFLAGS="-DCLEANUP" \
201		sh -c 'make -s all -j8 && make -s test'
202
203report-coverage: .PHONY
204	@echo 'covered  uncovered  file'
205	@${GCOV} ${GCOV_OPTS} ${SRCS} 2>&1 \
206	| perl -ne ${GCOV_PERL:Q} \
207	| sort -nr
208	@sed -i 's,^\([^:]*\): *[0-9]*:,\1: ,' *.gcov
209
210.include <bsd.prog.mk>
211.include <bsd.subdir.mk>
212
213# For -DCLEANUP and similar feature toggles.
214CPPFLAGS+=	${USER_CPPFLAGS}
215# For overriding -std=gnu99 or similar options.
216CFLAGS+=	${USER_CFLAGS}
217.if defined(LATE_CC)
218CC=		${LATE_CC}
219.endif
220