kmod.mk revision 266349
1145184Sglebius#	From: @(#)bsd.prog.mk	5.26 (Berkeley) 6/25/91
2145184Sglebius# $FreeBSD: head/sys/conf/kmod.mk 266349 2014-05-17 20:31:34Z imp $
3145184Sglebius#
4145184Sglebius# The include file <bsd.kmod.mk> handles building and installing loadable
5145184Sglebius# kernel modules.
6145184Sglebius#
7145184Sglebius#
8145184Sglebius# +++ variables +++
9145184Sglebius#
10145184Sglebius# CLEANFILES	Additional files to remove for the clean and cleandir targets.
11145184Sglebius#
12145184Sglebius# EXPORT_SYMS	A list of symbols that should be exported from the module,
13145184Sglebius#		or the name of a file containing a list of symbols, or YES
14145184Sglebius#		to export all symbols.  If not defined, no symbols are
15145184Sglebius#		exported.
16145184Sglebius#
17145184Sglebius# KMOD		The name of the kernel module to build.
18154186Sharti#
19145184Sglebius# KMODDIR	Base path for kernel modules (see kld(4)). [/boot/kernel]
20145184Sglebius#
21145184Sglebius# KMODOWN	Module file owner. [${BINOWN}]
22145184Sglebius#
23145184Sglebius# KMODGRP	Module file group. [${BINGRP}]
24145184Sglebius#
25145184Sglebius# KMODMODE	Module file mode. [${BINMODE}]
26145184Sglebius#
27145184Sglebius# KMODLOAD	Command to load a kernel module [/sbin/kldload]
28145184Sglebius#
29154186Sharti# KMODUNLOAD	Command to unload a kernel module [/sbin/kldunload]
30154186Sharti#
31154186Sharti# MFILES	Optionally a list of interfaces used by the module.
32154186Sharti#		This file contains a default list of interfaces.
33154186Sharti#
34154186Sharti# PROG		The name of the kernel module to build.
35154186Sharti#		If not supplied, ${KMOD}.ko is used.
36154186Sharti#
37154186Sharti# SRCS		List of source files.
38154186Sharti#
39154186Sharti# FIRMWS	List of firmware images in format filename:shortname:version
40154186Sharti#
41145184Sglebius# FIRMWARE_LICENSE
42154186Sharti#		Set to the name of the license the user has to agree on in
43145184Sglebius#		order to use this firmware. See /usr/share/doc/legal
44145184Sglebius#
45145184Sglebius# DESTDIR	The tree where the module gets installed. [not set]
46145184Sglebius#
47145184Sglebius# +++ targets +++
48145184Sglebius#
49145184Sglebius# 	install:
50145184Sglebius#               install the kernel module; if the Makefile
51145184Sglebius#               does not itself define the target install, the targets
52145184Sglebius#               beforeinstall and afterinstall may also be used to cause
53145184Sglebius#               actions immediately before and after the install target
54145184Sglebius#		is executed.
55145184Sglebius#
56145184Sglebius# 	load:
57145184Sglebius#		Load a module.
58145184Sglebius#
59145184Sglebius# 	unload:
60145184Sglebius#		Unload a module.
61145184Sglebius#
62145184Sglebius
63145184SglebiusAWK?=		awk
64145184SglebiusKMODLOAD?=	/sbin/kldload
65145184SglebiusKMODUNLOAD?=	/sbin/kldunload
66145184SglebiusOBJCOPY?=	objcopy
67145184Sglebius
68145184Sglebius# Grab all the options for a kernel build.
69145184Sglebius.include "kern.opts.mk"
70145184Sglebius.include <bsd.init.mk>
71145184Sglebius.include <bsd.compiler.mk>
72145184Sglebius
73145184Sglebius.SUFFIXES: .out .o .c .cc .cxx .C .y .l .s .S
74145184Sglebius
75145184Sglebius# amd64 and mips use direct linking for kmod, all others use shared binaries
76145184Sglebius.if ${MACHINE_CPUARCH} != amd64 && ${MACHINE_CPUARCH} != mips
77145184Sglebius__KLD_SHARED=yes
78145184Sglebius.else
79145184Sglebius__KLD_SHARED=no
80145184Sglebius.endif
81154177Sharti
82154177Sharti.if !empty(CFLAGS:M-O[23s]) && empty(CFLAGS:M-fno-strict-aliasing)
83154177ShartiCFLAGS+=	-fno-strict-aliasing
84154177Sharti.endif
85154177ShartiWERROR?=	-Werror
86154177ShartiCFLAGS+=	${WERROR}
87CFLAGS+=	-D_KERNEL
88CFLAGS+=	-DKLD_MODULE
89
90# Don't use any standard or source-relative include directories.
91CSTD=		c99
92NOSTDINC=	-nostdinc
93CFLAGS:=	${CFLAGS:N-I*} ${NOSTDINC} ${INCLMAGIC} ${CFLAGS:M-I*}
94.if defined(KERNBUILDDIR)
95CFLAGS+=	-DHAVE_KERNEL_OPTION_HEADERS -include ${KERNBUILDDIR}/opt_global.h
96.endif
97
98# Add -I paths for system headers.  Individual module makefiles don't
99# need any -I paths for this.  Similar defaults for .PATH can't be
100# set because there are no standard paths for non-headers.
101CFLAGS+=	-I. -I@
102
103# Add -I path for altq headers as they are included via net/if_var.h
104# for example.
105CFLAGS+=	-I@/contrib/altq
106
107CFLAGS.gcc+=	-finline-limit=${INLINE_LIMIT}
108CFLAGS.gcc+= --param inline-unit-growth=100
109CFLAGS.gcc+= --param large-function-growth=1000
110
111# Disallow common variables, and if we end up with commons from
112# somewhere unexpected, allocate storage for them in the module itself.
113CFLAGS+=	-fno-common
114LDFLAGS+=	-d -warn-common
115
116CFLAGS+=	${DEBUG_FLAGS}
117.if ${MACHINE_CPUARCH} == amd64
118CFLAGS+=	-fno-omit-frame-pointer -mno-omit-leaf-frame-pointer
119.endif
120
121.if ${MACHINE_CPUARCH} == powerpc
122CFLAGS+=	-mlongcall -fno-omit-frame-pointer
123.endif
124
125.if ${MACHINE_CPUARCH} == mips
126CFLAGS+=	-G0 -fno-pic -mno-abicalls -mlong-calls
127.endif
128
129.if defined(DEBUG) || defined(DEBUG_FLAGS)
130CTFFLAGS+=	-g
131.endif
132
133.if defined(FIRMWS)
134.if !exists(@)
135${KMOD:S/$/.c/}: @
136.else
137${KMOD:S/$/.c/}: @/tools/fw_stub.awk
138.endif
139	${AWK} -f @/tools/fw_stub.awk ${FIRMWS} -m${KMOD} -c${KMOD:S/$/.c/g} \
140	    ${FIRMWARE_LICENSE:C/.+/-l/}${FIRMWARE_LICENSE}
141
142SRCS+=	${KMOD:S/$/.c/}
143CLEANFILES+=	${KMOD:S/$/.c/}
144
145.for _firmw in ${FIRMWS}
146${_firmw:C/\:.*$/.fwo/}:	${_firmw:C/\:.*$//}
147	@${ECHO} ${_firmw:C/\:.*$//} ${.ALLSRC:M*${_firmw:C/\:.*$//}}
148	@if [ -e ${_firmw:C/\:.*$//} ]; then			\
149		${LD} -b binary --no-warn-mismatch ${LDFLAGS}	\
150		    -r -d -o ${.TARGET}	${_firmw:C/\:.*$//};	\
151	else							\
152		ln -s ${.ALLSRC:M*${_firmw:C/\:.*$//}} ${_firmw:C/\:.*$//}; \
153		${LD} -b binary --no-warn-mismatch ${LDFLAGS}	\
154		    -r -d -o ${.TARGET}	${_firmw:C/\:.*$//};	\
155		rm ${_firmw:C/\:.*$//};				\
156	fi
157
158OBJS+=	${_firmw:C/\:.*$/.fwo/}
159.endfor
160.endif
161
162OBJS+=	${SRCS:N*.h:R:S/$/.o/g}
163
164.if !defined(PROG)
165PROG=	${KMOD}.ko
166.endif
167
168.if !defined(DEBUG_FLAGS)
169FULLPROG=	${PROG}
170.else
171FULLPROG=	${PROG}.debug
172${PROG}: ${FULLPROG} ${PROG}.symbols
173	${OBJCOPY} --strip-debug --add-gnu-debuglink=${PROG}.symbols\
174	    ${FULLPROG} ${.TARGET}
175${PROG}.symbols: ${FULLPROG}
176	${OBJCOPY} --only-keep-debug ${FULLPROG} ${.TARGET}
177.endif
178
179.if ${__KLD_SHARED} == yes
180${FULLPROG}: ${KMOD}.kld
181	${LD} -Bshareable ${LDFLAGS} -o ${.TARGET} ${KMOD}.kld
182.if !defined(DEBUG_FLAGS)
183	${OBJCOPY} --strip-debug ${.TARGET}
184.endif
185.endif
186
187EXPORT_SYMS?=	NO
188.if ${EXPORT_SYMS} != YES
189CLEANFILES+=	export_syms
190.endif
191
192.if ${__KLD_SHARED} == yes
193${KMOD}.kld: ${OBJS}
194.else
195${FULLPROG}: ${OBJS}
196.endif
197	${LD} ${LDFLAGS} -r -d -o ${.TARGET} ${OBJS}
198.if ${MK_CTF} != "no"
199	${CTFMERGE} ${CTFFLAGS} -o ${.TARGET} ${OBJS}
200.endif
201.if defined(EXPORT_SYMS)
202.if ${EXPORT_SYMS} != YES
203.if ${EXPORT_SYMS} == NO
204	:> export_syms
205.elif !exists(${.CURDIR}/${EXPORT_SYMS})
206	echo ${EXPORT_SYMS} > export_syms
207.else
208	grep -v '^#' < ${EXPORT_SYMS} > export_syms
209.endif
210	awk -f ${SYSDIR}/conf/kmod_syms.awk ${.TARGET} \
211	    export_syms | xargs -J% ${OBJCOPY} % ${.TARGET}
212.endif
213.endif
214.if !defined(DEBUG_FLAGS) && ${__KLD_SHARED} == no
215	${OBJCOPY} --strip-debug ${.TARGET}
216.endif
217
218_ILINKS=@ machine
219.if ${MACHINE} != ${MACHINE_CPUARCH}
220_ILINKS+=${MACHINE_CPUARCH}
221.endif
222.if ${MACHINE_CPUARCH} == "i386" || ${MACHINE_CPUARCH} == "amd64"
223_ILINKS+=x86
224.endif
225
226all: objwarn ${PROG}
227
228beforedepend: ${_ILINKS}
229
230# Ensure that the links exist without depending on it when it exists which
231# causes all the modules to be rebuilt when the directory pointed to changes.
232.for _link in ${_ILINKS}
233.if !exists(${.OBJDIR}/${_link})
234${OBJS}: ${.OBJDIR}/${_link}
235.endif
236.endfor
237
238# Search for kernel source tree in standard places.
239.for _dir in ${.CURDIR}/../.. ${.CURDIR}/../../.. /sys /usr/src/sys
240.if !defined(SYSDIR) && exists(${_dir}/kern/)
241SYSDIR=	${_dir}
242.endif
243.endfor
244.if !defined(SYSDIR) || !exists(${SYSDIR}/kern/)
245.error "can't find kernel source tree"
246.endif
247
248.for _link in ${_ILINKS}
249.PHONY: ${_link}
250${_link}: ${.OBJDIR}/${_link}
251
252${.OBJDIR}/${_link}:
253	@case ${.TARGET:T} in \
254	machine) \
255		path=${SYSDIR}/${MACHINE}/include ;; \
256	@) \
257		path=${SYSDIR} ;; \
258	*) \
259		path=${SYSDIR}/${.TARGET:T}/include ;; \
260	esac ; \
261	path=`(cd $$path && /bin/pwd)` ; \
262	${ECHO} ${.TARGET:T} "->" $$path ; \
263	ln -sf $$path ${.TARGET:T}
264.endfor
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
339MFILES?= dev/acpica/acpi_if.m dev/acpi_support/acpi_wmi_if.m \
340	dev/agp/agp_if.m dev/ata/ata_if.m dev/eisa/eisa_if.m \
341	dev/fb/fb_if.m dev/gpio/gpio_if.m dev/gpio/gpiobus_if.m \
342	dev/iicbus/iicbb_if.m dev/iicbus/iicbus_if.m \
343	dev/mmc/mmcbr_if.m dev/mmc/mmcbus_if.m \
344	dev/mii/miibus_if.m dev/mvs/mvs_if.m dev/ofw/ofw_bus_if.m \
345	dev/pccard/card_if.m dev/pccard/power_if.m dev/pci/pci_if.m \
346	dev/pci/pcib_if.m dev/ppbus/ppbus_if.m \
347	dev/sdhci/sdhci_if.m dev/smbus/smbus_if.m dev/spibus/spibus_if.m \
348	dev/sound/pci/hda/hdac_if.m \
349	dev/sound/pcm/ac97_if.m dev/sound/pcm/channel_if.m \
350	dev/sound/pcm/feeder_if.m dev/sound/pcm/mixer_if.m \
351	dev/sound/midi/mpu_if.m dev/sound/midi/mpufoi_if.m \
352	dev/sound/midi/synth_if.m dev/usb/usb_if.m isa/isa_if.m \
353	kern/bus_if.m kern/clock_if.m \
354	kern/cpufreq_if.m kern/device_if.m kern/serdev_if.m \
355	libkern/iconv_converter_if.m opencrypto/cryptodev_if.m \
356	pc98/pc98/canbus_if.m dev/etherswitch/mdio_if.m
357
358.for _srcsrc in ${MFILES}
359.for _ext in c h
360.for _src in ${SRCS:M${_srcsrc:T:R}.${_ext}}
361CLEANFILES+=	${_src}
362.if !target(${_src})
363.if !exists(@)
364${_src}: @
365.else
366${_src}: @/tools/makeobjops.awk @/${_srcsrc}
367.endif
368	${AWK} -f @/tools/makeobjops.awk @/${_srcsrc} -${_ext}
369.endif
370.endfor # _src
371.endfor # _ext
372.endfor # _srcsrc
373
374.if !empty(SRCS:Mvnode_if.c)
375CLEANFILES+=	vnode_if.c
376.if !exists(@)
377vnode_if.c: @
378.else
379vnode_if.c: @/tools/vnode_if.awk @/kern/vnode_if.src
380.endif
381	${AWK} -f @/tools/vnode_if.awk @/kern/vnode_if.src -c
382.endif
383
384.if !empty(SRCS:Mvnode_if.h)
385CLEANFILES+=	vnode_if.h vnode_if_newproto.h vnode_if_typedef.h
386.if !exists(@)
387vnode_if.h vnode_if_newproto.h vnode_if_typedef.h: @
388.else
389vnode_if.h vnode_if_newproto.h vnode_if_typedef.h: @/tools/vnode_if.awk \
390    @/kern/vnode_if.src
391.endif
392vnode_if.h: vnode_if_newproto.h vnode_if_typedef.h
393	${AWK} -f @/tools/vnode_if.awk @/kern/vnode_if.src -h
394vnode_if_newproto.h:
395	${AWK} -f @/tools/vnode_if.awk @/kern/vnode_if.src -p
396vnode_if_typedef.h:
397	${AWK} -f @/tools/vnode_if.awk @/kern/vnode_if.src -q
398.endif
399
400.for _i in mii pccard
401.if !empty(SRCS:M${_i}devs.h)
402CLEANFILES+=	${_i}devs.h
403.if !exists(@)
404${_i}devs.h: @
405.else
406${_i}devs.h: @/tools/${_i}devs2h.awk @/dev/${_i}/${_i}devs
407.endif
408	${AWK} -f @/tools/${_i}devs2h.awk @/dev/${_i}/${_i}devs
409.endif
410.endfor # _i
411
412.if !empty(SRCS:Musbdevs.h)
413CLEANFILES+=	usbdevs.h
414.if !exists(@)
415usbdevs.h: @
416.else
417usbdevs.h: @/tools/usbdevs2h.awk @/dev/usb/usbdevs
418.endif
419	${AWK} -f @/tools/usbdevs2h.awk @/dev/usb/usbdevs -h
420.endif
421
422.if !empty(SRCS:Musbdevs_data.h)
423CLEANFILES+=	usbdevs_data.h
424.if !exists(@)
425usbdevs_data.h: @
426.else
427usbdevs_data.h: @/tools/usbdevs2h.awk @/dev/usb/usbdevs
428.endif
429	${AWK} -f @/tools/usbdevs2h.awk @/dev/usb/usbdevs -d
430.endif
431
432.if !empty(SRCS:Macpi_quirks.h)
433CLEANFILES+=	acpi_quirks.h
434.if !exists(@)
435acpi_quirks.h: @
436.else
437acpi_quirks.h: @/tools/acpi_quirks2h.awk @/dev/acpica/acpi_quirks
438.endif
439	${AWK} -f @/tools/acpi_quirks2h.awk @/dev/acpica/acpi_quirks
440.endif
441
442.if !empty(SRCS:Massym.s)
443CLEANFILES+=	assym.s genassym.o
444assym.s: genassym.o
445.if defined(KERNBUILDDIR)
446genassym.o: opt_global.h
447.endif
448.if !exists(@)
449assym.s: @
450.else
451assym.s: @/kern/genassym.sh
452.endif
453	sh @/kern/genassym.sh genassym.o > ${.TARGET}
454.if exists(@)
455genassym.o: @/${MACHINE_CPUARCH}/${MACHINE_CPUARCH}/genassym.c
456.endif
457genassym.o: @ machine ${SRCS:Mopt_*.h}
458	${CC} -c ${CFLAGS:N-fno-common} \
459	    @/${MACHINE_CPUARCH}/${MACHINE_CPUARCH}/genassym.c
460.endif
461
462lint: ${SRCS}
463	${LINT} ${LINTKERNFLAGS} ${CFLAGS:M-[DILU]*} ${.ALLSRC:M*.c}
464
465.if defined(KERNBUILDDIR)
466${OBJS}: opt_global.h
467.endif
468
469.include <bsd.dep.mk>
470
471cleandepend: cleanilinks
472# .depend needs include links so we remove them only together.
473cleanilinks:
474	rm -f ${_ILINKS}
475
476.if !exists(${.OBJDIR}/${DEPENDFILE})
477${OBJS}: ${SRCS:M*.h}
478.endif
479
480.include <bsd.obj.mk>
481.include "kern.mk"
482