kmod.mk revision 85570
1178825Sdfr#	From: @(#)bsd.prog.mk	5.26 (Berkeley) 6/25/91
2233294Sstas# $FreeBSD: head/sys/conf/kmod.mk 85570 2001-10-27 00:52:50Z des $
3233294Sstas#
4233294Sstas# The include file <bsd.kmod.mk> handles installing Kernel Loadable Device
5178825Sdfr# drivers (KLD's).
6233294Sstas#
7233294Sstas#
8233294Sstas# +++ variables +++
9178825Sdfr#
10233294Sstas# CLEANFILES	Additional files to remove for the clean and cleandir targets.
11233294Sstas#
12178825Sdfr# DISTRIBUTION  Name of distribution. [bin]
13233294Sstas#
14233294Sstas# KMOD          The name of the kernel module to build.
15233294Sstas#
16178825Sdfr# KMODDIR	Base path for kernel modules (see kld(4)). [/boot/kernel]
17233294Sstas#
18233294Sstas# KMODOWN	KLD owner. [${BINOWN}]
19233294Sstas#
20178825Sdfr# KMODGRP	KLD group. [${BINGRP}]
21233294Sstas#
22233294Sstas# KMODMODE	KLD mode. [${BINMODE}]
23233294Sstas#
24233294Sstas# LINKS		The list of KLD links; should be full pathnames, the
25233294Sstas#               linked-to file coming first, followed by the linked
26233294Sstas#               file.  The files are hard-linked.  For example, to link
27233294Sstas#               /modules/master and /modules/meister, use:
28233294Sstas#
29233294Sstas#			LINKS=  /modules/master /modules/meister
30233294Sstas#
31233294Sstas# KMODLOAD	Command to load a kernel module [/sbin/kldload]
32178825Sdfr#
33178825Sdfr# KMODUNLOAD	Command to unload a kernel module [/sbin/kldunload]
34233294Sstas#
35178825Sdfr# NOMAN		KLD does not have a manual page if set.
36178825Sdfr#
37178825Sdfr# PROG          The name of the kernel module to build.
38178825Sdfr#		If not supplied, ${KMOD}.o is used.
39178825Sdfr#
40178825Sdfr# SRCS          List of source files
41178825Sdfr#
42233294Sstas# SUBDIR        A list of subdirectories that should be built as well.
43178825Sdfr#               Each of the targets will execute the same target in the
44233294Sstas#               subdirectories.
45178825Sdfr#
46178825Sdfr# SYMLINKS	Same as LINKS, except it creates symlinks and the
47178825Sdfr#		linked-to pathname may be relative.
48233294Sstas#
49233294Sstas# DESTDIR, DISTDIR are set by other Makefiles (e.g. bsd.own.mk)
50233294Sstas#
51233294Sstas# MFILES	Optionally a list of interfaces used by the module.
52178825Sdfr#		This file contains a default list of interfaces.
53178825Sdfr#
54178825Sdfr# +++ targets +++
55178825Sdfr#
56178825Sdfr#       distribute:
57178825Sdfr#               This is a variant of install, which will
58178825Sdfr#               put the stuff into the right "distribution".
59178825Sdfr#
60178825Sdfr# 	install:
61178825Sdfr#               install the program and its manual pages; if the Makefile
62178825Sdfr#               does not itself define the target install, the targets
63178825Sdfr#               beforeinstall and afterinstall may also be used to cause
64178825Sdfr#               actions immediately before and after the install target
65178825Sdfr#		is executed.
66178825Sdfr#
67178825Sdfr# 	load:
68178825Sdfr#		Load KLD.
69178825Sdfr#
70178825Sdfr# 	unload:
71178825Sdfr#		Unload KLD.
72178825Sdfr#
73233294Sstas# bsd.obj.mk: clean, cleandir and obj
74178825Sdfr# bsd.dep.mk: cleandepend, depend and tags
75178825Sdfr# bsd.man.mk: maninstall
76178825Sdfr#
77178825Sdfr
78178825SdfrKMODLOAD?=	/sbin/kldload
79178825SdfrKMODUNLOAD?=	/sbin/kldunload
80178825SdfrOBJCOPY?=	objcopy
81178825Sdfr
82178825SdfrTARGET_ARCH?=	${MACHINE_ARCH}
83178825Sdfr
84178825Sdfr.if !target(__initialized__)
85178825Sdfr__initialized__:
86233294Sstas.if exists(${.CURDIR}/../Makefile.inc)
87233294Sstas.include "${.CURDIR}/../Makefile.inc"
88178825Sdfr.endif
89178825Sdfr.endif
90178825Sdfr
91178825Sdfr.SUFFIXES: .out .o .c .cc .cxx .C .y .l .s .S
92178825Sdfr
93178825SdfrCFLAGS+=	${COPTS} -D_KERNEL ${CWARNFLAGS}
94178825SdfrCFLAGS+=	-DKLD_MODULE
95178825Sdfr
96178825Sdfr# Don't use any standard or source-relative include directories.
97178825Sdfr# Since -nostdinc will annull any previous -I paths, we repeat all
98178825Sdfr# such paths after -nostdinc.  It doesn't seem to be possible to
99178825Sdfr# add to the front of `make' variable.
100178825Sdfr_ICFLAGS:=	${CFLAGS:M-I*}
101178825SdfrCFLAGS+=	-nostdinc -I- ${INCLMAGIC} ${_ICFLAGS}
102178825Sdfr
103178825Sdfr# Add -I paths for system headers.  Individual KLD makefiles don't
104178825Sdfr# need any -I paths for this.  Similar defaults for .PATH can't be
105178825Sdfr# set because there are no standard paths for non-headers.
106178825SdfrCFLAGS+=	-I. -I@ -I@/dev
107178825Sdfr
108178825Sdfr# Add a -I path to standard headers like <stddef.h>.  Use a relative
109178825Sdfr# path to src/include if possible.  If the @ symlink hasn't been built
110178825Sdfr# yet, then we can't tell if the relative path exists.  Add both the
111178825Sdfr# potential relative path and an absolute path in that case.
112178825Sdfr.if exists(@)
113178825Sdfr.if exists(@/../include)
114178825SdfrCFLAGS+=	-I@/../include
115178825Sdfr.else
116178825SdfrCFLAGS+=	-I${DESTDIR}/usr/include
117178825Sdfr.endif
118178825Sdfr.else # !@
119178825SdfrCFLAGS+=	-I@/../include -I${DESTDIR}/usr/include
120178825Sdfr.endif # @
121178825Sdfr
122178825SdfrCFLAGS+=	${DEBUG_FLAGS}
123178825Sdfr
124178825Sdfr.if ${OBJFORMAT} == elf
125178825SdfrCLEANFILES+=	setdef0.c setdef1.c setdefs.h
126178825SdfrCLEANFILES+=	setdef0.o setdef1.o
127178825Sdfr.endif
128178825Sdfr
129178825SdfrOBJS+=  ${SRCS:N*.h:R:S/$/.o/g}
130178825Sdfr
131178825Sdfr.if !defined(PROG)
132178825SdfrPROG=	${KMOD}.ko
133178825Sdfr.endif
134178825Sdfr
135178825Sdfr.if !defined(DEBUG)
136233294SstasFULLPROG=	${PROG}
137233294Sstas.else
138233294SstasFULLPROG=	${PROG}.debug
139178825Sdfr${PROG}: ${FULLPROG}
140178825Sdfr	${OBJCOPY} --strip-debug ${FULLPROG} ${PROG}
141178825Sdfr.endif
142178825Sdfr
143178825Sdfr${FULLPROG}: ${KMOD}.kld
144178825Sdfr	${LD} -Bshareable ${LDFLAGS} -o ${.TARGET} ${KMOD}.kld
145178825Sdfr
146178825Sdfr${KMOD}.kld: ${OBJS}
147178825Sdfr	${LD} ${LDFLAGS} -r -o ${.TARGET} ${OBJS}
148233294Sstas
149178825Sdfr.if !defined(NOMAN)
150178825Sdfr.if 0
151178825SdfrMAN?=	${KMOD}.4
152233294Sstas.endif
153233294Sstas.include <bsd.man.mk>
154178825Sdfr.else
155178825Sdfr.if !target(all-man)
156178825Sdfrall-man: _SUBDIR
157178825Sdfr.endif
158178825Sdfr.if !target(maninstall)
159233294Sstasmaninstall: _SUBDIR
160178825Sdfr.endif
161178825Sdfr.endif
162178825Sdfr
163178825Sdfr_ILINKS=@ machine
164233294Sstas
165178825Sdfr.MAIN: all
166178825Sdfrall: objwarn ${PROG} all-man _SUBDIR
167178825Sdfr
168178825Sdfrbeforedepend: ${_ILINKS}
169233294Sstas	@rm -f .depend
170178825Sdfr
171178825Sdfr# Ensure that the links exist without depending on it when it exists which
172178825Sdfr# causes all the modules to be rebuilt when the directory pointed to changes.
173178825Sdfr.for _link in ${_ILINKS}
174178825Sdfr.if !exists(${.OBJDIR}/${_link})
175178825Sdfr${OBJS}: ${_link}
176178825Sdfr.endif
177178825Sdfr.endfor
178178825Sdfr
179178825Sdfr# Search for kernel source tree in standard places.
180178825Sdfr.for _dir in ${.CURDIR}/../.. ${.CURDIR}/../../.. /sys /usr/src/sys
181178825Sdfr.if !defined(SYSDIR) && exists(${_dir}/kern/)
182178825SdfrSYSDIR=	${_dir}
183178825Sdfr.endif
184178825Sdfr.endfor
185178825Sdfr.if !defined(SYSDIR) || !exists(${SYSDIR}/kern)
186178825Sdfr.error "can't find kernel source tree"
187178825Sdfr.endif
188178825Sdfr
189178825Sdfr${_ILINKS}:
190178825Sdfr	@case ${.TARGET} in \
191178825Sdfr	machine) \
192178825Sdfr		path=${SYSDIR}/${MACHINE_ARCH}/include ;; \
193178825Sdfr	@) \
194178825Sdfr		path=${SYSDIR} ;; \
195178825Sdfr	esac ; \
196178825Sdfr	path=`(cd $$path && /bin/pwd)` ; \
197178825Sdfr	${ECHO} ${.TARGET} "->" $$path ; \
198233294Sstas	ln -s $$path ${.TARGET}
199233294Sstas
200233294SstasCLEANFILES+= ${PROG} ${FULLPROG} ${KMOD}.kld ${OBJS} ${_ILINKS} symb.tmp tmp.o
201233294Sstas
202233294Sstas.if !target(install)
203233294Sstas.if !target(beforeinstall)
204233294Sstasbeforeinstall:
205233294Sstas.endif
206233294Sstas.if !target(afterinstall)
207233294Sstasafterinstall:
208233294Sstas.endif
209233294Sstas
210178825Sdfr_INSTALLFLAGS:=	${INSTALLFLAGS}
211178825Sdfr.for ie in ${INSTALLFLAGS_EDIT}
212178825Sdfr_INSTALLFLAGS:=	${_INSTALLFLAGS${ie}}
213178825Sdfr.endfor
214178825Sdfr
215178825Sdfrinstall.debug: _SUBDIR
216178825Sdfr	${INSTALL} ${COPY} -o ${KMODOWN} -g ${KMODGRP} -m ${KMODMODE} \
217178825Sdfr	    ${_INSTALLFLAGS} ${FULLPROG} ${DESTDIR}${KMODDIR}/
218178825Sdfr
219178825Sdfrrealinstall: _SUBDIR
220178825Sdfr	${INSTALL} ${COPY} -o ${KMODOWN} -g ${KMODGRP} -m ${KMODMODE} \
221	    ${_INSTALLFLAGS} ${PROG} ${DESTDIR}${KMODDIR}/
222.if defined(LINKS) && !empty(LINKS)
223	@set ${LINKS}; \
224	while test $$# -ge 2; do \
225		l=${DESTDIR}$$1; \
226		shift; \
227		t=${DESTDIR}$$1; \
228		shift; \
229		${ECHO} $$t -\> $$l; \
230		ln -f $$l $$t; \
231	done; true
232.endif
233.if defined(SYMLINKS) && !empty(SYMLINKS)
234	@set ${SYMLINKS}; \
235	while test $$# -ge 2; do \
236		l=$$1; \
237		shift; \
238		t=${DESTDIR}$$1; \
239		shift; \
240		${ECHO} $$t -\> $$l; \
241		ln -fs $$l $$t; \
242	done; true
243.endif
244.if !defined(NO_XREF)
245	-kldxref ${DESTDIR}${KMODDIR}
246.endif
247
248install: afterinstall _SUBDIR
249.if !defined(NOMAN)
250afterinstall: realinstall maninstall
251.else
252afterinstall: realinstall
253.endif
254realinstall: beforeinstall
255.endif
256
257DISTRIBUTION?=	bin
258.if !target(distribute)
259distribute: _SUBDIR
260.for dist in ${DISTRIBUTION}
261	cd ${.CURDIR} ; $(MAKE) install DESTDIR=${DISTDIR}/${dist} SHARED=copies
262.endfor
263.endif
264
265.if !target(load)
266load:	${PROG}
267	${KMODLOAD} -v ${.CURDIR}/${KMOD}.ko
268.endif
269
270.if !target(unload)
271unload:
272	${KMODUNLOAD} -v ${KMOD}
273.endif
274
275.for _src in ${SRCS:Mopt_*.h}
276CLEANFILES+=	${_src}
277.if !target(${_src})
278${_src}:
279	touch ${.TARGET}
280.endif
281.endfor
282
283MFILES?= kern/bus_if.m kern/device_if.m dev/iicbus/iicbb_if.m \
284    dev/iicbus/iicbus_if.m isa/isa_if.m \
285    libkern/iconv_converter_if.m \
286    dev/mii/miibus_if.m \
287    dev/pccard/card_if.m dev/pccard/power_if.m dev/pci/pci_if.m \
288    dev/pci/pcib_if.m dev/ppbus/ppbus_if.m dev/smbus/smbus_if.m \
289    dev/usb/usb_if.m dev/sound/pcm/ac97_if.m dev/sound/pcm/channel_if.m \
290    dev/sound/pcm/feeder_if.m dev/sound/pcm/mixer_if.m pci/agp_if.m
291
292.for _srcsrc in ${MFILES}
293.for _ext in c h
294.for _src in ${SRCS:M${_srcsrc:T:R}.${_ext}}
295CLEANFILES+=	${_src}
296.if !target(${_src})
297.if !exists(@)
298${_src}: @
299.endif
300.if exists(@)
301${_src}: @/kern/makeobjops.pl @/${_srcsrc}
302.endif
303	perl @/kern/makeobjops.pl -${_ext} @/${_srcsrc}
304.endif
305.endfor # _src
306.endfor # _ext
307.endfor # _srcsrc
308
309.for _ext in c h
310.if ${SRCS:Mvnode_if.${_ext}} != ""
311CLEANFILES+=	vnode_if.${_ext}
312.if !exists(@)
313vnode_if.${_ext}: @
314.endif
315.if exists(@)
316vnode_if.${_ext}: @/kern/vnode_if.pl @/kern/vnode_if.src
317.endif
318	perl @/kern/vnode_if.pl -${_ext} @/kern/vnode_if.src
319.endif
320.endfor
321
322regress:
323
324.include <bsd.dep.mk>
325
326.if !exists(${DEPENDFILE})
327${OBJS}: ${SRCS:M*.h}
328.endif
329
330.include <bsd.obj.mk>
331
332.include <bsd.kern.mk>
333