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