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