Makefile revision 1.118
1#	$NetBSD: Makefile,v 1.118 2021/12/12 10:33:03 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+=  for.c
11SRCS+=  hash.c
12SRCS+=  job.c
13SRCS+=  lst.c
14SRCS+=  main.c
15SRCS+=	make.c
16SRCS+=  make_malloc.c
17SRCS+=  metachar.c
18SRCS+=  parse.c
19SRCS+=	str.c
20SRCS+=  suff.c
21SRCS+=  targ.c
22SRCS+=  trace.c
23SRCS+=  var.c
24SRCS+=  util.c
25
26# Whether to generate a coverage report after running the tests.
27USE_COVERAGE?=	no		# works only with gcc; clang9 fails to link
28.if ${USE_COVERAGE} == "yes"
29GCOV?=		gcov
30COPTS+=		--coverage -O0 -ggdb
31GCOV_PERL=	if (/^File '(?:.*\/)?(\S+)'/) {
32GCOV_PERL+=		$$file = $$1; $$func = "";
33GCOV_PERL+=	} elsif (/^Function '(\S+)'/) {
34GCOV_PERL+=		$$func = $$1;
35GCOV_PERL+=	} elsif (/^Lines executed:(\d+\.\d+)% of (\d+)/ && defined($$file)) {
36GCOV_PERL+=		my ($$percent, $$lines) = ($$1, $$2);
37GCOV_PERL+=		my $$uncovered = $$file =~ /\.h$$/
38GCOV_PERL+=		    ? '?'
39GCOV_PERL+=		    : `grep -c '\#\#\#\#\#:' < \$$(basename $$file.gcov)`;
40GCOV_PERL+=		chomp($$uncovered);
41GCOV_PERL+=		printf("%7.2f  %4s/%4d  %s%s\n",
42GCOV_PERL+=		    $$percent, $$uncovered, $$lines, $$file, $$func);
43GCOV_PERL+=		$$file = undef;
44GCOV_PERL+=	}
45LDADD+=		--coverage
46.endif
47CLEANFILES+=	*.gcda *.gcno *.gcov
48
49# Whether to compile using the Undefined Behavior Sanitizer (GCC, Clang).
50USE_UBSAN?=	no
51.if ${USE_UBSAN} == "yes"
52COPTS+=		-fsanitize=undefined
53LDADD+=		-fsanitize=undefined
54.endif
55
56# Whether to compile with GCC 10 from pkgsrc, during development.
57USE_GCC10?=	no
58.if ${USE_GCC10} == "yes"
59# CC is set further down in this file
60COPTS+=		-Wno-attributes	# for abs and labs
61COPTS.arch.c+=	-Wno-error=format-truncation
62COPTS.dir.c+=	-Wno-error=format-truncation
63COPTS.main.c+=	-Wno-error=format-truncation
64COPTS.meta.c+=	-Wno-error=format-truncation
65.endif
66
67# Whether to compile with GCC 9 from pkgsrc, during development.
68USE_GCC9?=	no
69.if ${USE_GCC9} == "yes"
70# CC is set further down in this file
71COPTS+=		-Wno-attributes	# for abs and labs
72COPTS.arch.c+=	-Wno-error=format-truncation
73COPTS.dir.c+=	-Wno-error=format-truncation
74COPTS.main.c+=	-Wno-error=format-truncation
75COPTS.meta.c+=	-Wno-error=format-truncation
76.endif
77
78# Whether to compile with GCC 8 from pkgsrc, during development.
79USE_GCC8?=	no
80.if ${USE_GCC8} == "yes"
81# CC is set further down in this file
82COPTS+=		-Wno-attributes	# for abs and labs
83COPTS.arch.c+=	-Wno-error=format-truncation
84COPTS.dir.c+=	-Wno-error=format-truncation
85COPTS.main.c+=	-Wno-error=format-truncation
86COPTS.meta.c+=	-Wno-error=format-truncation
87.endif
88
89USE_META?=	yes
90.if ${USE_META:tl} != "no"
91
92SRCS+=		meta.c
93CPPFLAGS+=	-DUSE_META
94
95USE_FILEMON?=	ktrace
96.  if ${USE_FILEMON:tl} != "no"
97
98.PATH:	${.CURDIR}/filemon
99SRCS+=		filemon_${USE_FILEMON}.c
100CPPFLAGS+=	-DUSE_FILEMON
101CPPFLAGS+=	-DUSE_FILEMON_${USE_FILEMON:tu}
102
103.    if ${USE_FILEMON} == "dev"
104FILEMON_H?=	/usr/include/dev/filemon/filemon.h
105.      if exists(${FILEMON_H}) && ${FILEMON_H:T} == "filemon.h"
106COPTS.filemon_dev.c+= \
107		-DHAVE_FILEMON_H -I${FILEMON_H:H}
108.      endif
109.    endif
110.  endif
111.endif
112
113SUBDIR.roff+=	PSD.doc
114.if make(obj) || make(clean)
115SUBDIR+=	unit-tests
116.endif
117
118LINTFLAGS+=	-T	# strict bool mode, available since 2021-01-11
119LINTFLAGS+=	-w	# treat warnings as errors
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
190.if ${USE_COVERAGE} == "yes"
191report-coverage: .PHONY
192	@echo 'covered  uncovered  file'
193	@${GCOV} ${GCOV_OPTS} *.gcda \
194	| perl -ne ${GCOV_PERL:Q} \
195	| sort -nr
196	@sed -i 's,^\([^:]*\): *[0-9]*:,\1: ,' *.gcov
197.endif
198
199.include <bsd.prog.mk>
200.include <bsd.subdir.mk>
201
202# For -DCLEANUP and similar feature toggles.
203CPPFLAGS+=	${USER_CPPFLAGS}
204# For overriding -std=gnu99 or similar options.
205CFLAGS+=	${USER_CFLAGS}
206.if defined(LATE_CC)
207CC=		${LATE_CC}
208.endif
209