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