kmod.mk revision 136400
1#	From: @(#)bsd.prog.mk	5.26 (Berkeley) 6/25/91
2# $FreeBSD: head/sys/conf/kmod.mk 136400 2004-10-11 21:23:56Z joerg $
3#
4# The include file <bsd.kmod.mk> handles installing Kernel Loadable Device
5# drivers (KLD's).
6#
7#
8# +++ variables +++
9#
10# CLEANFILES	Additional files to remove for the clean and cleandir targets.
11#
12# KMOD          The name of the kernel module to build.
13#
14# KMODDIR	Base path for kernel modules (see kld(4)). [/boot/kernel]
15#
16# KMODOWN	KLD owner. [${BINOWN}]
17#
18# KMODGRP	KLD group. [${BINGRP}]
19#
20# KMODMODE	KLD mode. [${BINMODE}]
21#
22# KMODLOAD	Command to load a kernel module [/sbin/kldload]
23#
24# KMODUNLOAD	Command to unload a kernel module [/sbin/kldunload]
25#
26# PROG          The name of the kernel module to build.
27#		If not supplied, ${KMOD}.o is used.
28#
29# SRCS          List of source files
30#
31# DESTDIR	Change the tree where the module gets installed. [not set]
32#
33# MFILES	Optionally a list of interfaces used by the module.
34#		This file contains a default list of interfaces.
35#
36# EXPORT_SYMS	A list of symbols that should be exported from the module,
37#		or the name of a file containing a list of symbols, or YES
38#		to export all symbols.  If not defined, no symbols are
39#		exported.
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 KLD.
52#
53# 	unload:
54#		Unload KLD.
55#
56# bsd.obj.mk: clean, cleandir and obj
57# bsd.dep.mk: cleandepend, depend and tags
58#
59
60AWK?=		awk
61KMODLOAD?=	/sbin/kldload
62KMODUNLOAD?=	/sbin/kldunload
63OBJCOPY?=	objcopy
64
65.if defined(KMODDEPS)
66.error "Do not use KMODDEPS on 5.0+, use MODULE_VERSION/MODULE_DEPEND"
67.endif
68
69.include <bsd.init.mk>
70
71.SUFFIXES: .out .o .c .cc .cxx .C .y .l .s .S
72
73.if ${CC} == "icc"
74CFLAGS:=	${CFLAGS:C/(-x[^M^K^W]+)[MKW]+|-x[MKW]+/\1/}
75.endif
76CFLAGS+=	-D_KERNEL
77CFLAGS+=	-DKLD_MODULE
78
79# Don't use any standard or source-relative include directories.
80# Since -nostdinc will annull any previous -I paths, we repeat all
81# such paths after -nostdinc.  It doesn't seem to be possible to
82# add to the front of `make' variable.
83_ICFLAGS:=	${CFLAGS:M-I*}
84.if ${CC} == "icc"
85NOSTDINC=	-X
86.else
87NOSTDINC=	-nostdinc
88.endif
89CFLAGS+=	${NOSTDINC} -I- ${INCLMAGIC} ${_ICFLAGS}
90.if defined(KERNBUILDDIR)
91CFLAGS+=       -include ${KERNBUILDDIR}/opt_global.h
92.endif
93
94# Add -I paths for system headers.  Individual KLD makefiles don't
95# need any -I paths for this.  Similar defaults for .PATH can't be
96# set because there are no standard paths for non-headers.
97CFLAGS+=	-I. -I@
98
99# Add -I path for altq headers as they are included via net/if_var.h
100# for example.
101CFLAGS+=	-I@/contrib/altq
102
103# Add a -I path to standard headers like <stddef.h>.  Use a relative
104# path to src/include if possible.  If the @ symlink hasn't been built
105# yet, then we can't tell if the relative path exists.  Add both the
106# potential relative path and an absolute path in that case.
107.if exists(@)
108.if exists(@/../include)
109CFLAGS+=	-I@/../include
110.else
111CFLAGS+=	-I${DESTDIR}/usr/include
112.endif
113.else # !@
114CFLAGS+=	-I@/../include -I${DESTDIR}/usr/include
115.endif # @
116
117.if ${CC} != "icc"
118CFLAGS+=	-finline-limit=${INLINE_LIMIT}
119.endif
120
121# Disallow common variables, and if we end up with commons from
122# somewhere unexpected, allocate storage for them in the module itself.
123.if ${CC} != "icc"
124CFLAGS+=	-fno-common
125.endif
126LDFLAGS+=	-d -warn-common
127
128CFLAGS+=	${DEBUG_FLAGS}
129.if ${MACHINE_ARCH} == amd64
130CFLAGS+=	-fno-omit-frame-pointer
131.endif
132
133OBJS+=  ${SRCS:N*.h:R:S/$/.o/g}
134
135.if !defined(PROG)
136PROG=	${KMOD}.ko
137.endif
138
139.if !defined(DEBUG_FLAGS)
140FULLPROG=	${PROG}
141.else
142FULLPROG=	${PROG}.debug
143${PROG}: ${FULLPROG}
144	${OBJCOPY} --strip-debug ${FULLPROG} ${PROG}
145.endif
146
147.if ${MACHINE_ARCH} != amd64
148${FULLPROG}: ${KMOD}.kld
149	${LD} -Bshareable ${LDFLAGS} -o ${.TARGET} ${KMOD}.kld
150.if !defined(DEBUG_FLAGS)
151	${OBJCOPY} --strip-debug ${.TARGET}
152.endif
153.endif
154
155EXPORT_SYMS?=	NO
156.if ${EXPORT_SYMS} != YES
157CLEANFILES+=	${.OBJDIR}/export_syms
158.endif
159
160.if ${MACHINE_ARCH} != amd64
161${KMOD}.kld: ${OBJS}
162.else
163${FULLPROG}: ${OBJS}
164.endif
165	${LD} ${LDFLAGS} -r -d -o ${.TARGET} ${OBJS}
166.if defined(EXPORT_SYMS)
167.if ${EXPORT_SYMS} != YES
168.if ${EXPORT_SYMS} == NO
169	touch ${.OBJDIR}/export_syms
170.elif !exists(${.CURDIR}/${EXPORT_SYMS})
171	echo ${EXPORT_SYMS} > ${.OBJDIR}/export_syms
172.else
173	grep -v '^#' < ${EXPORT_SYMS} >  ${.OBJDIR}/export_syms
174.endif
175	awk -f ${SYSDIR}/conf/kmod_syms.awk ${.TARGET} \
176		${.OBJDIR}/export_syms | \
177	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	@rm -f .depend
190
191# Ensure that the links exist without depending on it when it exists which
192# causes all the modules to be rebuilt when the directory pointed to changes.
193.for _link in ${_ILINKS}
194.if !exists(${.OBJDIR}/${_link})
195${OBJS}: ${_link}
196.endif
197.endfor
198
199# Search for kernel source tree in standard places.
200.for _dir in ${.CURDIR}/../.. ${.CURDIR}/../../.. /sys /usr/src/sys
201.if !defined(SYSDIR) && exists(${_dir}/kern/)
202SYSDIR=	${_dir}
203.endif
204.endfor
205.if !defined(SYSDIR) || !exists(${SYSDIR}/kern/)
206.error "can't find kernel source tree"
207.endif
208
209${_ILINKS}:
210	@case ${.TARGET} in \
211	machine) \
212		path=${SYSDIR}/${MACHINE_ARCH}/include ;; \
213	@) \
214		path=${SYSDIR} ;; \
215	esac ; \
216	path=`(cd $$path && /bin/pwd)` ; \
217	${ECHO} ${.TARGET} "->" $$path ; \
218	ln -s $$path ${.TARGET}
219
220CLEANFILES+= ${PROG} ${KMOD}.kld ${OBJS} ${_ILINKS} symb.tmp tmp.o
221
222.if defined(DEBUG_FLAGS)
223CLEANFILES+= ${FULLPROG}
224.endif
225
226.if !target(install)
227
228_INSTALLFLAGS:=	${INSTALLFLAGS}
229.for ie in ${INSTALLFLAGS_EDIT}
230_INSTALLFLAGS:=	${_INSTALLFLAGS${ie}}
231.endfor
232
233.if defined(DEBUG_FLAGS)
234install.debug:
235	cd ${.CURDIR}; ${MAKE} -DINSTALL_DEBUG install
236.endif
237
238.if !target(realinstall)
239realinstall: _kmodinstall
240.ORDER: beforeinstall _kmodinstall
241_kmodinstall:
242.if defined(DEBUG_FLAGS) && defined(INSTALL_DEBUG)
243	${INSTALL} -o ${KMODOWN} -g ${KMODGRP} -m ${KMODMODE} \
244	    ${_INSTALLFLAGS} ${FULLPROG} ${DESTDIR}${KMODDIR}
245.else
246	${INSTALL} -o ${KMODOWN} -g ${KMODGRP} -m ${KMODMODE} \
247	    ${_INSTALLFLAGS} ${PROG} ${DESTDIR}${KMODDIR}
248
249.include <bsd.links.mk>
250
251.if !defined(NO_XREF)
252afterinstall: _kldxref
253.ORDER: realinstall _kldxref
254.ORDER: _installlinks _kldxref
255_kldxref:
256	@if type kldxref >/dev/null 2>&1; then \
257		${ECHO} kldxref ${DESTDIR}${KMODDIR}; \
258		kldxref ${DESTDIR}${KMODDIR}; \
259	fi
260.endif
261.endif
262.endif !target(realinstall)
263
264.endif !target(install)
265
266.if !target(load)
267load:	${PROG}
268	${KMODLOAD} -v ${.OBJDIR}/${KMOD}.ko
269.endif
270
271.if !target(unload)
272unload:
273	${KMODUNLOAD} -v ${KMOD}
274.endif
275
276.if defined(KERNBUILDDIR)
277.PATH: ${KERNBUILDDIR}
278CFLAGS += -I${KERNBUILDDIR}
279.for _src in ${SRCS:Mopt_*.h}
280CLEANFILES+=	${_src}
281.if !target(${_src})
282${_src}:
283	ln -s ${KERNBUILDDIR}/${_src} ${.TARGET}
284.endif
285.endfor
286.else
287.for _src in ${SRCS:Mopt_*.h}
288CLEANFILES+=	${_src}
289.if !target(${_src})
290${_src}:
291	touch ${.TARGET}
292.endif
293.endfor
294.endif
295
296MFILES?= kern/bus_if.m kern/device_if.m dev/iicbus/iicbb_if.m \
297    dev/iicbus/iicbus_if.m isa/isa_if.m \
298    libkern/iconv_converter_if.m \
299    dev/acpica/acpi_if.m dev/eisa/eisa_if.m dev/mii/miibus_if.m \
300    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/usb/usb_if.m 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 pci/agp_if.m \
305    opencrypto/crypto_if.m pc98/pc98/canbus_if.m dev/uart/uart_if.m \
306    sparc64/pci/ofw_pci_if.m
307
308.for _srcsrc in ${MFILES}
309.for _ext in c h
310.for _src in ${SRCS:M${_srcsrc:T:R}.${_ext}}
311CLEANFILES+=	${_src}
312.if !target(${_src})
313.if !exists(@)
314${_src}: @
315.endif
316.if exists(@)
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.for _ext in c h
326.if ${SRCS:Mvnode_if.${_ext}} != ""
327CLEANFILES+=	vnode_if.${_ext}
328.if !exists(@)
329vnode_if.${_ext}: @
330.endif
331.if exists(@)
332vnode_if.${_ext}: @/tools/vnode_if.awk @/kern/vnode_if.src
333.endif
334	${AWK} -f @/tools/vnode_if.awk @/kern/vnode_if.src -${_ext}
335.endif
336.endfor
337
338.for _i in mii pccard usb
339.if ${SRCS:M${_i}devs.h} != ""
340CLEANFILES+=	${_i}devs.h
341.if !exists(@)
342${_i}devs.h: @
343.endif
344.if exists(@)
345${_i}devs.h: @/tools/${_i}devs2h.awk @/dev/${_i}/${_i}devs
346.endif
347	${AWK} -f @/tools/${_i}devs2h.awk @/dev/${_i}/${_i}devs
348.endif
349.endfor # _i
350
351.if ${SRCS:Macpi_quirks.h} != ""
352CLEANFILES+=	acpi_quirks.h
353.if !exists(@)
354acpi_quirks.h: @
355.endif
356.if exists(@)
357acpi_quirks.h: @/tools/acpi_quirks2h.awk @/dev/acpica/acpi_quirks
358.endif
359	${AWK} -f @/tools/acpi_quirks2h.awk @/dev/acpica/acpi_quirks
360.endif
361
362regress:
363
364lint: ${SRCS}
365	${LINT} ${LINTKERNFLAGS} ${CFLAGS:M-[DILU]*} ${.ALLSRC:M*.c}
366
367.include <bsd.dep.mk>
368
369.if !exists(${.OBJDIR}/${DEPENDFILE})
370${OBJS}: ${SRCS:M*.h}
371.endif
372
373.include <bsd.obj.mk>
374.include "kern.mk"
375