kmod.mk revision 72935
1#	From: @(#)bsd.prog.mk	5.26 (Berkeley) 6/25/91
2# $FreeBSD: head/sys/conf/kmod.mk 72935 2001-02-23 04:49:31Z imp $
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# NOMAN		KLD does not have a manual page if set.
36#
37# PROG          The name of the kernel module to build.
38#		If not supplied, ${KMOD}.o is used.
39#
40# SRCS          List of source files
41#
42# SUBDIR        A list of subdirectories that should be built as well.
43#               Each of the targets will execute the same target in the
44#               subdirectories.
45#
46# SYMLINKS	Same as LINKS, except it creates symlinks and the
47#		linked-to pathname may be relative.
48#
49# DESTDIR, DISTDIR are set by other Makefiles (e.g. bsd.own.mk)
50#
51# MFILES	Optionally a list of interfaces used by the module.
52#		This file contains a default list of interfaces.
53#
54# +++ targets +++
55#
56#       distribute:
57#               This is a variant of install, which will
58#               put the stuff into the right "distribution".
59#
60# 	install:
61#               install the program and its manual pages; if the Makefile
62#               does not itself define the target install, the targets
63#               beforeinstall and afterinstall may also be used to cause
64#               actions immediately before and after the install target
65#		is executed.
66#
67# 	load:
68#		Load KLD.
69#
70# 	unload:
71#		Unload KLD.
72#
73# bsd.obj.mk: clean, cleandir and obj
74# bsd.dep.mk: cleandepend, depend and tags
75# bsd.man.mk: maninstall
76#
77
78KMODLOAD?=	/sbin/kldload
79KMODUNLOAD?=	/sbin/kldunload
80
81.if !target(__initialized__)
82__initialized__:
83.if exists(${.CURDIR}/../Makefile.inc)
84.include "${.CURDIR}/../Makefile.inc"
85.endif
86.endif
87
88.SUFFIXES: .out .o .c .cc .cxx .C .y .l .s .S
89
90CFLAGS+=	${COPTS} -D_KERNEL ${CWARNFLAGS}
91CFLAGS+=	-DKLD_MODULE
92
93# Don't use any standard or source-relative include directories.
94# Since -nostdinc will annull any previous -I paths, we repeat all
95# such paths after -nostdinc.  It doesn't seem to be possible to
96# add to the front of `make' variable.
97_ICFLAGS:=	${CFLAGS:M-I*}
98CFLAGS+=	-nostdinc -I- ${INCLMAGIC} ${_ICFLAGS}
99
100# Add -I paths for system headers.  Individual KLD makefiles don't
101# need any -I paths for this.  Similar defaults for .PATH can't be
102# set because there are no standard paths for non-headers.
103CFLAGS+=	-I. -I@ -I@/dev
104
105# Add a -I path to standard headers like <stddef.h>.  Use a relative
106# path to src/include if possible.  If the @ symlink hasn't been built
107# yet, then we can't tell if the relative path exists.  Add both the
108# potential relative path and an absolute path in that case.
109.if exists(@)
110.if exists(@/../include)
111CFLAGS+=	-I@/../include
112.else
113CFLAGS+=	-I${DESTDIR}/usr/include
114.endif
115.else # !@
116CFLAGS+=	-I@/../include -I${DESTDIR}/usr/include
117.endif # @
118
119CFLAGS+=	${DEBUG_FLAGS}
120
121.if ${OBJFORMAT} == elf
122CLEANFILES+=	setdef0.c setdef1.c setdefs.h
123CLEANFILES+=	setdef0.o setdef1.o
124.endif
125
126OBJS+=  ${SRCS:N*.h:R:S/$/.o/g}
127
128.if !defined(PROG)
129PROG=	${KMOD}.ko
130.endif
131
132${PROG}: ${KMOD}.kld
133.if ${OBJFORMAT} == elf
134	perl5 @/kern/gensetdefs.pl ${KMOD}.kld
135	${CC} ${CFLAGS} -c setdef0.c
136	${CC} ${CFLAGS} -c setdef1.c
137	${LD} -Bshareable ${LDFLAGS} -o ${.TARGET} setdef0.o ${KMOD}.kld setdef1.o
138.else
139	${LD} -Bshareable ${LDFLAGS} -o ${.TARGET} ${KMOD}.kld
140.endif
141
142${KMOD}.kld: ${OBJS}
143	${LD} ${LDFLAGS} -r -o ${.TARGET} ${OBJS}
144
145.if !defined(NOMAN)
146.include <bsd.man.mk>
147.if !defined(_MANPAGES) || empty(_MANPAGES)
148MAN1=	${KMOD}.4
149.endif
150
151.elif !target(maninstall)
152maninstall: _SUBDIR
153all-man:
154.endif
155
156_ILINKS=@ machine
157
158.MAIN: all
159all: objwarn ${PROG} all-man _SUBDIR
160
161# Ensure that the links exist without depending on it when it exists which
162# causes all the modules to be rebuilt when the directory pointed to changes.
163.for _link in ${_ILINKS}
164.if !exists(${.OBJDIR}/${_link})
165${OBJS}: ${_link}
166beforedepend: ${_link}
167	@rm -f .depend
168.endif
169.endfor
170
171# Search for kernel source tree in standard places.
172.for _dir in ${.CURDIR}/../.. ${.CURDIR}/../../.. /sys /usr/src/sys
173.if !defined(SYSDIR) && exists(${_dir}/kern/)
174SYSDIR=	${_dir}
175.endif
176.endfor
177.if !defined(SYSDIR) || !exists(${SYSDIR}/kern)
178.error "can't find kernel source tree"
179.endif
180
181${_ILINKS}:
182	@case ${.TARGET} in \
183	machine) \
184		path=${SYSDIR}/${MACHINE_ARCH}/include ;; \
185	@) \
186		path=${SYSDIR} ;; \
187	esac ; \
188	path=`(cd $$path && /bin/pwd)` ; \
189	${ECHO} ${.TARGET} "->" $$path ; \
190	ln -s $$path ${.TARGET}
191
192CLEANFILES+= ${PROG} ${KMOD}.kld ${OBJS} ${_ILINKS} symb.tmp tmp.o
193
194.if !target(install)
195.if !target(beforeinstall)
196beforeinstall:
197.endif
198.if !target(afterinstall)
199afterinstall:
200.endif
201
202_INSTALLFLAGS:=	${INSTALLFLAGS}
203.for ie in ${INSTALLFLAGS_EDIT}
204_INSTALLFLAGS:=	${_INSTALLFLAGS${ie}}
205.endfor
206
207realinstall: _SUBDIR
208	${INSTALL} ${COPY} -o ${KMODOWN} -g ${KMODGRP} -m ${KMODMODE} \
209	    ${_INSTALLFLAGS} ${PROG} ${DESTDIR}${KMODDIR}
210.if defined(LINKS) && !empty(LINKS)
211	@set ${LINKS}; \
212	while test $$# -ge 2; do \
213		l=${DESTDIR}$$1; \
214		shift; \
215		t=${DESTDIR}$$1; \
216		shift; \
217		${ECHO} $$t -\> $$l; \
218		ln -f $$l $$t; \
219	done; true
220.endif
221.if defined(SYMLINKS) && !empty(SYMLINKS)
222	@set ${SYMLINKS}; \
223	while test $$# -ge 2; do \
224		l=$$1; \
225		shift; \
226		t=${DESTDIR}$$1; \
227		shift; \
228		${ECHO} $$t -\> $$l; \
229		ln -fs $$l $$t; \
230	done; true
231.endif
232
233install: afterinstall _SUBDIR
234.if !defined(NOMAN)
235afterinstall: realinstall maninstall
236.else
237afterinstall: realinstall
238.endif
239realinstall: beforeinstall
240.endif
241
242DISTRIBUTION?=	bin
243.if !target(distribute)
244distribute: _SUBDIR
245.for dist in ${DISTRIBUTION}
246	cd ${.CURDIR} ; $(MAKE) install DESTDIR=${DISTDIR}/${dist} SHARED=copies
247.endfor
248.endif
249
250.if !target(load)
251load:	${PROG}
252	${KMODLOAD} -v ./${KMOD}.ko
253.endif
254
255.if !target(unload)
256unload:
257	${KMODUNLOAD} -v ${KMOD}
258.endif
259
260.for _src in ${SRCS:Mopt_*.h}
261CLEANFILES+=	${_src}
262.if !target(${_src})
263${_src}:
264	touch ${.TARGET}
265.endif
266.endfor
267
268MFILES?= kern/bus_if.m kern/device_if.m dev/iicbus/iicbb_if.m \
269    dev/iicbus/iicbus_if.m isa/isa_if.m dev/mii/miibus_if.m \
270    dev/pccard/card_if.m dev/pccard/power_if.m dev/pci/pci_if.m \
271    dev/pci/pcib_if.m dev/ppbus/ppbus_if.m dev/smbus/smbus_if.m \
272    dev/usb/usb_if.m dev/sound/pcm/ac97_if.m dev/sound/pcm/channel_if.m \
273    dev/sound/pcm/feeder_if.m dev/sound/pcm/mixer_if.m
274
275.for _srcsrc in ${MFILES}
276.for _ext in c h
277.for _src in ${SRCS:M${_srcsrc:T:R}.${_ext}}
278CLEANFILES+=	${_src}
279.if !target(${_src})
280.if !exists(@)
281${_src}: @
282.endif
283.if exists(@)
284${_src}: @/kern/makeobjops.pl @/${_srcsrc}
285.endif
286	perl @/kern/makeobjops.pl -${_ext} @/${_srcsrc}
287.endif
288.endfor # _src
289.endfor # _ext
290.endfor # _srcsrc
291
292.for _ext in c h
293.if ${SRCS:Mvnode_if.${_ext}} != ""
294CLEANFILES+=	vnode_if.${_ext}
295.if !exists(@)
296vnode_if.${_ext}: @
297.endif
298.if exists(@)
299vnode_if.${_ext}: @/kern/vnode_if.pl @/kern/vnode_if.src
300.endif
301	perl @/kern/vnode_if.pl -${_ext} @/kern/vnode_if.src
302.endif
303.endfor
304
305regress:
306
307.include <bsd.dep.mk>
308
309.if !exists(${DEPENDFILE})
310${OBJS}: ${SRCS:M*.h}
311.endif
312
313.include <bsd.obj.mk>
314
315.include <bsd.kern.mk>
316