kmod.mk revision 71751
1#	From: @(#)bsd.prog.mk	5.26 (Berkeley) 6/25/91
2# $FreeBSD: head/sys/conf/kmod.mk 71751 2001-01-28 21:24:05Z marcel $
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	gensetdefs ${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
161beforedepend ${OBJS}: ${_ILINKS}
162
163# Search for kernel source tree in standard places.
164.for _dir in ${.CURDIR}/../.. ${.CURDIR}/../../.. /sys /usr/src/sys
165.if !defined(SYSDIR) && exists(${_dir}/kern/)
166SYSDIR=	${_dir}
167.endif
168.endfor
169.if !defined(SYSDIR) || !exists(${SYSDIR}/kern)
170.error "can't find kernel source tree"
171.endif
172
173${_ILINKS}:
174	@case ${.TARGET} in \
175	machine) \
176		path=${SYSDIR}/${MACHINE_ARCH}/include ;; \
177	@) \
178		path=${SYSDIR} ;; \
179	esac ; \
180	path=`(cd $$path && /bin/pwd)` ; \
181	${ECHO} ${.TARGET} "->" $$path ; \
182	ln -s $$path ${.TARGET}
183
184CLEANFILES+= ${PROG} ${KMOD}.kld ${OBJS} ${_ILINKS} symb.tmp tmp.o
185
186.if !target(install)
187.if !target(beforeinstall)
188beforeinstall:
189.endif
190.if !target(afterinstall)
191afterinstall:
192.endif
193
194_INSTALLFLAGS:=	${INSTALLFLAGS}
195.for ie in ${INSTALLFLAGS_EDIT}
196_INSTALLFLAGS:=	${_INSTALLFLAGS${ie}}
197.endfor
198
199realinstall: _SUBDIR
200	${INSTALL} ${COPY} -o ${KMODOWN} -g ${KMODGRP} -m ${KMODMODE} \
201	    ${_INSTALLFLAGS} ${PROG} ${DESTDIR}${KMODDIR}
202.if defined(LINKS) && !empty(LINKS)
203	@set ${LINKS}; \
204	while test $$# -ge 2; do \
205		l=${DESTDIR}$$1; \
206		shift; \
207		t=${DESTDIR}$$1; \
208		shift; \
209		${ECHO} $$t -\> $$l; \
210		ln -f $$l $$t; \
211	done; true
212.endif
213.if defined(SYMLINKS) && !empty(SYMLINKS)
214	@set ${SYMLINKS}; \
215	while test $$# -ge 2; do \
216		l=$$1; \
217		shift; \
218		t=${DESTDIR}$$1; \
219		shift; \
220		${ECHO} $$t -\> $$l; \
221		ln -fs $$l $$t; \
222	done; true
223.endif
224
225install: afterinstall _SUBDIR
226.if !defined(NOMAN)
227afterinstall: realinstall maninstall
228.else
229afterinstall: realinstall
230.endif
231realinstall: beforeinstall
232.endif
233
234DISTRIBUTION?=	bin
235.if !target(distribute)
236distribute: _SUBDIR
237.for dist in ${DISTRIBUTION}
238	cd ${.CURDIR} ; $(MAKE) install DESTDIR=${DISTDIR}/${dist} SHARED=copies
239.endfor
240.endif
241
242.if !target(load)
243load:	${PROG}
244	${KMODLOAD} -v ./${KMOD}.ko
245.endif
246
247.if !target(unload)
248unload:
249	${KMODUNLOAD} -v ${KMOD}
250.endif
251
252.for _src in ${SRCS:Mopt_*.h}
253CLEANFILES+=	${_src}
254.if !target(${_src})
255${_src}:
256	touch ${.TARGET}
257.endif
258.endfor
259
260MFILES?= kern/bus_if.m kern/device_if.m dev/iicbus/iicbb_if.m \
261    dev/iicbus/iicbus_if.m isa/isa_if.m dev/mii/miibus_if.m \
262    dev/pccard/card_if.m dev/pccard/power_if.m dev/pci/pci_if.m \
263    dev/pci/pcib_if.m dev/ppbus/ppbus_if.m dev/smbus/smbus_if.m \
264    dev/usb/usb_if.m dev/sound/pcm/ac97_if.m dev/sound/pcm/channel_if.m \
265    dev/sound/pcm/feeder_if.m dev/sound/pcm/mixer_if.m
266
267.for _srcsrc in ${MFILES}
268.for _ext in c h
269.for _src in ${SRCS:M${_srcsrc:T:R}.${_ext}}
270CLEANFILES+=	${_src}
271.if !target(${_src})
272${_src}: @
273.if exists(@)
274${_src}: @/kern/makeobjops.pl @/${_srcsrc}
275.endif
276	perl @/kern/makeobjops.pl -${_ext} @/${_srcsrc}
277.endif
278.endfor # _src
279.endfor # _ext
280.endfor # _srcsrc
281
282.for _ext in c h
283.if ${SRCS:Mvnode_if.${_ext}} != ""
284CLEANFILES+=	vnode_if.${_ext}
285vnode_if.${_ext}: @
286.if exists(@)
287vnode_if.${_ext}: @/kern/vnode_if.pl @/kern/vnode_if.src
288.endif
289	perl @/kern/vnode_if.pl -${_ext} @/kern/vnode_if.src
290.endif
291.endfor
292
293regress:
294
295.include <bsd.dep.mk>
296
297.if !exists(${DEPENDFILE})
298${OBJS}: ${SRCS:M*.h}
299.endif
300
301.include <bsd.obj.mk>
302
303.include <bsd.kern.mk>
304