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