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