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