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