xref: /freebsd/sys/conf/kmod.mk (revision d34048812292b714a0bf99967270d18fe3097c62)
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_ILINKS=machine
271.if ${MACHINE} != ${MACHINE_CPUARCH} && ${MACHINE} != "arm64"
272_ILINKS+=${MACHINE_CPUARCH}
273.endif
274.if ${MACHINE_CPUARCH} == "i386" || ${MACHINE_CPUARCH} == "amd64"
275_ILINKS+=x86
276.endif
277CLEANFILES+=${_ILINKS}
278
279all: ${PROG}
280
281beforedepend: ${_ILINKS}
282beforebuild: ${_ILINKS}
283
284# Ensure that the links exist without depending on it when it exists which
285# causes all the modules to be rebuilt when the directory pointed to changes.
286.for _link in ${_ILINKS}
287.if !exists(${.OBJDIR}/${_link})
288OBJS_DEPEND_GUESS+=	${_link}
289.endif
290.endfor
291
292.NOPATH: ${_ILINKS}
293
294${_ILINKS}:
295	@case ${.TARGET} in \
296	machine) \
297		path=${SYSDIR}/${MACHINE}/include ;; \
298	*) \
299		path=${SYSDIR}/${.TARGET:T}/include ;; \
300	esac ; \
301	path=`(cd $$path && /bin/pwd)` ; \
302	${ECHO} ${.TARGET:T} "->" $$path ; \
303	ln -fns $$path ${.TARGET:T}
304
305CLEANFILES+= ${PROG} ${KMOD}.kld ${OBJS}
306
307.if defined(DEBUG_FLAGS)
308CLEANFILES+= ${FULLPROG} ${PROG}.debug
309.endif
310
311.if !target(install)
312
313_INSTALLFLAGS:=	${INSTALLFLAGS}
314.for ie in ${INSTALLFLAGS_EDIT}
315_INSTALLFLAGS:=	${_INSTALLFLAGS${ie}}
316.endfor
317
318.if !target(realinstall)
319KERN_DEBUGDIR?=	${DEBUGDIR}
320realinstall: _kmodinstall
321.ORDER: beforeinstall _kmodinstall
322_kmodinstall: .PHONY
323	${INSTALL} -T release -o ${KMODOWN} -g ${KMODGRP} -m ${KMODMODE} \
324	    ${_INSTALLFLAGS} ${PROG} ${DESTDIR}${KMODDIR}/
325.if defined(DEBUG_FLAGS) && !defined(INSTALL_NODEBUG) && ${MK_KERNEL_SYMBOLS} != "no"
326	${INSTALL} -T debug -o ${KMODOWN} -g ${KMODGRP} -m ${KMODMODE} \
327	    ${_INSTALLFLAGS} ${PROG}.debug ${DESTDIR}${KERN_DEBUGDIR}${KMODDIR}/
328.endif
329
330.include <bsd.links.mk>
331
332.if !defined(NO_XREF)
333afterinstall: _kldxref
334.ORDER: realinstall _kldxref
335.ORDER: _installlinks _kldxref
336_kldxref: .PHONY
337	@if type kldxref >/dev/null 2>&1; then \
338		${ECHO} kldxref ${DESTDIR}${KMODDIR}; \
339		kldxref ${DESTDIR}${KMODDIR}; \
340	fi
341.endif
342.endif # !target(realinstall)
343
344.endif # !target(install)
345
346.if !target(load)
347load: ${PROG} .PHONY
348	${KMODLOAD} -v ${.OBJDIR}/${PROG}
349.endif
350
351.if !target(unload)
352unload: .PHONY
353	if ${KMODISLOADED} ${PROG} ; then ${KMODUNLOAD} -v ${PROG} ; fi
354.endif
355
356.if !target(reload)
357reload: unload load .PHONY
358.endif
359
360.if defined(KERNBUILDDIR)
361.PATH: ${KERNBUILDDIR}
362CFLAGS+=	-I${KERNBUILDDIR}
363.for _src in ${SRCS:Mopt_*.h}
364CLEANFILES+=	${_src}
365.if !target(${_src})
366${_src}:
367	ln -sf ${KERNBUILDDIR}/${_src} ${.TARGET}
368.endif
369.endfor
370.else
371.for _src in ${SRCS:Mopt_*.h}
372CLEANFILES+=	${_src}
373.if !target(${_src})
374${_src}:
375	:> ${.TARGET}
376.endif
377.endfor
378.endif
379
380# Add the sanitizer C flags
381CFLAGS+=	${SAN_CFLAGS}
382
383# Respect configuration-specific C flags.
384CFLAGS+=	${ARCH_FLAGS} ${CONF_CFLAGS}
385
386.if !empty(SRCS:Mvnode_if.c)
387CLEANFILES+=	vnode_if.c
388vnode_if.c: ${SYSDIR}/tools/vnode_if.awk ${SYSDIR}/kern/vnode_if.src
389	${AWK} -f ${SYSDIR}/tools/vnode_if.awk ${SYSDIR}/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
394vnode_if.h vnode_if_newproto.h vnode_if_typedef.h: ${SYSDIR}/tools/vnode_if.awk \
395    ${SYSDIR}/kern/vnode_if.src
396vnode_if.h: vnode_if_newproto.h vnode_if_typedef.h
397	${AWK} -f ${SYSDIR}/tools/vnode_if.awk ${SYSDIR}/kern/vnode_if.src -h
398vnode_if_newproto.h:
399	${AWK} -f ${SYSDIR}/tools/vnode_if.awk ${SYSDIR}/kern/vnode_if.src -p
400vnode_if_typedef.h:
401	${AWK} -f ${SYSDIR}/tools/vnode_if.awk ${SYSDIR}/kern/vnode_if.src -q
402.endif
403
404# Build _if.[ch] from _if.m, and clean them when we're done.
405# __MPATH defined in config.mk
406_MFILES=${__MPATH:T:O}
407_MPATH=${__MPATH:H:O:u}
408.PATH.m: ${_MPATH}
409.for _i in ${SRCS:M*_if.[ch]}
410_MATCH=M${_i:R:S/$/.m/}
411_MATCHES=${_MFILES:${_MATCH}}
412.if !empty(_MATCHES)
413CLEANFILES+=	${_i}
414.endif
415.endfor # _i
416.m.c:	${SYSDIR}/tools/makeobjops.awk
417	${AWK} -f ${SYSDIR}/tools/makeobjops.awk ${.IMPSRC} -c
418
419.m.h:	${SYSDIR}/tools/makeobjops.awk
420	${AWK} -f ${SYSDIR}/tools/makeobjops.awk ${.IMPSRC} -h
421
422.for _i in mii pccard
423.if !empty(SRCS:M${_i}devs.h)
424CLEANFILES+=	${_i}devs.h
425${_i}devs.h: ${SYSDIR}/tools/${_i}devs2h.awk ${SYSDIR}/dev/${_i}/${_i}devs
426	${AWK} -f ${SYSDIR}/tools/${_i}devs2h.awk ${SYSDIR}/dev/${_i}/${_i}devs
427.endif
428.endfor # _i
429
430.if !empty(SRCS:Mbhnd_nvram_map.h)
431CLEANFILES+=	bhnd_nvram_map.h
432bhnd_nvram_map.h: ${SYSDIR}/dev/bhnd/tools/nvram_map_gen.awk \
433    ${SYSDIR}/dev/bhnd/tools/nvram_map_gen.sh \
434    ${SYSDIR}/dev/bhnd/nvram/nvram_map
435bhnd_nvram_map.h:
436	sh ${SYSDIR}/dev/bhnd/tools/nvram_map_gen.sh \
437	    ${SYSDIR}/dev/bhnd/nvram/nvram_map -h
438.endif
439
440.if !empty(SRCS:Mbhnd_nvram_map_data.h)
441CLEANFILES+=	bhnd_nvram_map_data.h
442bhnd_nvram_map_data.h: ${SYSDIR}/dev/bhnd/tools/nvram_map_gen.awk \
443    ${SYSDIR}/dev/bhnd/tools/nvram_map_gen.sh \
444    ${SYSDIR}/dev/bhnd/nvram/nvram_map
445bhnd_nvram_map_data.h:
446	sh ${SYSDIR}/dev/bhnd/tools/nvram_map_gen.sh \
447	    ${SYSDIR}/dev/bhnd/nvram/nvram_map -d
448.endif
449
450.if !empty(SRCS:Musbdevs.h)
451CLEANFILES+=	usbdevs.h
452usbdevs.h: ${SYSDIR}/tools/usbdevs2h.awk ${SYSDIR}/dev/usb/usbdevs
453	${AWK} -f ${SYSDIR}/tools/usbdevs2h.awk ${SYSDIR}/dev/usb/usbdevs -h
454.endif
455
456.if !empty(SRCS:Musbdevs_data.h)
457CLEANFILES+=	usbdevs_data.h
458usbdevs_data.h: ${SYSDIR}/tools/usbdevs2h.awk ${SYSDIR}/dev/usb/usbdevs
459	${AWK} -f ${SYSDIR}/tools/usbdevs2h.awk ${SYSDIR}/dev/usb/usbdevs -d
460.endif
461
462.if !empty(SRCS:Macpi_quirks.h)
463CLEANFILES+=	acpi_quirks.h
464acpi_quirks.h: ${SYSDIR}/tools/acpi_quirks2h.awk ${SYSDIR}/dev/acpica/acpi_quirks
465	${AWK} -f ${SYSDIR}/tools/acpi_quirks2h.awk ${SYSDIR}/dev/acpica/acpi_quirks
466.endif
467
468.if !empty(SRCS:Massym.inc) || !empty(DPSRCS:Massym.inc)
469CLEANFILES+=	assym.inc
470DEPENDOBJS+=	genassym.o
471DPSRCS+=	offset.inc
472.endif
473.if defined(MODULE_TIED)
474DPSRCS+=	offset.inc
475.endif
476.if !empty(SRCS:Moffset.inc) || !empty(DPSRCS:Moffset.inc)
477CLEANFILES+=	offset.inc genoffset.o
478DEPENDOBJS+=	genoffset.o
479.endif
480assym.inc: genassym.o
481offset.inc: genoffset.o
482assym.inc: ${SYSDIR}/kern/genassym.sh
483	sh ${SYSDIR}/kern/genassym.sh genassym.o > ${.TARGET}
484genassym.o: ${SYSDIR}/${MACHINE}/${MACHINE}/genassym.c offset.inc
485genassym.o: ${SRCS:Mopt_*.h}
486	${CC} -c ${CFLAGS:N-flto:N-fno-common} \
487	    ${SYSDIR}/${MACHINE}/${MACHINE}/genassym.c
488offset.inc: ${SYSDIR}/kern/genoffset.sh genoffset.o
489	sh ${SYSDIR}/kern/genoffset.sh genoffset.o > ${.TARGET}
490genoffset.o: ${SYSDIR}/kern/genoffset.c
491genoffset.o: ${SRCS:Mopt_*.h}
492	${CC} -c ${CFLAGS:N-flto:N-fno-common} \
493	    ${SYSDIR}/kern/genoffset.c
494
495CLEANDEPENDFILES+=	${_ILINKS}
496# .depend needs include links so we remove them only together.
497cleanilinks:
498	rm -f ${_ILINKS}
499
500OBJS_DEPEND_GUESS+= ${SRCS:M*.h}
501.if defined(KERNBUILDDIR)
502OBJS_DEPEND_GUESS+= opt_global.h
503.endif
504
505.include <bsd.dep.mk>
506.include <bsd.clang-analyze.mk>
507.include <bsd.obj.mk>
508.include "kern.mk"
509