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