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