kmod.mk revision 72935
1193323Sed#	From: @(#)bsd.prog.mk	5.26 (Berkeley) 6/25/91
2193323Sed# $FreeBSD: head/sys/conf/kmod.mk 72935 2001-02-23 04:49:31Z imp $
3193323Sed#
4193323Sed# The include file <bsd.kmod.mk> handles installing Kernel Loadable Device
5193323Sed# drivers (KLD's).
6193323Sed#
7193323Sed#
8193323Sed# +++ variables +++
9193323Sed#
10193323Sed# CLEANFILES	Additional files to remove for the clean and cleandir targets.
11193323Sed#
12193323Sed# DISTRIBUTION  Name of distribution. [bin]
13193323Sed#
14193323Sed# KMOD          The name of the kernel module to build.
15193323Sed#
16193323Sed# KMODDIR	Base path for kernel modules (see kld(4)). [/boot/kernel]
17193323Sed#
18193323Sed# KMODOWN	KLD owner. [${BINOWN}]
19193323Sed#
20193323Sed# KMODGRP	KLD group. [${BINGRP}]
21193323Sed#
22193323Sed# KMODMODE	KLD mode. [${BINMODE}]
23193323Sed#
24193323Sed# LINKS		The list of KLD links; should be full pathnames, the
25193323Sed#               linked-to file coming first, followed by the linked
26193323Sed#               file.  The files are hard-linked.  For example, to link
27198090Srdivacky#               /modules/master and /modules/meister, use:
28193323Sed#
29193323Sed#			LINKS=  /modules/master /modules/meister
30193323Sed#
31193323Sed# KMODLOAD	Command to load a kernel module [/sbin/kldload]
32198090Srdivacky#
33193323Sed# KMODUNLOAD	Command to unload a kernel module [/sbin/kldunload]
34193323Sed#
35193323Sed# NOMAN		KLD does not have a manual page if set.
36193323Sed#
37193323Sed# PROG          The name of the kernel module to build.
38193323Sed#		If not supplied, ${KMOD}.o is used.
39193323Sed#
40193323Sed# SRCS          List of source files
41193323Sed#
42218893Sdim# SUBDIR        A list of subdirectories that should be built as well.
43218893Sdim#               Each of the targets will execute the same target in the
44193323Sed#               subdirectories.
45193323Sed#
46193323Sed# SYMLINKS	Same as LINKS, except it creates symlinks and the
47198892Srdivacky#		linked-to pathname may be relative.
48193323Sed#
49193323Sed# DESTDIR, DISTDIR are set by other Makefiles (e.g. bsd.own.mk)
50193323Sed#
51193323Sed# MFILES	Optionally a list of interfaces used by the module.
52221345Sdim#		This file contains a default list of interfaces.
53193323Sed#
54206083Srdivacky# +++ targets +++
55193323Sed#
56193323Sed#       distribute:
57193323Sed#               This is a variant of install, which will
58193323Sed#               put the stuff into the right "distribution".
59193323Sed#
60193323Sed# 	install:
61193323Sed#               install the program and its manual pages; if the Makefile
62193323Sed#               does not itself define the target install, the targets
63193323Sed#               beforeinstall and afterinstall may also be used to cause
64193323Sed#               actions immediately before and after the install target
65193323Sed#		is executed.
66193323Sed#
67193323Sed# 	load:
68193323Sed#		Load KLD.
69193323Sed#
70193323Sed# 	unload:
71193323Sed#		Unload KLD.
72193323Sed#
73193323Sed# bsd.obj.mk: clean, cleandir and obj
74193323Sed# bsd.dep.mk: cleandepend, depend and tags
75193323Sed# bsd.man.mk: maninstall
76206083Srdivacky#
77198090Srdivacky
78193323SedKMODLOAD?=	/sbin/kldload
79193323SedKMODUNLOAD?=	/sbin/kldunload
80193323Sed
81193323Sed.if !target(__initialized__)
82193323Sed__initialized__:
83193323Sed.if exists(${.CURDIR}/../Makefile.inc)
84193323Sed.include "${.CURDIR}/../Makefile.inc"
85193323Sed.endif
86193323Sed.endif
87193323Sed
88193323Sed.SUFFIXES: .out .o .c .cc .cxx .C .y .l .s .S
89193323Sed
90193323SedCFLAGS+=	${COPTS} -D_KERNEL ${CWARNFLAGS}
91193323SedCFLAGS+=	-DKLD_MODULE
92193323Sed
93193323Sed# Don't use any standard or source-relative include directories.
94193323Sed# Since -nostdinc will annull any previous -I paths, we repeat all
95193323Sed# such paths after -nostdinc.  It doesn't seem to be possible to
96193323Sed# add to the front of `make' variable.
97193323Sed_ICFLAGS:=	${CFLAGS:M-I*}
98193323SedCFLAGS+=	-nostdinc -I- ${INCLMAGIC} ${_ICFLAGS}
99193323Sed
100193323Sed# Add -I paths for system headers.  Individual KLD makefiles don't
101193323Sed# need any -I paths for this.  Similar defaults for .PATH can't be
102193323Sed# set because there are no standard paths for non-headers.
103193323SedCFLAGS+=	-I. -I@ -I@/dev
104193323Sed
105193323Sed# Add a -I path to standard headers like <stddef.h>.  Use a relative
106193323Sed# path to src/include if possible.  If the @ symlink hasn't been built
107193323Sed# yet, then we can't tell if the relative path exists.  Add both the
108193323Sed# potential relative path and an absolute path in that case.
109193323Sed.if exists(@)
110193323Sed.if exists(@/../include)
111193323SedCFLAGS+=	-I@/../include
112193323Sed.else
113193323SedCFLAGS+=	-I${DESTDIR}/usr/include
114193323Sed.endif
115193323Sed.else # !@
116193323SedCFLAGS+=	-I@/../include -I${DESTDIR}/usr/include
117193323Sed.endif # @
118193323Sed
119193323SedCFLAGS+=	${DEBUG_FLAGS}
120193323Sed
121193323Sed.if ${OBJFORMAT} == elf
122193323SedCLEANFILES+=	setdef0.c setdef1.c setdefs.h
123193323SedCLEANFILES+=	setdef0.o setdef1.o
124210299Sed.endif
125210299Sed
126212904SdimOBJS+=  ${SRCS:N*.h:R:S/$/.o/g}
127210299Sed
128193323Sed.if !defined(PROG)
129193323SedPROG=	${KMOD}.ko
130218893Sdim.endif
131218893Sdim
132218893Sdim${PROG}: ${KMOD}.kld
133210299Sed.if ${OBJFORMAT} == elf
134193323Sed	perl5 @/kern/gensetdefs.pl ${KMOD}.kld
135193323Sed	${CC} ${CFLAGS} -c setdef0.c
136193323Sed	${CC} ${CFLAGS} -c setdef1.c
137193323Sed	${LD} -Bshareable ${LDFLAGS} -o ${.TARGET} setdef0.o ${KMOD}.kld setdef1.o
138193323Sed.else
139193323Sed	${LD} -Bshareable ${LDFLAGS} -o ${.TARGET} ${KMOD}.kld
140206083Srdivacky.endif
141193323Sed
142206083Srdivacky${KMOD}.kld: ${OBJS}
143193323Sed	${LD} ${LDFLAGS} -r -o ${.TARGET} ${OBJS}
144206083Srdivacky
145193323Sed.if !defined(NOMAN)
146193323Sed.include <bsd.man.mk>
147193323Sed.if !defined(_MANPAGES) || empty(_MANPAGES)
148193323SedMAN1=	${KMOD}.4
149193323Sed.endif
150193323Sed
151193323Sed.elif !target(maninstall)
152218893Sdimmaninstall: _SUBDIR
153193323Sedall-man:
154193323Sed.endif
155193323Sed
156193323Sed_ILINKS=@ machine
157193323Sed
158218893Sdim.MAIN: all
159193323Sedall: objwarn ${PROG} all-man _SUBDIR
160193323Sed
161193323Sed# Ensure that the links exist without depending on it when it exists which
162193323Sed# causes all the modules to be rebuilt when the directory pointed to changes.
163193323Sed.for _link in ${_ILINKS}
164193323Sed.if !exists(${.OBJDIR}/${_link})
165193323Sed${OBJS}: ${_link}
166212904Sdimbeforedepend: ${_link}
167210299Sed	@rm -f .depend
168193323Sed.endif
169193323Sed.endfor
170193323Sed
171193323Sed# Search for kernel source tree in standard places.
172193323Sed.for _dir in ${.CURDIR}/../.. ${.CURDIR}/../../.. /sys /usr/src/sys
173212904Sdim.if !defined(SYSDIR) && exists(${_dir}/kern/)
174212904SdimSYSDIR=	${_dir}
175218893Sdim.endif
176193323Sed.endfor
177193323Sed.if !defined(SYSDIR) || !exists(${SYSDIR}/kern)
178193323Sed.error "can't find kernel source tree"
179193323Sed.endif
180193323Sed
181193323Sed${_ILINKS}:
182193323Sed	@case ${.TARGET} in \
183193323Sed	machine) \
184193323Sed		path=${SYSDIR}/${MACHINE_ARCH}/include ;; \
185193323Sed	@) \
186193323Sed		path=${SYSDIR} ;; \
187193323Sed	esac ; \
188193323Sed	path=`(cd $$path && /bin/pwd)` ; \
189193323Sed	${ECHO} ${.TARGET} "->" $$path ; \
190194178Sed	ln -s $$path ${.TARGET}
191194178Sed
192193323SedCLEANFILES+= ${PROG} ${KMOD}.kld ${OBJS} ${_ILINKS} symb.tmp tmp.o
193193323Sed
194193323Sed.if !target(install)
195193323Sed.if !target(beforeinstall)
196193323Sedbeforeinstall:
197193323Sed.endif
198193323Sed.if !target(afterinstall)
199193323Sedafterinstall:
200193323Sed.endif
201193323Sed
202193323Sed_INSTALLFLAGS:=	${INSTALLFLAGS}
203193323Sed.for ie in ${INSTALLFLAGS_EDIT}
204193323Sed_INSTALLFLAGS:=	${_INSTALLFLAGS${ie}}
205193323Sed.endfor
206193323Sed
207193323Sedrealinstall: _SUBDIR
208193323Sed	${INSTALL} ${COPY} -o ${KMODOWN} -g ${KMODGRP} -m ${KMODMODE} \
209226633Sdim	    ${_INSTALLFLAGS} ${PROG} ${DESTDIR}${KMODDIR}
210206083Srdivacky.if defined(LINKS) && !empty(LINKS)
211224145Sdim	@set ${LINKS}; \
212198090Srdivacky	while test $$# -ge 2; do \
213198090Srdivacky		l=${DESTDIR}$$1; \
214193323Sed		shift; \
215193323Sed		t=${DESTDIR}$$1; \
216193323Sed		shift; \
217193323Sed		${ECHO} $$t -\> $$l; \
218193323Sed		ln -f $$l $$t; \
219193323Sed	done; true
220193323Sed.endif
221193323Sed.if defined(SYMLINKS) && !empty(SYMLINKS)
222193323Sed	@set ${SYMLINKS}; \
223193323Sed	while test $$# -ge 2; do \
224193323Sed		l=$$1; \
225193323Sed		shift; \
226193323Sed		t=${DESTDIR}$$1; \
227212904Sdim		shift; \
228193323Sed		${ECHO} $$t -\> $$l; \
229193323Sed		ln -fs $$l $$t; \
230193323Sed	done; true
231212904Sdim.endif
232193323Sed
233193323Sedinstall: afterinstall _SUBDIR
234193323Sed.if !defined(NOMAN)
235193323Sedafterinstall: realinstall maninstall
236193323Sed.else
237193323Sedafterinstall: realinstall
238193323Sed.endif
239206083Srdivackyrealinstall: beforeinstall
240193323Sed.endif
241193323Sed
242193323SedDISTRIBUTION?=	bin
243193323Sed.if !target(distribute)
244193323Seddistribute: _SUBDIR
245193323Sed.for dist in ${DISTRIBUTION}
246193323Sed	cd ${.CURDIR} ; $(MAKE) install DESTDIR=${DISTDIR}/${dist} SHARED=copies
247224145Sdim.endfor
248193323Sed.endif
249193323Sed
250193323Sed.if !target(load)
251224145Sdimload:	${PROG}
252193323Sed	${KMODLOAD} -v ./${KMOD}.ko
253193323Sed.endif
254193323Sed
255193323Sed.if !target(unload)
256193323Sedunload:
257212904Sdim	${KMODUNLOAD} -v ${KMOD}
258207618Srdivacky.endif
259193323Sed
260193323Sed.for _src in ${SRCS:Mopt_*.h}
261193323SedCLEANFILES+=	${_src}
262193323Sed.if !target(${_src})
263193323Sed${_src}:
264193323Sed	touch ${.TARGET}
265193323Sed.endif
266193323Sed.endfor
267193323Sed
268193323SedMFILES?= kern/bus_if.m kern/device_if.m dev/iicbus/iicbb_if.m \
269193323Sed    dev/iicbus/iicbus_if.m isa/isa_if.m dev/mii/miibus_if.m \
270193323Sed    dev/pccard/card_if.m dev/pccard/power_if.m dev/pci/pci_if.m \
271193323Sed    dev/pci/pcib_if.m dev/ppbus/ppbus_if.m dev/smbus/smbus_if.m \
272193323Sed    dev/usb/usb_if.m dev/sound/pcm/ac97_if.m dev/sound/pcm/channel_if.m \
273193323Sed    dev/sound/pcm/feeder_if.m dev/sound/pcm/mixer_if.m
274193323Sed
275193323Sed.for _srcsrc in ${MFILES}
276221345Sdim.for _ext in c h
277221345Sdim.for _src in ${SRCS:M${_srcsrc:T:R}.${_ext}}
278193323SedCLEANFILES+=	${_src}
279193323Sed.if !target(${_src})
280193323Sed.if !exists(@)
281193323Sed${_src}: @
282193323Sed.endif
283193323Sed.if exists(@)
284193323Sed${_src}: @/kern/makeobjops.pl @/${_srcsrc}
285193323Sed.endif
286193323Sed	perl @/kern/makeobjops.pl -${_ext} @/${_srcsrc}
287193323Sed.endif
288193323Sed.endfor # _src
289193323Sed.endfor # _ext
290193323Sed.endfor # _srcsrc
291193323Sed
292218893Sdim.for _ext in c h
293218893Sdim.if ${SRCS:Mvnode_if.${_ext}} != ""
294218893SdimCLEANFILES+=	vnode_if.${_ext}
295218893Sdim.if !exists(@)
296218893Sdimvnode_if.${_ext}: @
297221345Sdim.endif
298218893Sdim.if exists(@)
299218893Sdimvnode_if.${_ext}: @/kern/vnode_if.pl @/kern/vnode_if.src
300218893Sdim.endif
301218893Sdim	perl @/kern/vnode_if.pl -${_ext} @/kern/vnode_if.src
302218893Sdim.endif
303218893Sdim.endfor
304218893Sdim
305218893Sdimregress:
306218893Sdim
307218893Sdim.include <bsd.dep.mk>
308218893Sdim
309218893Sdim.if !exists(${DEPENDFILE})
310218893Sdim${OBJS}: ${SRCS:M*.h}
311218893Sdim.endif
312218893Sdim
313218893Sdim.include <bsd.obj.mk>
314218893Sdim
315218893Sdim.include <bsd.kern.mk>
316218893Sdim