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