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