xref: /freebsd/sys/conf/kmod.mk (revision 4b63a7c678a1291c8056de3770cfa393773b1f94)
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.
92CSTD=		c99
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
104# Add -I path for altq headers as they are included via net/if_var.h
105# for example.
106CFLAGS+=	-I${SYSDIR}/contrib/altq
107
108CFLAGS.gcc+=	-finline-limit=${INLINE_LIMIT}
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.if ${MACHINE_CPUARCH} == powerpc
123CFLAGS+=	-mlongcall -fno-omit-frame-pointer
124.endif
125
126.if ${MACHINE_CPUARCH} == mips
127CFLAGS+=	-G0 -fno-pic -mno-abicalls -mlong-calls
128.endif
129
130.if defined(DEBUG) || defined(DEBUG_FLAGS)
131CTFFLAGS+=	-g
132.endif
133
134.if defined(FIRMWS)
135${KMOD:S/$/.c/}: ${SYSDIR}/tools/fw_stub.awk
136	${AWK} -f ${SYSDIR}/tools/fw_stub.awk ${FIRMWS} -m${KMOD} -c${KMOD:S/$/.c/g} \
137	    ${FIRMWARE_LICENSE:C/.+/-l/}${FIRMWARE_LICENSE}
138
139SRCS+=	${KMOD:S/$/.c/}
140CLEANFILES+=	${KMOD:S/$/.c/}
141
142.for _firmw in ${FIRMWS}
143${_firmw:C/\:.*$/.fwo/}:	${_firmw:C/\:.*$//}
144	@${ECHO} ${_firmw:C/\:.*$//} ${.ALLSRC:M*${_firmw:C/\:.*$//}}
145	@if [ -e ${_firmw:C/\:.*$//} ]; then			\
146		${LD} -b binary --no-warn-mismatch ${_LDFLAGS}	\
147		    -r -d -o ${.TARGET}	${_firmw:C/\:.*$//};	\
148	else							\
149		ln -s ${.ALLSRC:M*${_firmw:C/\:.*$//}} ${_firmw:C/\:.*$//}; \
150		${LD} -b binary --no-warn-mismatch ${_LDFLAGS}	\
151		    -r -d -o ${.TARGET}	${_firmw:C/\:.*$//};	\
152		rm ${_firmw:C/\:.*$//};				\
153	fi
154
155OBJS+=	${_firmw:C/\:.*$/.fwo/}
156.endfor
157.endif
158
159OBJS+=	${SRCS:N*.h:R:S/$/.o/g}
160
161.if !defined(PROG)
162PROG=	${KMOD}.ko
163.endif
164
165.if !defined(DEBUG_FLAGS)
166FULLPROG=	${PROG}
167.else
168FULLPROG=	${PROG}.debug
169${PROG}: ${FULLPROG} ${PROG}.symbols
170	${OBJCOPY} --strip-debug --add-gnu-debuglink=${PROG}.symbols\
171	    ${FULLPROG} ${.TARGET}
172${PROG}.symbols: ${FULLPROG}
173	${OBJCOPY} --only-keep-debug ${FULLPROG} ${.TARGET}
174.endif
175
176.if ${__KLD_SHARED} == yes
177${FULLPROG}: ${KMOD}.kld
178	${LD} -Bshareable ${_LDFLAGS} -o ${.TARGET} ${KMOD}.kld
179.if !defined(DEBUG_FLAGS)
180	${OBJCOPY} --strip-debug ${.TARGET}
181.endif
182.endif
183
184EXPORT_SYMS?=	NO
185.if ${EXPORT_SYMS} != YES
186CLEANFILES+=	export_syms
187.endif
188
189.if ${__KLD_SHARED} == yes
190${KMOD}.kld: ${OBJS}
191.else
192${FULLPROG}: ${OBJS}
193.endif
194	${LD} ${_LDFLAGS} -r -d -o ${.TARGET} ${OBJS}
195.if ${MK_CTF} != "no"
196	${CTFMERGE} ${CTFFLAGS} -o ${.TARGET} ${OBJS}
197.endif
198.if defined(EXPORT_SYMS)
199.if ${EXPORT_SYMS} != YES
200.if ${EXPORT_SYMS} == NO
201	:> export_syms
202.elif !exists(${.CURDIR}/${EXPORT_SYMS})
203	echo ${EXPORT_SYMS} > export_syms
204.else
205	grep -v '^#' < ${EXPORT_SYMS} > export_syms
206.endif
207	awk -f ${SYSDIR}/conf/kmod_syms.awk ${.TARGET} \
208	    export_syms | xargs -J% ${OBJCOPY} % ${.TARGET}
209.endif
210.endif
211.if !defined(DEBUG_FLAGS) && ${__KLD_SHARED} == no
212	${OBJCOPY} --strip-debug ${.TARGET}
213.endif
214
215_ILINKS=machine
216.if ${MACHINE} != ${MACHINE_CPUARCH}
217_ILINKS+=${MACHINE_CPUARCH}
218.endif
219.if ${MACHINE_CPUARCH} == "i386" || ${MACHINE_CPUARCH} == "amd64"
220_ILINKS+=x86
221.endif
222CLEANFILES+=${_ILINKS}
223
224all: objwarn ${PROG}
225
226beforedepend: ${_ILINKS}
227
228# Ensure that the links exist without depending on it when it exists which
229# causes all the modules to be rebuilt when the directory pointed to changes.
230.for _link in ${_ILINKS}
231.if !exists(${.OBJDIR}/${_link})
232${OBJS}: ${.OBJDIR}/${_link}
233.endif
234.endfor
235
236# Search for kernel source tree in standard places.
237.for _dir in ${.CURDIR}/../.. ${.CURDIR}/../../.. /sys /usr/src/sys
238.if !defined(SYSDIR) && exists(${_dir}/kern/)
239SYSDIR=	${_dir}
240.endif
241.endfor
242.if !defined(SYSDIR) || !exists(${SYSDIR}/kern/)
243.error "can't find kernel source tree"
244.endif
245
246.for _link in ${_ILINKS}
247.PHONY: ${_link}
248${_link}: ${.OBJDIR}/${_link}
249
250${.OBJDIR}/${_link}:
251	@case ${.TARGET:T} in \
252	machine) \
253		path=${SYSDIR}/${MACHINE}/include ;; \
254	*) \
255		path=${SYSDIR}/${.TARGET:T}/include ;; \
256	esac ; \
257	path=`(cd $$path && /bin/pwd)` ; \
258	${ECHO} ${.TARGET:T} "->" $$path ; \
259	ln -sf $$path ${.TARGET:T}
260.endfor
261
262CLEANFILES+= ${PROG} ${KMOD}.kld ${OBJS}
263
264.if defined(DEBUG_FLAGS)
265CLEANFILES+= ${FULLPROG} ${PROG}.symbols
266.endif
267
268.if !target(install)
269
270_INSTALLFLAGS:=	${INSTALLFLAGS}
271.for ie in ${INSTALLFLAGS_EDIT}
272_INSTALLFLAGS:=	${_INSTALLFLAGS${ie}}
273.endfor
274
275.if !target(realinstall)
276realinstall: _kmodinstall
277.ORDER: beforeinstall _kmodinstall
278_kmodinstall:
279	${INSTALL} -o ${KMODOWN} -g ${KMODGRP} -m ${KMODMODE} \
280	    ${_INSTALLFLAGS} ${PROG} ${DESTDIR}${KMODDIR}
281.if defined(DEBUG_FLAGS) && !defined(INSTALL_NODEBUG) && ${MK_KERNEL_SYMBOLS} != "no"
282	${INSTALL} -o ${KMODOWN} -g ${KMODGRP} -m ${KMODMODE} \
283	    ${_INSTALLFLAGS} ${PROG}.symbols ${DESTDIR}${KMODDIR}
284.endif
285
286.include <bsd.links.mk>
287
288.if !defined(NO_XREF)
289afterinstall: _kldxref
290.ORDER: realinstall _kldxref
291.ORDER: _installlinks _kldxref
292_kldxref:
293	@if type kldxref >/dev/null 2>&1; then \
294		${ECHO} kldxref ${DESTDIR}${KMODDIR}; \
295		kldxref ${DESTDIR}${KMODDIR}; \
296	fi
297.endif
298.endif # !target(realinstall)
299
300.endif # !target(install)
301
302.if !target(load)
303load: ${PROG}
304	${KMODLOAD} -v ${.OBJDIR}/${PROG}
305.endif
306
307.if !target(unload)
308unload:
309	${KMODUNLOAD} -v ${PROG}
310.endif
311
312# Generate options files that otherwise would be built
313# in substantially similar ways through the tree. Move
314# the code here when they all produce identical results
315# (or should)
316.if !defined(KERNBUILDDIR)
317opt_bpf.h:
318	echo "#define DEV_BPF 1" > ${.TARGET}
319.if ${MK_INET_SUPPORT} != "no"
320opt_inet.h:
321	@echo "#define INET 1" > ${.TARGET}
322	@echo "#define TCP_OFFLOAD 1" >> ${.TARGET}
323.endif
324.if ${MK_INET6_SUPPORT} != "no"
325opt_inet6.h:
326	@echo "#define INET6 1" > ${.TARGET}
327.endif
328opt_mrouting.h:
329	echo "#define MROUTING 1" > ${.TARGET}
330opt_natm.h:
331	echo "#define NATM 1" > ${.TARGET}
332opt_scsi.h:
333	echo "#define SCSI_DELAY 15000" > ${.TARGET}
334opt_wlan.h:
335	echo "#define IEEE80211_DEBUG 1" > ${.TARGET}
336	echo "#define IEEE80211_AMPDU_AGE 1" >> ${.TARGET}
337	echo "#define IEEE80211_SUPPORT_MESH 1" >> ${.TARGET}
338.endif
339
340.if defined(KERNBUILDDIR)
341.PATH: ${KERNBUILDDIR}
342CFLAGS+=	-I${KERNBUILDDIR}
343.for _src in ${SRCS:Mopt_*.h}
344CLEANFILES+=	${_src}
345.if !target(${_src})
346${_src}:
347	ln -sf ${KERNBUILDDIR}/${_src} ${.TARGET}
348.endif
349.endfor
350.else
351.for _src in ${SRCS:Mopt_*.h}
352CLEANFILES+=	${_src}
353.if !target(${_src})
354${_src}:
355	:> ${.TARGET}
356.endif
357.endfor
358.endif
359
360# Respect configuration-specific C flags.
361CFLAGS+=	${CONF_CFLAGS}
362
363MFILES?= dev/acpica/acpi_if.m dev/acpi_support/acpi_wmi_if.m \
364	dev/agp/agp_if.m dev/ata/ata_if.m dev/eisa/eisa_if.m \
365	dev/fb/fb_if.m dev/gpio/gpio_if.m dev/gpio/gpiobus_if.m \
366	dev/iicbus/iicbb_if.m dev/iicbus/iicbus_if.m \
367	dev/mmc/mmcbr_if.m dev/mmc/mmcbus_if.m \
368	dev/mii/miibus_if.m dev/mvs/mvs_if.m dev/ofw/ofw_bus_if.m \
369	dev/pccard/card_if.m dev/pccard/power_if.m dev/pci/pci_if.m \
370	dev/pci/pcib_if.m dev/ppbus/ppbus_if.m \
371	dev/sdhci/sdhci_if.m dev/smbus/smbus_if.m dev/spibus/spibus_if.m \
372	dev/sound/pci/hda/hdac_if.m \
373	dev/sound/pcm/ac97_if.m dev/sound/pcm/channel_if.m \
374	dev/sound/pcm/feeder_if.m dev/sound/pcm/mixer_if.m \
375	dev/sound/midi/mpu_if.m dev/sound/midi/mpufoi_if.m \
376	dev/sound/midi/synth_if.m dev/usb/usb_if.m isa/isa_if.m \
377	kern/bus_if.m kern/clock_if.m \
378	kern/cpufreq_if.m kern/device_if.m kern/serdev_if.m \
379	libkern/iconv_converter_if.m opencrypto/cryptodev_if.m \
380	pc98/pc98/canbus_if.m dev/etherswitch/mdio_if.m
381
382.for _srcsrc in ${MFILES}
383.for _ext in c h
384.for _src in ${SRCS:M${_srcsrc:T:R}.${_ext}}
385CLEANFILES+=	${_src}
386.if !target(${_src})
387${_src}: ${SYSDIR}/tools/makeobjops.awk ${SYSDIR}/${_srcsrc}
388	${AWK} -f ${SYSDIR}/tools/makeobjops.awk ${SYSDIR}/${_srcsrc} -${_ext}
389.endif
390.endfor # _src
391.endfor # _ext
392.endfor # _srcsrc
393
394.if !empty(SRCS:Mvnode_if.c)
395CLEANFILES+=	vnode_if.c
396vnode_if.c: ${SYSDIR}/tools/vnode_if.awk ${SYSDIR}/kern/vnode_if.src
397	${AWK} -f ${SYSDIR}/tools/vnode_if.awk ${SYSDIR}/kern/vnode_if.src -c
398.endif
399
400.if !empty(SRCS:Mvnode_if.h)
401CLEANFILES+=	vnode_if.h vnode_if_newproto.h vnode_if_typedef.h
402vnode_if.h vnode_if_newproto.h vnode_if_typedef.h: ${SYSDIR}/tools/vnode_if.awk \
403    ${SYSDIR}/kern/vnode_if.src
404vnode_if.h: vnode_if_newproto.h vnode_if_typedef.h
405	${AWK} -f ${SYSDIR}/tools/vnode_if.awk ${SYSDIR}/kern/vnode_if.src -h
406vnode_if_newproto.h:
407	${AWK} -f ${SYSDIR}/tools/vnode_if.awk ${SYSDIR}/kern/vnode_if.src -p
408vnode_if_typedef.h:
409	${AWK} -f ${SYSDIR}/tools/vnode_if.awk ${SYSDIR}/kern/vnode_if.src -q
410.endif
411
412.for _i in mii pccard
413.if !empty(SRCS:M${_i}devs.h)
414CLEANFILES+=	${_i}devs.h
415${_i}devs.h: ${SYSDIR}/tools/${_i}devs2h.awk ${SYSDIR}/dev/${_i}/${_i}devs
416	${AWK} -f ${SYSDIR}/tools/${_i}devs2h.awk ${SYSDIR}/dev/${_i}/${_i}devs
417.endif
418.endfor # _i
419
420.if !empty(SRCS:Musbdevs.h)
421CLEANFILES+=	usbdevs.h
422usbdevs.h: ${SYSDIR}/tools/usbdevs2h.awk ${SYSDIR}/dev/usb/usbdevs
423	${AWK} -f ${SYSDIR}/tools/usbdevs2h.awk ${SYSDIR}/dev/usb/usbdevs -h
424.endif
425
426.if !empty(SRCS:Musbdevs_data.h)
427CLEANFILES+=	usbdevs_data.h
428usbdevs_data.h: ${SYSDIR}/tools/usbdevs2h.awk ${SYSDIR}/dev/usb/usbdevs
429	${AWK} -f ${SYSDIR}/tools/usbdevs2h.awk ${SYSDIR}/dev/usb/usbdevs -d
430.endif
431
432.if !empty(SRCS:Macpi_quirks.h)
433CLEANFILES+=	acpi_quirks.h
434acpi_quirks.h: ${SYSDIR}/tools/acpi_quirks2h.awk ${SYSDIR}/dev/acpica/acpi_quirks
435	${AWK} -f ${SYSDIR}/tools/acpi_quirks2h.awk ${SYSDIR}/dev/acpica/acpi_quirks
436.endif
437
438.if !empty(SRCS:Massym.s)
439CLEANFILES+=	assym.s genassym.o
440assym.s: genassym.o
441.if defined(KERNBUILDDIR)
442genassym.o: opt_global.h
443.endif
444assym.s: ${SYSDIR}/kern/genassym.sh
445	sh ${SYSDIR}/kern/genassym.sh genassym.o > ${.TARGET}
446genassym.o: ${SYSDIR}/${MACHINE_CPUARCH}/${MACHINE_CPUARCH}/genassym.c
447genassym.o: ${SRCS:Mopt_*.h}
448	${CC} -c ${CFLAGS:N-fno-common} \
449	    ${SYSDIR}/${MACHINE_CPUARCH}/${MACHINE_CPUARCH}/genassym.c
450.endif
451
452lint: ${SRCS}
453	${LINT} ${LINTKERNFLAGS} ${CFLAGS:M-[DILU]*} ${.ALLSRC:M*.c}
454
455.if defined(KERNBUILDDIR)
456${OBJS}: opt_global.h
457.endif
458
459.include <bsd.dep.mk>
460
461cleandepend: cleanilinks
462# .depend needs include links so we remove them only together.
463cleanilinks:
464	rm -f ${_ILINKS}
465
466.if !exists(${.OBJDIR}/${DEPENDFILE})
467${OBJS}: ${SRCS:M*.h}
468.endif
469
470.include <bsd.obj.mk>
471.include "kern.mk"
472