kmod.mk revision 55206
1#	From: @(#)bsd.prog.mk	5.26 (Berkeley) 6/25/91
2# $FreeBSD: head/sys/conf/kmod.mk 55206 1999-12-29 05:07:58Z 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# KERN		Main Kernel source directory. [${.CURDIR}/../../sys/kern]
15#
16# KMOD          The name of the kernel module to build.
17#
18# KMODDIR	Base path for kernel modules (see kld(4)). [/modules]
19#
20# KMODOWN	KLD owner. [${BINOWN}]
21#
22# KMODGRP	KLD group. [${BINGRP}]
23#
24# KMODMODE	KLD mode. [${BINMODE}]
25#
26# LINKS		The list of KLD links; should be full pathnames, the
27#               linked-to file coming first, followed by the linked
28#               file.  The files are hard-linked.  For example, to link
29#               /modules/master and /modules/meister, use:
30#
31#			LINKS=  /modules/master /modules/meister
32#
33# KMODLOAD	Command to load a kernel module [/sbin/kldload]
34#
35# KMODUNLOAD	Command to unload a kernel module [/sbin/kldunload]
36#
37# NOMAN		KLD does not have a manual page if set.
38#
39# PROG          The name of the kernel module to build. 
40#		If not supplied, ${KMOD}.o is used.
41#
42# SRCS          List of source files 
43#
44# KMODDEPS	List of modules which this one is dependant on
45#
46# SUBDIR        A list of subdirectories that should be built as well.
47#               Each of the targets will execute the same target in the
48#               subdirectories.
49#
50# SYMLINKS	Same as LINKS, except it creates symlinks and the
51#		linked-to pathname may be relative.
52#
53# DESTDIR, DISTDIR are set by other Makefiles (e.g. bsd.own.mk)
54#
55#
56# +++ targets +++
57#
58#       distribute:
59#               This is a variant of install, which will
60#               put the stuff into the right "distribution".
61#
62# 	install:
63#               install the program and its manual pages; if the Makefile
64#               does not itself define the target install, the targets
65#               beforeinstall and afterinstall may also be used to cause
66#               actions immediately before and after the install target
67#		is executed.
68#
69# 	load:	
70#		Load KLD.
71#
72# 	unload:
73#		Unload KLD.
74#
75# bsd.obj.mk: clean, cleandir and obj
76# bsd.dep.mk: cleandepend, depend and tags
77# bsd.man.mk: maninstall
78#
79
80KMODLOAD?=	/sbin/kldload
81KMODUNLOAD?=	/sbin/kldunload
82
83.if !target(__initialized__)
84__initialized__:
85.if exists(${.CURDIR}/../Makefile.inc)
86.include "${.CURDIR}/../Makefile.inc"
87.endif
88.endif
89
90.SUFFIXES: .out .o .c .cc .cxx .C .y .l .s .S
91
92CFLAGS+=	${COPTS} -D_KERNEL ${CWARNFLAGS}
93CFLAGS+=	-DKLD_MODULE
94
95# Don't use any standard or source-relative include directories.
96# Since -nostdinc will annull any previous -I paths, we repeat all
97# such paths after -nostdinc.  It doesn't seem to be possible to
98# add to the front of `make' variable.
99_ICFLAGS:=	${CFLAGS:M-I*}
100CFLAGS+=	-nostdinc -I- ${_ICFLAGS}
101
102# Add -I paths for system headers.  Individual KLD makefiles don't
103# need any -I paths for this.  Similar defaults for .PATH can't be
104# set because there are no standard paths for non-headers.
105CFLAGS+=	-I${.OBJDIR} -I${.OBJDIR}/@
106
107# XXX this is now dubious.
108.if defined(DESTDIR)
109CFLAGS+=	-I${DESTDIR}/usr/include
110.endif
111
112.if ${OBJFORMAT} == elf
113CLEANFILES+=	setdef0.c setdef1.c setdefs.h
114CLEANFILES+=	setdef0.o setdef1.o
115.endif
116
117OBJS+=  ${SRCS:N*.h:R:S/$/.o/g}
118
119.if !defined(PROG)
120PROG=	${KMOD}.ko
121.endif
122
123${PROG}: ${OBJS} ${DPADD} ${KMODDEPS}
124.if ${OBJFORMAT} == elf
125	gensetdefs ${OBJS}
126	${CC} ${CFLAGS} -c setdef0.c
127	${CC} ${CFLAGS} -c setdef1.c
128	${LD} -Bshareable ${LDFLAGS} -o ${.TARGET} setdef0.o ${OBJS} setdef1.o  ${KMODDEPS}
129.else
130	${LD} -Bshareable ${LDFLAGS} -o ${.TARGET} ${OBJS} ${KMODDEPS}
131.endif
132
133.if defined(KMODDEPS)
134.for dep in ${KMODDEPS}
135CLEANFILES+=	${dep} __${dep}_hack_dep.c
136
137${dep}:
138	touch __${dep}_hack_dep.c
139	${CC} -shared ${CFLAGS} -o ${dep} __${dep}_hack_dep.c
140.endfor
141.endif
142
143.if !defined(NOMAN)
144.include <bsd.man.mk>
145.if !defined(_MANPAGES) || empty(_MANPAGES)
146MAN1=	${KMOD}.4
147.endif
148
149.elif !target(maninstall)
150maninstall: _SUBDIR
151all-man:
152.endif
153
154_ILINKS=@ machine
155
156.MAIN: all
157all: objwarn ${PROG} all-man _SUBDIR
158
159beforedepend ${OBJS}: ${_ILINKS}
160
161# The search for the link targets works best if we are in a normal src
162# tree, and not too deeply below src/sys/modules.  If we are near "/", then
163# we may find /sys - this is harmless.  Other abnormal "sys" directories
164# found in the search are likely to cause problems.  If nothing is found,
165# then the links default to /usr/include and /usr/include/machine.
166${_ILINKS}:
167	@set +x; for up in ../.. ../../.. ; do \
168		case ${.TARGET} in \
169		machine) \
170			testpath=${.CURDIR}/$$up/${MACHINE_ARCH}/include ; \
171			path=${.CURDIR}/$$up/${MACHINE_ARCH}/include ; \
172			defaultpath=/usr/include/machine ;; \
173		@) \
174			testpath=${.CURDIR}/$$up/sys ; \
175			path=${.CURDIR}/$$up ; \
176			defaultpath=/usr/include ;; \
177		esac ; \
178		if [ -d $$testpath ] ; then break ; fi ; \
179		path=$$defaultpath ; \
180	done ; \
181	path=`(cd $$path && /bin/pwd)` ; \
182	${ECHO} ${.TARGET} "->" $$path ; \
183	ln -s $$path ${.TARGET}
184
185CLEANFILES+= ${PROG} ${OBJS} ${_ILINKS} symb.tmp tmp.o
186
187.if !target(install)
188.if !target(beforeinstall)
189beforeinstall:
190.endif
191.if !target(afterinstall)
192afterinstall:
193.endif
194
195_INSTALLFLAGS:=	${INSTALLFLAGS}
196.for ie in ${INSTALLFLAGS_EDIT}
197_INSTALLFLAGS:=	${_INSTALLFLAGS${ie}}
198.endfor
199
200realinstall: _SUBDIR
201	${INSTALL} ${COPY} -o ${KMODOWN} -g ${KMODGRP} -m ${KMODMODE} \
202	    ${_INSTALLFLAGS} ${PROG} ${DESTDIR}${KMODDIR}
203.if defined(LINKS) && !empty(LINKS)
204	@set ${LINKS}; \
205	while test $$# -ge 2; do \
206		l=${DESTDIR}$$1; \
207		shift; \
208		t=${DESTDIR}$$1; \
209		shift; \
210		${ECHO} $$t -\> $$l; \
211		ln -f $$l $$t; \
212	done; true
213.endif
214.if defined(SYMLINKS) && !empty(SYMLINKS)
215	@set ${SYMLINKS}; \
216	while test $$# -ge 2; do \
217		l=$$1; \
218		shift; \
219		t=${DESTDIR}$$1; \
220		shift; \
221		${ECHO} $$t -\> $$l; \
222		ln -fs $$l $$t; \
223	done; true
224.endif
225
226install: afterinstall _SUBDIR
227.if !defined(NOMAN)
228afterinstall: realinstall maninstall
229.else
230afterinstall: realinstall
231.endif
232realinstall: beforeinstall
233.endif
234
235DISTRIBUTION?=	bin
236.if !target(distribute)
237distribute: _SUBDIR
238.for dist in ${DISTRIBUTION}
239	cd ${.CURDIR} ; $(MAKE) install DESTDIR=${DISTDIR}/${dist} SHARED=copies
240.endfor
241.endif
242
243.if !target(load)
244load:	${PROG} install
245	${KMODLOAD} ${KMOD}
246.endif
247
248.if !target(unload)
249unload:
250	${KMODUNLOAD} ${KMOD}
251.endif
252
253.if exists(${.CURDIR}/../../kern)
254KERN=	${.CURDIR}/../../kern
255.else
256KERN=	${.CURDIR}/../../sys/kern
257.endif
258
259.for _src in ${SRCS:Mopt_*.h}
260CLEANFILES+=	${_src}
261.if !target(${_src})
262${_src}:
263	touch ${.TARGET}
264.endif
265.endfor
266
267.for _srcsrc in kern/bus_if.m kern/device_if.m dev/iicbus/iicbb_if.m \
268    dev/iicbus/iicbus_if.m isa/isa_if.m dev/mii/miibus_if.m dev/pccard/card_if.m \
269    dev/pccard/power_if.m pci/pci_if.m dev/smbus/smbus_if.m dev/usb/usb_if.m
270.for _ext in c h
271.for _src in ${SRCS:M${_srcsrc:T:R}.${_ext}}
272CLEANFILES+=	${_src}
273.if !target(${_src})
274${_src}: @
275.if exists(@)
276${_src}: @/kern/makedevops.pl @/${_srcsrc}
277.endif
278	perl @/kern/makedevops.pl -${_ext} @/${_srcsrc}
279.endif
280.endfor # _src
281.endfor # _ext
282.endfor # _srcsrc
283
284.for _ext in c h
285.if ${SRCS:Mvnode_if.${_ext}} != ""
286CLEANFILES+=	vnode_if.${_ext}
287vnode_if.${_ext}: @
288.if exists(@)
289vnode_if.${_ext}: @/kern/vnode_if.pl @/kern/vnode_if.src
290.endif
291	perl @/kern/vnode_if.pl -${_ext} @/kern/vnode_if.src
292.endif
293.endfor
294
295regress:
296
297.include <bsd.dep.mk>
298
299.if !exists(${DEPENDFILE})
300${OBJS}: ${SRCS:M*.h}
301.endif
302
303.include <bsd.obj.mk>
304
305.include <bsd.kern.mk>
306