kmod.mk revision 155275
1238106Sdes#	From: @(#)bsd.prog.mk	5.26 (Berkeley) 6/25/91
2238106Sdes# $FreeBSD: head/sys/conf/kmod.mk 155275 2006-02-04 06:22:27Z imp $
3238106Sdes#
4238106Sdes# The include file <bsd.kmod.mk> handles building and installing loadable
5238106Sdes# kernel modules.
6238106Sdes#
7238106Sdes#
8238106Sdes# +++ variables +++
9238106Sdes#
10238106Sdes# CLEANFILES	Additional files to remove for the clean and cleandir targets.
11238106Sdes#
12238106Sdes# EXPORT_SYMS	A list of symbols that should be exported from the module,
13238106Sdes#		or the name of a file containing a list of symbols, or YES
14238106Sdes#		to export all symbols.  If not defined, no symbols are
15238106Sdes#		exported.
16238106Sdes#
17238106Sdes# KMOD		The name of the kernel module to build.
18238106Sdes#
19238106Sdes# KMODDIR	Base path for kernel modules (see kld(4)). [/boot/kernel]
20238106Sdes#
21238106Sdes# KMODOWN	Module file owner. [${BINOWN}]
22238106Sdes#
23238106Sdes# KMODGRP	Module file group. [${BINGRP}]
24269257Sdes#
25269257Sdes# KMODMODE	Module file mode. [${BINMODE}]
26269257Sdes#
27269257Sdes# KMODLOAD	Command to load a kernel module [/sbin/kldload]
28269257Sdes#
29269257Sdes# KMODUNLOAD	Command to unload a kernel module [/sbin/kldunload]
30269257Sdes#
31269257Sdes# MFILES	Optionally a list of interfaces used by the module.
32269257Sdes#		This file contains a default list of interfaces.
33269257Sdes#
34238106Sdes# PROG		The name of the kernel module to build.
35238106Sdes#		If not supplied, ${KMOD}.ko is used.
36238106Sdes#
37238106Sdes# SRCS		List of source files.
38238106Sdes#
39238106Sdes# FIRMWS	List of firmware images in format filename:shortname:version
40238106Sdes#
41238106Sdes# DESTDIR	The tree where the module gets installed. [not set]
42238106Sdes#
43238106Sdes# +++ targets +++
44238106Sdes#
45238106Sdes# 	install:
46238106Sdes#               install the kernel module; if the Makefile
47238106Sdes#               does not itself define the target install, the targets
48238106Sdes#               beforeinstall and afterinstall may also be used to cause
49238106Sdes#               actions immediately before and after the install target
50238106Sdes#		is executed.
51238106Sdes#
52238106Sdes# 	load:
53238106Sdes#		Load a module.
54238106Sdes#
55238106Sdes# 	unload:
56238106Sdes#		Unload a module.
57238106Sdes#
58238106Sdes
59238106SdesAWK?=		awk
60238106SdesKMODLOAD?=	/sbin/kldload
61238106SdesKMODUNLOAD?=	/sbin/kldunload
62238106SdesOBJCOPY?=	objcopy
63238106Sdes
64238106Sdes.if defined(KMODDEPS)
65238106Sdes.error "Do not use KMODDEPS on 5.0+; use MODULE_VERSION/MODULE_DEPEND"
66238106Sdes.endif
67238106Sdes
68238106Sdes.include <bsd.init.mk>
69238106Sdes
70238106Sdes.SUFFIXES: .out .o .c .cc .cxx .C .y .l .s .S
71238106Sdes
72255579Sdes.if ${CC} == "icc"
73255579SdesCFLAGS:=	${CFLAGS:C/(-x[^M^K^W]+)[MKW]+|-x[MKW]+/\1/}
74291767Sdes.else
75238106Sdes. if !empty(CFLAGS:M-O[23s]) && empty(CFLAGS:M-fno-strict-aliasing)
76238106SdesCFLAGS+=	-fno-strict-aliasing
77238106Sdes. endif
78238106SdesWERROR?=	-Werror
79238106Sdes.endif
80238106SdesCFLAGS+=	${WERROR}
81238106SdesCFLAGS+=	-D_KERNEL
82238106SdesCFLAGS+=	-DKLD_MODULE
83238106Sdes
84238106Sdes# Don't use any standard or source-relative include directories.
85238106Sdes.if ${CC} == "icc"
86238106SdesNOSTDINC=	-X
87238106Sdes.else
88238106SdesNOSTDINC=	-nostdinc
89291767Sdes.endif
90291767SdesCFLAGS:=	${CFLAGS:N-I*} ${NOSTDINC} -I- ${INCLMAGIC} ${CFLAGS:M-I*}
91238106Sdes.if defined(KERNBUILDDIR)
92238106SdesCFLAGS+=	-DHAVE_KERNEL_OPTION_HEADERS -include ${KERNBUILDDIR}/opt_global.h
93238106Sdes.endif
94238106Sdes
95238106Sdes# Add -I paths for system headers.  Individual module makefiles don't
96238106Sdes# need any -I paths for this.  Similar defaults for .PATH can't be
97238106Sdes# set because there are no standard paths for non-headers.
98238106SdesCFLAGS+=	-I. -I@
99238106Sdes
100238106Sdes# Add -I path for altq headers as they are included via net/if_var.h
101238106Sdes# for example.
102238106SdesCFLAGS+=	-I@/contrib/altq
103238106Sdes
104238106Sdes.if ${CC} != "icc"
105238106SdesCFLAGS+=	-finline-limit=${INLINE_LIMIT}
106238106SdesCFLAGS+= --param inline-unit-growth=100
107238106SdesCFLAGS+= --param large-function-growth=1000
108238106Sdes.endif
109238106Sdes
110238106Sdes# Disallow common variables, and if we end up with commons from
111238106Sdes# somewhere unexpected, allocate storage for them in the module itself.
112238106Sdes.if ${CC} != "icc"
113238106SdesCFLAGS+=	-fno-common
114238106Sdes.endif
115238106SdesLDFLAGS+=	-d -warn-common
116238106Sdes
117238106SdesCFLAGS+=	${DEBUG_FLAGS}
118238106Sdes.if ${MACHINE_ARCH} == amd64
119238106SdesCFLAGS+=	-fno-omit-frame-pointer
120238106Sdes.endif
121238106Sdes
122238106Sdes.if ${MACHINE_ARCH} == "powerpc"
123238106SdesCFLAGS+=	-mlongcall -fno-omit-frame-pointer
124238106Sdes.endif
125238106Sdes
126238106Sdes.if defined(FIRMWS)
127238106Sdes.if !exists(@)
128238106Sdes${KMOD:S/$/.c/}: @
129238106Sdes.else
130238106Sdes${KMOD:S/$/.c/}: @/tools/fw_stub.awk
131238106Sdes.endif
132238106Sdes	${AWK} -f @/tools/fw_stub.awk ${FIRMWS} -m${KMOD} -c${KMOD:S/$/.c/g}
133238106Sdes
134238106SdesSRCS+=	${KMOD:S/$/.c/}
135238106SdesCLEANFILES+=	${KMOD:S/$/.c/}
136238106Sdes
137238106Sdes.for _firmw in ${FIRMWS}
138238106Sdes${_firmw:C/\:.*$/.fwo/}:	${_firmw:C/\:.*$//}
139238106Sdes	@${ECHO} ${_firmw:C/\:.*$//} ${.ALLSRC:M*${_firmw:C/\:.*$//}}
140238106Sdes.if !exists(${.CURDIR}/${_firmw:C/\:.*$//})
141238106Sdes	ln -s ${.ALLSRC:M*${_firmw:C/\:.*$//}} ${_firmw:C/\:.*$//}
142238106Sdes	${LD} -b binary ${LDFLAGS} -r -d -o ${.TARGET} ${_firmw:C/\:.*$//}
143238106Sdes	rm -f ${_firmw:C/\:.*$//}
144238106Sdes.else
145238106Sdes	${LD} -b binary ${LDFLAGS} -r -d -o ${.TARGET} ${_firmw:C/\:.*$//}
146238106Sdes.endif
147238106Sdes
148238106SdesOBJS+=	${_firmw:C/\:.*$/.fwo/}
149238106Sdes.endfor
150238106Sdes.endif
151238106Sdes
152238106SdesOBJS+=	${SRCS:N*.h:R:S/$/.o/g}
153238106Sdes
154238106Sdes.if !defined(PROG)
155238106SdesPROG=	${KMOD}.ko
156238106Sdes.endif
157238106Sdes
158238106Sdes.if !defined(DEBUG_FLAGS)
159238106SdesFULLPROG=	${PROG}
160238106Sdes.else
161238106SdesFULLPROG=	${PROG}.debug
162238106Sdes${PROG}: ${FULLPROG} ${PROG}.symbols
163238106Sdes	${OBJCOPY} --strip-debug --add-gnu-debuglink=${PROG}.symbols\
164238106Sdes	    ${FULLPROG} ${.TARGET}
165238106Sdes${PROG}.symbols: ${FULLPROG}
166238106Sdes	${OBJCOPY} --only-keep-debug ${FULLPROG} ${.TARGET}
167238106Sdes.endif
168238106Sdes
169238106Sdes.if ${MACHINE_ARCH} != amd64
170238106Sdes${FULLPROG}: ${KMOD}.kld
171238106Sdes	${LD} -Bshareable ${LDFLAGS} -o ${.TARGET} ${KMOD}.kld
172238106Sdes.if !defined(DEBUG_FLAGS)
173238106Sdes	${OBJCOPY} --strip-debug ${.TARGET}
174238106Sdes.endif
175238106Sdes.endif
176238106Sdes
177238106SdesEXPORT_SYMS?=	NO
178238106Sdes.if ${EXPORT_SYMS} != YES
179238106SdesCLEANFILES+=	export_syms
180238106Sdes.endif
181238106Sdes
182238106Sdes.if ${MACHINE_ARCH} != amd64
183238106Sdes${KMOD}.kld: ${OBJS}
184238106Sdes.else
185238106Sdes${FULLPROG}: ${OBJS}
186269257Sdes.endif
187238106Sdes	${LD} ${LDFLAGS} -r -d -o ${.TARGET} ${OBJS}
188238106Sdes.if defined(EXPORT_SYMS)
189238106Sdes.if ${EXPORT_SYMS} != YES
190238106Sdes.if ${EXPORT_SYMS} == NO
191238106Sdes	touch export_syms
192238106Sdes.elif !exists(${.CURDIR}/${EXPORT_SYMS})
193238106Sdes	echo ${EXPORT_SYMS} > export_syms
194238106Sdes.else
195238106Sdes	grep -v '^#' < ${EXPORT_SYMS} > export_syms
196238106Sdes.endif
197238106Sdes	awk -f ${SYSDIR}/conf/kmod_syms.awk ${.TARGET} \
198238106Sdes	    export_syms | xargs -J% ${OBJCOPY} % ${.TARGET}
199238106Sdes.endif
200238106Sdes.endif
201238106Sdes.if !defined(DEBUG_FLAGS) && ${MACHINE_ARCH} == amd64
202238106Sdes	${OBJCOPY} --strip-debug ${.TARGET}
203238106Sdes.endif
204238106Sdes
205238106Sdes_ILINKS=@ machine
206238106Sdes.if ${MACHINE} != ${MACHINE_ARCH}
207238106Sdes_ILINKS+=${MACHINE_ARCH}
208238106Sdes.endif
209238106Sdes
210238106Sdesall: objwarn ${PROG}
211238106Sdes
212238106Sdesbeforedepend: ${_ILINKS}
213238106Sdes
214238106Sdes# Ensure that the links exist without depending on it when it exists which
215238106Sdes# causes all the modules to be rebuilt when the directory pointed to changes.
216238106Sdes.for _link in ${_ILINKS}
217238106Sdes.if !exists(${.OBJDIR}/${_link})
218238106Sdes${OBJS}: ${_link}
219238106Sdes.endif
220238106Sdes.endfor
221238106Sdes
222238106Sdes# Search for kernel source tree in standard places.
223238106Sdes.for _dir in ${.CURDIR}/../.. ${.CURDIR}/../../.. /sys /usr/src/sys
224238106Sdes.if !defined(SYSDIR) && exists(${_dir}/kern/)
225238106SdesSYSDIR=	${_dir}
226238106Sdes.endif
227238106Sdes.endfor
228238106Sdes.if !defined(SYSDIR) || !exists(${SYSDIR}/kern/)
229238106Sdes.error "can't find kernel source tree"
230238106Sdes.endif
231238106Sdes
232238106Sdes${_ILINKS}:
233238106Sdes	@case ${.TARGET} in \
234238106Sdes	${MACHINE_ARCH}) \
235238106Sdes		path=${SYSDIR}/${MACHINE_ARCH}/include ;; \
236238106Sdes	machine) \
237238106Sdes		path=${SYSDIR}/${MACHINE}/include ;; \
238238106Sdes	@) \
239238106Sdes		path=${SYSDIR} ;; \
240238106Sdes	esac ; \
241238106Sdes	path=`(cd $$path && /bin/pwd)` ; \
242238106Sdes	${ECHO} ${.TARGET} "->" $$path ; \
243238106Sdes	ln -s $$path ${.TARGET}
244238106Sdes
245238106SdesCLEANFILES+= ${PROG} ${KMOD}.kld ${OBJS} ${_ILINKS}
246238106Sdes
247238106Sdes.if defined(DEBUG_FLAGS)
248269257SdesCLEANFILES+= ${FULLPROG} ${PROG}.symbols
249269257Sdes.endif
250238106Sdes
251269257Sdes.if !target(install)
252238106Sdes
253238106Sdes_INSTALLFLAGS:=	${INSTALLFLAGS}
254238106Sdes.for ie in ${INSTALLFLAGS_EDIT}
255238106Sdes_INSTALLFLAGS:=	${_INSTALLFLAGS${ie}}
256238106Sdes.endfor
257238106Sdes
258238106Sdes.if !target(realinstall)
259238106Sdesrealinstall: _kmodinstall
260238106Sdes.ORDER: beforeinstall _kmodinstall
261238106Sdes_kmodinstall:
262238106Sdes	${INSTALL} -o ${KMODOWN} -g ${KMODGRP} -m ${KMODMODE} \
263238106Sdes	    ${_INSTALLFLAGS} ${PROG} ${DESTDIR}${KMODDIR}
264238106Sdes.if defined(DEBUG_FLAGS) && !defined(INSTALL_NODEBUG)
265238106Sdes	${INSTALL} -o ${KMODOWN} -g ${KMODGRP} -m ${KMODMODE} \
266238106Sdes	    ${_INSTALLFLAGS} ${PROG}.symbols ${DESTDIR}${KMODDIR}
267238106Sdes.endif
268238106Sdes
269238106Sdes.include <bsd.links.mk>
270238106Sdes
271238106Sdes.if !defined(NO_XREF)
272238106Sdesafterinstall: _kldxref
273238106Sdes.ORDER: realinstall _kldxref
274238106Sdes.ORDER: _installlinks _kldxref
275238106Sdes_kldxref:
276238106Sdes	@if type kldxref >/dev/null 2>&1; then \
277238106Sdes		${ECHO} kldxref ${DESTDIR}${KMODDIR}; \
278238106Sdes		kldxref ${DESTDIR}${KMODDIR}; \
279269257Sdes	fi
280269257Sdes.endif
281238106Sdes.endif # !target(realinstall)
282269257Sdes
283238106Sdes.endif # !target(install)
284238106Sdes
285238106Sdes.if !target(load)
286238106Sdesload: ${PROG}
287238106Sdes	${KMODLOAD} -v ${.OBJDIR}/${PROG}
288238106Sdes.endif
289238106Sdes
290238106Sdes.if !target(unload)
291238106Sdesunload:
292238106Sdes	${KMODUNLOAD} -v ${PROG}
293238106Sdes.endif
294238106Sdes
295238106Sdes.if defined(KERNBUILDDIR)
296291767Sdes.PATH: ${KERNBUILDDIR}
297291767SdesCFLAGS+=	-I${KERNBUILDDIR}
298291767Sdes.for _src in ${SRCS:Mopt_*.h}
299291767SdesCLEANFILES+=	${_src}
300291767Sdes.if !target(${_src})
301291767Sdes${_src}:
302291767Sdes	ln -s ${KERNBUILDDIR}/${_src} ${.TARGET}
303291767Sdes.endif
304291767Sdes.endfor
305291767Sdes.else
306291767Sdes.for _src in ${SRCS:Mopt_*.h}
307291767SdesCLEANFILES+=	${_src}
308291767Sdes.if !target(${_src})
309291767Sdes${_src}:
310291767Sdes	touch ${.TARGET}
311291767Sdes.endif
312291767Sdes.endfor
313291767Sdes.endif
314291767Sdes
315291767SdesMFILES?= dev/acpica/acpi_if.m dev/ata/ata_if.m dev/eisa/eisa_if.m \
316238106Sdes	dev/iicbus/iicbb_if.m dev/iicbus/iicbus_if.m \
317238106Sdes	dev/mii/miibus_if.m dev/ofw/ofw_bus_if.m \
318238106Sdes	dev/pccard/card_if.m dev/pccard/power_if.m dev/pci/pci_if.m \
319238106Sdes	dev/pci/pcib_if.m dev/ppbus/ppbus_if.m dev/smbus/smbus_if.m \
320238106Sdes	dev/sound/pcm/ac97_if.m dev/sound/pcm/channel_if.m \
321238106Sdes	dev/sound/pcm/feeder_if.m dev/sound/pcm/mixer_if.m dev/uart/uart_if.m \
322269257Sdes	dev/usb/usb_if.m isa/isa_if.m \
323238106Sdes	kern/bus_if.m kern/cpufreq_if.m kern/device_if.m \
324269257Sdes	libkern/iconv_converter_if.m opencrypto/crypto_if.m \
325238106Sdes	pc98/pc98/canbus_if.m pci/agp_if.m
326238106Sdes
327238106Sdes.for _srcsrc in ${MFILES}
328269257Sdes.for _ext in c h
329238106Sdes.for _src in ${SRCS:M${_srcsrc:T:R}.${_ext}}
330238106SdesCLEANFILES+=	${_src}
331238106Sdes.if !target(${_src})
332238106Sdes.if !exists(@)
333269257Sdes${_src}: @
334238106Sdes.else
335238106Sdes${_src}: @/tools/makeobjops.awk @/${_srcsrc}
336238106Sdes.endif
337269257Sdes	${AWK} -f @/tools/makeobjops.awk @/${_srcsrc} -${_ext}
338269257Sdes.endif
339238106Sdes.endfor # _src
340291767Sdes.endfor # _ext
341238106Sdes.endfor # _srcsrc
342269257Sdes
343238106Sdes.if !empty(SRCS:Mvnode_if.c)
344269257SdesCLEANFILES+=	vnode_if.c
345291767Sdes.if !exists(@)
346238106Sdesvnode_if.c: @
347269257Sdes.else
348238106Sdesvnode_if.c: @/tools/vnode_if.awk @/kern/vnode_if.src
349269257Sdes.endif
350291767Sdes	${AWK} -f @/tools/vnode_if.awk @/kern/vnode_if.src -c
351238106Sdes.endif
352269257Sdes
353238106Sdes.if !empty(SRCS:Mvnode_if.h)
354269257SdesCLEANFILES+=	vnode_if.h vnode_if_newproto.h vnode_if_typedef.h
355291767Sdes.if !exists(@)
356238106Sdesvnode_if.h vnode_if_newproto.h vnode_if_typedef.h: @
357269257Sdes.else
358238106Sdesvnode_if.h vnode_if_newproto.h vnode_if_typedef.h: @/tools/vnode_if.awk \
359269257Sdes    @/kern/vnode_if.src
360291767Sdes.endif
361238106Sdesvnode_if.h: vnode_if_newproto.h vnode_if_typedef.h
362269257Sdes	${AWK} -f @/tools/vnode_if.awk @/kern/vnode_if.src -h
363238106Sdesvnode_if_newproto.h:
364269257Sdes	${AWK} -f @/tools/vnode_if.awk @/kern/vnode_if.src -p
365291767Sdesvnode_if_typedef.h:
366238106Sdes	${AWK} -f @/tools/vnode_if.awk @/kern/vnode_if.src -q
367238106Sdes.endif
368238106Sdes
369238106Sdes.for _i in mii pccard
370238106Sdes.if !empty(SRCS:M${_i}devs.h)
371238106SdesCLEANFILES+=	${_i}devs.h
372238106Sdes.if !exists(@)
373238106Sdes${_i}devs.h: @
374238106Sdes.else
375238106Sdes${_i}devs.h: @/tools/${_i}devs2h.awk @/dev/${_i}/${_i}devs
376238106Sdes.endif
377238106Sdes	${AWK} -f @/tools/${_i}devs2h.awk @/dev/${_i}/${_i}devs
378238106Sdes.endif
379238106Sdes.endfor # _i
380238106Sdes
381238106Sdes.if !empty(SRCS:Musbdevs.h)
382238106SdesCLEANFILES+=	usbdevs.h
383238106Sdes.if !exists(@)
384238106Sdesusbdevs.h: @
385238106Sdes.else
386269257Sdesusbdevs.h: @/tools/usbdevs2h.awk @/dev/usb/usbdevs
387238106Sdes.endif
388238106Sdes	${AWK} -f @/tools/usbdevs2h.awk @/dev/usb/usbdevs -h
389238106Sdes.endif
390238106Sdes
391238106Sdes.if !empty(SRCS:Musbdevs_data.h)
392238106SdesCLEANFILES+=	usbdevs_data.h
393238106Sdes.if !exists(@)
394238106Sdesusbdevs_data.h: @
395238106Sdes.else
396238106Sdesusbdevs_data.h: @/tools/usbdevs2h.awk @/dev/usb/usbdevs
397238106Sdes.endif
398238106Sdes	${AWK} -f @/tools/usbdevs2h.awk @/dev/usb/usbdevs -d
399238106Sdes.endif
400238106Sdes
401238106Sdes.if !empty(SRCS:Macpi_quirks.h)
402238106SdesCLEANFILES+=	acpi_quirks.h
403238106Sdes.if !exists(@)
404238106Sdesacpi_quirks.h: @
405238106Sdes.else
406238106Sdesacpi_quirks.h: @/tools/acpi_quirks2h.awk @/dev/acpica/acpi_quirks
407238106Sdes.endif
408238106Sdes	${AWK} -f @/tools/acpi_quirks2h.awk @/dev/acpica/acpi_quirks
409238106Sdes.endif
410238106Sdes
411238106Sdes.if !empty(SRCS:Massym.s)
412238106SdesCLEANFILES+=	assym.s genassym.o
413238106Sdesassym.s: genassym.o
414238106Sdes.if !exists(@)
415238106Sdesassym.s: @
416238106Sdes.else
417238106Sdesassym.s: @/kern/genassym.sh
418238106Sdes.endif
419238106Sdes	sh @/kern/genassym.sh genassym.o > ${.TARGET}
420238106Sdes.if exists(@)
421238106Sdesgenassym.o: @/${MACHINE_ARCH}/${MACHINE_ARCH}/genassym.c
422238106Sdes.endif
423238106Sdesgenassym.o: @ machine ${SRCS:Mopt_*.h}
424238106Sdes	${CC} -c ${CFLAGS:N-fno-common} \
425238106Sdes	    @/${MACHINE_ARCH}/${MACHINE_ARCH}/genassym.c
426238106Sdes.endif
427238106Sdes
428238106Sdeslint: ${SRCS}
429238106Sdes	${LINT} ${LINTKERNFLAGS} ${CFLAGS:M-[DILU]*} ${.ALLSRC:M*.c}
430238106Sdes
431238106Sdes.include <bsd.dep.mk>
432238106Sdes
433238106Sdes.if !exists(${.OBJDIR}/${DEPENDFILE})
434238106Sdes${OBJS}: ${SRCS:M*.h}
435238106Sdes.endif
436238106Sdes
437238106Sdes.include <bsd.obj.mk>
438238106Sdes.include "kern.mk"
439238106Sdes