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