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