Makefile revision 1.128 1 # $NetBSD: Makefile,v 1.128 2025/01/19 10:57:10 rillig 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 . endif
80 . endif
81 .endif
82
83 SUBDIR.roff+= PSD.doc
84 .if make(obj) || make(clean) || make(cleandir)
85 SUBDIR+= unit-tests
86 .endif
87
88 LINTFLAGS+= -T # strict bool mode, available since 2021-01-11
89 LINTFLAGS+= -w # treat warnings as errors
90 CLEANFILES+= *.o # for filemon objects
91
92 COPTS.arch.c+= ${CC_WNO_FORMAT_TRUNCATION}
93 COPTS.dir.c+= ${CC_WNO_FORMAT_TRUNCATION}
94 COPTS.job.c+= -Wno-format-nonliteral # custom shell templates
95 COPTS.main.c+= ${CC_WNO_FORMAT_TRUNCATION} ${CC_WNO_STRINGOP_TRUNCATION}
96 COPTS.meta.c+= ${CC_WNO_FORMAT_TRUNCATION}
97 COPTS.var.c+= -Wno-format-nonliteral # strftime
98
99 CPPFLAGS+= -DMAKE_NATIVE
100
101 .if defined(TOOLDIR)
102 # This is a native NetBSD build, use libutil rather than the local emalloc etc.
103 CPPFLAGS+= -DUSE_EMALLOC
104 LDADD+= -lutil
105 DPADD+= ${LIBUTIL}
106 .endif
107
108 COPTS+= -Wdeclaration-after-statement
109
110 # A simple unit-test driver to help catch regressions
111 TEST_MAKE ?= ${.OBJDIR}/${PROG:T}
112 test: .MAKE
113 cd ${.CURDIR}/unit-tests \
114 && MAKEFLAGS= ${TEST_MAKE} -r -m / TEST_MAKE=${TEST_MAKE} ${TESTS:DTESTS=${TESTS:Q}} ${.TARGET}
115 .if ${USE_COVERAGE} == yes
116 ${MAKE} report-coverage
117 .endif
118
119 accept sync-mi: .MAKE
120 cd ${.CURDIR}/unit-tests && ${.MAKE} ${.TARGET}
121
122 retest:
123 ${.MAKE} -C ${.CURDIR}/unit-tests cleandir
124 .if ${USE_COVERAGE} == yes
125 rm -f *.gcov *.gcda
126 .endif
127 ${.MAKE} test
128
129 # Just out of curiosity, during development.
130 .SUFFIXES: .cpre .casm
131 .c.cpre:
132 ${COMPILE.c:S,^-c$,-E,} ${COPTS.${.IMPSRC}} ${.IMPSRC} -o ${.TARGET}
133 .c.casm:
134 ${COMPILE.c:S,^-c$,-S,} ${COPTS.${.IMPSRC}} ${.IMPSRC} -o ${.TARGET}
135
136 test-coverage: .PHONY
137 @make -s clean cleandir
138 @env USE_COVERAGE=yes COPTS="-O0 -ggdb" USER_CPPFLAGS="-DCLEANUP" \
139 sh -c 'make -s all -j8 && make -s test'
140 @env USE_COVERAGE=yes make report-coverage > coverage.txt
141
142 .if ${USE_COVERAGE} == "yes"
143 report-coverage: .PHONY
144 @echo 'covered uncovered file'
145 @${GCOV} ${GCOV_OPTS} *.gcda \
146 | perl -ne ${GCOV_PERL:Q} \
147 | sort -r -k4 \
148 | sort -nr -k1
149 @sed -i \
150 -e '1d' \
151 -e 's,^\([^:]*\): *[0-9]*:,\1: ,' \
152 -e 's, *$$,,g' \
153 *.gcov
154 .endif
155
156 .include <bsd.prog.mk>
157 .include <bsd.subdir.mk>
158
159 # For -DCLEANUP and similar feature toggles.
160 CPPFLAGS+= ${USER_CPPFLAGS}
161 # For overriding -std=gnu99 or similar options.
162 CFLAGS+= ${USER_CFLAGS}
163