kmod.mk revision 74849
1212904Sdim#	From: @(#)bsd.prog.mk	5.26 (Berkeley) 6/25/91
2212904Sdim# $FreeBSD: head/sys/conf/kmod.mk 74849 2001-03-27 11:50:44Z ru $
3353358Sdim#
4353358Sdim# The include file <bsd.kmod.mk> handles installing Kernel Loadable Device
5353358Sdim# drivers (KLD's).
6353358Sdim#
7212904Sdim#
8212904Sdim# +++ variables +++
9212904Sdim#
10212904Sdim# CLEANFILES	Additional files to remove for the clean and cleandir targets.
11212904Sdim#
12212904Sdim# DISTRIBUTION  Name of distribution. [bin]
13212904Sdim#
14212904Sdim# KMOD          The name of the kernel module to build.
15212904Sdim#
16212904Sdim# KMODDIR	Base path for kernel modules (see kld(4)). [/boot/kernel]
17212904Sdim#
18193323Sed# KMODOWN	KLD owner. [${BINOWN}]
19193323Sed#
20193323Sed# KMODGRP	KLD group. [${BINGRP}]
21193323Sed#
22360784Sdim# KMODMODE	KLD mode. [${BINMODE}]
23296417Sdim#
24212904Sdim# LINKS		The list of KLD links; should be full pathnames, the
25193323Sed#               linked-to file coming first, followed by the linked
26360784Sdim#               file.  The files are hard-linked.  For example, to link
27193323Sed#               /modules/master and /modules/meister, use:
28234353Sdim#
29234353Sdim#			LINKS=  /modules/master /modules/meister
30234353Sdim#
31234353Sdim# KMODLOAD	Command to load a kernel module [/sbin/kldload]
32234353Sdim#
33234353Sdim# KMODUNLOAD	Command to unload a kernel module [/sbin/kldunload]
34234353Sdim#
35202375Srdivacky# NOMAN		KLD does not have a manual page if set.
36193323Sed#
37193323Sed# PROG          The name of the kernel module to build.
38226633Sdim#		If not supplied, ${KMOD}.o is used.
39193323Sed#
40195098Sed# SRCS          List of source files
41210299Sed#
42210299Sed# SUBDIR        A list of subdirectories that should be built as well.
43198090Srdivacky#               Each of the targets will execute the same target in the
44201360Srdivacky#               subdirectories.
45261991Sdim#
46207618Srdivacky# SYMLINKS	Same as LINKS, except it creates symlinks and the
47195098Sed#		linked-to pathname may be relative.
48201360Srdivacky#
49193323Sed# DESTDIR, DISTDIR are set by other Makefiles (e.g. bsd.own.mk)
50224145Sdim#
51226633Sdim# MFILES	Optionally a list of interfaces used by the module.
52224145Sdim#		This file contains a default list of interfaces.
53224145Sdim#
54261991Sdim# +++ targets +++
55234353Sdim#
56234353Sdim#       distribute:
57239462Sdim#               This is a variant of install, which will
58234353Sdim#               put the stuff into the right "distribution".
59234353Sdim#
60234353Sdim# 	install:
61234353Sdim#               install the program and its manual pages; if the Makefile
62234353Sdim#               does not itself define the target install, the targets
63239462Sdim#               beforeinstall and afterinstall may also be used to cause
64234353Sdim#               actions immediately before and after the install target
65234353Sdim#		is executed.
66234353Sdim#
67234353Sdim# 	load:
68234353Sdim#		Load KLD.
69239462Sdim#
70234353Sdim# 	unload:
71234353Sdim#		Unload KLD.
72261991Sdim#
73198090Srdivacky# bsd.obj.mk: clean, cleandir and obj
74198090Srdivacky# bsd.dep.mk: cleandepend, depend and tags
75198090Srdivacky# bsd.man.mk: maninstall
76207618Srdivacky#
77198090Srdivacky
78198090SrdivackyKMODLOAD?=	/sbin/kldload
79201360SrdivackyKMODUNLOAD?=	/sbin/kldunload
80198090Srdivacky
81198090SrdivackyTARGET_ARCH?=	${MACHINE_ARCH}
82195098Sed
83195098Sed.if !target(__initialized__)
84195098Sed__initialized__:
85207618Srdivacky.if exists(${.CURDIR}/../Makefile.inc)
86195098Sed.include "${.CURDIR}/../Makefile.inc"
87195098Sed.endif
88201360Srdivacky.endif
89195098Sed
90234353Sdim.SUFFIXES: .out .o .c .cc .cxx .C .y .l .s .S
91234353Sdim
92234353SdimCFLAGS+=	${COPTS} -D_KERNEL ${CWARNFLAGS}
93234353SdimCFLAGS+=	-DKLD_MODULE
94234353Sdim
95234353Sdim# Don't use any standard or source-relative include directories.
96234353Sdim# Since -nostdinc will annull any previous -I paths, we repeat all
97234353Sdim# such paths after -nostdinc.  It doesn't seem to be possible to
98234353Sdim# add to the front of `make' variable.
99261991Sdim_ICFLAGS:=	${CFLAGS:M-I*}
100234353SdimCFLAGS+=	-nostdinc -I- ${INCLMAGIC} ${_ICFLAGS}
101234353Sdim
102234353Sdim# Add -I paths for system headers.  Individual KLD makefiles don't
103239462Sdim# need any -I paths for this.  Similar defaults for .PATH can't be
104234353Sdim# set because there are no standard paths for non-headers.
105234353SdimCFLAGS+=	-I. -I@ -I@/dev
106234353Sdim
107234353Sdim# Add a -I path to standard headers like <stddef.h>.  Use a relative
108261991Sdim# path to src/include if possible.  If the @ symlink hasn't been built
109234353Sdim# yet, then we can't tell if the relative path exists.  Add both the
110234353Sdim# potential relative path and an absolute path in that case.
111234353Sdim.if exists(@)
112239462Sdim.if exists(@/../include)
113234353SdimCFLAGS+=	-I@/../include
114234353Sdim.else
115234353SdimCFLAGS+=	-I${DESTDIR}/usr/include
116234353Sdim.endif
117261991Sdim.else # !@
118234353SdimCFLAGS+=	-I@/../include -I${DESTDIR}/usr/include
119234353Sdim.endif # @
120234353Sdim
121239462SdimCFLAGS+=	${DEBUG_FLAGS}
122234353Sdim
123234353Sdim.if ${OBJFORMAT} == elf
124234353SdimCLEANFILES+=	setdef0.c setdef1.c setdefs.h
125234353SdimCLEANFILES+=	setdef0.o setdef1.o
126234353Sdim.endif
127261991Sdim
128195098SedOBJS+=  ${SRCS:N*.h:R:S/$/.o/g}
129261991Sdim
130195098Sed.if !defined(PROG)
131207618SrdivackyPROG=	${KMOD}.ko
132195098Sed.endif
133212904Sdim
134212904Sdim${PROG}: ${KMOD}.kld
135212904Sdim.if ${OBJFORMAT} == elf
136226633Sdim	perl5 @/kern/gensetdefs.pl ${KMOD}.kld
137195098Sed	${CC} ${CFLAGS} -c setdef0.c
138195098Sed	${CC} ${CFLAGS} -c setdef1.c
139195098Sed	${LD} -Bshareable ${LDFLAGS} -o ${.TARGET} setdef0.o ${KMOD}.kld setdef1.o
140195098Sed.else
141261991Sdim	${LD} -Bshareable ${LDFLAGS} -o ${.TARGET} ${KMOD}.kld
142195098Sed.endif
143261991Sdim
144261991Sdim${KMOD}.kld: ${OBJS}
145261991Sdim	${LD} ${LDFLAGS} -r -o ${.TARGET} ${OBJS}
146261991Sdim
147261991Sdim.if !defined(NOMAN)
148261991Sdim.if 0
149261991SdimMAN?=	${KMOD}.4
150261991Sdim.endif
151261991Sdim.include <bsd.man.mk>
152261991Sdim.else
153261991Sdim.if !target(all-man)
154261991Sdimall-man: _SUBDIR
155261991Sdim.endif
156261991Sdim.if !target(maninstall)
157261991Sdimmaninstall: _SUBDIR
158261991Sdim.endif
159261991Sdim.endif
160261991Sdim
161261991Sdim_ILINKS=@ machine
162261991Sdim
163261991Sdim.MAIN: all
164261991Sdimall: objwarn ${PROG} all-man _SUBDIR
165261991Sdim
166261991Sdimbeforedepend: ${_ILINKS}
167261991Sdim	@rm -f .depend
168261991Sdim
169261991Sdim# Ensure that the links exist without depending on it when it exists which
170261991Sdim# causes all the modules to be rebuilt when the directory pointed to changes.
171261991Sdim.for _link in ${_ILINKS}
172261991Sdim.if !exists(${.OBJDIR}/${_link})
173261991Sdim${OBJS}: ${_link}
174261991Sdim.endif
175261991Sdim.endfor
176261991Sdim
177261991Sdim# Search for kernel source tree in standard places.
178261991Sdim.for _dir in ${.CURDIR}/../.. ${.CURDIR}/../../.. /sys /usr/src/sys
179193323Sed.if !defined(SYSDIR) && exists(${_dir}/kern/)
180193323SedSYSDIR=	${_dir}
181309124Sdim.endif
182309124Sdim.endfor
183309124Sdim.if !defined(SYSDIR) || !exists(${SYSDIR}/kern)
184309124Sdim.error "can't find kernel source tree"
185309124Sdim.endif
186309124Sdim
187309124Sdim${_ILINKS}:
188309124Sdim	@case ${.TARGET} in \
189309124Sdim	machine) \
190309124Sdim		path=${SYSDIR}/${MACHINE_ARCH}/include ;; \
191309124Sdim	@) \
192309124Sdim		path=${SYSDIR} ;; \
193309124Sdim	esac ; \
194309124Sdim	path=`(cd $$path && /bin/pwd)` ; \
195193323Sed	${ECHO} ${.TARGET} "->" $$path ; \
196243830Sdim	ln -s $$path ${.TARGET}
197193323Sed
198193323SedCLEANFILES+= ${PROG} ${KMOD}.kld ${OBJS} ${_ILINKS} symb.tmp tmp.o
199309124Sdim
200309124Sdim.if !target(install)
201309124Sdim.if !target(beforeinstall)
202193323Sedbeforeinstall:
203226633Sdim.endif
204226633Sdim.if !target(afterinstall)
205226633Sdimafterinstall:
206261991Sdim.endif
207261991Sdim
208226633Sdim_INSTALLFLAGS:=	-fschg ${INSTALLFLAGS}
209193323Sed.for ie in ${INSTALLFLAGS_EDIT}
210193323Sed_INSTALLFLAGS:=	${_INSTALLFLAGS${ie}}
211243830Sdim.endfor
212261991Sdim
213193323Sedrealinstall: _SUBDIR
214193323Sed	${INSTALL} ${COPY} -o ${KMODOWN} -g ${KMODGRP} -m ${KMODMODE} \
215193323Sed	    ${_INSTALLFLAGS} ${PROG} ${DESTDIR}${KMODDIR}
216243830Sdim.if defined(LINKS) && !empty(LINKS)
217261991Sdim	@set ${LINKS}; \
218193323Sed	while test $$# -ge 2; do \
219193323Sed		l=${DESTDIR}$$1; \
220243830Sdim		shift; \
221261991Sdim		t=${DESTDIR}$$1; \
222193323Sed		shift; \
223243830Sdim		${ECHO} $$t -\> $$l; \
224243830Sdim		ln -f $$l $$t; \
225243830Sdim	done; true
226261991Sdim.endif
227243830Sdim.if defined(SYMLINKS) && !empty(SYMLINKS)
228193323Sed	@set ${SYMLINKS}; \
229243830Sdim	while test $$# -ge 2; do \
230261991Sdim		l=$$1; \
231193323Sed		shift; \
232243830Sdim		t=${DESTDIR}$$1; \
233243830Sdim		shift; \
234243830Sdim		${ECHO} $$t -\> $$l; \
235261991Sdim		ln -fs $$l $$t; \
236243830Sdim	done; true
237261991Sdim.endif
238261991Sdim
239261991Sdiminstall: afterinstall _SUBDIR
240261991Sdim.if !defined(NOMAN)
241261991Sdimafterinstall: realinstall maninstall
242261991Sdim.else
243261991Sdimafterinstall: realinstall
244261991Sdim.endif
245261991Sdimrealinstall: beforeinstall
246261991Sdim.endif
247193323Sed
248243830SdimDISTRIBUTION?=	bin
249261991Sdim.if !target(distribute)
250193323Seddistribute: _SUBDIR
251193323Sed.for dist in ${DISTRIBUTION}
252243830Sdim	cd ${.CURDIR} ; $(MAKE) install DESTDIR=${DISTDIR}/${dist} SHARED=copies
253261991Sdim.endfor
254193323Sed.endif
255193323Sed
256243830Sdim.if !target(load)
257261991Sdimload:	${PROG}
258193323Sed	${KMODLOAD} -v ./${KMOD}.ko
259193323Sed.endif
260243830Sdim
261261991Sdim.if !target(unload)
262193323Sedunload:
263193323Sed	${KMODUNLOAD} -v ${KMOD}
264243830Sdim.endif
265261991Sdim
266193323Sed.for _src in ${SRCS:Mopt_*.h}
267193323SedCLEANFILES+=	${_src}
268243830Sdim.if !target(${_src})
269261991Sdim${_src}:
270193323Sed	touch ${.TARGET}
271193323Sed.endif
272243830Sdim.endfor
273261991Sdim
274193323SedMFILES?= kern/bus_if.m kern/device_if.m dev/iicbus/iicbb_if.m \
275193323Sed    dev/iicbus/iicbus_if.m isa/isa_if.m dev/mii/miibus_if.m \
276193323Sed    dev/pccard/card_if.m dev/pccard/power_if.m dev/pci/pci_if.m \
277193323Sed    dev/pci/pcib_if.m dev/ppbus/ppbus_if.m dev/smbus/smbus_if.m \
278261991Sdim    dev/usb/usb_if.m dev/sound/pcm/ac97_if.m dev/sound/pcm/channel_if.m \
279193323Sed    dev/sound/pcm/feeder_if.m dev/sound/pcm/mixer_if.m pci/agp_if.m
280193323Sed
281193323Sed.for _srcsrc in ${MFILES}
282193323Sed.for _ext in c h
283261991Sdim.for _src in ${SRCS:M${_srcsrc:T:R}.${_ext}}
284261991SdimCLEANFILES+=	${_src}
285193323Sed.if !target(${_src})
286234353Sdim.if !exists(@)
287234353Sdim${_src}: @
288234353Sdim.endif
289193323Sed.if exists(@)
290360784Sdim${_src}: @/kern/makeobjops.pl @/${_srcsrc}
291193323Sed.endif
292193323Sed	perl @/kern/makeobjops.pl -${_ext} @/${_srcsrc}
293.endif
294.endfor # _src
295.endfor # _ext
296.endfor # _srcsrc
297
298.for _ext in c h
299.if ${SRCS:Mvnode_if.${_ext}} != ""
300CLEANFILES+=	vnode_if.${_ext}
301.if !exists(@)
302vnode_if.${_ext}: @
303.endif
304.if exists(@)
305vnode_if.${_ext}: @/kern/vnode_if.pl @/kern/vnode_if.src
306.endif
307	perl @/kern/vnode_if.pl -${_ext} @/kern/vnode_if.src
308.endif
309.endfor
310
311regress:
312
313.include <bsd.dep.mk>
314
315.if !exists(${DEPENDFILE})
316${OBJS}: ${SRCS:M*.h}
317.endif
318
319.include <bsd.obj.mk>
320
321.include <bsd.kern.mk>
322