1# From: @(#)bsd.prog.mk 5.26 (Berkeley) 6/25/91 2# $FreeBSD: src/sys/conf/kmod.mk,v 1.82.2.15 2003/02/10 13:11:50 nyan Exp $ 3# 4# The include file <bsd.kmod.mk> handles installing Kernel Loadable Device 5# drivers (KLD's). 6# 7# 8# +++ variables +++ 9# CLEANFILES Additional files to remove for the clean and cleandir targets. 10# 11# KMOD The name of the kernel module to build. 12# 13# KMODDIR Base path for kernel modules (see kld(4)). 14# [${DESTKERNDIR}] 15# 16# KMODOWN KLD owner. [${BINOWN}] 17# 18# KMODGRP KLD group. [${BINGRP}] 19# 20# KMODMODE KLD mode. [${BINMODE}] 21# 22# KMODLOAD Command to load a kernel module [/sbin/kldload] 23# 24# KMODUNLOAD Command to unload a kernel module [/sbin/kldunload] 25# 26# PROG The name of the kernel module to build. 27# If not supplied, ${KMOD}.o is used. 28# 29# SRCS List of source files 30# 31# DESTKERNDIR Change the tree where the kernel and the modules get 32# installed. [/boot] ${DESTDIR} changes the root of the tree 33# pointed to by ${DESTKERNDIR}. 34# 35# MFILES Optionally a list of interfaces used by the module. 36# This file contains a default list of interfaces. 37# 38# FIRMWS List of firmware images in format filename:shortname:version 39# 40# FIRMWARE_LICENSE 41# Set to the name of the license the user has to agree on in 42# order to use this firmware. See /usr/share/doc/legal 43# 44# KERNBUILDDIR Set to the location of the kernel build directory where 45# the opt_*.h files, .o's and kernel wind up. 46# 47# +++ targets +++ 48# 49# install: 50# install the kernel module and its manual pages; 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 KLD. 58# 59# unload: 60# Unload KLD. 61# 62# bsd.obj.mk: clean, cleandir and obj 63# bsd.dep.mk: cleandepend, depend and tags 64# 65 66OBJCOPY?= objcopy 67KMODLOAD?= /sbin/kldload 68KMODUNLOAD?= /sbin/kldunload 69 70# KERNEL is needed when running make install directly from 71# the obj directory. 72KERNEL?= kernel 73 74KMODDIR?= ${DESTKERNDIR} 75KMODOWN?= ${BINOWN} 76KMODGRP?= ${BINGRP} 77KMODMODE?= ${BINMODE} 78 79.include <bsd.init.mk> 80 81.SUFFIXES: .out .o .c .cc .cxx .C .y .l .s .S 82 83.if !defined(NO_WERROR) && (${CCVER} == "gcc47" || ${CCVER} == "gcc80") 84WERROR=-Werror 85.endif 86 87COPTFLAGS?=-O2 -pipe 88 89# always use external as(1) 90.if ${CCVER:Mclang*} 91COPTFLAGS+= -no-integrated-as 92.endif 93 94# useful for debugging 95#.warning "KMOD-PREFILTER ${CFLAGS}" 96 97WORLD_CCOPTLEVEL= # XXX prevent world opt level affecting kernel modules 98CFLAGS= ${COPTFLAGS} ${KCFLAGS} ${COPTS} -D_KERNEL 99CFLAGS+= ${CWARNFLAGS} -std=${CSTD} ${WERROR} 100CFLAGS+= -DKLD_MODULE 101 102# Don't use any standard include directories. 103# Since -nostdinc will annull any previous -I paths, we repeat all 104# such paths after -nostdinc. It doesn't seem to be possible to 105# add to the front of `make' variable. 106# 107# Don't use -I- anymore, source-relative includes are desireable. 108_ICFLAGS:= ${CFLAGS:M-I*} 109CFLAGS+= -nostdinc ${_ICFLAGS} 110 111# Add -I paths for system headers. Individual KLD makefiles don't 112# need any -I paths for this. Similar defaults for .PATH can't be 113# set because there are no standard paths for non-headers. 114# 115# NOTE! Traditional platform paths such as <platform/pc64/blah.h> 116# must run through the "machine_base" softlink using 117# <machine_base/blah.h>. An explicit cross-platform path must 118# operate relative to /usr/src/sys using e.g. <platform/pc64/isa/blah.h> 119# 120CFLAGS+= -I. 121.if defined(FREEBSD_COMPAT) 122CFLAGS+= -Idragonfly/freebsd_compat 123CFLAGS+= -DFREEBSD_COMPAT=1 124.endif 125CFLAGS+= -Idragonfly 126 127# Add -I paths for headers in the kernel build directory 128# 129.if defined(KERNBUILDDIR) 130CFLAGS+= -I${KERNBUILDDIR} 131_MACHINE_FWD= ${KERNBUILDDIR} 132.else 133.if defined(MAKEOBJDIRPREFIX) 134_MACHINE_FWD= ${MAKEOBJDIRPREFIX}/${SYSDIR}/forwarder_${MACHINE_ARCH} 135.else 136_MACHINE_FWD= ${.OBJDIR}/forwarder_${MACHINE_ARCH} 137CLEANDIRS+= ${_MACHINE_FWD} 138.endif 139.endif 140CFLAGS+= -I${_MACHINE_FWD}/include 141 142.include "kern.fwd.mk" 143 144# Add a -I path to standard headers like <stddef.h>. Use a relative 145# path to src/include if possible. If the dragonfly symlink hasn't been 146# built yet, then we can't tell if the relative path exists. Add both the 147# potential relative path and an absolute path in that case. 148.if exists(dragonfly) 149.if exists(dragonfly/../include) 150CFLAGS+= -Idragonfly/../include 151.else 152CFLAGS+= -I${DESTDIR}/usr/include 153.endif 154.else 155CFLAGS+= -Idragonfly/../include -I${DESTDIR}/usr/include 156.endif 157 158.if defined(KERNBUILDDIR) && \ 159 exists(${KERNBUILDDIR}/opt_global.h) 160CFLAGS+= -DHAVE_KERNEL_OPTION_HEADERS -include ${KERNBUILDDIR}/opt_global.h 161.endif 162 163CFLAGS+= ${DEBUG_FLAGS} 164.if ${MACHINE_ARCH} == "x86_64" 165CFLAGS+= -fno-omit-frame-pointer 166.endif 167 168.if defined(FIRMWS) 169#AWK=/usr/bin/awk 170.if !exists(dragonfly) 171${KMOD:S/$/.c/}: dragonfly 172.else 173${KMOD:S/$/.c/}: dragonfly/tools/fw_stub.awk 174.endif 175 ${AWK} -f dragonfly/tools/fw_stub.awk ${FIRMWS} -m ${KMOD} -c ${KMOD:S/$/.c/g} \ 176 ${FIRMWARE_LICENSE:C/.+/-l/}${FIRMWARE_LICENSE} 177 178SRCS+= ${KMOD:S/$/.c/} 179CLEANFILES+= ${KMOD:S/$/.c/} 180 181.for _firmw in ${FIRMWS} 182${_firmw:C/\:.*$/.fwo/}: ${_firmw:C/\:.*$//} 183 @${ECHO} ${_firmw:C/\:.*$//} ${.ALLSRC:M*${_firmw:C/\:.*$//}} 184 @if [ -e ${_firmw:C/\:.*$//} ]; then \ 185 ${LD} -b binary --no-warn-mismatch ${LDFLAGS} \ 186 -r -d -o ${.TARGET} ${_firmw:C/\:.*$//}; \ 187 else \ 188 ln -s ${.ALLSRC:M*${_firmw:C/\:.*$//}} ${_firmw:C/\:.*$//}; \ 189 ${LD} -b binary --no-warn-mismatch ${LDFLAGS} \ 190 -r -d -o ${.TARGET} ${_firmw:C/\:.*$//}; \ 191 rm ${_firmw:C/\:.*$//}; \ 192 fi 193 194OBJS+= ${_firmw:C/\:.*$/.fwo/} 195.endfor 196.endif 197 198OBJS+= ${SRCS:N*.h:R:S/$/.o/g} 199 200.if !defined(PROG) 201PROG= ${KMOD}.ko 202.endif 203 204# In case of LTO provide all standard CFLAGS! 205.if ${CFLAGS:M-flto} 206ELDFLAGS+= ${CFLAGS} 207.endif 208 209.if ${MACHINE_ARCH} != x86_64 210${PROG}: ${KMOD}.kld 211 ${CC} ${ELDFLAGS} -nostdlib -Wl,--hash-style=sysv \ 212 -Wl,-Bshareable ${LDFLAGS} -o ${.TARGET} ${KMOD}.kld 213.endif 214 215.if ${MACHINE_ARCH} != x86_64 216${KMOD}.kld: ${OBJS} 217 ${CC} ${ELDFLAGS} -nostdlib -Wl,--hash-style=sysv \ 218 ${LDFLAGS} -r -o ${.TARGET} ${OBJS} 219.else 220${PROG}: ${OBJS} 221 ${CC} ${ELDFLAGS} -nostdlib -Wl,--hash-style=sysv \ 222 ${LDFLAGS} -r -Wl,-d -o ${.TARGET} ${OBJS} 223.endif 224 225# links to platform and cpu architecture include files. If we are 226# building with a kernel most of these already exist in the kernel build 227# dir. 228# 229# 'dragonfly' is a link to system sources. 230# 231# Note that 'dragonfly', 'machine_base', and 'cpu_base' primarily exist 232# when source files need to reference sources in the dragonfly codebase or 233# when header files need to differentiate between compat headers and base 234# system headers (e.g. when forwarding headers) 235# 236.if defined(KERNBUILDDIR) 237_ILINKS=dragonfly 238.else 239_ILINKS=dragonfly machine_base machine cpu_base cpu 240.endif 241 242.if defined(ARCH) 243_ILINKS+=${ARCH} 244.endif 245 246all: objwarn fwheaders ${PROG} 247 248beforedepend: fwheaders 249fwheaders: ${_ILINKS} ${FORWARD_HEADERS_COOKIE} 250# Ensure that the links exist without depending on it when it exists which 251# causes all the modules to be rebuilt when the directory pointed to changes. 252.for _link in ${_ILINKS} 253.if !exists(${.OBJDIR}/${_link}) 254${OBJS}: ${_link} 255.endif 256.endfor 257 258# Search for kernel source tree in standard places. 259.for _dir in ${.CURDIR}/../.. ${.CURDIR}/../../.. ${.CURDIR}/../../../.. /sys /usr/src/sys 260.if !defined(SYSDIR) && exists(${_dir}/kern/) 261SYSDIR= ${_dir} 262.endif 263.endfor 264.if !defined(SYSDIR) || !exists(${SYSDIR}/kern) 265.error "can't find kernel source tree" 266.endif 267S= ${SYSDIR} 268 269# path=`(cd $$path && /bin/pwd)` ; 270 271${_ILINKS}: 272 @case ${.TARGET} in \ 273 machine) \ 274 path=${SYSDIR}/platform/${MACHINE_PLATFORM}/include ;; \ 275 machine_base) \ 276 path=${SYSDIR}/platform/${MACHINE_PLATFORM} ;; \ 277 cpu) \ 278 path=${SYSDIR}/cpu/${MACHINE_ARCH}/include ;; \ 279 cpu_base) \ 280 path=${SYSDIR}/cpu/${MACHINE_ARCH} ;; \ 281 dragonfly) \ 282 path=${SYSDIR} ;; \ 283 arch_*) \ 284 path=${.CURDIR}/${MACHINE_ARCH} ;; \ 285 esac ; \ 286 ${ECHO} ${.TARGET} "->" $$path ; \ 287 ${LN} -s $$path ${.TARGET} 288 289CLEANFILES+= ${PROG} ${KMOD}.kld ${OBJS} ${_ILINKS} symb.tmp tmp.o 290 291.if !target(install) 292 293_INSTALLFLAGS:= ${INSTALLFLAGS} 294.for ie in ${INSTALLFLAGS_EDIT} 295_INSTALLFLAGS:= ${_INSTALLFLAGS${ie}} 296.endfor 297 298.if !target(realinstall) 299realinstall: _kmodinstall 300.ORDER: beforeinstall _kmodinstall 301_kmodinstall: 302.if defined(INSTALLSTRIPPEDMODULES) 303 ${INSTALL} -o ${KMODOWN} -g ${KMODGRP} -m ${KMODMODE} \ 304 ${_INSTALLFLAGS} ${PROG} ${DESTDIR}${KMODDIR} 305 ${OBJCOPY} --strip-debug ${DESTDIR}${KMODDIR}/${PROG} 306.else 307 ${INSTALL} -o ${KMODOWN} -g ${KMODGRP} -m ${KMODMODE} \ 308 ${_INSTALLFLAGS} ${PROG} ${DESTDIR}${KMODDIR} 309.endif 310.endif # !target(realinstall) 311 312.include <bsd.links.mk> 313 314.endif # !target(install) 315 316.if !target(load) 317load: ${PROG} 318 ${KMODLOAD} -v ./${KMOD}.ko 319.endif 320 321.if !target(unload) 322unload: 323 ${KMODUNLOAD} -v ${KMOD} 324.endif 325 326.for _src in ${SRCS:Mopt_*.h} ${SRCS:Muse_*.h} 327CLEANFILES+= ${_src} 328.if !target(${_src}) 329.if defined(KERNBUILDDIR) && exists(${KERNBUILDDIR}/${_src}) 330${_src}: ${KERNBUILDDIR}/${_src} 331# we do not have to copy these files any more, the kernel build 332# directory is included in the path now. 333# cp ${KERNBUILDDIR}/${_src} ${.TARGET} 334.else 335${_src}: 336 touch ${.TARGET} 337.endif # KERNBUILDDIR 338.endif 339.endfor 340 341MFILES?= kern/bus_if.m kern/device_if.m bus/iicbus/iicbb_if.m \ 342 bus/iicbus/iicbus_if.m bus/isa/isa_if.m dev/netif/mii_layer/miibus_if.m \ 343 bus/pccard/card_if.m bus/pccard/power_if.m bus/pci/pci_if.m \ 344 bus/pci/pcib_if.m \ 345 bus/ppbus/ppbus_if.m bus/smbus/smbus_if.m bus/u4b/usb_if.m \ 346 dev/acpica/acpi_if.m dev/acpica/acpi_wmi_if.m dev/disk/nata/ata_if.m \ 347 dev/disk/sdhci/sdhci_if.m \ 348 dev/sound/pci/hda/hdac_if.m \ 349 dev/sound/pcm/ac97_if.m dev/sound/pcm/channel_if.m \ 350 dev/sound/pcm/feeder_if.m dev/sound/pcm/mixer_if.m \ 351 dev/sound/midi/mpu_if.m dev/sound/midi/mpufoi_if.m \ 352 dev/sound/midi/synth_if.m \ 353 libiconv/iconv_converter_if.m dev/agp/agp_if.m opencrypto/cryptodev_if.m \ 354 bus/mmc/mmcbus_if.m bus/mmc/mmcbr_if.m \ 355 dev/virtual/virtio/virtio/virtio_bus_if.m \ 356 dev/misc/backlight/backlight_if.m dev/misc/coremctl/coremctl_if.m kern/cpu_if.m \ 357 bus/gpio/gpio_if.m \ 358 freebsd/net/ifdi_if.m 359 360.for _srcsrc in ${MFILES} 361.for _ext in c h 362.for _src in ${SRCS:M${_srcsrc:T:R}.${_ext}} 363CLEANFILES+= ${_src} 364.if !target(${_src}) 365${_src}: dragonfly 366.if exists(dragonfly) 367${_src}: dragonfly/tools/makeobjops.awk dragonfly/${_srcsrc} 368.endif 369 370.if defined(KERNBUILDDIR) && \ 371 exists(${KERNBUILDDIR}/${_src}) 372.else 373 awk -f dragonfly/tools/makeobjops.awk -- -${_ext} dragonfly/${_srcsrc} 374.endif 375.endif 376.endfor # _src 377.endfor # _ext 378.endfor # _srcsrc 379 380.if !empty(SRCS:Mmiidevs.h) 381CLEANFILES+= miidevs.h 382.if !exists(dragonfly) 383miidevs.h: dragonfly 384.else 385miidevs.h: dragonfly/tools/miidevs2h.awk dragonfly/dev/netif/mii_layer/miidevs 386.endif 387 ${AWK} -f dragonfly/tools/miidevs2h.awk dragonfly/dev/netif/mii_layer/miidevs 388.endif 389 390.if !empty(SRCS:Mpccarddevs.h) 391CLEANFILES+= pccarddevs.h 392.if !exists(dragonfly) 393pccarddevs.h: dragonfly 394.else 395pccarddevs.h: dragonfly/tools/pccarddevs2h.awk dragonfly/bus/pccard/pccarddevs 396.endif 397 ${AWK} -f dragonfly/tools/pccarddevs2h.awk dragonfly/bus/pccard/pccarddevs 398.endif 399 400.if !empty(SRCS:Mpcidevs.h) 401CLEANFILES+= pcidevs.h 402.if !exists(dragonfly) 403pcidevs.h: dragonfly 404.else 405pcidevs.h: dragonfly/tools/pcidevs2h.awk dragonfly/bus/pci/pcidevs 406.endif 407 ${AWK} -f dragonfly/tools/pcidevs2h.awk dragonfly/bus/pci/pcidevs 408.endif 409 410.if !empty(SRCS:Musbdevs.h) 411CLEANFILES+= usbdevs.h 412.if !exists(dragonfly) 413usbdevs.h: dragonfly 414.else 415usbdevs.h: dragonfly/tools/usbdevs2h.awk dragonfly/bus/u4b/usbdevs 416.endif 417 ${AWK} -f dragonfly/tools/usbdevs2h.awk dragonfly/bus/u4b/usbdevs -h 418.endif 419 420.if !empty(SRCS:Musbdevs_data.h) 421CLEANFILES+= usbdevs_data.h 422.if !exists(dragonfly) 423usbdevs_data.h: dragonfly 424.else 425usbdevs_data.h: dragonfly/tools/usbdevs2h.awk dragonfly/bus/u4b/usbdevs 426.endif 427 ${AWK} -f dragonfly/tools/usbdevs2h.awk dragonfly/bus/u4b/usbdevs -d 428.endif 429 430.if !empty(SRCS:Macpi_quirks.h) 431CLEANFILES+= acpi_quirks.h 432.if !exists(dragonfly) 433acpi_quirks.h: dragonfly 434.else 435acpi_quirks.h: dragonfly/tools/acpi_quirks2h.awk dragonfly/dev/acpica/acpi_quirks 436.endif 437 ${AWK} -f dragonfly/tools/acpi_quirks2h.awk dragonfly/dev/acpica/acpi_quirks 438.endif 439 440.if !empty(SRCS:Massym.s) 441CLEANFILES+= assym.s genassym.o 442assym.s: genassym.o 443.if defined(KERNBUILDDIR) 444genassym.o: opt_global.h 445.endif 446.if !exists(dragonfly) 447assym.s: dragonfly 448.else 449assym.s: dragonfly/kern/genassym.sh 450.endif 451 sh dragonfly/kern/genassym.sh genassym.o > ${.TARGET} 452.if exists(dragonfly) 453genassym.o: dragonfly/platform/${MACHINE_PLATFORM}/${MACHINE_ARCH}/genassym.c 454.endif 455genassym.o: dragonfly ${SRCS:Mopt_*.h} 456 ${CC} -c ${CFLAGS:N-fno-common:N-flto:N-mcmodel=small} -fcommon \ 457 ${WERROR} dragonfly/platform/${MACHINE_PLATFORM}/${MACHINE_ARCH}/genassym.c 458.endif 459 460regress: 461 462.include <bsd.dep.mk> 463 464.if !exists(${DEPENDFILE}) 465${OBJS}: ${SRCS:M*.h} 466.endif 467 468.include <bsd.obj.mk> 469.include "bsd.kern.mk" 470 471# Behaves like MODULE_OVERRIDE 472.if defined(KLD_DEPS) 473all: _kdeps_all 474_kdeps_all: dragonfly 475.for _mdep in ${KLD_DEPS} 476 cd ${SYSDIR}/${_mdep} && make all 477.endfor 478depend: _kdeps_depend 479_kdeps_depend: dragonfly 480.for _mdep in ${KLD_DEPS} 481 cd ${SYSDIR}/${_mdep} && make depend 482.endfor 483install: _kdeps_install 484_kdeps_install: dragonfly 485.for _mdep in ${KLD_DEPS} 486 cd ${SYSDIR}/${_mdep} && make install 487.endfor 488clean: _kdeps_clean 489_kdeps_clean: dragonfly 490.for _mdep in ${KLD_DEPS} 491 cd ${SYSDIR}/${_mdep} && make clean 492.endfor 493.endif 494