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