kmod.mk revision 89260
1117521Snjl#	From: @(#)bsd.prog.mk	5.26 (Berkeley) 6/25/91
2138287Smarks# $FreeBSD: head/sys/conf/kmod.mk 89260 2002-01-11 15:49:02Z ru $
3138287Smarks#
4138287Smarks# The include file <bsd.kmod.mk> handles installing Kernel Loadable Device
5138287Smarks# drivers (KLD's).
6138287Smarks#
7138287Smarks#
8138287Smarks# +++ variables +++
9138287Smarks#
10138287Smarks# CLEANFILES	Additional files to remove for the clean and cleandir targets.
11138287Smarks#
12138287Smarks# DISTRIBUTION  Name of distribution. [bin]
13138287Smarks#
14138287Smarks# KMOD          The name of the kernel module to build.
15138287Smarks#
16138287Smarks# KMODDIR	Base path for kernel modules (see kld(4)). [/boot/kernel]
17138287Smarks#
18138287Smarks# KMODOWN	KLD owner. [${BINOWN}]
19138287Smarks#
20138287Smarks# KMODGRP	KLD group. [${BINGRP}]
21138287Smarks#
22138287Smarks# KMODMODE	KLD mode. [${BINMODE}]
23138287Smarks#
24138287Smarks# LINKS		The list of KLD links; should be full pathnames, the
25138287Smarks#               linked-to file coming first, followed by the linked
26138287Smarks#               file.  The files are hard-linked.  For example, to link
27138287Smarks#               /modules/master and /modules/meister, use:
28138287Smarks#
29138287Smarks#			LINKS=  /modules/master /modules/meister
30138287Smarks#
31138287Smarks# KMODLOAD	Command to load a kernel module [/sbin/kldload]
32138287Smarks#
33138287Smarks# KMODUNLOAD	Command to unload a kernel module [/sbin/kldunload]
34138287Smarks#
35138287Smarks# PROG          The name of the kernel module to build.
36138287Smarks#		If not supplied, ${KMOD}.o is used.
37138287Smarks#
38138287Smarks# SRCS          List of source files
39138287Smarks#
40138287Smarks# SUBDIR        A list of subdirectories that should be built as well.
41138287Smarks#               Each of the targets will execute the same target in the
42138287Smarks#               subdirectories.
43138287Smarks#
44138287Smarks# SYMLINKS	Same as LINKS, except it creates symlinks and the
45138287Smarks#		linked-to pathname may be relative.
46138287Smarks#
47138287Smarks# DESTDIR, DISTDIR are set by other Makefiles (e.g. bsd.own.mk)
48138287Smarks#
49138287Smarks# MFILES	Optionally a list of interfaces used by the module.
50138287Smarks#		This file contains a default list of interfaces.
51138287Smarks#
52138287Smarks# EXPORT_SYMS	A list of symbols that should be exported from the module,
53138287Smarks#		or the name of a file containing a list of symbols, or YES
54138287Smarks#		to export all symbols.  If not defined, no symbols are
55138287Smarks#		exported.
56138287Smarks#
57138287Smarks# +++ targets +++
58138287Smarks#
59138287Smarks#       distribute:
60138287Smarks#               This is a variant of install, which will
61138287Smarks#               put the stuff into the right "distribution".
62138287Smarks#
63138287Smarks# 	install:
64138287Smarks#               install the kernel module; if the Makefile
65138287Smarks#               does not itself define the target install, the targets
66138287Smarks#               beforeinstall and afterinstall may also be used to cause
67138287Smarks#               actions immediately before and after the install target
68138287Smarks#		is executed.
69138287Smarks#
70138287Smarks# 	load:
71138287Smarks#		Load KLD.
72138287Smarks#
73138287Smarks# 	unload:
74138287Smarks#		Unload KLD.
75138287Smarks#
76138287Smarks# bsd.obj.mk: clean, cleandir and obj
77138287Smarks# bsd.dep.mk: cleandepend, depend and tags
78138287Smarks#
79138287Smarks
80138287SmarksKMODLOAD?=	/sbin/kldload
81138287SmarksKMODUNLOAD?=	/sbin/kldunload
82138287SmarksOBJCOPY?=	objcopy
83138287Smarks
84138287SmarksTARGET_ARCH?=	${MACHINE_ARCH}
85138287Smarks
86138287Smarks.if !target(__initialized__)
87138287Smarks__initialized__:
88138287Smarks.if exists(${.CURDIR}/../Makefile.inc)
89138287Smarks.include "${.CURDIR}/../Makefile.inc"
90138287Smarks.endif
91138287Smarks.endif
92138287Smarks
93138287Smarks.SUFFIXES: .out .o .c .cc .cxx .C .y .l .s .S
94138287Smarks
95138287SmarksCFLAGS+=	${COPTS} -D_KERNEL ${CWARNFLAGS}
96138287SmarksCFLAGS+=	-DKLD_MODULE
97138287Smarks
98138287Smarks# Don't use any standard or source-relative include directories.
99138287Smarks# Since -nostdinc will annull any previous -I paths, we repeat all
100138287Smarks# such paths after -nostdinc.  It doesn't seem to be possible to
101138287Smarks# add to the front of `make' variable.
102138287Smarks_ICFLAGS:=	${CFLAGS:M-I*}
103138287SmarksCFLAGS+=	-nostdinc -I- ${INCLMAGIC} ${_ICFLAGS}
104138287Smarks
105138287Smarks# Add -I paths for system headers.  Individual KLD makefiles don't
106138287Smarks# need any -I paths for this.  Similar defaults for .PATH can't be
107138287Smarks# set because there are no standard paths for non-headers.
108138287SmarksCFLAGS+=	-I. -I@ -I@/dev
109138287Smarks
110138287Smarks# Add a -I path to standard headers like <stddef.h>.  Use a relative
111138287Smarks# path to src/include if possible.  If the @ symlink hasn't been built
112138287Smarks# yet, then we can't tell if the relative path exists.  Add both the
113138287Smarks# potential relative path and an absolute path in that case.
114138287Smarks.if exists(@)
115138287Smarks.if exists(@/../include)
116138287SmarksCFLAGS+=	-I@/../include
117138287Smarks.else
118138287SmarksCFLAGS+=	-I${DESTDIR}/usr/include
119138287Smarks.endif
120138287Smarks.else # !@
121138287SmarksCFLAGS+=	-I@/../include -I${DESTDIR}/usr/include
122138287Smarks.endif # @
123138287Smarks
124138287Smarks# Disallow common variables, and if we end up with commons from
125138287Smarks# somewhere unexpected, allocate storage for them in the module itself.
126138287SmarksCFLAGS+=	-fno-common
127138287SmarksLDFLAGS+=	-d -warn-common
128138287Smarks
129138287SmarksCFLAGS+=	${DEBUG_FLAGS}
130138287Smarks
131138287Smarks.if ${OBJFORMAT} == elf
132138287SmarksCLEANFILES+=	setdef0.c setdef1.c setdefs.h
133138287SmarksCLEANFILES+=	setdef0.o setdef1.o
134138287Smarks.endif
135138287Smarks
136138287SmarksOBJS+=  ${SRCS:N*.h:R:S/$/.o/g}
137138287Smarks
138138287Smarks.if !defined(PROG)
139138287SmarksPROG=	${KMOD}.ko
140138287Smarks.endif
141138287Smarks
142138287Smarks.if !defined(DEBUG)
143138287SmarksFULLPROG=	${PROG}
144138287Smarks.else
145138287SmarksFULLPROG=	${PROG}.debug
146138287Smarks${PROG}: ${FULLPROG}
147138287Smarks	${OBJCOPY} --strip-debug ${FULLPROG} ${PROG}
148138287Smarks.endif
149138287Smarks
150138287Smarks${FULLPROG}: ${KMOD}.kld
151138287Smarks	${LD} -Bshareable ${LDFLAGS} -o ${.TARGET} ${KMOD}.kld
152138287Smarks
153138287SmarksEXPORT_SYMS?=	NO
154138287Smarks.if ${EXPORT_SYMS} != YES
155138287SmarksCLEANFILES+=	${.OBJDIR}/export_syms
156138287Smarks.endif
157138287Smarks
158138287Smarks${KMOD}.kld: ${OBJS}
159138287Smarks	${LD} ${LDFLAGS} -r -d -o ${.TARGET} ${OBJS}
160138287Smarks.if defined(EXPORT_SYMS)
161138287Smarks.if ${EXPORT_SYMS} != YES
162138287Smarks.if ${EXPORT_SYMS} == NO
163138287Smarks	touch ${.OBJDIR}/export_syms
164138287Smarks.elif !exists(${.CURDIR}/${EXPORT_SYMS})
165138287Smarks	echo ${EXPORT_SYMS} > ${.OBJDIR}/export_syms
166138287Smarks.else
167138287Smarks	grep -v '^#' < ${EXPORT_SYMS} >  ${.OBJDIR}/export_syms
168138287Smarks.endif
169138287Smarks	awk -f ${SYSDIR}/conf/kmod_syms.awk ${.TARGET} \
170138287Smarks		${.OBJDIR}/export_syms | \
171138287Smarks	xargs -J% ${OBJCOPY} % ${.TARGET}
172138287Smarks.endif
173138287Smarks.endif
174138287Smarks
175138287Smarks
176138287Smarks.if !target(all-man)
177138287Smarksall-man: _SUBDIR
178138287Smarks.endif
179138287Smarks.if !target(maninstall)
180138287Smarksmaninstall: _SUBDIR
181138287Smarks.endif
182138287Smarks
183138287Smarks_ILINKS=@ machine
184138287Smarks
185138287Smarks.MAIN: all
186138287Smarksall: objwarn ${PROG} _SUBDIR
187138287Smarks
188138287Smarksbeforedepend: ${_ILINKS}
189138287Smarks	@rm -f .depend
190138287Smarks
191138287Smarks# Ensure that the links exist without depending on it when it exists which
192138287Smarks# causes all the modules to be rebuilt when the directory pointed to changes.
193138287Smarks.for _link in ${_ILINKS}
194138287Smarks.if !exists(${.OBJDIR}/${_link})
195138287Smarks${OBJS}: ${_link}
196138287Smarks.endif
197138287Smarks.endfor
198138287Smarks
199138287Smarks# Search for kernel source tree in standard places.
200138287Smarks.for _dir in ${.CURDIR}/../.. ${.CURDIR}/../../.. /sys /usr/src/sys
201138287Smarks.if !defined(SYSDIR) && exists(${_dir}/kern/)
202138287SmarksSYSDIR=	${_dir}
203138287Smarks.endif
204138287Smarks.endfor
205138287Smarks.if !defined(SYSDIR) || !exists(${SYSDIR}/kern)
206138287Smarks.error "can't find kernel source tree"
207138287Smarks.endif
208138287Smarks
209138287Smarks${_ILINKS}:
210138287Smarks	@case ${.TARGET} in \
211138287Smarks	machine) \
212138287Smarks		path=${SYSDIR}/${MACHINE_ARCH}/include ;; \
213138287Smarks	@) \
214138287Smarks		path=${SYSDIR} ;; \
215138287Smarks	esac ; \
216138287Smarks	path=`(cd $$path && /bin/pwd)` ; \
217138287Smarks	${ECHO} ${.TARGET} "->" $$path ; \
218138287Smarks	ln -s $$path ${.TARGET}
219138287Smarks
220138287SmarksCLEANFILES+= ${PROG} ${FULLPROG} ${KMOD}.kld ${OBJS} ${_ILINKS} symb.tmp tmp.o
221138287Smarks
222138287Smarks.if !target(install)
223138287Smarks.if !target(beforeinstall)
224138287Smarksbeforeinstall:
225138287Smarks.endif
226138287Smarks.if !target(afterinstall)
227138287Smarksafterinstall:
228138287Smarks.endif
229138287Smarks
230138287Smarks_INSTALLFLAGS:=	${INSTALLFLAGS}
231138287Smarks.for ie in ${INSTALLFLAGS_EDIT}
232138287Smarks_INSTALLFLAGS:=	${_INSTALLFLAGS${ie}}
233138287Smarks.endfor
234138287Smarks
235138287Smarksinstall.debug: _SUBDIR
236138287Smarks	${INSTALL} ${COPY} -o ${KMODOWN} -g ${KMODGRP} -m ${KMODMODE} \
237138287Smarks	    ${_INSTALLFLAGS} ${FULLPROG} ${DESTDIR}${KMODDIR}/
238138287Smarks
239138287Smarksrealinstall: _SUBDIR
240138287Smarks	${INSTALL} ${COPY} -o ${KMODOWN} -g ${KMODGRP} -m ${KMODMODE} \
241138287Smarks	    ${_INSTALLFLAGS} ${PROG} ${DESTDIR}${KMODDIR}/
242138287Smarks.if defined(LINKS) && !empty(LINKS)
243138287Smarks	@set ${LINKS}; \
244138287Smarks	while test $$# -ge 2; do \
245138287Smarks		l=${DESTDIR}$$1; \
246138287Smarks		shift; \
247138287Smarks		t=${DESTDIR}$$1; \
248138287Smarks		shift; \
249138287Smarks		${ECHO} $$t -\> $$l; \
250138287Smarks		ln -f $$l $$t; \
251138287Smarks	done; true
252138287Smarks.endif
253138287Smarks.if defined(SYMLINKS) && !empty(SYMLINKS)
254138287Smarks	@set ${SYMLINKS}; \
255138287Smarks	while test $$# -ge 2; do \
256138287Smarks		l=$$1; \
257138287Smarks		shift; \
258138287Smarks		t=${DESTDIR}$$1; \
259138287Smarks		shift; \
260138287Smarks		${ECHO} $$t -\> $$l; \
261138287Smarks		ln -fs $$l $$t; \
262138287Smarks	done; true
263138287Smarks.endif
264138287Smarks.if !defined(NO_XREF)
265138287Smarks	-kldxref ${DESTDIR}${KMODDIR}
266138287Smarks.endif
267138287Smarks
268138287Smarksinstall: afterinstall _SUBDIR
269138287Smarksafterinstall: realinstall
270138287Smarksrealinstall: beforeinstall
271138287Smarks.endif
272138287Smarks
273138287SmarksDISTRIBUTION?=	bin
274138287Smarks.if !target(distribute)
275138287Smarksdistribute: _SUBDIR
276138287Smarks.for dist in ${DISTRIBUTION}
277138287Smarks	cd ${.CURDIR} ; $(MAKE) install DESTDIR=${DISTDIR}/${dist} SHARED=copies
278138287Smarks.endfor
279138287Smarks.endif
280138287Smarks
281138287Smarks.if !target(load)
282138287Smarksload:	${PROG}
283138287Smarks	${KMODLOAD} -v ${.CURDIR}/${KMOD}.ko
284138287Smarks.endif
285138287Smarks
286138287Smarks.if !target(unload)
287138287Smarksunload:
288138287Smarks	${KMODUNLOAD} -v ${KMOD}
289138287Smarks.endif
290138287Smarks
291138287Smarks.for _src in ${SRCS:Mopt_*.h}
292138287SmarksCLEANFILES+=	${_src}
293138287Smarks.if !target(${_src})
294138287Smarks${_src}:
295138287Smarks	touch ${.TARGET}
296138287Smarks.endif
297138287Smarks.endfor
298138287Smarks
299138287SmarksMFILES?= kern/bus_if.m kern/device_if.m dev/iicbus/iicbb_if.m \
300138287Smarks    dev/iicbus/iicbus_if.m isa/isa_if.m \
301138287Smarks    libkern/iconv_converter_if.m \
302138287Smarks    dev/mii/miibus_if.m \
303138287Smarks    dev/pccard/card_if.m dev/pccard/power_if.m dev/pci/pci_if.m \
304138287Smarks    dev/pci/pcib_if.m dev/ppbus/ppbus_if.m dev/smbus/smbus_if.m \
305138287Smarks    dev/usb/usb_if.m dev/sound/pcm/ac97_if.m dev/sound/pcm/channel_if.m \
306138287Smarks    dev/sound/pcm/feeder_if.m dev/sound/pcm/mixer_if.m pci/agp_if.m
307138287Smarks
308138287Smarks.for _srcsrc in ${MFILES}
309138287Smarks.for _ext in c h
310138287Smarks.for _src in ${SRCS:M${_srcsrc:T:R}.${_ext}}
311138287SmarksCLEANFILES+=	${_src}
312138287Smarks.if !target(${_src})
313138287Smarks.if !exists(@)
314138287Smarks${_src}: @
315138287Smarks.endif
316138287Smarks.if exists(@)
317138287Smarks${_src}: @/kern/makeobjops.pl @/${_srcsrc}
318138287Smarks.endif
319138287Smarks	perl @/kern/makeobjops.pl -${_ext} @/${_srcsrc}
320138287Smarks.endif
321138287Smarks.endfor # _src
322138287Smarks.endfor # _ext
323138287Smarks.endfor # _srcsrc
324138287Smarks
325138287Smarks.for _ext in c h
326138287Smarks.if ${SRCS:Mvnode_if.${_ext}} != ""
327138287SmarksCLEANFILES+=	vnode_if.${_ext}
328138287Smarks.if !exists(@)
329138287Smarksvnode_if.${_ext}: @
330138287Smarks.endif
331138287Smarks.if exists(@)
332138287Smarksvnode_if.${_ext}: @/kern/vnode_if.pl @/kern/vnode_if.src
333138287Smarks.endif
334138287Smarks	perl @/kern/vnode_if.pl -${_ext} @/kern/vnode_if.src
335138287Smarks.endif
336138287Smarks.endfor
337138287Smarks
338138287Smarksregress:
339138287Smarks
340138287Smarks.include <bsd.dep.mk>
341138287Smarks
342138287Smarks.if !exists(${DEPENDFILE})
343138287Smarks${OBJS}: ${SRCS:M*.h}
344138287Smarks.endif
345138287Smarks
346138287Smarks.include <bsd.obj.mk>
347138287Smarks
348138287Smarks.include <bsd.kern.mk>
349138287Smarks