kmod.mk revision 145248
1#	From: @(#)bsd.prog.mk	5.26 (Berkeley) 6/25/91
2# $FreeBSD: head/sys/conf/kmod.mk 145248 2005-04-18 21:10:38Z obrien $
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# DESTDIR	The tree where the module gets installed. [not set]
40#
41# +++ targets +++
42#
43# 	install:
44#               install the kernel module; if the Makefile
45#               does not itself define the target install, the targets
46#               beforeinstall and afterinstall may also be used to cause
47#               actions immediately before and after the install target
48#		is executed.
49#
50# 	load:
51#		Load a module.
52#
53# 	unload:
54#		Unload a module.
55#
56
57AWK?=		awk
58KMODLOAD?=	/sbin/kldload
59KMODUNLOAD?=	/sbin/kldunload
60OBJCOPY?=	objcopy
61
62.if defined(KMODDEPS)
63.error "Do not use KMODDEPS on 5.0+; use MODULE_VERSION/MODULE_DEPEND"
64.endif
65
66.include <bsd.init.mk>
67
68.SUFFIXES: .out .o .c .cc .cxx .C .y .l .s .S
69
70.if ${CC} == "icc"
71CFLAGS:=	${CFLAGS:C/(-x[^M^K^W]+)[MKW]+|-x[MKW]+/\1/}
72.else
73. if !empty(CFLAGS:M-O[23s]) && empty(CFLAGS:M-fno-strict-aliasing)
74CFLAGS+=	-fno-strict-aliasing
75. endif
76WERROR?=	-Werror
77.endif
78CFLAGS+=	${WERROR}
79CFLAGS+=	-D_KERNEL
80CFLAGS+=	-DKLD_MODULE
81
82# Don't use any standard or source-relative include directories.
83# Since -nostdinc will annull any previous -I paths, we repeat all
84# such paths after -nostdinc.  It doesn't seem to be possible to
85# add to the front of `make' variable.
86_ICFLAGS:=	${CFLAGS:M-I*}
87.if ${CC} == "icc"
88NOSTDINC=	-X
89.else
90NOSTDINC=	-nostdinc
91.endif
92CFLAGS+=	${NOSTDINC} -I- ${INCLMAGIC} ${_ICFLAGS}
93.if defined(KERNBUILDDIR)
94CFLAGS+=	-include ${KERNBUILDDIR}/opt_global.h
95.endif
96
97# Add -I paths for system headers.  Individual module makefiles don't
98# need any -I paths for this.  Similar defaults for .PATH can't be
99# set because there are no standard paths for non-headers.
100CFLAGS+=	-I. -I@
101
102# Add -I path for altq headers as they are included via net/if_var.h
103# for example.
104CFLAGS+=	-I@/contrib/altq
105
106# Add a -I path to standard headers like <stddef.h>.  Use a relative
107# path to src/include if possible.  If the @ symlink hasn't been built
108# yet, then we can't tell if the relative path exists.  Add both the
109# potential relative path and an absolute path in that case.
110.if exists(@)
111.if exists(@/../include)
112CFLAGS+=	-I@/../include
113.else
114CFLAGS+=	-I${DESTDIR}/usr/include
115.endif
116.else # !@
117CFLAGS+=	-I@/../include -I${DESTDIR}/usr/include
118.endif # @
119
120.if ${CC} != "icc"
121CFLAGS+=	-finline-limit=${INLINE_LIMIT}
122.endif
123
124# Disallow common variables, and if we end up with commons from
125# somewhere unexpected, allocate storage for them in the module itself.
126.if ${CC} != "icc"
127CFLAGS+=	-fno-common
128.endif
129LDFLAGS+=	-d -warn-common
130
131CFLAGS+=	${DEBUG_FLAGS}
132.if ${MACHINE_ARCH} == amd64
133CFLAGS+=	-fno-omit-frame-pointer
134.endif
135
136.if ${MACHINE_ARCH} == "powerpc"
137CFLAGS+=	-mlongcall -fno-omit-frame-pointer
138.endif
139
140OBJS+=	${SRCS:N*.h:R:S/$/.o/g}
141
142.if !defined(PROG)
143PROG=	${KMOD}.ko
144.endif
145
146.if !defined(DEBUG_FLAGS)
147FULLPROG=	${PROG}
148.else
149FULLPROG=	${PROG}.debug
150${PROG}: ${FULLPROG}
151	${OBJCOPY} --strip-debug ${FULLPROG} ${PROG}
152.endif
153
154.if ${MACHINE_ARCH} != amd64
155${FULLPROG}: ${KMOD}.kld
156	${LD} -Bshareable ${LDFLAGS} -o ${.TARGET} ${KMOD}.kld
157.if !defined(DEBUG_FLAGS)
158	${OBJCOPY} --strip-debug ${.TARGET}
159.endif
160.endif
161
162EXPORT_SYMS?=	NO
163.if ${EXPORT_SYMS} != YES
164CLEANFILES+=	export_syms
165.endif
166
167.if ${MACHINE_ARCH} != amd64
168${KMOD}.kld: ${OBJS}
169.else
170${FULLPROG}: ${OBJS}
171.endif
172	${LD} ${LDFLAGS} -r -d -o ${.TARGET} ${OBJS}
173.if defined(EXPORT_SYMS)
174.if ${EXPORT_SYMS} != YES
175.if ${EXPORT_SYMS} == NO
176	touch export_syms
177.elif !exists(${.CURDIR}/${EXPORT_SYMS})
178	echo ${EXPORT_SYMS} > export_syms
179.else
180	grep -v '^#' < ${EXPORT_SYMS} > export_syms
181.endif
182	awk -f ${SYSDIR}/conf/kmod_syms.awk ${.TARGET} \
183	    export_syms | xargs -J% ${OBJCOPY} % ${.TARGET}
184.endif
185.endif
186.if !defined(DEBUG_FLAGS) && ${MACHINE_ARCH} == amd64
187	${OBJCOPY} --strip-debug ${.TARGET}
188.endif
189
190_ILINKS=@ machine
191.if ${MACHINE} != ${MACHINE_ARCH}
192_ILINKS+=${MACHINE_ARCH}
193.endif
194
195all: objwarn ${PROG}
196
197beforedepend: ${_ILINKS} cleandepend
198beforedepend: cleandepend
199
200# Ensure that the links exist without depending on it when it exists which
201# causes all the modules to be rebuilt when the directory pointed to changes.
202.for _link in ${_ILINKS}
203.if !exists(${.OBJDIR}/${_link})
204${OBJS}: ${_link}
205.endif
206.endfor
207
208# Search for kernel source tree in standard places.
209.for _dir in ${.CURDIR}/../.. ${.CURDIR}/../../.. /sys /usr/src/sys
210.if !defined(SYSDIR) && exists(${_dir}/kern/)
211SYSDIR=	${_dir}
212.endif
213.endfor
214.if !defined(SYSDIR) || !exists(${SYSDIR}/kern/)
215.error "can't find kernel source tree"
216.endif
217
218${_ILINKS}:
219	@case ${.TARGET} in \
220	${MACHINE_ARCH}) \
221		path=${SYSDIR}/${MACHINE_ARCH}/include ;; \
222	machine) \
223		path=${SYSDIR}/${MACHINE}/include ;; \
224	@) \
225		path=${SYSDIR} ;; \
226	esac ; \
227	path=`(cd $$path && /bin/pwd)` ; \
228	${ECHO} ${.TARGET} "->" $$path ; \
229	ln -s $$path ${.TARGET}
230
231CLEANFILES+= ${PROG} ${KMOD}.kld ${OBJS} ${_ILINKS}
232
233.if defined(DEBUG_FLAGS)
234CLEANFILES+= ${FULLPROG}
235.endif
236
237.if !target(install)
238
239_INSTALLFLAGS:=	${INSTALLFLAGS}
240.for ie in ${INSTALLFLAGS_EDIT}
241_INSTALLFLAGS:=	${_INSTALLFLAGS${ie}}
242.endfor
243
244.if !target(install.debug) && defined(DEBUG_FLAGS)
245install.debug:
246	cd ${.CURDIR}; ${MAKE} -DINSTALL_DEBUG install
247.endif
248
249.if !target(realinstall)
250realinstall: _kmodinstall
251.ORDER: beforeinstall _kmodinstall
252_kmodinstall:
253.if defined(DEBUG_FLAGS) && defined(INSTALL_DEBUG)
254	${INSTALL} -o ${KMODOWN} -g ${KMODGRP} -m ${KMODMODE} \
255	    ${_INSTALLFLAGS} ${FULLPROG} ${DESTDIR}${KMODDIR}
256.else
257	${INSTALL} -o ${KMODOWN} -g ${KMODGRP} -m ${KMODMODE} \
258	    ${_INSTALLFLAGS} ${PROG} ${DESTDIR}${KMODDIR}
259
260.include <bsd.links.mk>
261
262.if !defined(NO_XREF)
263afterinstall: _kldxref
264.ORDER: realinstall _kldxref
265.ORDER: _installlinks _kldxref
266_kldxref:
267	@if type kldxref >/dev/null 2>&1; then \
268		${ECHO} kldxref ${DESTDIR}${KMODDIR}; \
269		kldxref ${DESTDIR}${KMODDIR}; \
270	fi
271.endif
272.endif
273.endif # !target(realinstall)
274
275.endif # !target(install)
276
277.if !target(load)
278load: ${PROG}
279	${KMODLOAD} -v ${.OBJDIR}/${PROG}
280.endif
281
282.if !target(unload)
283unload:
284	${KMODUNLOAD} -v ${PROG}
285.endif
286
287.if defined(KERNBUILDDIR)
288.PATH: ${KERNBUILDDIR}
289CFLAGS+=	-I${KERNBUILDDIR}
290.for _src in ${SRCS:Mopt_*.h}
291CLEANFILES+=	${_src}
292.if !target(${_src})
293${_src}:
294	ln -s ${KERNBUILDDIR}/${_src} ${.TARGET}
295.endif
296.endfor
297.else
298.for _src in ${SRCS:Mopt_*.h}
299CLEANFILES+=	${_src}
300.if !target(${_src})
301${_src}:
302	touch ${.TARGET}
303.endif
304.endfor
305.endif
306
307MFILES?= dev/acpica/acpi_if.m dev/ata/ata_if.m dev/eisa/eisa_if.m \
308	dev/iicbus/iicbb_if.m dev/iicbus/iicbus_if.m \
309	dev/mii/miibus_if.m dev/ofw/ofw_bus_if.m \
310	dev/pccard/card_if.m dev/pccard/power_if.m dev/pci/pci_if.m \
311	dev/pci/pcib_if.m dev/ppbus/ppbus_if.m dev/smbus/smbus_if.m \
312	dev/sound/pcm/ac97_if.m dev/sound/pcm/channel_if.m \
313	dev/sound/pcm/feeder_if.m dev/sound/pcm/mixer_if.m dev/uart/uart_if.m \
314	dev/usb/usb_if.m isa/isa_if.m \
315	kern/bus_if.m kern/cpufreq_if.m kern/device_if.m \
316	libkern/iconv_converter_if.m opencrypto/crypto_if.m \
317	pc98/pc98/canbus_if.m pci/agp_if.m
318
319.for _srcsrc in ${MFILES}
320.for _ext in c h
321.for _src in ${SRCS:M${_srcsrc:T:R}.${_ext}}
322CLEANFILES+=	${_src}
323.if !target(${_src})
324.if !exists(@)
325${_src}: @
326.else
327${_src}: @/tools/makeobjops.awk @/${_srcsrc}
328.endif
329	${AWK} -f @/tools/makeobjops.awk @/${_srcsrc} -${_ext}
330.endif
331.endfor # _src
332.endfor # _ext
333.endfor # _srcsrc
334
335.if ${SRCS:Mvnode_if.c} != ""
336CLEANFILES+=	vnode_if.c
337.if !exists(@)
338vnode_if.c: @
339.else
340vnode_if.c: @/tools/vnode_if.awk @/kern/vnode_if.src
341.endif
342	${AWK} -f @/tools/vnode_if.awk @/kern/vnode_if.src -c
343.endif
344
345.if ${SRCS:Mvnode_if.h} != ""
346CLEANFILES+=	vnode_if.h vnode_if_newproto.h vnode_if_typedef.h
347.if !exists(@)
348vnode_if.h vnode_if_newproto.h vnode_if_typedef.h: @
349.else
350vnode_if.h vnode_if_newproto.h vnode_if_typedef.h: @/tools/vnode_if.awk \
351    @/kern/vnode_if.src
352.endif
353vnode_if.h: vnode_if_newproto.h vnode_if_typedef.h
354	${AWK} -f @/tools/vnode_if.awk @/kern/vnode_if.src -h
355vnode_if_newproto.h:
356	${AWK} -f @/tools/vnode_if.awk @/kern/vnode_if.src -p
357vnode_if_typedef.h:
358	${AWK} -f @/tools/vnode_if.awk @/kern/vnode_if.src -q
359.endif
360
361.for _i in mii pccard
362.if ${SRCS:M${_i}devs.h} != ""
363CLEANFILES+=	${_i}devs.h
364.if !exists(@)
365${_i}devs.h: @
366.else
367${_i}devs.h: @/tools/${_i}devs2h.awk @/dev/${_i}/${_i}devs
368.endif
369	${AWK} -f @/tools/${_i}devs2h.awk @/dev/${_i}/${_i}devs
370.endif
371.endfor # _i
372
373.if ${SRCS:Musbdevs.h} != ""
374CLEANFILES+=	usbdevs.h
375.if !exists(@)
376usbdevs.h: @
377.else
378usbdevs.h: @/tools/usbdevs2h.awk @/dev/usb/usbdevs
379.endif
380	${AWK} -f @/tools/usbdevs2h.awk @/dev/usb/usbdevs -h
381.endif
382
383.if ${SRCS:Musbdevs_data.h} != ""
384CLEANFILES+=	usbdevs_data.h
385.if !exists(@)
386usbdevs_data.h: @
387.else
388usbdevs_data.h: @/tools/usbdevs2h.awk @/dev/usb/usbdevs
389.endif
390	${AWK} -f @/tools/usbdevs2h.awk @/dev/usb/usbdevs -d
391.endif
392
393.if ${SRCS:Macpi_quirks.h} != ""
394CLEANFILES+=	acpi_quirks.h
395.if !exists(@)
396acpi_quirks.h: @
397.else
398acpi_quirks.h: @/tools/acpi_quirks2h.awk @/dev/acpica/acpi_quirks
399.endif
400	${AWK} -f @/tools/acpi_quirks2h.awk @/dev/acpica/acpi_quirks
401.endif
402
403lint: ${SRCS}
404	${LINT} ${LINTKERNFLAGS} ${CFLAGS:M-[DILU]*} ${.ALLSRC:M*.c}
405
406.include <bsd.dep.mk>
407
408.if !exists(${.OBJDIR}/${DEPENDFILE})
409${OBJS}: ${SRCS:M*.h}
410.endif
411
412.include <bsd.obj.mk>
413.include "kern.mk"
414