kmod.mk revision 99772
1#	From: @(#)bsd.prog.mk	5.26 (Berkeley) 6/25/91
2# $FreeBSD: head/sys/conf/kmod.mk 99772 2002-07-11 14:13:37Z ru $
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# LINKS		The list of KLD links; should be full pathnames, the
23#               linked-to file coming first, followed by the linked
24#               file.  The files are hard-linked.  For example, to link
25#               /modules/master and /modules/meister, use:
26#
27#			LINKS=  /modules/master /modules/meister
28#
29# KMODLOAD	Command to load a kernel module [/sbin/kldload]
30#
31# KMODUNLOAD	Command to unload a kernel module [/sbin/kldunload]
32#
33# PROG          The name of the kernel module to build.
34#		If not supplied, ${KMOD}.o is used.
35#
36# SRCS          List of source files
37#
38# SUBDIR        A list of subdirectories that should be built as well.
39#               Each of the targets will execute the same target in the
40#               subdirectories.
41#
42# SYMLINKS	Same as LINKS, except it creates symlinks and the
43#		linked-to pathname may be relative.
44#
45# DESTDIR, DISTDIR are set by other Makefiles (e.g. bsd.own.mk)
46#
47# MFILES	Optionally a list of interfaces used by the module.
48#		This file contains a default list of interfaces.
49#
50# EXPORT_SYMS	A list of symbols that should be exported from the module,
51#		or the name of a file containing a list of symbols, or YES
52#		to export all symbols.  If not defined, no symbols are
53#		exported.
54#
55# +++ targets +++
56#
57#       distribute:
58#               This is a variant of install, which will
59#               put the stuff into the right "distribution".
60#
61# 	install:
62#               install the kernel module; if the Makefile
63#               does not itself define the target install, the targets
64#               beforeinstall and afterinstall may also be used to cause
65#               actions immediately before and after the install target
66#		is executed.
67#
68# 	load:
69#		Load KLD.
70#
71# 	unload:
72#		Unload KLD.
73#
74# bsd.obj.mk: clean, cleandir and obj
75# bsd.dep.mk: cleandepend, depend and tags
76#
77
78AWK?=		awk
79KMODLOAD?=	/sbin/kldload
80KMODUNLOAD?=	/sbin/kldunload
81OBJCOPY?=	objcopy
82
83.if defined(KMODDEPS)
84.error "Do not use KMODDEPS on 5.0+, use MODULE_VERSION/MODULE_DEPEND"
85.endif
86
87.include <bsd.init.mk>
88
89.SUFFIXES: .out .o .c .cc .cxx .C .y .l .s .S
90
91CFLAGS+=	${COPTS} -D_KERNEL ${CWARNFLAGS}
92CFLAGS+=	-DKLD_MODULE
93
94# Don't use any standard or source-relative include directories.
95# Since -nostdinc will annull any previous -I paths, we repeat all
96# such paths after -nostdinc.  It doesn't seem to be possible to
97# add to the front of `make' variable.
98_ICFLAGS:=	${CFLAGS:M-I*}
99CFLAGS+=	-nostdinc -I- ${INCLMAGIC} ${_ICFLAGS}
100
101# Add -I paths for system headers.  Individual KLD makefiles don't
102# need any -I paths for this.  Similar defaults for .PATH can't be
103# set because there are no standard paths for non-headers.
104CFLAGS+=	-I. -I@ -I@/dev
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# Disallow common variables, and if we end up with commons from
121# somewhere unexpected, allocate storage for them in the module itself.
122CFLAGS+=	-fno-common
123LDFLAGS+=	-d -warn-common
124
125CFLAGS+=	${DEBUG_FLAGS}
126
127.if ${OBJFORMAT} == elf
128CLEANFILES+=	setdef0.c setdef1.c setdefs.h
129CLEANFILES+=	setdef0.o setdef1.o
130.endif
131
132OBJS+=  ${SRCS:N*.h:R:S/$/.o/g}
133
134.if !defined(PROG)
135PROG=	${KMOD}.ko
136.endif
137
138.if !defined(DEBUG)
139FULLPROG=	${PROG}
140.else
141FULLPROG=	${PROG}.debug
142${PROG}: ${FULLPROG}
143	${OBJCOPY} --strip-debug ${FULLPROG} ${PROG}
144.endif
145
146${FULLPROG}: ${KMOD}.kld
147	${LD} -Bshareable ${LDFLAGS} -o ${.TARGET} ${KMOD}.kld
148
149EXPORT_SYMS?=	NO
150.if ${EXPORT_SYMS} != YES
151CLEANFILES+=	${.OBJDIR}/export_syms
152.endif
153
154${KMOD}.kld: ${OBJS}
155	${LD} ${LDFLAGS} -r -d -o ${.TARGET} ${OBJS}
156.if defined(EXPORT_SYMS)
157.if ${EXPORT_SYMS} != YES
158.if ${EXPORT_SYMS} == NO
159	touch ${.OBJDIR}/export_syms
160.elif !exists(${.CURDIR}/${EXPORT_SYMS})
161	echo ${EXPORT_SYMS} > ${.OBJDIR}/export_syms
162.else
163	grep -v '^#' < ${EXPORT_SYMS} >  ${.OBJDIR}/export_syms
164.endif
165	awk -f ${SYSDIR}/conf/kmod_syms.awk ${.TARGET} \
166		${.OBJDIR}/export_syms | \
167	xargs -J% ${OBJCOPY} % ${.TARGET}
168.endif
169.endif
170
171_ILINKS=@ machine
172
173all: objwarn ${PROG}
174
175beforedepend: ${_ILINKS}
176	@rm -f .depend
177
178# Ensure that the links exist without depending on it when it exists which
179# causes all the modules to be rebuilt when the directory pointed to changes.
180.for _link in ${_ILINKS}
181.if !exists(${.OBJDIR}/${_link})
182${OBJS}: ${_link}
183.endif
184.endfor
185
186# Search for kernel source tree in standard places.
187.for _dir in ${.CURDIR}/../.. ${.CURDIR}/../../.. /sys /usr/src/sys
188.if !defined(SYSDIR) && exists(${_dir}/kern/)
189SYSDIR=	${_dir}
190.endif
191.endfor
192.if !defined(SYSDIR) || !exists(${SYSDIR}/kern)
193.error "can't find kernel source tree"
194.endif
195
196${_ILINKS}:
197	@case ${.TARGET} in \
198	machine) \
199		path=${SYSDIR}/${MACHINE_ARCH}/include ;; \
200	@) \
201		path=${SYSDIR} ;; \
202	esac ; \
203	path=`(cd $$path && /bin/pwd)` ; \
204	${ECHO} ${.TARGET} "->" $$path ; \
205	ln -s $$path ${.TARGET}
206
207CLEANFILES+= ${PROG} ${FULLPROG} ${KMOD}.kld ${OBJS} ${_ILINKS} symb.tmp tmp.o
208
209.if !target(install)
210.if !target(beforeinstall)
211beforeinstall:
212.endif
213.if !target(afterinstall)
214afterinstall:
215.endif
216
217_INSTALLFLAGS:=	${INSTALLFLAGS}
218.for ie in ${INSTALLFLAGS_EDIT}
219_INSTALLFLAGS:=	${_INSTALLFLAGS${ie}}
220.endfor
221
222.if defined(DEBUG)
223install.debug:
224	cd ${.CURDIR}; ${MAKE} -DINSTALL_DEBUG install
225.endif
226
227realinstall:
228.if defined(DEBUG) && defined(INSTALL_DEBUG)
229	${INSTALL} ${COPY} -o ${KMODOWN} -g ${KMODGRP} -m ${KMODMODE} \
230	    ${_INSTALLFLAGS} ${FULLPROG} ${DESTDIR}${KMODDIR}/
231.else
232	${INSTALL} ${COPY} -o ${KMODOWN} -g ${KMODGRP} -m ${KMODMODE} \
233	    ${_INSTALLFLAGS} ${PROG} ${DESTDIR}${KMODDIR}/
234.if defined(LINKS) && !empty(LINKS)
235	@set ${LINKS}; \
236	while test $$# -ge 2; do \
237		l=${DESTDIR}$$1; \
238		shift; \
239		t=${DESTDIR}$$1; \
240		shift; \
241		${ECHO} $$t -\> $$l; \
242		ln -f $$l $$t; \
243	done; true
244.endif
245.if defined(SYMLINKS) && !empty(SYMLINKS)
246	@set ${SYMLINKS}; \
247	while test $$# -ge 2; do \
248		l=$$1; \
249		shift; \
250		t=${DESTDIR}$$1; \
251		shift; \
252		${ECHO} $$t -\> $$l; \
253		ln -fs $$l $$t; \
254	done; true
255.endif
256.if !defined(NO_XREF)
257	@if type kldxref >/dev/null 2>&1; then \
258		${ECHO} kldxref ${DESTDIR}${KMODDIR}; \
259		kldxref ${DESTDIR}${KMODDIR}; \
260	fi
261.endif
262.endif
263
264install: afterinstall
265afterinstall: realinstall
266realinstall: beforeinstall
267.endif
268
269.if !target(load)
270load:	${PROG}
271	${KMODLOAD} -v ${.OBJDIR}/${KMOD}.ko
272.endif
273
274.if !target(unload)
275unload:
276	${KMODUNLOAD} -v ${KMOD}
277.endif
278
279.for _src in ${SRCS:Mopt_*.h}
280CLEANFILES+=	${_src}
281.if !target(${_src})
282${_src}:
283	touch ${.TARGET}
284.endif
285.endfor
286
287MFILES?= kern/bus_if.m kern/device_if.m dev/iicbus/iicbb_if.m \
288    dev/iicbus/iicbus_if.m isa/isa_if.m \
289    libkern/iconv_converter_if.m \
290    dev/mii/miibus_if.m \
291    dev/pccard/card_if.m dev/pccard/power_if.m dev/pci/pci_if.m \
292    dev/pci/pcib_if.m dev/ppbus/ppbus_if.m dev/smbus/smbus_if.m \
293    dev/usb/usb_if.m dev/sound/pcm/ac97_if.m dev/sound/pcm/channel_if.m \
294    dev/sound/pcm/feeder_if.m dev/sound/pcm/mixer_if.m pci/agp_if.m
295
296.for _srcsrc in ${MFILES}
297.for _ext in c h
298.for _src in ${SRCS:M${_srcsrc:T:R}.${_ext}}
299CLEANFILES+=	${_src}
300.if !target(${_src})
301.if !exists(@)
302${_src}: @
303.endif
304.if exists(@)
305${_src}: @/tools/makeobjops.awk @/${_srcsrc}
306.endif
307	${AWK} -f @/tools/makeobjops.awk @/${_srcsrc} -${_ext}
308.endif
309.endfor # _src
310.endfor # _ext
311.endfor # _srcsrc
312
313.for _ext in c h
314.if ${SRCS:Mvnode_if.${_ext}} != ""
315CLEANFILES+=	vnode_if.${_ext}
316.if !exists(@)
317vnode_if.${_ext}: @
318.endif
319.if exists(@)
320vnode_if.${_ext}: @/tools/vnode_if.awk @/kern/vnode_if.src
321.endif
322	${AWK} -f @/tools/vnode_if.awk @/kern/vnode_if.src -${_ext}
323.endif
324.endfor
325
326regress:
327
328lint: ${SRCS}
329	${LINT} ${LINTKERNFLAGS} ${CFLAGS:M-[DILU]*} ${.ALLSRC:M*.c}
330
331.include <bsd.dep.mk>
332
333.if !exists(${DEPENDFILE})
334${OBJS}: ${SRCS:M*.h}
335.endif
336
337.include <bsd.obj.mk>
338.include <bsd.kern.mk>
339