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# MFILES Optionally a list of interfaces used by the module. 32# This file contains a default list of interfaces. 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 63AWK?= awk 64KMODLOAD?= /sbin/kldload 65KMODUNLOAD?= /sbin/kldunload 66OBJCOPY?= objcopy 67 68.include <bsd.init.mk> 69# Grab all the options for a kernel build. For backwards compat, we need to 70# do this after bsd.own.mk. 71.include "kern.opts.mk" 72.include <bsd.compiler.mk> 73 74.SUFFIXES: .out .o .c .cc .cxx .C .y .l .s .S 75 76# amd64 and mips use direct linking for kmod, all others use shared binaries 77.if ${MACHINE_CPUARCH} != amd64 && ${MACHINE_CPUARCH} != mips 78__KLD_SHARED=yes 79.else 80__KLD_SHARED=no 81.endif 82 83.if !empty(CFLAGS:M-O[23s]) && empty(CFLAGS:M-fno-strict-aliasing) 84CFLAGS+= -fno-strict-aliasing 85.endif 86WERROR?= -Werror 87CFLAGS+= ${WERROR} 88CFLAGS+= -D_KERNEL 89CFLAGS+= -DKLD_MODULE 90 91# Don't use any standard or source-relative include directories. 92NOSTDINC= -nostdinc 93CFLAGS:= ${CFLAGS:N-I*} ${NOSTDINC} ${INCLMAGIC} ${CFLAGS:M-I*} 94.if defined(KERNBUILDDIR) 95CFLAGS+= -DHAVE_KERNEL_OPTION_HEADERS -include ${KERNBUILDDIR}/opt_global.h 96.endif 97 98# Add -I paths for system headers. Individual module makefiles don't 99# need any -I paths for this. Similar defaults for .PATH can't be 100# set because there are no standard paths for non-headers. 101CFLAGS+= -I. -I${SYSDIR} 102 103# Add -I path for altq headers as they are included via net/if_var.h 104# for example. 105CFLAGS+= -I${SYSDIR}/contrib/altq 106 107CFLAGS.gcc+= -finline-limit=${INLINE_LIMIT} 108CFLAGS.gcc+= --param inline-unit-growth=100 109CFLAGS.gcc+= --param large-function-growth=1000 110 111# Disallow common variables, and if we end up with commons from 112# somewhere unexpected, allocate storage for them in the module itself. 113CFLAGS+= -fno-common 114LDFLAGS+= -d -warn-common 115 116CFLAGS+= ${DEBUG_FLAGS} 117.if ${MACHINE_CPUARCH} == amd64 118CFLAGS+= -fno-omit-frame-pointer -mno-omit-leaf-frame-pointer 119.endif 120 121# Temporary workaround for PR 196407, which contains the fascinating details. 122.if ${MACHINE_CPUARCH} == arm 123CFLAGS.clang+= -mllvm -arm-use-movt=0 124.endif 125 126.if ${MACHINE_CPUARCH} == powerpc 127CFLAGS+= -mlongcall -fno-omit-frame-pointer 128.endif 129 130.if ${MACHINE_CPUARCH} == mips 131CFLAGS+= -G0 -fno-pic -mno-abicalls -mlong-calls 132.endif 133 134.if defined(DEBUG) || defined(DEBUG_FLAGS) 135CTFFLAGS+= -g 136.endif 137 138.if defined(FIRMWS) 139${KMOD:S/$/.c/}: ${SYSDIR}/tools/fw_stub.awk 140 ${AWK} -f ${SYSDIR}/tools/fw_stub.awk ${FIRMWS} -m${KMOD} -c${KMOD:S/$/.c/g} \ 141 ${FIRMWARE_LICENSE:C/.+/-l/}${FIRMWARE_LICENSE} 142 143SRCS+= ${KMOD:S/$/.c/} 144CLEANFILES+= ${KMOD:S/$/.c/} 145 146.for _firmw in ${FIRMWS} 147${_firmw:C/\:.*$/.fwo/}: ${_firmw:C/\:.*$//} 148 @${ECHO} ${_firmw:C/\:.*$//} ${.ALLSRC:M*${_firmw:C/\:.*$//}} 149 @if [ -e ${_firmw:C/\:.*$//} ]; then \ 150 ${LD} -b binary --no-warn-mismatch ${_LDFLAGS} \ 151 -r -d -o ${.TARGET} ${_firmw:C/\:.*$//}; \ 152 else \ 153 ln -s ${.ALLSRC:M*${_firmw:C/\:.*$//}} ${_firmw:C/\:.*$//}; \ 154 ${LD} -b binary --no-warn-mismatch ${_LDFLAGS} \ 155 -r -d -o ${.TARGET} ${_firmw:C/\:.*$//}; \ 156 rm ${_firmw:C/\:.*$//}; \ 157 fi 158 159OBJS+= ${_firmw:C/\:.*$/.fwo/} 160.endfor 161.endif 162 163OBJS+= ${SRCS:N*.h:R:S/$/.o/g} 164 165.if !defined(PROG) 166PROG= ${KMOD}.ko 167.endif 168 169.if !defined(DEBUG_FLAGS) 170FULLPROG= ${PROG} 171.else 172FULLPROG= ${PROG}.debug 173${PROG}: ${FULLPROG} ${PROG}.symbols 174 ${OBJCOPY} --strip-debug --add-gnu-debuglink=${PROG}.symbols\ 175 ${FULLPROG} ${.TARGET} 176${PROG}.symbols: ${FULLPROG} 177 ${OBJCOPY} --only-keep-debug ${FULLPROG} ${.TARGET} 178.endif 179 180.if ${__KLD_SHARED} == yes 181${FULLPROG}: ${KMOD}.kld 182 ${LD} -Bshareable ${_LDFLAGS} -o ${.TARGET} ${KMOD}.kld 183.if !defined(DEBUG_FLAGS) 184 ${OBJCOPY} --strip-debug ${.TARGET} 185.endif 186.endif 187 188EXPORT_SYMS?= NO 189.if ${EXPORT_SYMS} != YES 190CLEANFILES+= export_syms 191.endif 192 193.if ${__KLD_SHARED} == yes 194${KMOD}.kld: ${OBJS} 195.else 196${FULLPROG}: ${OBJS} 197.endif 198 ${LD} ${_LDFLAGS} -r -d -o ${.TARGET} ${OBJS} 199.if ${MK_CTF} != "no" 200 ${CTFMERGE} ${CTFFLAGS} -o ${.TARGET} ${OBJS} 201.endif 202.if defined(EXPORT_SYMS) 203.if ${EXPORT_SYMS} != YES 204.if ${EXPORT_SYMS} == NO 205 :> export_syms 206.elif !exists(${.CURDIR}/${EXPORT_SYMS}) 207 echo ${EXPORT_SYMS} > export_syms 208.else 209 grep -v '^#' < ${EXPORT_SYMS} > export_syms 210.endif 211 awk -f ${SYSDIR}/conf/kmod_syms.awk ${.TARGET} \ 212 export_syms | xargs -J% ${OBJCOPY} % ${.TARGET} 213.endif 214.endif 215.if !defined(DEBUG_FLAGS) && ${__KLD_SHARED} == no 216 ${OBJCOPY} --strip-debug ${.TARGET} 217.endif 218 219_ILINKS=machine 220.if ${MACHINE} != ${MACHINE_CPUARCH} 221_ILINKS+=${MACHINE_CPUARCH} 222.endif 223.if ${MACHINE_CPUARCH} == "i386" || ${MACHINE_CPUARCH} == "amd64" 224_ILINKS+=x86 225.endif 226CLEANFILES+=${_ILINKS} 227 228all: objwarn ${PROG} 229 230beforedepend: ${_ILINKS} 231 232# Ensure that the links exist without depending on it when it exists which 233# causes all the modules to be rebuilt when the directory pointed to changes. 234.for _link in ${_ILINKS} 235.if !exists(${.OBJDIR}/${_link}) 236${OBJS}: ${.OBJDIR}/${_link} 237.endif 238.endfor 239 240# Search for kernel source tree in standard places. 241.for _dir in ${.CURDIR}/../.. ${.CURDIR}/../../.. /sys /usr/src/sys 242.if !defined(SYSDIR) && exists(${_dir}/kern/) 243SYSDIR= ${_dir} 244.endif 245.endfor 246.if !defined(SYSDIR) || !exists(${SYSDIR}/kern/) 247.error "can't find kernel source tree" 248.endif 249 250.for _link in ${_ILINKS} 251.PHONY: ${_link} 252${_link}: ${.OBJDIR}/${_link} 253 254${.OBJDIR}/${_link}: 255 @case ${.TARGET:T} in \ 256 machine) \ 257 path=${SYSDIR}/${MACHINE}/include ;; \ 258 *) \ 259 path=${SYSDIR}/${.TARGET:T}/include ;; \ 260 esac ; \ 261 path=`(cd $$path && /bin/pwd)` ; \ 262 ${ECHO} ${.TARGET:T} "->" $$path ; \ 263 ln -sf $$path ${.TARGET:T} 264.endfor 265 266CLEANFILES+= ${PROG} ${KMOD}.kld ${OBJS} 267 268.if defined(DEBUG_FLAGS) 269CLEANFILES+= ${FULLPROG} ${PROG}.symbols 270.endif 271 272.if !target(install) 273 274_INSTALLFLAGS:= ${INSTALLFLAGS} 275.for ie in ${INSTALLFLAGS_EDIT} 276_INSTALLFLAGS:= ${_INSTALLFLAGS${ie}} 277.endfor 278 279.if !target(realinstall) 280realinstall: _kmodinstall 281.ORDER: beforeinstall _kmodinstall 282_kmodinstall: 283 ${INSTALL} -o ${KMODOWN} -g ${KMODGRP} -m ${KMODMODE} \ 284 ${_INSTALLFLAGS} ${PROG} ${DESTDIR}${KMODDIR} 285.if defined(DEBUG_FLAGS) && !defined(INSTALL_NODEBUG) && ${MK_KERNEL_SYMBOLS} != "no" 286 ${INSTALL} -o ${KMODOWN} -g ${KMODGRP} -m ${KMODMODE} \ 287 ${_INSTALLFLAGS} ${PROG}.symbols ${DESTDIR}${KMODDIR} 288.endif 289 290.include <bsd.links.mk> 291 292.if !defined(NO_XREF) 293afterinstall: _kldxref 294.ORDER: realinstall _kldxref 295.ORDER: _installlinks _kldxref 296_kldxref: 297 @if type kldxref >/dev/null 2>&1; then \ 298 ${ECHO} kldxref ${DESTDIR}${KMODDIR}; \ 299 kldxref ${DESTDIR}${KMODDIR}; \ 300 fi 301.endif 302.endif # !target(realinstall) 303 304.endif # !target(install) 305 306.if !target(load) 307load: ${PROG} 308 ${KMODLOAD} -v ${.OBJDIR}/${PROG} 309.endif 310 311.if !target(unload) 312unload: 313 ${KMODUNLOAD} -v ${PROG} 314.endif 315 316# Generate options files that otherwise would be built 317# in substantially similar ways through the tree. Move 318# the code here when they all produce identical results 319# (or should) 320.if !defined(KERNBUILDDIR) 321opt_bpf.h: 322 echo "#define DEV_BPF 1" > ${.TARGET} 323.if ${MK_INET_SUPPORT} != "no" 324opt_inet.h: 325 @echo "#define INET 1" > ${.TARGET} 326 @echo "#define TCP_OFFLOAD 1" >> ${.TARGET} 327.endif 328.if ${MK_INET6_SUPPORT} != "no" 329opt_inet6.h: 330 @echo "#define INET6 1" > ${.TARGET} 331.endif 332opt_mrouting.h: 333 echo "#define MROUTING 1" > ${.TARGET} 334opt_natm.h: 335 echo "#define NATM 1" > ${.TARGET} 336opt_scsi.h: 337 echo "#define SCSI_DELAY 15000" > ${.TARGET} 338opt_wlan.h: 339 echo "#define IEEE80211_DEBUG 1" > ${.TARGET} 340 echo "#define IEEE80211_AMPDU_AGE 1" >> ${.TARGET} 341 echo "#define IEEE80211_SUPPORT_MESH 1" >> ${.TARGET} 342.endif 343 344.if defined(KERNBUILDDIR) 345.PATH: ${KERNBUILDDIR} 346CFLAGS+= -I${KERNBUILDDIR} 347.for _src in ${SRCS:Mopt_*.h} 348CLEANFILES+= ${_src} 349.if !target(${_src}) 350${_src}: 351 ln -sf ${KERNBUILDDIR}/${_src} ${.TARGET} 352.endif 353.endfor 354.else 355.for _src in ${SRCS:Mopt_*.h} 356CLEANFILES+= ${_src} 357.if !target(${_src}) 358${_src}: 359 :> ${.TARGET} 360.endif 361.endfor 362.endif 363 364# Respect configuration-specific C flags. 365CFLAGS+= ${CONF_CFLAGS} 366 367MFILES?= dev/acpica/acpi_if.m dev/acpi_support/acpi_wmi_if.m \ 368 dev/agp/agp_if.m dev/ata/ata_if.m dev/eisa/eisa_if.m \ 369 dev/fb/fb_if.m dev/gpio/gpio_if.m dev/gpio/gpiobus_if.m \ 370 dev/iicbus/iicbb_if.m dev/iicbus/iicbus_if.m \ 371 dev/mmc/mmcbr_if.m dev/mmc/mmcbus_if.m \ 372 dev/mii/miibus_if.m dev/mvs/mvs_if.m dev/ofw/ofw_bus_if.m \ 373 dev/pccard/card_if.m dev/pccard/power_if.m dev/pci/pci_if.m \ 374 dev/pci/pcib_if.m dev/ppbus/ppbus_if.m \ 375 dev/sdhci/sdhci_if.m dev/smbus/smbus_if.m dev/spibus/spibus_if.m \ 376 dev/sound/pci/hda/hdac_if.m \ 377 dev/sound/pcm/ac97_if.m dev/sound/pcm/channel_if.m \ 378 dev/sound/pcm/feeder_if.m dev/sound/pcm/mixer_if.m \ 379 dev/sound/midi/mpu_if.m dev/sound/midi/mpufoi_if.m \ 380 dev/sound/midi/synth_if.m dev/usb/usb_if.m isa/isa_if.m \ 381 kern/bus_if.m kern/clock_if.m \ 382 kern/cpufreq_if.m kern/device_if.m kern/serdev_if.m \ 383 libkern/iconv_converter_if.m opencrypto/cryptodev_if.m \ 384 pc98/pc98/canbus_if.m dev/etherswitch/mdio_if.m 385 386.for _srcsrc in ${MFILES} 387.for _ext in c h 388.for _src in ${SRCS:M${_srcsrc:T:R}.${_ext}} 389CLEANFILES+= ${_src} 390.if !target(${_src}) 391${_src}: ${SYSDIR}/tools/makeobjops.awk ${SYSDIR}/${_srcsrc} 392 ${AWK} -f ${SYSDIR}/tools/makeobjops.awk ${SYSDIR}/${_srcsrc} -${_ext} 393.endif 394.endfor # _src 395.endfor # _ext 396.endfor # _srcsrc 397 398.if !empty(SRCS:Mvnode_if.c) 399CLEANFILES+= vnode_if.c 400vnode_if.c: ${SYSDIR}/tools/vnode_if.awk ${SYSDIR}/kern/vnode_if.src 401 ${AWK} -f ${SYSDIR}/tools/vnode_if.awk ${SYSDIR}/kern/vnode_if.src -c 402.endif 403 404.if !empty(SRCS:Mvnode_if.h) 405CLEANFILES+= vnode_if.h vnode_if_newproto.h vnode_if_typedef.h 406vnode_if.h vnode_if_newproto.h vnode_if_typedef.h: ${SYSDIR}/tools/vnode_if.awk \ 407 ${SYSDIR}/kern/vnode_if.src 408vnode_if.h: vnode_if_newproto.h vnode_if_typedef.h 409 ${AWK} -f ${SYSDIR}/tools/vnode_if.awk ${SYSDIR}/kern/vnode_if.src -h 410vnode_if_newproto.h: 411 ${AWK} -f ${SYSDIR}/tools/vnode_if.awk ${SYSDIR}/kern/vnode_if.src -p 412vnode_if_typedef.h: 413 ${AWK} -f ${SYSDIR}/tools/vnode_if.awk ${SYSDIR}/kern/vnode_if.src -q 414.endif 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:Musbdevs.h) 425CLEANFILES+= usbdevs.h 426usbdevs.h: ${SYSDIR}/tools/usbdevs2h.awk ${SYSDIR}/dev/usb/usbdevs 427 ${AWK} -f ${SYSDIR}/tools/usbdevs2h.awk ${SYSDIR}/dev/usb/usbdevs -h 428.endif 429 430.if !empty(SRCS:Musbdevs_data.h) 431CLEANFILES+= usbdevs_data.h 432usbdevs_data.h: ${SYSDIR}/tools/usbdevs2h.awk ${SYSDIR}/dev/usb/usbdevs 433 ${AWK} -f ${SYSDIR}/tools/usbdevs2h.awk ${SYSDIR}/dev/usb/usbdevs -d 434.endif 435 436.if !empty(SRCS:Macpi_quirks.h) 437CLEANFILES+= acpi_quirks.h 438acpi_quirks.h: ${SYSDIR}/tools/acpi_quirks2h.awk ${SYSDIR}/dev/acpica/acpi_quirks 439 ${AWK} -f ${SYSDIR}/tools/acpi_quirks2h.awk ${SYSDIR}/dev/acpica/acpi_quirks 440.endif 441 442.if !empty(SRCS:Massym.s) 443CLEANFILES+= assym.s genassym.o 444assym.s: genassym.o 445.if defined(KERNBUILDDIR) 446genassym.o: opt_global.h 447.endif 448assym.s: ${SYSDIR}/kern/genassym.sh 449 sh ${SYSDIR}/kern/genassym.sh genassym.o > ${.TARGET} 450genassym.o: ${SYSDIR}/${MACHINE_CPUARCH}/${MACHINE_CPUARCH}/genassym.c 451genassym.o: ${SRCS:Mopt_*.h} 452 ${CC} -c ${CFLAGS:N-fno-common} \ 453 ${SYSDIR}/${MACHINE_CPUARCH}/${MACHINE_CPUARCH}/genassym.c 454.endif 455 456lint: ${SRCS} 457 ${LINT} ${LINTKERNFLAGS} ${CFLAGS:M-[DILU]*} ${.ALLSRC:M*.c} 458 459.if defined(KERNBUILDDIR) 460${OBJS}: opt_global.h 461.endif 462 463.include <bsd.dep.mk> 464 465cleandepend: cleanilinks 466# .depend needs include links so we remove them only together. 467cleanilinks: 468 rm -f ${_ILINKS} 469 470.if !exists(${.OBJDIR}/${DEPENDFILE}) 471${OBJS}: ${SRCS:M*.h} 472.endif 473 474.include <bsd.obj.mk> 475.include "kern.mk" 476