kmod.mk revision 133589
138494Sobrien#	From: @(#)bsd.prog.mk	5.26 (Berkeley) 6/25/91
2310490Scy# $FreeBSD: head/sys/conf/kmod.mk 133589 2004-08-12 17:41:33Z marius $
338494Sobrien#
438494Sobrien# The include file <bsd.kmod.mk> handles installing Kernel Loadable Device
538494Sobrien# drivers (KLD's).
638494Sobrien#
738494Sobrien#
838494Sobrien# +++ variables +++
938494Sobrien#
1038494Sobrien# CLEANFILES	Additional files to remove for the clean and cleandir targets.
1138494Sobrien#
1238494Sobrien# KMOD          The name of the kernel module to build.
1338494Sobrien#
1438494Sobrien# KMODDIR	Base path for kernel modules (see kld(4)). [/boot/kernel]
1538494Sobrien#
1638494Sobrien# KMODOWN	KLD owner. [${BINOWN}]
1738494Sobrien#
1838494Sobrien# KMODGRP	KLD group. [${BINGRP}]
19310490Scy#
2038494Sobrien# KMODMODE	KLD mode. [${BINMODE}]
2138494Sobrien#
2238494Sobrien# KMODLOAD	Command to load a kernel module [/sbin/kldload]
2338494Sobrien#
2438494Sobrien# KMODUNLOAD	Command to unload a kernel module [/sbin/kldunload]
2538494Sobrien#
2638494Sobrien# PROG          The name of the kernel module to build.
2738494Sobrien#		If not supplied, ${KMOD}.o is used.
2838494Sobrien#
2938494Sobrien# SRCS          List of source files
3038494Sobrien#
3138494Sobrien# DESTDIR	Change the tree where the module gets installed. [not set]
3238494Sobrien#
3338494Sobrien# MFILES	Optionally a list of interfaces used by the module.
3438494Sobrien#		This file contains a default list of interfaces.
3538494Sobrien#
36174294Sobrien# EXPORT_SYMS	A list of symbols that should be exported from the module,
3738494Sobrien#		or the name of a file containing a list of symbols, or YES
3838494Sobrien#		to export all symbols.  If not defined, no symbols are
3938494Sobrien#		exported.
4038494Sobrien#
4138494Sobrien# +++ targets +++
4238494Sobrien#
4338494Sobrien# 	install:
4438494Sobrien#               install the kernel module; if the Makefile
4538494Sobrien#               does not itself define the target install, the targets
4638494Sobrien#               beforeinstall and afterinstall may also be used to cause
4738494Sobrien#               actions immediately before and after the install target
4838494Sobrien#		is executed.
4938494Sobrien#
5038494Sobrien# 	load:
5138494Sobrien#		Load KLD.
5238494Sobrien#
53174294Sobrien# 	unload:
5438494Sobrien#		Unload KLD.
5538494Sobrien#
5638494Sobrien# bsd.obj.mk: clean, cleandir and obj
5738494Sobrien# bsd.dep.mk: cleandepend, depend and tags
5838494Sobrien#
5938494Sobrien
6038494SobrienAWK?=		awk
6138494SobrienKMODLOAD?=	/sbin/kldload
6238494SobrienKMODUNLOAD?=	/sbin/kldunload
63174294SobrienOBJCOPY?=	objcopy
64174294Sobrien
6538494Sobrien.if defined(KMODDEPS)
6638494Sobrien.error "Do not use KMODDEPS on 5.0+, use MODULE_VERSION/MODULE_DEPEND"
67174294Sobrien.endif
68174294Sobrien
69174294Sobrien.include <bsd.init.mk>
70174294Sobrien
7138494Sobrien.SUFFIXES: .out .o .c .cc .cxx .C .y .l .s .S
72174294Sobrien
73174294Sobrien.if ${CC} == "icc"
74174294SobrienCFLAGS:=	${CFLAGS:C/(-x[^M^K^W]+)[MKW]+|-x[MKW]+/\1/}
75174294Sobrien.endif
76174294SobrienCFLAGS+=	${COPTS} -D_KERNEL
77174294SobrienCFLAGS+=	-DKLD_MODULE
78174294Sobrien
7938494Sobrien# Don't use any standard or source-relative include directories.
8038494Sobrien# Since -nostdinc will annull any previous -I paths, we repeat all
8138494Sobrien# such paths after -nostdinc.  It doesn't seem to be possible to
8238494Sobrien# add to the front of `make' variable.
8338494Sobrien_ICFLAGS:=	${CFLAGS:M-I*}
8438494Sobrien.if ${CC} == "icc"
8538494SobrienNOSTDINC=	-X
8638494Sobrien.else
87174294SobrienNOSTDINC=	-nostdinc
8838494Sobrien.endif
8938494SobrienCFLAGS+=	${NOSTDINC} -I- ${INCLMAGIC} ${_ICFLAGS}
9038494Sobrien.if defined(KERNBUILDDIR)
91174294SobrienCFLAGS+=       -include ${KERNBUILDDIR}/opt_global.h
92174294Sobrien.endif
93174294Sobrien
94310490Scy# Add -I paths for system headers.  Individual KLD makefiles don't
9538494Sobrien# need any -I paths for this.  Similar defaults for .PATH can't be
9638494Sobrien# set because there are no standard paths for non-headers.
9782794SobrienCFLAGS+=	-I. -I@
9838494Sobrien
9938494Sobrien# Add -I path for altq headers as they are included via net/if_var.h
10038494Sobrien# for example.
10138494SobrienCFLAGS+=	-I@/contrib/altq
10238494Sobrien
10338494Sobrien# Add a -I path to standard headers like <stddef.h>.  Use a relative
10438494Sobrien# path to src/include if possible.  If the @ symlink hasn't been built
10538494Sobrien# yet, then we can't tell if the relative path exists.  Add both the
10638494Sobrien# potential relative path and an absolute path in that case.
10738494Sobrien.if exists(@)
108174294Sobrien.if exists(@/../include)
109174294SobrienCFLAGS+=	-I@/../include
11038494Sobrien.else
111174294SobrienCFLAGS+=	-I${DESTDIR}/usr/include
11238494Sobrien.endif
11338494Sobrien.else # !@
11438494SobrienCFLAGS+=	-I@/../include -I${DESTDIR}/usr/include
11538494Sobrien.endif # @
11638494Sobrien
117174294Sobrien.if ${CC} != "icc"
118174294SobrienCFLAGS+=	-finline-limit=${INLINE_LIMIT}
119174294Sobrien.endif
120310490Scy
12138494Sobrien# Disallow common variables, and if we end up with commons from
122310490Scy# somewhere unexpected, allocate storage for them in the module itself.
123174294Sobrien.if ${CC} != "icc"
12438494SobrienCFLAGS+=	-fno-common
12538494Sobrien.endif
12638494SobrienLDFLAGS+=	-d -warn-common
12738494Sobrien
12838494SobrienCFLAGS+=	${DEBUG_FLAGS}
129
130OBJS+=  ${SRCS:N*.h:R:S/$/.o/g}
131
132.if !defined(PROG)
133PROG=	${KMOD}.ko
134.endif
135
136.if !defined(DEBUG_FLAGS)
137FULLPROG=	${PROG}
138.else
139FULLPROG=	${PROG}.debug
140${PROG}: ${FULLPROG}
141	${OBJCOPY} --strip-debug ${FULLPROG} ${PROG}
142.endif
143
144.if ${MACHINE_ARCH} != amd64
145${FULLPROG}: ${KMOD}.kld
146	${LD} -Bshareable ${LDFLAGS} -o ${.TARGET} ${KMOD}.kld
147.if !defined(DEBUG_FLAGS)
148	${OBJCOPY} --strip-debug ${.TARGET}
149.endif
150.endif
151
152EXPORT_SYMS?=	NO
153.if ${EXPORT_SYMS} != YES
154CLEANFILES+=	${.OBJDIR}/export_syms
155.endif
156
157.if ${MACHINE_ARCH} != amd64
158${KMOD}.kld: ${OBJS}
159.else
160${FULLPROG}: ${OBJS}
161.endif
162	${LD} ${LDFLAGS} -r -d -o ${.TARGET} ${OBJS}
163.if defined(EXPORT_SYMS)
164.if ${EXPORT_SYMS} != YES
165.if ${EXPORT_SYMS} == NO
166	touch ${.OBJDIR}/export_syms
167.elif !exists(${.CURDIR}/${EXPORT_SYMS})
168	echo ${EXPORT_SYMS} > ${.OBJDIR}/export_syms
169.else
170	grep -v '^#' < ${EXPORT_SYMS} >  ${.OBJDIR}/export_syms
171.endif
172	awk -f ${SYSDIR}/conf/kmod_syms.awk ${.TARGET} \
173		${.OBJDIR}/export_syms | \
174	xargs -J% ${OBJCOPY} % ${.TARGET}
175.endif
176.endif
177.if !defined(DEBUG_FLAGS) && ${MACHINE_ARCH} == amd64
178	${OBJCOPY} --strip-debug ${.TARGET}
179.endif
180
181_ILINKS=@ machine
182
183all: objwarn ${PROG}
184
185beforedepend: ${_ILINKS}
186	@rm -f .depend
187
188# Ensure that the links exist without depending on it when it exists which
189# causes all the modules to be rebuilt when the directory pointed to changes.
190.for _link in ${_ILINKS}
191.if !exists(${.OBJDIR}/${_link})
192${OBJS}: ${_link}
193.endif
194.endfor
195
196# Search for kernel source tree in standard places.
197.for _dir in ${.CURDIR}/../.. ${.CURDIR}/../../.. /sys /usr/src/sys
198.if !defined(SYSDIR) && exists(${_dir}/kern/)
199SYSDIR=	${_dir}
200.endif
201.endfor
202.if !defined(SYSDIR) || !exists(${SYSDIR}/kern/)
203.error "can't find kernel source tree"
204.endif
205
206${_ILINKS}:
207	@case ${.TARGET} in \
208	machine) \
209		path=${SYSDIR}/${MACHINE_ARCH}/include ;; \
210	@) \
211		path=${SYSDIR} ;; \
212	esac ; \
213	path=`(cd $$path && /bin/pwd)` ; \
214	${ECHO} ${.TARGET} "->" $$path ; \
215	ln -s $$path ${.TARGET}
216
217CLEANFILES+= ${PROG} ${KMOD}.kld ${OBJS} ${_ILINKS} symb.tmp tmp.o
218
219.if defined(DEBUG_FLAGS)
220CLEANFILES+= ${FULLPROG}
221.endif
222
223.if !target(install)
224
225_INSTALLFLAGS:=	${INSTALLFLAGS}
226.for ie in ${INSTALLFLAGS_EDIT}
227_INSTALLFLAGS:=	${_INSTALLFLAGS${ie}}
228.endfor
229
230.if defined(DEBUG_FLAGS)
231install.debug:
232	cd ${.CURDIR}; ${MAKE} -DINSTALL_DEBUG install
233.endif
234
235.if !target(realinstall)
236realinstall: _kmodinstall
237.ORDER: beforeinstall _kmodinstall
238_kmodinstall:
239.if defined(DEBUG_FLAGS) && defined(INSTALL_DEBUG)
240	${INSTALL} -o ${KMODOWN} -g ${KMODGRP} -m ${KMODMODE} \
241	    ${_INSTALLFLAGS} ${FULLPROG} ${DESTDIR}${KMODDIR}
242.else
243	${INSTALL} -o ${KMODOWN} -g ${KMODGRP} -m ${KMODMODE} \
244	    ${_INSTALLFLAGS} ${PROG} ${DESTDIR}${KMODDIR}
245
246.include <bsd.links.mk>
247
248.if !defined(NO_XREF)
249afterinstall: _kldxref
250.ORDER: realinstall _kldxref
251.ORDER: _installlinks _kldxref
252_kldxref:
253	@if type kldxref >/dev/null 2>&1; then \
254		${ECHO} kldxref ${DESTDIR}${KMODDIR}; \
255		kldxref ${DESTDIR}${KMODDIR}; \
256	fi
257.endif
258.endif
259.endif !target(realinstall)
260
261.endif !target(install)
262
263.if !target(load)
264load:	${PROG}
265	${KMODLOAD} -v ${.OBJDIR}/${KMOD}.ko
266.endif
267
268.if !target(unload)
269unload:
270	${KMODUNLOAD} -v ${KMOD}
271.endif
272
273.if defined(KERNBUILDDIR)
274.PATH: ${KERNBUILDDIR}
275CFLAGS += -I${KERNBUILDDIR}
276.for _src in ${SRCS:Mopt_*.h}
277CLEANFILES+=	${_src}
278.if !target(${_src})
279${_src}:
280	ln -s ${KERNBUILDDIR}/${_src} ${.TARGET}
281.endif
282.endfor
283.else
284.for _src in ${SRCS:Mopt_*.h}
285CLEANFILES+=	${_src}
286.if !target(${_src})
287${_src}:
288	touch ${.TARGET}
289.endif
290.endfor
291.endif
292
293MFILES?= kern/bus_if.m kern/device_if.m dev/iicbus/iicbb_if.m \
294    dev/iicbus/iicbus_if.m isa/isa_if.m \
295    libkern/iconv_converter_if.m \
296    dev/acpica/acpi_if.m dev/eisa/eisa_if.m dev/mii/miibus_if.m \
297    dev/ofw/ofw_bus_if.m \
298    dev/pccard/card_if.m dev/pccard/power_if.m dev/pci/pci_if.m \
299    dev/pci/pcib_if.m dev/ppbus/ppbus_if.m dev/smbus/smbus_if.m \
300    dev/usb/usb_if.m dev/sound/pcm/ac97_if.m dev/sound/pcm/channel_if.m \
301    dev/sound/pcm/feeder_if.m dev/sound/pcm/mixer_if.m pci/agp_if.m \
302    opencrypto/crypto_if.m pc98/pc98/canbus_if.m dev/uart/uart_if.m
303
304.for _srcsrc in ${MFILES}
305.for _ext in c h
306.for _src in ${SRCS:M${_srcsrc:T:R}.${_ext}}
307CLEANFILES+=	${_src}
308.if !target(${_src})
309.if !exists(@)
310${_src}: @
311.endif
312.if exists(@)
313${_src}: @/tools/makeobjops.awk @/${_srcsrc}
314.endif
315	${AWK} -f @/tools/makeobjops.awk @/${_srcsrc} -${_ext}
316.endif
317.endfor # _src
318.endfor # _ext
319.endfor # _srcsrc
320
321.for _ext in c h
322.if ${SRCS:Mvnode_if.${_ext}} != ""
323CLEANFILES+=	vnode_if.${_ext}
324.if !exists(@)
325vnode_if.${_ext}: @
326.endif
327.if exists(@)
328vnode_if.${_ext}: @/tools/vnode_if.awk @/kern/vnode_if.src
329.endif
330	${AWK} -f @/tools/vnode_if.awk @/kern/vnode_if.src -${_ext}
331.endif
332.endfor
333
334.for _i in mii pccard usb
335.if ${SRCS:M${_i}devs.h} != ""
336CLEANFILES+=	${_i}devs.h
337.if !exists(@)
338${_i}devs.h: @
339.endif
340.if exists(@)
341${_i}devs.h: @/tools/${_i}devs2h.awk @/dev/${_i}/${_i}devs
342.endif
343	${AWK} -f @/tools/${_i}devs2h.awk @/dev/${_i}/${_i}devs
344.endif
345.endfor # _i
346
347.if ${SRCS:Macpi_quirks.h} != ""
348CLEANFILES+=	acpi_quirks.h
349.if !exists(@)
350acpi_quirks.h: @
351.endif
352.if exists(@)
353acpi_quirks.h: @/tools/acpi_quirks2h.awk @/dev/acpica/acpi_quirks
354.endif
355	${AWK} -f @/tools/acpi_quirks2h.awk @/dev/acpica/acpi_quirks
356.endif
357
358regress:
359
360lint: ${SRCS}
361	${LINT} ${LINTKERNFLAGS} ${CFLAGS:M-[DILU]*} ${.ALLSRC:M*.c}
362
363.include <bsd.dep.mk>
364
365.if !exists(${.OBJDIR}/${DEPENDFILE})
366${OBJS}: ${SRCS:M*.h}
367.endif
368
369.include <bsd.obj.mk>
370.include "kern.mk"
371