kmod.mk revision 83321
1#	From: @(#)bsd.prog.mk	5.26 (Berkeley) 6/25/91
2# $FreeBSD: head/sys/conf/kmod.mk 83321 2001-09-11 01:09:24Z peter $
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
80OBJCOPY?=	objcopy
81
82TARGET_ARCH?=	${MACHINE_ARCH}
83
84.if !target(__initialized__)
85__initialized__:
86.if exists(${.CURDIR}/../Makefile.inc)
87.include "${.CURDIR}/../Makefile.inc"
88.endif
89.endif
90
91.SUFFIXES: .out .o .c .cc .cxx .C .y .l .s .S
92
93CFLAGS+=	${COPTS} -D_KERNEL ${CWARNFLAGS}
94CFLAGS+=	-DKLD_MODULE
95
96# Don't use any standard or source-relative include directories.
97# Since -nostdinc will annull any previous -I paths, we repeat all
98# such paths after -nostdinc.  It doesn't seem to be possible to
99# add to the front of `make' variable.
100_ICFLAGS:=	${CFLAGS:M-I*}
101CFLAGS+=	-nostdinc -I- ${INCLMAGIC} ${_ICFLAGS}
102
103# Add -I paths for system headers.  Individual KLD makefiles don't
104# need any -I paths for this.  Similar defaults for .PATH can't be
105# set because there are no standard paths for non-headers.
106CFLAGS+=	-I. -I@ -I@/dev
107
108# Add a -I path to standard headers like <stddef.h>.  Use a relative
109# path to src/include if possible.  If the @ symlink hasn't been built
110# yet, then we can't tell if the relative path exists.  Add both the
111# potential relative path and an absolute path in that case.
112.if exists(@)
113.if exists(@/../include)
114CFLAGS+=	-I@/../include
115.else
116CFLAGS+=	-I${DESTDIR}/usr/include
117.endif
118.else # !@
119CFLAGS+=	-I@/../include -I${DESTDIR}/usr/include
120.endif # @
121
122CFLAGS+=	${DEBUG_FLAGS}
123
124.if ${OBJFORMAT} == elf
125CLEANFILES+=	setdef0.c setdef1.c setdefs.h
126CLEANFILES+=	setdef0.o setdef1.o
127.endif
128
129OBJS+=  ${SRCS:N*.h:R:S/$/.o/g}
130
131.if !defined(PROG)
132PROG=	${KMOD}.ko
133.endif
134
135.if !defined(DEBUG)
136FULLPROG=	${PROG}
137.else
138FULLPROG=	${PROG}.debug
139${PROG}: ${FULLPROG}
140	${OBJCOPY} --strip-debug ${FULLPROG} ${PROG}
141.endif
142
143${FULLPROG}: ${KMOD}.kld
144	${LD} -Bshareable ${LDFLAGS} -o ${.TARGET} ${KMOD}.kld
145
146${KMOD}.kld: ${OBJS}
147	${LD} ${LDFLAGS} -r -o ${.TARGET} ${OBJS}
148
149.if !defined(NOMAN)
150.if 0
151MAN?=	${KMOD}.4
152.endif
153.include <bsd.man.mk>
154.else
155.if !target(all-man)
156all-man: _SUBDIR
157.endif
158.if !target(maninstall)
159maninstall: _SUBDIR
160.endif
161.endif
162
163_ILINKS=@ machine
164
165.MAIN: all
166all: objwarn ${PROG} all-man _SUBDIR
167
168beforedepend: ${_ILINKS}
169	@rm -f .depend
170
171# Ensure that the links exist without depending on it when it exists which
172# causes all the modules to be rebuilt when the directory pointed to changes.
173.for _link in ${_ILINKS}
174.if !exists(${.OBJDIR}/${_link})
175${OBJS}: ${_link}
176.endif
177.endfor
178
179# Search for kernel source tree in standard places.
180.for _dir in ${.CURDIR}/../.. ${.CURDIR}/../../.. /sys /usr/src/sys
181.if !defined(SYSDIR) && exists(${_dir}/kern/)
182SYSDIR=	${_dir}
183.endif
184.endfor
185.if !defined(SYSDIR) || !exists(${SYSDIR}/kern)
186.error "can't find kernel source tree"
187.endif
188
189${_ILINKS}:
190	@case ${.TARGET} in \
191	machine) \
192		path=${SYSDIR}/${MACHINE_ARCH}/include ;; \
193	@) \
194		path=${SYSDIR} ;; \
195	esac ; \
196	path=`(cd $$path && /bin/pwd)` ; \
197	${ECHO} ${.TARGET} "->" $$path ; \
198	ln -s $$path ${.TARGET}
199
200CLEANFILES+= ${PROG} ${FULLPROG} ${KMOD}.kld ${OBJS} ${_ILINKS} symb.tmp tmp.o
201
202.if !target(install)
203.if !target(beforeinstall)
204beforeinstall:
205.endif
206.if !target(afterinstall)
207afterinstall:
208.endif
209
210_INSTALLFLAGS:=	${INSTALLFLAGS}
211.for ie in ${INSTALLFLAGS_EDIT}
212_INSTALLFLAGS:=	${_INSTALLFLAGS${ie}}
213.endfor
214
215realinstall: _SUBDIR
216	${INSTALL} ${COPY} -o ${KMODOWN} -g ${KMODGRP} -m ${KMODMODE} \
217	    ${_INSTALLFLAGS} ${PROG} ${DESTDIR}${KMODDIR}/
218.if defined(LINKS) && !empty(LINKS)
219	@set ${LINKS}; \
220	while test $$# -ge 2; do \
221		l=${DESTDIR}$$1; \
222		shift; \
223		t=${DESTDIR}$$1; \
224		shift; \
225		${ECHO} $$t -\> $$l; \
226		ln -f $$l $$t; \
227	done; true
228.endif
229.if defined(SYMLINKS) && !empty(SYMLINKS)
230	@set ${SYMLINKS}; \
231	while test $$# -ge 2; do \
232		l=$$1; \
233		shift; \
234		t=${DESTDIR}$$1; \
235		shift; \
236		${ECHO} $$t -\> $$l; \
237		ln -fs $$l $$t; \
238	done; true
239.endif
240.if !defined(NO_XREF)
241	kldxref ${DESTDIR}${KMODDIR}
242.endif
243
244install: afterinstall _SUBDIR
245.if !defined(NOMAN)
246afterinstall: realinstall maninstall
247.else
248afterinstall: realinstall
249.endif
250realinstall: beforeinstall
251.endif
252
253DISTRIBUTION?=	bin
254.if !target(distribute)
255distribute: _SUBDIR
256.for dist in ${DISTRIBUTION}
257	cd ${.CURDIR} ; $(MAKE) install DESTDIR=${DISTDIR}/${dist} SHARED=copies
258.endfor
259.endif
260
261.if !target(load)
262load:	${PROG}
263	${KMODLOAD} -v ${.CURDIR}/${KMOD}.ko
264.endif
265
266.if !target(unload)
267unload:
268	${KMODUNLOAD} -v ${KMOD}
269.endif
270
271.for _src in ${SRCS:Mopt_*.h}
272CLEANFILES+=	${_src}
273.if !target(${_src})
274${_src}:
275	touch ${.TARGET}
276.endif
277.endfor
278
279MFILES?= kern/bus_if.m kern/device_if.m dev/iicbus/iicbb_if.m \
280    dev/iicbus/iicbus_if.m isa/isa_if.m \
281    libkern/iconv_converter_if.m \
282    dev/mii/miibus_if.m \
283    dev/pccard/card_if.m dev/pccard/power_if.m dev/pci/pci_if.m \
284    dev/pci/pcib_if.m dev/ppbus/ppbus_if.m dev/smbus/smbus_if.m \
285    dev/usb/usb_if.m dev/sound/pcm/ac97_if.m dev/sound/pcm/channel_if.m \
286    dev/sound/pcm/feeder_if.m dev/sound/pcm/mixer_if.m pci/agp_if.m
287
288.for _srcsrc in ${MFILES}
289.for _ext in c h
290.for _src in ${SRCS:M${_srcsrc:T:R}.${_ext}}
291CLEANFILES+=	${_src}
292.if !target(${_src})
293.if !exists(@)
294${_src}: @
295.endif
296.if exists(@)
297${_src}: @/kern/makeobjops.pl @/${_srcsrc}
298.endif
299	perl @/kern/makeobjops.pl -${_ext} @/${_srcsrc}
300.endif
301.endfor # _src
302.endfor # _ext
303.endfor # _srcsrc
304
305.for _ext in c h
306.if ${SRCS:Mvnode_if.${_ext}} != ""
307CLEANFILES+=	vnode_if.${_ext}
308.if !exists(@)
309vnode_if.${_ext}: @
310.endif
311.if exists(@)
312vnode_if.${_ext}: @/kern/vnode_if.pl @/kern/vnode_if.src
313.endif
314	perl @/kern/vnode_if.pl -${_ext} @/kern/vnode_if.src
315.endif
316.endfor
317
318regress:
319
320.include <bsd.dep.mk>
321
322.if !exists(${DEPENDFILE})
323${OBJS}: ${SRCS:M*.h}
324.endif
325
326.include <bsd.obj.mk>
327
328.include <bsd.kern.mk>
329