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