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