xref: /freebsd/sys/conf/kmod.mk (revision 07d397d74651435ff6f4d474876589e85967350e)
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# Respect configuration-specific C flags.
381CFLAGS+=	${ARCH_FLAGS} ${CONF_CFLAGS}
382
383.if !empty(SRCS:Mvnode_if.c)
384CLEANFILES+=	vnode_if.c
385vnode_if.c: ${SYSDIR}/tools/vnode_if.awk ${SYSDIR}/kern/vnode_if.src
386	${AWK} -f ${SYSDIR}/tools/vnode_if.awk ${SYSDIR}/kern/vnode_if.src -c
387.endif
388
389.if !empty(SRCS:Mvnode_if.h)
390CLEANFILES+=	vnode_if.h vnode_if_newproto.h vnode_if_typedef.h
391vnode_if.h vnode_if_newproto.h vnode_if_typedef.h: ${SYSDIR}/tools/vnode_if.awk \
392    ${SYSDIR}/kern/vnode_if.src
393vnode_if.h: vnode_if_newproto.h vnode_if_typedef.h
394	${AWK} -f ${SYSDIR}/tools/vnode_if.awk ${SYSDIR}/kern/vnode_if.src -h
395vnode_if_newproto.h:
396	${AWK} -f ${SYSDIR}/tools/vnode_if.awk ${SYSDIR}/kern/vnode_if.src -p
397vnode_if_typedef.h:
398	${AWK} -f ${SYSDIR}/tools/vnode_if.awk ${SYSDIR}/kern/vnode_if.src -q
399.endif
400
401# Build _if.[ch] from _if.m, and clean them when we're done.
402# __MPATH defined in config.mk
403_MFILES=${__MPATH:T:O}
404_MPATH=${__MPATH:H:O:u}
405.PATH.m: ${_MPATH}
406.for _i in ${SRCS:M*_if.[ch]}
407_MATCH=M${_i:R:S/$/.m/}
408_MATCHES=${_MFILES:${_MATCH}}
409.if !empty(_MATCHES)
410CLEANFILES+=	${_i}
411.endif
412.endfor # _i
413.m.c:	${SYSDIR}/tools/makeobjops.awk
414	${AWK} -f ${SYSDIR}/tools/makeobjops.awk ${.IMPSRC} -c
415
416.m.h:	${SYSDIR}/tools/makeobjops.awk
417	${AWK} -f ${SYSDIR}/tools/makeobjops.awk ${.IMPSRC} -h
418
419.for _i in mii pccard
420.if !empty(SRCS:M${_i}devs.h)
421CLEANFILES+=	${_i}devs.h
422${_i}devs.h: ${SYSDIR}/tools/${_i}devs2h.awk ${SYSDIR}/dev/${_i}/${_i}devs
423	${AWK} -f ${SYSDIR}/tools/${_i}devs2h.awk ${SYSDIR}/dev/${_i}/${_i}devs
424.endif
425.endfor # _i
426
427.if !empty(SRCS:Mbhnd_nvram_map.h)
428CLEANFILES+=	bhnd_nvram_map.h
429bhnd_nvram_map.h: ${SYSDIR}/dev/bhnd/tools/nvram_map_gen.awk \
430    ${SYSDIR}/dev/bhnd/tools/nvram_map_gen.sh \
431    ${SYSDIR}/dev/bhnd/nvram/nvram_map
432bhnd_nvram_map.h:
433	sh ${SYSDIR}/dev/bhnd/tools/nvram_map_gen.sh \
434	    ${SYSDIR}/dev/bhnd/nvram/nvram_map -h
435.endif
436
437.if !empty(SRCS:Mbhnd_nvram_map_data.h)
438CLEANFILES+=	bhnd_nvram_map_data.h
439bhnd_nvram_map_data.h: ${SYSDIR}/dev/bhnd/tools/nvram_map_gen.awk \
440    ${SYSDIR}/dev/bhnd/tools/nvram_map_gen.sh \
441    ${SYSDIR}/dev/bhnd/nvram/nvram_map
442bhnd_nvram_map_data.h:
443	sh ${SYSDIR}/dev/bhnd/tools/nvram_map_gen.sh \
444	    ${SYSDIR}/dev/bhnd/nvram/nvram_map -d
445.endif
446
447.if !empty(SRCS:Musbdevs.h)
448CLEANFILES+=	usbdevs.h
449usbdevs.h: ${SYSDIR}/tools/usbdevs2h.awk ${SYSDIR}/dev/usb/usbdevs
450	${AWK} -f ${SYSDIR}/tools/usbdevs2h.awk ${SYSDIR}/dev/usb/usbdevs -h
451.endif
452
453.if !empty(SRCS:Musbdevs_data.h)
454CLEANFILES+=	usbdevs_data.h
455usbdevs_data.h: ${SYSDIR}/tools/usbdevs2h.awk ${SYSDIR}/dev/usb/usbdevs
456	${AWK} -f ${SYSDIR}/tools/usbdevs2h.awk ${SYSDIR}/dev/usb/usbdevs -d
457.endif
458
459.if !empty(SRCS:Macpi_quirks.h)
460CLEANFILES+=	acpi_quirks.h
461acpi_quirks.h: ${SYSDIR}/tools/acpi_quirks2h.awk ${SYSDIR}/dev/acpica/acpi_quirks
462	${AWK} -f ${SYSDIR}/tools/acpi_quirks2h.awk ${SYSDIR}/dev/acpica/acpi_quirks
463.endif
464
465.if !empty(SRCS:Massym.inc) || !empty(DPSRCS:Massym.inc)
466CLEANFILES+=	assym.inc
467DEPENDOBJS+=	genassym.o
468DPSRCS+=	offset.inc
469.endif
470.if defined(MODULE_TIED)
471DPSRCS+=	offset.inc
472.endif
473.if !empty(SRCS:Moffset.inc) || !empty(DPSRCS:Moffset.inc)
474CLEANFILES+=	offset.inc genoffset.o
475DEPENDOBJS+=	genoffset.o
476.endif
477assym.inc: genassym.o
478offset.inc: genoffset.o
479assym.inc: ${SYSDIR}/kern/genassym.sh
480	sh ${SYSDIR}/kern/genassym.sh genassym.o > ${.TARGET}
481genassym.o: ${SYSDIR}/${MACHINE}/${MACHINE}/genassym.c offset.inc
482genassym.o: ${SRCS:Mopt_*.h}
483	${CC} -c ${CFLAGS:N-flto:N-fno-common} \
484	    ${SYSDIR}/${MACHINE}/${MACHINE}/genassym.c
485offset.inc: ${SYSDIR}/kern/genoffset.sh genoffset.o
486	sh ${SYSDIR}/kern/genoffset.sh genoffset.o > ${.TARGET}
487genoffset.o: ${SYSDIR}/kern/genoffset.c
488genoffset.o: ${SRCS:Mopt_*.h}
489	${CC} -c ${CFLAGS:N-flto:N-fno-common} \
490	    ${SYSDIR}/kern/genoffset.c
491
492CLEANDEPENDFILES+=	${_ILINKS}
493# .depend needs include links so we remove them only together.
494cleanilinks:
495	rm -f ${_ILINKS}
496
497OBJS_DEPEND_GUESS+= ${SRCS:M*.h}
498.if defined(KERNBUILDDIR)
499OBJS_DEPEND_GUESS+= opt_global.h
500.endif
501
502.include <bsd.dep.mk>
503.include <bsd.clang-analyze.mk>
504.include <bsd.obj.mk>
505.include "kern.mk"
506