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