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