kmod.mk revision 45873
1#	From: @(#)bsd.prog.mk	5.26 (Berkeley) 6/25/91
2#	$Id: bsd.kmod.mk,v 1.59 1999/03/23 03:06:25 bde Exp $
3#
4# The include file <bsd.kmod.mk> handles installing Loadable Kernel Modules.
5#
6#
7# +++ variables +++
8#
9# CLEANFILES	Additional files to remove for the clean and cleandir targets.
10#
11# DISTRIBUTION  Name of distribution. [bin]
12#
13# KERN		Main Kernel source directory. [${.CURDIR}/../../sys/kern]
14#
15# KMOD          The name of the loadable kernel module to build.
16#
17# KMODDIR	Base path for loadable kernel modules
18#		(see lkm(4)). [/modules]
19#
20# KMODOWN	LKM owner. [${BINOWN}]
21#
22# KMODGRP	LKM group. [${BINGRP}]
23#
24# KMODMODE	LKM mode. [${BINMODE}]
25#
26# LINKS		The list of LKM 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# MODLOAD	Command to load a kernel module [/sbin/modload]
34#
35# MODUNLOAD	Command to unload a kernel module [/sbin/modunload]
36#
37# NOMAN		LKM does not have a manual page if set.
38#
39# PROG          The name of the loadable 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 LKM.
71#
72# 	unload:
73#		Unload LKM.
74#
75# bsd.obj.mk: clean, cleandir and obj
76# bsd.dep.mk: cleandepend, depend and tags
77# bsd.man.mk: maninstall
78#
79
80MODLOAD?=	/sbin/modload
81MODUNLOAD?=	/sbin/modunload
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} -DKERNEL ${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 LKM 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 defined(VFS_KLD)
113SRCS+=		vnode_if.h
114CLEANFILES+=	vnode_if.h vnode_if.c
115.endif
116
117.if ${OBJFORMAT} == elf
118CLEANFILES+=	setdef0.c setdef1.c setdefs.h
119CLEANFILES+=	setdef0.o setdef1.o
120.endif
121
122OBJS+=  ${SRCS:N*.h:R:S/$/.o/g}
123
124.if !defined(PROG)
125PROG=	${KMOD}.ko
126.endif
127
128${PROG}: ${OBJS} ${DPADD} ${KMODDEPS}
129.if ${OBJFORMAT} == elf
130	gensetdefs ${OBJS}
131	${CC} ${CFLAGS} -c setdef0.c
132	${CC} ${CFLAGS} -c setdef1.c
133	${LD} -Bshareable ${LDFLAGS} -o ${.TARGET} setdef0.o ${OBJS} setdef1.o  ${KMODDEPS}
134.else
135	${LD} -Bshareable ${LDFLAGS} -o ${.TARGET} ${OBJS} ${KMODDEPS}
136.endif
137
138.if defined(KMODDEPS)
139.for dep in ${KMODDEPS}
140CLEANFILES+=	${dep} __${dep}_hack_dep.c
141
142${dep}:
143	touch __${dep}_hack_dep.c
144	${CC} -shared ${CFLAGS} -o ${dep} __${dep}_hack_dep.c
145.endfor
146.endif
147
148.if !defined(NOMAN)
149.include <bsd.man.mk>
150.if !defined(_MANPAGES) || empty(_MANPAGES)
151MAN1=	${KMOD}.4
152.endif
153
154.elif !target(maninstall)
155maninstall: _SUBDIR
156all-man:
157.endif
158
159_ILINKS=@ machine
160
161.MAIN: all
162all: objwarn ${PROG} all-man _SUBDIR
163
164beforedepend ${OBJS}: ${_ILINKS}
165
166# The search for the link targets works best if we are in a normal src
167# tree, and not too deeply below src/sys/modules.  If we are near "/", then
168# we may find /sys - this is harmless.  Other abnormal "sys" directories
169# found in the search are likely to cause problems.  If nothing is found,
170# then the links default to /usr/include and /usr/include/machine.
171${_ILINKS}:
172	@set +x; for up in ../.. ../../.. ; do \
173		case ${.TARGET} in \
174		machine) \
175			testpath=${.CURDIR}/$$up/${MACHINE_ARCH}/include ; \
176			path=${.CURDIR}/$$up/${MACHINE_ARCH}/include ; \
177			defaultpath=/usr/include/machine ;; \
178		@) \
179			testpath=${.CURDIR}/$$up/sys ; \
180			path=${.CURDIR}/$$up ; \
181			defaultpath=/usr/include ;; \
182		esac ; \
183		if [ -d $$testpath ] ; then break ; fi ; \
184		path=$$defaultpath ; \
185	done ; \
186	path=`(cd $$path && /bin/pwd)` ; \
187	${ECHO} ${.TARGET} "->" $$path ; \
188	ln -s $$path ${.TARGET}
189
190CLEANFILES+= ${PROG} ${OBJS} ${_ILINKS} symb.tmp tmp.o
191
192.if !target(install)
193.if !target(beforeinstall)
194beforeinstall:
195.endif
196.if !target(afterinstall)
197afterinstall:
198.endif
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}
245	${MODLOAD} -o ${KMOD} -e${KMOD} ${PROG}
246.endif
247
248.if !target(unload)
249unload:	${PROG}
250	${MODUNLOAD} -n ${KMOD}
251.endif
252
253.if exists(${.CURDIR}/../../kern)
254KERN=	${.CURDIR}/../../kern
255.else
256KERN=	${.CURDIR}/../../sys/kern
257.endif
258
259vnode_if.h:	${KERN}/vnode_if.sh ${KERN}/vnode_if.src
260	sh ${KERN}/vnode_if.sh ${KERN}/vnode_if.src
261
262regress:
263
264.include <bsd.dep.mk>
265
266.if !exists(${DEPENDFILE})
267${OBJS}: ${SRCS:M*.h}
268.endif
269
270.include <bsd.obj.mk>
271
272.include <bsd.kern.mk>
273