xref: /freebsd/sys/conf/kmod.mk (revision 62cfcf62f627e5093fb37026a6d8c98e4d2ef04c)
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# KMODISLOADED	Command to check whether a kernel module is
32#		loaded [/sbin/kldstat -q -n]
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# KERNBUILDDIR
48#		Set to the location of the kernel build directory where
49#		the opt_*.h files, .o's and kernel winds up.
50#
51# +++ targets +++
52#
53# 	install:
54#               install the kernel module; if the Makefile
55#               does not itself define the target install, the targets
56#               beforeinstall and afterinstall may also be used to cause
57#               actions immediately before and after the install target
58#		is executed.
59#
60# 	load:
61#		Load a module.
62#
63# 	unload:
64#		Unload a module.
65#
66#	reload:
67#		Unload if loaded, then load.
68#
69
70AWK?=		awk
71KMODLOAD?=	/sbin/kldload
72KMODUNLOAD?=	/sbin/kldunload
73KMODISLOADED?=	/sbin/kldstat -q -n
74OBJCOPY?=	objcopy
75
76.include "kmod.opts.mk"
77.include <bsd.sysdir.mk>
78
79.SUFFIXES: .out .o .c .cc .cxx .C .y .l .s .S .m
80
81# amd64 and mips use direct linking for kmod, all others use shared binaries
82.if ${MACHINE_CPUARCH} != amd64 && ${MACHINE_CPUARCH} != mips
83__KLD_SHARED=yes
84.else
85__KLD_SHARED=no
86.endif
87
88.if !empty(CFLAGS:M-O[23s]) && empty(CFLAGS:M-fno-strict-aliasing)
89CFLAGS+=	-fno-strict-aliasing
90.endif
91WERROR?=	-Werror
92
93LINUXKPI_GENSRCS+= \
94	bus_if.h \
95	device_if.h \
96	pci_if.h \
97	pci_iov_if.h \
98	vnode_if.h \
99	usb_if.h \
100	opt_usb.h \
101	opt_stack.h
102
103CFLAGS+=	${WERROR}
104CFLAGS+=	-D_KERNEL
105CFLAGS+=	-DKLD_MODULE
106.if defined(MODULE_TIED)
107CFLAGS+=	-DKLD_TIED
108.endif
109
110# Don't use any standard or source-relative include directories.
111NOSTDINC=	-nostdinc
112CFLAGS:=	${CFLAGS:N-I*} ${NOSTDINC} ${INCLMAGIC} ${CFLAGS:M-I*}
113.if defined(KERNBUILDDIR)
114CFLAGS+=	-DHAVE_KERNEL_OPTION_HEADERS -include ${KERNBUILDDIR}/opt_global.h
115.else
116SRCS+=		opt_global.h
117CFLAGS+=	-include ${.OBJDIR}/opt_global.h
118.endif
119
120# Add -I paths for system headers.  Individual module makefiles don't
121# need any -I paths for this.  Similar defaults for .PATH can't be
122# set because there are no standard paths for non-headers.
123CFLAGS+=	-I. -I${SYSDIR} -I${SYSDIR}/contrib/ck/include
124
125CFLAGS.gcc+=	-finline-limit=${INLINE_LIMIT}
126CFLAGS.gcc+=	-fms-extensions
127CFLAGS.gcc+= --param inline-unit-growth=100
128CFLAGS.gcc+= --param large-function-growth=1000
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#
133# -fno-common is the default for src builds, but this should be left in place
134# until at least we catch up to GCC10/LLVM11 or otherwise enable -fno-common
135# in <bsd.sys.mk> instead.  For now, we will have duplicate -fno-common in
136# CFLAGS for in-tree module builds as they will also pick it up from
137# share/mk/src.sys.mk, but the following is important for out-of-tree modules
138# (e.g. ports).
139CFLAGS+=	-fno-common
140LDFLAGS+=	-d -warn-common
141
142.if defined(LINKER_FEATURES) && ${LINKER_FEATURES:Mbuild-id}
143LDFLAGS+=	--build-id=sha1
144.endif
145
146CFLAGS+=	${DEBUG_FLAGS}
147.if ${MACHINE_CPUARCH} == amd64
148CFLAGS+=	-fno-omit-frame-pointer -mno-omit-leaf-frame-pointer
149.endif
150
151.if ${MACHINE_CPUARCH} == "aarch64" || ${MACHINE_CPUARCH} == "riscv" || \
152    ${MACHINE_CPUARCH} == "powerpc"
153CFLAGS+=	-fPIC
154.endif
155
156# Temporary workaround for PR 196407, which contains the fascinating details.
157# Don't allow clang to use fpu instructions or registers in kernel modules.
158.if ${MACHINE_CPUARCH} == arm
159CFLAGS.clang+=	-mno-movt
160CFLAGS.clang+=	-mfpu=none
161CFLAGS+=	-funwind-tables
162.endif
163
164.if ${MACHINE_CPUARCH} == powerpc
165CFLAGS+=	-mlongcall -fno-omit-frame-pointer
166.if ${LINKER_TYPE} == "lld"
167# TOC optimization in LLD (9.0) currently breaks kernel modules, so disable it
168LDFLAGS+=	--no-toc-optimize
169.endif
170.endif
171
172.if ${MACHINE_CPUARCH} == mips
173CFLAGS+=	-G0 -fno-pic -mno-abicalls -mlong-calls
174.endif
175
176.if defined(DEBUG) || defined(DEBUG_FLAGS)
177CTFFLAGS+=	-g
178.endif
179
180.if defined(FIRMWS)
181${KMOD:S/$/.c/}: ${SYSDIR}/tools/fw_stub.awk
182	${AWK} -f ${SYSDIR}/tools/fw_stub.awk ${FIRMWS} -m${KMOD} -c${KMOD:S/$/.c/g} \
183	    ${FIRMWARE_LICENSE:C/.+/-l/}${FIRMWARE_LICENSE}
184
185SRCS+=	${KMOD:S/$/.c/}
186CLEANFILES+=	${KMOD:S/$/.c/}
187
188.for _firmw in ${FIRMWS}
189${_firmw:C/\:.*$/.fwo/:T}:	${_firmw:C/\:.*$//}
190	@${ECHO} ${_firmw:C/\:.*$//} ${.ALLSRC:M*${_firmw:C/\:.*$//}}
191	@if [ -e ${_firmw:C/\:.*$//} ]; then			\
192		${LD} -b binary --no-warn-mismatch ${_LDFLAGS}	\
193		    -m ${LD_EMULATION} -r -d			\
194		    -o ${.TARGET} ${_firmw:C/\:.*$//};		\
195	else							\
196		ln -s ${.ALLSRC:M*${_firmw:C/\:.*$//}} ${_firmw:C/\:.*$//}; \
197		${LD} -b binary --no-warn-mismatch ${_LDFLAGS}	\
198		    -m ${LD_EMULATION} -r -d			\
199		    -o ${.TARGET} ${_firmw:C/\:.*$//};		\
200		rm ${_firmw:C/\:.*$//};				\
201	fi
202
203OBJS+=	${_firmw:C/\:.*$/.fwo/:T}
204.endfor
205.endif
206
207# Conditionally include SRCS based on kernel config options.
208.for _o in ${KERN_OPTS}
209SRCS+=${SRCS.${_o}}
210.endfor
211
212OBJS+=	${SRCS:N*.h:R:S/$/.o/g}
213
214.if !defined(PROG)
215PROG=	${KMOD}.ko
216.endif
217
218.if !defined(DEBUG_FLAGS) || ${MK_KERNEL_SYMBOLS} == "no"
219FULLPROG=	${PROG}
220.else
221FULLPROG=	${PROG}.full
222${PROG}: ${FULLPROG} ${PROG}.debug
223	${OBJCOPY} --strip-debug --add-gnu-debuglink=${PROG}.debug \
224	    ${FULLPROG} ${.TARGET}
225${PROG}.debug: ${FULLPROG}
226	${OBJCOPY} --only-keep-debug ${FULLPROG} ${.TARGET}
227.endif
228
229.if ${__KLD_SHARED} == yes
230${FULLPROG}: ${KMOD}.kld
231	${LD} -m ${LD_EMULATION} -Bshareable -znotext -znorelro ${_LDFLAGS} \
232	    -o ${.TARGET} ${KMOD}.kld
233.if !defined(DEBUG_FLAGS)
234	${OBJCOPY} --strip-debug ${.TARGET}
235.endif
236.endif
237
238EXPORT_SYMS?=	NO
239.if ${EXPORT_SYMS} != YES
240CLEANFILES+=	export_syms
241.endif
242
243.if exists(${SYSDIR}/conf/ldscript.kmod.${MACHINE_ARCH})
244LDSCRIPT_FLAGS?= -T ${SYSDIR}/conf/ldscript.kmod.${MACHINE_ARCH}
245.endif
246
247.if ${__KLD_SHARED} == yes
248${KMOD}.kld: ${OBJS}
249.else
250${FULLPROG}: ${OBJS}
251.endif
252	${LD} -m ${LD_EMULATION} ${_LDFLAGS} ${LDSCRIPT_FLAGS} -r -d \
253	    -o ${.TARGET} ${OBJS}
254.if ${MK_CTF} != "no"
255	${CTFMERGE} ${CTFFLAGS} -o ${.TARGET} ${OBJS}
256.endif
257.if defined(EXPORT_SYMS)
258.if ${EXPORT_SYMS} != YES
259.if ${EXPORT_SYMS} == NO
260	:> export_syms
261.elif !exists(${.CURDIR}/${EXPORT_SYMS})
262	echo -n "${EXPORT_SYMS:@s@$s${.newline}@}" > export_syms
263.else
264	grep -v '^#' < ${EXPORT_SYMS} > export_syms
265.endif
266	${AWK} -f ${SYSDIR}/conf/kmod_syms.awk ${.TARGET} \
267	    export_syms | xargs -J% ${OBJCOPY} % ${.TARGET}
268.endif
269.endif # defined(EXPORT_SYMS)
270.if defined(PREFIX_SYMS)
271	${AWK} -v prefix=${PREFIX_SYMS} -f ${SYSDIR}/conf/kmod_syms_prefix.awk \
272	    ${.TARGET} /dev/null | xargs -J% ${OBJCOPY} % ${.TARGET}
273.endif
274.if !defined(DEBUG_FLAGS) && ${__KLD_SHARED} == no
275	${OBJCOPY} --strip-debug ${.TARGET}
276.endif
277
278_MAP_DEBUG_PREFIX= yes
279
280_ILINKS=machine
281.if ${MACHINE_CPUARCH} == "i386" || ${MACHINE_CPUARCH} == "amd64"
282_ILINKS+=x86
283.endif
284CLEANFILES+=${_ILINKS}
285
286all: ${PROG}
287
288beforedepend: ${_ILINKS}
289beforebuild: ${_ILINKS}
290
291# Ensure that the links exist without depending on it when it exists which
292# causes all the modules to be rebuilt when the directory pointed to changes.
293# Ensure that debug info references the path in the source tree.
294.for _link in ${_ILINKS}
295.if !exists(${.OBJDIR}/${_link})
296OBJS_DEPEND_GUESS+=	${_link}
297.endif
298.if defined(_MAP_DEBUG_PREFIX)
299.if ${_link} == "machine"
300CFLAGS+= -fdebug-prefix-map=./machine=${SYSDIR}/${MACHINE}/include
301.else
302CFLAGS+= -fdebug-prefix-map=./${_link}=${SYSDIR}/${_link}/include
303.endif
304.endif
305.endfor
306
307.NOPATH: ${_ILINKS}
308
309${_ILINKS}:
310	@case ${.TARGET} in \
311	machine) \
312		path=${SYSDIR}/${MACHINE}/include ;; \
313	*) \
314		path=${SYSDIR}/${.TARGET:T}/include ;; \
315	esac ; \
316	path=`(cd $$path && /bin/pwd)` ; \
317	${ECHO} ${.TARGET:T} "->" $$path ; \
318	ln -fns $$path ${.TARGET:T}
319
320CLEANFILES+= ${PROG} ${KMOD}.kld ${OBJS}
321
322.if defined(DEBUG_FLAGS) && ${MK_KERNEL_SYMBOLS} != "no"
323CLEANFILES+= ${FULLPROG} ${PROG}.debug
324.endif
325
326.if !target(install)
327
328_INSTALLFLAGS:=	${INSTALLFLAGS}
329.for ie in ${INSTALLFLAGS_EDIT}
330_INSTALLFLAGS:=	${_INSTALLFLAGS${ie}}
331.endfor
332
333.if !target(realinstall)
334KERN_DEBUGDIR?=	${DEBUGDIR}
335realinstall: _kmodinstall
336.ORDER: beforeinstall _kmodinstall
337_kmodinstall: .PHONY
338	${INSTALL} -T release -o ${KMODOWN} -g ${KMODGRP} -m ${KMODMODE} \
339	    ${_INSTALLFLAGS} ${PROG} ${DESTDIR}${KMODDIR}/
340.if defined(DEBUG_FLAGS) && !defined(INSTALL_NODEBUG) && ${MK_KERNEL_SYMBOLS} != "no"
341	${INSTALL} -T dbg -o ${KMODOWN} -g ${KMODGRP} -m ${KMODMODE} \
342	    ${_INSTALLFLAGS} ${PROG}.debug ${DESTDIR}${KERN_DEBUGDIR}${KMODDIR}/
343.endif
344
345.include <bsd.links.mk>
346
347.if !defined(NO_XREF)
348afterinstall: _kldxref
349.ORDER: realinstall _kldxref
350.ORDER: _installlinks _kldxref
351_kldxref: .PHONY
352	@if type kldxref >/dev/null 2>&1; then \
353		${ECHO} ${KLDXREF_CMD} ${DESTDIR}${KMODDIR}; \
354		${KLDXREF_CMD} ${DESTDIR}${KMODDIR}; \
355	fi
356.endif
357.endif # !target(realinstall)
358
359.endif # !target(install)
360
361.if !target(load)
362load: ${PROG} .PHONY
363	${KMODLOAD} -v ${.OBJDIR}/${PROG}
364.endif
365
366.if !target(unload)
367unload: .PHONY
368	if ${KMODISLOADED} ${PROG} ; then ${KMODUNLOAD} -v ${PROG} ; fi
369.endif
370
371.if !target(reload)
372reload: unload load .PHONY
373.endif
374
375.if defined(KERNBUILDDIR)
376.PATH: ${KERNBUILDDIR}
377CFLAGS+=	-I${KERNBUILDDIR}
378.for _src in ${SRCS:Mopt_*.h}
379CLEANFILES+=	${_src}
380.if !target(${_src})
381${_src}:
382	ln -sf ${KERNBUILDDIR}/${_src} ${.TARGET}
383.endif
384.endfor
385.else
386.for _src in ${SRCS:Mopt_*.h}
387CLEANFILES+=	${_src}
388.if !target(${_src})
389${_src}:
390	:> ${.TARGET}
391.endif
392.endfor
393.endif
394
395# Add the sanitizer C flags
396CFLAGS+=	${SAN_CFLAGS}
397
398# Add the gcov flags
399CFLAGS+=	${GCOV_CFLAGS}
400
401# Respect configuration-specific C flags.
402CFLAGS+=	${ARCH_FLAGS} ${CONF_CFLAGS}
403
404.if !empty(SRCS:Mvnode_if.c)
405CLEANFILES+=	vnode_if.c
406vnode_if.c: ${SYSDIR}/tools/vnode_if.awk ${SYSDIR}/kern/vnode_if.src
407	${AWK} -f ${SYSDIR}/tools/vnode_if.awk ${SYSDIR}/kern/vnode_if.src -c
408.endif
409
410.if !empty(SRCS:Mvnode_if.h)
411CLEANFILES+=	vnode_if.h vnode_if_newproto.h vnode_if_typedef.h
412vnode_if.h vnode_if_newproto.h vnode_if_typedef.h: ${SYSDIR}/tools/vnode_if.awk \
413    ${SYSDIR}/kern/vnode_if.src
414vnode_if.h: vnode_if_newproto.h vnode_if_typedef.h
415	${AWK} -f ${SYSDIR}/tools/vnode_if.awk ${SYSDIR}/kern/vnode_if.src -h
416vnode_if_newproto.h:
417	${AWK} -f ${SYSDIR}/tools/vnode_if.awk ${SYSDIR}/kern/vnode_if.src -p
418vnode_if_typedef.h:
419	${AWK} -f ${SYSDIR}/tools/vnode_if.awk ${SYSDIR}/kern/vnode_if.src -q
420.endif
421
422# Build _if.[ch] from _if.m, and clean them when we're done.
423# __MPATH defined in config.mk
424_MFILES=${__MPATH:T:O}
425_MPATH=${__MPATH:H:O:u}
426.PATH.m: ${_MPATH}
427.for _i in ${SRCS:M*_if.[ch]}
428_MATCH=M${_i:R:S/$/.m/}
429_MATCHES=${_MFILES:${_MATCH}}
430.if !empty(_MATCHES)
431CLEANFILES+=	${_i}
432.endif
433.endfor # _i
434.m.c:	${SYSDIR}/tools/makeobjops.awk
435	${AWK} -f ${SYSDIR}/tools/makeobjops.awk ${.IMPSRC} -c
436
437.m.h:	${SYSDIR}/tools/makeobjops.awk
438	${AWK} -f ${SYSDIR}/tools/makeobjops.awk ${.IMPSRC} -h
439
440.for _i in mii pccard
441.if !empty(SRCS:M${_i}devs.h)
442CLEANFILES+=	${_i}devs.h
443${_i}devs.h: ${SYSDIR}/tools/${_i}devs2h.awk ${SYSDIR}/dev/${_i}/${_i}devs
444	${AWK} -f ${SYSDIR}/tools/${_i}devs2h.awk ${SYSDIR}/dev/${_i}/${_i}devs
445.endif
446.endfor # _i
447
448.if !empty(SRCS:Mbhnd_nvram_map.h)
449CLEANFILES+=	bhnd_nvram_map.h
450bhnd_nvram_map.h: ${SYSDIR}/dev/bhnd/tools/nvram_map_gen.awk \
451    ${SYSDIR}/dev/bhnd/tools/nvram_map_gen.sh \
452    ${SYSDIR}/dev/bhnd/nvram/nvram_map
453bhnd_nvram_map.h:
454	sh ${SYSDIR}/dev/bhnd/tools/nvram_map_gen.sh \
455	    ${SYSDIR}/dev/bhnd/nvram/nvram_map -h
456.endif
457
458.if !empty(SRCS:Mbhnd_nvram_map_data.h)
459CLEANFILES+=	bhnd_nvram_map_data.h
460bhnd_nvram_map_data.h: ${SYSDIR}/dev/bhnd/tools/nvram_map_gen.awk \
461    ${SYSDIR}/dev/bhnd/tools/nvram_map_gen.sh \
462    ${SYSDIR}/dev/bhnd/nvram/nvram_map
463bhnd_nvram_map_data.h:
464	sh ${SYSDIR}/dev/bhnd/tools/nvram_map_gen.sh \
465	    ${SYSDIR}/dev/bhnd/nvram/nvram_map -d
466.endif
467
468.if !empty(SRCS:Musbdevs.h)
469CLEANFILES+=	usbdevs.h
470usbdevs.h: ${SYSDIR}/tools/usbdevs2h.awk ${SYSDIR}/dev/usb/usbdevs
471	${AWK} -f ${SYSDIR}/tools/usbdevs2h.awk ${SYSDIR}/dev/usb/usbdevs -h
472.endif
473
474.if !empty(SRCS:Musbdevs_data.h)
475CLEANFILES+=	usbdevs_data.h
476usbdevs_data.h: ${SYSDIR}/tools/usbdevs2h.awk ${SYSDIR}/dev/usb/usbdevs
477	${AWK} -f ${SYSDIR}/tools/usbdevs2h.awk ${SYSDIR}/dev/usb/usbdevs -d
478.endif
479
480.if !empty(SRCS:Msdiodevs.h)
481CLEANFILES+=	sdiodevs.h
482sdiodevs.h: ${SYSDIR}/tools/sdiodevs2h.awk ${SYSDIR}/dev/sdio/sdiodevs
483	${AWK} -f ${SYSDIR}/tools/sdiodevs2h.awk ${SYSDIR}/dev/sdio/sdiodevs -h
484.endif
485
486.if !empty(SRCS:Msdiodevs_data.h)
487CLEANFILES+=	sdiodevs_data.h
488sdiodevs_data.h: ${SYSDIR}/tools/sdiodevs2h.awk ${SYSDIR}/dev/sdio/sdiodevs
489	${AWK} -f ${SYSDIR}/tools/sdiodevs2h.awk ${SYSDIR}/dev/sdio/sdiodevs -d
490.endif
491
492.if !empty(SRCS:Macpi_quirks.h)
493CLEANFILES+=	acpi_quirks.h
494acpi_quirks.h: ${SYSDIR}/tools/acpi_quirks2h.awk ${SYSDIR}/dev/acpica/acpi_quirks
495	${AWK} -f ${SYSDIR}/tools/acpi_quirks2h.awk ${SYSDIR}/dev/acpica/acpi_quirks
496.endif
497
498.if !empty(SRCS:Massym.inc) || !empty(DPSRCS:Massym.inc)
499CLEANFILES+=	assym.inc genassym.o
500DEPENDOBJS+=	genassym.o
501DPSRCS+=	offset.inc
502.endif
503.if defined(MODULE_TIED)
504DPSRCS+=	offset.inc
505.endif
506.if !empty(SRCS:Moffset.inc) || !empty(DPSRCS:Moffset.inc)
507CLEANFILES+=	offset.inc genoffset.o
508DEPENDOBJS+=	genoffset.o
509.endif
510assym.inc: genassym.o
511offset.inc: genoffset.o
512assym.inc: ${SYSDIR}/kern/genassym.sh
513	sh ${SYSDIR}/kern/genassym.sh genassym.o > ${.TARGET}
514genassym.o: ${SYSDIR}/${MACHINE}/${MACHINE}/genassym.c offset.inc
515genassym.o: ${SRCS:Mopt_*.h}
516	${CC} -c ${CFLAGS:N-flto:N-fno-common} -fcommon \
517	    ${SYSDIR}/${MACHINE}/${MACHINE}/genassym.c
518offset.inc: ${SYSDIR}/kern/genoffset.sh genoffset.o
519	sh ${SYSDIR}/kern/genoffset.sh genoffset.o > ${.TARGET}
520genoffset.o: ${SYSDIR}/kern/genoffset.c
521genoffset.o: ${SRCS:Mopt_*.h}
522	${CC} -c ${CFLAGS:N-flto:N-fno-common} -fcommon \
523	    ${SYSDIR}/kern/genoffset.c
524
525CLEANDEPENDFILES+=	${_ILINKS}
526# .depend needs include links so we remove them only together.
527cleanilinks:
528	rm -f ${_ILINKS}
529
530OBJS_DEPEND_GUESS+= ${SRCS:M*.h}
531.if defined(KERNBUILDDIR)
532OBJS_DEPEND_GUESS+= opt_global.h
533.endif
534
535.include <bsd.dep.mk>
536.include <bsd.clang-analyze.mk>
537.include <bsd.obj.mk>
538.include "kern.mk"
539