kmod.mk revision 59097
1#	From: @(#)bsd.prog.mk	5.26 (Berkeley) 6/25/91
2# $FreeBSD: head/sys/conf/kmod.mk 59097 2000-04-08 17:20:00Z imp $
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# KMOD          The name of the kernel module to build.
15#
16# KMODDIR	Base path for kernel modules (see kld(4)). [/modules]
17#
18# KMODOWN	KLD owner. [${BINOWN}]
19#
20# KMODGRP	KLD group. [${BINGRP}]
21#
22# KMODMODE	KLD mode. [${BINMODE}]
23#
24# LINKS		The list of KLD links; should be full pathnames, the
25#               linked-to file coming first, followed by the linked
26#               file.  The files are hard-linked.  For example, to link
27#               /modules/master and /modules/meister, use:
28#
29#			LINKS=  /modules/master /modules/meister
30#
31# KMODLOAD	Command to load a kernel module [/sbin/kldload]
32#
33# KMODUNLOAD	Command to unload a kernel module [/sbin/kldunload]
34#
35# NOMAN		KLD does not have a manual page if set.
36#
37# PROG          The name of the kernel module to build. 
38#		If not supplied, ${KMOD}.o is used.
39#
40# SRCS          List of source files 
41#
42# KMODDEPS	List of modules which this one is dependant on
43#
44# SUBDIR        A list of subdirectories that should be built as well.
45#               Each of the targets will execute the same target in the
46#               subdirectories.
47#
48# SYMLINKS	Same as LINKS, except it creates symlinks and the
49#		linked-to pathname may be relative.
50#
51# DESTDIR, DISTDIR are set by other Makefiles (e.g. bsd.own.mk)
52#
53# MFILES	Optionally a list of interfaces used by the module.
54#		This file contains a default list of interfaces.
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. -I@
106
107# Add a -I path to standard headers like <stddef.h>.  Use a relative
108# path to src/include if possible.  If the @ symlink hasn't been built
109# yet, then we can't tell if the relative path exists.  Add both the
110# potential relative path and an absolute path in that case.
111.if exists(@)
112.if exists(@/../include)
113CFLAGS+=	-I@/../include
114.else
115CFLAGS+=	-I${DESTDIR}/usr/include
116.endif
117.else # !@
118CFLAGS+=	-I@/../include -I${DESTDIR}/usr/include
119.endif # @
120
121CFLAGS+=	${DEBUG_FLAGS}
122
123.if ${OBJFORMAT} == elf
124CLEANFILES+=	setdef0.c setdef1.c setdefs.h
125CLEANFILES+=	setdef0.o setdef1.o
126.endif
127
128OBJS+=  ${SRCS:N*.h:R:S/$/.o/g}
129
130.if !defined(PROG)
131PROG=	${KMOD}.ko
132.endif
133
134${PROG}: ${KMOD}.kld ${KMODDEPS}
135	${LD} -Bshareable ${LDFLAGS} -o ${.TARGET} ${KMOD}.kld ${KMODDEPS}
136
137${KMOD}.kld: ${OBJS}
138.if ${OBJFORMAT} == elf
139	gensetdefs ${OBJS}
140	${CC} ${CFLAGS} -c setdef0.c
141	${CC} ${CFLAGS} -c setdef1.c
142	${LD} ${LDFLAGS} -r -o ${.TARGET} setdef0.o ${OBJS} setdef1.o
143.else
144	${LD} ${LDFLAGS} -r -o ${.TARGET} ${OBJS}
145.endif
146
147.if defined(KMODDEPS)
148.for dep in ${KMODDEPS}
149CLEANFILES+=	${dep} __${dep}_hack_dep.c
150
151${dep}:
152	touch __${dep}_hack_dep.c
153	${CC} -shared ${CFLAGS} -o ${dep} __${dep}_hack_dep.c
154.endfor
155.endif
156
157.if !defined(NOMAN)
158.include <bsd.man.mk>
159.if !defined(_MANPAGES) || empty(_MANPAGES)
160MAN1=	${KMOD}.4
161.endif
162
163.elif !target(maninstall)
164maninstall: _SUBDIR
165all-man:
166.endif
167
168_ILINKS=@ machine
169
170.MAIN: all
171all: objwarn ${PROG} all-man _SUBDIR
172
173beforedepend ${OBJS}: ${_ILINKS}
174
175# Search for kernel source tree in standard places.
176.for _dir in ${.CURDIR}/../.. ${.CURDIR}/../../.. /sys /usr/src/sys
177.if !defined(SYSDIR) && exists(${_dir}/kern/)
178SYSDIR=	${_dir}
179.endif
180.endfor
181.if !defined(SYSDIR) || !exists(${SYSDIR}/kern/)
182.error "can't find kernel source tree"
183.endif
184
185${_ILINKS}:
186	@case ${.TARGET} in \
187	machine) \
188		path=${SYSDIR}/${MACHINE_ARCH}/include ;; \
189	@) \
190		path=${SYSDIR} ;; \
191	esac ; \
192	path=`(cd $$path && /bin/pwd)` ; \
193	${ECHO} ${.TARGET} "->" $$path ; \
194	ln -s $$path ${.TARGET}
195
196CLEANFILES+= ${PROG} ${KMOD}.kld ${OBJS} ${_ILINKS} symb.tmp tmp.o
197
198.if !target(install)
199.if !target(beforeinstall)
200beforeinstall:
201.endif
202.if !target(afterinstall)
203afterinstall:
204.endif
205
206_INSTALLFLAGS:=	${INSTALLFLAGS}
207.for ie in ${INSTALLFLAGS_EDIT}
208_INSTALLFLAGS:=	${_INSTALLFLAGS${ie}}
209.endfor
210
211realinstall: _SUBDIR
212	${INSTALL} ${COPY} -o ${KMODOWN} -g ${KMODGRP} -m ${KMODMODE} \
213	    ${_INSTALLFLAGS} ${PROG} ${DESTDIR}${KMODDIR}
214.if defined(LINKS) && !empty(LINKS)
215	@set ${LINKS}; \
216	while test $$# -ge 2; do \
217		l=${DESTDIR}$$1; \
218		shift; \
219		t=${DESTDIR}$$1; \
220		shift; \
221		${ECHO} $$t -\> $$l; \
222		ln -f $$l $$t; \
223	done; true
224.endif
225.if defined(SYMLINKS) && !empty(SYMLINKS)
226	@set ${SYMLINKS}; \
227	while test $$# -ge 2; do \
228		l=$$1; \
229		shift; \
230		t=${DESTDIR}$$1; \
231		shift; \
232		${ECHO} $$t -\> $$l; \
233		ln -fs $$l $$t; \
234	done; true
235.endif
236
237install: afterinstall _SUBDIR
238.if !defined(NOMAN)
239afterinstall: realinstall maninstall
240.else
241afterinstall: realinstall
242.endif
243realinstall: beforeinstall
244.endif
245
246DISTRIBUTION?=	bin
247.if !target(distribute)
248distribute: _SUBDIR
249.for dist in ${DISTRIBUTION}
250	cd ${.CURDIR} ; $(MAKE) install DESTDIR=${DISTDIR}/${dist} SHARED=copies
251.endfor
252.endif
253
254.if !target(load)
255load:	${PROG}
256	${KMODLOAD} -v ./${KMOD}
257.endif
258
259.if !target(unload)
260unload:
261	${KMODUNLOAD} -v ${KMOD}
262.endif
263
264.for _src in ${SRCS:Mopt_*.h}
265CLEANFILES+=	${_src}
266.if !target(${_src})
267${_src}:
268	touch ${.TARGET}
269.endif
270.endfor
271
272MFILES?= kern/bus_if.m kern/device_if.m dev/iicbus/iicbb_if.m \
273    dev/iicbus/iicbus_if.m isa/isa_if.m dev/mii/miibus_if.m \
274    dev/pccard/card_if.m dev/pccard/power_if.m pci/pci_if.m \
275    dev/ppbus/ppbus_if.m dev/smbus/smbus_if.m dev/usb/usb_if.m
276
277.for _srcsrc in ${MFILES}
278.for _ext in c h
279.for _src in ${SRCS:M${_srcsrc:T:R}.${_ext}}
280CLEANFILES+=	${_src}
281.if !target(${_src})
282${_src}: @
283.if exists(@)
284${_src}: @/kern/makeobjops.pl @/${_srcsrc}
285.endif
286	perl @/kern/makeobjops.pl -${_ext} @/${_srcsrc}
287.endif
288.endfor # _src
289.endfor # _ext
290.endfor # _srcsrc
291
292.for _ext in c h
293.if ${SRCS:Mvnode_if.${_ext}} != ""
294CLEANFILES+=	vnode_if.${_ext}
295vnode_if.${_ext}: @
296.if exists(@)
297vnode_if.${_ext}: @/kern/vnode_if.pl @/kern/vnode_if.src
298.endif
299	perl @/kern/vnode_if.pl -${_ext} @/kern/vnode_if.src
300.endif
301.endfor
302
303regress:
304
305.include <bsd.dep.mk>
306
307.if !exists(${DEPENDFILE})
308${OBJS}: ${SRCS:M*.h}
309.endif
310
311.include <bsd.obj.mk>
312
313.include <bsd.kern.mk>
314