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
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
26 # Whether to generate a coverage report after running the tests.
27 USE_COVERAGE?= no # works only with gcc; clang9 fails to link
28 .if ${USE_COVERAGE} == "yes"
29 GCOV?= gcov
30 COPTS+= --coverage -O0 -ggdb
31 GCOV_PERL= if (/^File '(?:.*\/)?(\S+)'/) {
32 GCOV_PERL+= $$file = $$1; $$func = "";
33 GCOV_PERL+= } elsif (/^Function '(\S+)'/) {
34 GCOV_PERL+= $$func = $$1;
35 GCOV_PERL+= } elsif (/^Lines executed:(\d+\.\d+)% of (\d+)/ && defined($$file)) {
36 GCOV_PERL+= my ($$percent, $$lines) = ($$1, $$2);
37 GCOV_PERL+= my $$uncovered =
38 GCOV_PERL+= $$percent eq '100.00' ? '0'
39 GCOV_PERL+= : $$file =~ /\.h$$/ ? '?'
40 GCOV_PERL+= : `grep -c '\#\#\#\#\#:' < \$$(basename $$file.gcov)`;
41 GCOV_PERL+= chomp($$uncovered);
42 GCOV_PERL+= printf("%7.2f %4s/%4d %s%s\n",
43 GCOV_PERL+= $$percent, $$uncovered, $$lines, $$file, $$func);
44 GCOV_PERL+= $$file = undef;
45 GCOV_PERL+= }
46 LDADD+= --coverage
47 .endif
48 CLEANFILES+= *.gcda *.gcno *.gcov
49
50 # Whether to compile using the Undefined Behavior Sanitizer (GCC, Clang).
51 USE_UBSAN?= no
52 .if ${USE_UBSAN} == "yes"
53 COPTS+= -fsanitize=undefined
54 LDADD+= -fsanitize=undefined
55 .endif
56
57 USE_META?= yes
58 .if ${USE_META:tl} != "no"
59
60 SRCS+= meta.c
61 CPPFLAGS+= -DUSE_META
62
63 USE_FILEMON?= ktrace
64 . if ${USE_FILEMON:tl} != "no"
65
66 .PATH: ${.CURDIR}/filemon
67 SRCS+= filemon_${USE_FILEMON}.c
68 CPPFLAGS+= -DUSE_FILEMON
69 CPPFLAGS+= -DUSE_FILEMON_${USE_FILEMON:tu}
70
71 . if ${USE_FILEMON} == "dev"
72 FILEMON_H?= /usr/include/dev/filemon/filemon.h
73 . if exists(${FILEMON_H}) && ${FILEMON_H:T} == "filemon.h"
74 COPTS.filemon_dev.c+= \
75 -DHAVE_FILEMON_H -I${FILEMON_H:H}
76 . endif
77 . endif
78 . endif
79 .endif
80
81 SUBDIR.roff+= PSD.doc
82 .if make(obj) || make(clean)
83 SUBDIR+= unit-tests
84 .endif
85
86 LINTFLAGS+= -T # strict bool mode, available since 2021-01-11
87 LINTFLAGS+= -w # treat warnings as errors
88 CLEANFILES+= *.o # for filemon objects
89
90 COPTS.arch.c+= ${GCC_NO_FORMAT_TRUNCATION}
91 COPTS.dir.c+= ${GCC_NO_FORMAT_TRUNCATION}
92 COPTS.job.c+= -Wno-format-nonliteral # custom shell templates
93 COPTS.main.c+= ${GCC_NO_FORMAT_TRUNCATION} ${GCC_NO_STRINGOP_TRUNCATION}
94 COPTS.meta.c+= ${GCC_NO_FORMAT_TRUNCATION}
95 COPTS.var.c+= -Wno-format-nonliteral # strftime
96
97 CPPFLAGS+= -DMAKE_NATIVE
98
99 .if defined(TOOLDIR)
100 # This is a native NetBSD build, use libutil rather than the local emalloc etc.
101 CPPFLAGS+= -DUSE_EMALLOC
102 LDADD+= -lutil
103 DPADD+= ${LIBUTIL}
104 .endif
105
106 COPTS+= -Wdeclaration-after-statement
107
108 # A simple unit-test driver to help catch regressions
109 TEST_MAKE ?= ${.OBJDIR}/${PROG:T}
110 test: .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
117 accept sync-mi: .MAKE
118 cd ${.CURDIR}/unit-tests && ${.MAKE} ${.TARGET}
119
120 retest:
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
134 test-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"
141 report-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.
153 CPPFLAGS+= ${USER_CPPFLAGS}
154 # For overriding -std=gnu99 or similar options.
155 CFLAGS+= ${USER_CFLAGS}
156