1# From: @(#)bsd.prog.mk 5.26 (Berkeley) 6/25/91 2# $FreeBSD$ 3# 4# The include file <bsd.kmod.mk> handles building and installing loadable 5# kernel modules. 6# 7# 8# +++ variables +++ 9# 10# CLEANFILES Additional files to remove for the clean and cleandir targets. 11# 12# EXPORT_SYMS A list of symbols that should be exported from the module, 13# or the name of a file containing a list of symbols, or YES 14# to export all symbols. If not defined, no symbols are 15# exported. 16# 17# KMOD The name of the kernel module to build. 18# 19# KMODDIR Base path for kernel modules (see kld(4)). [/boot/kernel] 20# 21# KMODOWN Module file owner. [${BINOWN}] 22# 23# KMODGRP Module file group. [${BINGRP}] 24# 25# KMODMODE Module file mode. [${BINMODE}] 26# 27# KMODLOAD Command to load a kernel module [/sbin/kldload] 28# 29# KMODUNLOAD Command to unload a kernel module [/sbin/kldunload] 30# 31# KMODISLOADED Command to check whether a kernel module is 32# loaded [/sbin/kldstat -q -n] 33# 34# PROG The name of the kernel module to build. 35# If not supplied, ${KMOD}.ko is used. 36# 37# SRCS List of source files. 38# 39# FIRMWS List of firmware images in format filename:shortname:version 40# 41# FIRMWARE_LICENSE 42# Set to the name of the license the user has to agree on in 43# order to use this firmware. See /usr/share/doc/legal 44# 45# DESTDIR The tree where the module gets installed. [not set] 46# 47# KERNBUILDDIR 48# Set to the location of the kernel build directory where 49# the opt_*.h files, .o's and kernel winds up. 50# 51# +++ targets +++ 52# 53# install: 54# install the kernel module; if the Makefile 55# does not itself define the target install, the targets 56# beforeinstall and afterinstall may also be used to cause 57# actions immediately before and after the install target 58# is executed. 59# 60# load: 61# Load a module. 62# 63# unload: 64# Unload a module. 65# 66# reload: 67# Unload if loaded, then load. 68# 69 70AWK?= awk 71KMODLOAD?= /sbin/kldload 72KMODUNLOAD?= /sbin/kldunload 73KMODISLOADED?= /sbin/kldstat -q -n 74OBJCOPY?= objcopy 75 76.include <bsd.init.mk> 77# Grab all the options for a kernel build. For backwards compat, we need to 78# do this after bsd.own.mk. 79.include "kern.opts.mk" 80.include <bsd.compiler.mk> 81.include "config.mk" 82 83# Search for kernel source tree in standard places. 84.if empty(KERNBUILDDIR) 85.if !defined(SYSDIR) 86.for _dir in ${SRCTOP:D${SRCTOP}/sys} \ 87 ${.CURDIR}/../.. ${.CURDIR}/../../.. /sys /usr/src/sys 88.if !defined(SYSDIR) && exists(${_dir}/kern/) 89SYSDIR= ${_dir:tA} 90.endif 91.endfor 92.endif 93.if !defined(SYSDIR) || !exists(${SYSDIR}/kern/) 94.error "can't find kernel source tree" 95.endif 96.endif 97 98.SUFFIXES: .out .o .c .cc .cxx .C .y .l .s .S .m 99 100# amd64 and mips use direct linking for kmod, all others use shared binaries 101.if ${MACHINE_CPUARCH} != amd64 && ${MACHINE_CPUARCH} != mips 102__KLD_SHARED=yes 103.else 104__KLD_SHARED=no 105.endif 106 107.if !empty(CFLAGS:M-O[23s]) && empty(CFLAGS:M-fno-strict-aliasing) 108CFLAGS+= -fno-strict-aliasing 109.endif 110.if ${COMPILER_TYPE} == "gcc" && ${COMPILER_VERSION} < 50000 111WERROR?= -Wno-error 112.else 113WERROR?= -Werror 114.endif 115 116CFLAGS+= ${WERROR} 117CFLAGS+= -D_KERNEL 118CFLAGS+= -DKLD_MODULE 119.if defined(MODULE_TIED) 120CFLAGS+= -DKLD_TIED 121.endif 122 123# Don't use any standard or source-relative include directories. 124NOSTDINC= -nostdinc 125CFLAGS:= ${CFLAGS:N-I*} ${NOSTDINC} ${INCLMAGIC} ${CFLAGS:M-I*} 126.if defined(KERNBUILDDIR) 127CFLAGS+= -DHAVE_KERNEL_OPTION_HEADERS -include ${KERNBUILDDIR}/opt_global.h 128.endif 129 130# Add -I paths for system headers. Individual module makefiles don't 131# need any -I paths for this. Similar defaults for .PATH can't be 132# set because there are no standard paths for non-headers. 133CFLAGS+= -I. -I${SYSDIR} -I${SYSDIR}/contrib/ck/include 134 135CFLAGS.gcc+= -finline-limit=${INLINE_LIMIT} 136CFLAGS.gcc+= -fms-extensions 137CFLAGS.gcc+= --param inline-unit-growth=100 138CFLAGS.gcc+= --param large-function-growth=1000 139 140# Disallow common variables, and if we end up with commons from 141# somewhere unexpected, allocate storage for them in the module itself. 142CFLAGS+= -fno-common 143LDFLAGS+= -d -warn-common 144 145.if defined(LINKER_FEATURES) && ${LINKER_FEATURES:Mbuild-id} 146LDFLAGS+= --build-id=sha1 147.endif 148 149CFLAGS+= ${DEBUG_FLAGS} 150.if ${MACHINE_CPUARCH} == amd64 151CFLAGS+= -fno-omit-frame-pointer -mno-omit-leaf-frame-pointer 152.endif 153 154.if ${MACHINE_CPUARCH} == "aarch64" || ${MACHINE_CPUARCH} == "riscv" 155CFLAGS+= -fPIC 156.endif 157 158# Temporary workaround for PR 196407, which contains the fascinating details. 159# Don't allow clang to use fpu instructions or registers in kernel modules. 160.if ${MACHINE_CPUARCH} == arm 161.if ${COMPILER_VERSION} < 30800 162CFLAGS.clang+= -mllvm -arm-use-movt=0 163.else 164CFLAGS.clang+= -mno-movt 165.endif 166CFLAGS.clang+= -mfpu=none 167CFLAGS+= -funwind-tables 168.endif 169 170.if ${MACHINE_CPUARCH} == powerpc 171CFLAGS+= -mlongcall -fno-omit-frame-pointer 172.endif 173 174.if ${MACHINE_CPUARCH} == mips 175CFLAGS+= -G0 -fno-pic -mno-abicalls -mlong-calls 176.endif 177 178.if defined(DEBUG) || defined(DEBUG_FLAGS) 179CTFFLAGS+= -g 180.endif 181 182.if defined(FIRMWS) 183${KMOD:S/$/.c/}: ${SYSDIR}/tools/fw_stub.awk 184 ${AWK} -f ${SYSDIR}/tools/fw_stub.awk ${FIRMWS} -m${KMOD} -c${KMOD:S/$/.c/g} \ 185 ${FIRMWARE_LICENSE:C/.+/-l/}${FIRMWARE_LICENSE} 186 187SRCS+= ${KMOD:S/$/.c/} 188CLEANFILES+= ${KMOD:S/$/.c/} 189 190.for _firmw in ${FIRMWS} 191${_firmw:C/\:.*$/.fwo/:T}: ${_firmw:C/\:.*$//} 192 @${ECHO} ${_firmw:C/\:.*$//} ${.ALLSRC:M*${_firmw:C/\:.*$//}} 193 @if [ -e ${_firmw:C/\:.*$//} ]; then \ 194 ${LD} -b binary --no-warn-mismatch ${_LDFLAGS} \ 195 -m ${LD_EMULATION} -r -d \ 196 -o ${.TARGET} ${_firmw:C/\:.*$//}; \ 197 else \ 198 ln -s ${.ALLSRC:M*${_firmw:C/\:.*$//}} ${_firmw:C/\:.*$//}; \ 199 ${LD} -b binary --no-warn-mismatch ${_LDFLAGS} \ 200 -m ${LD_EMULATION} -r -d \ 201 -o ${.TARGET} ${_firmw:C/\:.*$//}; \ 202 rm ${_firmw:C/\:.*$//}; \ 203 fi 204 205OBJS+= ${_firmw:C/\:.*$/.fwo/:T} 206.endfor 207.endif 208 209# Conditionally include SRCS based on kernel config options. 210.for _o in ${KERN_OPTS} 211SRCS+=${SRCS.${_o}} 212.endfor 213 214OBJS+= ${SRCS:N*.h:R:S/$/.o/g} 215 216.if !defined(PROG) 217PROG= ${KMOD}.ko 218.endif 219 220.if !defined(DEBUG_FLAGS) 221FULLPROG= ${PROG} 222.else 223FULLPROG= ${PROG}.full 224${PROG}: ${FULLPROG} ${PROG}.debug 225 ${OBJCOPY} --strip-debug --add-gnu-debuglink=${PROG}.debug \ 226 ${FULLPROG} ${.TARGET} 227${PROG}.debug: ${FULLPROG} 228 ${OBJCOPY} --only-keep-debug ${FULLPROG} ${.TARGET} 229.endif 230 231.if ${__KLD_SHARED} == yes 232${FULLPROG}: ${KMOD}.kld 233 ${LD} -m ${LD_EMULATION} -Bshareable -znotext ${_LDFLAGS} \ 234 -o ${.TARGET} ${KMOD}.kld 235.if !defined(DEBUG_FLAGS) 236 ${OBJCOPY} --strip-debug ${.TARGET} 237.endif 238.endif 239 240EXPORT_SYMS?= NO 241.if ${EXPORT_SYMS} != YES 242CLEANFILES+= export_syms 243.endif 244 245.if ${__KLD_SHARED} == yes 246${KMOD}.kld: ${OBJS} 247.else 248${FULLPROG}: ${OBJS} 249.endif 250.if !defined(FIRMWS) && (${MACHINE_CPUARCH} == "i386") 251 ${LD} -m ${LD_EMULATION} ${_LDFLAGS} -r \ 252 -T ${SYSDIR}/conf/ldscript.set_padding \ 253 -d -o ${.TARGET} ${OBJS} 254.else 255 ${LD} -m ${LD_EMULATION} ${_LDFLAGS} -r -d -o ${.TARGET} ${OBJS} 256.endif 257.if ${MK_CTF} != "no" 258 ${CTFMERGE} ${CTFFLAGS} -o ${.TARGET} ${OBJS} 259.endif 260.if defined(EXPORT_SYMS) 261.if ${EXPORT_SYMS} != YES 262.if ${EXPORT_SYMS} == NO 263 :> export_syms 264.elif !exists(${.CURDIR}/${EXPORT_SYMS}) 265 echo -n "${EXPORT_SYMS:@s@$s${.newline}@}" > export_syms 266.else 267 grep -v '^#' < ${EXPORT_SYMS} > export_syms 268.endif 269 ${AWK} -f ${SYSDIR}/conf/kmod_syms.awk ${.TARGET} \ 270 export_syms | xargs -J% ${OBJCOPY} % ${.TARGET} 271.endif 272.endif # defined(EXPORT_SYMS) 273.if defined(PREFIX_SYMS) 274 ${AWK} -v prefix=${PREFIX_SYMS} -f ${SYSDIR}/conf/kmod_syms_prefix.awk \ 275 ${.TARGET} /dev/null | xargs -J% ${OBJCOPY} % ${.TARGET} 276.endif 277.if !defined(DEBUG_FLAGS) && ${__KLD_SHARED} == no 278 ${OBJCOPY} --strip-debug ${.TARGET} 279.endif 280 281.if ${COMPILER_TYPE} == "clang" || \ 282 (${COMPILER_TYPE} == "gcc" && ${COMPILER_VERSION} >= 60000) 283_MAP_DEBUG_PREFIX= yes 284.endif 285 286_ILINKS=machine 287.if ${MACHINE} != ${MACHINE_CPUARCH} && ${MACHINE} != "arm64" 288_ILINKS+=${MACHINE_CPUARCH} 289.endif 290.if ${MACHINE_CPUARCH} == "i386" || ${MACHINE_CPUARCH} == "amd64" 291_ILINKS+=x86 292.endif 293CLEANFILES+=${_ILINKS} 294 295all: ${PROG} 296 297beforedepend: ${_ILINKS} 298beforebuild: ${_ILINKS} 299 300# Ensure that the links exist without depending on it when it exists which 301# causes all the modules to be rebuilt when the directory pointed to changes. 302# Ensure that debug info references the path in the source tree. 303.for _link in ${_ILINKS} 304.if !exists(${.OBJDIR}/${_link}) 305OBJS_DEPEND_GUESS+= ${_link} 306.endif 307.if defined(_MAP_DEBUG_PREFIX) 308.if ${_link} == "machine" 309CFLAGS+= -fdebug-prefix-map=./machine=${SYSDIR}/${MACHINE}/include 310.else 311CFLAGS+= -fdebug-prefix-map=./${_link}=${SYSDIR}/${_link}/include 312.endif 313.endif 314.endfor 315 316.NOPATH: ${_ILINKS} 317 318${_ILINKS}: 319 @case ${.TARGET} in \ 320 machine) \ 321 path=${SYSDIR}/${MACHINE}/include ;; \ 322 *) \ 323 path=${SYSDIR}/${.TARGET:T}/include ;; \ 324 esac ; \ 325 path=`(cd $$path && /bin/pwd)` ; \ 326 ${ECHO} ${.TARGET:T} "->" $$path ; \ 327 ln -fns $$path ${.TARGET:T} 328 329CLEANFILES+= ${PROG} ${KMOD}.kld ${OBJS} 330 331.if defined(DEBUG_FLAGS) 332CLEANFILES+= ${FULLPROG} ${PROG}.debug 333.endif 334 335.if !target(install) 336 337_INSTALLFLAGS:= ${INSTALLFLAGS} 338.for ie in ${INSTALLFLAGS_EDIT} 339_INSTALLFLAGS:= ${_INSTALLFLAGS${ie}} 340.endfor 341 342.if !target(realinstall) 343KERN_DEBUGDIR?= ${DEBUGDIR} 344realinstall: _kmodinstall 345.ORDER: beforeinstall _kmodinstall 346_kmodinstall: .PHONY 347 ${INSTALL} -T release -o ${KMODOWN} -g ${KMODGRP} -m ${KMODMODE} \ 348 ${_INSTALLFLAGS} ${PROG} ${DESTDIR}${KMODDIR}/ 349.if defined(DEBUG_FLAGS) && !defined(INSTALL_NODEBUG) && ${MK_KERNEL_SYMBOLS} != "no" 350 ${INSTALL} -T debug -o ${KMODOWN} -g ${KMODGRP} -m ${KMODMODE} \ 351 ${_INSTALLFLAGS} ${PROG}.debug ${DESTDIR}${KERN_DEBUGDIR}${KMODDIR}/ 352.endif 353 354.include <bsd.links.mk> 355 356.if !defined(NO_XREF) 357afterinstall: _kldxref 358.ORDER: realinstall _kldxref 359.ORDER: _installlinks _kldxref 360_kldxref: .PHONY 361 @if type kldxref >/dev/null 2>&1; then \ 362 ${ECHO} ${KLDXREF_CMD} ${DESTDIR}${KMODDIR}; \ 363 ${KLDXREF_CMD} ${DESTDIR}${KMODDIR}; \ 364 fi 365.endif 366.endif # !target(realinstall) 367 368.endif # !target(install) 369 370.if !target(load) 371load: ${PROG} .PHONY 372 ${KMODLOAD} -v ${.OBJDIR}/${PROG} 373.endif 374 375.if !target(unload) 376unload: .PHONY 377 if ${KMODISLOADED} ${PROG} ; then ${KMODUNLOAD} -v ${PROG} ; fi 378.endif 379 380.if !target(reload) 381reload: unload load .PHONY 382.endif 383 384.if defined(KERNBUILDDIR) 385.PATH: ${KERNBUILDDIR} 386CFLAGS+= -I${KERNBUILDDIR} 387.for _src in ${SRCS:Mopt_*.h} 388CLEANFILES+= ${_src} 389.if !target(${_src}) 390${_src}: 391 ln -sf ${KERNBUILDDIR}/${_src} ${.TARGET} 392.endif 393.endfor 394.else 395.for _src in ${SRCS:Mopt_*.h} 396CLEANFILES+= ${_src} 397.if !target(${_src}) 398${_src}: 399 :> ${.TARGET} 400.endif 401.endfor 402.endif 403 404# Add the sanitizer C flags 405CFLAGS+= ${SAN_CFLAGS} 406 407# Add the gcov flags 408CFLAGS+= ${GCOV_CFLAGS} 409 410# Respect configuration-specific C flags. 411CFLAGS+= ${ARCH_FLAGS} ${CONF_CFLAGS} 412 413.if !empty(SRCS:Mvnode_if.c) 414CLEANFILES+= vnode_if.c 415vnode_if.c: ${SYSDIR}/tools/vnode_if.awk ${SYSDIR}/kern/vnode_if.src 416 ${AWK} -f ${SYSDIR}/tools/vnode_if.awk ${SYSDIR}/kern/vnode_if.src -c 417.endif 418 419.if !empty(SRCS:Mvnode_if.h) 420CLEANFILES+= vnode_if.h vnode_if_newproto.h vnode_if_typedef.h 421vnode_if.h vnode_if_newproto.h vnode_if_typedef.h: ${SYSDIR}/tools/vnode_if.awk \ 422 ${SYSDIR}/kern/vnode_if.src 423vnode_if.h: vnode_if_newproto.h vnode_if_typedef.h 424 ${AWK} -f ${SYSDIR}/tools/vnode_if.awk ${SYSDIR}/kern/vnode_if.src -h 425vnode_if_newproto.h: 426 ${AWK} -f ${SYSDIR}/tools/vnode_if.awk ${SYSDIR}/kern/vnode_if.src -p 427vnode_if_typedef.h: 428 ${AWK} -f ${SYSDIR}/tools/vnode_if.awk ${SYSDIR}/kern/vnode_if.src -q 429.endif 430 431# Build _if.[ch] from _if.m, and clean them when we're done. 432# __MPATH defined in config.mk 433_MFILES=${__MPATH:T:O} 434_MPATH=${__MPATH:H:O:u} 435.PATH.m: ${_MPATH} 436.for _i in ${SRCS:M*_if.[ch]} 437_MATCH=M${_i:R:S/$/.m/} 438_MATCHES=${_MFILES:${_MATCH}} 439.if !empty(_MATCHES) 440CLEANFILES+= ${_i} 441.endif 442.endfor # _i 443.m.c: ${SYSDIR}/tools/makeobjops.awk 444 ${AWK} -f ${SYSDIR}/tools/makeobjops.awk ${.IMPSRC} -c 445 446.m.h: ${SYSDIR}/tools/makeobjops.awk 447 ${AWK} -f ${SYSDIR}/tools/makeobjops.awk ${.IMPSRC} -h 448 449.for _i in mii pccard 450.if !empty(SRCS:M${_i}devs.h) 451CLEANFILES+= ${_i}devs.h 452${_i}devs.h: ${SYSDIR}/tools/${_i}devs2h.awk ${SYSDIR}/dev/${_i}/${_i}devs 453 ${AWK} -f ${SYSDIR}/tools/${_i}devs2h.awk ${SYSDIR}/dev/${_i}/${_i}devs 454.endif 455.endfor # _i 456 457.if !empty(SRCS:Mbhnd_nvram_map.h) 458CLEANFILES+= bhnd_nvram_map.h 459bhnd_nvram_map.h: ${SYSDIR}/dev/bhnd/tools/nvram_map_gen.awk \ 460 ${SYSDIR}/dev/bhnd/tools/nvram_map_gen.sh \ 461 ${SYSDIR}/dev/bhnd/nvram/nvram_map 462bhnd_nvram_map.h: 463 sh ${SYSDIR}/dev/bhnd/tools/nvram_map_gen.sh \ 464 ${SYSDIR}/dev/bhnd/nvram/nvram_map -h 465.endif 466 467.if !empty(SRCS:Mbhnd_nvram_map_data.h) 468CLEANFILES+= bhnd_nvram_map_data.h 469bhnd_nvram_map_data.h: ${SYSDIR}/dev/bhnd/tools/nvram_map_gen.awk \ 470 ${SYSDIR}/dev/bhnd/tools/nvram_map_gen.sh \ 471 ${SYSDIR}/dev/bhnd/nvram/nvram_map 472bhnd_nvram_map_data.h: 473 sh ${SYSDIR}/dev/bhnd/tools/nvram_map_gen.sh \ 474 ${SYSDIR}/dev/bhnd/nvram/nvram_map -d 475.endif 476 477.if !empty(SRCS:Musbdevs.h) 478CLEANFILES+= usbdevs.h 479usbdevs.h: ${SYSDIR}/tools/usbdevs2h.awk ${SYSDIR}/dev/usb/usbdevs 480 ${AWK} -f ${SYSDIR}/tools/usbdevs2h.awk ${SYSDIR}/dev/usb/usbdevs -h 481.endif 482 483.if !empty(SRCS:Musbdevs_data.h) 484CLEANFILES+= usbdevs_data.h 485usbdevs_data.h: ${SYSDIR}/tools/usbdevs2h.awk ${SYSDIR}/dev/usb/usbdevs 486 ${AWK} -f ${SYSDIR}/tools/usbdevs2h.awk ${SYSDIR}/dev/usb/usbdevs -d 487.endif 488 489.if !empty(SRCS:Msdiodevs.h) 490CLEANFILES+= sdiodevs.h 491sdiodevs.h: ${SYSDIR}/tools/sdiodevs2h.awk ${SYSDIR}/dev/sdio/sdiodevs 492 ${AWK} -f ${SYSDIR}/tools/sdiodevs2h.awk ${SYSDIR}/dev/sdio/sdiodevs -h 493.endif 494 495.if !empty(SRCS:Msdiodevs_data.h) 496CLEANFILES+= sdiodevs_data.h 497sdiodevs_data.h: ${SYSDIR}/tools/sdiodevs2h.awk ${SYSDIR}/dev/sdio/sdiodevs 498 ${AWK} -f ${SYSDIR}/tools/sdiodevs2h.awk ${SYSDIR}/dev/sdio/sdiodevs -d 499.endif 500 501.if !empty(SRCS:Macpi_quirks.h) 502CLEANFILES+= acpi_quirks.h 503acpi_quirks.h: ${SYSDIR}/tools/acpi_quirks2h.awk ${SYSDIR}/dev/acpica/acpi_quirks 504 ${AWK} -f ${SYSDIR}/tools/acpi_quirks2h.awk ${SYSDIR}/dev/acpica/acpi_quirks 505.endif 506 507.if !empty(SRCS:Massym.inc) || !empty(DPSRCS:Massym.inc) 508CLEANFILES+= assym.inc genassym.o 509DEPENDOBJS+= genassym.o 510DPSRCS+= offset.inc 511.endif 512.if defined(MODULE_TIED) 513DPSRCS+= offset.inc 514.endif 515.if !empty(SRCS:Moffset.inc) || !empty(DPSRCS:Moffset.inc) 516CLEANFILES+= offset.inc genoffset.o 517DEPENDOBJS+= genoffset.o 518.endif 519assym.inc: genassym.o 520offset.inc: genoffset.o 521assym.inc: ${SYSDIR}/kern/genassym.sh 522 sh ${SYSDIR}/kern/genassym.sh genassym.o > ${.TARGET} 523genassym.o: ${SYSDIR}/${MACHINE}/${MACHINE}/genassym.c offset.inc 524genassym.o: ${SRCS:Mopt_*.h} 525 ${CC} -c ${CFLAGS:N-flto:N-fno-common} \ 526 ${SYSDIR}/${MACHINE}/${MACHINE}/genassym.c 527offset.inc: ${SYSDIR}/kern/genoffset.sh genoffset.o 528 sh ${SYSDIR}/kern/genoffset.sh genoffset.o > ${.TARGET} 529genoffset.o: ${SYSDIR}/kern/genoffset.c 530genoffset.o: ${SRCS:Mopt_*.h} 531 ${CC} -c ${CFLAGS:N-flto:N-fno-common} \ 532 ${SYSDIR}/kern/genoffset.c 533 534CLEANDEPENDFILES+= ${_ILINKS} 535# .depend needs include links so we remove them only together. 536cleanilinks: 537 rm -f ${_ILINKS} 538 539OBJS_DEPEND_GUESS+= ${SRCS:M*.h} 540.if defined(KERNBUILDDIR) 541OBJS_DEPEND_GUESS+= opt_global.h 542.endif 543 544.include <bsd.dep.mk> 545.include <bsd.clang-analyze.mk> 546.include <bsd.obj.mk> 547.include "kern.mk" 548