xref: /freebsd/sys/conf/kmod.mk (revision 7562eaabc01a48e6b11d5b558c41e3b92dae5c2d)
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# 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# DESTDIR	The tree where the module gets installed. [not set]
40#
41# +++ targets +++
42#
43# 	install:
44#               install the kernel module; if the Makefile
45#               does not itself define the target install, the targets
46#               beforeinstall and afterinstall may also be used to cause
47#               actions immediately before and after the install target
48#		is executed.
49#
50# 	load:
51#		Load a module.
52#
53# 	unload:
54#		Unload a module.
55#
56
57AWK?=		awk
58KMODLOAD?=	/sbin/kldload
59KMODUNLOAD?=	/sbin/kldunload
60OBJCOPY?=	objcopy
61
62.if defined(KMODDEPS)
63.error "Do not use KMODDEPS on 5.0+; use MODULE_VERSION/MODULE_DEPEND"
64.endif
65
66.include <bsd.init.mk>
67
68.SUFFIXES: .out .o .c .cc .cxx .C .y .l .s .S
69
70.if ${CC} == "icc"
71CFLAGS:=	${CFLAGS:C/(-x[^M^K^W]+)[MKW]+|-x[MKW]+/\1/}
72.endif
73CFLAGS+=	-D_KERNEL
74CFLAGS+=	-DKLD_MODULE
75
76# Don't use any standard or source-relative include directories.
77# Since -nostdinc will annull any previous -I paths, we repeat all
78# such paths after -nostdinc.  It doesn't seem to be possible to
79# add to the front of `make' variable.
80_ICFLAGS:=	${CFLAGS:M-I*}
81.if ${CC} == "icc"
82NOSTDINC=	-X
83.else
84NOSTDINC=	-nostdinc
85.endif
86CFLAGS+=	${NOSTDINC} -I- ${INCLMAGIC} ${_ICFLAGS}
87.if defined(KERNBUILDDIR)
88CFLAGS+=	-include ${KERNBUILDDIR}/opt_global.h
89.endif
90
91# Add -I paths for system headers.  Individual module makefiles don't
92# need any -I paths for this.  Similar defaults for .PATH can't be
93# set because there are no standard paths for non-headers.
94CFLAGS+=	-I. -I@
95
96# Add -I path for altq headers as they are included via net/if_var.h
97# for example.
98CFLAGS+=	-I@/contrib/altq
99
100# Add a -I path to standard headers like <stddef.h>.  Use a relative
101# path to src/include if possible.  If the @ symlink hasn't been built
102# yet, then we can't tell if the relative path exists.  Add both the
103# potential relative path and an absolute path in that case.
104.if exists(@)
105.if exists(@/../include)
106CFLAGS+=	-I@/../include
107.else
108CFLAGS+=	-I${DESTDIR}/usr/include
109.endif
110.else # !@
111CFLAGS+=	-I@/../include -I${DESTDIR}/usr/include
112.endif # @
113
114.if ${CC} != "icc"
115CFLAGS+=	-finline-limit=${INLINE_LIMIT}
116.endif
117
118# Disallow common variables, and if we end up with commons from
119# somewhere unexpected, allocate storage for them in the module itself.
120.if ${CC} != "icc"
121CFLAGS+=	-fno-common
122.endif
123LDFLAGS+=	-d -warn-common
124
125CFLAGS+=	${DEBUG_FLAGS}
126.if ${MACHINE_ARCH} == amd64
127CFLAGS+=	-fno-omit-frame-pointer
128.endif
129
130OBJS+=	${SRCS:N*.h:R:S/$/.o/g}
131
132.if !defined(PROG)
133PROG=	${KMOD}.ko
134.endif
135
136.if !defined(DEBUG_FLAGS)
137FULLPROG=	${PROG}
138.else
139FULLPROG=	${PROG}.debug
140${PROG}: ${FULLPROG}
141	${OBJCOPY} --strip-debug ${FULLPROG} ${PROG}
142.endif
143
144.if ${MACHINE_ARCH} != amd64
145${FULLPROG}: ${KMOD}.kld
146	${LD} -Bshareable ${LDFLAGS} -o ${.TARGET} ${KMOD}.kld
147.if !defined(DEBUG_FLAGS)
148	${OBJCOPY} --strip-debug ${.TARGET}
149.endif
150.endif
151
152EXPORT_SYMS?=	NO
153.if ${EXPORT_SYMS} != YES
154CLEANFILES+=	export_syms
155.endif
156
157.if ${MACHINE_ARCH} != amd64
158${KMOD}.kld: ${OBJS}
159.else
160${FULLPROG}: ${OBJS}
161.endif
162	${LD} ${LDFLAGS} -r -d -o ${.TARGET} ${OBJS}
163.if defined(EXPORT_SYMS)
164.if ${EXPORT_SYMS} != YES
165.if ${EXPORT_SYMS} == NO
166	touch export_syms
167.elif !exists(${.CURDIR}/${EXPORT_SYMS})
168	echo ${EXPORT_SYMS} > export_syms
169.else
170	grep -v '^#' < ${EXPORT_SYMS} > export_syms
171.endif
172	awk -f ${SYSDIR}/conf/kmod_syms.awk ${.TARGET} \
173	    export_syms | xargs -J% ${OBJCOPY} % ${.TARGET}
174.endif
175.endif
176.if !defined(DEBUG_FLAGS) && ${MACHINE_ARCH} == amd64
177	${OBJCOPY} --strip-debug ${.TARGET}
178.endif
179
180_ILINKS=@ machine
181
182all: objwarn ${PROG}
183
184beforedepend: ${_ILINKS}
185
186# Ensure that the links exist without depending on it when it exists which
187# causes all the modules to be rebuilt when the directory pointed to changes.
188.for _link in ${_ILINKS}
189.if !exists(${.OBJDIR}/${_link})
190${OBJS}: ${_link}
191.endif
192.endfor
193
194# Search for kernel source tree in standard places.
195.for _dir in ${.CURDIR}/../.. ${.CURDIR}/../../.. /sys /usr/src/sys
196.if !defined(SYSDIR) && exists(${_dir}/kern/)
197SYSDIR=	${_dir}
198.endif
199.endfor
200.if !defined(SYSDIR) || !exists(${SYSDIR}/kern/)
201.error "can't find kernel source tree"
202.endif
203
204${_ILINKS}:
205	@case ${.TARGET} in \
206	machine) \
207		path=${SYSDIR}/${MACHINE_ARCH}/include ;; \
208	@) \
209		path=${SYSDIR} ;; \
210	esac ; \
211	path=`(cd $$path && /bin/pwd)` ; \
212	${ECHO} ${.TARGET} "->" $$path ; \
213	ln -s $$path ${.TARGET}
214
215CLEANFILES+= ${PROG} ${KMOD}.kld ${OBJS} ${_ILINKS}
216
217.if defined(DEBUG_FLAGS)
218CLEANFILES+= ${FULLPROG}
219.endif
220
221.if !target(install)
222
223_INSTALLFLAGS:=	${INSTALLFLAGS}
224.for ie in ${INSTALLFLAGS_EDIT}
225_INSTALLFLAGS:=	${_INSTALLFLAGS${ie}}
226.endfor
227
228.if !target(install.debug) && defined(DEBUG_FLAGS)
229install.debug:
230	cd ${.CURDIR}; ${MAKE} -DINSTALL_DEBUG install
231.endif
232
233.if !target(realinstall)
234realinstall: _kmodinstall
235.ORDER: beforeinstall _kmodinstall
236_kmodinstall:
237.if defined(DEBUG_FLAGS) && defined(INSTALL_DEBUG)
238	${INSTALL} -o ${KMODOWN} -g ${KMODGRP} -m ${KMODMODE} \
239	    ${_INSTALLFLAGS} ${FULLPROG} ${DESTDIR}${KMODDIR}
240.else
241	${INSTALL} -o ${KMODOWN} -g ${KMODGRP} -m ${KMODMODE} \
242	    ${_INSTALLFLAGS} ${PROG} ${DESTDIR}${KMODDIR}
243
244.include <bsd.links.mk>
245
246.if !defined(NO_XREF)
247afterinstall: _kldxref
248.ORDER: realinstall _kldxref
249.ORDER: _installlinks _kldxref
250_kldxref:
251	@if type kldxref >/dev/null 2>&1; then \
252		${ECHO} kldxref ${DESTDIR}${KMODDIR}; \
253		kldxref ${DESTDIR}${KMODDIR}; \
254	fi
255.endif
256.endif
257.endif !target(realinstall)
258
259.endif !target(install)
260
261.if !target(load)
262load: ${PROG}
263	${KMODLOAD} -v ${.OBJDIR}/${PROG}
264.endif
265
266.if !target(unload)
267unload:
268	${KMODUNLOAD} -v ${PROG}
269.endif
270
271.if defined(KERNBUILDDIR)
272.PATH: ${KERNBUILDDIR}
273CFLAGS+=	-I${KERNBUILDDIR}
274.for _src in ${SRCS:Mopt_*.h}
275CLEANFILES+=	${_src}
276.if !target(${_src})
277${_src}:
278	ln -s ${KERNBUILDDIR}/${_src} ${.TARGET}
279.endif
280.endfor
281.else
282.for _src in ${SRCS:Mopt_*.h}
283CLEANFILES+=	${_src}
284.if !target(${_src})
285${_src}:
286	touch ${.TARGET}
287.endif
288.endfor
289.endif
290
291MFILES?= kern/bus_if.m kern/device_if.m dev/iicbus/iicbb_if.m \
292    dev/iicbus/iicbus_if.m isa/isa_if.m \
293    libkern/iconv_converter_if.m \
294    dev/acpica/acpi_if.m dev/eisa/eisa_if.m dev/mii/miibus_if.m \
295    dev/ofw/ofw_bus_if.m \
296    dev/pccard/card_if.m dev/pccard/power_if.m dev/pci/pci_if.m \
297    dev/pci/pcib_if.m dev/ppbus/ppbus_if.m dev/smbus/smbus_if.m \
298    dev/usb/usb_if.m dev/sound/pcm/ac97_if.m dev/sound/pcm/channel_if.m \
299    dev/sound/pcm/feeder_if.m dev/sound/pcm/mixer_if.m pci/agp_if.m \
300    opencrypto/crypto_if.m pc98/pc98/canbus_if.m dev/uart/uart_if.m \
301    sparc64/pci/ofw_pci_if.m
302
303.for _srcsrc in ${MFILES}
304.for _ext in c h
305.for _src in ${SRCS:M${_srcsrc:T:R}.${_ext}}
306CLEANFILES+=	${_src}
307.if !target(${_src})
308.if !exists(@)
309${_src}: @
310.endif
311.if exists(@)
312${_src}: @/tools/makeobjops.awk @/${_srcsrc}
313.endif
314	${AWK} -f @/tools/makeobjops.awk @/${_srcsrc} -${_ext}
315.endif
316.endfor # _src
317.endfor # _ext
318.endfor # _srcsrc
319
320.for _ext in c h
321.if ${SRCS:Mvnode_if.${_ext}} != ""
322CLEANFILES+=	vnode_if.${_ext}
323.if !exists(@)
324vnode_if.${_ext}: @
325.endif
326.if exists(@)
327vnode_if.${_ext}: @/tools/vnode_if.awk @/kern/vnode_if.src
328.endif
329	${AWK} -f @/tools/vnode_if.awk @/kern/vnode_if.src -${_ext}
330.endif
331.endfor
332
333.for _i in mii pccard usb
334.if ${SRCS:M${_i}devs.h} != ""
335CLEANFILES+=	${_i}devs.h
336_i=		${_i:Musb}
337.if !empty(_i)
338CLEANFILES+=	${_i}devs_data.h
339.endif
340.if !exists(@)
341${_i}devs.h: @
342.endif
343.if exists(@)
344${_i}devs.h: @/tools/${_i}devs2h.awk @/dev/${_i}/${_i}devs
345.endif
346	${AWK} -f @/tools/${_i}devs2h.awk @/dev/${_i}/${_i}devs
347.endif
348.endfor # _i
349
350.if ${SRCS:Macpi_quirks.h} != ""
351CLEANFILES+=	acpi_quirks.h
352.if !exists(@)
353acpi_quirks.h: @
354.endif
355.if exists(@)
356acpi_quirks.h: @/tools/acpi_quirks2h.awk @/dev/acpica/acpi_quirks
357.endif
358	${AWK} -f @/tools/acpi_quirks2h.awk @/dev/acpica/acpi_quirks
359.endif
360
361lint: ${SRCS}
362	${LINT} ${LINTKERNFLAGS} ${CFLAGS:M-[DILU]*} ${.ALLSRC:M*.c}
363
364.include <bsd.dep.mk>
365
366.if !exists(${.OBJDIR}/${DEPENDFILE})
367${OBJS}: ${SRCS:M*.h}
368.endif
369
370.include <bsd.obj.mk>
371.include "kern.mk"
372