kmod.mk revision 151646
1178525Sjb#	From: @(#)bsd.prog.mk	5.26 (Berkeley) 6/25/91
2178525Sjb# $FreeBSD: head/sys/conf/kmod.mk 151646 2005-10-25 09:05:07Z ru $
3178525Sjb#
4178525Sjb# The include file <bsd.kmod.mk> handles building and installing loadable
5178525Sjb# kernel modules.
6178525Sjb#
7178525Sjb#
8178525Sjb# +++ variables +++
9178525Sjb#
10178525Sjb# CLEANFILES	Additional files to remove for the clean and cleandir targets.
11178525Sjb#
12178525Sjb# EXPORT_SYMS	A list of symbols that should be exported from the module,
13178525Sjb#		or the name of a file containing a list of symbols, or YES
14178525Sjb#		to export all symbols.  If not defined, no symbols are
15178525Sjb#		exported.
16178525Sjb#
17178525Sjb# KMOD		The name of the kernel module to build.
18178525Sjb#
19178525Sjb# KMODDIR	Base path for kernel modules (see kld(4)). [/boot/kernel]
20178525Sjb#
21178525Sjb# KMODOWN	Module file owner. [${BINOWN}]
22178525Sjb#
23178525Sjb# KMODGRP	Module file group. [${BINGRP}]
24178525Sjb#
25178525Sjb# KMODMODE	Module file mode. [${BINMODE}]
26178525Sjb#
27178525Sjb# KMODLOAD	Command to load a kernel module [/sbin/kldload]
28178525Sjb#
29178525Sjb# KMODUNLOAD	Command to unload a kernel module [/sbin/kldunload]
30178525Sjb#
31178525Sjb# MFILES	Optionally a list of interfaces used by the module.
32178525Sjb#		This file contains a default list of interfaces.
33178525Sjb#
34178525Sjb# PROG		The name of the kernel module to build.
35178525Sjb#		If not supplied, ${KMOD}.ko is used.
36178525Sjb#
37178525Sjb# SRCS		List of source files.
38178525Sjb#
39178525Sjb# DESTDIR	The tree where the module gets installed. [not set]
40178525Sjb#
41178525Sjb# +++ targets +++
42178525Sjb#
43178539Sjb# 	install:
44178539Sjb#               install the kernel module; if the Makefile
45178539Sjb#               does not itself define the target install, the targets
46178525Sjb#               beforeinstall and afterinstall may also be used to cause
47178525Sjb#               actions immediately before and after the install target
48178525Sjb#		is executed.
49178525Sjb#
50178525Sjb# 	load:
51178525Sjb#		Load a module.
52178525Sjb#
53178525Sjb# 	unload:
54178525Sjb#		Unload a module.
55178525Sjb#
56178525Sjb
57178539SjbAWK?=		awk
58178539SjbKMODLOAD?=	/sbin/kldload
59178539SjbKMODUNLOAD?=	/sbin/kldunload
60178525SjbOBJCOPY?=	objcopy
61178539Sjb
62178525Sjb.if defined(KMODDEPS)
63178525Sjb.error "Do not use KMODDEPS on 5.0+; use MODULE_VERSION/MODULE_DEPEND"
64178525Sjb.endif
65178525Sjb
66178525Sjb.include <bsd.init.mk>
67178525Sjb
68178539Sjb.SUFFIXES: .out .o .c .cc .cxx .C .y .l .s .S
69178539Sjb
70178539Sjb.if ${CC} == "icc"
71178525SjbCFLAGS:=	${CFLAGS:C/(-x[^M^K^W]+)[MKW]+|-x[MKW]+/\1/}
72178525Sjb.else
73178525Sjb. if !empty(CFLAGS:M-O[23s]) && empty(CFLAGS:M-fno-strict-aliasing)
74178525SjbCFLAGS+=	-fno-strict-aliasing
75178525Sjb. endif
76178525SjbWERROR?=	-Werror
77178525Sjb.endif
78178525SjbCFLAGS+=	${WERROR}
79178525SjbCFLAGS+=	-D_KERNEL
80178525SjbCFLAGS+=	-DKLD_MODULE
81178525Sjb
82178539Sjb# Don't use any standard or source-relative include directories.
83178539Sjb.if ${CC} == "icc"
84178539SjbNOSTDINC=	-X
85178525Sjb.else
86178539SjbNOSTDINC=	-nostdinc
87178525Sjb.endif
88CFLAGS:=	${CFLAGS:N-I*} ${NOSTDINC} -I- ${INCLMAGIC} ${CFLAGS:M-I*}
89.if defined(KERNBUILDDIR)
90CFLAGS+=	-DHAVE_KERNEL_OPTION_HEADERS -include ${KERNBUILDDIR}/opt_global.h
91.endif
92
93# Add -I paths for system headers.  Individual module makefiles don't
94# need any -I paths for this.  Similar defaults for .PATH can't be
95# set because there are no standard paths for non-headers.
96CFLAGS+=	-I. -I@
97
98# Add -I path for altq headers as they are included via net/if_var.h
99# for example.
100CFLAGS+=	-I@/contrib/altq
101
102# Add a -I path to standard headers like <stddef.h>.  Use a relative
103# path to src/include if possible.  If the @ symlink hasn't been built
104# yet, then we can't tell if the relative path exists.  Add both the
105# potential relative path and an absolute path in that case.
106.if exists(@)
107.if exists(@/../include)
108CFLAGS+=	-I@/../include
109.else
110CFLAGS+=	-I${DESTDIR}/usr/include
111.endif
112.else # !@
113CFLAGS+=	-I@/../include -I${DESTDIR}/usr/include
114.endif # @
115
116.if ${CC} != "icc"
117CFLAGS+=	-finline-limit=${INLINE_LIMIT}
118.endif
119
120# Disallow common variables, and if we end up with commons from
121# somewhere unexpected, allocate storage for them in the module itself.
122.if ${CC} != "icc"
123CFLAGS+=	-fno-common
124.endif
125LDFLAGS+=	-d -warn-common
126
127CFLAGS+=	${DEBUG_FLAGS}
128.if ${MACHINE_ARCH} == amd64
129CFLAGS+=	-fno-omit-frame-pointer
130.endif
131
132.if ${MACHINE_ARCH} == "powerpc"
133CFLAGS+=	-mlongcall -fno-omit-frame-pointer
134.endif
135
136OBJS+=	${SRCS:N*.h:R:S/$/.o/g}
137
138.if !defined(PROG)
139PROG=	${KMOD}.ko
140.endif
141
142.if !defined(DEBUG_FLAGS)
143FULLPROG=	${PROG}
144.else
145FULLPROG=	${PROG}.debug
146${PROG}: ${FULLPROG}
147	${OBJCOPY} --strip-debug ${FULLPROG} ${PROG}
148.endif
149
150.if ${MACHINE_ARCH} != amd64
151${FULLPROG}: ${KMOD}.kld
152	${LD} -Bshareable ${LDFLAGS} -o ${.TARGET} ${KMOD}.kld
153.if !defined(DEBUG_FLAGS)
154	${OBJCOPY} --strip-debug ${.TARGET}
155.endif
156.endif
157
158EXPORT_SYMS?=	NO
159.if ${EXPORT_SYMS} != YES
160CLEANFILES+=	export_syms
161.endif
162
163.if ${MACHINE_ARCH} != amd64
164${KMOD}.kld: ${OBJS}
165.else
166${FULLPROG}: ${OBJS}
167.endif
168	${LD} ${LDFLAGS} -r -d -o ${.TARGET} ${OBJS}
169.if defined(EXPORT_SYMS)
170.if ${EXPORT_SYMS} != YES
171.if ${EXPORT_SYMS} == NO
172	touch export_syms
173.elif !exists(${.CURDIR}/${EXPORT_SYMS})
174	echo ${EXPORT_SYMS} > export_syms
175.else
176	grep -v '^#' < ${EXPORT_SYMS} > export_syms
177.endif
178	awk -f ${SYSDIR}/conf/kmod_syms.awk ${.TARGET} \
179	    export_syms | xargs -J% ${OBJCOPY} % ${.TARGET}
180.endif
181.endif
182.if !defined(DEBUG_FLAGS) && ${MACHINE_ARCH} == amd64
183	${OBJCOPY} --strip-debug ${.TARGET}
184.endif
185
186_ILINKS=@ machine
187.if ${MACHINE} != ${MACHINE_ARCH}
188_ILINKS+=${MACHINE_ARCH}
189.endif
190
191all: objwarn ${PROG}
192
193beforedepend: ${_ILINKS}
194
195# Ensure that the links exist without depending on it when it exists which
196# causes all the modules to be rebuilt when the directory pointed to changes.
197.for _link in ${_ILINKS}
198.if !exists(${.OBJDIR}/${_link})
199${OBJS}: ${_link}
200.endif
201.endfor
202
203# Search for kernel source tree in standard places.
204.for _dir in ${.CURDIR}/../.. ${.CURDIR}/../../.. /sys /usr/src/sys
205.if !defined(SYSDIR) && exists(${_dir}/kern/)
206SYSDIR=	${_dir}
207.endif
208.endfor
209.if !defined(SYSDIR) || !exists(${SYSDIR}/kern/)
210.error "can't find kernel source tree"
211.endif
212
213${_ILINKS}:
214	@case ${.TARGET} in \
215	${MACHINE_ARCH}) \
216		path=${SYSDIR}/${MACHINE_ARCH}/include ;; \
217	machine) \
218		path=${SYSDIR}/${MACHINE}/include ;; \
219	@) \
220		path=${SYSDIR} ;; \
221	esac ; \
222	path=`(cd $$path && /bin/pwd)` ; \
223	${ECHO} ${.TARGET} "->" $$path ; \
224	ln -s $$path ${.TARGET}
225
226CLEANFILES+= ${PROG} ${KMOD}.kld ${OBJS} ${_ILINKS}
227
228.if defined(DEBUG_FLAGS)
229CLEANFILES+= ${FULLPROG}
230.endif
231
232.if !target(install)
233
234_INSTALLFLAGS:=	${INSTALLFLAGS}
235.for ie in ${INSTALLFLAGS_EDIT}
236_INSTALLFLAGS:=	${_INSTALLFLAGS${ie}}
237.endfor
238
239.if !target(realinstall)
240realinstall: _kmodinstall
241.ORDER: beforeinstall _kmodinstall
242_kmodinstall:
243.if defined(DEBUG_FLAGS) && !defined(INSTALL_NODEBUG)
244	${INSTALL} -o ${KMODOWN} -g ${KMODGRP} -m ${KMODMODE} \
245	    ${_INSTALLFLAGS} ${FULLPROG} ${DESTDIR}${KMODDIR}/${PROG}
246.else
247	${INSTALL} -o ${KMODOWN} -g ${KMODGRP} -m ${KMODMODE} \
248	    ${_INSTALLFLAGS} ${PROG} ${DESTDIR}${KMODDIR}
249.endif
250
251.include <bsd.links.mk>
252
253.if !defined(NO_XREF)
254afterinstall: _kldxref
255.ORDER: realinstall _kldxref
256.ORDER: _installlinks _kldxref
257_kldxref:
258	@if type kldxref >/dev/null 2>&1; then \
259		${ECHO} kldxref ${DESTDIR}${KMODDIR}; \
260		kldxref ${DESTDIR}${KMODDIR}; \
261	fi
262.endif
263.endif # !target(realinstall)
264
265.endif # !target(install)
266
267.if !target(load)
268load: ${PROG}
269	${KMODLOAD} -v ${.OBJDIR}/${PROG}
270.endif
271
272.if !target(unload)
273unload:
274	${KMODUNLOAD} -v ${PROG}
275.endif
276
277.if defined(KERNBUILDDIR)
278.PATH: ${KERNBUILDDIR}
279CFLAGS+=	-I${KERNBUILDDIR}
280.for _src in ${SRCS:Mopt_*.h}
281CLEANFILES+=	${_src}
282.if !target(${_src})
283${_src}:
284	ln -s ${KERNBUILDDIR}/${_src} ${.TARGET}
285.endif
286.endfor
287.else
288.for _src in ${SRCS:Mopt_*.h}
289CLEANFILES+=	${_src}
290.if !target(${_src})
291${_src}:
292	touch ${.TARGET}
293.endif
294.endfor
295.endif
296
297MFILES?= dev/acpica/acpi_if.m dev/ata/ata_if.m dev/eisa/eisa_if.m \
298	dev/iicbus/iicbb_if.m dev/iicbus/iicbus_if.m \
299	dev/mii/miibus_if.m dev/ofw/ofw_bus_if.m \
300	dev/pccard/card_if.m dev/pccard/power_if.m dev/pci/pci_if.m \
301	dev/pci/pcib_if.m dev/ppbus/ppbus_if.m dev/smbus/smbus_if.m \
302	dev/sound/pcm/ac97_if.m dev/sound/pcm/channel_if.m \
303	dev/sound/pcm/feeder_if.m dev/sound/pcm/mixer_if.m dev/uart/uart_if.m \
304	dev/usb/usb_if.m isa/isa_if.m \
305	kern/bus_if.m kern/cpufreq_if.m kern/device_if.m \
306	libkern/iconv_converter_if.m opencrypto/crypto_if.m \
307	pc98/pc98/canbus_if.m pci/agp_if.m
308
309.for _srcsrc in ${MFILES}
310.for _ext in c h
311.for _src in ${SRCS:M${_srcsrc:T:R}.${_ext}}
312CLEANFILES+=	${_src}
313.if !target(${_src})
314.if !exists(@)
315${_src}: @
316.else
317${_src}: @/tools/makeobjops.awk @/${_srcsrc}
318.endif
319	${AWK} -f @/tools/makeobjops.awk @/${_srcsrc} -${_ext}
320.endif
321.endfor # _src
322.endfor # _ext
323.endfor # _srcsrc
324
325.if !empty(SRCS:Mvnode_if.c)
326CLEANFILES+=	vnode_if.c
327.if !exists(@)
328vnode_if.c: @
329.else
330vnode_if.c: @/tools/vnode_if.awk @/kern/vnode_if.src
331.endif
332	${AWK} -f @/tools/vnode_if.awk @/kern/vnode_if.src -c
333.endif
334
335.if !empty(SRCS:Mvnode_if.h)
336CLEANFILES+=	vnode_if.h vnode_if_newproto.h vnode_if_typedef.h
337.if !exists(@)
338vnode_if.h vnode_if_newproto.h vnode_if_typedef.h: @
339.else
340vnode_if.h vnode_if_newproto.h vnode_if_typedef.h: @/tools/vnode_if.awk \
341    @/kern/vnode_if.src
342.endif
343vnode_if.h: vnode_if_newproto.h vnode_if_typedef.h
344	${AWK} -f @/tools/vnode_if.awk @/kern/vnode_if.src -h
345vnode_if_newproto.h:
346	${AWK} -f @/tools/vnode_if.awk @/kern/vnode_if.src -p
347vnode_if_typedef.h:
348	${AWK} -f @/tools/vnode_if.awk @/kern/vnode_if.src -q
349.endif
350
351.for _i in mii pccard
352.if !empty(SRCS:M${_i}devs.h)
353CLEANFILES+=	${_i}devs.h
354.if !exists(@)
355${_i}devs.h: @
356.else
357${_i}devs.h: @/tools/${_i}devs2h.awk @/dev/${_i}/${_i}devs
358.endif
359	${AWK} -f @/tools/${_i}devs2h.awk @/dev/${_i}/${_i}devs
360.endif
361.endfor # _i
362
363.if !empty(SRCS:Musbdevs.h)
364CLEANFILES+=	usbdevs.h
365.if !exists(@)
366usbdevs.h: @
367.else
368usbdevs.h: @/tools/usbdevs2h.awk @/dev/usb/usbdevs
369.endif
370	${AWK} -f @/tools/usbdevs2h.awk @/dev/usb/usbdevs -h
371.endif
372
373.if !empty(SRCS:Musbdevs_data.h)
374CLEANFILES+=	usbdevs_data.h
375.if !exists(@)
376usbdevs_data.h: @
377.else
378usbdevs_data.h: @/tools/usbdevs2h.awk @/dev/usb/usbdevs
379.endif
380	${AWK} -f @/tools/usbdevs2h.awk @/dev/usb/usbdevs -d
381.endif
382
383.if !empty(SRCS:Macpi_quirks.h)
384CLEANFILES+=	acpi_quirks.h
385.if !exists(@)
386acpi_quirks.h: @
387.else
388acpi_quirks.h: @/tools/acpi_quirks2h.awk @/dev/acpica/acpi_quirks
389.endif
390	${AWK} -f @/tools/acpi_quirks2h.awk @/dev/acpica/acpi_quirks
391.endif
392
393.if !empty(SRCS:Massym.s)
394CLEANFILES+=	assym.s genassym.o
395assym.s: genassym.o
396.if !exists(@)
397assym.s: @
398.else
399assym.s: @/kern/genassym.sh
400.endif
401	sh @/kern/genassym.sh genassym.o > ${.TARGET}
402.if exists(@)
403genassym.o: @/${MACHINE_ARCH}/${MACHINE_ARCH}/genassym.c
404.endif
405genassym.o: @ machine ${SRCS:Mopt_*.h}
406	${CC} -c ${CFLAGS:N-fno-common} \
407	    @/${MACHINE_ARCH}/${MACHINE_ARCH}/genassym.c
408.endif
409
410lint: ${SRCS}
411	${LINT} ${LINTKERNFLAGS} ${CFLAGS:M-[DILU]*} ${.ALLSRC:M*.c}
412
413.include <bsd.dep.mk>
414
415.if !exists(${.OBJDIR}/${DEPENDFILE})
416${OBJS}: ${SRCS:M*.h}
417.endif
418
419.include <bsd.obj.mk>
420.include "kern.mk"
421