Makefile revision 1.122
1#	$NetBSD: Makefile,v 1.122 2022/05/03 19:05:34 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 =
38GCOV_PERL+=		    $$percent eq '100.00' ? '0'
39GCOV_PERL+=		    : $$file =~ /\.h$$/ ? '?'
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
57USE_META?=	yes
58.if ${USE_META:tl} != "no"
59
60SRCS+=		meta.c
61CPPFLAGS+=	-DUSE_META
62
63USE_FILEMON?=	ktrace
64.  if ${USE_FILEMON:tl} != "no"
65
66.PATH:	${.CURDIR}/filemon
67SRCS+=		filemon_${USE_FILEMON}.c
68CPPFLAGS+=	-DUSE_FILEMON
69CPPFLAGS+=	-DUSE_FILEMON_${USE_FILEMON:tu}
70
71.    if ${USE_FILEMON} == "dev"
72FILEMON_H?=	/usr/include/dev/filemon/filemon.h
73.      if exists(${FILEMON_H}) && ${FILEMON_H:T} == "filemon.h"
74COPTS.filemon_dev.c+= \
75		-DHAVE_FILEMON_H -I${FILEMON_H:H}
76.      endif
77.    endif
78.  endif
79.endif
80
81SUBDIR.roff+=	PSD.doc
82.if make(obj) || make(clean)
83SUBDIR+=	unit-tests
84.endif
85
86LINTFLAGS+=	-T	# strict bool mode, available since 2021-01-11
87LINTFLAGS+=	-w	# treat warnings as errors
88CLEANFILES+=	*.o	# for filemon objects
89
90COPTS.arch.c+=	${GCC_NO_FORMAT_TRUNCATION}
91COPTS.dir.c+=	${GCC_NO_FORMAT_TRUNCATION}
92COPTS.job.c+=	-Wno-format-nonliteral	# custom shell templates
93COPTS.main.c+=	${GCC_NO_FORMAT_TRUNCATION} ${GCC_NO_STRINGOP_TRUNCATION}
94COPTS.meta.c+=	${GCC_NO_FORMAT_TRUNCATION}
95COPTS.var.c+=	-Wno-format-nonliteral	# strftime
96
97CPPFLAGS+=	-DMAKE_NATIVE
98
99.if defined(TOOLDIR)
100# This is a native NetBSD build, use libutil rather than the local emalloc etc.
101CPPFLAGS+=	-DUSE_EMALLOC
102LDADD+=		-lutil
103DPADD+=		${LIBUTIL}
104.endif
105
106COPTS+=		-Wdeclaration-after-statement
107
108# A simple unit-test driver to help catch regressions
109TEST_MAKE ?= ${.OBJDIR}/${PROG:T}
110test: .MAKE
111	cd ${.CURDIR}/unit-tests \
112	&& MAKEFLAGS= ${TEST_MAKE} -r -m / TEST_MAKE=${TEST_MAKE} ${TESTS:DTESTS=${TESTS:Q}} ${.TARGET}
113.if ${USE_COVERAGE} == yes
114	${MAKE} report-coverage
115.endif
116
117accept sync-mi: .MAKE
118	cd ${.CURDIR}/unit-tests && ${.MAKE} ${.TARGET}
119
120retest:
121	${.MAKE} -C ${.CURDIR}/unit-tests cleandir
122.if ${USE_COVERAGE} == yes
123	rm -f *.gcov *.gcda
124.endif
125	${.MAKE} test
126
127# Just out of curiosity, during development.
128.SUFFIXES: .cpre .casm
129.c.cpre:
130	${COMPILE.c:S,^-c$,-E,} ${COPTS.${.IMPSRC}} ${.IMPSRC} -o ${.TARGET}
131.c.casm:
132	${COMPILE.c:S,^-c$,-S,} ${COPTS.${.IMPSRC}} ${.IMPSRC} -o ${.TARGET}
133
134test-coverage: .PHONY
135	@make -s clean cleandir
136	@env USE_COVERAGE=yes COPTS="-O0 -ggdb" USER_CPPFLAGS="-DCLEANUP" \
137		sh -c 'make -s all -j8 && make -s test'
138	@env USE_COVERAGE=yes make report-coverage > coverage.txt
139
140.if ${USE_COVERAGE} == "yes"
141report-coverage: .PHONY
142	@echo 'covered  uncovered  file'
143	@${GCOV} ${GCOV_OPTS} *.gcda \
144	| perl -ne ${GCOV_PERL:Q} \
145	| sort -nr
146	@sed -i 's,^\([^:]*\): *[0-9]*:,\1: ,' *.gcov
147.endif
148
149.include <bsd.prog.mk>
150.include <bsd.subdir.mk>
151
152# For -DCLEANUP and similar feature toggles.
153CPPFLAGS+=	${USER_CPPFLAGS}
154# For overriding -std=gnu99 or similar options.
155CFLAGS+=	${USER_CFLAGS}
156