xref: /freebsd/sys/conf/kmod.mk (revision 6e3875ebcf0e768332735f8306bf4625b30ab61c)
11f089616SRuslan Ermilov# The include file <bsd.kmod.mk> handles building and installing loadable
21f089616SRuslan Ermilov# kernel modules.
353996fadSWolfram Schneider#
453996fadSWolfram Schneider#
553996fadSWolfram Schneider# +++ variables +++
653996fadSWolfram Schneider#
753996fadSWolfram Schneider# CLEANFILES	Additional files to remove for the clean and cleandir targets.
853996fadSWolfram Schneider#
91f089616SRuslan Ermilov# EXPORT_SYMS	A list of symbols that should be exported from the module,
101f089616SRuslan Ermilov#		or the name of a file containing a list of symbols, or YES
111f089616SRuslan Ermilov#		to export all symbols.  If not defined, no symbols are
121f089616SRuslan Ermilov#		exported.
131f089616SRuslan Ermilov#
14fe3cac87SNick Hibma# KMOD		The name of the kernel module to build.
1553996fadSWolfram Schneider#
163bdfa9e5SDavid E. O'Brien# KMODDIR	Base path for kernel modules (see kld(4)). [/boot/kernel]
1753996fadSWolfram Schneider#
181f089616SRuslan Ermilov# KMODOWN	Module file owner. [${BINOWN}]
1953996fadSWolfram Schneider#
201f089616SRuslan Ermilov# KMODGRP	Module file group. [${BINGRP}]
2153996fadSWolfram Schneider#
221f089616SRuslan Ermilov# KMODMODE	Module file mode. [${BINMODE}]
2353996fadSWolfram Schneider#
24fe3cac87SNick Hibma# KMODLOAD	Command to load a kernel module [/sbin/kldload]
257bc830bbSWolfram Schneider#
26fe3cac87SNick Hibma# KMODUNLOAD	Command to unload a kernel module [/sbin/kldunload]
277bc830bbSWolfram Schneider#
2893bffaf2SDag-Erling Smørgrav# KMODISLOADED	Command to check whether a kernel module is
2993bffaf2SDag-Erling Smørgrav#		loaded [/sbin/kldstat -q -n]
3093bffaf2SDag-Erling Smørgrav#
311f089616SRuslan Ermilov# PROG		The name of the kernel module to build.
321f089616SRuslan Ermilov#		If not supplied, ${KMOD}.ko is used.
331f089616SRuslan Ermilov#
341f089616SRuslan Ermilov# SRCS		List of source files.
351f089616SRuslan Ermilov#
366aec1278SMax Laier# FIRMWS	List of firmware images in format filename:shortname:version
376aec1278SMax Laier#
3841e80f2aSFlorent Thoumie# FIRMWARE_LICENSE
397bd6fde3SFlorent Thoumie#		Set to the name of the license the user has to agree on in
407bd6fde3SFlorent Thoumie#		order to use this firmware. See /usr/share/doc/legal
417bd6fde3SFlorent Thoumie#
421f089616SRuslan Ermilov# DESTDIR	The tree where the module gets installed. [not set]
430b3178a4SMike Smith#
44e8dca53aSWarner Losh# KERNBUILDDIR	Set to the location of the kernel build directory where
45489b536fSWarner Losh#		the opt_*.h files, .o's and kernel winds up.
46489b536fSWarner Losh#
47e8dca53aSWarner Losh# BLOB_OBJS	Prebuilt binary blobs .o's from the src tree to be linked into
48e8dca53aSWarner Losh#		the module. These are precious and not removed in make clean.
49e8dca53aSWarner Losh#
5053996fadSWolfram Schneider# +++ targets +++
5153996fadSWolfram Schneider#
5253996fadSWolfram Schneider# 	install:
5317d6c636SRuslan Ermilov#               install the kernel module; if the Makefile
5453996fadSWolfram Schneider#               does not itself define the target install, the targets
5553996fadSWolfram Schneider#               beforeinstall and afterinstall may also be used to cause
5653996fadSWolfram Schneider#               actions immediately before and after the install target
5753996fadSWolfram Schneider#		is executed.
5853996fadSWolfram Schneider#
5953996fadSWolfram Schneider# 	load:
601f089616SRuslan Ermilov#		Load a module.
6153996fadSWolfram Schneider#
6253996fadSWolfram Schneider# 	unload:
631f089616SRuslan Ermilov#		Unload a module.
6453996fadSWolfram Schneider#
6593bffaf2SDag-Erling Smørgrav#	reload:
6693bffaf2SDag-Erling Smørgrav#		Unload if loaded, then load.
6793bffaf2SDag-Erling Smørgrav#
68639a2e50SGarrett Wollman
69b7bbb709SDavid E. O'BrienAWK?=		awk
70fe3cac87SNick HibmaKMODLOAD?=	/sbin/kldload
71fe3cac87SNick HibmaKMODUNLOAD?=	/sbin/kldunload
7293bffaf2SDag-Erling SmørgravKMODISLOADED?=	/sbin/kldstat -q -n
73f6611608SSheldon HearnOBJCOPY?=	objcopy
74dd4d206cSSimon J. GerratyXARGS?=		xargs
75dd4d206cSSimon J. GerratyXARGS_J?=	-J
767bc830bbSWolfram Schneider
779ab286b7SJohn Baldwin.include "kmod.opts.mk"
783cc5d6a4SWarner Losh.include <bsd.sysdir.mk>
795d4447e0SBryan Drewery
8012f05b84SWarner Losh.SUFFIXES: .out .o .c .cc .cxx .C .y .l .s .S .m
81639a2e50SGarrett Wollman
82c09981f1SWarner Losh# amd64 uses direct linking for kmod, all others use shared binaries
83c09981f1SWarner Losh.if ${MACHINE_CPUARCH} != amd64
843a0d123eSWarner Losh__KLD_SHARED=yes
853a0d123eSWarner Losh.else
863a0d123eSWarner Losh__KLD_SHARED=no
873a0d123eSWarner Losh.endif
883a0d123eSWarner Losh
89f165cb7cSRuslan Ermilov.if !empty(CFLAGS:M-O[23s]) && empty(CFLAGS:M-fno-strict-aliasing)
90db0cd7e1SDavid E. O'BrienCFLAGS+=	-fno-strict-aliasing
91db0cd7e1SDavid E. O'Brien.endif
925d3b2922SRuslan ErmilovWERROR?=	-Werror
9317c12f64SWarner Losh
9415fe2513SKonstantin BelousovLINUXKPI_GENSRCS+= \
952b68c973SEmmanuel Vadot	backlight_if.h \
9615fe2513SKonstantin Belousov	bus_if.h \
9715fe2513SKonstantin Belousov	device_if.h \
981961a14aSEmmanuel Vadot	iicbus_if.h \
991961a14aSEmmanuel Vadot	iicbb_if.h \
1001961a14aSEmmanuel Vadot	lkpi_iic_if.c \
1011961a14aSEmmanuel Vadot	lkpi_iic_if.h \
10215fe2513SKonstantin Belousov	pci_if.h \
10315fe2513SKonstantin Belousov	pci_iov_if.h \
104de27805fSKonstantin Belousov	pcib_if.h \
10515fe2513SKonstantin Belousov	vnode_if.h \
10615fe2513SKonstantin Belousov	usb_if.h \
10715fe2513SKonstantin Belousov	opt_usb.h \
10815fe2513SKonstantin Belousov	opt_stack.h
10915fe2513SKonstantin Belousov
110514fb387SBjoern A. ZeebLINUXKPI_INCLUDES+= \
111f8bad561SBjoern A. Zeeb	-I${SYSDIR}/compat/linuxkpi/common/include \
11241283b45SBjoern A. Zeeb	-I${SYSDIR}/compat/linuxkpi/dummy/include \
11341283b45SBjoern A. Zeeb	-include ${SYSDIR}/compat/linuxkpi/common/include/linux/kconfig.h
114514fb387SBjoern A. Zeeb
115daaea395SWarner LoshCFLAGS+=	${WERROR}
11648aea4daSRuslan ErmilovCFLAGS+=	-D_KERNEL
1178fe644b2SMike SmithCFLAGS+=	-DKLD_MODULE
118f4b36404SMatt Macy.if defined(MODULE_TIED)
119f4b36404SMatt MacyCFLAGS+=	-DKLD_TIED
120f4b36404SMatt Macy.endif
1214a2d2630SBruce Evans
122021886ffSBruce Evans# Don't use any standard or source-relative include directories.
12306d6e4fcSTom RhodesNOSTDINC=	-nostdinc
124da3bcc92SAlexander KabaevCFLAGS:=	${CFLAGS:N-I*} ${NOSTDINC} ${INCLMAGIC} ${CFLAGS:M-I*}
1258fa793d0SBrian Feldman.if defined(KERNBUILDDIR)
1260c063641SGleb SmirnoffCFLAGS+=	-DHAVE_KERNEL_OPTION_HEADERS -include ${KERNBUILDDIR}/opt_global.h
1270c4c5948SKyle Evans.else
1280c4c5948SKyle EvansSRCS+=		opt_global.h
1290c4c5948SKyle EvansCFLAGS+=	-include ${.OBJDIR}/opt_global.h
1308fa793d0SBrian Feldman.endif
131021886ffSBruce Evans
1321f089616SRuslan Ermilov# Add -I paths for system headers.  Individual module makefiles don't
133021886ffSBruce Evans# need any -I paths for this.  Similar defaults for .PATH can't be
134021886ffSBruce Evans# set because there are no standard paths for non-headers.
1357bf272a6SMatt MacyCFLAGS+=	-I. -I${SYSDIR} -I${SYSDIR}/contrib/ck/include
1364a2d2630SBruce Evans
13722ba0b2fSWarner LoshCFLAGS.gcc+=	-finline-limit=${INLINE_LIMIT}
138cc4a90c4SGleb SmirnoffCFLAGS.gcc+=	-fms-extensions
13922ba0b2fSWarner LoshCFLAGS.gcc+= --param inline-unit-growth=100
14022ba0b2fSWarner LoshCFLAGS.gcc+= --param large-function-growth=1000
1412a51529fSPeter Wemm
1420b3178a4SMike Smith# Disallow common variables, and if we end up with commons from
1430b3178a4SMike Smith# somewhere unexpected, allocate storage for them in the module itself.
1448eb1a0ceSKyle Evans#
1458eb1a0ceSKyle Evans# -fno-common is the default for src builds, but this should be left in place
1468eb1a0ceSKyle Evans# until at least we catch up to GCC10/LLVM11 or otherwise enable -fno-common
1478eb1a0ceSKyle Evans# in <bsd.sys.mk> instead.  For now, we will have duplicate -fno-common in
1488eb1a0ceSKyle Evans# CFLAGS for in-tree module builds as they will also pick it up from
1498eb1a0ceSKyle Evans# share/mk/src.sys.mk, but the following is important for out-of-tree modules
1508eb1a0ceSKyle Evans# (e.g. ports).
1510b3178a4SMike SmithCFLAGS+=	-fno-common
1520817c8dcSDimitry Andric.if ${LINKER_TYPE} != "lld" || ${LINKER_VERSION} < 140000
1530817c8dcSDimitry Andric# lld >= 14 warns that -d is deprecated, and will be removed.
1540817c8dcSDimitry AndricLDFLAGS+=	-d
1550817c8dcSDimitry Andric.endif
1560817c8dcSDimitry AndricLDFLAGS+=	-warn-common
1570b3178a4SMike Smith
158738cf81bSBryan Drewery.if defined(LINKER_FEATURES) && ${LINKER_FEATURES:Mbuild-id}
159b5155bc9SMark JohnstonLDFLAGS+=	--build-id=sha1
16053e0bebaSEd Maste.endif
16153e0bebaSEd Maste
1629e4be63dSBruce EvansCFLAGS+=	${DEBUG_FLAGS}
163b926b6dbSMitchell Horne.if ${MACHINE_CPUARCH} == aarch64 || ${MACHINE_CPUARCH} == amd64 || \
164b926b6dbSMitchell Horne    ${MACHINE_CPUARCH} == riscv
1652565edc4SAndriy GaponCFLAGS+=	-fno-omit-frame-pointer -mno-omit-leaf-frame-pointer
16683c457dcSIan Dowse.endif
1679e4be63dSBruce Evans
168b1f8da38SBrandon Bergren.if ${MACHINE_CPUARCH} == "aarch64" || ${MACHINE_CPUARCH} == "riscv" || \
169c84617e8SDmitry Chagin    ${MACHINE_CPUARCH} == "powerpc" || ${MACHINE_CPUARCH} == "i386"
1708598392cSKonstantin BelousovCFLAGS+=	-fPIC
1718598392cSKonstantin Belousov.endif
1728598392cSKonstantin Belousov
17380e4ac29SDimitry Andric.if ${MACHINE_CPUARCH} == "aarch64"
17480e4ac29SDimitry Andric# https://bugs.freebsd.org/264094
17580e4ac29SDimitry Andric# lld >= 14 and recent GNU ld can relax adrp+add and adrp+ldr instructions,
17680e4ac29SDimitry Andric# which breaks VNET.
17780e4ac29SDimitry AndricLDFLAGS+=	--no-relax
17880e4ac29SDimitry Andric.endif
17980e4ac29SDimitry Andric
180b54c389fSIan Lepore# Temporary workaround for PR 196407, which contains the fascinating details.
181c6f7b900SIan Lepore# Don't allow clang to use fpu instructions or registers in kernel modules.
182b54c389fSIan Lepore.if ${MACHINE_CPUARCH} == arm
18367e3484fSDimitry AndricCFLAGS.clang+=	-mno-movt
184c6f7b900SIan LeporeCFLAGS.clang+=	-mfpu=none
1851b23f9b9SAndrew TurnerCFLAGS+=	-funwind-tables
186b54c389fSIan Lepore.endif
187b54c389fSIan Lepore
1883a0d123eSWarner Losh.if ${MACHINE_CPUARCH} == powerpc
18955a5dcc8SPeter GrehanCFLAGS+=	-mlongcall -fno-omit-frame-pointer
190c16f7fabSBrandon Bergren.if ${LINKER_TYPE} == "lld"
191c16f7fabSBrandon Bergren# TOC optimization in LLD (9.0) currently breaks kernel modules, so disable it
192c16f7fabSBrandon BergrenLDFLAGS+=	--no-toc-optimize
193c16f7fabSBrandon Bergren.endif
194fded756eSPeter Grehan.endif
195fded756eSPeter Grehan
19669745d38SAlexander Leidinger.if defined(DEBUG) || defined(DEBUG_FLAGS)
19769745d38SAlexander LeidingerCTFFLAGS+=	-g
19869745d38SAlexander Leidinger.endif
19969745d38SAlexander Leidinger
2006aec1278SMax Laier.if defined(FIRMWS)
2015852ae2dSBryan Drewery${KMOD:S/$/.c/}: ${SYSDIR}/tools/fw_stub.awk
202528013d5SWarner Losh	${AWK} -f ${SYSDIR}/tools/fw_stub.awk ${FIRMWS} -m${KMOD} -c${KMOD:S/$/.c/g} \
2037bd6fde3SFlorent Thoumie	    ${FIRMWARE_LICENSE:C/.+/-l/}${FIRMWARE_LICENSE}
2046aec1278SMax Laier
2056aec1278SMax LaierSRCS+=	${KMOD:S/$/.c/}
2066aec1278SMax LaierCLEANFILES+=	${KMOD:S/$/.c/}
2076aec1278SMax Laier
2086aec1278SMax Laier.for _firmw in ${FIRMWS}
209a0953903SJohn Baldwin${_firmw:C/\:.*$/.fwo/:T}:	${_firmw:C/\:.*$//} ${SYSDIR}/kern/firmw.S
2106aec1278SMax Laier	@${ECHO} ${_firmw:C/\:.*$//} ${.ALLSRC:M*${_firmw:C/\:.*$//}}
211a0953903SJohn Baldwin	${CC:N${CCACHE_BIN}} -c -x assembler-with-cpp -DLOCORE 	\
212a0953903SJohn Baldwin	    ${CFLAGS} ${WERROR} 				\
2138a6ab0f7SJessica Clarke	    -DFIRMW_FILE=\""${.ALLSRC:M*${_firmw:C/\:.*$//}}"\"	\
214628a4156SJohn Baldwin	    -DFIRMW_SYMBOL="${_firmw:C/\:.*$//:C/[-.\/@]/_/g}"	\
215a0953903SJohn Baldwin	    ${SYSDIR}/kern/firmw.S -o ${.TARGET}
2166aec1278SMax Laier
217d9eed6f4SJohn BaldwinOBJS+=	${_firmw:C/\:.*$/.fwo/:T}
2186aec1278SMax Laier.endfor
2196aec1278SMax Laier.endif
2206aec1278SMax Laier
22135c4406dSWarner Losh# Conditionally include SRCS based on kernel config options.
22235c4406dSWarner Losh.for _o in ${KERN_OPTS}
22335c4406dSWarner LoshSRCS+=${SRCS.${_o}}
22435c4406dSWarner Losh.endfor
22535c4406dSWarner Losh
226639a2e50SGarrett WollmanOBJS+=	${SRCS:N*.h:R:S/$/.o/g}
227639a2e50SGarrett Wollman
228639a2e50SGarrett Wollman.if !defined(PROG)
22990f60fc9SMike SmithPROG=	${KMOD}.ko
23090f60fc9SMike Smith.endif
231639a2e50SGarrett Wollman
232e3709cfeSEd Maste.if !defined(DEBUG_FLAGS) || ${MK_SPLIT_KERNEL_DEBUG} == "no"
233f6611608SSheldon HearnFULLPROG=	${PROG}
234f6611608SSheldon Hearn.else
23505117b57SEd MasteFULLPROG=	${PROG}.full
2365852ae2dSBryan Drewery${PROG}: ${FULLPROG} ${PROG}.debug
23705117b57SEd Maste	${OBJCOPY} --strip-debug --add-gnu-debuglink=${PROG}.debug \
23898b45a8fSRuslan Ermilov	    ${FULLPROG} ${.TARGET}
2395852ae2dSBryan Drewery${PROG}.debug: ${FULLPROG}
24098b45a8fSRuslan Ermilov	${OBJCOPY} --only-keep-debug ${FULLPROG} ${.TARGET}
241f6611608SSheldon Hearn.endif
242f6611608SSheldon Hearn
2433a0d123eSWarner Losh.if ${__KLD_SHARED} == yes
2445852ae2dSBryan Drewery${FULLPROG}: ${KMOD}.kld
24582dd4542SDimitry Andric	${LD} -m ${LD_EMULATION} -Bshareable -znotext -znorelro ${_LDFLAGS} \
24659642947SEd Maste	    -o ${.TARGET} ${KMOD}.kld
247817b59bfSRuslan Ermilov.if !defined(DEBUG_FLAGS)
248817b59bfSRuslan Ermilov	${OBJCOPY} --strip-debug ${.TARGET}
2494c293d35SRuslan Ermilov.endif
2504d6bcc83SPeter Wemm.endif
25109be11baSPeter Wemm
25293f03548SMike SmithEXPORT_SYMS?=	NO
25393f03548SMike Smith.if ${EXPORT_SYMS} != YES
2541f089616SRuslan ErmilovCLEANFILES+=	export_syms
2550b3178a4SMike Smith.endif
2560b3178a4SMike Smith
2572754373fSAndrew Turner.if exists(${SYSDIR}/conf/ldscript.kmod.${MACHINE})
2582754373fSAndrew TurnerLDSCRIPT_FLAGS?= -T ${SYSDIR}/conf/ldscript.kmod.${MACHINE}
259d6793db2SMark Johnston.endif
260d6793db2SMark Johnston
2613a0d123eSWarner Losh.if ${__KLD_SHARED} == yes
262e8dca53aSWarner Losh${KMOD}.kld: ${OBJS} ${BLOB_OBJS}
2634d6bcc83SPeter Wemm.else
264e8dca53aSWarner Losh${FULLPROG}: ${OBJS} ${BLOB_OBJS}
2654d6bcc83SPeter Wemm.endif
2660817c8dcSDimitry Andric	${LD} -m ${LD_EMULATION} ${_LDFLAGS} ${LDSCRIPT_FLAGS} -r \
267e8dca53aSWarner Losh	    -o ${.TARGET} ${OBJS} ${BLOB_OBJS}
268724afafaSWarner Losh.if ${MK_CTF} != "no"
269e8dca53aSWarner Losh	${CTFMERGE} ${CTFFLAGS} -o ${.TARGET} ${OBJS} ${BLOB_OBJS}
270cd49c2e8SMax Khon.endif
2710b3178a4SMike Smith.if defined(EXPORT_SYMS)
27293f03548SMike Smith.if ${EXPORT_SYMS} != YES
2730b3178a4SMike Smith.if ${EXPORT_SYMS} == NO
2748b3ae668SDag-Erling Smørgrav	:> export_syms
2750b3178a4SMike Smith.elif !exists(${.CURDIR}/${EXPORT_SYMS})
276d1e44bc9SJessica Clarke	printf '%s' "${EXPORT_SYMS:@s@$s${.newline}@}" > export_syms
2770b3178a4SMike Smith.else
2781f089616SRuslan Ermilov	grep -v '^#' < ${EXPORT_SYMS} > export_syms
2790b3178a4SMike Smith.endif
28011f1d284SMark Johnston	${AWK} -f ${SYSDIR}/conf/kmod_syms.awk ${.TARGET} \
281dd4d206cSSimon J. Gerraty	    export_syms | ${XARGS} ${XARGS_J} % ${OBJCOPY} % ${.TARGET}
28293f03548SMike Smith.endif
283b8283138SEd Maste.endif # defined(EXPORT_SYMS)
284dc6a41b9SJonathan T. Looney.if defined(PREFIX_SYMS)
285dc6a41b9SJonathan T. Looney	${AWK} -v prefix=${PREFIX_SYMS} -f ${SYSDIR}/conf/kmod_syms_prefix.awk \
286dd4d206cSSimon J. Gerraty	    ${.TARGET} /dev/null | ${XARGS} ${XARGS_J} % ${OBJCOPY} % ${.TARGET}
287dc6a41b9SJonathan T. Looney.endif
2883a0d123eSWarner Losh.if !defined(DEBUG_FLAGS) && ${__KLD_SHARED} == no
2894d6bcc83SPeter Wemm	${OBJCOPY} --strip-debug ${.TARGET}
2904d6bcc83SPeter Wemm.endif
2910b3178a4SMike Smith
292528013d5SWarner Losh_ILINKS=machine
293544de89dSJohn Baldwin.if ${MACHINE_CPUARCH} == "i386" || ${MACHINE_CPUARCH} == "amd64"
294544de89dSJohn Baldwin_ILINKS+=x86
295544de89dSJohn Baldwin.endif
296d07600c5SBrooks Davis.if ${MACHINE_CPUARCH} == "amd64"
297d07600c5SBrooks Davis_ILINKS+=i386
298d07600c5SBrooks Davis.endif
29931a01497SSean BrunoCLEANFILES+=${_ILINKS}
300639a2e50SGarrett Wollman
3011bd4272fSBryan Dreweryall: ${PROG}
3024a2d2630SBruce Evans
303db00e6c5SDavid E. O'Brienbeforedepend: ${_ILINKS}
30468d96923SBryan Drewerybeforebuild: ${_ILINKS}
305985f41afSWarner Losh
306f3cc9575SWarner Losh# Ensure that the links exist without depending on it when it exists which
307f3cc9575SWarner Losh# causes all the modules to be rebuilt when the directory pointed to changes.
3087114b176SMark Johnston# Ensure that debug info references the path in the source tree.
309f3cc9575SWarner Losh.for _link in ${_ILINKS}
310f3cc9575SWarner Losh.if !exists(${.OBJDIR}/${_link})
3113063d216SBryan DreweryOBJS_DEPEND_GUESS+=	${_link}
312f3cc9575SWarner Losh.endif
3137114b176SMark Johnston.if ${_link} == "machine"
3147114b176SMark JohnstonCFLAGS+= -fdebug-prefix-map=./machine=${SYSDIR}/${MACHINE}/include
3157114b176SMark Johnston.else
3167114b176SMark JohnstonCFLAGS+= -fdebug-prefix-map=./${_link}=${SYSDIR}/${_link}/include
3177114b176SMark Johnston.endif
318f3cc9575SWarner Losh.endfor
3194a2d2630SBruce Evans
3207750ad47SMarcel Moolenaar.NOPATH: ${_ILINKS}
3213c4368b2SMark Johnston
3224a2d2630SBruce Evans${_ILINKS}:
3239a8631b3SWarner Losh	@case ${.TARGET} in \
324df3a103dSWarner Losh	machine) \
325df3a103dSWarner Losh		path=${SYSDIR}/${MACHINE}/include ;; \
326544de89dSJohn Baldwin	*) \
3273c4368b2SMark Johnston		path=${SYSDIR}/${.TARGET:T}/include ;; \
32808a04a89SPeter Wemm	esac ; \
329bcb7f57aSMateusz Guzik	path=`realpath $$path`; \
3303c4368b2SMark Johnston	${ECHO} ${.TARGET:T} "->" $$path ; \
331454ce383SAlex Richardson	ln -fns $$path ${.TARGET:T}
3324a2d2630SBruce Evans
33374465532SRuslan ErmilovCLEANFILES+= ${PROG} ${KMOD}.kld ${OBJS}
334361186e5SMaxime Henrion
335e3709cfeSEd Maste.if defined(DEBUG_FLAGS) && ${MK_SPLIT_KERNEL_DEBUG} != "no"
33605117b57SEd MasteCLEANFILES+= ${FULLPROG} ${PROG}.debug
337361186e5SMaxime Henrion.endif
338639a2e50SGarrett Wollman
339639a2e50SGarrett Wollman.if !target(install)
340639a2e50SGarrett Wollman
341ea87b3cbSDavid E. O'Brien_INSTALLFLAGS:=	${INSTALLFLAGS}
342e902c1bbSTim Vanderhoek.for ie in ${INSTALLFLAGS_EDIT}
343e902c1bbSTim Vanderhoek_INSTALLFLAGS:=	${_INSTALLFLAGS${ie}}
344e902c1bbSTim Vanderhoek.endfor
345e902c1bbSTim Vanderhoek
3462b06260dSRuslan Ermilov.if !target(realinstall)
34705117b57SEd MasteKERN_DEBUGDIR?=	${DEBUGDIR}
3482b06260dSRuslan Ermilovrealinstall: _kmodinstall
3492b06260dSRuslan Ermilov.ORDER: beforeinstall _kmodinstall
35049773547SBryan Drewery_kmodinstall: .PHONY
3511c6eb1b2SBaptiste Daroussin	${INSTALL} -T release -o ${KMODOWN} -g ${KMODGRP} -m ${KMODMODE} \
3520ed70e42SBryan Drewery	    ${_INSTALLFLAGS} ${PROG} ${DESTDIR}${KMODDIR}/
353cd95660fSDavid E. O'Brien.if defined(DEBUG_FLAGS) && !defined(INSTALL_NODEBUG) && ${MK_KERNEL_SYMBOLS} != "no"
354697b271dSEd Maste	${INSTALL} -T dbg -o ${KMODOWN} -g ${KMODGRP} -m ${KMODMODE} \
3550ed70e42SBryan Drewery	    ${_INSTALLFLAGS} ${PROG}.debug ${DESTDIR}${KERN_DEBUGDIR}${KMODDIR}/
356d24864f7SRuslan Ermilov.endif
3572b06260dSRuslan Ermilov
3582b06260dSRuslan Ermilov.include <bsd.links.mk>
3592b06260dSRuslan Ermilov
3603812c653SJessica Clarke.if !defined(NO_XREF)
3612b06260dSRuslan Ermilovafterinstall: _kldxref
3622b06260dSRuslan Ermilov.ORDER: realinstall _kldxref
3632b06260dSRuslan Ermilov.ORDER: _installlinks _kldxref
36449773547SBryan Drewery_kldxref: .PHONY
365ff7c12c1SJessica Clarke	${KLDXREF_CMD} ${DESTDIR}${KMODDIR}
366ff7c12c1SJessica Clarke.if defined(NO_ROOT) && defined(METALOG)
367ff7c12c1SJessica Clarke	echo ".${DISTBASE}${KMODDIR}/linker.hints type=file mode=0644 uname=root gname=wheel" | \
368ff7c12c1SJessica Clarke	    cat -l >> ${METALOG}
369ff7c12c1SJessica Clarke.endif
370505222d3SPeter Wemm.endif
37122a8b405SMaxime Henrion.endif # !target(realinstall)
372639a2e50SGarrett Wollman
37322a8b405SMaxime Henrion.endif # !target(install)
374639a2e50SGarrett Wollman
375639a2e50SGarrett Wollman.if !target(load)
37649773547SBryan Dreweryload: ${PROG} .PHONY
3771f089616SRuslan Ermilov	${KMODLOAD} -v ${.OBJDIR}/${PROG}
378639a2e50SGarrett Wollman.endif
379639a2e50SGarrett Wollman
380639a2e50SGarrett Wollman.if !target(unload)
38149773547SBryan Dreweryunload: .PHONY
38293bffaf2SDag-Erling Smørgrav	if ${KMODISLOADED} ${PROG} ; then ${KMODUNLOAD} -v ${PROG} ; fi
38393bffaf2SDag-Erling Smørgrav.endif
38493bffaf2SDag-Erling Smørgrav
38593bffaf2SDag-Erling Smørgrav.if !target(reload)
38649773547SBryan Dreweryreload: unload load .PHONY
38708a04a89SPeter Wemm.endif
3881aa57be3SGarrett Wollman
3896b1bb559SWarner Losh.if defined(KERNBUILDDIR)
3906b1bb559SWarner Losh.PATH: ${KERNBUILDDIR}
3916b1bb559SWarner LoshCFLAGS+=	-I${KERNBUILDDIR}
3926b1bb559SWarner Losh.for _src in ${SRCS:Mopt_*.h}
3936b1bb559SWarner LoshCLEANFILES+=	${_src}
3946b1bb559SWarner Losh.if !target(${_src})
3956b1bb559SWarner Losh${_src}:
39640244cc2SDavid E. O'Brien	ln -sf ${KERNBUILDDIR}/${_src} ${.TARGET}
3976b1bb559SWarner Losh.endif
3986b1bb559SWarner Losh.endfor
3996b1bb559SWarner Losh.else
4004fea6704SBruce Evans.for _src in ${SRCS:Mopt_*.h}
4014fea6704SBruce EvansCLEANFILES+=	${_src}
4024fea6704SBruce Evans.if !target(${_src})
4034fea6704SBruce Evans${_src}:
4048b3ae668SDag-Erling Smørgrav	:> ${.TARGET}
4054fea6704SBruce Evans.endif
4064fea6704SBruce Evans.endfor
4076b1bb559SWarner Losh.endif
4084fea6704SBruce Evans
409*6e3875ebSKa Ho NgKASAN_ENABLED=	${KERN_OPTS:MKASAN}
410*6e3875ebSKa Ho NgKCSAN_ENABLED=	${KERN_OPTS:MKCSAN}
411*6e3875ebSKa Ho NgKMSAN_ENABLED=	${KERN_OPTS:MKMSAN}
412*6e3875ebSKa Ho NgKUBSAN_ENABLED=	${KERN_OPTS:MKUBSAN}
4134ea56599SAndrew Turner
4145426539cSMatt Macy# Add the gcov flags
4155426539cSMatt MacyCFLAGS+=	${GCOV_CFLAGS}
4165426539cSMatt Macy
4173fd1e05bSAlexander Leidinger# Respect configuration-specific C flags.
418256054fdSBrooks DavisCFLAGS+=	${ARCH_FLAGS} ${CONF_CFLAGS}
41962769330SAlexander Leidinger
420a7d30e5fSDavid E. O'Brien.if !empty(SRCS:Mvnode_if.c)
42178b2c868SBrian FeldmanCLEANFILES+=	vnode_if.c
422528013d5SWarner Loshvnode_if.c: ${SYSDIR}/tools/vnode_if.awk ${SYSDIR}/kern/vnode_if.src
423528013d5SWarner Losh	${AWK} -f ${SYSDIR}/tools/vnode_if.awk ${SYSDIR}/kern/vnode_if.src -c
4244fea6704SBruce Evans.endif
42578b2c868SBrian Feldman
426a7d30e5fSDavid E. O'Brien.if !empty(SRCS:Mvnode_if.h)
4279f73f0e8SRuslan ErmilovCLEANFILES+=	vnode_if.h vnode_if_newproto.h vnode_if_typedef.h
428528013d5SWarner Loshvnode_if.h vnode_if_newproto.h vnode_if_typedef.h: ${SYSDIR}/tools/vnode_if.awk \
429528013d5SWarner Losh    ${SYSDIR}/kern/vnode_if.src
4309f73f0e8SRuslan Ermilovvnode_if.h: vnode_if_newproto.h vnode_if_typedef.h
431528013d5SWarner Losh	${AWK} -f ${SYSDIR}/tools/vnode_if.awk ${SYSDIR}/kern/vnode_if.src -h
4329f73f0e8SRuslan Ermilovvnode_if_newproto.h:
433528013d5SWarner Losh	${AWK} -f ${SYSDIR}/tools/vnode_if.awk ${SYSDIR}/kern/vnode_if.src -p
4349f73f0e8SRuslan Ermilovvnode_if_typedef.h:
435528013d5SWarner Losh	${AWK} -f ${SYSDIR}/tools/vnode_if.awk ${SYSDIR}/kern/vnode_if.src -q
43678b2c868SBrian Feldman.endif
4371aa57be3SGarrett Wollman
43812f05b84SWarner Losh# Build _if.[ch] from _if.m, and clean them when we're done.
439ec4047adSBryan Drewery# __MPATH defined in config.mk
4402cd2ff0eSJohn Baldwin_MFILES=${__MPATH:T:O}
4412cd2ff0eSJohn Baldwin_MPATH=${__MPATH:H:O:u}
44212f05b84SWarner Losh.PATH.m: ${_MPATH}
443160c44feSWarner Losh.for _i in ${SRCS:M*_if.[ch]}
4442cd2ff0eSJohn Baldwin_MATCH=M${_i:R:S/$/.m/}
4452cd2ff0eSJohn Baldwin_MATCHES=${_MFILES:${_MATCH}}
4462cd2ff0eSJohn Baldwin.if !empty(_MATCHES)
4472cd2ff0eSJohn BaldwinCLEANFILES+=	${_i}
4482cd2ff0eSJohn Baldwin.endif
449160c44feSWarner Losh.endfor # _i
4505852ae2dSBryan Drewery.m.c:	${SYSDIR}/tools/makeobjops.awk
45112f05b84SWarner Losh	${AWK} -f ${SYSDIR}/tools/makeobjops.awk ${.IMPSRC} -c
45212f05b84SWarner Losh
4535852ae2dSBryan Drewery.m.h:	${SYSDIR}/tools/makeobjops.awk
45412f05b84SWarner Losh	${AWK} -f ${SYSDIR}/tools/makeobjops.awk ${.IMPSRC} -h
45512f05b84SWarner Losh
4564e91eaf2SWarner Losh.for _i in mii pccard
457a7d30e5fSDavid E. O'Brien.if !empty(SRCS:M${_i}devs.h)
458dba6dd17SWarner LoshCLEANFILES+=	${_i}devs.h
4595852ae2dSBryan Drewery${_i}devs.h: ${SYSDIR}/tools/${_i}devs2h.awk ${SYSDIR}/dev/${_i}/${_i}devs
460528013d5SWarner Losh	${AWK} -f ${SYSDIR}/tools/${_i}devs2h.awk ${SYSDIR}/dev/${_i}/${_i}devs
4612d3ce713SDavid E. O'Brien.endif
462dba6dd17SWarner Losh.endfor # _i
4632d3ce713SDavid E. O'Brien
464e83ce340SAdrian Chadd.if !empty(SRCS:Mbhnd_nvram_map.h)
465e83ce340SAdrian ChaddCLEANFILES+=	bhnd_nvram_map.h
466e83ce340SAdrian Chaddbhnd_nvram_map.h: ${SYSDIR}/dev/bhnd/tools/nvram_map_gen.awk \
467e83ce340SAdrian Chadd    ${SYSDIR}/dev/bhnd/tools/nvram_map_gen.sh \
4685852ae2dSBryan Drewery    ${SYSDIR}/dev/bhnd/nvram/nvram_map
4695852ae2dSBryan Drewerybhnd_nvram_map.h:
47016fba18fSDimitry Andric	sh ${SYSDIR}/dev/bhnd/tools/nvram_map_gen.sh \
471e83ce340SAdrian Chadd	    ${SYSDIR}/dev/bhnd/nvram/nvram_map -h
472e83ce340SAdrian Chadd.endif
473e83ce340SAdrian Chadd
474e83ce340SAdrian Chadd.if !empty(SRCS:Mbhnd_nvram_map_data.h)
475e83ce340SAdrian ChaddCLEANFILES+=	bhnd_nvram_map_data.h
476e83ce340SAdrian Chaddbhnd_nvram_map_data.h: ${SYSDIR}/dev/bhnd/tools/nvram_map_gen.awk \
477e83ce340SAdrian Chadd    ${SYSDIR}/dev/bhnd/tools/nvram_map_gen.sh \
4785852ae2dSBryan Drewery    ${SYSDIR}/dev/bhnd/nvram/nvram_map
4795852ae2dSBryan Drewerybhnd_nvram_map_data.h:
48016fba18fSDimitry Andric	sh ${SYSDIR}/dev/bhnd/tools/nvram_map_gen.sh \
481e83ce340SAdrian Chadd	    ${SYSDIR}/dev/bhnd/nvram/nvram_map -d
482e83ce340SAdrian Chadd.endif
483e83ce340SAdrian Chadd
484a7d30e5fSDavid E. O'Brien.if !empty(SRCS:Musbdevs.h)
4854e91eaf2SWarner LoshCLEANFILES+=	usbdevs.h
4865852ae2dSBryan Dreweryusbdevs.h: ${SYSDIR}/tools/usbdevs2h.awk ${SYSDIR}/dev/usb/usbdevs
487528013d5SWarner Losh	${AWK} -f ${SYSDIR}/tools/usbdevs2h.awk ${SYSDIR}/dev/usb/usbdevs -h
4884e91eaf2SWarner Losh.endif
4894e91eaf2SWarner Losh
490a7d30e5fSDavid E. O'Brien.if !empty(SRCS:Musbdevs_data.h)
4914e91eaf2SWarner LoshCLEANFILES+=	usbdevs_data.h
4925852ae2dSBryan Dreweryusbdevs_data.h: ${SYSDIR}/tools/usbdevs2h.awk ${SYSDIR}/dev/usb/usbdevs
493528013d5SWarner Losh	${AWK} -f ${SYSDIR}/tools/usbdevs2h.awk ${SYSDIR}/dev/usb/usbdevs -d
4944e91eaf2SWarner Losh.endif
4954e91eaf2SWarner Losh
49667ca7330SBjoern A. Zeeb.if !empty(SRCS:Msdiodevs.h)
49767ca7330SBjoern A. ZeebCLEANFILES+=	sdiodevs.h
49867ca7330SBjoern A. Zeebsdiodevs.h: ${SYSDIR}/tools/sdiodevs2h.awk ${SYSDIR}/dev/sdio/sdiodevs
49967ca7330SBjoern A. Zeeb	${AWK} -f ${SYSDIR}/tools/sdiodevs2h.awk ${SYSDIR}/dev/sdio/sdiodevs -h
50067ca7330SBjoern A. Zeeb.endif
50167ca7330SBjoern A. Zeeb
50267ca7330SBjoern A. Zeeb.if !empty(SRCS:Msdiodevs_data.h)
50367ca7330SBjoern A. ZeebCLEANFILES+=	sdiodevs_data.h
50467ca7330SBjoern A. Zeebsdiodevs_data.h: ${SYSDIR}/tools/sdiodevs2h.awk ${SYSDIR}/dev/sdio/sdiodevs
50567ca7330SBjoern A. Zeeb	${AWK} -f ${SYSDIR}/tools/sdiodevs2h.awk ${SYSDIR}/dev/sdio/sdiodevs -d
50667ca7330SBjoern A. Zeeb.endif
50767ca7330SBjoern A. Zeeb
508a7d30e5fSDavid E. O'Brien.if !empty(SRCS:Macpi_quirks.h)
509684c1c1dSNate LawsonCLEANFILES+=	acpi_quirks.h
5105852ae2dSBryan Dreweryacpi_quirks.h: ${SYSDIR}/tools/acpi_quirks2h.awk ${SYSDIR}/dev/acpica/acpi_quirks
511528013d5SWarner Losh	${AWK} -f ${SYSDIR}/tools/acpi_quirks2h.awk ${SYSDIR}/dev/acpica/acpi_quirks
512684c1c1dSNate Lawson.endif
513684c1c1dSNate Lawson
514fc2a8776SEd Maste.if !empty(SRCS:Massym.inc) || !empty(DPSRCS:Massym.inc)
5159d2b4c4fSBryan DreweryCLEANFILES+=	assym.inc genassym.o
516ccca101fSBryan DreweryDEPENDOBJS+=	genassym.o
517ccca101fSBryan DreweryDPSRCS+=	offset.inc
518ccca101fSBryan Drewery.endif
51982c98d01SBryan Drewery.if defined(MODULE_TIED)
52082c98d01SBryan DreweryDPSRCS+=	offset.inc
52182c98d01SBryan Drewery.endif
522ccca101fSBryan Drewery.if !empty(SRCS:Moffset.inc) || !empty(DPSRCS:Moffset.inc)
523ccca101fSBryan DreweryCLEANFILES+=	offset.inc genoffset.o
524ccca101fSBryan DreweryDEPENDOBJS+=	genoffset.o
525ccca101fSBryan Drewery.endif
526fc2a8776SEd Masteassym.inc: genassym.o
5278bedbb4dSMatt Macyoffset.inc: genoffset.o
528fc2a8776SEd Masteassym.inc: ${SYSDIR}/kern/genassym.sh
529528013d5SWarner Losh	sh ${SYSDIR}/kern/genassym.sh genassym.o > ${.TARGET}
530d69d8a46SBryan Drewerygenassym.o: ${SYSDIR}/${MACHINE}/${MACHINE}/genassym.c offset.inc
5315852ae2dSBryan Drewerygenassym.o: ${SRCS:Mopt_*.h}
53212a6257aSAndrew Turner	${CC} -c ${NOSAN_CFLAGS:N-flto*:N-fno-common} -fcommon \
5337dd824c3SEd Maste	    ${SYSDIR}/${MACHINE}/${MACHINE}/genassym.c
5348bedbb4dSMatt Macyoffset.inc: ${SYSDIR}/kern/genoffset.sh genoffset.o
5358bedbb4dSMatt Macy	sh ${SYSDIR}/kern/genoffset.sh genoffset.o > ${.TARGET}
536d69d8a46SBryan Drewerygenoffset.o: ${SYSDIR}/kern/genoffset.c
5378bedbb4dSMatt Macygenoffset.o: ${SRCS:Mopt_*.h}
53812a6257aSAndrew Turner	${CC} -c ${NOSAN_CFLAGS:N-flto*:N-fno-common} -fcommon \
539d69d8a46SBryan Drewery	    ${SYSDIR}/kern/genoffset.c
540705d63cdSIan Dowse
541de1a3bd9SBryan DreweryCLEANDEPENDFILES+=	${_ILINKS}
54274465532SRuslan Ermilov# .depend needs include links so we remove them only together.
54374465532SRuslan Ermilovcleanilinks:
54474465532SRuslan Ermilov	rm -f ${_ILINKS}
54574465532SRuslan Ermilov
546d7beff08SBryan DreweryOBJS_DEPEND_GUESS+= ${SRCS:M*.h}
547817f7baaSBryan Drewery.if defined(KERNBUILDDIR)
548817f7baaSBryan DreweryOBJS_DEPEND_GUESS+= opt_global.h
549817f7baaSBryan Drewery.endif
55088fe1f7aSBryan Drewery
5519e5787d2SMatt MacyZINCDIR=${SYSDIR}/contrib/openzfs/include
5529e5787d2SMatt MacyOPENZFS_CFLAGS=     \
5539e5787d2SMatt Macy	-D_SYS_VMEM_H_  \
5549e5787d2SMatt Macy	-D__KERNEL__ \
5559e5787d2SMatt Macy	-nostdinc \
5569e5787d2SMatt Macy	-DSMP \
5579e5787d2SMatt Macy	-I${ZINCDIR}  \
5589e5787d2SMatt Macy	-I${ZINCDIR}/os/freebsd \
5599e5787d2SMatt Macy	-I${ZINCDIR}/os/freebsd/spl \
5609e5787d2SMatt Macy	-I${ZINCDIR}/os/freebsd/zfs \
5619e5787d2SMatt Macy	-I${SYSDIR}/cddl/compat/opensolaris \
5629e5787d2SMatt Macy	-I${SYSDIR}/cddl/contrib/opensolaris/uts/common \
5639e5787d2SMatt Macy	-include ${ZINCDIR}/os/freebsd/spl/sys/ccompile.h
5649e5787d2SMatt Macy
56588fe1f7aSBryan Drewery.include <bsd.dep.mk>
566aae63957SBryan Drewery.include <bsd.clang-analyze.mk>
567afba897fSBruce Evans.include <bsd.obj.mk>
568ddc474a8SRuslan Ermilov.include "kern.mk"
569