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# DESTDIR The tree where the module gets installed. [not set] 42# 43# +++ targets +++ 44# 45# install: 46# install the kernel module; if the Makefile 47# does not itself define the target install, the targets 48# beforeinstall and afterinstall may also be used to cause 49# actions immediately before and after the install target 50# is executed. 51# 52# load: 53# Load a module. 54# 55# unload: 56# Unload a module. 57# 58 59AWK?= awk 60KMODLOAD?= /sbin/kldload 61KMODUNLOAD?= /sbin/kldunload 62OBJCOPY?= objcopy 63 64.if defined(KMODDEPS) 65.error "Do not use KMODDEPS on 5.0+; use MODULE_VERSION/MODULE_DEPEND" 66.endif 67 68.include <bsd.init.mk> 69 70.SUFFIXES: .out .o .c .cc .cxx .C .y .l .s .S 71 72.if ${CC} == "icc" 73CFLAGS:= ${CFLAGS:C/(-x[^M^K^W]+)[MKW]+|-x[MKW]+/\1/} 74.else 75. if !empty(CFLAGS:M-O[23s]) && empty(CFLAGS:M-fno-strict-aliasing) 76CFLAGS+= -fno-strict-aliasing 77. endif 78WERROR?= -Werror 79.endif 80CFLAGS+= ${WERROR} 81CFLAGS+= -D_KERNEL 82CFLAGS+= -DKLD_MODULE 83 84# Don't use any standard or source-relative include directories. 85.if ${CC} == "icc" 86NOSTDINC= -X 87.else 88C_DIALECT= -std=c99 89NOSTDINC= -nostdinc 90.endif 91CFLAGS+= ${C_DIALECT} 92CFLAGS:= ${CFLAGS:N-I*} ${NOSTDINC} -I- ${INCLMAGIC} ${CFLAGS:M-I*} 93.if defined(KERNBUILDDIR) 94CFLAGS+= -DHAVE_KERNEL_OPTION_HEADERS -include ${KERNBUILDDIR}/opt_global.h 95.endif 96 97# Add -I paths for system headers. Individual module makefiles don't 98# need any -I paths for this. Similar defaults for .PATH can't be 99# set because there are no standard paths for non-headers. 100CFLAGS+= -I. -I@ 101 102# Add -I path for altq headers as they are included via net/if_var.h 103# for example. 104CFLAGS+= -I@/contrib/altq 105 106.if ${CC} != "icc" 107CFLAGS+= -finline-limit=${INLINE_LIMIT} 108CFLAGS+= --param inline-unit-growth=100 109CFLAGS+= --param large-function-growth=1000 110.endif 111 112# Disallow common variables, and if we end up with commons from 113# somewhere unexpected, allocate storage for them in the module itself. 114.if ${CC} != "icc" 115CFLAGS+= -fno-common 116.endif 117LDFLAGS+= -d -warn-common 118 119CFLAGS+= ${DEBUG_FLAGS} 120.if ${MACHINE_ARCH} == amd64 121CFLAGS+= -fno-omit-frame-pointer 122.endif 123 124.if ${MACHINE_ARCH} == "powerpc" 125CFLAGS+= -mlongcall -fno-omit-frame-pointer 126.endif 127 128.if defined(FIRMWS) 129.if !exists(@) 130${KMOD:S/$/.c/}: @ 131.else 132${KMOD:S/$/.c/}: @/tools/fw_stub.awk 133.endif 134 ${AWK} -f @/tools/fw_stub.awk ${FIRMWS} -m${KMOD} -c${KMOD:S/$/.c/g} 135 136SRCS+= ${KMOD:S/$/.c/} 137CLEANFILES+= ${KMOD:S/$/.c/} 138 139.for _firmw in ${FIRMWS} 140${_firmw:C/\:.*$/.fwo/}: ${_firmw:C/\:.*$//} 141 @${ECHO} ${_firmw:C/\:.*$//} ${.ALLSRC:M*${_firmw:C/\:.*$//}} 142 @if [ -e ${_firmw:C/\:.*$//} ]; then \ 143 ${LD} -b binary ${LDFLAGS} -r -d -o ${.TARGET} \ 144 ${_firmw:C/\:.*$//}; \ 145 else \ 146 ln -s ${.ALLSRC:M*${_firmw:C/\:.*$//}} ${_firmw:C/\:.*$//}; \ 147 ${LD} -b binary ${LDFLAGS} -r -d -o ${.TARGET} \ 148 ${_firmw:C/\:.*$//}; \ 149 rm ${_firmw:C/\:.*$//}; \ 150 fi 151 152OBJS+= ${_firmw:C/\:.*$/.fwo/} 153.endfor 154.endif 155 156OBJS+= ${SRCS:N*.h:R:S/$/.o/g} 157 158.if !defined(PROG) 159PROG= ${KMOD}.ko 160.endif 161 162.if !defined(DEBUG_FLAGS) 163FULLPROG= ${PROG} 164.else 165FULLPROG= ${PROG}.debug 166${PROG}: ${FULLPROG} ${PROG}.symbols 167 ${OBJCOPY} --strip-debug --add-gnu-debuglink=${PROG}.symbols\ 168 ${FULLPROG} ${.TARGET} 169${PROG}.symbols: ${FULLPROG} 170 ${OBJCOPY} --only-keep-debug ${FULLPROG} ${.TARGET} 171.endif 172 173.if ${MACHINE_ARCH} != amd64 174${FULLPROG}: ${KMOD}.kld 175 ${LD} -Bshareable ${LDFLAGS} -o ${.TARGET} ${KMOD}.kld 176.if !defined(DEBUG_FLAGS) 177 ${OBJCOPY} --strip-debug ${.TARGET} 178.endif 179.endif 180 181EXPORT_SYMS?= NO 182.if ${EXPORT_SYMS} != YES 183CLEANFILES+= export_syms 184.endif 185 186.if ${MACHINE_ARCH} != amd64 187${KMOD}.kld: ${OBJS} 188.else 189${FULLPROG}: ${OBJS} 190.endif 191 ${LD} ${LDFLAGS} -r -d -o ${.TARGET} ${OBJS} 192.if defined(EXPORT_SYMS) 193.if ${EXPORT_SYMS} != YES 194.if ${EXPORT_SYMS} == NO 195 touch export_syms 196.elif !exists(${.CURDIR}/${EXPORT_SYMS}) 197 echo ${EXPORT_SYMS} > export_syms 198.else 199 grep -v '^#' < ${EXPORT_SYMS} > export_syms 200.endif 201 awk -f ${SYSDIR}/conf/kmod_syms.awk ${.TARGET} \ 202 export_syms | xargs -J% ${OBJCOPY} % ${.TARGET} 203.endif 204.endif 205.if !defined(DEBUG_FLAGS) && ${MACHINE_ARCH} == amd64 206 ${OBJCOPY} --strip-debug ${.TARGET} 207.endif 208 209_ILINKS=@ machine 210.if ${MACHINE} != ${MACHINE_ARCH} 211_ILINKS+=${MACHINE_ARCH} 212.endif 213 214all: objwarn ${PROG} 215 216beforedepend: ${_ILINKS} 217 218# Ensure that the links exist without depending on it when it exists which 219# causes all the modules to be rebuilt when the directory pointed to changes. 220.for _link in ${_ILINKS} 221.if !exists(${.OBJDIR}/${_link}) 222${OBJS}: ${_link} 223.endif 224.endfor 225 226# Search for kernel source tree in standard places. 227.for _dir in ${.CURDIR}/../.. ${.CURDIR}/../../.. /sys /usr/src/sys 228.if !defined(SYSDIR) && exists(${_dir}/kern/) 229SYSDIR= ${_dir} 230.endif 231.endfor 232.if !defined(SYSDIR) || !exists(${SYSDIR}/kern/) 233.error "can't find kernel source tree" 234.endif 235 236${_ILINKS}: 237 @case ${.TARGET} in \ 238 ${MACHINE_ARCH}) \ 239 path=${SYSDIR}/${MACHINE_ARCH}/include ;; \ 240 machine) \ 241 path=${SYSDIR}/${MACHINE}/include ;; \ 242 @) \ 243 path=${SYSDIR} ;; \ 244 esac ; \ 245 path=`(cd $$path && /bin/pwd)` ; \ 246 ${ECHO} ${.TARGET} "->" $$path ; \ 247 ln -sf $$path ${.TARGET} 248 249CLEANFILES+= ${PROG} ${KMOD}.kld ${OBJS} ${_ILINKS} 250 251.if defined(DEBUG_FLAGS) 252CLEANFILES+= ${FULLPROG} ${PROG}.symbols 253.endif 254 255.if !target(install) 256 257_INSTALLFLAGS:= ${INSTALLFLAGS} 258.for ie in ${INSTALLFLAGS_EDIT} 259_INSTALLFLAGS:= ${_INSTALLFLAGS${ie}} 260.endfor 261 262.if !target(realinstall) 263realinstall: _kmodinstall 264.ORDER: beforeinstall _kmodinstall 265_kmodinstall: 266 ${INSTALL} -o ${KMODOWN} -g ${KMODGRP} -m ${KMODMODE} \ 267 ${_INSTALLFLAGS} ${PROG} ${DESTDIR}${KMODDIR} 268.if defined(DEBUG_FLAGS) && !defined(INSTALL_NODEBUG) 269 ${INSTALL} -o ${KMODOWN} -g ${KMODGRP} -m ${KMODMODE} \ 270 ${_INSTALLFLAGS} ${PROG}.symbols ${DESTDIR}${KMODDIR} 271.endif 272 273.include <bsd.links.mk> 274 275.if !defined(NO_XREF) 276afterinstall: _kldxref 277.ORDER: realinstall _kldxref 278.ORDER: _installlinks _kldxref 279_kldxref: 280 @if type kldxref >/dev/null 2>&1; then \ 281 ${ECHO} kldxref ${DESTDIR}${KMODDIR}; \ 282 kldxref ${DESTDIR}${KMODDIR}; \ 283 fi 284.endif 285.endif # !target(realinstall) 286 287.endif # !target(install) 288 289.if !target(load) 290load: ${PROG} 291 ${KMODLOAD} -v ${.OBJDIR}/${PROG} 292.endif 293 294.if !target(unload) 295unload: 296 ${KMODUNLOAD} -v ${PROG} 297.endif 298 299.if defined(KERNBUILDDIR) 300.PATH: ${KERNBUILDDIR} 301CFLAGS+= -I${KERNBUILDDIR} 302.for _src in ${SRCS:Mopt_*.h} 303CLEANFILES+= ${_src} 304.if !target(${_src}) 305${_src}: 306 ln -sf ${KERNBUILDDIR}/${_src} ${.TARGET} 307.endif 308.endfor 309.else 310.for _src in ${SRCS:Mopt_*.h} 311CLEANFILES+= ${_src} 312.if !target(${_src}) 313${_src}: 314 touch ${.TARGET} 315.endif 316.endfor 317.endif 318 319MFILES?= dev/acpica/acpi_if.m dev/ata/ata_if.m dev/eisa/eisa_if.m \ 320 dev/iicbus/iicbb_if.m dev/iicbus/iicbus_if.m \ 321 dev/mii/miibus_if.m dev/ofw/ofw_bus_if.m \ 322 dev/pccard/card_if.m dev/pccard/power_if.m dev/pci/pci_if.m \ 323 dev/pci/pcib_if.m dev/ppbus/ppbus_if.m dev/smbus/smbus_if.m \ 324 dev/sound/pcm/ac97_if.m dev/sound/pcm/channel_if.m \ 325 dev/sound/pcm/feeder_if.m dev/sound/pcm/mixer_if.m \ 326 dev/sound/midi/mpu_if.m dev/sound/midi/mpufoi_if.m \ 327 dev/sound/midi/synth_if.m dev/usb/usb_if.m isa/isa_if.m \ 328 kern/bus_if.m kern/cpufreq_if.m kern/device_if.m kern/serdev_if.m \ 329 libkern/iconv_converter_if.m opencrypto/crypto_if.m \ 330 pc98/pc98/canbus_if.m pci/agp_if.m 331 332.for _srcsrc in ${MFILES} 333.for _ext in c h 334.for _src in ${SRCS:M${_srcsrc:T:R}.${_ext}} 335CLEANFILES+= ${_src} 336.if !target(${_src}) 337.if !exists(@) 338${_src}: @ 339.else 340${_src}: @/tools/makeobjops.awk @/${_srcsrc} 341.endif 342 ${AWK} -f @/tools/makeobjops.awk @/${_srcsrc} -${_ext} 343.endif 344.endfor # _src 345.endfor # _ext 346.endfor # _srcsrc 347 348.if !empty(SRCS:Mvnode_if.c) 349CLEANFILES+= vnode_if.c 350.if !exists(@) 351vnode_if.c: @ 352.else 353vnode_if.c: @/tools/vnode_if.awk @/kern/vnode_if.src 354.endif 355 ${AWK} -f @/tools/vnode_if.awk @/kern/vnode_if.src -c 356.endif 357 358.if !empty(SRCS:Mvnode_if.h) 359CLEANFILES+= vnode_if.h vnode_if_newproto.h vnode_if_typedef.h 360.if !exists(@) 361vnode_if.h vnode_if_newproto.h vnode_if_typedef.h: @ 362.else 363vnode_if.h vnode_if_newproto.h vnode_if_typedef.h: @/tools/vnode_if.awk \ 364 @/kern/vnode_if.src 365.endif 366vnode_if.h: vnode_if_newproto.h vnode_if_typedef.h 367 ${AWK} -f @/tools/vnode_if.awk @/kern/vnode_if.src -h 368vnode_if_newproto.h: 369 ${AWK} -f @/tools/vnode_if.awk @/kern/vnode_if.src -p 370vnode_if_typedef.h: 371 ${AWK} -f @/tools/vnode_if.awk @/kern/vnode_if.src -q 372.endif 373 374.for _i in mii pccard 375.if !empty(SRCS:M${_i}devs.h) 376CLEANFILES+= ${_i}devs.h 377.if !exists(@) 378${_i}devs.h: @ 379.else 380${_i}devs.h: @/tools/${_i}devs2h.awk @/dev/${_i}/${_i}devs 381.endif 382 ${AWK} -f @/tools/${_i}devs2h.awk @/dev/${_i}/${_i}devs 383.endif 384.endfor # _i 385 386.if !empty(SRCS:Musbdevs.h) 387CLEANFILES+= usbdevs.h 388.if !exists(@) 389usbdevs.h: @ 390.else 391usbdevs.h: @/tools/usbdevs2h.awk @/dev/usb/usbdevs 392.endif 393 ${AWK} -f @/tools/usbdevs2h.awk @/dev/usb/usbdevs -h 394.endif 395 396.if !empty(SRCS:Musbdevs_data.h) 397CLEANFILES+= usbdevs_data.h 398.if !exists(@) 399usbdevs_data.h: @ 400.else 401usbdevs_data.h: @/tools/usbdevs2h.awk @/dev/usb/usbdevs 402.endif 403 ${AWK} -f @/tools/usbdevs2h.awk @/dev/usb/usbdevs -d 404.endif 405 406.if !empty(SRCS:Macpi_quirks.h) 407CLEANFILES+= acpi_quirks.h 408.if !exists(@) 409acpi_quirks.h: @ 410.else 411acpi_quirks.h: @/tools/acpi_quirks2h.awk @/dev/acpica/acpi_quirks 412.endif 413 ${AWK} -f @/tools/acpi_quirks2h.awk @/dev/acpica/acpi_quirks 414.endif 415 416.if !empty(SRCS:Massym.s) 417CLEANFILES+= assym.s genassym.o 418assym.s: genassym.o 419.if !exists(@) 420assym.s: @ 421.else 422assym.s: @/kern/genassym.sh 423.endif 424 sh @/kern/genassym.sh genassym.o > ${.TARGET} 425.if exists(@) 426genassym.o: @/${MACHINE_ARCH}/${MACHINE_ARCH}/genassym.c 427.endif 428genassym.o: @ machine ${SRCS:Mopt_*.h} 429 ${CC} -c ${CFLAGS:N-fno-common} \ 430 @/${MACHINE_ARCH}/${MACHINE_ARCH}/genassym.c 431.endif 432 433lint: ${SRCS} 434 ${LINT} ${LINTKERNFLAGS} ${CFLAGS:M-[DILU]*} ${.ALLSRC:M*.c} 435 436.include <bsd.dep.mk> 437 438.if !exists(${.OBJDIR}/${DEPENDFILE}) 439${OBJS}: ${SRCS:M*.h} 440.endif 441 442.include <bsd.obj.mk> 443.include "kern.mk" 444