xref: /dragonfly/sys/conf/kmod.mk (revision 8a7bdfea)
1#	From: @(#)bsd.prog.mk	5.26 (Berkeley) 6/25/91
2# $FreeBSD: src/sys/conf/kmod.mk,v 1.82.2.15 2003/02/10 13:11:50 nyan Exp $
3# $DragonFly: src/sys/conf/kmod.mk,v 1.33 2007/08/26 07:53:25 corecode Exp $
4#
5# The include file <bsd.kmod.mk> handles installing Kernel Loadable Device
6# drivers (KLD's).
7#
8#
9# +++ variables +++
10#
11# CLEANFILES	Additional files to remove for the clean and cleandir targets.
12#
13# KMOD          The name of the kernel module to build.
14#
15# KMODDIR	Base path for kernel modules (see kld(4)). [/modules]
16#
17# KMODOWN	KLD owner. [${BINOWN}]
18#
19# KMODGRP	KLD group. [${BINGRP}]
20#
21# KMODMODE	KLD mode. [${BINMODE}]
22#
23# KMODLOAD	Command to load a kernel module [/sbin/kldload]
24#
25# KMODUNLOAD	Command to unload a kernel module [/sbin/kldunload]
26#
27# NOMAN		KLD does not have a manual page if set.
28#
29# PROG          The name of the kernel module to build.
30#		If not supplied, ${KMOD}.o is used.
31#
32# SRCS          List of source files
33#
34# KMODDEPS	List of modules which this one is dependant on
35#
36# DESTDIR	Change the tree where the module gets installed. [not set]
37#
38# MFILES	Optionally a list of interfaces used by the module.
39#		This file contains a default list of interfaces.
40#
41# +++ targets +++
42#
43# 	install:
44#               install the kernel module and its manual pages; if the Makefile
45#               does not itself define the target install, the targets
46#               beforeinstall and afterinstall may also be used to cause
47#               actions immediately before and after the install target
48#		is executed.
49#
50# 	load:
51#		Load KLD.
52#
53# 	unload:
54#		Unload KLD.
55#
56# bsd.obj.mk: clean, cleandir and obj
57# bsd.dep.mk: cleandepend, depend and tags
58# bsd.man.mk: maninstall
59#
60
61OBJCOPY?=	objcopy
62KMODLOAD?=	/sbin/kldload
63KMODUNLOAD?=	/sbin/kldunload
64
65.include <bsd.init.mk>
66
67.SUFFIXES: .out .o .c .cc .cxx .C .y .l .s .S
68
69CFLAGS+=	${COPTS} -D_KERNEL ${CWARNFLAGS}
70CFLAGS+=	-DKLD_MODULE
71
72# Don't use any standard include directories.
73# Since -nostdinc will annull any previous -I paths, we repeat all
74# such paths after -nostdinc.  It doesn't seem to be possible to
75# add to the front of `make' variable.
76#
77# Don't use -I- anymore, source-relative includes are desireable.
78_ICFLAGS:=	${CFLAGS:M-I*}
79CFLAGS+=	-nostdinc ${_ICFLAGS}
80
81# Add -I paths for system headers.  Individual KLD makefiles don't
82# need any -I paths for this.  Similar defaults for .PATH can't be
83# set because there are no standard paths for non-headers.
84#
85# NOTE!  Traditional architecture paths such as <i386/i386/blah.h>
86# must run through the "machine_base" softlink using
87# <machine_base/i386/blah.h>.  An explicit cross-architecture path must
88# operate relative to /usr/src/sys using e.g. <arch/i386/i386/blah.h>
89#
90CFLAGS+=	-I. -I@
91
92# Add -I paths for headers in the kernel build directory
93#
94.if defined(BUILDING_WITH_KERNEL)
95CFLAGS+=	-I${BUILDING_WITH_KERNEL}
96_MACHINE_FWD=	${BUILDING_WITH_KERNEL}
97.else
98.if defined(MAKEOBJDIRPREFIX)
99_MACHINE_FWD=	${MAKEOBJDIRPREFIX}/${SYSDIR}/forwarder_${MACHINE_ARCH}
100.else
101_MACHINE_FWD=	${.OBJDIR}/forwarder_${MACHINE_ARCH}
102CLEANDIRS+=	${_MACHINE_FWD}
103.endif
104.endif
105CFLAGS+=	-I${_MACHINE_FWD}/include
106.include "kern.fwd.mk"
107
108# Add a -I path to standard headers like <stddef.h>.  Use a relative
109# path to src/include if possible.  If the @ symlink hasn't been built
110# yet, then we can't tell if the relative path exists.  Add both the
111# potential relative path and an absolute path in that case.
112.if exists(@)
113.if exists(@/../include)
114CFLAGS+=	-I@/../include
115.else
116CFLAGS+=	-I${DESTDIR}/usr/include
117.endif
118.else # !@
119CFLAGS+=	-I@/../include -I${DESTDIR}/usr/include
120.endif # @
121
122.if defined(BUILDING_WITH_KERNEL) && \
123    exists(${BUILDING_WITH_KERNEL}/opt_global.h)
124CFLAGS+=	-include ${BUILDING_WITH_KERNEL}/opt_global.h
125.endif
126
127CFLAGS+=	${DEBUG_FLAGS}
128
129.include <bsd.patch.mk>
130
131OBJS+=  ${SRCS:N*.h:N*.patch:R:S/$/.o/g}
132
133.if !defined(PROG)
134PROG=	${KMOD}.ko
135.endif
136
137${PROG}: ${KMOD}.kld ${KMODDEPS}
138	${LD} -Bshareable ${LDFLAGS} -o ${.TARGET} ${KMOD}.kld ${KMODDEPS}
139
140.if defined(KMODDEPS)
141.for dep in ${KMODDEPS}
142CLEANFILES+=	${dep} __${dep}_hack_dep.c
143
144${dep}:
145	touch __${dep}_hack_dep.c
146	${CC} -shared ${CFLAGS} -o ${dep} __${dep}_hack_dep.c
147.endfor
148.endif
149
150${KMOD}.kld: ${OBJS}
151	${LD} ${LDFLAGS} -r -o ${.TARGET} ${OBJS}
152
153.if !defined(NOMAN)
154.include <bsd.man.mk>
155.endif
156
157# links to platform and cpu architecture include files.  If we are
158# building with a kernel these already exist in the kernel build dir.
159# '@' is a link to the system source.
160.if defined(BUILDING_WITH_KERNEL)
161_ILINKS=@
162.else
163_ILINKS=@ machine_base machine cpu_base cpu
164.endif
165
166.if defined(ARCH)
167_ILINKS+=${ARCH}
168.endif
169
170all: objwarn fwheaders ${PROG}
171.if !defined(NOMAN)
172all: _manpages
173.endif
174
175beforedepend: fwheaders
176fwheaders: ${_ILINKS} ${FORWARD_HEADERS_COOKIE}
177# Ensure that the links exist without depending on it when it exists which
178# causes all the modules to be rebuilt when the directory pointed to changes.
179.for _link in ${_ILINKS}
180.if !exists(${.OBJDIR}/${_link})
181${OBJS}: ${_link}
182.endif
183.endfor
184
185# Search for kernel source tree in standard places.
186.for _dir in ${.CURDIR}/../.. ${.CURDIR}/../../.. ${.CURDIR}/../../../.. /sys /usr/src/sys
187.if !defined(SYSDIR) && exists(${_dir}/kern/)
188SYSDIR=	${_dir}
189.endif
190.endfor
191.if !defined(SYSDIR) || !exists(${SYSDIR}/kern)
192.error "can't find kernel source tree"
193.endif
194S=	${SYSDIR}
195
196#	path=`(cd $$path && /bin/pwd)` ;
197
198${_ILINKS}:
199	@case ${.TARGET} in \
200	machine) \
201		path=${SYSDIR}/platform/${MACHINE_PLATFORM}/include ;; \
202	machine_base) \
203		path=${SYSDIR}/platform/${MACHINE_PLATFORM} ;; \
204	cpu) \
205		path=${SYSDIR}/cpu/${MACHINE_ARCH}/include ;; \
206	cpu_base) \
207		path=${SYSDIR}/cpu/${MACHINE_ARCH} ;; \
208	@) \
209		path=${SYSDIR} ;; \
210	arch_*) \
211		path=${.CURDIR}/${MACHINE_ARCH} ;; \
212	esac ; \
213	${ECHO} ${.TARGET} "->" $$path ; \
214	${LN} -s $$path ${.TARGET}
215
216CLEANFILES+= ${PROG} ${KMOD}.kld ${OBJS} ${_ILINKS} symb.tmp tmp.o
217
218.if !target(install)
219
220_INSTALLFLAGS:=	${INSTALLFLAGS}
221.for ie in ${INSTALLFLAGS_EDIT}
222_INSTALLFLAGS:=	${_INSTALLFLAGS${ie}}
223.endfor
224
225.if !target(realinstall)
226realinstall: _kmodinstall
227.ORDER: beforeinstall _kmodinstall
228_kmodinstall:
229.if defined(INSTALLSTRIPPEDMODULES)
230	${INSTALL} -o ${KMODOWN} -g ${KMODGRP} -m ${KMODMODE} \
231	    ${_INSTALLFLAGS} ${PROG} ${DESTDIR}${KMODDIR}
232	${OBJCOPY} --strip-debug ${DESTDIR}${KMODDIR}/${PROG}
233.else
234	${INSTALL} -o ${KMODOWN} -g ${KMODGRP} -m ${KMODMODE} \
235	    ${_INSTALLFLAGS} ${PROG} ${DESTDIR}${KMODDIR}
236.endif
237.endif # !target(realinstall)
238
239.include <bsd.links.mk>
240
241.if !defined(NOMAN)
242realinstall: _maninstall
243.ORDER: beforeinstall _maninstall
244.endif
245
246.endif # !target(install)
247
248.if !target(load)
249load:	${PROG}
250	${KMODLOAD} -v ./${KMOD}.ko
251.endif
252
253.if !target(unload)
254unload:
255	${KMODUNLOAD} -v ${KMOD}
256.endif
257
258.for _src in ${SRCS:Mopt_*.h} ${SRCS:Muse_*.h}
259CLEANFILES+=	${_src}
260.if !target(${_src})
261.if defined(BUILDING_WITH_KERNEL) && exists(${BUILDING_WITH_KERNEL}/${_src})
262${_src}: ${BUILDING_WITH_KERNEL}/${_src}
263# we do not have to copy these files any more, the kernel build
264# directory is included in the path now.
265#	cp ${BUILDING_WITH_KERNEL}/${_src} ${.TARGET}
266.else
267${_src}:
268	touch ${.TARGET}
269.endif	# BUILDING_WITH_KERNEL
270.endif
271.endfor
272
273MFILES?= kern/bus_if.m kern/device_if.m bus/iicbus/iicbb_if.m \
274    bus/iicbus/iicbus_if.m bus/isa/isa_if.m dev/netif/mii_layer/miibus_if.m \
275    bus/pccard/card_if.m bus/pccard/power_if.m bus/pci/pci_if.m \
276    bus/pci/pcib_if.m \
277    bus/ppbus/ppbus_if.m bus/smbus/smbus_if.m bus/usb/usb_if.m \
278    dev/disk/nata/ata_if.m \
279    dev/sound/pcm/ac97_if.m dev/sound/pcm/channel_if.m \
280    dev/sound/pcm/feeder_if.m dev/sound/pcm/mixer_if.m \
281    libiconv/iconv_converter_if.m dev/agp/agp_if.m opencrypto/crypto_if.m
282
283.for _srcsrc in ${MFILES}
284.for _ext in c h
285.for _src in ${SRCS:M${_srcsrc:T:R}.${_ext}}
286CLEANFILES+=	${_src}
287.if !target(${_src})
288${_src}: @
289.if exists(@)
290${_src}: @/tools/makeobjops.awk @/${_srcsrc}
291.endif
292
293.if defined(BUILDING_WITH_KERNEL) && \
294    exists(${BUILDING_WITH_KERNEL}/${_src})
295.else
296	awk -f @/tools/makeobjops.awk -- -${_ext} @/${_srcsrc}
297.endif
298.endif
299.endfor # _src
300.endfor # _ext
301.endfor # _srcsrc
302
303#.for _ext in c h
304#.if ${SRCS:Mvnode_if.${_ext}} != ""
305#CLEANFILES+=	vnode_if.${_ext}
306#vnode_if.${_ext}: @
307#.if exists(@)
308#vnode_if.${_ext}: @/tools/vnode_if.awk @/kern/vnode_if.src
309#.endif
310#	awk -f @/tools/vnode_if.awk -- -${_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.include "bsd.kern.mk"
324
325# Behaves like MODULE_OVERRIDE
326.if defined(KLD_DEPS)
327all: _kdeps_all
328_kdeps_all: @
329.for _mdep in ${KLD_DEPS}
330	cd ${SYSDIR}/${_mdep} && make all
331.endfor
332depend: _kdeps_depend
333_kdeps_depend: @
334.for _mdep in ${KLD_DEPS}
335	cd ${SYSDIR}/${_mdep} && make depend
336.endfor
337install: _kdeps_install
338_kdeps_install: @
339.for _mdep in ${KLD_DEPS}
340	cd ${SYSDIR}/${_mdep} && make install
341.endfor
342clean: _kdeps_clean
343_kdeps_clean: @
344.for _mdep in ${KLD_DEPS}
345	cd ${SYSDIR}/${_mdep} && make clean
346.endfor
347.endif
348