kmod.mk revision 285068
1#	From: @(#)bsd.prog.mk	5.26 (Berkeley) 6/25/91
2# $FreeBSD: head/sys/conf/kmod.mk 285068 2015-07-03 01:50:26Z imp $
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}.debug
176${PROG}: ${FULLPROG} ${PROG}.symbols
177	${OBJCOPY} --strip-debug --add-gnu-debuglink=${PROG}.symbols\
178	    ${FULLPROG} ${.TARGET}
179${PROG}.symbols: ${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}.symbols
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)
280realinstall: _kmodinstall
281.ORDER: beforeinstall _kmodinstall
282_kmodinstall:
283	${INSTALL} -o ${KMODOWN} -g ${KMODGRP} -m ${KMODMODE} \
284	    ${_INSTALLFLAGS} ${PROG} ${DESTDIR}${KMODDIR}
285.if defined(DEBUG_FLAGS) && !defined(INSTALL_NODEBUG) && ${MK_KERNEL_SYMBOLS} != "no"
286	${INSTALL} -o ${KMODOWN} -g ${KMODGRP} -m ${KMODMODE} \
287	    ${_INSTALLFLAGS} ${PROG}.symbols ${DESTDIR}${KMODDIR}
288.endif
289
290.include <bsd.links.mk>
291
292.if !defined(NO_XREF)
293afterinstall: _kldxref
294.ORDER: realinstall _kldxref
295.ORDER: _installlinks _kldxref
296_kldxref:
297	@if type kldxref >/dev/null 2>&1; then \
298		${ECHO} kldxref ${DESTDIR}${KMODDIR}; \
299		kldxref ${DESTDIR}${KMODDIR}; \
300	fi
301.endif
302.endif # !target(realinstall)
303
304.endif # !target(install)
305
306.if !target(load)
307load: ${PROG}
308	${KMODLOAD} -v ${.OBJDIR}/${PROG}
309.endif
310
311.if !target(unload)
312unload:
313	${KMODUNLOAD} -v ${PROG}
314.endif
315
316.if defined(KERNBUILDDIR)
317.PATH: ${KERNBUILDDIR}
318CFLAGS+=	-I${KERNBUILDDIR}
319.for _src in ${SRCS:Mopt_*.h}
320CLEANFILES+=	${_src}
321.if !target(${_src})
322${_src}:
323	ln -sf ${KERNBUILDDIR}/${_src} ${.TARGET}
324.endif
325.endfor
326.else
327.for _src in ${SRCS:Mopt_*.h}
328CLEANFILES+=	${_src}
329.if !target(${_src})
330${_src}:
331	:> ${.TARGET}
332.endif
333.endfor
334.endif
335
336# Respect configuration-specific C flags.
337CFLAGS+=	${CONF_CFLAGS}
338
339.if !empty(SRCS:Mvnode_if.c)
340CLEANFILES+=	vnode_if.c
341vnode_if.c: ${SYSDIR}/tools/vnode_if.awk ${SYSDIR}/kern/vnode_if.src
342	${AWK} -f ${SYSDIR}/tools/vnode_if.awk ${SYSDIR}/kern/vnode_if.src -c
343.endif
344
345.if !empty(SRCS:Mvnode_if.h)
346CLEANFILES+=	vnode_if.h vnode_if_newproto.h vnode_if_typedef.h
347vnode_if.h vnode_if_newproto.h vnode_if_typedef.h: ${SYSDIR}/tools/vnode_if.awk \
348    ${SYSDIR}/kern/vnode_if.src
349vnode_if.h: vnode_if_newproto.h vnode_if_typedef.h
350	${AWK} -f ${SYSDIR}/tools/vnode_if.awk ${SYSDIR}/kern/vnode_if.src -h
351vnode_if_newproto.h:
352	${AWK} -f ${SYSDIR}/tools/vnode_if.awk ${SYSDIR}/kern/vnode_if.src -p
353vnode_if_typedef.h:
354	${AWK} -f ${SYSDIR}/tools/vnode_if.awk ${SYSDIR}/kern/vnode_if.src -q
355.endif
356
357# Build _if.[ch] from _if.m, and clean them when we're done.
358__MPATH!=find ${SYSDIR:tA}/ -name \*_if.m
359_MPATH=${__MPATH:H:O:u}
360.PATH.m: ${_MPATH}
361.for _s in ${SRCS:M*_if.[ch]}
362.if eixsts(${_s:R}.m})
363CLEANFILES+=	${_s}
364.endif
365.endfor # _s
366.m.c:	${SYSDIR}/tools/makeobjops.awk
367	${AWK} -f ${SYSDIR}/tools/makeobjops.awk ${.IMPSRC} -c
368
369.m.h:	${SYSDIR}/tools/makeobjops.awk
370	${AWK} -f ${SYSDIR}/tools/makeobjops.awk ${.IMPSRC} -h
371
372.for _i in mii pccard
373.if !empty(SRCS:M${_i}devs.h)
374CLEANFILES+=	${_i}devs.h
375${_i}devs.h: ${SYSDIR}/tools/${_i}devs2h.awk ${SYSDIR}/dev/${_i}/${_i}devs
376	${AWK} -f ${SYSDIR}/tools/${_i}devs2h.awk ${SYSDIR}/dev/${_i}/${_i}devs
377.endif
378.endfor # _i
379
380.if !empty(SRCS:Musbdevs.h)
381CLEANFILES+=	usbdevs.h
382usbdevs.h: ${SYSDIR}/tools/usbdevs2h.awk ${SYSDIR}/dev/usb/usbdevs
383	${AWK} -f ${SYSDIR}/tools/usbdevs2h.awk ${SYSDIR}/dev/usb/usbdevs -h
384.endif
385
386.if !empty(SRCS:Musbdevs_data.h)
387CLEANFILES+=	usbdevs_data.h
388usbdevs_data.h: ${SYSDIR}/tools/usbdevs2h.awk ${SYSDIR}/dev/usb/usbdevs
389	${AWK} -f ${SYSDIR}/tools/usbdevs2h.awk ${SYSDIR}/dev/usb/usbdevs -d
390.endif
391
392.if !empty(SRCS:Macpi_quirks.h)
393CLEANFILES+=	acpi_quirks.h
394acpi_quirks.h: ${SYSDIR}/tools/acpi_quirks2h.awk ${SYSDIR}/dev/acpica/acpi_quirks
395	${AWK} -f ${SYSDIR}/tools/acpi_quirks2h.awk ${SYSDIR}/dev/acpica/acpi_quirks
396.endif
397
398.if !empty(SRCS:Massym.s)
399CLEANFILES+=	assym.s genassym.o
400assym.s: genassym.o
401.if defined(KERNBUILDDIR)
402genassym.o: opt_global.h
403.endif
404assym.s: ${SYSDIR}/kern/genassym.sh
405	sh ${SYSDIR}/kern/genassym.sh genassym.o > ${.TARGET}
406genassym.o: ${SYSDIR}/${MACHINE}/${MACHINE}/genassym.c
407genassym.o: ${SRCS:Mopt_*.h}
408	${CC} -c ${CFLAGS:N-fno-common} \
409	    ${SYSDIR}/${MACHINE}/${MACHINE}/genassym.c
410.endif
411
412lint: ${SRCS}
413	${LINT} ${LINTKERNFLAGS} ${CFLAGS:M-[DILU]*} ${.ALLSRC:M*.c}
414
415.if defined(KERNBUILDDIR)
416${OBJS}: opt_global.h
417.endif
418
419.include <bsd.dep.mk>
420
421cleandepend: cleanilinks
422# .depend needs include links so we remove them only together.
423cleanilinks:
424	rm -f ${_ILINKS}
425
426.if !exists(${.OBJDIR}/${DEPENDFILE})
427${OBJS}: ${SRCS:M*.h}
428.endif
429
430.include <bsd.obj.mk>
431.include "kern.mk"
432