kmod.mk revision 295414
1213284Sjilles#	From: @(#)bsd.prog.mk	5.26 (Berkeley) 6/25/91
2# $FreeBSD: head/sys/conf/kmod.mk 295414 2016-02-08 20:58:52Z 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: beforebuild .WAIT ${PROG}
253beforebuild: objwarn
254
255beforedepend: ${_ILINKS}
256beforebuild: ${_ILINKS}
257
258# Ensure that the links exist without depending on it when it exists which
259# causes all the modules to be rebuilt when the directory pointed to changes.
260.for _link in ${_ILINKS}
261.if !exists(${.OBJDIR}/${_link})
262${OBJS}: ${_link}
263.endif
264.endfor
265
266# Search for kernel source tree in standard places.
267.for _dir in ${.CURDIR}/../.. ${.CURDIR}/../../.. /sys /usr/src/sys
268.if !defined(SYSDIR) && exists(${_dir}/kern/)
269SYSDIR=	${_dir}
270.endif
271.endfor
272.if !defined(SYSDIR) || !exists(${SYSDIR}/kern/)
273.error "can't find kernel source tree"
274.endif
275
276.NOPATH: ${_ILINKS}
277
278${_ILINKS}:
279	@case ${.TARGET} in \
280	machine) \
281		path=${SYSDIR}/${MACHINE}/include ;; \
282	*) \
283		path=${SYSDIR}/${.TARGET:T}/include ;; \
284	esac ; \
285	path=`(cd $$path && /bin/pwd)` ; \
286	${ECHO} ${.TARGET:T} "->" $$path ; \
287	ln -sf $$path ${.TARGET:T}
288
289CLEANFILES+= ${PROG} ${KMOD}.kld ${OBJS}
290
291.if defined(DEBUG_FLAGS)
292CLEANFILES+= ${FULLPROG} ${PROG}.debug
293.endif
294
295.if !target(install)
296
297_INSTALLFLAGS:=	${INSTALLFLAGS}
298.for ie in ${INSTALLFLAGS_EDIT}
299_INSTALLFLAGS:=	${_INSTALLFLAGS${ie}}
300.endfor
301
302.if !target(realinstall)
303KERN_DEBUGDIR?=	${DEBUGDIR}
304realinstall: _kmodinstall
305.ORDER: beforeinstall _kmodinstall
306_kmodinstall:
307	${INSTALL} -o ${KMODOWN} -g ${KMODGRP} -m ${KMODMODE} \
308	    ${_INSTALLFLAGS} ${PROG} ${DESTDIR}${KMODDIR}/
309.if defined(DEBUG_FLAGS) && !defined(INSTALL_NODEBUG) && ${MK_KERNEL_SYMBOLS} != "no"
310	${INSTALL} -o ${KMODOWN} -g ${KMODGRP} -m ${KMODMODE} \
311	    ${_INSTALLFLAGS} ${PROG}.debug ${DESTDIR}${KERN_DEBUGDIR}${KMODDIR}/
312.endif
313
314.include <bsd.links.mk>
315
316.if !defined(NO_XREF)
317afterinstall: _kldxref
318.ORDER: realinstall _kldxref
319.ORDER: _installlinks _kldxref
320_kldxref:
321	@if type kldxref >/dev/null 2>&1; then \
322		${ECHO} kldxref ${DESTDIR}${KMODDIR}; \
323		kldxref ${DESTDIR}${KMODDIR}; \
324	fi
325.endif
326.endif # !target(realinstall)
327
328.endif # !target(install)
329
330.if !target(load)
331load: ${PROG}
332	${KMODLOAD} -v ${.OBJDIR}/${PROG}
333.endif
334
335.if !target(unload)
336unload:
337	if ${KMODISLOADED} ${PROG} ; then ${KMODUNLOAD} -v ${PROG} ; fi
338.endif
339
340.if !target(reload)
341reload: unload load
342.endif
343
344.if defined(KERNBUILDDIR)
345.PATH: ${KERNBUILDDIR}
346CFLAGS+=	-I${KERNBUILDDIR}
347.for _src in ${SRCS:Mopt_*.h}
348CLEANFILES+=	${_src}
349.if !target(${_src})
350${_src}:
351	ln -sf ${KERNBUILDDIR}/${_src} ${.TARGET}
352.endif
353.endfor
354.else
355.for _src in ${SRCS:Mopt_*.h}
356CLEANFILES+=	${_src}
357.if !target(${_src})
358${_src}:
359	:> ${.TARGET}
360.endif
361.endfor
362.endif
363
364# Respect configuration-specific C flags.
365CFLAGS+=	${CONF_CFLAGS}
366
367.if !empty(SRCS:Mvnode_if.c)
368CLEANFILES+=	vnode_if.c
369vnode_if.c: ${SYSDIR}/tools/vnode_if.awk ${SYSDIR}/kern/vnode_if.src
370	${AWK} -f ${SYSDIR}/tools/vnode_if.awk ${SYSDIR}/kern/vnode_if.src -c
371.endif
372
373.if !empty(SRCS:Mvnode_if.h)
374CLEANFILES+=	vnode_if.h vnode_if_newproto.h vnode_if_typedef.h
375vnode_if.h vnode_if_newproto.h vnode_if_typedef.h: ${SYSDIR}/tools/vnode_if.awk \
376    ${SYSDIR}/kern/vnode_if.src
377vnode_if.h: vnode_if_newproto.h vnode_if_typedef.h
378	${AWK} -f ${SYSDIR}/tools/vnode_if.awk ${SYSDIR}/kern/vnode_if.src -h
379vnode_if_newproto.h:
380	${AWK} -f ${SYSDIR}/tools/vnode_if.awk ${SYSDIR}/kern/vnode_if.src -p
381vnode_if_typedef.h:
382	${AWK} -f ${SYSDIR}/tools/vnode_if.awk ${SYSDIR}/kern/vnode_if.src -q
383.endif
384
385# Build _if.[ch] from _if.m, and clean them when we're done.
386# This is duplicated in sys/modules/Makefile.
387.if !defined(__MPATH)
388__MPATH!=find ${SYSDIR:tA}/ -name \*_if.m
389.export __MPATH
390.endif
391_MFILES=${__MPATH:T:O}
392_MPATH=${__MPATH:H:O:u}
393.PATH.m: ${_MPATH}
394.for _i in ${SRCS:M*_if.[ch]}
395_MATCH=M${_i:R:S/$/.m/}
396_MATCHES=${_MFILES:${_MATCH}}
397.if !empty(_MATCHES)
398CLEANFILES+=	${_i}
399.endif
400.endfor # _i
401.m.c:	${SYSDIR}/tools/makeobjops.awk
402	${AWK} -f ${SYSDIR}/tools/makeobjops.awk ${.IMPSRC} -c
403
404.m.h:	${SYSDIR}/tools/makeobjops.awk
405	${AWK} -f ${SYSDIR}/tools/makeobjops.awk ${.IMPSRC} -h
406
407.for _i in mii pccard
408.if !empty(SRCS:M${_i}devs.h)
409CLEANFILES+=	${_i}devs.h
410${_i}devs.h: ${SYSDIR}/tools/${_i}devs2h.awk ${SYSDIR}/dev/${_i}/${_i}devs
411	${AWK} -f ${SYSDIR}/tools/${_i}devs2h.awk ${SYSDIR}/dev/${_i}/${_i}devs
412.endif
413.endfor # _i
414
415.if !empty(SRCS:Musbdevs.h)
416CLEANFILES+=	usbdevs.h
417usbdevs.h: ${SYSDIR}/tools/usbdevs2h.awk ${SYSDIR}/dev/usb/usbdevs
418	${AWK} -f ${SYSDIR}/tools/usbdevs2h.awk ${SYSDIR}/dev/usb/usbdevs -h
419.endif
420
421.if !empty(SRCS:Musbdevs_data.h)
422CLEANFILES+=	usbdevs_data.h
423usbdevs_data.h: ${SYSDIR}/tools/usbdevs2h.awk ${SYSDIR}/dev/usb/usbdevs
424	${AWK} -f ${SYSDIR}/tools/usbdevs2h.awk ${SYSDIR}/dev/usb/usbdevs -d
425.endif
426
427.if !empty(SRCS:Macpi_quirks.h)
428CLEANFILES+=	acpi_quirks.h
429acpi_quirks.h: ${SYSDIR}/tools/acpi_quirks2h.awk ${SYSDIR}/dev/acpica/acpi_quirks
430	${AWK} -f ${SYSDIR}/tools/acpi_quirks2h.awk ${SYSDIR}/dev/acpica/acpi_quirks
431.endif
432
433.if !empty(SRCS:Massym.s)
434CLEANFILES+=	assym.s genassym.o
435assym.s: genassym.o
436.if defined(KERNBUILDDIR)
437genassym.o: opt_global.h
438.endif
439assym.s: ${SYSDIR}/kern/genassym.sh
440	sh ${SYSDIR}/kern/genassym.sh genassym.o > ${.TARGET}
441genassym.o: ${SYSDIR}/${MACHINE}/${MACHINE}/genassym.c
442genassym.o: ${SRCS:Mopt_*.h}
443	${CC} -c ${CFLAGS:N-fno-common} \
444	    ${SYSDIR}/${MACHINE}/${MACHINE}/genassym.c
445.endif
446
447lint: ${SRCS}
448	${LINT} ${LINTKERNFLAGS} ${CFLAGS:M-[DILU]*} ${.ALLSRC:M*.c}
449
450.if defined(KERNBUILDDIR)
451${OBJS}: opt_global.h
452.endif
453
454.include <bsd.dep.mk>
455
456cleandepend: cleanilinks
457# .depend needs include links so we remove them only together.
458cleanilinks:
459	rm -f ${_ILINKS}
460
461.if !exists(${.OBJDIR}/${DEPENDFILE})
462${OBJS}: ${SRCS:M*.h}
463.endif
464
465.include <bsd.obj.mk>
466.include "kern.mk"
467