kmod.mk revision 163528
1140072Strhodes#	From: @(#)bsd.prog.mk	5.26 (Berkeley) 6/25/91
2140072Strhodes# $FreeBSD: head/sys/conf/kmod.mk 163528 2006-10-20 07:31:15Z imp $
3140072Strhodes#
4140072Strhodes# The include file <bsd.kmod.mk> handles building and installing loadable
5140072Strhodes# kernel modules.
6140072Strhodes#
7140072Strhodes#
8140072Strhodes# +++ variables +++
9140072Strhodes#
10140072Strhodes# CLEANFILES	Additional files to remove for the clean and cleandir targets.
11140072Strhodes#
12140072Strhodes# EXPORT_SYMS	A list of symbols that should be exported from the module,
13140072Strhodes#		or the name of a file containing a list of symbols, or YES
14140072Strhodes#		to export all symbols.  If not defined, no symbols are
15140072Strhodes#		exported.
16140072Strhodes#
17140072Strhodes# KMOD		The name of the kernel module to build.
18140072Strhodes#
19140072Strhodes# KMODDIR	Base path for kernel modules (see kld(4)). [/boot/kernel]
20140072Strhodes#
21140072Strhodes# KMODOWN	Module file owner. [${BINOWN}]
22140072Strhodes#
23140072Strhodes# KMODGRP	Module file group. [${BINGRP}]
24140072Strhodes#
25140072Strhodes# KMODMODE	Module file mode. [${BINMODE}]
26140072Strhodes#
27140072Strhodes# KMODLOAD	Command to load a kernel module [/sbin/kldload]
28140072Strhodes#
29140072Strhodes# KMODUNLOAD	Command to unload a kernel module [/sbin/kldunload]
30140072Strhodes#
31140072Strhodes# MFILES	Optionally a list of interfaces used by the module.
32140072Strhodes#		This file contains a default list of interfaces.
33140072Strhodes#
34140072Strhodes# PROG		The name of the kernel module to build.
35140072Strhodes#		If not supplied, ${KMOD}.ko is used.
36140072Strhodes#
37140072Strhodes# SRCS		List of source files.
38140072Strhodes#
39140072Strhodes# FIRMWS	List of firmware images in format filename:shortname:version
40140072Strhodes#
41140072Strhodes# DESTDIR	The tree where the module gets installed. [not set]
42140072Strhodes#
43140072Strhodes# +++ targets +++
44140072Strhodes#
45140072Strhodes# 	install:
46140072Strhodes#               install the kernel module; if the Makefile
47140072Strhodes#               does not itself define the target install, the targets
48140072Strhodes#               beforeinstall and afterinstall may also be used to cause
49140072Strhodes#               actions immediately before and after the install target
50140072Strhodes#		is executed.
51140072Strhodes#
52140072Strhodes# 	load:
53140072Strhodes#		Load a module.
54140072Strhodes#
55140072Strhodes# 	unload:
56140072Strhodes#		Unload a module.
57140072Strhodes#
58140072Strhodes
59140072StrhodesAWK?=		awk
60140072StrhodesKMODLOAD?=	/sbin/kldload
61140072StrhodesKMODUNLOAD?=	/sbin/kldunload
62140072StrhodesOBJCOPY?=	objcopy
63140072Strhodes
64140072Strhodes.if defined(KMODDEPS)
65241737Sed.error "Do not use KMODDEPS on 5.0+; use MODULE_VERSION/MODULE_DEPEND"
66140072Strhodes.endif
67140072Strhodes
68140072Strhodes.include <bsd.init.mk>
69140072Strhodes
70140072Strhodes.SUFFIXES: .out .o .c .cc .cxx .C .y .l .s .S
71140241Sdelphij
72140072Strhodes.if ${CC} == "icc"
73140072StrhodesCFLAGS:=	${CFLAGS:C/(-x[^M^K^W]+)[MKW]+|-x[MKW]+/\1/}
74140072Strhodes.else
75140072Strhodes. if !empty(CFLAGS:M-O[23s]) && empty(CFLAGS:M-fno-strict-aliasing)
76140072StrhodesCFLAGS+=	-fno-strict-aliasing
77140072Strhodes. endif
78140072StrhodesWERROR?=	-Werror
79140072Strhodes.endif
80140072StrhodesCFLAGS+=	${WERROR}
81140072StrhodesCFLAGS+=	-D_KERNEL
82140072StrhodesCFLAGS+=	-DKLD_MODULE
83140072Strhodes
84140072Strhodes# Don't use any standard or source-relative include directories.
85201217Sed.if ${CC} == "icc"
86140072StrhodesNOSTDINC=	-X
87140072Strhodes.else
88140072StrhodesC_DIALECT=	-std=c99
89140072StrhodesNOSTDINC=	-nostdinc
90140072Strhodes.endif
91140072StrhodesCFLAGS+=	${C_DIALECT}
92140072StrhodesCFLAGS:=	${CFLAGS:N-I*} ${NOSTDINC} -I- ${INCLMAGIC} ${CFLAGS:M-I*}
93140072Strhodes.if defined(KERNBUILDDIR)
94140072StrhodesCFLAGS+=	-DHAVE_KERNEL_OPTION_HEADERS -include ${KERNBUILDDIR}/opt_global.h
95140072Strhodes.endif
96140072Strhodes
97140072Strhodes# Add -I paths for system headers.  Individual module makefiles don't
98140072Strhodes# need any -I paths for this.  Similar defaults for .PATH can't be
99140072Strhodes# set because there are no standard paths for non-headers.
100201217SedCFLAGS+=	-I. -I@
101140072Strhodes
102140072Strhodes# Add -I path for altq headers as they are included via net/if_var.h
103140072Strhodes# for example.
104140072StrhodesCFLAGS+=	-I@/contrib/altq
105140072Strhodes
106140072Strhodes.if ${CC} != "icc"
107140072StrhodesCFLAGS+=	-finline-limit=${INLINE_LIMIT}
108140072StrhodesCFLAGS+= --param inline-unit-growth=100
109140072StrhodesCFLAGS+= --param large-function-growth=1000
110140072Strhodes.endif
111140072Strhodes
112140072Strhodes# Disallow common variables, and if we end up with commons from
113140072Strhodes# somewhere unexpected, allocate storage for them in the module itself.
114140072Strhodes.if ${CC} != "icc"
115140072StrhodesCFLAGS+=	-fno-common
116140072Strhodes.endif
117201217SedLDFLAGS+=	-d -warn-common
118140072Strhodes
119140072StrhodesCFLAGS+=	${DEBUG_FLAGS}
120140072Strhodes.if ${MACHINE_ARCH} == amd64
121140072StrhodesCFLAGS+=	-fno-omit-frame-pointer
122140072Strhodes.endif
123140072Strhodes
124140072Strhodes.if ${MACHINE_ARCH} == "powerpc"
125140072StrhodesCFLAGS+=	-mlongcall -fno-omit-frame-pointer
126140072Strhodes.endif
127140072Strhodes
128140072Strhodes.if defined(FIRMWS)
129140072Strhodes.if !exists(@)
130140072Strhodes${KMOD:S/$/.c/}: @
131140072Strhodes.else
132140072Strhodes${KMOD:S/$/.c/}: @/tools/fw_stub.awk
133140072Strhodes.endif
134140072Strhodes	${AWK} -f @/tools/fw_stub.awk ${FIRMWS} -m${KMOD} -c${KMOD:S/$/.c/g}
135140072Strhodes
136140072StrhodesSRCS+=	${KMOD:S/$/.c/}
137140072StrhodesCLEANFILES+=	${KMOD:S/$/.c/}
138140072Strhodes
139140072Strhodes.for _firmw in ${FIRMWS}
140140072Strhodes${_firmw:C/\:.*$/.fwo/}:	${_firmw:C/\:.*$//}
141140072Strhodes	@${ECHO} ${_firmw:C/\:.*$//} ${.ALLSRC:M*${_firmw:C/\:.*$//}}
142201217Sed	@if [ -e ${_firmw:C/\:.*$//} ]; then			\
143140072Strhodes		${LD} -b binary ${LDFLAGS} -r -d -o ${.TARGET}	\
144140072Strhodes		    ${_firmw:C/\:.*$//};			\
145140072Strhodes	else							\
146140072Strhodes		ln -s ${.ALLSRC:M*${_firmw:C/\:.*$//}} ${_firmw:C/\:.*$//}; \
147140072Strhodes		${LD} -b binary ${LDFLAGS} -r -d -o ${.TARGET}	\
148140072Strhodes		    ${_firmw:C/\:.*$//};			\
149140072Strhodes		rm ${_firmw:C/\:.*$//};				\
150140072Strhodes	fi
151140072Strhodes
152140072StrhodesOBJS+=	${_firmw:C/\:.*$/.fwo/}
153140072Strhodes.endfor
154140072Strhodes.endif
155140072Strhodes
156140072StrhodesOBJS+=	${SRCS:N*.h:R:S/$/.o/g}
157140072Strhodes
158140072Strhodes.if !defined(PROG)
159140072StrhodesPROG=	${KMOD}.ko
160140072Strhodes.endif
161140072Strhodes
162229780Suqs.if !defined(DEBUG_FLAGS)
163140072StrhodesFULLPROG=	${PROG}
164140072Strhodes.else
165140072StrhodesFULLPROG=	${PROG}.debug
166140072Strhodes${PROG}: ${FULLPROG} ${PROG}.symbols
167140072Strhodes	${OBJCOPY} --strip-debug --add-gnu-debuglink=${PROG}.symbols\
168140072Strhodes	    ${FULLPROG} ${.TARGET}
169140072Strhodes${PROG}.symbols: ${FULLPROG}
170140072Strhodes	${OBJCOPY} --only-keep-debug ${FULLPROG} ${.TARGET}
171140072Strhodes.endif
172140072Strhodes
173140072Strhodes.if ${MACHINE_ARCH} != amd64
174140072Strhodes${FULLPROG}: ${KMOD}.kld
175140072Strhodes	${LD} -Bshareable ${LDFLAGS} -o ${.TARGET} ${KMOD}.kld
176140072Strhodes.if !defined(DEBUG_FLAGS)
177140072Strhodes	${OBJCOPY} --strip-debug ${.TARGET}
178140072Strhodes.endif
179140072Strhodes.endif
180140072Strhodes
181140072StrhodesEXPORT_SYMS?=	NO
182140072Strhodes.if ${EXPORT_SYMS} != YES
183201217SedCLEANFILES+=	export_syms
184140072Strhodes.endif
185140072Strhodes
186140072Strhodes.if ${MACHINE_ARCH} != amd64
187140072Strhodes${KMOD}.kld: ${OBJS}
188140072Strhodes.else
189140072Strhodes${FULLPROG}: ${OBJS}
190140072Strhodes.endif
191140072Strhodes	${LD} ${LDFLAGS} -r -d -o ${.TARGET} ${OBJS}
192140072Strhodes.if defined(EXPORT_SYMS)
193140072Strhodes.if ${EXPORT_SYMS} != YES
194140072Strhodes.if ${EXPORT_SYMS} == NO
195140072Strhodes	:> export_syms
196140072Strhodes.elif !exists(${.CURDIR}/${EXPORT_SYMS})
197140072Strhodes	echo ${EXPORT_SYMS} > export_syms
198140072Strhodes.else
199140072Strhodes	grep -v '^#' < ${EXPORT_SYMS} > export_syms
200140072Strhodes.endif
201140072Strhodes	awk -f ${SYSDIR}/conf/kmod_syms.awk ${.TARGET} \
202140072Strhodes	    export_syms | xargs -J% ${OBJCOPY} % ${.TARGET}
203140072Strhodes.endif
204140072Strhodes.endif
205140072Strhodes.if !defined(DEBUG_FLAGS) && ${MACHINE_ARCH} == amd64
206140072Strhodes	${OBJCOPY} --strip-debug ${.TARGET}
207201217Sed.endif
208140072Strhodes
209140072Strhodes_ILINKS=@ machine
210140072Strhodes.if ${MACHINE} != ${MACHINE_ARCH}
211140072Strhodes_ILINKS+=${MACHINE_ARCH}
212140072Strhodes.endif
213140072Strhodes
214140072Strhodesall: objwarn ${PROG}
215140072Strhodes
216140072Strhodesbeforedepend: ${_ILINKS}
217140072Strhodes
218140072Strhodes# Ensure that the links exist without depending on it when it exists which
219140072Strhodes# causes all the modules to be rebuilt when the directory pointed to changes.
220140072Strhodes.for _link in ${_ILINKS}
221140072Strhodes.if !exists(${.OBJDIR}/${_link})
222140072Strhodes${OBJS}: ${_link}
223140072Strhodes.endif
224140072Strhodes.endfor
225140072Strhodes
226140072Strhodes# Search for kernel source tree in standard places.
227140072Strhodes.for _dir in ${.CURDIR}/../.. ${.CURDIR}/../../.. /sys /usr/src/sys
228140072Strhodes.if !defined(SYSDIR) && exists(${_dir}/kern/)
229140072StrhodesSYSDIR=	${_dir}
230140072Strhodes.endif
231140072Strhodes.endfor
232140072Strhodes.if !defined(SYSDIR) || !exists(${SYSDIR}/kern/)
233140072Strhodes.error "can't find kernel source tree"
234140072Strhodes.endif
235140072Strhodes
236140072Strhodes${_ILINKS}:
237140072Strhodes	@case ${.TARGET} in \
238140072Strhodes	${MACHINE_ARCH}) \
239140072Strhodes		path=${SYSDIR}/${MACHINE_ARCH}/include ;; \
240140072Strhodes	machine) \
241140072Strhodes		path=${SYSDIR}/${MACHINE}/include ;; \
242140072Strhodes	@) \
243140072Strhodes		path=${SYSDIR} ;; \
244140072Strhodes	esac ; \
245140072Strhodes	path=`(cd $$path && /bin/pwd)` ; \
246140072Strhodes	${ECHO} ${.TARGET} "->" $$path ; \
247140072Strhodes	ln -sf $$path ${.TARGET}
248140072Strhodes
249140072StrhodesCLEANFILES+= ${PROG} ${KMOD}.kld ${OBJS}
250140072Strhodes
251140072Strhodes.if defined(DEBUG_FLAGS)
252140072StrhodesCLEANFILES+= ${FULLPROG} ${PROG}.symbols
253140072Strhodes.endif
254140072Strhodes
255201217Sed.if !target(install)
256140072Strhodes
257140241Sdelphij_INSTALLFLAGS:=	${INSTALLFLAGS}
258140072Strhodes.for ie in ${INSTALLFLAGS_EDIT}
259140072Strhodes_INSTALLFLAGS:=	${_INSTALLFLAGS${ie}}
260140072Strhodes.endfor
261140072Strhodes
262140072Strhodes.if !target(realinstall)
263140072Strhodesrealinstall: _kmodinstall
264140072Strhodes.ORDER: beforeinstall _kmodinstall
265140072Strhodes_kmodinstall:
266140072Strhodes	${INSTALL} -o ${KMODOWN} -g ${KMODGRP} -m ${KMODMODE} \
267140072Strhodes	    ${_INSTALLFLAGS} ${PROG} ${DESTDIR}${KMODDIR}
268140072Strhodes.if defined(DEBUG_FLAGS) && !defined(INSTALL_NODEBUG)
269140072Strhodes	${INSTALL} -o ${KMODOWN} -g ${KMODGRP} -m ${KMODMODE} \
270140072Strhodes	    ${_INSTALLFLAGS} ${PROG}.symbols ${DESTDIR}${KMODDIR}
271140072Strhodes.endif
272140072Strhodes
273140072Strhodes.include <bsd.links.mk>
274140072Strhodes
275140072Strhodes.if !defined(NO_XREF)
276140072Strhodesafterinstall: _kldxref
277140072Strhodes.ORDER: realinstall _kldxref
278140072Strhodes.ORDER: _installlinks _kldxref
279140072Strhodes_kldxref:
280140072Strhodes	@if type kldxref >/dev/null 2>&1; then \
281140072Strhodes		${ECHO} kldxref ${DESTDIR}${KMODDIR}; \
282140072Strhodes		kldxref ${DESTDIR}${KMODDIR}; \
283140072Strhodes	fi
284140072Strhodes.endif
285140072Strhodes.endif # !target(realinstall)
286140072Strhodes
287140072Strhodes.endif # !target(install)
288140072Strhodes
289140072Strhodes.if !target(load)
290140072Strhodesload: ${PROG}
291140072Strhodes	${KMODLOAD} -v ${.OBJDIR}/${PROG}
292140072Strhodes.endif
293140072Strhodes
294140072Strhodes.if !target(unload)
295140072Strhodesunload:
296140072Strhodes	${KMODUNLOAD} -v ${PROG}
297140072Strhodes.endif
298140072Strhodes
299140072Strhodes.if defined(KERNBUILDDIR)
300140072Strhodes.PATH: ${KERNBUILDDIR}
301140072StrhodesCFLAGS+=	-I${KERNBUILDDIR}
302140072Strhodes.for _src in ${SRCS:Mopt_*.h}
303140072StrhodesCLEANFILES+=	${_src}
304140072Strhodes.if !target(${_src})
305140072Strhodes${_src}:
306140072Strhodes	ln -sf ${KERNBUILDDIR}/${_src} ${.TARGET}
307140072Strhodes.endif
308140072Strhodes.endfor
309140072Strhodes.else
310140072Strhodes.for _src in ${SRCS:Mopt_*.h}
311140072StrhodesCLEANFILES+=	${_src}
312140072Strhodes.if !target(${_src})
313140072Strhodes${_src}:
314140072Strhodes	:> ${.TARGET}
315140072Strhodes.endif
316140072Strhodes.endfor
317140072Strhodes.endif
318140072Strhodes
319140072StrhodesMFILES?= dev/acpica/acpi_if.m dev/ata/ata_if.m dev/eisa/eisa_if.m \
320140072Strhodes	dev/iicbus/iicbb_if.m dev/iicbus/iicbus_if.m \
321140072Strhodes	dev/mmc/mmcbr_if.m mmc/mmcbus_if.m \
322140072Strhodes	dev/mii/miibus_if.m dev/ofw/ofw_bus_if.m \
323140072Strhodes	dev/pccard/card_if.m dev/pccard/power_if.m dev/pci/pci_if.m \
324140072Strhodes	dev/pci/pcib_if.m dev/ppbus/ppbus_if.m dev/smbus/smbus_if.m \
325140072Strhodes	dev/sound/pcm/ac97_if.m dev/sound/pcm/channel_if.m \
326	dev/sound/pcm/feeder_if.m dev/sound/pcm/mixer_if.m \
327	dev/sound/midi/mpu_if.m dev/sound/midi/mpufoi_if.m \
328	dev/sound/midi/synth_if.m dev/usb/usb_if.m isa/isa_if.m \
329	kern/bus_if.m kern/cpufreq_if.m kern/device_if.m kern/serdev_if.m \
330	libkern/iconv_converter_if.m opencrypto/crypto_if.m \
331	pc98/pc98/canbus_if.m pci/agp_if.m
332
333.for _srcsrc in ${MFILES}
334.for _ext in c h
335.for _src in ${SRCS:M${_srcsrc:T:R}.${_ext}}
336CLEANFILES+=	${_src}
337.if !target(${_src})
338.if !exists(@)
339${_src}: @
340.else
341${_src}: @/tools/makeobjops.awk @/${_srcsrc}
342.endif
343	${AWK} -f @/tools/makeobjops.awk @/${_srcsrc} -${_ext}
344.endif
345.endfor # _src
346.endfor # _ext
347.endfor # _srcsrc
348
349.if !empty(SRCS:Mvnode_if.c)
350CLEANFILES+=	vnode_if.c
351.if !exists(@)
352vnode_if.c: @
353.else
354vnode_if.c: @/tools/vnode_if.awk @/kern/vnode_if.src
355.endif
356	${AWK} -f @/tools/vnode_if.awk @/kern/vnode_if.src -c
357.endif
358
359.if !empty(SRCS:Mvnode_if.h)
360CLEANFILES+=	vnode_if.h vnode_if_newproto.h vnode_if_typedef.h
361.if !exists(@)
362vnode_if.h vnode_if_newproto.h vnode_if_typedef.h: @
363.else
364vnode_if.h vnode_if_newproto.h vnode_if_typedef.h: @/tools/vnode_if.awk \
365    @/kern/vnode_if.src
366.endif
367vnode_if.h: vnode_if_newproto.h vnode_if_typedef.h
368	${AWK} -f @/tools/vnode_if.awk @/kern/vnode_if.src -h
369vnode_if_newproto.h:
370	${AWK} -f @/tools/vnode_if.awk @/kern/vnode_if.src -p
371vnode_if_typedef.h:
372	${AWK} -f @/tools/vnode_if.awk @/kern/vnode_if.src -q
373.endif
374
375.for _i in mii pccard
376.if !empty(SRCS:M${_i}devs.h)
377CLEANFILES+=	${_i}devs.h
378.if !exists(@)
379${_i}devs.h: @
380.else
381${_i}devs.h: @/tools/${_i}devs2h.awk @/dev/${_i}/${_i}devs
382.endif
383	${AWK} -f @/tools/${_i}devs2h.awk @/dev/${_i}/${_i}devs
384.endif
385.endfor # _i
386
387.if !empty(SRCS:Musbdevs.h)
388CLEANFILES+=	usbdevs.h
389.if !exists(@)
390usbdevs.h: @
391.else
392usbdevs.h: @/tools/usbdevs2h.awk @/dev/usb/usbdevs
393.endif
394	${AWK} -f @/tools/usbdevs2h.awk @/dev/usb/usbdevs -h
395.endif
396
397.if !empty(SRCS:Musbdevs_data.h)
398CLEANFILES+=	usbdevs_data.h
399.if !exists(@)
400usbdevs_data.h: @
401.else
402usbdevs_data.h: @/tools/usbdevs2h.awk @/dev/usb/usbdevs
403.endif
404	${AWK} -f @/tools/usbdevs2h.awk @/dev/usb/usbdevs -d
405.endif
406
407.if !empty(SRCS:Macpi_quirks.h)
408CLEANFILES+=	acpi_quirks.h
409.if !exists(@)
410acpi_quirks.h: @
411.else
412acpi_quirks.h: @/tools/acpi_quirks2h.awk @/dev/acpica/acpi_quirks
413.endif
414	${AWK} -f @/tools/acpi_quirks2h.awk @/dev/acpica/acpi_quirks
415.endif
416
417.if !empty(SRCS:Massym.s)
418CLEANFILES+=	assym.s genassym.o
419assym.s: genassym.o
420.if !exists(@)
421assym.s: @
422.else
423assym.s: @/kern/genassym.sh
424.endif
425	sh @/kern/genassym.sh genassym.o > ${.TARGET}
426.if exists(@)
427genassym.o: @/${MACHINE_ARCH}/${MACHINE_ARCH}/genassym.c
428.endif
429genassym.o: @ machine ${SRCS:Mopt_*.h}
430	${CC} -c ${CFLAGS:N-fno-common} \
431	    @/${MACHINE_ARCH}/${MACHINE_ARCH}/genassym.c
432.endif
433
434lint: ${SRCS}
435	${LINT} ${LINTKERNFLAGS} ${CFLAGS:M-[DILU]*} ${.ALLSRC:M*.c}
436
437.include <bsd.dep.mk>
438
439cleandepend: cleanilinks
440# .depend needs include links so we remove them only together.
441cleanilinks:
442	rm -f ${_ILINKS}
443
444.if !exists(${.OBJDIR}/${DEPENDFILE})
445${OBJS}: ${SRCS:M*.h}
446.endif
447
448.include <bsd.obj.mk>
449.include "kern.mk"
450