xref: /freebsd/sys/conf/kmod.mk (revision a8976aec4535acfb77e68e5b6551348dd68812d6)
1639a2e50SGarrett Wollman#	From: @(#)bsd.prog.mk	5.26 (Berkeley) 6/25/91
27f3dea24SPeter Wemm# $FreeBSD$
353996fadSWolfram Schneider#
41f089616SRuslan Ermilov# The include file <bsd.kmod.mk> handles building and installing loadable
51f089616SRuslan Ermilov# kernel modules.
653996fadSWolfram Schneider#
753996fadSWolfram Schneider#
853996fadSWolfram Schneider# +++ variables +++
953996fadSWolfram Schneider#
1053996fadSWolfram Schneider# CLEANFILES	Additional files to remove for the clean and cleandir targets.
1153996fadSWolfram Schneider#
121f089616SRuslan Ermilov# EXPORT_SYMS	A list of symbols that should be exported from the module,
131f089616SRuslan Ermilov#		or the name of a file containing a list of symbols, or YES
141f089616SRuslan Ermilov#		to export all symbols.  If not defined, no symbols are
151f089616SRuslan Ermilov#		exported.
161f089616SRuslan Ermilov#
17fe3cac87SNick Hibma# KMOD		The name of the kernel module to build.
1853996fadSWolfram Schneider#
193bdfa9e5SDavid E. O'Brien# KMODDIR	Base path for kernel modules (see kld(4)). [/boot/kernel]
2053996fadSWolfram Schneider#
211f089616SRuslan Ermilov# KMODOWN	Module file owner. [${BINOWN}]
2253996fadSWolfram Schneider#
231f089616SRuslan Ermilov# KMODGRP	Module file group. [${BINGRP}]
2453996fadSWolfram Schneider#
251f089616SRuslan Ermilov# KMODMODE	Module file mode. [${BINMODE}]
2653996fadSWolfram Schneider#
27fe3cac87SNick Hibma# KMODLOAD	Command to load a kernel module [/sbin/kldload]
287bc830bbSWolfram Schneider#
29fe3cac87SNick Hibma# KMODUNLOAD	Command to unload a kernel module [/sbin/kldunload]
307bc830bbSWolfram Schneider#
3193bffaf2SDag-Erling Smørgrav# KMODISLOADED	Command to check whether a kernel module is
3293bffaf2SDag-Erling Smørgrav#		loaded [/sbin/kldstat -q -n]
3393bffaf2SDag-Erling Smørgrav#
341f089616SRuslan Ermilov# PROG		The name of the kernel module to build.
351f089616SRuslan Ermilov#		If not supplied, ${KMOD}.ko is used.
361f089616SRuslan Ermilov#
371f089616SRuslan Ermilov# SRCS		List of source files.
381f089616SRuslan Ermilov#
396aec1278SMax Laier# FIRMWS	List of firmware images in format filename:shortname:version
406aec1278SMax Laier#
4141e80f2aSFlorent Thoumie# FIRMWARE_LICENSE
427bd6fde3SFlorent Thoumie#		Set to the name of the license the user has to agree on in
437bd6fde3SFlorent Thoumie#		order to use this firmware. See /usr/share/doc/legal
447bd6fde3SFlorent Thoumie#
451f089616SRuslan Ermilov# DESTDIR	The tree where the module gets installed. [not set]
460b3178a4SMike Smith#
47489b536fSWarner Losh# KERNBUILDDIR
48489b536fSWarner Losh#		Set to the location of the kernel build directory where
49489b536fSWarner Losh#		the opt_*.h files, .o's and kernel winds up.
50489b536fSWarner Losh#
5153996fadSWolfram Schneider# +++ targets +++
5253996fadSWolfram Schneider#
5353996fadSWolfram Schneider# 	install:
5417d6c636SRuslan Ermilov#               install the kernel module; if the Makefile
5553996fadSWolfram Schneider#               does not itself define the target install, the targets
5653996fadSWolfram Schneider#               beforeinstall and afterinstall may also be used to cause
5753996fadSWolfram Schneider#               actions immediately before and after the install target
5853996fadSWolfram Schneider#		is executed.
5953996fadSWolfram Schneider#
6053996fadSWolfram Schneider# 	load:
611f089616SRuslan Ermilov#		Load a module.
6253996fadSWolfram Schneider#
6353996fadSWolfram Schneider# 	unload:
641f089616SRuslan Ermilov#		Unload a module.
6553996fadSWolfram Schneider#
6693bffaf2SDag-Erling Smørgrav#	reload:
6793bffaf2SDag-Erling Smørgrav#		Unload if loaded, then load.
6893bffaf2SDag-Erling Smørgrav#
69639a2e50SGarrett Wollman
70b7bbb709SDavid E. O'BrienAWK?=		awk
71fe3cac87SNick HibmaKMODLOAD?=	/sbin/kldload
72fe3cac87SNick HibmaKMODUNLOAD?=	/sbin/kldunload
7393bffaf2SDag-Erling SmørgravKMODISLOADED?=	/sbin/kldstat -q -n
74f6611608SSheldon HearnOBJCOPY?=	objcopy
757bc830bbSWolfram Schneider
769ab286b7SJohn Baldwin.include "kmod.opts.mk"
773cc5d6a4SWarner Losh.include <bsd.sysdir.mk>
785d4447e0SBryan Drewery
7912f05b84SWarner Losh.SUFFIXES: .out .o .c .cc .cxx .C .y .l .s .S .m
80639a2e50SGarrett Wollman
813a0d123eSWarner Losh# amd64 and mips use direct linking for kmod, all others use shared binaries
823a0d123eSWarner Losh.if ${MACHINE_CPUARCH} != amd64 && ${MACHINE_CPUARCH} != mips
833a0d123eSWarner Losh__KLD_SHARED=yes
843a0d123eSWarner Losh.else
853a0d123eSWarner Losh__KLD_SHARED=no
863a0d123eSWarner Losh.endif
873a0d123eSWarner Losh
88f165cb7cSRuslan Ermilov.if !empty(CFLAGS:M-O[23s]) && empty(CFLAGS:M-fno-strict-aliasing)
89db0cd7e1SDavid E. O'BrienCFLAGS+=	-fno-strict-aliasing
90db0cd7e1SDavid E. O'Brien.endif
9117c12f64SWarner Losh.if ${COMPILER_TYPE} == "gcc" && ${COMPILER_VERSION} < 50000
9217c12f64SWarner LoshWERROR?=	-Wno-error
9317c12f64SWarner Losh.else
945d3b2922SRuslan ErmilovWERROR?=	-Werror
9517c12f64SWarner Losh.endif
9617c12f64SWarner Losh
9715fe2513SKonstantin BelousovLINUXKPI_GENSRCS+= \
9815fe2513SKonstantin Belousov	bus_if.h \
9915fe2513SKonstantin Belousov	device_if.h \
10015fe2513SKonstantin Belousov	pci_if.h \
10115fe2513SKonstantin Belousov	pci_iov_if.h \
10215fe2513SKonstantin Belousov	vnode_if.h \
10315fe2513SKonstantin Belousov	usb_if.h \
10415fe2513SKonstantin Belousov	opt_usb.h \
10515fe2513SKonstantin Belousov	opt_stack.h
10615fe2513SKonstantin Belousov
107daaea395SWarner LoshCFLAGS+=	${WERROR}
10848aea4daSRuslan ErmilovCFLAGS+=	-D_KERNEL
1098fe644b2SMike SmithCFLAGS+=	-DKLD_MODULE
110f4b36404SMatt Macy.if defined(MODULE_TIED)
111f4b36404SMatt MacyCFLAGS+=	-DKLD_TIED
112f4b36404SMatt Macy.endif
1134a2d2630SBruce Evans
114021886ffSBruce Evans# Don't use any standard or source-relative include directories.
11506d6e4fcSTom RhodesNOSTDINC=	-nostdinc
116da3bcc92SAlexander KabaevCFLAGS:=	${CFLAGS:N-I*} ${NOSTDINC} ${INCLMAGIC} ${CFLAGS:M-I*}
1178fa793d0SBrian Feldman.if defined(KERNBUILDDIR)
1180c063641SGleb SmirnoffCFLAGS+=	-DHAVE_KERNEL_OPTION_HEADERS -include ${KERNBUILDDIR}/opt_global.h
1190c4c5948SKyle Evans.else
1200c4c5948SKyle EvansSRCS+=		opt_global.h
1210c4c5948SKyle EvansCFLAGS+=	-include ${.OBJDIR}/opt_global.h
1228fa793d0SBrian Feldman.endif
123021886ffSBruce Evans
1241f089616SRuslan Ermilov# Add -I paths for system headers.  Individual module makefiles don't
125021886ffSBruce Evans# need any -I paths for this.  Similar defaults for .PATH can't be
126021886ffSBruce Evans# set because there are no standard paths for non-headers.
1277bf272a6SMatt MacyCFLAGS+=	-I. -I${SYSDIR} -I${SYSDIR}/contrib/ck/include
1284a2d2630SBruce Evans
12922ba0b2fSWarner LoshCFLAGS.gcc+=	-finline-limit=${INLINE_LIMIT}
130cc4a90c4SGleb SmirnoffCFLAGS.gcc+=	-fms-extensions
13122ba0b2fSWarner LoshCFLAGS.gcc+= --param inline-unit-growth=100
13222ba0b2fSWarner LoshCFLAGS.gcc+= --param large-function-growth=1000
1332a51529fSPeter Wemm
1340b3178a4SMike Smith# Disallow common variables, and if we end up with commons from
1350b3178a4SMike Smith# somewhere unexpected, allocate storage for them in the module itself.
1368eb1a0ceSKyle Evans#
1378eb1a0ceSKyle Evans# -fno-common is the default for src builds, but this should be left in place
1388eb1a0ceSKyle Evans# until at least we catch up to GCC10/LLVM11 or otherwise enable -fno-common
1398eb1a0ceSKyle Evans# in <bsd.sys.mk> instead.  For now, we will have duplicate -fno-common in
1408eb1a0ceSKyle Evans# CFLAGS for in-tree module builds as they will also pick it up from
1418eb1a0ceSKyle Evans# share/mk/src.sys.mk, but the following is important for out-of-tree modules
1428eb1a0ceSKyle Evans# (e.g. ports).
1430b3178a4SMike SmithCFLAGS+=	-fno-common
1440b3178a4SMike SmithLDFLAGS+=	-d -warn-common
1450b3178a4SMike Smith
146738cf81bSBryan Drewery.if defined(LINKER_FEATURES) && ${LINKER_FEATURES:Mbuild-id}
147b5155bc9SMark JohnstonLDFLAGS+=	--build-id=sha1
14853e0bebaSEd Maste.endif
14953e0bebaSEd Maste
1509e4be63dSBruce EvansCFLAGS+=	${DEBUG_FLAGS}
1513a0d123eSWarner Losh.if ${MACHINE_CPUARCH} == amd64
1522565edc4SAndriy GaponCFLAGS+=	-fno-omit-frame-pointer -mno-omit-leaf-frame-pointer
15383c457dcSIan Dowse.endif
1549e4be63dSBruce Evans
155b1f8da38SBrandon Bergren.if ${MACHINE_CPUARCH} == "aarch64" || ${MACHINE_CPUARCH} == "riscv" || \
156b1f8da38SBrandon Bergren    ${MACHINE_CPUARCH} == "powerpc"
1578598392cSKonstantin BelousovCFLAGS+=	-fPIC
1588598392cSKonstantin Belousov.endif
1598598392cSKonstantin Belousov
160b54c389fSIan Lepore# Temporary workaround for PR 196407, which contains the fascinating details.
161c6f7b900SIan Lepore# Don't allow clang to use fpu instructions or registers in kernel modules.
162b54c389fSIan Lepore.if ${MACHINE_CPUARCH} == arm
16367e3484fSDimitry Andric.if ${COMPILER_VERSION} < 30800
164b54c389fSIan LeporeCFLAGS.clang+=	-mllvm -arm-use-movt=0
16567e3484fSDimitry Andric.else
16667e3484fSDimitry AndricCFLAGS.clang+=	-mno-movt
16767e3484fSDimitry Andric.endif
168c6f7b900SIan LeporeCFLAGS.clang+=	-mfpu=none
1691b23f9b9SAndrew TurnerCFLAGS+=	-funwind-tables
170b54c389fSIan Lepore.endif
171b54c389fSIan Lepore
1723a0d123eSWarner Losh.if ${MACHINE_CPUARCH} == powerpc
17355a5dcc8SPeter GrehanCFLAGS+=	-mlongcall -fno-omit-frame-pointer
174c16f7fabSBrandon Bergren.if ${LINKER_TYPE} == "lld"
175c16f7fabSBrandon Bergren# TOC optimization in LLD (9.0) currently breaks kernel modules, so disable it
176c16f7fabSBrandon BergrenLDFLAGS+=	--no-toc-optimize
177c16f7fabSBrandon Bergren.endif
178fded756eSPeter Grehan.endif
179fded756eSPeter Grehan
1803a0d123eSWarner Losh.if ${MACHINE_CPUARCH} == mips
1816f3c6327SNeel NatuCFLAGS+=	-G0 -fno-pic -mno-abicalls -mlong-calls
1826f3c6327SNeel Natu.endif
1836f3c6327SNeel Natu
18469745d38SAlexander Leidinger.if defined(DEBUG) || defined(DEBUG_FLAGS)
18569745d38SAlexander LeidingerCTFFLAGS+=	-g
18669745d38SAlexander Leidinger.endif
18769745d38SAlexander Leidinger
1886aec1278SMax Laier.if defined(FIRMWS)
1895852ae2dSBryan Drewery${KMOD:S/$/.c/}: ${SYSDIR}/tools/fw_stub.awk
190528013d5SWarner Losh	${AWK} -f ${SYSDIR}/tools/fw_stub.awk ${FIRMWS} -m${KMOD} -c${KMOD:S/$/.c/g} \
1917bd6fde3SFlorent Thoumie	    ${FIRMWARE_LICENSE:C/.+/-l/}${FIRMWARE_LICENSE}
1926aec1278SMax Laier
1936aec1278SMax LaierSRCS+=	${KMOD:S/$/.c/}
1946aec1278SMax LaierCLEANFILES+=	${KMOD:S/$/.c/}
1956aec1278SMax Laier
1966aec1278SMax Laier.for _firmw in ${FIRMWS}
1975852ae2dSBryan Drewery${_firmw:C/\:.*$/.fwo/:T}:	${_firmw:C/\:.*$//}
1986aec1278SMax Laier	@${ECHO} ${_firmw:C/\:.*$//} ${.ALLSRC:M*${_firmw:C/\:.*$//}}
199009b3d3dSMax Laier	@if [ -e ${_firmw:C/\:.*$//} ]; then			\
2005b6dc2efSSimon J. Gerraty		${LD} -b binary --no-warn-mismatch ${_LDFLAGS}	\
2011dd50172SEd Maste		    -m ${LD_EMULATION} -r -d			\
2021dd50172SEd Maste		    -o ${.TARGET} ${_firmw:C/\:.*$//};		\
203ff8e7669SMax Laier	else							\
204ff8e7669SMax Laier		ln -s ${.ALLSRC:M*${_firmw:C/\:.*$//}} ${_firmw:C/\:.*$//}; \
2055b6dc2efSSimon J. Gerraty		${LD} -b binary --no-warn-mismatch ${_LDFLAGS}	\
2061dd50172SEd Maste		    -m ${LD_EMULATION} -r -d			\
2071dd50172SEd Maste		    -o ${.TARGET} ${_firmw:C/\:.*$//};		\
208ff8e7669SMax Laier		rm ${_firmw:C/\:.*$//};				\
209ff8e7669SMax Laier	fi
2106aec1278SMax Laier
211d9eed6f4SJohn BaldwinOBJS+=	${_firmw:C/\:.*$/.fwo/:T}
2126aec1278SMax Laier.endfor
2136aec1278SMax Laier.endif
2146aec1278SMax Laier
21535c4406dSWarner Losh# Conditionally include SRCS based on kernel config options.
21635c4406dSWarner Losh.for _o in ${KERN_OPTS}
21735c4406dSWarner LoshSRCS+=${SRCS.${_o}}
21835c4406dSWarner Losh.endfor
21935c4406dSWarner Losh
220639a2e50SGarrett WollmanOBJS+=	${SRCS:N*.h:R:S/$/.o/g}
221639a2e50SGarrett Wollman
222639a2e50SGarrett Wollman.if !defined(PROG)
22390f60fc9SMike SmithPROG=	${KMOD}.ko
22490f60fc9SMike Smith.endif
225639a2e50SGarrett Wollman
226817b59bfSRuslan Ermilov.if !defined(DEBUG_FLAGS)
227f6611608SSheldon HearnFULLPROG=	${PROG}
228f6611608SSheldon Hearn.else
22905117b57SEd MasteFULLPROG=	${PROG}.full
2305852ae2dSBryan Drewery${PROG}: ${FULLPROG} ${PROG}.debug
23105117b57SEd Maste	${OBJCOPY} --strip-debug --add-gnu-debuglink=${PROG}.debug \
23298b45a8fSRuslan Ermilov	    ${FULLPROG} ${.TARGET}
2335852ae2dSBryan Drewery${PROG}.debug: ${FULLPROG}
23498b45a8fSRuslan Ermilov	${OBJCOPY} --only-keep-debug ${FULLPROG} ${.TARGET}
235f6611608SSheldon Hearn.endif
236f6611608SSheldon Hearn
2373a0d123eSWarner Losh.if ${__KLD_SHARED} == yes
2385852ae2dSBryan Drewery${FULLPROG}: ${KMOD}.kld
23982dd4542SDimitry Andric	${LD} -m ${LD_EMULATION} -Bshareable -znotext -znorelro ${_LDFLAGS} \
24059642947SEd Maste	    -o ${.TARGET} ${KMOD}.kld
241817b59bfSRuslan Ermilov.if !defined(DEBUG_FLAGS)
242817b59bfSRuslan Ermilov	${OBJCOPY} --strip-debug ${.TARGET}
2434c293d35SRuslan Ermilov.endif
2444d6bcc83SPeter Wemm.endif
24509be11baSPeter Wemm
24693f03548SMike SmithEXPORT_SYMS?=	NO
24793f03548SMike Smith.if ${EXPORT_SYMS} != YES
2481f089616SRuslan ErmilovCLEANFILES+=	export_syms
2490b3178a4SMike Smith.endif
2500b3178a4SMike Smith
251ecae3df1SMark Johnston.if exists(${SYSDIR}/conf/ldscript.kmod.${MACHINE_ARCH})
252ecae3df1SMark JohnstonLDSCRIPT_FLAGS?= -T ${SYSDIR}/conf/ldscript.kmod.${MACHINE_ARCH}
253d6793db2SMark Johnston.endif
254d6793db2SMark Johnston
2553a0d123eSWarner Losh.if ${__KLD_SHARED} == yes
2565852ae2dSBryan Drewery${KMOD}.kld: ${OBJS}
2574d6bcc83SPeter Wemm.else
2585852ae2dSBryan Drewery${FULLPROG}: ${OBJS}
2594d6bcc83SPeter Wemm.endif
260ecae3df1SMark Johnston	${LD} -m ${LD_EMULATION} ${_LDFLAGS} ${LDSCRIPT_FLAGS} -r -d \
261ecae3df1SMark Johnston	    -o ${.TARGET} ${OBJS}
262724afafaSWarner Losh.if ${MK_CTF} != "no"
263cd49c2e8SMax Khon	${CTFMERGE} ${CTFFLAGS} -o ${.TARGET} ${OBJS}
264cd49c2e8SMax Khon.endif
2650b3178a4SMike Smith.if defined(EXPORT_SYMS)
26693f03548SMike Smith.if ${EXPORT_SYMS} != YES
2670b3178a4SMike Smith.if ${EXPORT_SYMS} == NO
2688b3ae668SDag-Erling Smørgrav	:> export_syms
2690b3178a4SMike Smith.elif !exists(${.CURDIR}/${EXPORT_SYMS})
270b8283138SEd Maste	echo -n "${EXPORT_SYMS:@s@$s${.newline}@}" > export_syms
2710b3178a4SMike Smith.else
2721f089616SRuslan Ermilov	grep -v '^#' < ${EXPORT_SYMS} > export_syms
2730b3178a4SMike Smith.endif
27411f1d284SMark Johnston	${AWK} -f ${SYSDIR}/conf/kmod_syms.awk ${.TARGET} \
2751f089616SRuslan Ermilov	    export_syms | xargs -J% ${OBJCOPY} % ${.TARGET}
27693f03548SMike Smith.endif
277b8283138SEd Maste.endif # defined(EXPORT_SYMS)
278dc6a41b9SJonathan T. Looney.if defined(PREFIX_SYMS)
279dc6a41b9SJonathan T. Looney	${AWK} -v prefix=${PREFIX_SYMS} -f ${SYSDIR}/conf/kmod_syms_prefix.awk \
280dc6a41b9SJonathan T. Looney	    ${.TARGET} /dev/null | xargs -J% ${OBJCOPY} % ${.TARGET}
281dc6a41b9SJonathan T. Looney.endif
2823a0d123eSWarner Losh.if !defined(DEBUG_FLAGS) && ${__KLD_SHARED} == no
2834d6bcc83SPeter Wemm	${OBJCOPY} --strip-debug ${.TARGET}
2844d6bcc83SPeter Wemm.endif
2850b3178a4SMike Smith
2867114b176SMark Johnston.if ${COMPILER_TYPE} == "clang" || \
2877114b176SMark Johnston    (${COMPILER_TYPE} == "gcc" && ${COMPILER_VERSION} >= 60000)
2887114b176SMark Johnston_MAP_DEBUG_PREFIX= yes
2897114b176SMark Johnston.endif
2907114b176SMark Johnston
291528013d5SWarner Losh_ILINKS=machine
292544de89dSJohn Baldwin.if ${MACHINE_CPUARCH} == "i386" || ${MACHINE_CPUARCH} == "amd64"
293544de89dSJohn Baldwin_ILINKS+=x86
294544de89dSJohn Baldwin.endif
29531a01497SSean BrunoCLEANFILES+=${_ILINKS}
296639a2e50SGarrett Wollman
2971bd4272fSBryan Dreweryall: ${PROG}
2984a2d2630SBruce Evans
299db00e6c5SDavid E. O'Brienbeforedepend: ${_ILINKS}
30068d96923SBryan Drewerybeforebuild: ${_ILINKS}
301985f41afSWarner Losh
302f3cc9575SWarner Losh# Ensure that the links exist without depending on it when it exists which
303f3cc9575SWarner Losh# causes all the modules to be rebuilt when the directory pointed to changes.
3047114b176SMark Johnston# Ensure that debug info references the path in the source tree.
305f3cc9575SWarner Losh.for _link in ${_ILINKS}
306f3cc9575SWarner Losh.if !exists(${.OBJDIR}/${_link})
3073063d216SBryan DreweryOBJS_DEPEND_GUESS+=	${_link}
308f3cc9575SWarner Losh.endif
3097114b176SMark Johnston.if defined(_MAP_DEBUG_PREFIX)
3107114b176SMark Johnston.if ${_link} == "machine"
3117114b176SMark JohnstonCFLAGS+= -fdebug-prefix-map=./machine=${SYSDIR}/${MACHINE}/include
3127114b176SMark Johnston.else
3137114b176SMark JohnstonCFLAGS+= -fdebug-prefix-map=./${_link}=${SYSDIR}/${_link}/include
3147114b176SMark Johnston.endif
3157114b176SMark Johnston.endif
316f3cc9575SWarner Losh.endfor
3174a2d2630SBruce Evans
3187750ad47SMarcel Moolenaar.NOPATH: ${_ILINKS}
3193c4368b2SMark Johnston
3204a2d2630SBruce Evans${_ILINKS}:
3219a8631b3SWarner Losh	@case ${.TARGET} in \
322df3a103dSWarner Losh	machine) \
323df3a103dSWarner Losh		path=${SYSDIR}/${MACHINE}/include ;; \
324544de89dSJohn Baldwin	*) \
3253c4368b2SMark Johnston		path=${SYSDIR}/${.TARGET:T}/include ;; \
32608a04a89SPeter Wemm	esac ; \
32708a04a89SPeter Wemm	path=`(cd $$path && /bin/pwd)` ; \
3283c4368b2SMark Johnston	${ECHO} ${.TARGET:T} "->" $$path ; \
329454ce383SAlex Richardson	ln -fns $$path ${.TARGET:T}
3304a2d2630SBruce Evans
33174465532SRuslan ErmilovCLEANFILES+= ${PROG} ${KMOD}.kld ${OBJS}
332361186e5SMaxime Henrion
333817b59bfSRuslan Ermilov.if defined(DEBUG_FLAGS)
33405117b57SEd MasteCLEANFILES+= ${FULLPROG} ${PROG}.debug
335361186e5SMaxime Henrion.endif
336639a2e50SGarrett Wollman
337639a2e50SGarrett Wollman.if !target(install)
338639a2e50SGarrett Wollman
339ea87b3cbSDavid E. O'Brien_INSTALLFLAGS:=	${INSTALLFLAGS}
340e902c1bbSTim Vanderhoek.for ie in ${INSTALLFLAGS_EDIT}
341e902c1bbSTim Vanderhoek_INSTALLFLAGS:=	${_INSTALLFLAGS${ie}}
342e902c1bbSTim Vanderhoek.endfor
343e902c1bbSTim Vanderhoek
3442b06260dSRuslan Ermilov.if !target(realinstall)
34505117b57SEd MasteKERN_DEBUGDIR?=	${DEBUGDIR}
3462b06260dSRuslan Ermilovrealinstall: _kmodinstall
3472b06260dSRuslan Ermilov.ORDER: beforeinstall _kmodinstall
34849773547SBryan Drewery_kmodinstall: .PHONY
3491c6eb1b2SBaptiste Daroussin	${INSTALL} -T release -o ${KMODOWN} -g ${KMODGRP} -m ${KMODMODE} \
3500ed70e42SBryan Drewery	    ${_INSTALLFLAGS} ${PROG} ${DESTDIR}${KMODDIR}/
351cd95660fSDavid E. O'Brien.if defined(DEBUG_FLAGS) && !defined(INSTALL_NODEBUG) && ${MK_KERNEL_SYMBOLS} != "no"
3521c6eb1b2SBaptiste Daroussin	${INSTALL} -T debug -o ${KMODOWN} -g ${KMODGRP} -m ${KMODMODE} \
3530ed70e42SBryan Drewery	    ${_INSTALLFLAGS} ${PROG}.debug ${DESTDIR}${KERN_DEBUGDIR}${KMODDIR}/
354d24864f7SRuslan Ermilov.endif
3552b06260dSRuslan Ermilov
3562b06260dSRuslan Ermilov.include <bsd.links.mk>
3572b06260dSRuslan Ermilov
3584875819cSJake Burkholder.if !defined(NO_XREF)
3592b06260dSRuslan Ermilovafterinstall: _kldxref
3602b06260dSRuslan Ermilov.ORDER: realinstall _kldxref
3612b06260dSRuslan Ermilov.ORDER: _installlinks _kldxref
36249773547SBryan Drewery_kldxref: .PHONY
363be1d673dSRuslan Ermilov	@if type kldxref >/dev/null 2>&1; then \
3645ec57af4SMaxim Sobolev		${ECHO} ${KLDXREF_CMD} ${DESTDIR}${KMODDIR}; \
3655ec57af4SMaxim Sobolev		${KLDXREF_CMD} ${DESTDIR}${KMODDIR}; \
366be1d673dSRuslan Ermilov	fi
367505222d3SPeter Wemm.endif
36822a8b405SMaxime Henrion.endif # !target(realinstall)
369639a2e50SGarrett Wollman
37022a8b405SMaxime Henrion.endif # !target(install)
371639a2e50SGarrett Wollman
372639a2e50SGarrett Wollman.if !target(load)
37349773547SBryan Dreweryload: ${PROG} .PHONY
3741f089616SRuslan Ermilov	${KMODLOAD} -v ${.OBJDIR}/${PROG}
375639a2e50SGarrett Wollman.endif
376639a2e50SGarrett Wollman
377639a2e50SGarrett Wollman.if !target(unload)
37849773547SBryan Dreweryunload: .PHONY
37993bffaf2SDag-Erling Smørgrav	if ${KMODISLOADED} ${PROG} ; then ${KMODUNLOAD} -v ${PROG} ; fi
38093bffaf2SDag-Erling Smørgrav.endif
38193bffaf2SDag-Erling Smørgrav
38293bffaf2SDag-Erling Smørgrav.if !target(reload)
38349773547SBryan Dreweryreload: unload load .PHONY
38408a04a89SPeter Wemm.endif
3851aa57be3SGarrett Wollman
3866b1bb559SWarner Losh.if defined(KERNBUILDDIR)
3876b1bb559SWarner Losh.PATH: ${KERNBUILDDIR}
3886b1bb559SWarner LoshCFLAGS+=	-I${KERNBUILDDIR}
3896b1bb559SWarner Losh.for _src in ${SRCS:Mopt_*.h}
3906b1bb559SWarner LoshCLEANFILES+=	${_src}
3916b1bb559SWarner Losh.if !target(${_src})
3926b1bb559SWarner Losh${_src}:
39340244cc2SDavid E. O'Brien	ln -sf ${KERNBUILDDIR}/${_src} ${.TARGET}
3946b1bb559SWarner Losh.endif
3956b1bb559SWarner Losh.endfor
3966b1bb559SWarner Losh.else
3974fea6704SBruce Evans.for _src in ${SRCS:Mopt_*.h}
3984fea6704SBruce EvansCLEANFILES+=	${_src}
3994fea6704SBruce Evans.if !target(${_src})
4004fea6704SBruce Evans${_src}:
4018b3ae668SDag-Erling Smørgrav	:> ${.TARGET}
4024fea6704SBruce Evans.endif
4034fea6704SBruce Evans.endfor
4046b1bb559SWarner Losh.endif
4054fea6704SBruce Evans
4064ea56599SAndrew Turner# Add the sanitizer C flags
4074ea56599SAndrew TurnerCFLAGS+=	${SAN_CFLAGS}
4084ea56599SAndrew Turner
4095426539cSMatt Macy# Add the gcov flags
4105426539cSMatt MacyCFLAGS+=	${GCOV_CFLAGS}
4115426539cSMatt Macy
4123fd1e05bSAlexander Leidinger# Respect configuration-specific C flags.
413256054fdSBrooks DavisCFLAGS+=	${ARCH_FLAGS} ${CONF_CFLAGS}
41462769330SAlexander Leidinger
415a7d30e5fSDavid E. O'Brien.if !empty(SRCS:Mvnode_if.c)
41678b2c868SBrian FeldmanCLEANFILES+=	vnode_if.c
417528013d5SWarner Loshvnode_if.c: ${SYSDIR}/tools/vnode_if.awk ${SYSDIR}/kern/vnode_if.src
418528013d5SWarner Losh	${AWK} -f ${SYSDIR}/tools/vnode_if.awk ${SYSDIR}/kern/vnode_if.src -c
4194fea6704SBruce Evans.endif
42078b2c868SBrian Feldman
421a7d30e5fSDavid E. O'Brien.if !empty(SRCS:Mvnode_if.h)
4229f73f0e8SRuslan ErmilovCLEANFILES+=	vnode_if.h vnode_if_newproto.h vnode_if_typedef.h
423528013d5SWarner Loshvnode_if.h vnode_if_newproto.h vnode_if_typedef.h: ${SYSDIR}/tools/vnode_if.awk \
424528013d5SWarner Losh    ${SYSDIR}/kern/vnode_if.src
4259f73f0e8SRuslan Ermilovvnode_if.h: vnode_if_newproto.h vnode_if_typedef.h
426528013d5SWarner Losh	${AWK} -f ${SYSDIR}/tools/vnode_if.awk ${SYSDIR}/kern/vnode_if.src -h
4279f73f0e8SRuslan Ermilovvnode_if_newproto.h:
428528013d5SWarner Losh	${AWK} -f ${SYSDIR}/tools/vnode_if.awk ${SYSDIR}/kern/vnode_if.src -p
4299f73f0e8SRuslan Ermilovvnode_if_typedef.h:
430528013d5SWarner Losh	${AWK} -f ${SYSDIR}/tools/vnode_if.awk ${SYSDIR}/kern/vnode_if.src -q
43178b2c868SBrian Feldman.endif
4321aa57be3SGarrett Wollman
43312f05b84SWarner Losh# Build _if.[ch] from _if.m, and clean them when we're done.
434ec4047adSBryan Drewery# __MPATH defined in config.mk
4352cd2ff0eSJohn Baldwin_MFILES=${__MPATH:T:O}
4362cd2ff0eSJohn Baldwin_MPATH=${__MPATH:H:O:u}
43712f05b84SWarner Losh.PATH.m: ${_MPATH}
438160c44feSWarner Losh.for _i in ${SRCS:M*_if.[ch]}
4392cd2ff0eSJohn Baldwin_MATCH=M${_i:R:S/$/.m/}
4402cd2ff0eSJohn Baldwin_MATCHES=${_MFILES:${_MATCH}}
4412cd2ff0eSJohn Baldwin.if !empty(_MATCHES)
4422cd2ff0eSJohn BaldwinCLEANFILES+=	${_i}
4432cd2ff0eSJohn Baldwin.endif
444160c44feSWarner Losh.endfor # _i
4455852ae2dSBryan Drewery.m.c:	${SYSDIR}/tools/makeobjops.awk
44612f05b84SWarner Losh	${AWK} -f ${SYSDIR}/tools/makeobjops.awk ${.IMPSRC} -c
44712f05b84SWarner Losh
4485852ae2dSBryan Drewery.m.h:	${SYSDIR}/tools/makeobjops.awk
44912f05b84SWarner Losh	${AWK} -f ${SYSDIR}/tools/makeobjops.awk ${.IMPSRC} -h
45012f05b84SWarner Losh
4514e91eaf2SWarner Losh.for _i in mii pccard
452a7d30e5fSDavid E. O'Brien.if !empty(SRCS:M${_i}devs.h)
453dba6dd17SWarner LoshCLEANFILES+=	${_i}devs.h
4545852ae2dSBryan Drewery${_i}devs.h: ${SYSDIR}/tools/${_i}devs2h.awk ${SYSDIR}/dev/${_i}/${_i}devs
455528013d5SWarner Losh	${AWK} -f ${SYSDIR}/tools/${_i}devs2h.awk ${SYSDIR}/dev/${_i}/${_i}devs
4562d3ce713SDavid E. O'Brien.endif
457dba6dd17SWarner Losh.endfor # _i
4582d3ce713SDavid E. O'Brien
459e83ce340SAdrian Chadd.if !empty(SRCS:Mbhnd_nvram_map.h)
460e83ce340SAdrian ChaddCLEANFILES+=	bhnd_nvram_map.h
461e83ce340SAdrian Chaddbhnd_nvram_map.h: ${SYSDIR}/dev/bhnd/tools/nvram_map_gen.awk \
462e83ce340SAdrian Chadd    ${SYSDIR}/dev/bhnd/tools/nvram_map_gen.sh \
4635852ae2dSBryan Drewery    ${SYSDIR}/dev/bhnd/nvram/nvram_map
4645852ae2dSBryan Drewerybhnd_nvram_map.h:
46516fba18fSDimitry Andric	sh ${SYSDIR}/dev/bhnd/tools/nvram_map_gen.sh \
466e83ce340SAdrian Chadd	    ${SYSDIR}/dev/bhnd/nvram/nvram_map -h
467e83ce340SAdrian Chadd.endif
468e83ce340SAdrian Chadd
469e83ce340SAdrian Chadd.if !empty(SRCS:Mbhnd_nvram_map_data.h)
470e83ce340SAdrian ChaddCLEANFILES+=	bhnd_nvram_map_data.h
471e83ce340SAdrian Chaddbhnd_nvram_map_data.h: ${SYSDIR}/dev/bhnd/tools/nvram_map_gen.awk \
472e83ce340SAdrian Chadd    ${SYSDIR}/dev/bhnd/tools/nvram_map_gen.sh \
4735852ae2dSBryan Drewery    ${SYSDIR}/dev/bhnd/nvram/nvram_map
4745852ae2dSBryan Drewerybhnd_nvram_map_data.h:
47516fba18fSDimitry Andric	sh ${SYSDIR}/dev/bhnd/tools/nvram_map_gen.sh \
476e83ce340SAdrian Chadd	    ${SYSDIR}/dev/bhnd/nvram/nvram_map -d
477e83ce340SAdrian Chadd.endif
478e83ce340SAdrian Chadd
479a7d30e5fSDavid E. O'Brien.if !empty(SRCS:Musbdevs.h)
4804e91eaf2SWarner LoshCLEANFILES+=	usbdevs.h
4815852ae2dSBryan Dreweryusbdevs.h: ${SYSDIR}/tools/usbdevs2h.awk ${SYSDIR}/dev/usb/usbdevs
482528013d5SWarner Losh	${AWK} -f ${SYSDIR}/tools/usbdevs2h.awk ${SYSDIR}/dev/usb/usbdevs -h
4834e91eaf2SWarner Losh.endif
4844e91eaf2SWarner Losh
485a7d30e5fSDavid E. O'Brien.if !empty(SRCS:Musbdevs_data.h)
4864e91eaf2SWarner LoshCLEANFILES+=	usbdevs_data.h
4875852ae2dSBryan Dreweryusbdevs_data.h: ${SYSDIR}/tools/usbdevs2h.awk ${SYSDIR}/dev/usb/usbdevs
488528013d5SWarner Losh	${AWK} -f ${SYSDIR}/tools/usbdevs2h.awk ${SYSDIR}/dev/usb/usbdevs -d
4894e91eaf2SWarner Losh.endif
4904e91eaf2SWarner Losh
49167ca7330SBjoern A. Zeeb.if !empty(SRCS:Msdiodevs.h)
49267ca7330SBjoern A. ZeebCLEANFILES+=	sdiodevs.h
49367ca7330SBjoern A. Zeebsdiodevs.h: ${SYSDIR}/tools/sdiodevs2h.awk ${SYSDIR}/dev/sdio/sdiodevs
49467ca7330SBjoern A. Zeeb	${AWK} -f ${SYSDIR}/tools/sdiodevs2h.awk ${SYSDIR}/dev/sdio/sdiodevs -h
49567ca7330SBjoern A. Zeeb.endif
49667ca7330SBjoern A. Zeeb
49767ca7330SBjoern A. Zeeb.if !empty(SRCS:Msdiodevs_data.h)
49867ca7330SBjoern A. ZeebCLEANFILES+=	sdiodevs_data.h
49967ca7330SBjoern A. Zeebsdiodevs_data.h: ${SYSDIR}/tools/sdiodevs2h.awk ${SYSDIR}/dev/sdio/sdiodevs
50067ca7330SBjoern A. Zeeb	${AWK} -f ${SYSDIR}/tools/sdiodevs2h.awk ${SYSDIR}/dev/sdio/sdiodevs -d
50167ca7330SBjoern A. Zeeb.endif
50267ca7330SBjoern A. Zeeb
503a7d30e5fSDavid E. O'Brien.if !empty(SRCS:Macpi_quirks.h)
504684c1c1dSNate LawsonCLEANFILES+=	acpi_quirks.h
5055852ae2dSBryan Dreweryacpi_quirks.h: ${SYSDIR}/tools/acpi_quirks2h.awk ${SYSDIR}/dev/acpica/acpi_quirks
506528013d5SWarner Losh	${AWK} -f ${SYSDIR}/tools/acpi_quirks2h.awk ${SYSDIR}/dev/acpica/acpi_quirks
507684c1c1dSNate Lawson.endif
508684c1c1dSNate Lawson
509fc2a8776SEd Maste.if !empty(SRCS:Massym.inc) || !empty(DPSRCS:Massym.inc)
5109d2b4c4fSBryan DreweryCLEANFILES+=	assym.inc genassym.o
511ccca101fSBryan DreweryDEPENDOBJS+=	genassym.o
512ccca101fSBryan DreweryDPSRCS+=	offset.inc
513ccca101fSBryan Drewery.endif
51482c98d01SBryan Drewery.if defined(MODULE_TIED)
51582c98d01SBryan DreweryDPSRCS+=	offset.inc
51682c98d01SBryan Drewery.endif
517ccca101fSBryan Drewery.if !empty(SRCS:Moffset.inc) || !empty(DPSRCS:Moffset.inc)
518ccca101fSBryan DreweryCLEANFILES+=	offset.inc genoffset.o
519ccca101fSBryan DreweryDEPENDOBJS+=	genoffset.o
520ccca101fSBryan Drewery.endif
521fc2a8776SEd Masteassym.inc: genassym.o
5228bedbb4dSMatt Macyoffset.inc: genoffset.o
523fc2a8776SEd Masteassym.inc: ${SYSDIR}/kern/genassym.sh
524528013d5SWarner Losh	sh ${SYSDIR}/kern/genassym.sh genassym.o > ${.TARGET}
525d69d8a46SBryan Drewerygenassym.o: ${SYSDIR}/${MACHINE}/${MACHINE}/genassym.c offset.inc
5265852ae2dSBryan Drewerygenassym.o: ${SRCS:Mopt_*.h}
527*a8976aecSAlex Richardson	${CC} -c ${CFLAGS:N-flto:N-fno-common} -fcommon \
5287dd824c3SEd Maste	    ${SYSDIR}/${MACHINE}/${MACHINE}/genassym.c
5298bedbb4dSMatt Macyoffset.inc: ${SYSDIR}/kern/genoffset.sh genoffset.o
5308bedbb4dSMatt Macy	sh ${SYSDIR}/kern/genoffset.sh genoffset.o > ${.TARGET}
531d69d8a46SBryan Drewerygenoffset.o: ${SYSDIR}/kern/genoffset.c
5328bedbb4dSMatt Macygenoffset.o: ${SRCS:Mopt_*.h}
533*a8976aecSAlex Richardson	${CC} -c ${CFLAGS:N-flto:N-fno-common} -fcommon \
534d69d8a46SBryan Drewery	    ${SYSDIR}/kern/genoffset.c
535705d63cdSIan Dowse
536de1a3bd9SBryan DreweryCLEANDEPENDFILES+=	${_ILINKS}
53774465532SRuslan Ermilov# .depend needs include links so we remove them only together.
53874465532SRuslan Ermilovcleanilinks:
53974465532SRuslan Ermilov	rm -f ${_ILINKS}
54074465532SRuslan Ermilov
541d7beff08SBryan DreweryOBJS_DEPEND_GUESS+= ${SRCS:M*.h}
542817f7baaSBryan Drewery.if defined(KERNBUILDDIR)
543817f7baaSBryan DreweryOBJS_DEPEND_GUESS+= opt_global.h
544817f7baaSBryan Drewery.endif
54588fe1f7aSBryan Drewery
54688fe1f7aSBryan Drewery.include <bsd.dep.mk>
547aae63957SBryan Drewery.include <bsd.clang-analyze.mk>
548afba897fSBruce Evans.include <bsd.obj.mk>
549ddc474a8SRuslan Ermilov.include "kern.mk"
550