xref: /freebsd/sys/conf/kmod.mk (revision 66e576525d35c68fcb86f142ebaa5a448555c0c7)
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
63# backwards compat option for older systems.
64MACHINE_CPUARCH?=${MACHINE_ARCH:C/mipse[lb]/mips/:C/armeb/arm/:C/powerpc64/powerpc/}
65
66AWK?=		awk
67KMODLOAD?=	/sbin/kldload
68KMODUNLOAD?=	/sbin/kldunload
69OBJCOPY?=	objcopy
70
71.if defined(KMODDEPS)
72.error "Do not use KMODDEPS on 5.0+; use MODULE_VERSION/MODULE_DEPEND"
73.endif
74
75# Enable CTF conversion on request.
76.if defined(WITH_CTF)
77.undef NO_CTF
78.endif
79
80.include <bsd.init.mk>
81
82.SUFFIXES: .out .o .c .cc .cxx .C .y .l .s .S
83
84# amd64 and mips use direct linking for kmod, all others use shared binaries
85.if ${MACHINE_CPUARCH} != amd64 && ${MACHINE_CPUARCH} != mips
86__KLD_SHARED=yes
87.else
88__KLD_SHARED=no
89.endif
90
91.if ${CC:T:Micc} == "icc"
92CFLAGS:=	${CFLAGS:C/(-x[^M^K^W]+)[MKW]+|-x[MKW]+/\1/}
93.else
94. if !empty(CFLAGS:M-O[23s]) && empty(CFLAGS:M-fno-strict-aliasing)
95CFLAGS+=	-fno-strict-aliasing
96. endif
97WERROR?=	-Werror
98.endif
99CFLAGS+=	${WERROR}
100CFLAGS+=	-D_KERNEL
101CFLAGS+=	-DKLD_MODULE
102
103# Don't use any standard or source-relative include directories.
104.if ${CC:T:Micc} == "icc"
105NOSTDINC=	-X
106.else
107CSTD=		c99
108NOSTDINC=	-nostdinc
109.endif
110CFLAGS:=	${CFLAGS:N-I*} ${NOSTDINC} ${INCLMAGIC} ${CFLAGS:M-I*}
111.if defined(KERNBUILDDIR)
112CFLAGS+=	-DHAVE_KERNEL_OPTION_HEADERS -include ${KERNBUILDDIR}/opt_global.h
113.endif
114
115# Add -I paths for system headers.  Individual module makefiles don't
116# need any -I paths for this.  Similar defaults for .PATH can't be
117# set because there are no standard paths for non-headers.
118CFLAGS+=	-I. -I@
119
120# Add -I path for altq headers as they are included via net/if_var.h
121# for example.
122CFLAGS+=	-I@/contrib/altq
123
124.if ${CC:T:Micc} != "icc" && ${CC:T:Mclang} != "clang"
125CFLAGS+=	-finline-limit=${INLINE_LIMIT}
126CFLAGS+= --param inline-unit-growth=100
127CFLAGS+= --param large-function-growth=1000
128.endif
129
130# Disallow common variables, and if we end up with commons from
131# somewhere unexpected, allocate storage for them in the module itself.
132.if ${CC:T:Micc} != "icc"
133CFLAGS+=	-fno-common
134.endif
135LDFLAGS+=	-d -warn-common
136
137CFLAGS+=	${DEBUG_FLAGS}
138.if ${MACHINE_CPUARCH} == amd64
139CFLAGS+=	-fno-omit-frame-pointer
140.endif
141
142.if ${MACHINE_CPUARCH} == powerpc
143CFLAGS+=	-mlongcall -fno-omit-frame-pointer
144.endif
145
146.if ${MACHINE_CPUARCH} == mips
147CFLAGS+=	-G0 -fno-pic -mno-abicalls -mlong-calls
148.endif
149
150.if defined(DEBUG) || defined(DEBUG_FLAGS)
151CTFFLAGS+=	-g
152.endif
153
154.if defined(FIRMWS)
155.if !exists(@)
156${KMOD:S/$/.c/}: @
157.else
158${KMOD:S/$/.c/}: @/tools/fw_stub.awk
159.endif
160	${AWK} -f @/tools/fw_stub.awk ${FIRMWS} -m${KMOD} -c${KMOD:S/$/.c/g} \
161	    ${FIRMWARE_LICENSE:C/.+/-l/}${FIRMWARE_LICENSE}
162
163SRCS+=	${KMOD:S/$/.c/}
164CLEANFILES+=	${KMOD:S/$/.c/}
165
166.for _firmw in ${FIRMWS}
167${_firmw:C/\:.*$/.fwo/}:	${_firmw:C/\:.*$//}
168	@${ECHO} ${_firmw:C/\:.*$//} ${.ALLSRC:M*${_firmw:C/\:.*$//}}
169	@if [ -e ${_firmw:C/\:.*$//} ]; then			\
170		${LD} -b binary --no-warn-mismatch ${LDFLAGS}	\
171		    -r -d -o ${.TARGET}	${_firmw:C/\:.*$//};	\
172	else							\
173		ln -s ${.ALLSRC:M*${_firmw:C/\:.*$//}} ${_firmw:C/\:.*$//}; \
174		${LD} -b binary --no-warn-mismatch ${LDFLAGS}	\
175		    -r -d -o ${.TARGET}	${_firmw:C/\:.*$//};	\
176		rm ${_firmw:C/\:.*$//};				\
177	fi
178
179OBJS+=	${_firmw:C/\:.*$/.fwo/}
180.endfor
181.endif
182
183OBJS+=	${SRCS:N*.h:R:S/$/.o/g}
184
185.if !defined(PROG)
186PROG=	${KMOD}.ko
187.endif
188
189.if !defined(DEBUG_FLAGS)
190FULLPROG=	${PROG}
191.else
192FULLPROG=	${PROG}.debug
193${PROG}: ${FULLPROG} ${PROG}.symbols
194	${OBJCOPY} --strip-debug --add-gnu-debuglink=${PROG}.symbols\
195	    ${FULLPROG} ${.TARGET}
196${PROG}.symbols: ${FULLPROG}
197	${OBJCOPY} --only-keep-debug ${FULLPROG} ${.TARGET}
198.endif
199
200.if ${__KLD_SHARED} == yes
201${FULLPROG}: ${KMOD}.kld
202	${LD} -Bshareable ${LDFLAGS} -o ${.TARGET} ${KMOD}.kld
203.if !defined(DEBUG_FLAGS)
204	${OBJCOPY} --strip-debug ${.TARGET}
205.endif
206.endif
207
208EXPORT_SYMS?=	NO
209.if ${EXPORT_SYMS} != YES
210CLEANFILES+=	export_syms
211.endif
212
213.if ${__KLD_SHARED} == yes
214${KMOD}.kld: ${OBJS}
215.else
216${FULLPROG}: ${OBJS}
217.endif
218	${LD} ${LDFLAGS} -r -d -o ${.TARGET} ${OBJS}
219	@[ -z "${CTFMERGE}" -o -n "${NO_CTF}" ] || ${CTFMERGE} ${CTFFLAGS} -o ${.TARGET} ${OBJS}
220.if defined(EXPORT_SYMS)
221.if ${EXPORT_SYMS} != YES
222.if ${EXPORT_SYMS} == NO
223	:> export_syms
224.elif !exists(${.CURDIR}/${EXPORT_SYMS})
225	echo ${EXPORT_SYMS} > export_syms
226.else
227	grep -v '^#' < ${EXPORT_SYMS} > export_syms
228.endif
229	awk -f ${SYSDIR}/conf/kmod_syms.awk ${.TARGET} \
230	    export_syms | xargs -J% ${OBJCOPY} % ${.TARGET}
231.endif
232.endif
233.if !defined(DEBUG_FLAGS) && ${__KLD_SHARED} == no
234	${OBJCOPY} --strip-debug ${.TARGET}
235.endif
236
237_ILINKS=@ machine
238.if ${MACHINE} != ${MACHINE_CPUARCH}
239_ILINKS+=${MACHINE_CPUARCH}
240.endif
241
242all: objwarn ${PROG}
243
244beforedepend: ${_ILINKS}
245
246# Ensure that the links exist without depending on it when it exists which
247# causes all the modules to be rebuilt when the directory pointed to changes.
248.for _link in ${_ILINKS}
249.if !exists(${.OBJDIR}/${_link})
250${OBJS}: ${_link}
251.endif
252.endfor
253
254# Search for kernel source tree in standard places.
255.for _dir in ${.CURDIR}/../.. ${.CURDIR}/../../.. /sys /usr/src/sys
256.if !defined(SYSDIR) && exists(${_dir}/kern/)
257SYSDIR=	${_dir}
258.endif
259.endfor
260.if !defined(SYSDIR) || !exists(${SYSDIR}/kern/)
261.error "can't find kernel source tree"
262.endif
263
264${_ILINKS}:
265	@case ${.TARGET} in \
266	${MACHINE_CPUARCH}) \
267		path=${SYSDIR}/${MACHINE_CPUARCH}/include ;; \
268	machine) \
269		path=${SYSDIR}/${MACHINE}/include ;; \
270	@) \
271		path=${SYSDIR} ;; \
272	esac ; \
273	path=`(cd $$path && /bin/pwd)` ; \
274	${ECHO} ${.TARGET} "->" $$path ; \
275	ln -sf $$path ${.TARGET}
276
277CLEANFILES+= ${PROG} ${KMOD}.kld ${OBJS}
278
279.if defined(DEBUG_FLAGS)
280CLEANFILES+= ${FULLPROG} ${PROG}.symbols
281.endif
282
283.if !target(install)
284
285_INSTALLFLAGS:=	${INSTALLFLAGS}
286.for ie in ${INSTALLFLAGS_EDIT}
287_INSTALLFLAGS:=	${_INSTALLFLAGS${ie}}
288.endfor
289
290.if !target(realinstall)
291realinstall: _kmodinstall
292.ORDER: beforeinstall _kmodinstall
293_kmodinstall:
294	${INSTALL} -o ${KMODOWN} -g ${KMODGRP} -m ${KMODMODE} \
295	    ${_INSTALLFLAGS} ${PROG} ${DESTDIR}${KMODDIR}
296.if defined(DEBUG_FLAGS) && !defined(INSTALL_NODEBUG)
297	${INSTALL} -o ${KMODOWN} -g ${KMODGRP} -m ${KMODMODE} \
298	    ${_INSTALLFLAGS} ${PROG}.symbols ${DESTDIR}${KMODDIR}
299.endif
300
301.include <bsd.links.mk>
302
303.if !defined(NO_XREF)
304afterinstall: _kldxref
305.ORDER: realinstall _kldxref
306.ORDER: _installlinks _kldxref
307_kldxref:
308	@if type kldxref >/dev/null 2>&1; then \
309		${ECHO} kldxref ${DESTDIR}${KMODDIR}; \
310		kldxref ${DESTDIR}${KMODDIR}; \
311	fi
312.endif
313.endif # !target(realinstall)
314
315.endif # !target(install)
316
317.if !target(load)
318load: ${PROG}
319	${KMODLOAD} -v ${.OBJDIR}/${PROG}
320.endif
321
322.if !target(unload)
323unload:
324	${KMODUNLOAD} -v ${PROG}
325.endif
326
327.if defined(KERNBUILDDIR)
328.PATH: ${KERNBUILDDIR}
329CFLAGS+=	-I${KERNBUILDDIR}
330.for _src in ${SRCS:Mopt_*.h}
331CLEANFILES+=	${_src}
332.if !target(${_src})
333${_src}:
334	ln -sf ${KERNBUILDDIR}/${_src} ${.TARGET}
335.endif
336.endfor
337.else
338.for _src in ${SRCS:Mopt_*.h}
339CLEANFILES+=	${_src}
340.if !target(${_src})
341${_src}:
342	:> ${.TARGET}
343.endif
344.endfor
345.endif
346
347# Respect configuration-specific C flags.
348CFLAGS+=	${CONF_CFLAGS}
349
350MFILES?= dev/acpica/acpi_if.m dev/acpi_support/acpi_wmi_if.m \
351	dev/agp/agp_if.m dev/ata/ata_if.m dev/eisa/eisa_if.m \
352	dev/iicbus/iicbb_if.m dev/iicbus/iicbus_if.m \
353	dev/mmc/mmcbr_if.m dev/mmc/mmcbus_if.m \
354	dev/mii/miibus_if.m dev/mvs/mvs_if.m dev/ofw/ofw_bus_if.m \
355	dev/pccard/card_if.m dev/pccard/power_if.m dev/pci/pci_if.m \
356	dev/pci/pcib_if.m dev/ppbus/ppbus_if.m dev/smbus/smbus_if.m \
357	dev/sound/pcm/ac97_if.m dev/sound/pcm/channel_if.m \
358	dev/sound/pcm/feeder_if.m dev/sound/pcm/mixer_if.m \
359	dev/sound/midi/mpu_if.m dev/sound/midi/mpufoi_if.m \
360	dev/sound/midi/synth_if.m dev/usb/usb_if.m isa/isa_if.m \
361	kern/bus_if.m kern/clock_if.m \
362	kern/cpufreq_if.m kern/device_if.m kern/serdev_if.m \
363	libkern/iconv_converter_if.m opencrypto/cryptodev_if.m \
364	pc98/pc98/canbus_if.m
365
366.for _srcsrc in ${MFILES}
367.for _ext in c h
368.for _src in ${SRCS:M${_srcsrc:T:R}.${_ext}}
369CLEANFILES+=	${_src}
370.if !target(${_src})
371.if !exists(@)
372${_src}: @
373.else
374${_src}: @/tools/makeobjops.awk @/${_srcsrc}
375.endif
376	${AWK} -f @/tools/makeobjops.awk @/${_srcsrc} -${_ext}
377.endif
378.endfor # _src
379.endfor # _ext
380.endfor # _srcsrc
381
382.if !empty(SRCS:Mvnode_if.c)
383CLEANFILES+=	vnode_if.c
384.if !exists(@)
385vnode_if.c: @
386.else
387vnode_if.c: @/tools/vnode_if.awk @/kern/vnode_if.src
388.endif
389	${AWK} -f @/tools/vnode_if.awk @/kern/vnode_if.src -c
390.endif
391
392.if !empty(SRCS:Mvnode_if.h)
393CLEANFILES+=	vnode_if.h vnode_if_newproto.h vnode_if_typedef.h
394.if !exists(@)
395vnode_if.h vnode_if_newproto.h vnode_if_typedef.h: @
396.else
397vnode_if.h vnode_if_newproto.h vnode_if_typedef.h: @/tools/vnode_if.awk \
398    @/kern/vnode_if.src
399.endif
400vnode_if.h: vnode_if_newproto.h vnode_if_typedef.h
401	${AWK} -f @/tools/vnode_if.awk @/kern/vnode_if.src -h
402vnode_if_newproto.h:
403	${AWK} -f @/tools/vnode_if.awk @/kern/vnode_if.src -p
404vnode_if_typedef.h:
405	${AWK} -f @/tools/vnode_if.awk @/kern/vnode_if.src -q
406.endif
407
408.for _i in mii pccard
409.if !empty(SRCS:M${_i}devs.h)
410CLEANFILES+=	${_i}devs.h
411.if !exists(@)
412${_i}devs.h: @
413.else
414${_i}devs.h: @/tools/${_i}devs2h.awk @/dev/${_i}/${_i}devs
415.endif
416	${AWK} -f @/tools/${_i}devs2h.awk @/dev/${_i}/${_i}devs
417.endif
418.endfor # _i
419
420.if !empty(SRCS:Musbdevs.h)
421CLEANFILES+=	usbdevs.h
422.if !exists(@)
423usbdevs.h: @
424.else
425usbdevs.h: @/tools/usbdevs2h.awk @/dev/usb/usbdevs
426.endif
427	${AWK} -f @/tools/usbdevs2h.awk @/dev/usb/usbdevs -h
428.endif
429
430.if !empty(SRCS:Musbdevs_data.h)
431CLEANFILES+=	usbdevs_data.h
432.if !exists(@)
433usbdevs_data.h: @
434.else
435usbdevs_data.h: @/tools/usbdevs2h.awk @/dev/usb/usbdevs
436.endif
437	${AWK} -f @/tools/usbdevs2h.awk @/dev/usb/usbdevs -d
438.endif
439
440.if !empty(SRCS:Macpi_quirks.h)
441CLEANFILES+=	acpi_quirks.h
442.if !exists(@)
443acpi_quirks.h: @
444.else
445acpi_quirks.h: @/tools/acpi_quirks2h.awk @/dev/acpica/acpi_quirks
446.endif
447	${AWK} -f @/tools/acpi_quirks2h.awk @/dev/acpica/acpi_quirks
448.endif
449
450.if !empty(SRCS:Massym.s)
451CLEANFILES+=	assym.s genassym.o
452assym.s: genassym.o
453.if defined(KERNBUILDDIR)
454genassym.o: opt_global.h
455.endif
456.if !exists(@)
457assym.s: @
458.else
459assym.s: @/kern/genassym.sh
460.endif
461	sh @/kern/genassym.sh genassym.o > ${.TARGET}
462.if exists(@)
463genassym.o: @/${MACHINE_CPUARCH}/${MACHINE_CPUARCH}/genassym.c
464.endif
465genassym.o: @ machine ${SRCS:Mopt_*.h}
466	${CC} -c ${CFLAGS:N-fno-common} \
467	    @/${MACHINE_CPUARCH}/${MACHINE_CPUARCH}/genassym.c
468.endif
469
470lint: ${SRCS}
471	${LINT} ${LINTKERNFLAGS} ${CFLAGS:M-[DILU]*} ${.ALLSRC:M*.c}
472
473.if defined(KERNBUILDDIR)
474${OBJS}: opt_global.h
475.endif
476
477.include <bsd.dep.mk>
478
479cleandepend: cleanilinks
480# .depend needs include links so we remove them only together.
481cleanilinks:
482	rm -f ${_ILINKS}
483
484.if !exists(${.OBJDIR}/${DEPENDFILE})
485${OBJS}: ${SRCS:M*.h}
486.endif
487
488.include <bsd.obj.mk>
489.include "kern.mk"
490