xref: /freebsd/sys/conf/kmod.mk (revision cc4a90c445aa04be36c3ef745cbe67fa339b94b5)
1#	From: @(#)bsd.prog.mk	5.26 (Berkeley) 6/25/91
2# $FreeBSD$
3#
4# The include file <bsd.kmod.mk> handles building and installing loadable
5# kernel modules.
6#
7#
8# +++ variables +++
9#
10# CLEANFILES	Additional files to remove for the clean and cleandir targets.
11#
12# EXPORT_SYMS	A list of symbols that should be exported from the module,
13#		or the name of a file containing a list of symbols, or YES
14#		to export all symbols.  If not defined, no symbols are
15#		exported.
16#
17# KMOD		The name of the kernel module to build.
18#
19# KMODDIR	Base path for kernel modules (see kld(4)). [/boot/kernel]
20#
21# KMODOWN	Module file owner. [${BINOWN}]
22#
23# KMODGRP	Module file group. [${BINGRP}]
24#
25# KMODMODE	Module file mode. [${BINMODE}]
26#
27# KMODLOAD	Command to load a kernel module [/sbin/kldload]
28#
29# KMODUNLOAD	Command to unload a kernel module [/sbin/kldunload]
30#
31# MFILES	Optionally a list of interfaces used by the module.
32#		This file contains a default list of interfaces.
33#
34# PROG		The name of the kernel module to build.
35#		If not supplied, ${KMOD}.ko is used.
36#
37# SRCS		List of source files.
38#
39# FIRMWS	List of firmware images in format filename:shortname:version
40#
41# FIRMWARE_LICENSE
42#		Set to the name of the license the user has to agree on in
43#		order to use this firmware. See /usr/share/doc/legal
44#
45# DESTDIR	The tree where the module gets installed. [not set]
46#
47# +++ targets +++
48#
49# 	install:
50#               install the kernel module; 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 a module.
58#
59# 	unload:
60#		Unload a module.
61#
62
63AWK?=		awk
64KMODLOAD?=	/sbin/kldload
65KMODUNLOAD?=	/sbin/kldunload
66OBJCOPY?=	objcopy
67
68.include <bsd.init.mk>
69# Grab all the options for a kernel build. For backwards compat, we need to
70# do this after bsd.own.mk.
71.include "kern.opts.mk"
72.include <bsd.compiler.mk>
73
74.SUFFIXES: .out .o .c .cc .cxx .C .y .l .s .S
75
76# amd64 and mips use direct linking for kmod, all others use shared binaries
77.if ${MACHINE_CPUARCH} != amd64 && ${MACHINE_CPUARCH} != mips
78__KLD_SHARED=yes
79.else
80__KLD_SHARED=no
81.endif
82
83.if !empty(CFLAGS:M-O[23s]) && empty(CFLAGS:M-fno-strict-aliasing)
84CFLAGS+=	-fno-strict-aliasing
85.endif
86WERROR?=	-Werror
87CFLAGS+=	${WERROR}
88CFLAGS+=	-D_KERNEL
89CFLAGS+=	-DKLD_MODULE
90
91# Don't use any standard or source-relative include directories.
92NOSTDINC=	-nostdinc
93CFLAGS:=	${CFLAGS:N-I*} ${NOSTDINC} ${INCLMAGIC} ${CFLAGS:M-I*}
94.if defined(KERNBUILDDIR)
95CFLAGS+=	-DHAVE_KERNEL_OPTION_HEADERS -include ${KERNBUILDDIR}/opt_global.h
96.endif
97
98# Add -I paths for system headers.  Individual module makefiles don't
99# need any -I paths for this.  Similar defaults for .PATH can't be
100# set because there are no standard paths for non-headers.
101CFLAGS+=	-I. -I${SYSDIR}
102
103# Add -I path for altq headers as they are included via net/if_var.h
104# for example.
105CFLAGS+=	-I${SYSDIR}/contrib/altq
106
107CFLAGS.gcc+=	-finline-limit=${INLINE_LIMIT}
108CFLAGS.gcc+=	-fms-extensions
109CFLAGS.gcc+= --param inline-unit-growth=100
110CFLAGS.gcc+= --param large-function-growth=1000
111
112# Disallow common variables, and if we end up with commons from
113# somewhere unexpected, allocate storage for them in the module itself.
114CFLAGS+=	-fno-common
115LDFLAGS+=	-d -warn-common
116
117CFLAGS+=	${DEBUG_FLAGS}
118.if ${MACHINE_CPUARCH} == amd64
119CFLAGS+=	-fno-omit-frame-pointer -mno-omit-leaf-frame-pointer
120.endif
121
122# Temporary workaround for PR 196407, which contains the fascinating details.
123# Don't allow clang to use fpu instructions or registers in kernel modules.
124.if ${MACHINE_CPUARCH} == arm
125CFLAGS.clang+=	-mllvm -arm-use-movt=0
126CFLAGS.clang+=	-mfpu=none
127.endif
128
129.if ${MACHINE_CPUARCH} == powerpc
130CFLAGS+=	-mlongcall -fno-omit-frame-pointer
131.endif
132
133.if ${MACHINE_CPUARCH} == mips
134CFLAGS+=	-G0 -fno-pic -mno-abicalls -mlong-calls
135.endif
136
137.if defined(DEBUG) || defined(DEBUG_FLAGS)
138CTFFLAGS+=	-g
139.endif
140
141.if defined(FIRMWS)
142${KMOD:S/$/.c/}: ${SYSDIR}/tools/fw_stub.awk
143	${AWK} -f ${SYSDIR}/tools/fw_stub.awk ${FIRMWS} -m${KMOD} -c${KMOD:S/$/.c/g} \
144	    ${FIRMWARE_LICENSE:C/.+/-l/}${FIRMWARE_LICENSE}
145
146SRCS+=	${KMOD:S/$/.c/}
147CLEANFILES+=	${KMOD:S/$/.c/}
148
149.for _firmw in ${FIRMWS}
150${_firmw:C/\:.*$/.fwo/}:	${_firmw:C/\:.*$//}
151	@${ECHO} ${_firmw:C/\:.*$//} ${.ALLSRC:M*${_firmw:C/\:.*$//}}
152	@if [ -e ${_firmw:C/\:.*$//} ]; then			\
153		${LD} -b binary --no-warn-mismatch ${_LDFLAGS}	\
154		    -r -d -o ${.TARGET}	${_firmw:C/\:.*$//};	\
155	else							\
156		ln -s ${.ALLSRC:M*${_firmw:C/\:.*$//}} ${_firmw:C/\:.*$//}; \
157		${LD} -b binary --no-warn-mismatch ${_LDFLAGS}	\
158		    -r -d -o ${.TARGET}	${_firmw:C/\:.*$//};	\
159		rm ${_firmw:C/\:.*$//};				\
160	fi
161
162OBJS+=	${_firmw:C/\:.*$/.fwo/}
163.endfor
164.endif
165
166OBJS+=	${SRCS:N*.h:R:S/$/.o/g}
167
168.if !defined(PROG)
169PROG=	${KMOD}.ko
170.endif
171
172.if !defined(DEBUG_FLAGS)
173FULLPROG=	${PROG}
174.else
175FULLPROG=	${PROG}.debug
176${PROG}: ${FULLPROG} ${PROG}.symbols
177	${OBJCOPY} --strip-debug --add-gnu-debuglink=${PROG}.symbols\
178	    ${FULLPROG} ${.TARGET}
179${PROG}.symbols: ${FULLPROG}
180	${OBJCOPY} --only-keep-debug ${FULLPROG} ${.TARGET}
181.endif
182
183.if ${__KLD_SHARED} == yes
184${FULLPROG}: ${KMOD}.kld
185	${LD} -Bshareable ${_LDFLAGS} -o ${.TARGET} ${KMOD}.kld
186.if !defined(DEBUG_FLAGS)
187	${OBJCOPY} --strip-debug ${.TARGET}
188.endif
189.endif
190
191EXPORT_SYMS?=	NO
192.if ${EXPORT_SYMS} != YES
193CLEANFILES+=	export_syms
194.endif
195
196.if ${__KLD_SHARED} == yes
197${KMOD}.kld: ${OBJS}
198.else
199${FULLPROG}: ${OBJS}
200.endif
201	${LD} ${_LDFLAGS} -r -d -o ${.TARGET} ${OBJS}
202.if ${MK_CTF} != "no"
203	${CTFMERGE} ${CTFFLAGS} -o ${.TARGET} ${OBJS}
204.endif
205.if defined(EXPORT_SYMS)
206.if ${EXPORT_SYMS} != YES
207.if ${EXPORT_SYMS} == NO
208	:> export_syms
209.elif !exists(${.CURDIR}/${EXPORT_SYMS})
210	echo ${EXPORT_SYMS} > export_syms
211.else
212	grep -v '^#' < ${EXPORT_SYMS} > export_syms
213.endif
214	awk -f ${SYSDIR}/conf/kmod_syms.awk ${.TARGET} \
215	    export_syms | xargs -J% ${OBJCOPY} % ${.TARGET}
216.endif
217.endif
218.if !defined(DEBUG_FLAGS) && ${__KLD_SHARED} == no
219	${OBJCOPY} --strip-debug ${.TARGET}
220.endif
221
222_ILINKS=machine
223.if ${MACHINE} != ${MACHINE_CPUARCH}
224_ILINKS+=${MACHINE_CPUARCH}
225.endif
226.if ${MACHINE_CPUARCH} == "i386" || ${MACHINE_CPUARCH} == "amd64"
227_ILINKS+=x86
228.endif
229CLEANFILES+=${_ILINKS}
230
231all: objwarn ${PROG}
232
233beforedepend: ${_ILINKS}
234
235# Ensure that the links exist without depending on it when it exists which
236# causes all the modules to be rebuilt when the directory pointed to changes.
237.for _link in ${_ILINKS}
238.if !exists(${.OBJDIR}/${_link})
239${OBJS}: ${.OBJDIR}/${_link}
240.endif
241.endfor
242
243# Search for kernel source tree in standard places.
244.for _dir in ${.CURDIR}/../.. ${.CURDIR}/../../.. /sys /usr/src/sys
245.if !defined(SYSDIR) && exists(${_dir}/kern/)
246SYSDIR=	${_dir}
247.endif
248.endfor
249.if !defined(SYSDIR) || !exists(${SYSDIR}/kern/)
250.error "can't find kernel source tree"
251.endif
252
253.for _link in ${_ILINKS}
254.PHONY: ${_link}
255${_link}: ${.OBJDIR}/${_link}
256
257${.OBJDIR}/${_link}:
258	@case ${.TARGET:T} in \
259	machine) \
260		path=${SYSDIR}/${MACHINE}/include ;; \
261	*) \
262		path=${SYSDIR}/${.TARGET:T}/include ;; \
263	esac ; \
264	path=`(cd $$path && /bin/pwd)` ; \
265	${ECHO} ${.TARGET:T} "->" $$path ; \
266	ln -sf $$path ${.TARGET:T}
267.endfor
268
269CLEANFILES+= ${PROG} ${KMOD}.kld ${OBJS}
270
271.if defined(DEBUG_FLAGS)
272CLEANFILES+= ${FULLPROG} ${PROG}.symbols
273.endif
274
275.if !target(install)
276
277_INSTALLFLAGS:=	${INSTALLFLAGS}
278.for ie in ${INSTALLFLAGS_EDIT}
279_INSTALLFLAGS:=	${_INSTALLFLAGS${ie}}
280.endfor
281
282.if !target(realinstall)
283realinstall: _kmodinstall
284.ORDER: beforeinstall _kmodinstall
285_kmodinstall:
286	${INSTALL} -o ${KMODOWN} -g ${KMODGRP} -m ${KMODMODE} \
287	    ${_INSTALLFLAGS} ${PROG} ${DESTDIR}${KMODDIR}
288.if defined(DEBUG_FLAGS) && !defined(INSTALL_NODEBUG) && ${MK_KERNEL_SYMBOLS} != "no"
289	${INSTALL} -o ${KMODOWN} -g ${KMODGRP} -m ${KMODMODE} \
290	    ${_INSTALLFLAGS} ${PROG}.symbols ${DESTDIR}${KMODDIR}
291.endif
292
293.include <bsd.links.mk>
294
295.if !defined(NO_XREF)
296afterinstall: _kldxref
297.ORDER: realinstall _kldxref
298.ORDER: _installlinks _kldxref
299_kldxref:
300	@if type kldxref >/dev/null 2>&1; then \
301		${ECHO} kldxref ${DESTDIR}${KMODDIR}; \
302		kldxref ${DESTDIR}${KMODDIR}; \
303	fi
304.endif
305.endif # !target(realinstall)
306
307.endif # !target(install)
308
309.if !target(load)
310load: ${PROG}
311	${KMODLOAD} -v ${.OBJDIR}/${PROG}
312.endif
313
314.if !target(unload)
315unload:
316	${KMODUNLOAD} -v ${PROG}
317.endif
318
319# Generate options files that otherwise would be built
320# in substantially similar ways through the tree. Move
321# the code here when they all produce identical results
322# (or should)
323.if !defined(KERNBUILDDIR)
324opt_bpf.h:
325	echo "#define DEV_BPF 1" > ${.TARGET}
326.if ${MK_INET_SUPPORT} != "no"
327opt_inet.h:
328	@echo "#define INET 1" > ${.TARGET}
329	@echo "#define TCP_OFFLOAD 1" >> ${.TARGET}
330.endif
331.if ${MK_INET6_SUPPORT} != "no"
332opt_inet6.h:
333	@echo "#define INET6 1" > ${.TARGET}
334.endif
335opt_mrouting.h:
336	echo "#define MROUTING 1" > ${.TARGET}
337opt_natm.h:
338	echo "#define NATM 1" > ${.TARGET}
339opt_scsi.h:
340	echo "#define SCSI_DELAY 15000" > ${.TARGET}
341opt_wlan.h:
342	echo "#define IEEE80211_DEBUG 1" > ${.TARGET}
343	echo "#define IEEE80211_AMPDU_AGE 1" >> ${.TARGET}
344	echo "#define IEEE80211_SUPPORT_MESH 1" >> ${.TARGET}
345.endif
346
347.if defined(KERNBUILDDIR)
348.PATH: ${KERNBUILDDIR}
349CFLAGS+=	-I${KERNBUILDDIR}
350.for _src in ${SRCS:Mopt_*.h}
351CLEANFILES+=	${_src}
352.if !target(${_src})
353${_src}:
354	ln -sf ${KERNBUILDDIR}/${_src} ${.TARGET}
355.endif
356.endfor
357.else
358.for _src in ${SRCS:Mopt_*.h}
359CLEANFILES+=	${_src}
360.if !target(${_src})
361${_src}:
362	:> ${.TARGET}
363.endif
364.endfor
365.endif
366
367# Respect configuration-specific C flags.
368CFLAGS+=	${CONF_CFLAGS}
369
370MFILES?= dev/acpica/acpi_if.m dev/acpi_support/acpi_wmi_if.m \
371	dev/agp/agp_if.m dev/ata/ata_if.m dev/eisa/eisa_if.m \
372	dev/fb/fb_if.m dev/gpio/gpio_if.m dev/gpio/gpiobus_if.m \
373	dev/iicbus/iicbb_if.m dev/iicbus/iicbus_if.m \
374	dev/mbox/mbox_if.m dev/mmc/mmcbr_if.m dev/mmc/mmcbus_if.m \
375	dev/mii/miibus_if.m dev/mvs/mvs_if.m dev/ofw/ofw_bus_if.m \
376	dev/pccard/card_if.m dev/pccard/power_if.m dev/pci/pci_if.m \
377	dev/pci/pcib_if.m dev/ppbus/ppbus_if.m \
378	dev/sdhci/sdhci_if.m dev/smbus/smbus_if.m dev/spibus/spibus_if.m \
379	dev/sound/pci/hda/hdac_if.m \
380	dev/sound/pcm/ac97_if.m dev/sound/pcm/channel_if.m \
381	dev/sound/pcm/feeder_if.m dev/sound/pcm/mixer_if.m \
382	dev/sound/midi/mpu_if.m dev/sound/midi/mpufoi_if.m \
383	dev/sound/midi/synth_if.m dev/usb/usb_if.m isa/isa_if.m \
384	kern/bus_if.m kern/clock_if.m \
385	kern/cpufreq_if.m kern/device_if.m kern/serdev_if.m \
386	libkern/iconv_converter_if.m opencrypto/cryptodev_if.m \
387	pc98/pc98/canbus_if.m dev/etherswitch/mdio_if.m
388
389.for _srcsrc in ${MFILES}
390.for _ext in c h
391.for _src in ${SRCS:M${_srcsrc:T:R}.${_ext}}
392CLEANFILES+=	${_src}
393.if !target(${_src})
394${_src}: ${SYSDIR}/tools/makeobjops.awk ${SYSDIR}/${_srcsrc}
395	${AWK} -f ${SYSDIR}/tools/makeobjops.awk ${SYSDIR}/${_srcsrc} -${_ext}
396.endif
397.endfor # _src
398.endfor # _ext
399.endfor # _srcsrc
400
401.if !empty(SRCS:Mvnode_if.c)
402CLEANFILES+=	vnode_if.c
403vnode_if.c: ${SYSDIR}/tools/vnode_if.awk ${SYSDIR}/kern/vnode_if.src
404	${AWK} -f ${SYSDIR}/tools/vnode_if.awk ${SYSDIR}/kern/vnode_if.src -c
405.endif
406
407.if !empty(SRCS:Mvnode_if.h)
408CLEANFILES+=	vnode_if.h vnode_if_newproto.h vnode_if_typedef.h
409vnode_if.h vnode_if_newproto.h vnode_if_typedef.h: ${SYSDIR}/tools/vnode_if.awk \
410    ${SYSDIR}/kern/vnode_if.src
411vnode_if.h: vnode_if_newproto.h vnode_if_typedef.h
412	${AWK} -f ${SYSDIR}/tools/vnode_if.awk ${SYSDIR}/kern/vnode_if.src -h
413vnode_if_newproto.h:
414	${AWK} -f ${SYSDIR}/tools/vnode_if.awk ${SYSDIR}/kern/vnode_if.src -p
415vnode_if_typedef.h:
416	${AWK} -f ${SYSDIR}/tools/vnode_if.awk ${SYSDIR}/kern/vnode_if.src -q
417.endif
418
419.for _i in mii pccard
420.if !empty(SRCS:M${_i}devs.h)
421CLEANFILES+=	${_i}devs.h
422${_i}devs.h: ${SYSDIR}/tools/${_i}devs2h.awk ${SYSDIR}/dev/${_i}/${_i}devs
423	${AWK} -f ${SYSDIR}/tools/${_i}devs2h.awk ${SYSDIR}/dev/${_i}/${_i}devs
424.endif
425.endfor # _i
426
427.if !empty(SRCS:Musbdevs.h)
428CLEANFILES+=	usbdevs.h
429usbdevs.h: ${SYSDIR}/tools/usbdevs2h.awk ${SYSDIR}/dev/usb/usbdevs
430	${AWK} -f ${SYSDIR}/tools/usbdevs2h.awk ${SYSDIR}/dev/usb/usbdevs -h
431.endif
432
433.if !empty(SRCS:Musbdevs_data.h)
434CLEANFILES+=	usbdevs_data.h
435usbdevs_data.h: ${SYSDIR}/tools/usbdevs2h.awk ${SYSDIR}/dev/usb/usbdevs
436	${AWK} -f ${SYSDIR}/tools/usbdevs2h.awk ${SYSDIR}/dev/usb/usbdevs -d
437.endif
438
439.if !empty(SRCS:Macpi_quirks.h)
440CLEANFILES+=	acpi_quirks.h
441acpi_quirks.h: ${SYSDIR}/tools/acpi_quirks2h.awk ${SYSDIR}/dev/acpica/acpi_quirks
442	${AWK} -f ${SYSDIR}/tools/acpi_quirks2h.awk ${SYSDIR}/dev/acpica/acpi_quirks
443.endif
444
445.if !empty(SRCS:Massym.s)
446CLEANFILES+=	assym.s genassym.o
447assym.s: genassym.o
448.if defined(KERNBUILDDIR)
449genassym.o: opt_global.h
450.endif
451assym.s: ${SYSDIR}/kern/genassym.sh
452	sh ${SYSDIR}/kern/genassym.sh genassym.o > ${.TARGET}
453genassym.o: ${SYSDIR}/${MACHINE_CPUARCH}/${MACHINE_CPUARCH}/genassym.c
454genassym.o: ${SRCS:Mopt_*.h}
455	${CC} -c ${CFLAGS:N-fno-common} \
456	    ${SYSDIR}/${MACHINE_CPUARCH}/${MACHINE_CPUARCH}/genassym.c
457.endif
458
459lint: ${SRCS}
460	${LINT} ${LINTKERNFLAGS} ${CFLAGS:M-[DILU]*} ${.ALLSRC:M*.c}
461
462.if defined(KERNBUILDDIR)
463${OBJS}: opt_global.h
464.endif
465
466.include <bsd.dep.mk>
467
468cleandepend: cleanilinks
469# .depend needs include links so we remove them only together.
470cleanilinks:
471	rm -f ${_ILINKS}
472
473.if !exists(${.OBJDIR}/${DEPENDFILE})
474${OBJS}: ${SRCS:M*.h}
475.endif
476
477.include <bsd.obj.mk>
478.include "kern.mk"
479