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