kmod.mk revision 204031
1#	From: @(#)bsd.prog.mk	5.26 (Berkeley) 6/25/91
2# $FreeBSD: head/sys/conf/kmod.mk 204031 2010-02-18 05:49:52Z neel $
3#
4# The include file <bsd.kmod.mk> handles building and installing loadable
5# kernel modules.
6#
7#
8# +++ variables +++
9#
10# CLEANFILES	Additional files to remove for the clean and cleandir targets.
11#
12# EXPORT_SYMS	A list of symbols that should be exported from the module,
13#		or the name of a file containing a list of symbols, or YES
14#		to export all symbols.  If not defined, no symbols are
15#		exported.
16#
17# KMOD		The name of the kernel module to build.
18#
19# KMODDIR	Base path for kernel modules (see kld(4)). [/boot/kernel]
20#
21# KMODOWN	Module file owner. [${BINOWN}]
22#
23# KMODGRP	Module file group. [${BINGRP}]
24#
25# KMODMODE	Module file mode. [${BINMODE}]
26#
27# KMODLOAD	Command to load a kernel module [/sbin/kldload]
28#
29# KMODUNLOAD	Command to unload a kernel module [/sbin/kldunload]
30#
31# MFILES	Optionally a list of interfaces used by the module.
32#		This file contains a default list of interfaces.
33#
34# PROG		The name of the kernel module to build.
35#		If not supplied, ${KMOD}.ko is used.
36#
37# SRCS		List of source files.
38#
39# FIRMWS	List of firmware images in format filename:shortname:version
40#
41# FIRMWARE_LICENSE
42#		Set to the name of the license the user has to agree on in
43#		order to use this firmware. See /usr/share/doc/legal
44#
45# DESTDIR	The tree where the module gets installed. [not set]
46#
47# +++ targets +++
48#
49# 	install:
50#               install the kernel module; if the Makefile
51#               does not itself define the target install, the targets
52#               beforeinstall and afterinstall may also be used to cause
53#               actions immediately before and after the install target
54#		is executed.
55#
56# 	load:
57#		Load a module.
58#
59# 	unload:
60#		Unload a module.
61#
62
63AWK?=		awk
64KMODLOAD?=	/sbin/kldload
65KMODUNLOAD?=	/sbin/kldunload
66OBJCOPY?=	objcopy
67
68.if defined(KMODDEPS)
69.error "Do not use KMODDEPS on 5.0+; use MODULE_VERSION/MODULE_DEPEND"
70.endif
71
72.include <bsd.init.mk>
73
74.SUFFIXES: .out .o .c .cc .cxx .C .y .l .s .S
75
76.if ${CC} == "icc"
77CFLAGS:=	${CFLAGS:C/(-x[^M^K^W]+)[MKW]+|-x[MKW]+/\1/}
78.else
79. if !empty(CFLAGS:M-O[23s]) && empty(CFLAGS:M-fno-strict-aliasing)
80CFLAGS+=	-fno-strict-aliasing
81. endif
82WERROR?=	-Werror
83.endif
84CFLAGS+=	${WERROR}
85CFLAGS+=	-D_KERNEL
86CFLAGS+=	-DKLD_MODULE
87
88# Don't use any standard or source-relative include directories.
89.if ${CC} == "icc"
90NOSTDINC=	-X
91.else
92CSTD=		c99
93NOSTDINC=	-nostdinc
94.endif
95CFLAGS:=	${CFLAGS:N-I*} ${NOSTDINC} ${INCLMAGIC} ${CFLAGS:M-I*}
96.if defined(KERNBUILDDIR)
97CFLAGS+=	-DHAVE_KERNEL_OPTION_HEADERS -include ${KERNBUILDDIR}/opt_global.h
98.endif
99
100# Add -I paths for system headers.  Individual module makefiles don't
101# need any -I paths for this.  Similar defaults for .PATH can't be
102# set because there are no standard paths for non-headers.
103CFLAGS+=	-I. -I@
104
105# Add -I path for altq headers as they are included via net/if_var.h
106# for example.
107CFLAGS+=	-I@/contrib/altq
108
109.if ${CC} != "icc"
110CFLAGS+=	-finline-limit=${INLINE_LIMIT}
111CFLAGS+= --param inline-unit-growth=100
112CFLAGS+= --param large-function-growth=1000
113.endif
114
115# Disallow common variables, and if we end up with commons from
116# somewhere unexpected, allocate storage for them in the module itself.
117.if ${CC} != "icc"
118CFLAGS+=	-fno-common
119.endif
120LDFLAGS+=	-d -warn-common
121
122CFLAGS+=	${DEBUG_FLAGS}
123.if ${MACHINE_ARCH} == amd64
124CFLAGS+=	-fno-omit-frame-pointer
125.endif
126
127.if ${MACHINE_ARCH} == "powerpc"
128CFLAGS+=	-mlongcall -fno-omit-frame-pointer
129.endif
130
131.if ${MACHINE_ARCH} == "mips"
132CFLAGS+=	-G0 -fno-pic -mno-abicalls -mlong-calls
133.endif
134
135.if defined(FIRMWS)
136.if !exists(@)
137${KMOD:S/$/.c/}: @
138.else
139${KMOD:S/$/.c/}: @/tools/fw_stub.awk
140.endif
141	${AWK} -f @/tools/fw_stub.awk ${FIRMWS} -m${KMOD} -c${KMOD:S/$/.c/g} \
142	    ${FIRMWARE_LICENSE:C/.+/-l/}${FIRMWARE_LICENSE}
143
144SRCS+=	${KMOD:S/$/.c/}
145CLEANFILES+=	${KMOD:S/$/.c/}
146
147.for _firmw in ${FIRMWS}
148${_firmw:C/\:.*$/.fwo/}:	${_firmw:C/\:.*$//}
149	@${ECHO} ${_firmw:C/\:.*$//} ${.ALLSRC:M*${_firmw:C/\:.*$//}}
150	@if [ -e ${_firmw:C/\:.*$//} ]; then			\
151		${LD} -b binary --no-warn-mismatch ${LDFLAGS}	\
152		    -r -d -o ${.TARGET}	${_firmw:C/\:.*$//};	\
153	else							\
154		ln -s ${.ALLSRC:M*${_firmw:C/\:.*$//}} ${_firmw:C/\:.*$//}; \
155		${LD} -b binary --no-warn-mismatch ${LDFLAGS}	\
156		    -r -d -o ${.TARGET}	${_firmw:C/\:.*$//};	\
157		rm ${_firmw:C/\:.*$//};				\
158	fi
159
160OBJS+=	${_firmw:C/\:.*$/.fwo/}
161.endfor
162.endif
163
164OBJS+=	${SRCS:N*.h:R:S/$/.o/g}
165
166.if !defined(PROG)
167PROG=	${KMOD}.ko
168.endif
169
170.if !defined(DEBUG_FLAGS)
171FULLPROG=	${PROG}
172.else
173FULLPROG=	${PROG}.debug
174${PROG}: ${FULLPROG} ${PROG}.symbols
175	${OBJCOPY} --strip-debug --add-gnu-debuglink=${PROG}.symbols\
176	    ${FULLPROG} ${.TARGET}
177${PROG}.symbols: ${FULLPROG}
178	${OBJCOPY} --only-keep-debug ${FULLPROG} ${.TARGET}
179.endif
180
181.if ${MACHINE_ARCH} != amd64 && ${MACHINE_ARCH} != mips
182${FULLPROG}: ${KMOD}.kld
183	${LD} -Bshareable ${LDFLAGS} -o ${.TARGET} ${KMOD}.kld
184.if !defined(DEBUG_FLAGS)
185	${OBJCOPY} --strip-debug ${.TARGET}
186.endif
187.endif
188
189EXPORT_SYMS?=	NO
190.if ${EXPORT_SYMS} != YES
191CLEANFILES+=	export_syms
192.endif
193
194.if ${MACHINE_ARCH} != amd64 && ${MACHINE_ARCH} != mips
195${KMOD}.kld: ${OBJS}
196.else
197${FULLPROG}: ${OBJS}
198.endif
199	${LD} ${LDFLAGS} -r -d -o ${.TARGET} ${OBJS}
200.if defined(EXPORT_SYMS)
201.if ${EXPORT_SYMS} != YES
202.if ${EXPORT_SYMS} == NO
203	:> export_syms
204.elif !exists(${.CURDIR}/${EXPORT_SYMS})
205	echo ${EXPORT_SYMS} > export_syms
206.else
207	grep -v '^#' < ${EXPORT_SYMS} > export_syms
208.endif
209	awk -f ${SYSDIR}/conf/kmod_syms.awk ${.TARGET} \
210	    export_syms | xargs -J% ${OBJCOPY} % ${.TARGET}
211.endif
212.endif
213.if !defined(DEBUG_FLAGS) && \
214    (${MACHINE_ARCH} == amd64 || ${MACHINE_ARCH} == mips)
215	${OBJCOPY} --strip-debug ${.TARGET}
216.endif
217
218_ILINKS=@ machine
219.if ${MACHINE} != ${MACHINE_ARCH}
220_ILINKS+=${MACHINE_ARCH}
221.endif
222
223all: objwarn ${PROG}
224
225beforedepend: ${_ILINKS}
226
227# Ensure that the links exist without depending on it when it exists which
228# causes all the modules to be rebuilt when the directory pointed to changes.
229.for _link in ${_ILINKS}
230.if !exists(${.OBJDIR}/${_link})
231${OBJS}: ${_link}
232.endif
233.endfor
234
235# Search for kernel source tree in standard places.
236.for _dir in ${.CURDIR}/../.. ${.CURDIR}/../../.. /sys /usr/src/sys
237.if !defined(SYSDIR) && exists(${_dir}/kern/)
238SYSDIR=	${_dir}
239.endif
240.endfor
241.if !defined(SYSDIR) || !exists(${SYSDIR}/kern/)
242.error "can't find kernel source tree"
243.endif
244
245${_ILINKS}:
246	@case ${.TARGET} in \
247	${MACHINE_ARCH}) \
248		path=${SYSDIR}/${MACHINE_ARCH}/include ;; \
249	machine) \
250		path=${SYSDIR}/${MACHINE}/include ;; \
251	@) \
252		path=${SYSDIR} ;; \
253	esac ; \
254	path=`(cd $$path && /bin/pwd)` ; \
255	${ECHO} ${.TARGET} "->" $$path ; \
256	ln -sf $$path ${.TARGET}
257
258CLEANFILES+= ${PROG} ${KMOD}.kld ${OBJS}
259
260.if defined(DEBUG_FLAGS)
261CLEANFILES+= ${FULLPROG} ${PROG}.symbols
262.endif
263
264.if !target(install)
265
266_INSTALLFLAGS:=	${INSTALLFLAGS}
267.for ie in ${INSTALLFLAGS_EDIT}
268_INSTALLFLAGS:=	${_INSTALLFLAGS${ie}}
269.endfor
270
271.if !target(realinstall)
272realinstall: _kmodinstall
273.ORDER: beforeinstall _kmodinstall
274_kmodinstall:
275	${INSTALL} -o ${KMODOWN} -g ${KMODGRP} -m ${KMODMODE} \
276	    ${_INSTALLFLAGS} ${PROG} ${DESTDIR}${KMODDIR}
277.if defined(DEBUG_FLAGS) && !defined(INSTALL_NODEBUG)
278	${INSTALL} -o ${KMODOWN} -g ${KMODGRP} -m ${KMODMODE} \
279	    ${_INSTALLFLAGS} ${PROG}.symbols ${DESTDIR}${KMODDIR}
280.endif
281
282.include <bsd.links.mk>
283
284.if !defined(NO_XREF)
285afterinstall: _kldxref
286.ORDER: realinstall _kldxref
287.ORDER: _installlinks _kldxref
288_kldxref:
289	@if type kldxref >/dev/null 2>&1; then \
290		${ECHO} kldxref ${DESTDIR}${KMODDIR}; \
291		kldxref ${DESTDIR}${KMODDIR}; \
292	fi
293.endif
294.endif # !target(realinstall)
295
296.endif # !target(install)
297
298.if !target(load)
299load: ${PROG}
300	${KMODLOAD} -v ${.OBJDIR}/${PROG}
301.endif
302
303.if !target(unload)
304unload:
305	${KMODUNLOAD} -v ${PROG}
306.endif
307
308.if defined(KERNBUILDDIR)
309.PATH: ${KERNBUILDDIR}
310CFLAGS+=	-I${KERNBUILDDIR}
311.for _src in ${SRCS:Mopt_*.h}
312CLEANFILES+=	${_src}
313.if !target(${_src})
314${_src}:
315	ln -sf ${KERNBUILDDIR}/${_src} ${.TARGET}
316.endif
317.endfor
318.else
319.for _src in ${SRCS:Mopt_*.h}
320CLEANFILES+=	${_src}
321.if !target(${_src})
322${_src}:
323	:> ${.TARGET}
324.endif
325.endfor
326.endif
327
328MFILES?= dev/acpica/acpi_if.m dev/acpi_support/acpi_wmi_if.m \
329	dev/agp/agp_if.m dev/ata/ata_if.m dev/eisa/eisa_if.m \
330	dev/iicbus/iicbb_if.m dev/iicbus/iicbus_if.m \
331	dev/mmc/mmcbr_if.m dev/mmc/mmcbus_if.m \
332	dev/mii/miibus_if.m dev/ofw/ofw_bus_if.m \
333	dev/pccard/card_if.m dev/pccard/power_if.m dev/pci/pci_if.m \
334	dev/pci/pcib_if.m dev/ppbus/ppbus_if.m dev/smbus/smbus_if.m \
335	dev/sound/pcm/ac97_if.m dev/sound/pcm/channel_if.m \
336	dev/sound/pcm/feeder_if.m dev/sound/pcm/mixer_if.m \
337	dev/sound/midi/mpu_if.m dev/sound/midi/mpufoi_if.m \
338	dev/sound/midi/synth_if.m dev/usb/usb_if.m isa/isa_if.m \
339	kern/bus_if.m kern/clock_if.m \
340	kern/cpufreq_if.m kern/device_if.m kern/serdev_if.m \
341	libkern/iconv_converter_if.m opencrypto/cryptodev_if.m \
342	pc98/pc98/canbus_if.m
343
344.for _srcsrc in ${MFILES}
345.for _ext in c h
346.for _src in ${SRCS:M${_srcsrc:T:R}.${_ext}}
347CLEANFILES+=	${_src}
348.if !target(${_src})
349.if !exists(@)
350${_src}: @
351.else
352${_src}: @/tools/makeobjops.awk @/${_srcsrc}
353.endif
354	${AWK} -f @/tools/makeobjops.awk @/${_srcsrc} -${_ext}
355.endif
356.endfor # _src
357.endfor # _ext
358.endfor # _srcsrc
359
360.if !empty(SRCS:Mvnode_if.c)
361CLEANFILES+=	vnode_if.c
362.if !exists(@)
363vnode_if.c: @
364.else
365vnode_if.c: @/tools/vnode_if.awk @/kern/vnode_if.src
366.endif
367	${AWK} -f @/tools/vnode_if.awk @/kern/vnode_if.src -c
368.endif
369
370.if !empty(SRCS:Mvnode_if.h)
371CLEANFILES+=	vnode_if.h vnode_if_newproto.h vnode_if_typedef.h
372.if !exists(@)
373vnode_if.h vnode_if_newproto.h vnode_if_typedef.h: @
374.else
375vnode_if.h vnode_if_newproto.h vnode_if_typedef.h: @/tools/vnode_if.awk \
376    @/kern/vnode_if.src
377.endif
378vnode_if.h: vnode_if_newproto.h vnode_if_typedef.h
379	${AWK} -f @/tools/vnode_if.awk @/kern/vnode_if.src -h
380vnode_if_newproto.h:
381	${AWK} -f @/tools/vnode_if.awk @/kern/vnode_if.src -p
382vnode_if_typedef.h:
383	${AWK} -f @/tools/vnode_if.awk @/kern/vnode_if.src -q
384.endif
385
386.for _i in mii pccard
387.if !empty(SRCS:M${_i}devs.h)
388CLEANFILES+=	${_i}devs.h
389.if !exists(@)
390${_i}devs.h: @
391.else
392${_i}devs.h: @/tools/${_i}devs2h.awk @/dev/${_i}/${_i}devs
393.endif
394	${AWK} -f @/tools/${_i}devs2h.awk @/dev/${_i}/${_i}devs
395.endif
396.endfor # _i
397
398.if !empty(SRCS:Musbdevs.h)
399CLEANFILES+=	usbdevs.h
400.if !exists(@)
401usbdevs.h: @
402.else
403usbdevs.h: @/tools/usbdevs2h.awk @/dev/usb/usbdevs
404.endif
405	${AWK} -f @/tools/usbdevs2h.awk @/dev/usb/usbdevs -h
406.endif
407
408.if !empty(SRCS:Musbdevs_data.h)
409CLEANFILES+=	usbdevs_data.h
410.if !exists(@)
411usbdevs_data.h: @
412.else
413usbdevs_data.h: @/tools/usbdevs2h.awk @/dev/usb/usbdevs
414.endif
415	${AWK} -f @/tools/usbdevs2h.awk @/dev/usb/usbdevs -d
416.endif
417
418.if !empty(SRCS:Macpi_quirks.h)
419CLEANFILES+=	acpi_quirks.h
420.if !exists(@)
421acpi_quirks.h: @
422.else
423acpi_quirks.h: @/tools/acpi_quirks2h.awk @/dev/acpica/acpi_quirks
424.endif
425	${AWK} -f @/tools/acpi_quirks2h.awk @/dev/acpica/acpi_quirks
426.endif
427
428.if !empty(SRCS:Massym.s)
429CLEANFILES+=	assym.s genassym.o
430assym.s: genassym.o
431.if defined(KERNBUILDDIR)
432genassym.o: opt_global.h
433.endif
434.if !exists(@)
435assym.s: @
436.else
437assym.s: @/kern/genassym.sh
438.endif
439	sh @/kern/genassym.sh genassym.o > ${.TARGET}
440.if exists(@)
441genassym.o: @/${MACHINE_ARCH}/${MACHINE_ARCH}/genassym.c
442.endif
443genassym.o: @ machine ${SRCS:Mopt_*.h}
444	${CC} -c ${CFLAGS:N-fno-common} \
445	    @/${MACHINE_ARCH}/${MACHINE_ARCH}/genassym.c
446.endif
447
448lint: ${SRCS}
449	${LINT} ${LINTKERNFLAGS} ${CFLAGS:M-[DILU]*} ${.ALLSRC:M*.c}
450
451.if defined(KERNBUILDDIR)
452${OBJS}: opt_global.h
453.endif
454
455.include <bsd.dep.mk>
456
457cleandepend: cleanilinks
458# .depend needs include links so we remove them only together.
459cleanilinks:
460	rm -f ${_ILINKS}
461
462.if !exists(${.OBJDIR}/${DEPENDFILE})
463${OBJS}: ${SRCS:M*.h}
464.endif
465
466.include <bsd.obj.mk>
467.include "kern.mk"
468