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