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