Home | History | Annotate | Line # | Download | only in mk
bsd.sys.mk revision 1.319
      1 #	$NetBSD: bsd.sys.mk,v 1.319 2026/02/03 19:39:37 christos Exp $
      2 #
      3 # Build definitions used for NetBSD source tree builds.
      4 
      5 .if !defined(_BSD_SYS_MK_)
      6 _BSD_SYS_MK_=1
      7 
      8 .if !empty(.INCLUDEDFROMFILE:MMakefile*)
      9 error1:
     10 	@(echo "bsd.sys.mk should not be included from Makefiles" >& 2; exit 1)
     11 .endif
     12 .if !defined(_BSD_OWN_MK_)
     13 error2:
     14 	@(echo "bsd.own.mk must be included before bsd.sys.mk" >& 2; exit 1)
     15 .endif
     16 
     17 # XXX: LLVM does not support -iremap and -fdebug-*
     18 .if ${MKREPRO:Uno} == "yes" && ${MKLLVM:Uno} != "yes"
     19 .export NETBSDSRCDIR DESTDIR X11SRCDIR
     20 
     21 .if !empty(DESTDIR)
     22 CPPFLAGS+=	-Wp,-iremap,${DESTDIR}:
     23 REPROFLAGS+=	-fdebug-prefix-map=\$$DESTDIR=
     24 REPROFLAGS+=	-fmacro-prefix-map=\$$DESTDIR=
     25 .endif
     26 
     27 CPPFLAGS+=	-Wp,-fno-canonical-system-headers
     28 CPPFLAGS+=	-Wp,-iremap,${NETBSDSRCDIR}:/usr/src
     29 CPPFLAGS+=	-Wp,-iremap,${X11SRCDIR}:/usr/xsrc
     30 
     31 REPROFLAGS+=	-fdebug-prefix-map=\$$NETBSDSRCDIR=/usr/src
     32 CREPROFLAGS+=	-fmacro-prefix-map=\$$NETBSDSRCDIR=/usr/src
     33 REPROFLAGS+=	-fdebug-prefix-map=\$$X11SRCDIR=/usr/xsrc
     34 CREPROFLAGS+=	-fmacro-prefix-map=\$$X11SRCDIR=/usr/xsrc
     35 .if defined(MAKEOBJDIRPREFIX)
     36 NETBSDOBJDIR=	${MAKEOBJDIRPREFIX}${NETBSDSRCDIR}
     37 .endif
     38 
     39 .if defined(NETBSDOBJDIR)
     40 .export NETBSDOBJDIR
     41 REPROFLAGS+=	-fdebug-prefix-map=\$$NETBSDOBJDIR=/usr/obj
     42 REPROFLAGS+=	-fmacro-prefix-map=\$$NETBSDOBJDIR=/usr/obj
     43 .endif
     44 
     45 LINTFLAGS+=	-R${NETBSDSRCDIR}=/usr/src -R${X11SRCDIR}=/usr/xsrc
     46 LINTFLAGS+=	-R${DESTDIR}=
     47 
     48 # XXX: Cannot handle MAKEOBJDIR, yet.
     49 REPROFLAGS+=	-fdebug-regex-map='/usr/src/(.*)/obj$$=/usr/obj/\1'
     50 REPROFLAGS+=	-fdebug-regex-map='/usr/src/(.*)/obj/(.*)=/usr/obj/\1/\2'
     51 REPROFLAGS+=	-fdebug-regex-map='/usr/src/(.*)/obj\..*=/usr/obj/\1'
     52 REPROFLAGS+=	-fdebug-regex-map='/usr/src/(.*)/obj\..*/(.*)=/usr/obj/\1/\2'
     53 
     54 CFLAGS+=	${REPROFLAGS} ${CREPROFLAGS}
     55 CXXFLAGS+=	${REPROFLAGS} ${CREPROFLAGS}
     56 FFLAGS+=	${REPROFLAGS}
     57 .endif
     58 
     59 # NetBSD sources use C99 style, with some GCC extensions.
     60 # Coverity does not like -std=gnu99 (XXX untested but likely not gnu11 either)
     61 #
     62 # XXX Ideally we would not rely on GNU extensions, but currently some
     63 # code uses alloca(3) which -std=c11 compiles broken-at-runtime.  Until
     64 # we fix that (PR toolchain/58969: use of alloca is warning, not error,
     65 # with -std=c11), we have to continue using -std=gnu11 and not
     66 # -std=c11.
     67 .if !defined(COVERITY_TOP_CONFIG) && empty(CFLAGS:M*-std=*)
     68 CFLAGS+=	${${ACTIVE_CC} == "clang":? -std=gnu11 :}
     69 CFLAGS+=	${${ACTIVE_CC} == "gcc":? -std=gnu11 :}
     70 CFLAGS+=	${${ACTIVE_CC} == "pcc":? -std=gnu11 :}
     71 .endif
     72 
     73 .if defined(WARNS)
     74 CFLAGS+=	${${ACTIVE_CC} == "clang":? -Wno-sign-compare -Wno-pointer-sign :}
     75 .if ${WARNS} > 0
     76 CFLAGS+=	-Wall -Wstrict-prototypes -Wmissing-prototypes -Wpointer-arith
     77 #CFLAGS+=	-Wmissing-declarations -Wredundant-decls -Wnested-externs
     78 # Add -Wno-sign-compare.  -Wsign-compare is included in -Wall as of GCC 3.3,
     79 # but our sources aren't up for it yet. Also, add -Wno-traditional because
     80 # gcc includes #elif in the warnings, which is 'this code will not compile
     81 # in a traditional environment' warning, as opposed to 'this code behaves
     82 # differently in traditional and ansi environments' which is the warning
     83 # we wanted, and now we don't get anymore.
     84 CFLAGS+=	-Wno-sign-compare
     85 # Don't suppress warnings coming from constructs in system headers.
     86 # Our system headers should be clean and we want to warn about things like:
     87 # isdigit((char)1)
     88 CFLAGS+=	${${ACTIVE_CC} == "gcc" :? -Wsystem-headers :}
     89 CFLAGS+=	${${ACTIVE_CC} == "gcc" :? -Wno-traditional :}
     90 .if !defined(NOGCCERROR)
     91 # Set assembler warnings to be fatal
     92 CFLAGS+=	${${ACTIVE_CC} == "gcc" :? -Wa,--fatal-warnings :}
     93 .endif
     94 
     95 # Set linker warnings to be fatal
     96 # XXX no proper way to avoid "FOO is a patented algorithm" warnings
     97 # XXX on linking static libs
     98 .if (!defined(MKPIC) || ${MKPIC} != "no") && \
     99     (!defined(LDSTATIC) || ${LDSTATIC} != "-static")
    100 # XXX there are some strange problems not yet resolved
    101 . if !defined(HAVE_GCC) || defined(HAVE_LLVM)
    102 LDFLAGS+=	-Wl,--fatal-warnings
    103 . endif
    104 .endif
    105 .endif
    106 
    107 LDFLAGS+=	-Wl,--warn-shared-textrel
    108 
    109 .if ${WARNS} > 1
    110 CFLAGS+=	-Wreturn-type -Wswitch -Wshadow
    111 .endif
    112 .if ${WARNS} > 2
    113 CFLAGS+=	-Wcast-qual -Wwrite-strings
    114 CFLAGS+=	-Wextra -Wno-unused-parameter
    115 # Readd -Wno-sign-compare to override -Wextra with clang
    116 CFLAGS+=	-Wno-sign-compare
    117 .if "${ACTIVE_CC}" == "gcc" && ${HAVE_GCC} < 8
    118 #  XXX: Won't warn about anything.  -Wabi warns about differences from
    119 #  the most up-to-date ABI, which in g++ 8 is used by default.
    120 CXXFLAGS+=	-Wabi
    121 .endif
    122 CXXFLAGS+=	-Wold-style-cast
    123 CXXFLAGS+=	-Wctor-dtor-privacy -Wnon-virtual-dtor -Wreorder \
    124 		-Wno-deprecated -Woverloaded-virtual -Wsign-promo -Wsynth
    125 CXXFLAGS+=	${${ACTIVE_CXX} == "gcc":? -Wno-non-template-friend -Wno-pmf-conversions :}
    126 .endif
    127 .if ${WARNS} > 3 && (defined(HAVE_GCC) || defined(HAVE_LLVM))
    128 .if ${WARNS} > 4
    129 CFLAGS+=	-Wold-style-definition
    130 .endif
    131 .if ${WARNS} > 5
    132 CFLAGS+=	-Wconversion
    133 .endif
    134 CFLAGS+=	-Wsign-compare -Wformat=2
    135 CFLAGS+=	${${ACTIVE_CC} == "gcc":? -Wno-format-zero-length :}
    136 .endif
    137 .if ${WARNS} > 3 && defined(HAVE_LLVM)
    138 CFLAGS+=	${${ACTIVE_CC} == "clang":? -Wpointer-sign -Wmissing-noreturn :}
    139 .endif
    140 .if (defined(HAVE_GCC) \
    141      && (${MACHINE_ARCH} == "coldfire" || \
    142 	 ${MACHINE_CPU} == "sh3" || \
    143 	 ${MACHINE_CPU} == "m68k"))
    144 # XXX GCC 4.5 for sh3 and m68k (which we compile with -Os) is extra noisy for
    145 # cases it should be better with
    146 CFLAGS+=	-Wno-uninitialized
    147 CFLAGS+=	-Wno-maybe-uninitialized
    148 .endif
    149 .endif
    150 
    151 .if ${MKRELRO:Uno} != "no"
    152 LDFLAGS+=	-Wl,-z,relro
    153 .endif
    154 
    155 .if ${MKRELRO:Uno} == "full" && ${NOFULLRELRO:Uno} == "no"
    156 LDFLAGS+=	-Wl,-z,now
    157 .endif
    158 
    159 .if ${MKSANITIZER:Uno} == "yes"
    160 SANITIZERFLAGS:=	-fsanitize=${USE_SANITIZER} ${SANITIZERFLAGS}
    161 .else
    162 SANITIZERFLAGS=		# empty
    163 .endif
    164 
    165 .if ${MKLIBCSANITIZER:Uno} == "yes"
    166 LIBCSANITIZERFLAGS:=	-fsanitize=${USE_LIBCSANITIZER} ${LIBCSANITIZERFLAGS}
    167 LIBCSANITIZERFLAGS+=	-fno-sanitize=vptr	# Unsupported in micro-UBSan
    168 .else
    169 LIBCSANITIZERFLAGS=	# empty
    170 .endif
    171 
    172 CWARNFLAGS+=	${CWARNFLAGS.${ACTIVE_CC}}
    173 
    174 CPPFLAGS+=	${AUDIT:D-D__AUDIT__}
    175 _NOWERROR=	${defined(NOGCCERROR) || (${ACTIVE_CC} == "clang" && defined(NOCLANGERROR)):?yes:no}
    176 CFLAGS+=	${${_NOWERROR} == "no" :?-Werror:} ${CWARNFLAGS}
    177 .if !empty(DESTDIR)
    178 LINTFLAGS+=	-d ${DESTDIR}
    179 .endif
    180 
    181 .if !defined(NOSSP) && (${USE_SSP:Uno} != "no") && (${BINDIR:Ux} != "/usr/mdec")
    182 .   if !defined(KERNSRCDIR) && !defined(KERN) # not for kernels / kern modules
    183 CPPFLAGS+=	-D_FORTIFY_SOURCE=2
    184 .   endif
    185 .   if !defined(COVERITY_TOP_CONFIG)
    186 COPTS+=	-fstack-protector -Wstack-protector 
    187 
    188 # GCC 4.8 on m68k erroneously does not protect functions with
    189 # variables needing special alignment, see
    190 #	http://gcc.gnu.org/bugzilla/show_bug.cgi?id=59674
    191 # (the underlying issue for sh and vax may be different, needs more
    192 # investigation, symptoms are similar but for different sources)
    193 # also true for GCC 5, assume GCC 6 too.
    194 .	if "${ACTIVE_CC}" == "gcc" && \
    195      ( ${HAVE_GCC} == "5" || \
    196        ${HAVE_GCC} == "6" ) && \
    197      ( ${MACHINE_CPU} == "sh3" || \
    198        ${MACHINE_ARCH} == "vax" || \
    199        ${MACHINE_CPU} == "m68k" || \
    200        ${MACHINE_CPU} == "or1k" )
    201 COPTS+=	-Wno-error=stack-protector 
    202 .	endif
    203 
    204 COPTS+=	${${ACTIVE_CC} == "clang":? --param ssp-buffer-size=1 :}
    205 COPTS+=	${${ACTIVE_CC} == "gcc":? --param ssp-buffer-size=1 :}
    206 .   endif
    207 .endif
    208 
    209 .if ${MKSOFTFLOAT:Uno} != "no"
    210 # sh3 defaults to soft-float and specifies hard-float a different way
    211 .if ${MACHINE_CPU} != "sh3"
    212 COPTS+=		${${ACTIVE_CC} == "gcc":? -msoft-float :}
    213 FOPTS+=		-msoft-float
    214 .endif
    215 .elif ${MACHINE_ARCH} == "coldfire"
    216 COPTS+=		-mhard-float
    217 FOPTS+=		-mhard-float
    218 .endif
    219 
    220 #.if !empty(MACHINE_ARCH:Mearmv7*)
    221 #COPTS+=		-mthumb
    222 #FOPTS+=		-mthumb
    223 #.endif
    224 
    225 .if ${MKIEEEFP:Uno} != "no"
    226 .if ${MACHINE_ARCH} == "alpha"
    227 CFLAGS+=	-mieee
    228 FFLAGS+=	-mieee
    229 .endif
    230 .endif
    231 
    232 .if ${MACHINE} == "sparc64" && ${MACHINE_ARCH} == "sparc"
    233 CFLAGS+=	-Wa,-Av8plus
    234 .endif
    235 
    236 .if !defined(NOGCCERROR)
    237 .if ${MACHINE_MIPS64} || ${MACHINE_CPU} == "m68k"
    238 CPUFLAGS+=	-Wa,--fatal-warnings
    239 .endif
    240 .endif
    241 
    242 #.if ${MACHINE} == "sbmips"
    243 #CFLAGS+=	-mips64 -mtune=sb1
    244 #.endif
    245 
    246 #.if ${MACHINE_MIPS64} && defined(MKPIC) && ${MKPIC} == "no"
    247 #CPUFLAGS+=	-mno-abicalls -fno-PIC
    248 #.endif
    249 CFLAGS+=	${CPUFLAGS}
    250 AFLAGS+=	${CPUFLAGS}
    251 
    252 .if ${KCOV:U0} > 0
    253 KCOVFLAGS=	-fsanitize-coverage=trace-pc,trace-cmp
    254 .for f in subr_kcov.c subr_asan.c subr_csan.c subr_msan.c ubsan.c
    255 KCOVFLAGS.${f}=		# empty
    256 .endfor
    257 CFLAGS+=	${KCOVFLAGS.${.IMPSRC:T}:U${KCOVFLAGS}}
    258 .endif
    259 
    260 .if !defined(NOPIE) && (!defined(LDSTATIC) || ${LDSTATIC} != "-static")
    261 # Position Independent Executable flags
    262 PIE_CFLAGS?=        -fPIE
    263 PIE_LDFLAGS?=       -pie ${${ACTIVE_CC} == "gcc":? -shared-libgcc :}
    264 PIE_AFLAGS?=	    -fPIE
    265 .endif
    266 
    267 ARM_ELF2AOUT?=	elf2aout
    268 M68K_ELF2AOUT?=	elf2aout
    269 MIPS_ELF2ECOFF?=	elf2ecoff
    270 MKDEP?=		mkdep
    271 MKDEPCXX?=	mkdep
    272 OBJCOPY?=	objcopy
    273 OBJDUMP?=	objdump
    274 PAXCTL?=	paxctl
    275 STRIP?=		strip
    276 
    277 .SUFFIXES:	.o .ln .lo .c .cc .cpp .cxx .C .m ${YHEADER:D.h}
    278 
    279 # C
    280 .c.o:
    281 	${_MKTARGET_COMPILE}
    282 	${COMPILE.c} ${COPTS.${.IMPSRC:T}} ${CPUFLAGS.${.IMPSRC:T}} ${CPPFLAGS.${.IMPSRC:T}} ${.IMPSRC} ${OBJECT_TARGET}
    283 	${CTFCONVERT_RUN}
    284 
    285 .c.ln:
    286 	${_MKTARGET_COMPILE}
    287 	${LINT} ${LINTFLAGS} ${LINTFLAGS.${.IMPSRC:T}} \
    288 	    ${CPPFLAGS:C/-([IDUW])[  ]*/-\1/Wg:M-[IDUW]*} \
    289 	    ${CPPFLAGS.${.IMPSRC:T}:C/-([IDUW])[  ]*/-\1/Wg:M-[IDUW]*} \
    290 	    -i ${.IMPSRC}
    291 
    292 # C++
    293 .cc.o .cpp.o .cxx.o .C.o:
    294 	${_MKTARGET_COMPILE}
    295 	${COMPILE.cc} ${COPTS.${.IMPSRC:T}} ${CPUFLAGS.${.IMPSRC:T}} ${CPPFLAGS.${.IMPSRC:T}} ${.IMPSRC}
    296 
    297 # Objective C
    298 # (Defined here rather than in <sys.mk> because `.m' is not just
    299 #  used for Objective C source)
    300 .m.o:
    301 	${_MKTARGET_COMPILE}
    302 	${COMPILE.m} ${OBJCOPTS} ${OBJCOPTS.${.IMPSRC:T}} ${.IMPSRC} ${OBJECT_TARGET}
    303 	${CTFCONVERT_RUN}
    304 
    305 # Host-compiled C objects
    306 # The intermediate step is necessary for Sun CC, which objects to calling
    307 # object files anything but *.o
    308 .c.lo:
    309 	${_MKTARGET_COMPILE}
    310 	${HOST_COMPILE.c} -o ${.TARGET}.o ${COPTS.${.IMPSRC:T}} ${CPUFLAGS.${.IMPSRC:T}} ${CPPFLAGS.${.IMPSRC:T}} ${.IMPSRC}
    311 	${MV} ${.TARGET}.o ${.TARGET}
    312 
    313 # C++
    314 .cc.lo .cpp.lo .cxx.lo .C.lo:
    315 	${_MKTARGET_COMPILE}
    316 	${HOST_COMPILE.cc} -o ${.TARGET}.o ${COPTS.${.IMPSRC:T}} ${CPUFLAGS.${.IMPSRC:T}} ${CPPFLAGS.${.IMPSRC:T}} ${.IMPSRC}
    317 	${MV} ${.TARGET}.o ${.TARGET}
    318 
    319 # Assembly
    320 .s.o:
    321 	${_MKTARGET_COMPILE}
    322 	${COMPILE.s} ${COPTS.${.IMPSRC:T}} ${CPUFLAGS.${.IMPSRC:T}} ${CPPFLAGS.${.IMPSRC:T}} ${.IMPSRC} ${OBJECT_TARGET}
    323 	${CTFCONVERT_RUN}
    324 
    325 .S.o:
    326 	${_MKTARGET_COMPILE}
    327 	${COMPILE.S} ${COPTS.${.IMPSRC:T}} ${CPUFLAGS.${.IMPSRC:T}} ${CPPFLAGS.${.IMPSRC:T}} ${.IMPSRC} ${OBJECT_TARGET}
    328 	${CTFCONVERT_RUN}
    329 
    330 # Lex
    331 LFLAGS+=	${LPREFIX.${.IMPSRC:T}:D-P${LPREFIX.${.IMPSRC:T}}}
    332 LFLAGS+=	${LPREFIX:D-P${LPREFIX}} ${LFLAGS.${.IMPSRC:T}}
    333 
    334 .l.c:
    335 	${_MKTARGET_LEX}
    336 	${LEX.l} -o${.TARGET} ${.IMPSRC}
    337 
    338 # Yacc
    339 YFLAGS+=	${YPREFIX.${.IMPSRC:T}:D-p${YPREFIX.${.IMPSRC:T}}} ${YHEADER.${.IMPSRC:T}:D-d}
    340 YFLAGS+=	${YPREFIX:D-p${YPREFIX}} ${YHEADER:D-d} ${YFLAGS.${.IMPSRC:T}}
    341 
    342 .y.c:
    343 	${_MKTARGET_YACC}
    344 	${YACC.y} -o ${.TARGET} ${.IMPSRC}
    345 
    346 .ifdef YHEADER
    347 .if empty(.MAKEFLAGS:M-n)
    348 .y.h: ${.TARGET:.h=.c}
    349 .endif
    350 .endif
    351 
    352 # Objcopy
    353 .if ${MACHINE_ARCH} == aarch64eb
    354 # AARCH64 big endian needs to preserve $x/$d symbols for the linker.
    355 OBJCOPYLIBFLAGS_EXTRA=-w -K '[$$][dx]' -K '[$$][dx]\.*'
    356 .elif ${MACHINE_CPU} == "arm"
    357 # ARM big endian needs to preserve $a/$d/$t symbols for the linker.
    358 OBJCOPYLIBFLAGS_EXTRA=-w -K '[$$][adt]' -K '[$$][adt]\.*'
    359 .endif
    360 
    361 .if ${MKSTRIPSYM} != "no"
    362 OBJCOPYLIBFLAGS?=${"${.TARGET:M*.po}" != "":?-X:-x} ${OBJCOPYLIBFLAGS_EXTRA}
    363 .else
    364 OBJCOPYLIBFLAGS?=-X ${OBJCOPYLIBFLAGS_EXTRA}
    365 .endif
    366 
    367 .endif	# !defined(_BSD_SYS_MK_)
    368