xref: /dragonfly/sys/conf/kmod.mk (revision b3c6ec2b)
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	List of firmware images in format filename:shortname:version
39#
40# FIRMWARE_LICENSE
41#		Set to the name of the license the user has to agree on in
42#		order to use this firmware. See /usr/share/doc/legal
43#
44# +++ targets +++
45#
46# 	install:
47#               install the kernel module and its manual pages; if the Makefile
48#               does not itself define the target install, the targets
49#               beforeinstall and afterinstall may also be used to cause
50#               actions immediately before and after the install target
51#		is executed.
52#
53# 	load:
54#		Load KLD.
55#
56# 	unload:
57#		Unload KLD.
58#
59# bsd.obj.mk: clean, cleandir and obj
60# bsd.dep.mk: cleandepend, depend and tags
61#
62
63OBJCOPY?=	objcopy
64KMODLOAD?=	/sbin/kldload
65KMODUNLOAD?=	/sbin/kldunload
66
67# KERNEL is needed when running make install directly from
68# the obj directory.
69KERNEL?=	kernel
70
71KMODDIR?=	${DESTKERNDIR}
72KMODOWN?=	${BINOWN}
73KMODGRP?=	${BINGRP}
74KMODMODE?=	${BINMODE}
75
76.include <bsd.init.mk>
77
78.SUFFIXES: .out .o .c .cc .cxx .C .y .l .s .S
79
80.if !defined(NO_WERROR) && (${CCVER} == "gcc47" || ${CCVER} == "gcc50")
81WERROR=-Werror
82.endif
83
84WORLD_CCOPTLEVEL=	# XXX prevent world opt level affecting kernel modules
85CFLAGS+=	${COPTS} -D_KERNEL ${CWARNFLAGS} ${WERROR}
86CFLAGS+=	-DKLD_MODULE
87
88# Don't use any standard include directories.
89# Since -nostdinc will annull any previous -I paths, we repeat all
90# such paths after -nostdinc.  It doesn't seem to be possible to
91# add to the front of `make' variable.
92#
93# Don't use -I- anymore, source-relative includes are desireable.
94_ICFLAGS:=	${CFLAGS:M-I*}
95CFLAGS+=	-nostdinc ${_ICFLAGS}
96
97# Add -I paths for system headers.  Individual KLD makefiles don't
98# need any -I paths for this.  Similar defaults for .PATH can't be
99# set because there are no standard paths for non-headers.
100#
101# NOTE!  Traditional architecture paths such as <i386/i386/blah.h>
102# must run through the "machine_base" softlink using
103# <machine_base/i386/blah.h>.  An explicit cross-architecture path must
104# operate relative to /usr/src/sys using e.g. <arch/i386/i386/blah.h>
105#
106CFLAGS+=	-I. -I@
107
108# Add -I paths for headers in the kernel build directory
109#
110.if defined(BUILDING_WITH_KERNEL)
111CFLAGS+=	-I${BUILDING_WITH_KERNEL}
112_MACHINE_FWD=	${BUILDING_WITH_KERNEL}
113.else
114.if defined(MAKEOBJDIRPREFIX)
115_MACHINE_FWD=	${MAKEOBJDIRPREFIX}/${SYSDIR}/forwarder_${MACHINE_ARCH}
116.else
117_MACHINE_FWD=	${.OBJDIR}/forwarder_${MACHINE_ARCH}
118CLEANDIRS+=	${_MACHINE_FWD}
119.endif
120.endif
121CFLAGS+=	-I${_MACHINE_FWD}/include
122.include "kern.fwd.mk"
123
124# Add a -I path to standard headers like <stddef.h>.  Use a relative
125# path to src/include if possible.  If the @ symlink hasn't been built
126# yet, then we can't tell if the relative path exists.  Add both the
127# potential relative path and an absolute path in that case.
128.if exists(@)
129.if exists(@/../include)
130CFLAGS+=	-I@/../include
131.else
132CFLAGS+=	-I${DESTDIR}/usr/include
133.endif
134.else # !@
135CFLAGS+=	-I@/../include -I${DESTDIR}/usr/include
136.endif # @
137
138.if defined(BUILDING_WITH_KERNEL) && \
139    exists(${BUILDING_WITH_KERNEL}/opt_global.h)
140CFLAGS+=	-DHAVE_KERNEL_OPTION_HEADERS -include ${BUILDING_WITH_KERNEL}/opt_global.h
141.endif
142
143CFLAGS+=	${DEBUG_FLAGS}
144.if ${MACHINE_ARCH} == "x86_64"
145CFLAGS+=	-fno-omit-frame-pointer
146.endif
147
148.include <bsd.patch.mk>
149
150.if defined(FIRMWS)
151AWK=/usr/bin/awk
152.if !exists(@)
153${KMOD:S/$/.c/}: @
154.else
155${KMOD:S/$/.c/}: @/tools/fw_stub.awk
156.endif
157	${AWK} -f @/tools/fw_stub.awk ${FIRMWS} -m${KMOD} -c${KMOD:S/$/.c/g} \
158	    ${FIRMWARE_LICENSE:C/.+/-l/}${FIRMWARE_LICENSE}
159
160SRCS+=	${KMOD:S/$/.c/}
161CLEANFILES+=	${KMOD:S/$/.c/}
162
163.for _firmw in ${FIRMWS}
164${_firmw:C/\:.*$/.fwo/}:	${_firmw:C/\:.*$//}
165	@${ECHO} ${_firmw:C/\:.*$//} ${.ALLSRC:M*${_firmw:C/\:.*$//}}
166	@if [ -e ${_firmw:C/\:.*$//} ]; then			\
167		${LD} -b binary --no-warn-mismatch ${LDFLAGS}	\
168		    -r -d -o ${.TARGET}	${_firmw:C/\:.*$//};	\
169	else							\
170		ln -s ${.ALLSRC:M*${_firmw:C/\:.*$//}} ${_firmw:C/\:.*$//}; \
171		${LD} -b binary --no-warn-mismatch ${LDFLAGS}	\
172		    -r -d -o ${.TARGET}	${_firmw:C/\:.*$//};	\
173		rm ${_firmw:C/\:.*$//};				\
174	fi
175
176OBJS+=	${_firmw:C/\:.*$/.fwo/}
177.endfor
178.endif
179
180OBJS+=  ${SRCS:N*.h:N*.patch:R:S/$/.o/g}
181
182.if !defined(PROG)
183PROG=	${KMOD}.ko
184.endif
185
186.if ${MACHINE_ARCH} != x86_64
187${PROG}: ${KMOD}.kld
188	${LD} -Bshareable ${LDFLAGS} -o ${.TARGET} ${KMOD}.kld
189.endif
190
191.if ${MACHINE_ARCH} != x86_64
192${KMOD}.kld: ${OBJS}
193	${LD} ${LDFLAGS} -r -o ${.TARGET} ${OBJS}
194.else
195${PROG}: ${OBJS}
196	${LD} ${LDFLAGS} -r -d -o ${.TARGET} ${OBJS}
197.endif
198
199# links to platform and cpu architecture include files.  If we are
200# building with a kernel these already exist in the kernel build dir.
201# '@' is a link to the system source.
202.if defined(BUILDING_WITH_KERNEL)
203_ILINKS=@
204.else
205_ILINKS=@ machine_base machine cpu_base cpu
206.endif
207
208.if defined(ARCH)
209_ILINKS+=${ARCH}
210.endif
211
212all: objwarn fwheaders ${PROG}
213
214beforedepend: fwheaders
215fwheaders: ${_ILINKS} ${FORWARD_HEADERS_COOKIE}
216# Ensure that the links exist without depending on it when it exists which
217# causes all the modules to be rebuilt when the directory pointed to changes.
218.for _link in ${_ILINKS}
219.if !exists(${.OBJDIR}/${_link})
220${OBJS}: ${_link}
221.endif
222.endfor
223
224# Search for kernel source tree in standard places.
225.for _dir in ${.CURDIR}/../.. ${.CURDIR}/../../.. ${.CURDIR}/../../../.. /sys /usr/src/sys
226.if !defined(SYSDIR) && exists(${_dir}/kern/)
227SYSDIR=	${_dir}
228.endif
229.endfor
230.if !defined(SYSDIR) || !exists(${SYSDIR}/kern)
231.error "can't find kernel source tree"
232.endif
233S=	${SYSDIR}
234
235#	path=`(cd $$path && /bin/pwd)` ;
236
237${_ILINKS}:
238	@case ${.TARGET} in \
239	machine) \
240		path=${SYSDIR}/platform/${MACHINE_PLATFORM}/include ;; \
241	machine_base) \
242		path=${SYSDIR}/platform/${MACHINE_PLATFORM} ;; \
243	cpu) \
244		path=${SYSDIR}/cpu/${MACHINE_ARCH}/include ;; \
245	cpu_base) \
246		path=${SYSDIR}/cpu/${MACHINE_ARCH} ;; \
247	@) \
248		path=${SYSDIR} ;; \
249	arch_*) \
250		path=${.CURDIR}/${MACHINE_ARCH} ;; \
251	esac ; \
252	${ECHO} ${.TARGET} "->" $$path ; \
253	${LN} -s $$path ${.TARGET}
254
255CLEANFILES+= ${PROG} ${KMOD}.kld ${OBJS} ${_ILINKS} symb.tmp tmp.o
256
257.if !target(install)
258
259_INSTALLFLAGS:=	${INSTALLFLAGS}
260.for ie in ${INSTALLFLAGS_EDIT}
261_INSTALLFLAGS:=	${_INSTALLFLAGS${ie}}
262.endfor
263
264.if !target(realinstall)
265realinstall: _kmodinstall
266.ORDER: beforeinstall _kmodinstall
267_kmodinstall:
268.if defined(INSTALLSTRIPPEDMODULES)
269	${INSTALL} -o ${KMODOWN} -g ${KMODGRP} -m ${KMODMODE} \
270	    ${_INSTALLFLAGS} ${PROG} ${DESTDIR}${KMODDIR}
271	${OBJCOPY} --strip-debug ${DESTDIR}${KMODDIR}/${PROG}
272.else
273	${INSTALL} -o ${KMODOWN} -g ${KMODGRP} -m ${KMODMODE} \
274	    ${_INSTALLFLAGS} ${PROG} ${DESTDIR}${KMODDIR}
275.endif
276.endif # !target(realinstall)
277
278.include <bsd.links.mk>
279
280.endif # !target(install)
281
282.if !target(load)
283load:	${PROG}
284	${KMODLOAD} -v ./${KMOD}.ko
285.endif
286
287.if !target(unload)
288unload:
289	${KMODUNLOAD} -v ${KMOD}
290.endif
291
292.for _src in ${SRCS:Mopt_*.h} ${SRCS:Muse_*.h}
293CLEANFILES+=	${_src}
294.if !target(${_src})
295.if defined(BUILDING_WITH_KERNEL) && exists(${BUILDING_WITH_KERNEL}/${_src})
296${_src}: ${BUILDING_WITH_KERNEL}/${_src}
297# we do not have to copy these files any more, the kernel build
298# directory is included in the path now.
299#	cp ${BUILDING_WITH_KERNEL}/${_src} ${.TARGET}
300.else
301${_src}:
302	touch ${.TARGET}
303.endif	# BUILDING_WITH_KERNEL
304.endif
305.endfor
306
307MFILES?= kern/bus_if.m kern/device_if.m bus/iicbus/iicbb_if.m \
308    bus/iicbus/iicbus_if.m bus/isa/isa_if.m dev/netif/mii_layer/miibus_if.m \
309    bus/pccard/card_if.m bus/pccard/power_if.m bus/pci/pci_if.m \
310    bus/pci/pcib_if.m \
311    bus/ppbus/ppbus_if.m bus/smbus/smbus_if.m bus/u4b/usb_if.m \
312    dev/acpica/acpi_if.m dev/acpica/acpi_wmi_if.m dev/disk/nata/ata_if.m \
313    dev/disk/sdhci/sdhci_if.m \
314    dev/sound/pci/hda/hdac_if.m \
315    dev/sound/pcm/ac97_if.m dev/sound/pcm/channel_if.m \
316    dev/sound/pcm/feeder_if.m dev/sound/pcm/mixer_if.m \
317    dev/sound/midi/mpu_if.m dev/sound/midi/mpufoi_if.m \
318    dev/sound/midi/synth_if.m  \
319    libiconv/iconv_converter_if.m dev/agp/agp_if.m opencrypto/cryptodev_if.m \
320    bus/mmc/mmcbus_if.m bus/mmc/mmcbr_if.m \
321    dev/virtual/virtio/virtio/virtio_bus_if.m \
322    dev/virtual/virtio/virtio/virtio_if.m \
323    dev/misc/coremctl/coremctl_if.m kern/cpu_if.m \
324    bus/gpio/gpio_if.m
325
326.for _srcsrc in ${MFILES}
327.for _ext in c h
328.for _src in ${SRCS:M${_srcsrc:T:R}.${_ext}}
329CLEANFILES+=	${_src}
330.if !target(${_src})
331${_src}: @
332.if exists(@)
333${_src}: @/tools/makeobjops.awk @/${_srcsrc}
334.endif
335
336.if defined(BUILDING_WITH_KERNEL) && \
337    exists(${BUILDING_WITH_KERNEL}/${_src})
338.else
339	awk -f @/tools/makeobjops.awk -- -${_ext} @/${_srcsrc}
340.endif
341.endif
342.endfor # _src
343.endfor # _ext
344.endfor # _srcsrc
345
346#.for _ext in c h
347#.if ${SRCS:Mvnode_if.${_ext}} != ""
348#CLEANFILES+=	vnode_if.${_ext}
349#vnode_if.${_ext}: @
350#.if exists(@)
351#vnode_if.${_ext}: @/tools/vnode_if.awk @/kern/vnode_if.src
352#.endif
353#	awk -f @/tools/vnode_if.awk -- -${_ext} @/kern/vnode_if.src
354#.endif
355#.endfor
356
357.if !empty(SRCS:Mmiidevs.h)
358CLEANFILES+=	miidevs.h
359.if !exists(@)
360miidevs.h: @
361.else
362miidevs.h: @/tools/miidevs2h.awk @/dev/netif/mii_layer/miidevs
363.endif
364	${AWK} -f @/tools/miidevs2h.awk @/dev/netif/mii_layer/miidevs
365.endif
366
367.if !empty(SRCS:Mpccarddevs.h)
368CLEANFILES+=	pccarddevs.h
369.if !exists(@)
370pccarddevs.h: @
371.else
372pccarddevs.h: @/tools/pccarddevs2h.awk @/bus/pccard/pccarddevs
373.endif
374	${AWK} -f @/tools/pccarddevs2h.awk @/bus/pccard/pccarddevs
375.endif
376
377.if !empty(SRCS:Mpcidevs.h)
378CLEANFILES+=	pcidevs.h
379.if !exists(@)
380pcidevs.h: @
381.else
382pcidevs.h: @/tools/pcidevs2h.awk @/bus/pci/pcidevs
383.endif
384	${AWK} -f @/tools/pcidevs2h.awk @/bus/pci/pcidevs
385.endif
386
387.if !empty(SRCS:Musbdevs.h)
388CLEANFILES+=	usbdevs.h
389.if !exists(@)
390usbdevs.h: @
391.else
392usbdevs.h: @/tools/usbdevs2h.awk @/bus/u4b/usbdevs
393.endif
394	${AWK} -f @/tools/usbdevs2h.awk @/bus/u4b/usbdevs -h
395.endif
396
397.if !empty(SRCS:Musbdevs_data.h)
398CLEANFILES+=	usbdevs_data.h
399.if !exists(@)
400usbdevs_data.h: @
401.else
402usbdevs_data.h: @/tools/usbdevs2h.awk @/bus/u4b/usbdevs
403.endif
404	${AWK} -f @/tools/usbdevs2h.awk @/bus/u4b/usbdevs -d
405.endif
406
407.if !empty(SRCS:Macpi_quirks.h)
408CLEANFILES+=	acpi_quirks.h
409.if !exists(@)
410acpi_quirks.h: @
411.else
412acpi_quirks.h: @/tools/acpi_quirks2h.awk @/dev/acpica/acpi_quirks
413.endif
414	${AWK} -f @/tools/acpi_quirks2h.awk @/dev/acpica/acpi_quirks
415.endif
416
417.if !empty(SRCS:Massym.s)
418CLEANFILES+=	assym.s genassym.o
419assym.s: genassym.o
420.if defined(BUILDING_WITH_KERNEL)
421genassym.o: opt_global.h
422.endif
423.if !exists(@)
424assym.s: @
425.else
426assym.s: @/kern/genassym.sh
427.endif
428	sh @/kern/genassym.sh genassym.o > ${.TARGET}
429.if exists(@)
430genassym.o: @/platform/${MACHINE_PLATFORM}/${MACHINE_ARCH}/genassym.c
431.endif
432genassym.o: @ ${SRCS:Mopt_*.h}
433	${CC} -c ${CFLAGS:N-fno-common:N-mcmodel=small} ${WERROR} \
434	@/platform/${MACHINE_PLATFORM}/${MACHINE_ARCH}/genassym.c
435.endif
436
437regress:
438
439.include <bsd.dep.mk>
440
441.if !exists(${DEPENDFILE})
442${OBJS}: ${SRCS:M*.h}
443.endif
444
445.include <bsd.obj.mk>
446.include "bsd.kern.mk"
447
448# Behaves like MODULE_OVERRIDE
449.if defined(KLD_DEPS)
450all: _kdeps_all
451_kdeps_all: @
452.for _mdep in ${KLD_DEPS}
453	cd ${SYSDIR}/${_mdep} && make all
454.endfor
455depend: _kdeps_depend
456_kdeps_depend: @
457.for _mdep in ${KLD_DEPS}
458	cd ${SYSDIR}/${_mdep} && make depend
459.endfor
460install: _kdeps_install
461_kdeps_install: @
462.for _mdep in ${KLD_DEPS}
463	cd ${SYSDIR}/${_mdep} && make install
464.endfor
465clean: _kdeps_clean
466_kdeps_clean: @
467.for _mdep in ${KLD_DEPS}
468	cd ${SYSDIR}/${_mdep} && make clean
469.endfor
470.endif
471