xref: /freebsd/sys/conf/kmod.mk (revision 4875819c6e13176308eb5906bf83d00502cc0d51)
1639a2e50SGarrett Wollman#	From: @(#)bsd.prog.mk	5.26 (Berkeley) 6/25/91
27f3dea24SPeter Wemm# $FreeBSD$
353996fadSWolfram Schneider#
4fe3cac87SNick Hibma# The include file <bsd.kmod.mk> handles installing Kernel Loadable Device
5fe3cac87SNick Hibma# drivers (KLD's).
653996fadSWolfram Schneider#
753996fadSWolfram Schneider#
853996fadSWolfram Schneider# +++ variables +++
953996fadSWolfram Schneider#
1053996fadSWolfram Schneider# CLEANFILES	Additional files to remove for the clean and cleandir targets.
1153996fadSWolfram Schneider#
12fe3cac87SNick Hibma# KMOD          The name of the kernel module to build.
1353996fadSWolfram Schneider#
143bdfa9e5SDavid E. O'Brien# KMODDIR	Base path for kernel modules (see kld(4)). [/boot/kernel]
1553996fadSWolfram Schneider#
16fe3cac87SNick Hibma# KMODOWN	KLD owner. [${BINOWN}]
1753996fadSWolfram Schneider#
18fe3cac87SNick Hibma# KMODGRP	KLD group. [${BINGRP}]
1953996fadSWolfram Schneider#
20fe3cac87SNick Hibma# KMODMODE	KLD mode. [${BINMODE}]
2153996fadSWolfram Schneider#
22fe3cac87SNick Hibma# KMODLOAD	Command to load a kernel module [/sbin/kldload]
237bc830bbSWolfram Schneider#
24fe3cac87SNick Hibma# KMODUNLOAD	Command to unload a kernel module [/sbin/kldunload]
257bc830bbSWolfram Schneider#
26fe3cac87SNick Hibma# PROG          The name of the kernel module to build.
277bc830bbSWolfram Schneider#		If not supplied, ${KMOD}.o is used.
2853996fadSWolfram Schneider#
2953996fadSWolfram Schneider# SRCS          List of source files
3053996fadSWolfram Schneider#
312b06260dSRuslan Ermilov# DESTDIR	Change the tree where the module gets installed. [not set]
3253996fadSWolfram Schneider#
337fecffecSDoug Rabson# MFILES	Optionally a list of interfaces used by the module.
347fecffecSDoug Rabson#		This file contains a default list of interfaces.
3553996fadSWolfram Schneider#
360b3178a4SMike Smith# EXPORT_SYMS	A list of symbols that should be exported from the module,
3793f03548SMike Smith#		or the name of a file containing a list of symbols, or YES
3893f03548SMike Smith#		to export all symbols.  If not defined, no symbols are
390b3178a4SMike Smith#		exported.
400b3178a4SMike Smith#
4153996fadSWolfram Schneider# +++ targets +++
4253996fadSWolfram Schneider#
4353996fadSWolfram Schneider# 	install:
4417d6c636SRuslan Ermilov#               install the kernel module; if the Makefile
4553996fadSWolfram Schneider#               does not itself define the target install, the targets
4653996fadSWolfram Schneider#               beforeinstall and afterinstall may also be used to cause
4753996fadSWolfram Schneider#               actions immediately before and after the install target
4853996fadSWolfram Schneider#		is executed.
4953996fadSWolfram Schneider#
5053996fadSWolfram Schneider# 	load:
51fe3cac87SNick Hibma#		Load KLD.
5253996fadSWolfram Schneider#
5353996fadSWolfram Schneider# 	unload:
54fe3cac87SNick Hibma#		Unload KLD.
5553996fadSWolfram Schneider#
5653996fadSWolfram Schneider# bsd.obj.mk: clean, cleandir and obj
57161e8999SBruce Evans# bsd.dep.mk: cleandepend, depend and tags
5853996fadSWolfram Schneider#
59639a2e50SGarrett Wollman
60b7bbb709SDavid E. O'BrienAWK?=		awk
61fe3cac87SNick HibmaKMODLOAD?=	/sbin/kldload
62fe3cac87SNick HibmaKMODUNLOAD?=	/sbin/kldunload
63f6611608SSheldon HearnOBJCOPY?=	objcopy
647bc830bbSWolfram Schneider
6506639be7SPeter Wemm.if defined(KMODDEPS)
6606639be7SPeter Wemm.error "Do not use KMODDEPS on 5.0+, use MODULE_VERSION/MODULE_DEPEND"
6706639be7SPeter Wemm.endif
6806639be7SPeter Wemm
690ac2fc87SRuslan Ermilov.include <bsd.init.mk>
70639a2e50SGarrett Wollman
71639a2e50SGarrett Wollman.SUFFIXES: .out .o .c .cc .cxx .C .y .l .s .S
72639a2e50SGarrett Wollman
73c4473420SPeter WemmCFLAGS+=	${COPTS} -D_KERNEL ${CWARNFLAGS}
748fe644b2SMike SmithCFLAGS+=	-DKLD_MODULE
754a2d2630SBruce Evans
76021886ffSBruce Evans# Don't use any standard or source-relative include directories.
77021886ffSBruce Evans# Since -nostdinc will annull any previous -I paths, we repeat all
78021886ffSBruce Evans# such paths after -nostdinc.  It doesn't seem to be possible to
79021886ffSBruce Evans# add to the front of `make' variable.
80021886ffSBruce Evans_ICFLAGS:=	${CFLAGS:M-I*}
8126dac111SPoul-Henning KampCFLAGS+=	-nostdinc -I- ${INCLMAGIC} ${_ICFLAGS}
82021886ffSBruce Evans
83fe3cac87SNick Hibma# Add -I paths for system headers.  Individual KLD makefiles don't
84021886ffSBruce Evans# need any -I paths for this.  Similar defaults for .PATH can't be
85021886ffSBruce Evans# set because there are no standard paths for non-headers.
867e820aaaSMike SmithCFLAGS+=	-I. -I@ -I@/dev
874a2d2630SBruce Evans
88a0b845feSBruce Evans# Add a -I path to standard headers like <stddef.h>.  Use a relative
89a0b845feSBruce Evans# path to src/include if possible.  If the @ symlink hasn't been built
90a0b845feSBruce Evans# yet, then we can't tell if the relative path exists.  Add both the
91a0b845feSBruce Evans# potential relative path and an absolute path in that case.
92a0b845feSBruce Evans.if exists(@)
93a0b845feSBruce Evans.if exists(@/../include)
94a0b845feSBruce EvansCFLAGS+=	-I@/../include
95a0b845feSBruce Evans.else
96b3e17ba2SSatoshi AsamiCFLAGS+=	-I${DESTDIR}/usr/include
97b3e17ba2SSatoshi Asami.endif
98a0b845feSBruce Evans.else # !@
99a0b845feSBruce EvansCFLAGS+=	-I@/../include -I${DESTDIR}/usr/include
100a0b845feSBruce Evans.endif # @
10156bac51bSPoul-Henning Kamp
1020b3178a4SMike Smith# Disallow common variables, and if we end up with commons from
1030b3178a4SMike Smith# somewhere unexpected, allocate storage for them in the module itself.
1040b3178a4SMike SmithCFLAGS+=	-fno-common
1050b3178a4SMike SmithLDFLAGS+=	-d -warn-common
1060b3178a4SMike Smith
1079e4be63dSBruce EvansCFLAGS+=	${DEBUG_FLAGS}
1089e4be63dSBruce Evans
109639a2e50SGarrett WollmanOBJS+=  ${SRCS:N*.h:R:S/$/.o/g}
110639a2e50SGarrett Wollman
111639a2e50SGarrett Wollman.if !defined(PROG)
11290f60fc9SMike SmithPROG=	${KMOD}.ko
11390f60fc9SMike Smith.endif
114639a2e50SGarrett Wollman
115f6611608SSheldon Hearn.if !defined(DEBUG)
116f6611608SSheldon HearnFULLPROG=	${PROG}
117f6611608SSheldon Hearn.else
118f6611608SSheldon HearnFULLPROG=	${PROG}.debug
119f6611608SSheldon Hearn${PROG}: ${FULLPROG}
120f6611608SSheldon Hearn	${OBJCOPY} --strip-debug ${FULLPROG} ${PROG}
121f6611608SSheldon Hearn.endif
122f6611608SSheldon Hearn
123f6611608SSheldon Hearn${FULLPROG}: ${KMOD}.kld
12409be11baSPeter Wemm	${LD} -Bshareable ${LDFLAGS} -o ${.TARGET} ${KMOD}.kld
12509be11baSPeter Wemm
12693f03548SMike SmithEXPORT_SYMS?=	NO
12793f03548SMike Smith.if ${EXPORT_SYMS} != YES
1280b3178a4SMike SmithCLEANFILES+=	${.OBJDIR}/export_syms
1290b3178a4SMike Smith.endif
1300b3178a4SMike Smith
13109be11baSPeter Wemm${KMOD}.kld: ${OBJS}
1320b3178a4SMike Smith	${LD} ${LDFLAGS} -r -d -o ${.TARGET} ${OBJS}
1330b3178a4SMike Smith.if defined(EXPORT_SYMS)
13493f03548SMike Smith.if ${EXPORT_SYMS} != YES
1350b3178a4SMike Smith.if ${EXPORT_SYMS} == NO
1360b3178a4SMike Smith	touch ${.OBJDIR}/export_syms
1370b3178a4SMike Smith.elif !exists(${.CURDIR}/${EXPORT_SYMS})
1380b3178a4SMike Smith	echo ${EXPORT_SYMS} > ${.OBJDIR}/export_syms
1390b3178a4SMike Smith.else
1400b3178a4SMike Smith	grep -v '^#' < ${EXPORT_SYMS} >  ${.OBJDIR}/export_syms
1410b3178a4SMike Smith.endif
1420b3178a4SMike Smith	awk -f ${SYSDIR}/conf/kmod_syms.awk ${.TARGET} \
1430b3178a4SMike Smith		${.OBJDIR}/export_syms | \
14493f03548SMike Smith	xargs -J% ${OBJCOPY} % ${.TARGET}
14593f03548SMike Smith.endif
1460b3178a4SMike Smith.endif
1470b3178a4SMike Smith
1484a2d2630SBruce Evans_ILINKS=@ machine
149639a2e50SGarrett Wollman
150b25a566dSRuslan Ermilovall: objwarn ${PROG}
1514a2d2630SBruce Evans
152985f41afSWarner Loshbeforedepend: ${_ILINKS}
153985f41afSWarner Losh	@rm -f .depend
154985f41afSWarner Losh
155f3cc9575SWarner Losh# Ensure that the links exist without depending on it when it exists which
156f3cc9575SWarner Losh# causes all the modules to be rebuilt when the directory pointed to changes.
157f3cc9575SWarner Losh.for _link in ${_ILINKS}
158f3cc9575SWarner Losh.if !exists(${.OBJDIR}/${_link})
159f3cc9575SWarner Losh${OBJS}: ${_link}
160f3cc9575SWarner Losh.endif
161f3cc9575SWarner Losh.endfor
1624a2d2630SBruce Evans
1639a8631b3SWarner Losh# Search for kernel source tree in standard places.
1649a8631b3SWarner Losh.for _dir in ${.CURDIR}/../.. ${.CURDIR}/../../.. /sys /usr/src/sys
1659a8631b3SWarner Losh.if !defined(SYSDIR) && exists(${_dir}/kern/)
1669a8631b3SWarner LoshSYSDIR=	${_dir}
1679a8631b3SWarner Losh.endif
1689a8631b3SWarner Losh.endfor
169aabafca0SJason Evans.if !defined(SYSDIR) || !exists(${SYSDIR}/kern)
1709a8631b3SWarner Losh.error "can't find kernel source tree"
1719a8631b3SWarner Losh.endif
1729a8631b3SWarner Losh
1734a2d2630SBruce Evans${_ILINKS}:
1749a8631b3SWarner Losh	@case ${.TARGET} in \
17508a04a89SPeter Wemm	machine) \
1769a8631b3SWarner Losh		path=${SYSDIR}/${MACHINE_ARCH}/include ;; \
17708a04a89SPeter Wemm	@) \
1789a8631b3SWarner Losh		path=${SYSDIR} ;; \
17908a04a89SPeter Wemm	esac ; \
18008a04a89SPeter Wemm	path=`(cd $$path && /bin/pwd)` ; \
18108a04a89SPeter Wemm	${ECHO} ${.TARGET} "->" $$path ; \
18208a04a89SPeter Wemm	ln -s $$path ${.TARGET}
1834a2d2630SBruce Evans
184361186e5SMaxime HenrionCLEANFILES+= ${PROG} ${KMOD}.kld ${OBJS} ${_ILINKS} symb.tmp tmp.o
185361186e5SMaxime Henrion
186361186e5SMaxime Henrion.if defined(DEBUG)
187361186e5SMaxime HenrionCLEANFILES+= ${FULLPROG}
188361186e5SMaxime Henrion.endif
189639a2e50SGarrett Wollman
190639a2e50SGarrett Wollman.if !target(install)
191639a2e50SGarrett Wollman
192ea87b3cbSDavid E. O'Brien_INSTALLFLAGS:=	${INSTALLFLAGS}
193e902c1bbSTim Vanderhoek.for ie in ${INSTALLFLAGS_EDIT}
194e902c1bbSTim Vanderhoek_INSTALLFLAGS:=	${_INSTALLFLAGS${ie}}
195e902c1bbSTim Vanderhoek.endfor
196e902c1bbSTim Vanderhoek
1972cbc81ffSRuslan Ermilov.if defined(DEBUG)
198b25a566dSRuslan Ermilovinstall.debug:
1992cbc81ffSRuslan Ermilov	cd ${.CURDIR}; ${MAKE} -DINSTALL_DEBUG install
2002cbc81ffSRuslan Ermilov.endif
201a4c00586SDag-Erling Smørgrav
2022b06260dSRuslan Ermilov.if !target(realinstall)
2032b06260dSRuslan Ermilovrealinstall: _kmodinstall
2042b06260dSRuslan Ermilov.ORDER: beforeinstall _kmodinstall
2052cbc81ffSRuslan Ermilov.if defined(DEBUG) && defined(INSTALL_DEBUG)
2062b06260dSRuslan Ermilov_kmodinstall:
207d2893b16SRuslan Ermilov	${INSTALL} -o ${KMODOWN} -g ${KMODGRP} -m ${KMODMODE} \
2082b06260dSRuslan Ermilov	    ${_INSTALLFLAGS} ${FULLPROG} ${DESTDIR}${KMODDIR}
2092cbc81ffSRuslan Ermilov.else
2102b06260dSRuslan Ermilov_kmodinstall:
211d2893b16SRuslan Ermilov	${INSTALL} -o ${KMODOWN} -g ${KMODGRP} -m ${KMODMODE} \
2122b06260dSRuslan Ermilov	    ${_INSTALLFLAGS} ${PROG} ${DESTDIR}${KMODDIR}
2132b06260dSRuslan Ermilov
2142b06260dSRuslan Ermilov.include <bsd.links.mk>
2152b06260dSRuslan Ermilov
2164875819cSJake Burkholder.if !defined(NO_XREF)
2172b06260dSRuslan Ermilovafterinstall: _kldxref
2182b06260dSRuslan Ermilov.ORDER: realinstall _kldxref
2192b06260dSRuslan Ermilov.ORDER: _installlinks _kldxref
2202b06260dSRuslan Ermilov_kldxref:
221be1d673dSRuslan Ermilov	@if type kldxref >/dev/null 2>&1; then \
222be1d673dSRuslan Ermilov		${ECHO} kldxref ${DESTDIR}${KMODDIR}; \
223be1d673dSRuslan Ermilov		kldxref ${DESTDIR}${KMODDIR}; \
224be1d673dSRuslan Ermilov	fi
225505222d3SPeter Wemm.endif
2262cbc81ffSRuslan Ermilov.endif
2272b06260dSRuslan Ermilov.endif !target(realinstall)
228639a2e50SGarrett Wollman
2292b06260dSRuslan Ermilov.endif !target(install)
230639a2e50SGarrett Wollman
231639a2e50SGarrett Wollman.if !target(load)
2329a8631b3SWarner Loshload:	${PROG}
233dba3dc7bSNick Hibma	${KMODLOAD} -v ${.OBJDIR}/${KMOD}.ko
234639a2e50SGarrett Wollman.endif
235639a2e50SGarrett Wollman
236639a2e50SGarrett Wollman.if !target(unload)
237fe3cac87SNick Hibmaunload:
2389a8631b3SWarner Losh	${KMODUNLOAD} -v ${KMOD}
23908a04a89SPeter Wemm.endif
2401aa57be3SGarrett Wollman
2414fea6704SBruce Evans.for _src in ${SRCS:Mopt_*.h}
2424fea6704SBruce EvansCLEANFILES+=	${_src}
2434fea6704SBruce Evans.if !target(${_src})
2444fea6704SBruce Evans${_src}:
2454fea6704SBruce Evans	touch ${.TARGET}
2464fea6704SBruce Evans.endif
2474fea6704SBruce Evans.endfor
2484fea6704SBruce Evans
2497fecffecSDoug RabsonMFILES?= kern/bus_if.m kern/device_if.m dev/iicbus/iicbb_if.m \
2506f2d8adbSBoris Popov    dev/iicbus/iicbus_if.m isa/isa_if.m \
2516f2d8adbSBoris Popov    libkern/iconv_converter_if.m \
2526f2d8adbSBoris Popov    dev/mii/miibus_if.m \
2537e820aaaSMike Smith    dev/pccard/card_if.m dev/pccard/power_if.m dev/pci/pci_if.m \
2547e820aaaSMike Smith    dev/pci/pcib_if.m dev/ppbus/ppbus_if.m dev/smbus/smbus_if.m \
2550f55ac6cSCameron Grant    dev/usb/usb_if.m dev/sound/pcm/ac97_if.m dev/sound/pcm/channel_if.m \
256c4f9e3aeSSam Leffler    dev/sound/pcm/feeder_if.m dev/sound/pcm/mixer_if.m pci/agp_if.m \
257c4f9e3aeSSam Leffler    opencrypto/crypto_if.m
2587fecffecSDoug Rabson
2597fecffecSDoug Rabson.for _srcsrc in ${MFILES}
2604fea6704SBruce Evans.for _ext in c h
2614fea6704SBruce Evans.for _src in ${SRCS:M${_srcsrc:T:R}.${_ext}}
2624fea6704SBruce EvansCLEANFILES+=	${_src}
2634fea6704SBruce Evans.if !target(${_src})
264f3cc9575SWarner Losh.if !exists(@)
2654fea6704SBruce Evans${_src}: @
266f3cc9575SWarner Losh.endif
2674fea6704SBruce Evans.if exists(@)
2689fa411aeSDavid E. O'Brien${_src}: @/tools/makeobjops.awk @/${_srcsrc}
2694fea6704SBruce Evans.endif
2709fa411aeSDavid E. O'Brien	${AWK} -f @/tools/makeobjops.awk @/${_srcsrc} -${_ext}
2714fea6704SBruce Evans.endif
2724fea6704SBruce Evans.endfor # _src
2734fea6704SBruce Evans.endfor # _ext
2744fea6704SBruce Evans.endfor # _srcsrc
2754fea6704SBruce Evans
2769029b644SPeter Wemm.for _ext in c h
2779029b644SPeter Wemm.if ${SRCS:Mvnode_if.${_ext}} != ""
2789029b644SPeter WemmCLEANFILES+=	vnode_if.${_ext}
279f3cc9575SWarner Losh.if !exists(@)
2809029b644SPeter Wemmvnode_if.${_ext}: @
281f3cc9575SWarner Losh.endif
2824fea6704SBruce Evans.if exists(@)
283b7bbb709SDavid E. O'Brienvnode_if.${_ext}: @/tools/vnode_if.awk @/kern/vnode_if.src
2844fea6704SBruce Evans.endif
285b7bbb709SDavid E. O'Brien	${AWK} -f @/tools/vnode_if.awk @/kern/vnode_if.src -${_ext}
2864fea6704SBruce Evans.endif
2879029b644SPeter Wemm.endfor
2881aa57be3SGarrett Wollman
2892d3ce713SDavid E. O'Brien.if ${SRCS:Mmiidevs.h} != ""
2902d3ce713SDavid E. O'BrienCLEANFILES+=	miidevs.h
2912d3ce713SDavid E. O'Brien.if !exists(@)
2922d3ce713SDavid E. O'Brienmiidevs.h: @
2932d3ce713SDavid E. O'Brien.endif
2942d3ce713SDavid E. O'Brien.if exists(@)
2952d3ce713SDavid E. O'Brienmiidevs.h: @/tools/devlist2h.awk @/dev/mii/miidevs
2962d3ce713SDavid E. O'Brien.endif
2972d3ce713SDavid E. O'Brien	${AWK} -f @/tools/devlist2h.awk @/dev/mii/miidevs
2982d3ce713SDavid E. O'Brien.endif
2992d3ce713SDavid E. O'Brien
30071395925SPeter Wemmregress:
30169b87d3eSEivind Eklund
302766f7d6eSMark Murraylint: ${SRCS}
303bbb467cdSRuslan Ermilov	${LINT} ${LINTKERNFLAGS} ${CFLAGS:M-[DILU]*} ${.ALLSRC:M*.c}
304766f7d6eSMark Murray
30568e00b92SJordan K. Hubbard.include <bsd.dep.mk>
30647da63c3SBruce Evans
30747da63c3SBruce Evans.if !exists(${DEPENDFILE})
308589d9341SBruce Evans${OBJS}: ${SRCS:M*.h}
30947da63c3SBruce Evans.endif
31047da63c3SBruce Evans
311afba897fSBruce Evans.include <bsd.obj.mk>
3129e72552fSBruce Evans.include <bsd.kern.mk>
313