kmod.mk revision 144562
1#	From: @(#)bsd.prog.mk	5.26 (Berkeley) 6/25/91
2# $FreeBSD: head/sys/conf/kmod.mk 144562 2005-04-03 04:56:05Z imp $
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}
198
199# Ensure that the links exist without depending on it when it exists which
200# causes all the modules to be rebuilt when the directory pointed to changes.
201.for _link in ${_ILINKS}
202.if !exists(${.OBJDIR}/${_link})
203${OBJS}: ${_link}
204.endif
205.endfor
206
207# Search for kernel source tree in standard places.
208.for _dir in ${.CURDIR}/../.. ${.CURDIR}/../../.. /sys /usr/src/sys
209.if !defined(SYSDIR) && exists(${_dir}/kern/)
210SYSDIR=	${_dir}
211.endif
212.endfor
213.if !defined(SYSDIR) || !exists(${SYSDIR}/kern/)
214.error "can't find kernel source tree"
215.endif
216
217${_ILINKS}:
218	@case ${.TARGET} in \
219	${MACHINE_ARCH}) \
220		path=${SYSDIR}/${MACHINE_ARCH}/include ;; \
221	machine) \
222		path=${SYSDIR}/${MACHINE}/include ;; \
223	@) \
224		path=${SYSDIR} ;; \
225	esac ; \
226	path=`(cd $$path && /bin/pwd)` ; \
227	${ECHO} ${.TARGET} "->" $$path ; \
228	ln -s $$path ${.TARGET}
229
230CLEANFILES+= ${PROG} ${KMOD}.kld ${OBJS} ${_ILINKS}
231
232.if defined(DEBUG_FLAGS)
233CLEANFILES+= ${FULLPROG}
234.endif
235
236.if !target(install)
237
238_INSTALLFLAGS:=	${INSTALLFLAGS}
239.for ie in ${INSTALLFLAGS_EDIT}
240_INSTALLFLAGS:=	${_INSTALLFLAGS${ie}}
241.endfor
242
243.if !target(install.debug) && defined(DEBUG_FLAGS)
244install.debug:
245	cd ${.CURDIR}; ${MAKE} -DINSTALL_DEBUG install
246.endif
247
248.if !target(realinstall)
249realinstall: _kmodinstall
250.ORDER: beforeinstall _kmodinstall
251_kmodinstall:
252.if defined(DEBUG_FLAGS) && defined(INSTALL_DEBUG)
253	${INSTALL} -o ${KMODOWN} -g ${KMODGRP} -m ${KMODMODE} \
254	    ${_INSTALLFLAGS} ${FULLPROG} ${DESTDIR}${KMODDIR}
255.else
256	${INSTALL} -o ${KMODOWN} -g ${KMODGRP} -m ${KMODMODE} \
257	    ${_INSTALLFLAGS} ${PROG} ${DESTDIR}${KMODDIR}
258
259.include <bsd.links.mk>
260
261.if !defined(NO_XREF)
262afterinstall: _kldxref
263.ORDER: realinstall _kldxref
264.ORDER: _installlinks _kldxref
265_kldxref:
266	@if type kldxref >/dev/null 2>&1; then \
267		${ECHO} kldxref ${DESTDIR}${KMODDIR}; \
268		kldxref ${DESTDIR}${KMODDIR}; \
269	fi
270.endif
271.endif
272.endif !target(realinstall)
273
274.endif !target(install)
275
276.if !target(load)
277load: ${PROG}
278	${KMODLOAD} -v ${.OBJDIR}/${PROG}
279.endif
280
281.if !target(unload)
282unload:
283	${KMODUNLOAD} -v ${PROG}
284.endif
285
286.if defined(KERNBUILDDIR)
287.PATH: ${KERNBUILDDIR}
288CFLAGS+=	-I${KERNBUILDDIR}
289.for _src in ${SRCS:Mopt_*.h}
290CLEANFILES+=	${_src}
291.if !target(${_src})
292${_src}:
293	ln -s ${KERNBUILDDIR}/${_src} ${.TARGET}
294.endif
295.endfor
296.else
297.for _src in ${SRCS:Mopt_*.h}
298CLEANFILES+=	${_src}
299.if !target(${_src})
300${_src}:
301	touch ${.TARGET}
302.endif
303.endfor
304.endif
305
306MFILES?= dev/acpica/acpi_if.m dev/ata/ata_if.m dev/eisa/eisa_if.m \
307	dev/iicbus/iicbb_if.m dev/iicbus/iicbus_if.m \
308	dev/mii/miibus_if.m dev/ofw/ofw_bus_if.m \
309	dev/pccard/card_if.m dev/pccard/power_if.m dev/pci/pci_if.m \
310	dev/pci/pcib_if.m dev/ppbus/ppbus_if.m dev/smbus/smbus_if.m \
311	dev/sound/pcm/ac97_if.m dev/sound/pcm/channel_if.m \
312	dev/sound/pcm/feeder_if.m dev/sound/pcm/mixer_if.m dev/uart/uart_if.m \
313	dev/usb/usb_if.m isa/isa_if.m \
314	kern/bus_if.m kern/cpufreq_if.m kern/device_if.m \
315	libkern/iconv_converter_if.m opencrypto/crypto_if.m \
316	pc98/pc98/canbus_if.m pci/agp_if.m
317
318.for _srcsrc in ${MFILES}
319.for _ext in c h
320.for _src in ${SRCS:M${_srcsrc:T:R}.${_ext}}
321CLEANFILES+=	${_src}
322.if !target(${_src})
323.if !exists(@)
324${_src}: @
325.else
326${_src}: @/tools/makeobjops.awk @/${_srcsrc}
327.endif
328	${AWK} -f @/tools/makeobjops.awk @/${_srcsrc} -${_ext}
329.endif
330.endfor # _src
331.endfor # _ext
332.endfor # _srcsrc
333
334.if ${SRCS:Mvnode_if.c} != ""
335CLEANFILES+=	vnode_if.c
336.if !exists(@)
337vnode_if.c: @
338.else
339vnode_if.c: @/tools/vnode_if.awk @/kern/vnode_if.src
340.endif
341	${AWK} -f @/tools/vnode_if.awk @/kern/vnode_if.src -c
342.endif
343
344.if ${SRCS:Mvnode_if.h} != ""
345CLEANFILES+=	vnode_if.h vnode_if_newproto.h vnode_if_typedef.h
346.if !exists(@)
347vnode_if.h vnode_if_newproto.h vnode_if_typedef.h: @
348.else
349vnode_if.h vnode_if_newproto.h vnode_if_typedef.h: @/tools/vnode_if.awk \
350    @/kern/vnode_if.src
351.endif
352vnode_if.h: vnode_if_newproto.h vnode_if_typedef.h
353	${AWK} -f @/tools/vnode_if.awk @/kern/vnode_if.src -h
354vnode_if_newproto.h:
355	${AWK} -f @/tools/vnode_if.awk @/kern/vnode_if.src -p
356vnode_if_typedef.h:
357	${AWK} -f @/tools/vnode_if.awk @/kern/vnode_if.src -q
358.endif
359
360.for _i in mii pccard
361.if ${SRCS:M${_i}devs.h} != ""
362CLEANFILES+=	${_i}devs.h
363.if !exists(@)
364${_i}devs.h: @
365.else
366${_i}devs.h: @/tools/${_i}devs2h.awk @/dev/${_i}/${_i}devs
367.endif
368	${AWK} -f @/tools/${_i}devs2h.awk @/dev/${_i}/${_i}devs
369.endif
370.endfor # _i
371
372.if ${SRCS:Musbdevs.h} != ""
373CLEANFILES+=	usbdevs.h
374.if !exists(@)
375usbdevs.h: @
376.else
377usbdevs.h: @/tools/usbdevs2h.awk @/dev/usb/usbdevs
378.endif
379	${AWK} -f @/tools/usbdevs2h.awk @/dev/usb/usbdevs -h
380.endif
381
382.if ${SRCS:Musbdevs_data.h} != ""
383CLEANFILES+=	usbdevs_data.h
384.if !exists(@)
385usbdevs_data.h: @
386.else
387usbdevs_data.h: @/tools/usbdevs2h.awk @/dev/usb/usbdevs
388.endif
389	${AWK} -f @/tools/usbdevs2h.awk @/dev/usb/usbdevs -d
390.endif
391
392.if ${SRCS:Macpi_quirks.h} != ""
393CLEANFILES+=	acpi_quirks.h
394.if !exists(@)
395acpi_quirks.h: @
396.else
397acpi_quirks.h: @/tools/acpi_quirks2h.awk @/dev/acpica/acpi_quirks
398.endif
399	${AWK} -f @/tools/acpi_quirks2h.awk @/dev/acpica/acpi_quirks
400.endif
401
402lint: ${SRCS}
403	${LINT} ${LINTKERNFLAGS} ${CFLAGS:M-[DILU]*} ${.ALLSRC:M*.c}
404
405.include <bsd.dep.mk>
406
407.if !exists(${.OBJDIR}/${DEPENDFILE})
408${OBJS}: ${SRCS:M*.h}
409.endif
410
411.include <bsd.obj.mk>
412.include "kern.mk"
413