xref: /freebsd/sys/conf/kmod.mk (revision c3aac50f284c6cca5b4f2eb46aaa13812cb8b630)
1#	From: @(#)bsd.prog.mk	5.26 (Berkeley) 6/25/91
2# $FreeBSD$
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 kld(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
200_INSTALLFLAGS:=	${INSTALLFLAGS}
201.for ie in ${INSTALLFLAGS_EDIT}
202_INSTALLFLAGS:=	${_INSTALLFLAGS${ie}}
203.endfor
204
205realinstall: _SUBDIR
206	${INSTALL} ${COPY} -o ${KMODOWN} -g ${KMODGRP} -m ${KMODMODE} \
207	    ${_INSTALLFLAGS} ${PROG} ${DESTDIR}${KMODDIR}
208.if defined(LINKS) && !empty(LINKS)
209	@set ${LINKS}; \
210	while test $$# -ge 2; do \
211		l=${DESTDIR}$$1; \
212		shift; \
213		t=${DESTDIR}$$1; \
214		shift; \
215		${ECHO} $$t -\> $$l; \
216		ln -f $$l $$t; \
217	done; true
218.endif
219.if defined(SYMLINKS) && !empty(SYMLINKS)
220	@set ${SYMLINKS}; \
221	while test $$# -ge 2; do \
222		l=$$1; \
223		shift; \
224		t=${DESTDIR}$$1; \
225		shift; \
226		${ECHO} $$t -\> $$l; \
227		ln -fs $$l $$t; \
228	done; true
229.endif
230
231install: afterinstall _SUBDIR
232.if !defined(NOMAN)
233afterinstall: realinstall maninstall
234.else
235afterinstall: realinstall
236.endif
237realinstall: beforeinstall
238.endif
239
240DISTRIBUTION?=	bin
241.if !target(distribute)
242distribute: _SUBDIR
243.for dist in ${DISTRIBUTION}
244	cd ${.CURDIR} ; $(MAKE) install DESTDIR=${DISTDIR}/${dist} SHARED=copies
245.endfor
246.endif
247
248.if !target(load)
249load:	${PROG}
250	${MODLOAD} -o ${KMOD} -e${KMOD} ${PROG}
251.endif
252
253.if !target(unload)
254unload:	${PROG}
255	${MODUNLOAD} -n ${KMOD}
256.endif
257
258.if exists(${.CURDIR}/../../kern)
259KERN=	${.CURDIR}/../../kern
260.else
261KERN=	${.CURDIR}/../../sys/kern
262.endif
263
264vnode_if.h:	${KERN}/vnode_if.sh ${KERN}/vnode_if.src
265	sh ${KERN}/vnode_if.sh ${KERN}/vnode_if.src
266
267regress:
268
269.include <bsd.dep.mk>
270
271.if !exists(${DEPENDFILE})
272${OBJS}: ${SRCS:M*.h}
273.endif
274
275.include <bsd.obj.mk>
276
277.include <bsd.kern.mk>
278