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 ${.CURDIR}/../.. ${.CURDIR}/../../.. /sys /usr/src/sys 87.if exists(${_dir}/kern/) 88SYSDIR= ${_dir:tA} 89.endif 90.endfor 91.endif 92.if !defined(SYSDIR) || !exists(${SYSDIR}/kern/) 93.error "can't find kernel source tree" 94.endif 95.endif 96 97.SUFFIXES: .out .o .c .cc .cxx .C .y .l .s .S .m 98 99# amd64 and mips use direct linking for kmod, all others use shared binaries 100.if ${MACHINE_CPUARCH} != amd64 && ${MACHINE_CPUARCH} != mips 101__KLD_SHARED=yes 102.else 103__KLD_SHARED=no 104.endif 105 106.if !empty(CFLAGS:M-O[23s]) && empty(CFLAGS:M-fno-strict-aliasing) 107CFLAGS+= -fno-strict-aliasing 108.endif 109WERROR?= -Werror 110CFLAGS+= ${WERROR} 111CFLAGS+= -D_KERNEL 112CFLAGS+= -DKLD_MODULE 113 114# Don't use any standard or source-relative include directories. 115NOSTDINC= -nostdinc 116CFLAGS:= ${CFLAGS:N-I*} ${NOSTDINC} ${INCLMAGIC} ${CFLAGS:M-I*} 117.if defined(KERNBUILDDIR) 118CFLAGS+= -DHAVE_KERNEL_OPTION_HEADERS -include ${KERNBUILDDIR}/opt_global.h 119.endif 120 121# Add -I paths for system headers. Individual module makefiles don't 122# need any -I paths for this. Similar defaults for .PATH can't be 123# set because there are no standard paths for non-headers. 124CFLAGS+= -I. -I${SYSDIR} 125 126CFLAGS.gcc+= -finline-limit=${INLINE_LIMIT} 127CFLAGS.gcc+= -fms-extensions 128CFLAGS.gcc+= --param inline-unit-growth=100 129CFLAGS.gcc+= --param large-function-growth=1000 130 131# Disallow common variables, and if we end up with commons from 132# somewhere unexpected, allocate storage for them in the module itself. 133CFLAGS+= -fno-common 134LDFLAGS+= -d -warn-common 135 136.if defined(LINKER_FEATURES) && ${LINKER_FEATURES:Mbuild-id} 137LDFLAGS+= -Wl,--build-id=sha1 138.endif 139 140CFLAGS+= ${DEBUG_FLAGS} 141.if ${MACHINE_CPUARCH} == amd64 142CFLAGS+= -fno-omit-frame-pointer -mno-omit-leaf-frame-pointer 143.endif 144 145.if ${MACHINE_CPUARCH} == "aarch64" || ${MACHINE_CPUARCH} == "riscv" 146CFLAGS+= -fPIC 147.endif 148 149# Temporary workaround for PR 196407, which contains the fascinating details. 150# Don't allow clang to use fpu instructions or registers in kernel modules. 151.if ${MACHINE_CPUARCH} == arm 152.if ${COMPILER_VERSION} < 30800 153CFLAGS.clang+= -mllvm -arm-use-movt=0 154.else 155CFLAGS.clang+= -mno-movt 156.endif 157CFLAGS.clang+= -mfpu=none 158CFLAGS+= -funwind-tables 159.endif 160 161.if ${MACHINE_CPUARCH} == powerpc 162CFLAGS+= -mlongcall -fno-omit-frame-pointer 163.endif 164 165.if ${MACHINE_CPUARCH} == mips 166CFLAGS+= -G0 -fno-pic -mno-abicalls -mlong-calls 167.endif 168 169.if defined(DEBUG) || defined(DEBUG_FLAGS) 170CTFFLAGS+= -g 171.endif 172 173.if defined(FIRMWS) 174${KMOD:S/$/.c/}: ${SYSDIR}/tools/fw_stub.awk 175 ${AWK} -f ${SYSDIR}/tools/fw_stub.awk ${FIRMWS} -m${KMOD} -c${KMOD:S/$/.c/g} \ 176 ${FIRMWARE_LICENSE:C/.+/-l/}${FIRMWARE_LICENSE} 177 178SRCS+= ${KMOD:S/$/.c/} 179CLEANFILES+= ${KMOD:S/$/.c/} 180 181.for _firmw in ${FIRMWS} 182${_firmw:C/\:.*$/.fwo/:T}: ${_firmw:C/\:.*$//} 183 @${ECHO} ${_firmw:C/\:.*$//} ${.ALLSRC:M*${_firmw:C/\:.*$//}} 184 @if [ -e ${_firmw:C/\:.*$//} ]; then \ 185 ${LD} -b binary --no-warn-mismatch ${_LDFLAGS} \ 186 -m ${LD_EMULATION} -r -d \ 187 -o ${.TARGET} ${_firmw:C/\:.*$//}; \ 188 else \ 189 ln -s ${.ALLSRC:M*${_firmw:C/\:.*$//}} ${_firmw:C/\:.*$//}; \ 190 ${LD} -b binary --no-warn-mismatch ${_LDFLAGS} \ 191 -m ${LD_EMULATION} -r -d \ 192 -o ${.TARGET} ${_firmw:C/\:.*$//}; \ 193 rm ${_firmw:C/\:.*$//}; \ 194 fi 195 196OBJS+= ${_firmw:C/\:.*$/.fwo/:T} 197.endfor 198.endif 199 200# Conditionally include SRCS based on kernel config options. 201.for _o in ${KERN_OPTS} 202SRCS+=${SRCS.${_o}} 203.endfor 204 205OBJS+= ${SRCS:N*.h:R:S/$/.o/g} 206 207.if !defined(PROG) 208PROG= ${KMOD}.ko 209.endif 210 211.if !defined(DEBUG_FLAGS) 212FULLPROG= ${PROG} 213.else 214FULLPROG= ${PROG}.full 215${PROG}: ${FULLPROG} ${PROG}.debug 216 ${OBJCOPY} --strip-debug --add-gnu-debuglink=${PROG}.debug \ 217 ${FULLPROG} ${.TARGET} 218${PROG}.debug: ${FULLPROG} 219 ${OBJCOPY} --only-keep-debug ${FULLPROG} ${.TARGET} 220.endif 221 222.if ${__KLD_SHARED} == yes 223${FULLPROG}: ${KMOD}.kld 224 ${LD} -m ${LD_EMULATION} -Bshareable -znotext ${_LDFLAGS} \ 225 -o ${.TARGET} ${KMOD}.kld 226.if !defined(DEBUG_FLAGS) 227 ${OBJCOPY} --strip-debug ${.TARGET} 228.endif 229.endif 230 231EXPORT_SYMS?= NO 232.if ${EXPORT_SYMS} != YES 233CLEANFILES+= export_syms 234.endif 235 236.if ${__KLD_SHARED} == yes 237${KMOD}.kld: ${OBJS} 238.else 239${FULLPROG}: ${OBJS} 240.endif 241 ${LD} -m ${LD_EMULATION} ${_LDFLAGS} -r -d -o ${.TARGET} ${OBJS} 242.if ${MK_CTF} != "no" 243 ${CTFMERGE} ${CTFFLAGS} -o ${.TARGET} ${OBJS} 244.endif 245.if defined(EXPORT_SYMS) 246.if ${EXPORT_SYMS} != YES 247.if ${EXPORT_SYMS} == NO 248 :> export_syms 249.elif !exists(${.CURDIR}/${EXPORT_SYMS}) 250 echo -n "${EXPORT_SYMS:@s@$s${.newline}@}" > export_syms 251.else 252 grep -v '^#' < ${EXPORT_SYMS} > export_syms 253.endif 254 ${AWK} -f ${SYSDIR}/conf/kmod_syms.awk ${.TARGET} \ 255 export_syms | xargs -J% ${OBJCOPY} % ${.TARGET} 256.endif 257.endif # defined(EXPORT_SYMS) 258.if defined(PREFIX_SYMS) 259 ${AWK} -v prefix=${PREFIX_SYMS} -f ${SYSDIR}/conf/kmod_syms_prefix.awk \ 260 ${.TARGET} /dev/null | xargs -J% ${OBJCOPY} % ${.TARGET} 261.endif 262.if !defined(DEBUG_FLAGS) && ${__KLD_SHARED} == no 263 ${OBJCOPY} --strip-debug ${.TARGET} 264.endif 265 266_ILINKS=machine 267.if ${MACHINE} != ${MACHINE_CPUARCH} && ${MACHINE} != "arm64" 268_ILINKS+=${MACHINE_CPUARCH} 269.endif 270.if ${MACHINE_CPUARCH} == "i386" || ${MACHINE_CPUARCH} == "amd64" 271_ILINKS+=x86 272.endif 273CLEANFILES+=${_ILINKS} 274 275all: ${PROG} 276 277beforedepend: ${_ILINKS} 278beforebuild: ${_ILINKS} 279 280# Ensure that the links exist without depending on it when it exists which 281# causes all the modules to be rebuilt when the directory pointed to changes. 282.for _link in ${_ILINKS} 283.if !exists(${.OBJDIR}/${_link}) 284OBJS_DEPEND_GUESS+= ${_link} 285.endif 286.endfor 287 288.NOPATH: ${_ILINKS} 289 290${_ILINKS}: 291 @case ${.TARGET} in \ 292 machine) \ 293 path=${SYSDIR}/${MACHINE}/include ;; \ 294 *) \ 295 path=${SYSDIR}/${.TARGET:T}/include ;; \ 296 esac ; \ 297 path=`(cd $$path && /bin/pwd)` ; \ 298 ${ECHO} ${.TARGET:T} "->" $$path ; \ 299 ln -fns $$path ${.TARGET:T} 300 301CLEANFILES+= ${PROG} ${KMOD}.kld ${OBJS} 302 303.if defined(DEBUG_FLAGS) 304CLEANFILES+= ${FULLPROG} ${PROG}.debug 305.endif 306 307.if !target(install) 308 309_INSTALLFLAGS:= ${INSTALLFLAGS} 310.for ie in ${INSTALLFLAGS_EDIT} 311_INSTALLFLAGS:= ${_INSTALLFLAGS${ie}} 312.endfor 313 314.if !target(realinstall) 315KERN_DEBUGDIR?= ${DEBUGDIR} 316realinstall: _kmodinstall 317.ORDER: beforeinstall _kmodinstall 318_kmodinstall: .PHONY 319 ${INSTALL} -T release -o ${KMODOWN} -g ${KMODGRP} -m ${KMODMODE} \ 320 ${_INSTALLFLAGS} ${PROG} ${DESTDIR}${KMODDIR}/ 321.if defined(DEBUG_FLAGS) && !defined(INSTALL_NODEBUG) && ${MK_KERNEL_SYMBOLS} != "no" 322 ${INSTALL} -T debug -o ${KMODOWN} -g ${KMODGRP} -m ${KMODMODE} \ 323 ${_INSTALLFLAGS} ${PROG}.debug ${DESTDIR}${KERN_DEBUGDIR}${KMODDIR}/ 324.endif 325 326.include <bsd.links.mk> 327 328.if !defined(NO_XREF) 329afterinstall: _kldxref 330.ORDER: realinstall _kldxref 331.ORDER: _installlinks _kldxref 332_kldxref: .PHONY 333 @if type kldxref >/dev/null 2>&1; then \ 334 ${ECHO} kldxref ${DESTDIR}${KMODDIR}; \ 335 kldxref ${DESTDIR}${KMODDIR}; \ 336 fi 337.endif 338.endif # !target(realinstall) 339 340.endif # !target(install) 341 342.if !target(load) 343load: ${PROG} .PHONY 344 ${KMODLOAD} -v ${.OBJDIR}/${PROG} 345.endif 346 347.if !target(unload) 348unload: .PHONY 349 if ${KMODISLOADED} ${PROG} ; then ${KMODUNLOAD} -v ${PROG} ; fi 350.endif 351 352.if !target(reload) 353reload: unload load .PHONY 354.endif 355 356.if defined(KERNBUILDDIR) 357.PATH: ${KERNBUILDDIR} 358CFLAGS+= -I${KERNBUILDDIR} 359.for _src in ${SRCS:Mopt_*.h} 360CLEANFILES+= ${_src} 361.if !target(${_src}) 362${_src}: 363 ln -sf ${KERNBUILDDIR}/${_src} ${.TARGET} 364.endif 365.endfor 366.else 367.for _src in ${SRCS:Mopt_*.h} 368CLEANFILES+= ${_src} 369.if !target(${_src}) 370${_src}: 371 :> ${.TARGET} 372.endif 373.endfor 374.endif 375 376# Respect configuration-specific C flags. 377CFLAGS+= ${ARCH_FLAGS} ${CONF_CFLAGS} 378 379.if !empty(SRCS:Mvnode_if.c) 380CLEANFILES+= vnode_if.c 381vnode_if.c: ${SYSDIR}/tools/vnode_if.awk ${SYSDIR}/kern/vnode_if.src 382 ${AWK} -f ${SYSDIR}/tools/vnode_if.awk ${SYSDIR}/kern/vnode_if.src -c 383.endif 384 385.if !empty(SRCS:Mvnode_if.h) 386CLEANFILES+= vnode_if.h vnode_if_newproto.h vnode_if_typedef.h 387vnode_if.h vnode_if_newproto.h vnode_if_typedef.h: ${SYSDIR}/tools/vnode_if.awk \ 388 ${SYSDIR}/kern/vnode_if.src 389vnode_if.h: vnode_if_newproto.h vnode_if_typedef.h 390 ${AWK} -f ${SYSDIR}/tools/vnode_if.awk ${SYSDIR}/kern/vnode_if.src -h 391vnode_if_newproto.h: 392 ${AWK} -f ${SYSDIR}/tools/vnode_if.awk ${SYSDIR}/kern/vnode_if.src -p 393vnode_if_typedef.h: 394 ${AWK} -f ${SYSDIR}/tools/vnode_if.awk ${SYSDIR}/kern/vnode_if.src -q 395.endif 396 397# Build _if.[ch] from _if.m, and clean them when we're done. 398# __MPATH defined in config.mk 399_MFILES=${__MPATH:T:O} 400_MPATH=${__MPATH:H:O:u} 401.PATH.m: ${_MPATH} 402.for _i in ${SRCS:M*_if.[ch]} 403_MATCH=M${_i:R:S/$/.m/} 404_MATCHES=${_MFILES:${_MATCH}} 405.if !empty(_MATCHES) 406CLEANFILES+= ${_i} 407.endif 408.endfor # _i 409.m.c: ${SYSDIR}/tools/makeobjops.awk 410 ${AWK} -f ${SYSDIR}/tools/makeobjops.awk ${.IMPSRC} -c 411 412.m.h: ${SYSDIR}/tools/makeobjops.awk 413 ${AWK} -f ${SYSDIR}/tools/makeobjops.awk ${.IMPSRC} -h 414 415.for _i in mii pccard 416.if !empty(SRCS:M${_i}devs.h) 417CLEANFILES+= ${_i}devs.h 418${_i}devs.h: ${SYSDIR}/tools/${_i}devs2h.awk ${SYSDIR}/dev/${_i}/${_i}devs 419 ${AWK} -f ${SYSDIR}/tools/${_i}devs2h.awk ${SYSDIR}/dev/${_i}/${_i}devs 420.endif 421.endfor # _i 422 423.if !empty(SRCS:Mbhnd_nvram_map.h) 424CLEANFILES+= bhnd_nvram_map.h 425bhnd_nvram_map.h: ${SYSDIR}/dev/bhnd/tools/nvram_map_gen.awk \ 426 ${SYSDIR}/dev/bhnd/tools/nvram_map_gen.sh \ 427 ${SYSDIR}/dev/bhnd/nvram/nvram_map 428bhnd_nvram_map.h: 429 sh ${SYSDIR}/dev/bhnd/tools/nvram_map_gen.sh \ 430 ${SYSDIR}/dev/bhnd/nvram/nvram_map -h 431.endif 432 433.if !empty(SRCS:Mbhnd_nvram_map_data.h) 434CLEANFILES+= bhnd_nvram_map_data.h 435bhnd_nvram_map_data.h: ${SYSDIR}/dev/bhnd/tools/nvram_map_gen.awk \ 436 ${SYSDIR}/dev/bhnd/tools/nvram_map_gen.sh \ 437 ${SYSDIR}/dev/bhnd/nvram/nvram_map 438bhnd_nvram_map_data.h: 439 sh ${SYSDIR}/dev/bhnd/tools/nvram_map_gen.sh \ 440 ${SYSDIR}/dev/bhnd/nvram/nvram_map -d 441.endif 442 443.if !empty(SRCS:Musbdevs.h) 444CLEANFILES+= usbdevs.h 445usbdevs.h: ${SYSDIR}/tools/usbdevs2h.awk ${SYSDIR}/dev/usb/usbdevs 446 ${AWK} -f ${SYSDIR}/tools/usbdevs2h.awk ${SYSDIR}/dev/usb/usbdevs -h 447.endif 448 449.if !empty(SRCS:Musbdevs_data.h) 450CLEANFILES+= usbdevs_data.h 451usbdevs_data.h: ${SYSDIR}/tools/usbdevs2h.awk ${SYSDIR}/dev/usb/usbdevs 452 ${AWK} -f ${SYSDIR}/tools/usbdevs2h.awk ${SYSDIR}/dev/usb/usbdevs -d 453.endif 454 455.if !empty(SRCS:Macpi_quirks.h) 456CLEANFILES+= acpi_quirks.h 457acpi_quirks.h: ${SYSDIR}/tools/acpi_quirks2h.awk ${SYSDIR}/dev/acpica/acpi_quirks 458 ${AWK} -f ${SYSDIR}/tools/acpi_quirks2h.awk ${SYSDIR}/dev/acpica/acpi_quirks 459.endif 460 461.if !empty(SRCS:Massym.inc) || !empty(DPSRCS:Massym.inc) 462CLEANFILES+= assym.inc genassym.o 463DEPENDOBJS+= genassym.o 464assym.inc: genassym.o 465.if defined(KERNBUILDDIR) 466genassym.o: opt_global.h 467.endif 468assym.inc: ${SYSDIR}/kern/genassym.sh 469 sh ${SYSDIR}/kern/genassym.sh genassym.o > ${.TARGET} 470genassym.o: ${SYSDIR}/${MACHINE}/${MACHINE}/genassym.c 471genassym.o: ${SRCS:Mopt_*.h} 472 ${CC} -c ${CFLAGS:N-flto:N-fno-common} \ 473 ${SYSDIR}/${MACHINE}/${MACHINE}/genassym.c 474.endif 475 476.if defined(KERNBUILDDIR) 477${OBJS}: opt_global.h 478.endif 479 480CLEANDEPENDFILES+= ${_ILINKS} 481# .depend needs include links so we remove them only together. 482cleanilinks: 483 rm -f ${_ILINKS} 484 485OBJS_DEPEND_GUESS+= ${SRCS:M*.h} 486 487.include <bsd.dep.mk> 488.include <bsd.clang-analyze.mk> 489.include <bsd.obj.mk> 490.include "kern.mk" 491