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