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