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