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