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