xref: /freebsd/sys/conf/kmod.mk (revision 298022457a9a016cbdda4e22d751abb5cd91c919)
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.include "config.mk"
74
75.SUFFIXES: .out .o .c .cc .cxx .C .y .l .s .S
76
77# amd64 and mips use direct linking for kmod, all others use shared binaries
78.if ${MACHINE_CPUARCH} != amd64 && ${MACHINE_CPUARCH} != mips
79__KLD_SHARED=yes
80.else
81__KLD_SHARED=no
82.endif
83
84.if !empty(CFLAGS:M-O[23s]) && empty(CFLAGS:M-fno-strict-aliasing)
85CFLAGS+=	-fno-strict-aliasing
86.endif
87WERROR?=	-Werror
88CFLAGS+=	${WERROR}
89CFLAGS+=	-D_KERNEL
90CFLAGS+=	-DKLD_MODULE
91
92# Don't use any standard or source-relative include directories.
93NOSTDINC=	-nostdinc
94CFLAGS:=	${CFLAGS:N-I*} ${NOSTDINC} ${INCLMAGIC} ${CFLAGS:M-I*}
95.if defined(KERNBUILDDIR)
96CFLAGS+=	-DHAVE_KERNEL_OPTION_HEADERS -include ${KERNBUILDDIR}/opt_global.h
97.endif
98
99# Add -I paths for system headers.  Individual module makefiles don't
100# need any -I paths for this.  Similar defaults for .PATH can't be
101# set because there are no standard paths for non-headers.
102CFLAGS+=	-I. -I${SYSDIR}
103
104CFLAGS.gcc+=	-finline-limit=${INLINE_LIMIT}
105CFLAGS.gcc+=	-fms-extensions
106CFLAGS.gcc+= --param inline-unit-growth=100
107CFLAGS.gcc+= --param large-function-growth=1000
108
109# Disallow common variables, and if we end up with commons from
110# somewhere unexpected, allocate storage for them in the module itself.
111CFLAGS+=	-fno-common
112LDFLAGS+=	-d -warn-common
113
114CFLAGS+=	${DEBUG_FLAGS}
115.if ${MACHINE_CPUARCH} == amd64
116CFLAGS+=	-fno-omit-frame-pointer -mno-omit-leaf-frame-pointer
117.endif
118
119# Temporary workaround for PR 196407, which contains the fascinating details.
120# Don't allow clang to use fpu instructions or registers in kernel modules.
121.if ${MACHINE_CPUARCH} == arm
122CFLAGS.clang+=	-mllvm -arm-use-movt=0
123CFLAGS.clang+=	-mfpu=none
124CFLAGS+=	-funwind-tables
125.endif
126
127.if ${MACHINE_CPUARCH} == powerpc
128CFLAGS+=	-mlongcall -fno-omit-frame-pointer
129.endif
130
131.if ${MACHINE_CPUARCH} == mips
132CFLAGS+=	-G0 -fno-pic -mno-abicalls -mlong-calls
133.endif
134
135.if defined(DEBUG) || defined(DEBUG_FLAGS)
136CTFFLAGS+=	-g
137.endif
138
139.if defined(FIRMWS)
140${KMOD:S/$/.c/}: ${SYSDIR}/tools/fw_stub.awk
141	${AWK} -f ${SYSDIR}/tools/fw_stub.awk ${FIRMWS} -m${KMOD} -c${KMOD:S/$/.c/g} \
142	    ${FIRMWARE_LICENSE:C/.+/-l/}${FIRMWARE_LICENSE}
143
144SRCS+=	${KMOD:S/$/.c/}
145CLEANFILES+=	${KMOD:S/$/.c/}
146
147.for _firmw in ${FIRMWS}
148${_firmw:C/\:.*$/.fwo/}:	${_firmw:C/\:.*$//}
149	@${ECHO} ${_firmw:C/\:.*$//} ${.ALLSRC:M*${_firmw:C/\:.*$//}}
150	@if [ -e ${_firmw:C/\:.*$//} ]; then			\
151		${LD} -b binary --no-warn-mismatch ${_LDFLAGS}	\
152		    -r -d -o ${.TARGET}	${_firmw:C/\:.*$//};	\
153	else							\
154		ln -s ${.ALLSRC:M*${_firmw:C/\:.*$//}} ${_firmw:C/\:.*$//}; \
155		${LD} -b binary --no-warn-mismatch ${_LDFLAGS}	\
156		    -r -d -o ${.TARGET}	${_firmw:C/\:.*$//};	\
157		rm ${_firmw:C/\:.*$//};				\
158	fi
159
160OBJS+=	${_firmw:C/\:.*$/.fwo/}
161.endfor
162.endif
163
164# Conditionally include SRCS based on kernel config options.
165.for _o in ${KERN_OPTS}
166SRCS+=${SRCS.${_o}}
167.endfor
168
169OBJS+=	${SRCS:N*.h:R:S/$/.o/g}
170
171.if !defined(PROG)
172PROG=	${KMOD}.ko
173.endif
174
175.if !defined(DEBUG_FLAGS)
176FULLPROG=	${PROG}
177.else
178FULLPROG=	${PROG}.debug
179${PROG}: ${FULLPROG} ${PROG}.symbols
180	${OBJCOPY} --strip-debug --add-gnu-debuglink=${PROG}.symbols\
181	    ${FULLPROG} ${.TARGET}
182${PROG}.symbols: ${FULLPROG}
183	${OBJCOPY} --only-keep-debug ${FULLPROG} ${.TARGET}
184.endif
185
186.if ${__KLD_SHARED} == yes
187${FULLPROG}: ${KMOD}.kld
188	${LD} -Bshareable ${_LDFLAGS} -o ${.TARGET} ${KMOD}.kld
189.if !defined(DEBUG_FLAGS)
190	${OBJCOPY} --strip-debug ${.TARGET}
191.endif
192.endif
193
194EXPORT_SYMS?=	NO
195.if ${EXPORT_SYMS} != YES
196CLEANFILES+=	export_syms
197.endif
198
199.if ${__KLD_SHARED} == yes
200${KMOD}.kld: ${OBJS}
201.else
202${FULLPROG}: ${OBJS}
203.endif
204	${LD} ${_LDFLAGS} -r -d -o ${.TARGET} ${OBJS}
205.if ${MK_CTF} != "no"
206	${CTFMERGE} ${CTFFLAGS} -o ${.TARGET} ${OBJS}
207.endif
208.if defined(EXPORT_SYMS)
209.if ${EXPORT_SYMS} != YES
210.if ${EXPORT_SYMS} == NO
211	:> export_syms
212.elif !exists(${.CURDIR}/${EXPORT_SYMS})
213	echo ${EXPORT_SYMS} > export_syms
214.else
215	grep -v '^#' < ${EXPORT_SYMS} > export_syms
216.endif
217	awk -f ${SYSDIR}/conf/kmod_syms.awk ${.TARGET} \
218	    export_syms | xargs -J% ${OBJCOPY} % ${.TARGET}
219.endif
220.endif
221.if !defined(DEBUG_FLAGS) && ${__KLD_SHARED} == no
222	${OBJCOPY} --strip-debug ${.TARGET}
223.endif
224
225_ILINKS=machine
226.if ${MACHINE} != ${MACHINE_CPUARCH}
227_ILINKS+=${MACHINE_CPUARCH}
228.endif
229.if ${MACHINE_CPUARCH} == "i386" || ${MACHINE_CPUARCH} == "amd64"
230_ILINKS+=x86
231.endif
232CLEANFILES+=${_ILINKS}
233
234all: objwarn ${PROG}
235
236beforedepend: ${_ILINKS}
237
238# Ensure that the links exist without depending on it when it exists which
239# causes all the modules to be rebuilt when the directory pointed to changes.
240.for _link in ${_ILINKS}
241.if !exists(${.OBJDIR}/${_link})
242${OBJS}: ${_link}
243.endif
244.endfor
245
246# Search for kernel source tree in standard places.
247.for _dir in ${.CURDIR}/../.. ${.CURDIR}/../../.. /sys /usr/src/sys
248.if !defined(SYSDIR) && exists(${_dir}/kern/)
249SYSDIR=	${_dir}
250.endif
251.endfor
252.if !defined(SYSDIR) || !exists(${SYSDIR}/kern/)
253.error "can't find kernel source tree"
254.endif
255
256.NOPATH: ${_ILINKS}
257
258${_ILINKS}:
259	@case ${.TARGET} in \
260	machine) \
261		path=${SYSDIR}/${MACHINE}/include ;; \
262	*) \
263		path=${SYSDIR}/${.TARGET:T}/include ;; \
264	esac ; \
265	path=`(cd $$path && /bin/pwd)` ; \
266	${ECHO} ${.TARGET:T} "->" $$path ; \
267	ln -sf $$path ${.TARGET:T}
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.if defined(KERNBUILDDIR)
320.PATH: ${KERNBUILDDIR}
321CFLAGS+=	-I${KERNBUILDDIR}
322.for _src in ${SRCS:Mopt_*.h}
323CLEANFILES+=	${_src}
324.if !target(${_src})
325${_src}:
326	ln -sf ${KERNBUILDDIR}/${_src} ${.TARGET}
327.endif
328.endfor
329.else
330.for _src in ${SRCS:Mopt_*.h}
331CLEANFILES+=	${_src}
332.if !target(${_src})
333${_src}:
334	:> ${.TARGET}
335.endif
336.endfor
337.endif
338
339# Respect configuration-specific C flags.
340CFLAGS+=	${CONF_CFLAGS}
341
342MFILES?= dev/acpica/acpi_if.m dev/acpi_support/acpi_wmi_if.m \
343	dev/agp/agp_if.m dev/ata/ata_if.m dev/eisa/eisa_if.m \
344	dev/fb/fb_if.m dev/gpio/gpio_if.m dev/gpio/gpiobus_if.m \
345	dev/iicbus/iicbb_if.m dev/iicbus/iicbus_if.m \
346	dev/mbox/mbox_if.m dev/mmc/mmcbr_if.m dev/mmc/mmcbus_if.m \
347	dev/mii/miibus_if.m dev/mvs/mvs_if.m dev/ofw/ofw_bus_if.m \
348	dev/pccard/card_if.m dev/pccard/power_if.m dev/pci/pci_if.m \
349	dev/pci/pci_iov_if.m dev/pci/pcib_if.m dev/ppbus/ppbus_if.m \
350	dev/sdhci/sdhci_if.m dev/smbus/smbus_if.m dev/spibus/spibus_if.m \
351	dev/sound/pci/hda/hdac_if.m \
352	dev/sound/pcm/ac97_if.m dev/sound/pcm/channel_if.m \
353	dev/sound/pcm/feeder_if.m dev/sound/pcm/mixer_if.m \
354	dev/sound/midi/mpu_if.m dev/sound/midi/mpufoi_if.m \
355	dev/sound/midi/synth_if.m dev/usb/usb_if.m isa/isa_if.m \
356	kern/bus_if.m kern/clock_if.m \
357	kern/cpufreq_if.m kern/device_if.m kern/serdev_if.m \
358	libkern/iconv_converter_if.m opencrypto/cryptodev_if.m \
359	pc98/pc98/canbus_if.m dev/etherswitch/mdio_if.m
360
361.for _srcsrc in ${MFILES}
362.for _ext in c h
363.for _src in ${SRCS:M${_srcsrc:T:R}.${_ext}}
364CLEANFILES+=	${_src}
365.if !target(${_src})
366${_src}: ${SYSDIR}/tools/makeobjops.awk ${SYSDIR}/${_srcsrc}
367	${AWK} -f ${SYSDIR}/tools/makeobjops.awk ${SYSDIR}/${_srcsrc} -${_ext}
368.endif
369.endfor # _src
370.endfor # _ext
371.endfor # _srcsrc
372
373.if !empty(SRCS:Mvnode_if.c)
374CLEANFILES+=	vnode_if.c
375vnode_if.c: ${SYSDIR}/tools/vnode_if.awk ${SYSDIR}/kern/vnode_if.src
376	${AWK} -f ${SYSDIR}/tools/vnode_if.awk ${SYSDIR}/kern/vnode_if.src -c
377.endif
378
379.if !empty(SRCS:Mvnode_if.h)
380CLEANFILES+=	vnode_if.h vnode_if_newproto.h vnode_if_typedef.h
381vnode_if.h vnode_if_newproto.h vnode_if_typedef.h: ${SYSDIR}/tools/vnode_if.awk \
382    ${SYSDIR}/kern/vnode_if.src
383vnode_if.h: vnode_if_newproto.h vnode_if_typedef.h
384	${AWK} -f ${SYSDIR}/tools/vnode_if.awk ${SYSDIR}/kern/vnode_if.src -h
385vnode_if_newproto.h:
386	${AWK} -f ${SYSDIR}/tools/vnode_if.awk ${SYSDIR}/kern/vnode_if.src -p
387vnode_if_typedef.h:
388	${AWK} -f ${SYSDIR}/tools/vnode_if.awk ${SYSDIR}/kern/vnode_if.src -q
389.endif
390
391.for _i in mii pccard
392.if !empty(SRCS:M${_i}devs.h)
393CLEANFILES+=	${_i}devs.h
394${_i}devs.h: ${SYSDIR}/tools/${_i}devs2h.awk ${SYSDIR}/dev/${_i}/${_i}devs
395	${AWK} -f ${SYSDIR}/tools/${_i}devs2h.awk ${SYSDIR}/dev/${_i}/${_i}devs
396.endif
397.endfor # _i
398
399.if !empty(SRCS:Musbdevs.h)
400CLEANFILES+=	usbdevs.h
401usbdevs.h: ${SYSDIR}/tools/usbdevs2h.awk ${SYSDIR}/dev/usb/usbdevs
402	${AWK} -f ${SYSDIR}/tools/usbdevs2h.awk ${SYSDIR}/dev/usb/usbdevs -h
403.endif
404
405.if !empty(SRCS:Musbdevs_data.h)
406CLEANFILES+=	usbdevs_data.h
407usbdevs_data.h: ${SYSDIR}/tools/usbdevs2h.awk ${SYSDIR}/dev/usb/usbdevs
408	${AWK} -f ${SYSDIR}/tools/usbdevs2h.awk ${SYSDIR}/dev/usb/usbdevs -d
409.endif
410
411.if !empty(SRCS:Macpi_quirks.h)
412CLEANFILES+=	acpi_quirks.h
413acpi_quirks.h: ${SYSDIR}/tools/acpi_quirks2h.awk ${SYSDIR}/dev/acpica/acpi_quirks
414	${AWK} -f ${SYSDIR}/tools/acpi_quirks2h.awk ${SYSDIR}/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(KERNBUILDDIR)
421genassym.o: opt_global.h
422.endif
423assym.s: ${SYSDIR}/kern/genassym.sh
424	sh ${SYSDIR}/kern/genassym.sh genassym.o > ${.TARGET}
425genassym.o: ${SYSDIR}/${MACHINE}/${MACHINE}/genassym.c
426genassym.o: ${SRCS:Mopt_*.h}
427	${CC} -c ${CFLAGS:N-fno-common} \
428	    ${SYSDIR}/${MACHINE}/${MACHINE}/genassym.c
429.endif
430
431lint: ${SRCS}
432	${LINT} ${LINTKERNFLAGS} ${CFLAGS:M-[DILU]*} ${.ALLSRC:M*.c}
433
434.if defined(KERNBUILDDIR)
435${OBJS}: opt_global.h
436.endif
437
438.include <bsd.dep.mk>
439
440cleandepend: cleanilinks
441# .depend needs include links so we remove them only together.
442cleanilinks:
443	rm -f ${_ILINKS}
444
445.if !exists(${.OBJDIR}/${DEPENDFILE})
446${OBJS}: ${SRCS:M*.h}
447.endif
448
449.include <bsd.obj.mk>
450.include "kern.mk"
451