86d97c3152e7059c5ca6da715d8093fb9284b079
[dragonfly.git] / sys / conf / kmod.mk
1 #       From: @(#)bsd.prog.mk   5.26 (Berkeley) 6/25/91
2 # $FreeBSD: src/sys/conf/kmod.mk,v 1.82.2.15 2003/02/10 13:11:50 nyan Exp $
3 # $DragonFly: src/sys/conf/kmod.mk,v 1.35 2008/08/27 16:35:19 hasso Exp $
4 #
5 # The include file <bsd.kmod.mk> handles installing Kernel Loadable Device
6 # drivers (KLD's).
7 #
8 #
9 # +++ variables +++
10 #
11 # CLEANFILES    Additional files to remove for the clean and cleandir targets.
12 #
13 # KMOD          The name of the kernel module to build.
14 #
15 # KMODDIR       Base path for kernel modules (see kld(4)).
16 #               [${DESTKERNDIR}/${DESTMODULESNAME}]
17 #
18 # KMODOWN       KLD owner. [${BINOWN}]
19 #
20 # KMODGRP       KLD group. [${BINGRP}]
21 #
22 # KMODMODE      KLD mode. [${BINMODE}]
23 #
24 # KMODLOAD      Command to load a kernel module [/sbin/kldload]
25 #
26 # KMODUNLOAD    Command to unload a kernel module [/sbin/kldunload]
27 #
28 # PROG          The name of the kernel module to build.
29 #               If not supplied, ${KMOD}.o is used.
30 #
31 # SRCS          List of source files
32 #
33 # DESTKERNDIR   Change the tree where the kernel and the modules get
34 #               installed. [/boot]  ${DESTDIR} changes the root of the tree
35 #               pointed to by ${DESTKERNDIR}.
36 #
37 # MFILES        Optionally a list of interfaces used by the module.
38 #               This file contains a default list of interfaces.
39 #
40 # +++ targets +++
41 #
42 #       install:
43 #               install the kernel module and its manual pages; if the Makefile
44 #               does not itself define the target install, the targets
45 #               beforeinstall and afterinstall may also be used to cause
46 #               actions immediately before and after the install target
47 #               is executed.
48 #
49 #       load:
50 #               Load KLD.
51 #
52 #       unload:
53 #               Unload KLD.
54 #
55 # bsd.obj.mk: clean, cleandir and obj
56 # bsd.dep.mk: cleandepend, depend and tags
57 #
58
59 OBJCOPY?=       objcopy
60 KMODLOAD?=      /sbin/kldload
61 KMODUNLOAD?=    /sbin/kldunload
62
63 KMODDIR?=       ${DESTKERNDIR}/${DESTMODULESNAME}
64 KMODOWN?=       ${BINOWN}
65 KMODGRP?=       ${BINGRP}
66 KMODMODE?=      ${BINMODE}
67
68 .include <bsd.init.mk>
69
70 .SUFFIXES: .out .o .c .cc .cxx .C .y .l .s .S
71
72 CFLAGS+=        ${COPTS} -D_KERNEL ${CWARNFLAGS}
73 CFLAGS+=        -DKLD_MODULE
74
75 # Don't use any standard include directories.
76 # Since -nostdinc will annull any previous -I paths, we repeat all
77 # such paths after -nostdinc.  It doesn't seem to be possible to
78 # add to the front of `make' variable.
79 #
80 # Don't use -I- anymore, source-relative includes are desireable.
81 _ICFLAGS:=      ${CFLAGS:M-I*}
82 CFLAGS+=        -nostdinc ${_ICFLAGS}
83
84 # Add -I paths for system headers.  Individual KLD makefiles don't
85 # need any -I paths for this.  Similar defaults for .PATH can't be
86 # set because there are no standard paths for non-headers.
87 #
88 # NOTE!  Traditional architecture paths such as <i386/i386/blah.h>
89 # must run through the "machine_base" softlink using 
90 # <machine_base/i386/blah.h>.  An explicit cross-architecture path must
91 # operate relative to /usr/src/sys using e.g. <arch/i386/i386/blah.h>
92 #
93 CFLAGS+=        -I. -I@
94
95 # Add -I paths for headers in the kernel build directory
96 #
97 .if defined(BUILDING_WITH_KERNEL)
98 CFLAGS+=        -I${BUILDING_WITH_KERNEL}
99 _MACHINE_FWD=   ${BUILDING_WITH_KERNEL}
100 .else
101 .if defined(MAKEOBJDIRPREFIX)
102 _MACHINE_FWD=   ${MAKEOBJDIRPREFIX}/${SYSDIR}/forwarder_${MACHINE_ARCH}
103 .else
104 _MACHINE_FWD=   ${.OBJDIR}/forwarder_${MACHINE_ARCH}
105 CLEANDIRS+=     ${_MACHINE_FWD}
106 .endif
107 .endif
108 CFLAGS+=        -I${_MACHINE_FWD}/include
109 .include "kern.fwd.mk"
110
111 # Add a -I path to standard headers like <stddef.h>.  Use a relative
112 # path to src/include if possible.  If the @ symlink hasn't been built
113 # yet, then we can't tell if the relative path exists.  Add both the
114 # potential relative path and an absolute path in that case.
115 .if exists(@)
116 .if exists(@/../include)
117 CFLAGS+=        -I@/../include
118 .else
119 CFLAGS+=        -I${DESTDIR}/usr/include
120 .endif
121 .else # !@
122 CFLAGS+=        -I@/../include -I${DESTDIR}/usr/include
123 .endif # @
124
125 .if defined(BUILDING_WITH_KERNEL) && \
126     exists(${BUILDING_WITH_KERNEL}/opt_global.h)
127 CFLAGS+=        -include ${BUILDING_WITH_KERNEL}/opt_global.h
128 .endif
129
130 CFLAGS+=        ${DEBUG_FLAGS}
131 .if ${MACHINE_ARCH} == x86_64
132 CFLAGS+=        -fno-omit-frame-pointer
133 .endif
134
135 .include <bsd.patch.mk>
136
137 OBJS+=  ${SRCS:N*.h:N*.patch:R:S/$/.o/g}
138
139 .if !defined(PROG)
140 PROG=   ${KMOD}.ko
141 .endif
142
143 .if ${MACHINE_ARCH} != x86_64
144 ${PROG}: ${KMOD}.kld
145         ${LD} -Bshareable ${LDFLAGS} -o ${.TARGET} ${KMOD}.kld
146 .endif
147
148 .if ${MACHINE_ARCH} != x86_64
149 ${KMOD}.kld: ${OBJS}
150         ${LD} ${LDFLAGS} -r -o ${.TARGET} ${OBJS}
151 .else
152 ${PROG}: ${OBJS}
153         ${LD} ${LDFLAGS} -r -d -o ${.TARGET} ${OBJS}
154 .endif
155
156 # links to platform and cpu architecture include files.  If we are
157 # building with a kernel these already exist in the kernel build dir.
158 # '@' is a link to the system source.
159 .if defined(BUILDING_WITH_KERNEL)
160 _ILINKS=@
161 .else
162 _ILINKS=@ machine_base machine cpu_base cpu
163 .endif
164
165 .if defined(ARCH)
166 _ILINKS+=${ARCH}
167 .endif
168
169 all: objwarn fwheaders ${PROG}
170
171 beforedepend: fwheaders
172 fwheaders: ${_ILINKS} ${FORWARD_HEADERS_COOKIE}
173 # Ensure that the links exist without depending on it when it exists which
174 # causes all the modules to be rebuilt when the directory pointed to changes.
175 .for _link in ${_ILINKS}
176 .if !exists(${.OBJDIR}/${_link})
177 ${OBJS}: ${_link}
178 .endif
179 .endfor
180
181 # Search for kernel source tree in standard places.
182 .for _dir in ${.CURDIR}/../.. ${.CURDIR}/../../.. ${.CURDIR}/../../../.. /sys /usr/src/sys
183 .if !defined(SYSDIR) && exists(${_dir}/kern/)
184 SYSDIR= ${_dir}
185 .endif
186 .endfor
187 .if !defined(SYSDIR) || !exists(${SYSDIR}/kern)
188 .error "can't find kernel source tree"
189 .endif
190 S=      ${SYSDIR}
191
192 #       path=`(cd $$path && /bin/pwd)` ; 
193
194 ${_ILINKS}:
195         @case ${.TARGET} in \
196         machine) \
197                 path=${SYSDIR}/platform/${MACHINE_PLATFORM}/include ;; \
198         machine_base) \
199                 path=${SYSDIR}/platform/${MACHINE_PLATFORM} ;; \
200         cpu) \
201                 path=${SYSDIR}/cpu/${MACHINE_ARCH}/include ;; \
202         cpu_base) \
203                 path=${SYSDIR}/cpu/${MACHINE_ARCH} ;; \
204         @) \
205                 path=${SYSDIR} ;; \
206         arch_*) \
207                 path=${.CURDIR}/${MACHINE_ARCH} ;; \
208         esac ; \
209         ${ECHO} ${.TARGET} "->" $$path ; \
210         ${LN} -s $$path ${.TARGET}
211
212 CLEANFILES+= ${PROG} ${KMOD}.kld ${OBJS} ${_ILINKS} symb.tmp tmp.o
213
214 .if !target(install)
215
216 _INSTALLFLAGS:= ${INSTALLFLAGS}
217 .for ie in ${INSTALLFLAGS_EDIT}
218 _INSTALLFLAGS:= ${_INSTALLFLAGS${ie}}
219 .endfor
220
221 .if !target(realinstall)
222 realinstall: _kmodinstall
223 .ORDER: beforeinstall _kmodinstall
224 _kmodinstall:
225 .if defined(INSTALLSTRIPPEDMODULES)
226         ${INSTALL} -o ${KMODOWN} -g ${KMODGRP} -m ${KMODMODE} \
227             ${_INSTALLFLAGS} ${PROG} ${DESTDIR}${KMODDIR}
228         ${OBJCOPY} --strip-debug ${DESTDIR}${KMODDIR}/${PROG}
229 .else
230         ${INSTALL} -o ${KMODOWN} -g ${KMODGRP} -m ${KMODMODE} \
231             ${_INSTALLFLAGS} ${PROG} ${DESTDIR}${KMODDIR}
232 .endif
233 .endif # !target(realinstall)
234
235 .include <bsd.links.mk>
236
237 .endif # !target(install)
238
239 .if !target(load)
240 load:   ${PROG}
241         ${KMODLOAD} -v ./${KMOD}.ko
242 .endif
243
244 .if !target(unload)
245 unload:
246         ${KMODUNLOAD} -v ${KMOD}
247 .endif
248
249 .for _src in ${SRCS:Mopt_*.h} ${SRCS:Muse_*.h}
250 CLEANFILES+=    ${_src}
251 .if !target(${_src})
252 .if defined(BUILDING_WITH_KERNEL) && exists(${BUILDING_WITH_KERNEL}/${_src})
253 ${_src}: ${BUILDING_WITH_KERNEL}/${_src}
254 # we do not have to copy these files any more, the kernel build
255 # directory is included in the path now.
256 #       cp ${BUILDING_WITH_KERNEL}/${_src} ${.TARGET}
257 .else
258 ${_src}:
259         touch ${.TARGET}
260 .endif  # BUILDING_WITH_KERNEL
261 .endif
262 .endfor
263
264 MFILES?= kern/bus_if.m kern/device_if.m bus/iicbus/iicbb_if.m \
265     bus/iicbus/iicbus_if.m bus/isa/isa_if.m dev/netif/mii_layer/miibus_if.m \
266     bus/pccard/card_if.m bus/pccard/power_if.m bus/pci/pci_if.m \
267     bus/pci/pcib_if.m \
268     bus/ppbus/ppbus_if.m bus/smbus/smbus_if.m bus/usb/usb_if.m \
269     dev/acpica5/acpi_if.m dev/disk/nata/ata_if.m \
270     dev/sound/pcm/ac97_if.m dev/sound/pcm/channel_if.m \
271     dev/sound/pcm/feeder_if.m dev/sound/pcm/mixer_if.m \
272     libiconv/iconv_converter_if.m dev/agp/agp_if.m opencrypto/cryptodev_if.m
273
274 .for _srcsrc in ${MFILES}
275 .for _ext in c h
276 .for _src in ${SRCS:M${_srcsrc:T:R}.${_ext}}
277 CLEANFILES+=    ${_src}
278 .if !target(${_src})
279 ${_src}: @
280 .if exists(@)
281 ${_src}: @/tools/makeobjops.awk @/${_srcsrc}
282 .endif
283
284 .if defined(BUILDING_WITH_KERNEL) && \
285     exists(${BUILDING_WITH_KERNEL}/${_src})
286 .else
287         awk -f @/tools/makeobjops.awk -- -${_ext} @/${_srcsrc}
288 .endif
289 .endif
290 .endfor # _src
291 .endfor # _ext
292 .endfor # _srcsrc
293
294 #.for _ext in c h
295 #.if ${SRCS:Mvnode_if.${_ext}} != ""
296 #CLEANFILES+=   vnode_if.${_ext}
297 #vnode_if.${_ext}: @
298 #.if exists(@)
299 #vnode_if.${_ext}: @/tools/vnode_if.awk @/kern/vnode_if.src
300 #.endif
301 #       awk -f @/tools/vnode_if.awk -- -${_ext} @/kern/vnode_if.src
302 #.endif
303 #.endfor
304
305 regress:
306
307 .include <bsd.dep.mk>
308
309 .if !exists(${DEPENDFILE})
310 ${OBJS}: ${SRCS:M*.h}
311 .endif
312
313 .include <bsd.obj.mk>
314 .include "bsd.kern.mk"
315
316 # Behaves like MODULE_OVERRIDE
317 .if defined(KLD_DEPS)
318 all: _kdeps_all
319 _kdeps_all: @
320 .for _mdep in ${KLD_DEPS}
321         cd ${SYSDIR}/${_mdep} && make all
322 .endfor
323 depend: _kdeps_depend
324 _kdeps_depend: @
325 .for _mdep in ${KLD_DEPS}
326         cd ${SYSDIR}/${_mdep} && make depend
327 .endfor
328 install: _kdeps_install
329 _kdeps_install: @
330 .for _mdep in ${KLD_DEPS}
331         cd ${SYSDIR}/${_mdep} && make install
332 .endfor
333 clean: _kdeps_clean
334 _kdeps_clean: @
335 .for _mdep in ${KLD_DEPS}
336         cd ${SYSDIR}/${_mdep} && make clean
337 .endfor
338 .endif