kmod.mk revision 265841
1#	From: @(#)bsd.prog.mk	5.26 (Berkeley) 6/25/91
2# $FreeBSD: head/sys/conf/kmod.mk 265841 2014-05-10 16:39:15Z 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# MFILES	Optionally a list of interfaces used by the module.
32#		This file contains a default list of interfaces.
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
63AWK?=		awk
64KMODLOAD?=	/sbin/kldload
65KMODUNLOAD?=	/sbin/kldunload
66OBJCOPY?=	objcopy
67
68# Note: we're really bsd.kmod.mk, so we have to allow src.opts.mk to be
69# optional. Include it if we can so we can get /etc/src.conf changes,
70# if we're in the tree. If we can't include it that's OK. kern.opts.mk
71# has all the kernel options in it, and should be included after src.opts.mk
72# so it picks everything up.
73.sinclude <src.opts.mk>
74.include <bsd.init.mk>
75.include <bsd.compiler.mk>
76.include "kern.opts.mk"
77
78.SUFFIXES: .out .o .c .cc .cxx .C .y .l .s .S
79
80# amd64 and mips use direct linking for kmod, all others use shared binaries
81.if ${MACHINE_CPUARCH} != amd64 && ${MACHINE_CPUARCH} != mips
82__KLD_SHARED=yes
83.else
84__KLD_SHARED=no
85.endif
86
87.if !empty(CFLAGS:M-O[23s]) && empty(CFLAGS:M-fno-strict-aliasing)
88CFLAGS+=	-fno-strict-aliasing
89.endif
90WERROR?=	-Werror
91CFLAGS+=	${WERROR}
92CFLAGS+=	-D_KERNEL
93CFLAGS+=	-DKLD_MODULE
94
95# Don't use any standard or source-relative include directories.
96CSTD=		c99
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@
107
108# Add -I path for altq headers as they are included via net/if_var.h
109# for example.
110CFLAGS+=	-I@/contrib/altq
111
112CFLAGS.gcc+=	-finline-limit=${INLINE_LIMIT}
113CFLAGS.gcc+= --param inline-unit-growth=100
114CFLAGS.gcc+= --param large-function-growth=1000
115
116# Disallow common variables, and if we end up with commons from
117# somewhere unexpected, allocate storage for them in the module itself.
118CFLAGS+=	-fno-common
119LDFLAGS+=	-d -warn-common
120
121CFLAGS+=	${DEBUG_FLAGS}
122.if ${MACHINE_CPUARCH} == amd64
123CFLAGS+=	-fno-omit-frame-pointer -mno-omit-leaf-frame-pointer
124.endif
125
126.if ${MACHINE_CPUARCH} == powerpc
127CFLAGS+=	-mlongcall -fno-omit-frame-pointer
128.endif
129
130.if ${MACHINE_CPUARCH} == mips
131CFLAGS+=	-G0 -fno-pic -mno-abicalls -mlong-calls
132.endif
133
134.if defined(DEBUG) || defined(DEBUG_FLAGS)
135CTFFLAGS+=	-g
136.endif
137
138.if defined(FIRMWS)
139.if !exists(@)
140${KMOD:S/$/.c/}: @
141.else
142${KMOD:S/$/.c/}: @/tools/fw_stub.awk
143.endif
144	${AWK} -f @/tools/fw_stub.awk ${FIRMWS} -m${KMOD} -c${KMOD:S/$/.c/g} \
145	    ${FIRMWARE_LICENSE:C/.+/-l/}${FIRMWARE_LICENSE}
146
147SRCS+=	${KMOD:S/$/.c/}
148CLEANFILES+=	${KMOD:S/$/.c/}
149
150.for _firmw in ${FIRMWS}
151${_firmw:C/\:.*$/.fwo/}:	${_firmw:C/\:.*$//}
152	@${ECHO} ${_firmw:C/\:.*$//} ${.ALLSRC:M*${_firmw:C/\:.*$//}}
153	@if [ -e ${_firmw:C/\:.*$//} ]; then			\
154		${LD} -b binary --no-warn-mismatch ${LDFLAGS}	\
155		    -r -d -o ${.TARGET}	${_firmw:C/\:.*$//};	\
156	else							\
157		ln -s ${.ALLSRC:M*${_firmw:C/\:.*$//}} ${_firmw:C/\:.*$//}; \
158		${LD} -b binary --no-warn-mismatch ${LDFLAGS}	\
159		    -r -d -o ${.TARGET}	${_firmw:C/\:.*$//};	\
160		rm ${_firmw:C/\:.*$//};				\
161	fi
162
163OBJS+=	${_firmw:C/\:.*$/.fwo/}
164.endfor
165.endif
166
167OBJS+=	${SRCS:N*.h:R:S/$/.o/g}
168
169.if !defined(PROG)
170PROG=	${KMOD}.ko
171.endif
172
173.if !defined(DEBUG_FLAGS)
174FULLPROG=	${PROG}
175.else
176FULLPROG=	${PROG}.debug
177${PROG}: ${FULLPROG} ${PROG}.symbols
178	${OBJCOPY} --strip-debug --add-gnu-debuglink=${PROG}.symbols\
179	    ${FULLPROG} ${.TARGET}
180${PROG}.symbols: ${FULLPROG}
181	${OBJCOPY} --only-keep-debug ${FULLPROG} ${.TARGET}
182.endif
183
184.if ${__KLD_SHARED} == yes
185${FULLPROG}: ${KMOD}.kld
186	${LD} -Bshareable ${LDFLAGS} -o ${.TARGET} ${KMOD}.kld
187.if !defined(DEBUG_FLAGS)
188	${OBJCOPY} --strip-debug ${.TARGET}
189.endif
190.endif
191
192EXPORT_SYMS?=	NO
193.if ${EXPORT_SYMS} != YES
194CLEANFILES+=	export_syms
195.endif
196
197.if ${__KLD_SHARED} == yes
198${KMOD}.kld: ${OBJS}
199.else
200${FULLPROG}: ${OBJS}
201.endif
202	${LD} ${LDFLAGS} -r -d -o ${.TARGET} ${OBJS}
203.if ${MK_CTF} != "no"
204	${CTFMERGE} ${CTFFLAGS} -o ${.TARGET} ${OBJS}
205.endif
206.if defined(EXPORT_SYMS)
207.if ${EXPORT_SYMS} != YES
208.if ${EXPORT_SYMS} == NO
209	:> export_syms
210.elif !exists(${.CURDIR}/${EXPORT_SYMS})
211	echo ${EXPORT_SYMS} > export_syms
212.else
213	grep -v '^#' < ${EXPORT_SYMS} > export_syms
214.endif
215	awk -f ${SYSDIR}/conf/kmod_syms.awk ${.TARGET} \
216	    export_syms | xargs -J% ${OBJCOPY} % ${.TARGET}
217.endif
218.endif
219.if !defined(DEBUG_FLAGS) && ${__KLD_SHARED} == no
220	${OBJCOPY} --strip-debug ${.TARGET}
221.endif
222
223_ILINKS=@ machine
224.if ${MACHINE} != ${MACHINE_CPUARCH}
225_ILINKS+=${MACHINE_CPUARCH}
226.endif
227.if ${MACHINE_CPUARCH} == "i386" || ${MACHINE_CPUARCH} == "amd64"
228_ILINKS+=x86
229.endif
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}: ${.OBJDIR}/${_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.for _link in ${_ILINKS}
254.PHONY: ${_link}
255${_link}: ${.OBJDIR}/${_link}
256
257${.OBJDIR}/${_link}:
258	@case ${.TARGET:T} in \
259	machine) \
260		path=${SYSDIR}/${MACHINE}/include ;; \
261	@) \
262		path=${SYSDIR} ;; \
263	*) \
264		path=${SYSDIR}/${.TARGET:T}/include ;; \
265	esac ; \
266	path=`(cd $$path && /bin/pwd)` ; \
267	${ECHO} ${.TARGET:T} "->" $$path ; \
268	ln -sf $$path ${.TARGET:T}
269.endfor
270
271CLEANFILES+= ${PROG} ${KMOD}.kld ${OBJS}
272
273.if defined(DEBUG_FLAGS)
274CLEANFILES+= ${FULLPROG} ${PROG}.symbols
275.endif
276
277.if !target(install)
278
279_INSTALLFLAGS:=	${INSTALLFLAGS}
280.for ie in ${INSTALLFLAGS_EDIT}
281_INSTALLFLAGS:=	${_INSTALLFLAGS${ie}}
282.endfor
283
284.if !target(realinstall)
285realinstall: _kmodinstall
286.ORDER: beforeinstall _kmodinstall
287_kmodinstall:
288	${INSTALL} -o ${KMODOWN} -g ${KMODGRP} -m ${KMODMODE} \
289	    ${_INSTALLFLAGS} ${PROG} ${DESTDIR}${KMODDIR}
290.if defined(DEBUG_FLAGS) && !defined(INSTALL_NODEBUG) && ${MK_KERNEL_SYMBOLS} != "no"
291	${INSTALL} -o ${KMODOWN} -g ${KMODGRP} -m ${KMODMODE} \
292	    ${_INSTALLFLAGS} ${PROG}.symbols ${DESTDIR}${KMODDIR}
293.endif
294
295.include <bsd.links.mk>
296
297.if !defined(NO_XREF)
298afterinstall: _kldxref
299.ORDER: realinstall _kldxref
300.ORDER: _installlinks _kldxref
301_kldxref:
302	@if type kldxref >/dev/null 2>&1; then \
303		${ECHO} kldxref ${DESTDIR}${KMODDIR}; \
304		kldxref ${DESTDIR}${KMODDIR}; \
305	fi
306.endif
307.endif # !target(realinstall)
308
309.endif # !target(install)
310
311.if !target(load)
312load: ${PROG}
313	${KMODLOAD} -v ${.OBJDIR}/${PROG}
314.endif
315
316.if !target(unload)
317unload:
318	${KMODUNLOAD} -v ${PROG}
319.endif
320
321.if defined(KERNBUILDDIR)
322.PATH: ${KERNBUILDDIR}
323CFLAGS+=	-I${KERNBUILDDIR}
324.for _src in ${SRCS:Mopt_*.h}
325CLEANFILES+=	${_src}
326.if !target(${_src})
327${_src}:
328	ln -sf ${KERNBUILDDIR}/${_src} ${.TARGET}
329.endif
330.endfor
331.else
332.for _src in ${SRCS:Mopt_*.h}
333CLEANFILES+=	${_src}
334.if !target(${_src})
335${_src}:
336	:> ${.TARGET}
337.endif
338.endfor
339.endif
340
341# Respect configuration-specific C flags.
342CFLAGS+=	${CONF_CFLAGS}
343
344MFILES?= dev/acpica/acpi_if.m dev/acpi_support/acpi_wmi_if.m \
345	dev/agp/agp_if.m dev/ata/ata_if.m dev/eisa/eisa_if.m \
346	dev/fb/fb_if.m dev/gpio/gpio_if.m dev/gpio/gpiobus_if.m \
347	dev/iicbus/iicbb_if.m dev/iicbus/iicbus_if.m \
348	dev/mmc/mmcbr_if.m dev/mmc/mmcbus_if.m \
349	dev/mii/miibus_if.m dev/mvs/mvs_if.m dev/ofw/ofw_bus_if.m \
350	dev/pccard/card_if.m dev/pccard/power_if.m dev/pci/pci_if.m \
351	dev/pci/pcib_if.m dev/ppbus/ppbus_if.m \
352	dev/sdhci/sdhci_if.m dev/smbus/smbus_if.m dev/spibus/spibus_if.m \
353	dev/sound/pci/hda/hdac_if.m \
354	dev/sound/pcm/ac97_if.m dev/sound/pcm/channel_if.m \
355	dev/sound/pcm/feeder_if.m dev/sound/pcm/mixer_if.m \
356	dev/sound/midi/mpu_if.m dev/sound/midi/mpufoi_if.m \
357	dev/sound/midi/synth_if.m dev/usb/usb_if.m isa/isa_if.m \
358	kern/bus_if.m kern/clock_if.m \
359	kern/cpufreq_if.m kern/device_if.m kern/serdev_if.m \
360	libkern/iconv_converter_if.m opencrypto/cryptodev_if.m \
361	pc98/pc98/canbus_if.m dev/etherswitch/mdio_if.m
362
363.for _srcsrc in ${MFILES}
364.for _ext in c h
365.for _src in ${SRCS:M${_srcsrc:T:R}.${_ext}}
366CLEANFILES+=	${_src}
367.if !target(${_src})
368.if !exists(@)
369${_src}: @
370.else
371${_src}: @/tools/makeobjops.awk @/${_srcsrc}
372.endif
373	${AWK} -f @/tools/makeobjops.awk @/${_srcsrc} -${_ext}
374.endif
375.endfor # _src
376.endfor # _ext
377.endfor # _srcsrc
378
379.if !empty(SRCS:Mvnode_if.c)
380CLEANFILES+=	vnode_if.c
381.if !exists(@)
382vnode_if.c: @
383.else
384vnode_if.c: @/tools/vnode_if.awk @/kern/vnode_if.src
385.endif
386	${AWK} -f @/tools/vnode_if.awk @/kern/vnode_if.src -c
387.endif
388
389.if !empty(SRCS:Mvnode_if.h)
390CLEANFILES+=	vnode_if.h vnode_if_newproto.h vnode_if_typedef.h
391.if !exists(@)
392vnode_if.h vnode_if_newproto.h vnode_if_typedef.h: @
393.else
394vnode_if.h vnode_if_newproto.h vnode_if_typedef.h: @/tools/vnode_if.awk \
395    @/kern/vnode_if.src
396.endif
397vnode_if.h: vnode_if_newproto.h vnode_if_typedef.h
398	${AWK} -f @/tools/vnode_if.awk @/kern/vnode_if.src -h
399vnode_if_newproto.h:
400	${AWK} -f @/tools/vnode_if.awk @/kern/vnode_if.src -p
401vnode_if_typedef.h:
402	${AWK} -f @/tools/vnode_if.awk @/kern/vnode_if.src -q
403.endif
404
405.for _i in mii pccard
406.if !empty(SRCS:M${_i}devs.h)
407CLEANFILES+=	${_i}devs.h
408.if !exists(@)
409${_i}devs.h: @
410.else
411${_i}devs.h: @/tools/${_i}devs2h.awk @/dev/${_i}/${_i}devs
412.endif
413	${AWK} -f @/tools/${_i}devs2h.awk @/dev/${_i}/${_i}devs
414.endif
415.endfor # _i
416
417.if !empty(SRCS:Musbdevs.h)
418CLEANFILES+=	usbdevs.h
419.if !exists(@)
420usbdevs.h: @
421.else
422usbdevs.h: @/tools/usbdevs2h.awk @/dev/usb/usbdevs
423.endif
424	${AWK} -f @/tools/usbdevs2h.awk @/dev/usb/usbdevs -h
425.endif
426
427.if !empty(SRCS:Musbdevs_data.h)
428CLEANFILES+=	usbdevs_data.h
429.if !exists(@)
430usbdevs_data.h: @
431.else
432usbdevs_data.h: @/tools/usbdevs2h.awk @/dev/usb/usbdevs
433.endif
434	${AWK} -f @/tools/usbdevs2h.awk @/dev/usb/usbdevs -d
435.endif
436
437.if !empty(SRCS:Macpi_quirks.h)
438CLEANFILES+=	acpi_quirks.h
439.if !exists(@)
440acpi_quirks.h: @
441.else
442acpi_quirks.h: @/tools/acpi_quirks2h.awk @/dev/acpica/acpi_quirks
443.endif
444	${AWK} -f @/tools/acpi_quirks2h.awk @/dev/acpica/acpi_quirks
445.endif
446
447.if !empty(SRCS:Massym.s)
448CLEANFILES+=	assym.s genassym.o
449assym.s: genassym.o
450.if defined(KERNBUILDDIR)
451genassym.o: opt_global.h
452.endif
453.if !exists(@)
454assym.s: @
455.else
456assym.s: @/kern/genassym.sh
457.endif
458	sh @/kern/genassym.sh genassym.o > ${.TARGET}
459.if exists(@)
460genassym.o: @/${MACHINE_CPUARCH}/${MACHINE_CPUARCH}/genassym.c
461.endif
462genassym.o: @ machine ${SRCS:Mopt_*.h}
463	${CC} -c ${CFLAGS:N-fno-common} \
464	    @/${MACHINE_CPUARCH}/${MACHINE_CPUARCH}/genassym.c
465.endif
466
467lint: ${SRCS}
468	${LINT} ${LINTKERNFLAGS} ${CFLAGS:M-[DILU]*} ${.ALLSRC:M*.c}
469
470.if defined(KERNBUILDDIR)
471${OBJS}: opt_global.h
472.endif
473
474.include <bsd.dep.mk>
475
476cleandepend: cleanilinks
477# .depend needs include links so we remove them only together.
478cleanilinks:
479	rm -f ${_ILINKS}
480
481.if !exists(${.OBJDIR}/${DEPENDFILE})
482${OBJS}: ${SRCS:M*.h}
483.endif
484
485.include <bsd.obj.mk>
486.include "kern.mk"
487