xref: /freebsd/sys/conf/kmod.mk (revision 75504450a1882e722a045a277ac547f46871a173)
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}
828fa793d0SBrian Feldman.if defined(KERNBUILDDIR)
8375504450SBrian FeldmanCFLAGS+=       -include ${KERNBUILDDIR}/opt_global.h
848fa793d0SBrian Feldman.endif
85021886ffSBruce Evans
86fe3cac87SNick Hibma# Add -I paths for system headers.  Individual KLD makefiles don't
87021886ffSBruce Evans# need any -I paths for this.  Similar defaults for .PATH can't be
88021886ffSBruce Evans# set because there are no standard paths for non-headers.
8974455e55SWarner LoshCFLAGS+=	-I. -I@
904a2d2630SBruce Evans
91a0b845feSBruce Evans# Add a -I path to standard headers like <stddef.h>.  Use a relative
92a0b845feSBruce Evans# path to src/include if possible.  If the @ symlink hasn't been built
93a0b845feSBruce Evans# yet, then we can't tell if the relative path exists.  Add both the
94a0b845feSBruce Evans# potential relative path and an absolute path in that case.
95a0b845feSBruce Evans.if exists(@)
96a0b845feSBruce Evans.if exists(@/../include)
97a0b845feSBruce EvansCFLAGS+=	-I@/../include
98a0b845feSBruce Evans.else
99b3e17ba2SSatoshi AsamiCFLAGS+=	-I${DESTDIR}/usr/include
100b3e17ba2SSatoshi Asami.endif
101a0b845feSBruce Evans.else # !@
102a0b845feSBruce EvansCFLAGS+=	-I@/../include -I${DESTDIR}/usr/include
103a0b845feSBruce Evans.endif # @
10456bac51bSPoul-Henning Kamp
105a92a2144SPeter WemmCFLAGS+=	-finline-limit=${INLINE_LIMIT}
1062a51529fSPeter Wemm
1070b3178a4SMike Smith# Disallow common variables, and if we end up with commons from
1080b3178a4SMike Smith# somewhere unexpected, allocate storage for them in the module itself.
1090b3178a4SMike SmithCFLAGS+=	-fno-common
1100b3178a4SMike SmithLDFLAGS+=	-d -warn-common
1110b3178a4SMike Smith
1129e4be63dSBruce EvansCFLAGS+=	${DEBUG_FLAGS}
1139e4be63dSBruce Evans
114639a2e50SGarrett WollmanOBJS+=  ${SRCS:N*.h:R:S/$/.o/g}
115639a2e50SGarrett Wollman
116639a2e50SGarrett Wollman.if !defined(PROG)
11790f60fc9SMike SmithPROG=	${KMOD}.ko
11890f60fc9SMike Smith.endif
119639a2e50SGarrett Wollman
120f6611608SSheldon Hearn.if !defined(DEBUG)
121f6611608SSheldon HearnFULLPROG=	${PROG}
122f6611608SSheldon Hearn.else
123f6611608SSheldon HearnFULLPROG=	${PROG}.debug
124f6611608SSheldon Hearn${PROG}: ${FULLPROG}
125f6611608SSheldon Hearn	${OBJCOPY} --strip-debug ${FULLPROG} ${PROG}
126f6611608SSheldon Hearn.endif
127f6611608SSheldon Hearn
128f6611608SSheldon Hearn${FULLPROG}: ${KMOD}.kld
12909be11baSPeter Wemm	${LD} -Bshareable ${LDFLAGS} -o ${.TARGET} ${KMOD}.kld
13009be11baSPeter Wemm
13193f03548SMike SmithEXPORT_SYMS?=	NO
13293f03548SMike Smith.if ${EXPORT_SYMS} != YES
1330b3178a4SMike SmithCLEANFILES+=	${.OBJDIR}/export_syms
1340b3178a4SMike Smith.endif
1350b3178a4SMike Smith
13609be11baSPeter Wemm${KMOD}.kld: ${OBJS}
1370b3178a4SMike Smith	${LD} ${LDFLAGS} -r -d -o ${.TARGET} ${OBJS}
1380b3178a4SMike Smith.if defined(EXPORT_SYMS)
13993f03548SMike Smith.if ${EXPORT_SYMS} != YES
1400b3178a4SMike Smith.if ${EXPORT_SYMS} == NO
1410b3178a4SMike Smith	touch ${.OBJDIR}/export_syms
1420b3178a4SMike Smith.elif !exists(${.CURDIR}/${EXPORT_SYMS})
1430b3178a4SMike Smith	echo ${EXPORT_SYMS} > ${.OBJDIR}/export_syms
1440b3178a4SMike Smith.else
1450b3178a4SMike Smith	grep -v '^#' < ${EXPORT_SYMS} >  ${.OBJDIR}/export_syms
1460b3178a4SMike Smith.endif
1470b3178a4SMike Smith	awk -f ${SYSDIR}/conf/kmod_syms.awk ${.TARGET} \
1480b3178a4SMike Smith		${.OBJDIR}/export_syms | \
14993f03548SMike Smith	xargs -J% ${OBJCOPY} % ${.TARGET}
15093f03548SMike Smith.endif
1510b3178a4SMike Smith.endif
1520b3178a4SMike Smith
1534a2d2630SBruce Evans_ILINKS=@ machine
154639a2e50SGarrett Wollman
155b25a566dSRuslan Ermilovall: objwarn ${PROG}
1564a2d2630SBruce Evans
157985f41afSWarner Loshbeforedepend: ${_ILINKS}
158985f41afSWarner Losh	@rm -f .depend
159985f41afSWarner Losh
160f3cc9575SWarner Losh# Ensure that the links exist without depending on it when it exists which
161f3cc9575SWarner Losh# causes all the modules to be rebuilt when the directory pointed to changes.
162f3cc9575SWarner Losh.for _link in ${_ILINKS}
163f3cc9575SWarner Losh.if !exists(${.OBJDIR}/${_link})
164f3cc9575SWarner Losh${OBJS}: ${_link}
165f3cc9575SWarner Losh.endif
166f3cc9575SWarner Losh.endfor
1674a2d2630SBruce Evans
1689a8631b3SWarner Losh# Search for kernel source tree in standard places.
1699a8631b3SWarner Losh.for _dir in ${.CURDIR}/../.. ${.CURDIR}/../../.. /sys /usr/src/sys
170eefb5d68SWarner Losh.if !defined(SYSDIR) && exists(${_dir}/kern)
1719a8631b3SWarner LoshSYSDIR=	${_dir}
1729a8631b3SWarner Losh.endif
1739a8631b3SWarner Losh.endfor
174eefb5d68SWarner Losh.if !defined(SYSDIR) || !exists(${SYSDIR}/kern)
1759a8631b3SWarner Losh.error "can't find kernel source tree"
1769a8631b3SWarner Losh.endif
1779a8631b3SWarner Losh
1784a2d2630SBruce Evans${_ILINKS}:
1799a8631b3SWarner Losh	@case ${.TARGET} in \
18008a04a89SPeter Wemm	machine) \
1819a8631b3SWarner Losh		path=${SYSDIR}/${MACHINE_ARCH}/include ;; \
18208a04a89SPeter Wemm	@) \
1839a8631b3SWarner Losh		path=${SYSDIR} ;; \
18408a04a89SPeter Wemm	esac ; \
18508a04a89SPeter Wemm	path=`(cd $$path && /bin/pwd)` ; \
18608a04a89SPeter Wemm	${ECHO} ${.TARGET} "->" $$path ; \
18708a04a89SPeter Wemm	ln -s $$path ${.TARGET}
1884a2d2630SBruce Evans
189361186e5SMaxime HenrionCLEANFILES+= ${PROG} ${KMOD}.kld ${OBJS} ${_ILINKS} symb.tmp tmp.o
190361186e5SMaxime Henrion
191361186e5SMaxime Henrion.if defined(DEBUG)
192361186e5SMaxime HenrionCLEANFILES+= ${FULLPROG}
193361186e5SMaxime Henrion.endif
194639a2e50SGarrett Wollman
195639a2e50SGarrett Wollman.if !target(install)
196639a2e50SGarrett Wollman
197ea87b3cbSDavid E. O'Brien_INSTALLFLAGS:=	${INSTALLFLAGS}
198e902c1bbSTim Vanderhoek.for ie in ${INSTALLFLAGS_EDIT}
199e902c1bbSTim Vanderhoek_INSTALLFLAGS:=	${_INSTALLFLAGS${ie}}
200e902c1bbSTim Vanderhoek.endfor
201e902c1bbSTim Vanderhoek
2022cbc81ffSRuslan Ermilov.if defined(DEBUG)
203b25a566dSRuslan Ermilovinstall.debug:
2042cbc81ffSRuslan Ermilov	cd ${.CURDIR}; ${MAKE} -DINSTALL_DEBUG install
2052cbc81ffSRuslan Ermilov.endif
206a4c00586SDag-Erling Smørgrav
2072b06260dSRuslan Ermilov.if !target(realinstall)
2082b06260dSRuslan Ermilovrealinstall: _kmodinstall
2092b06260dSRuslan Ermilov.ORDER: beforeinstall _kmodinstall
2102cbc81ffSRuslan Ermilov.if defined(DEBUG) && defined(INSTALL_DEBUG)
2112b06260dSRuslan Ermilov_kmodinstall:
212d2893b16SRuslan Ermilov	${INSTALL} -o ${KMODOWN} -g ${KMODGRP} -m ${KMODMODE} \
2132b06260dSRuslan Ermilov	    ${_INSTALLFLAGS} ${FULLPROG} ${DESTDIR}${KMODDIR}
2142cbc81ffSRuslan Ermilov.else
2152b06260dSRuslan Ermilov_kmodinstall:
216d2893b16SRuslan Ermilov	${INSTALL} -o ${KMODOWN} -g ${KMODGRP} -m ${KMODMODE} \
2172b06260dSRuslan Ermilov	    ${_INSTALLFLAGS} ${PROG} ${DESTDIR}${KMODDIR}
2182b06260dSRuslan Ermilov
2192b06260dSRuslan Ermilov.include <bsd.links.mk>
2202b06260dSRuslan Ermilov
2214875819cSJake Burkholder.if !defined(NO_XREF)
2222b06260dSRuslan Ermilovafterinstall: _kldxref
2232b06260dSRuslan Ermilov.ORDER: realinstall _kldxref
2242b06260dSRuslan Ermilov.ORDER: _installlinks _kldxref
2252b06260dSRuslan Ermilov_kldxref:
226be1d673dSRuslan Ermilov	@if type kldxref >/dev/null 2>&1; then \
227be1d673dSRuslan Ermilov		${ECHO} kldxref ${DESTDIR}${KMODDIR}; \
228be1d673dSRuslan Ermilov		kldxref ${DESTDIR}${KMODDIR}; \
229be1d673dSRuslan Ermilov	fi
230505222d3SPeter Wemm.endif
2312cbc81ffSRuslan Ermilov.endif
2322b06260dSRuslan Ermilov.endif !target(realinstall)
233639a2e50SGarrett Wollman
2342b06260dSRuslan Ermilov.endif !target(install)
235639a2e50SGarrett Wollman
236639a2e50SGarrett Wollman.if !target(load)
2379a8631b3SWarner Loshload:	${PROG}
238dba3dc7bSNick Hibma	${KMODLOAD} -v ${.OBJDIR}/${KMOD}.ko
239639a2e50SGarrett Wollman.endif
240639a2e50SGarrett Wollman
241639a2e50SGarrett Wollman.if !target(unload)
242fe3cac87SNick Hibmaunload:
2439a8631b3SWarner Losh	${KMODUNLOAD} -v ${KMOD}
24408a04a89SPeter Wemm.endif
2451aa57be3SGarrett Wollman
2466b1bb559SWarner Losh.if defined(KERNBUILDDIR)
2476b1bb559SWarner Losh.PATH: ${KERNBUILDDIR}
2486b1bb559SWarner LoshCFLAGS += -I${KERNBUILDDIR}
2496b1bb559SWarner Losh.for _src in ${SRCS:Mopt_*.h}
2506b1bb559SWarner LoshCLEANFILES+=	${_src}
2516b1bb559SWarner Losh.if !target(${_src})
2526b1bb559SWarner Losh${_src}:
2536b1bb559SWarner Losh	ln -s ${KERNBUILDDIR}/${_src} ${.TARGET}
2546b1bb559SWarner Losh.endif
2556b1bb559SWarner Losh.endfor
2566b1bb559SWarner Losh.else
2574fea6704SBruce Evans.for _src in ${SRCS:Mopt_*.h}
2584fea6704SBruce EvansCLEANFILES+=	${_src}
2594fea6704SBruce Evans.if !target(${_src})
2604fea6704SBruce Evans${_src}:
2614fea6704SBruce Evans	touch ${.TARGET}
2624fea6704SBruce Evans.endif
2634fea6704SBruce Evans.endfor
2646b1bb559SWarner Losh.endif
2654fea6704SBruce Evans
2667fecffecSDoug RabsonMFILES?= kern/bus_if.m kern/device_if.m dev/iicbus/iicbb_if.m \
2676f2d8adbSBoris Popov    dev/iicbus/iicbus_if.m isa/isa_if.m \
2686f2d8adbSBoris Popov    libkern/iconv_converter_if.m \
2696f2d8adbSBoris Popov    dev/mii/miibus_if.m \
2707e820aaaSMike Smith    dev/pccard/card_if.m dev/pccard/power_if.m dev/pci/pci_if.m \
2717e820aaaSMike Smith    dev/pci/pcib_if.m dev/ppbus/ppbus_if.m dev/smbus/smbus_if.m \
2720f55ac6cSCameron Grant    dev/usb/usb_if.m dev/sound/pcm/ac97_if.m dev/sound/pcm/channel_if.m \
273c4f9e3aeSSam Leffler    dev/sound/pcm/feeder_if.m dev/sound/pcm/mixer_if.m pci/agp_if.m \
2742bec1c89SMarcel Moolenaar    opencrypto/crypto_if.m pc98/pc98/canbus_if.m dev/uart/uart_if.m
2757fecffecSDoug Rabson
2767fecffecSDoug Rabson.for _srcsrc in ${MFILES}
2774fea6704SBruce Evans.for _ext in c h
2784fea6704SBruce Evans.for _src in ${SRCS:M${_srcsrc:T:R}.${_ext}}
2794fea6704SBruce EvansCLEANFILES+=	${_src}
2804fea6704SBruce Evans.if !target(${_src})
281f3cc9575SWarner Losh.if !exists(@)
2824fea6704SBruce Evans${_src}: @
283f3cc9575SWarner Losh.endif
2844fea6704SBruce Evans.if exists(@)
2859fa411aeSDavid E. O'Brien${_src}: @/tools/makeobjops.awk @/${_srcsrc}
2864fea6704SBruce Evans.endif
2879fa411aeSDavid E. O'Brien	${AWK} -f @/tools/makeobjops.awk @/${_srcsrc} -${_ext}
2884fea6704SBruce Evans.endif
2894fea6704SBruce Evans.endfor # _src
2904fea6704SBruce Evans.endfor # _ext
2914fea6704SBruce Evans.endfor # _srcsrc
2924fea6704SBruce Evans
2939029b644SPeter Wemm.for _ext in c h
2949029b644SPeter Wemm.if ${SRCS:Mvnode_if.${_ext}} != ""
2959029b644SPeter WemmCLEANFILES+=	vnode_if.${_ext}
296f3cc9575SWarner Losh.if !exists(@)
2979029b644SPeter Wemmvnode_if.${_ext}: @
298f3cc9575SWarner Losh.endif
2994fea6704SBruce Evans.if exists(@)
300b7bbb709SDavid E. O'Brienvnode_if.${_ext}: @/tools/vnode_if.awk @/kern/vnode_if.src
3014fea6704SBruce Evans.endif
302b7bbb709SDavid E. O'Brien	${AWK} -f @/tools/vnode_if.awk @/kern/vnode_if.src -${_ext}
3034fea6704SBruce Evans.endif
3049029b644SPeter Wemm.endfor
3051aa57be3SGarrett Wollman
3062d3ce713SDavid E. O'Brien.if ${SRCS:Mmiidevs.h} != ""
3072d3ce713SDavid E. O'BrienCLEANFILES+=	miidevs.h
3082d3ce713SDavid E. O'Brien.if !exists(@)
3092d3ce713SDavid E. O'Brienmiidevs.h: @
3102d3ce713SDavid E. O'Brien.endif
3112d3ce713SDavid E. O'Brien.if exists(@)
3122d3ce713SDavid E. O'Brienmiidevs.h: @/tools/devlist2h.awk @/dev/mii/miidevs
3132d3ce713SDavid E. O'Brien.endif
3142d3ce713SDavid E. O'Brien	${AWK} -f @/tools/devlist2h.awk @/dev/mii/miidevs
3152d3ce713SDavid E. O'Brien.endif
3162d3ce713SDavid E. O'Brien
31771395925SPeter Wemmregress:
31869b87d3eSEivind Eklund
319766f7d6eSMark Murraylint: ${SRCS}
320bbb467cdSRuslan Ermilov	${LINT} ${LINTKERNFLAGS} ${CFLAGS:M-[DILU]*} ${.ALLSRC:M*.c}
321766f7d6eSMark Murray
32268e00b92SJordan K. Hubbard.include <bsd.dep.mk>
32347da63c3SBruce Evans
324f2bffe0fSRuslan Ermilov.if !exists(${.OBJDIR}/${DEPENDFILE})
325589d9341SBruce Evans${OBJS}: ${SRCS:M*.h}
32647da63c3SBruce Evans.endif
32747da63c3SBruce Evans
328afba897fSBruce Evans.include <bsd.obj.mk>
329ddc474a8SRuslan Ermilov.include "kern.mk"
330