Reorganize the way machine architectures are handled. Consolidate the
[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.26 2006/10/22 16:09:19 dillon 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)). [/modules]
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 # NOMAN         KLD does not have a manual page if set.
28 #
29 # PROG          The name of the kernel module to build.
30 #               If not supplied, ${KMOD}.o is used.
31 #
32 # SRCS          List of source files
33 #
34 # KMODDEPS      List of modules which this one is dependant on
35 #
36 # DESTDIR       Change the tree where the module gets installed. [not set]
37 #
38 # MFILES        Optionally a list of interfaces used by the module.
39 #               This file contains a default list of interfaces.
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 # bsd.man.mk: maninstall
59 #
60
61 OBJCOPY?=       objcopy
62 KMODLOAD?=      /sbin/kldload
63 KMODUNLOAD?=    /sbin/kldunload
64
65 .include <bsd.init.mk>
66
67 .SUFFIXES: .out .o .c .cc .cxx .C .y .l .s .S
68
69 CFLAGS+=        ${COPTS} -D_KERNEL ${CWARNFLAGS}
70 CFLAGS+=        -DKLD_MODULE
71
72 # Don't use any standard include directories.
73 # Since -nostdinc will annull any previous -I paths, we repeat all
74 # such paths after -nostdinc.  It doesn't seem to be possible to
75 # add to the front of `make' variable.
76 #
77 # Don't use -I- anymore, source-relative includes are desireable.
78 _ICFLAGS:=      ${CFLAGS:M-I*}
79 CFLAGS+=        -nostdinc ${_ICFLAGS}
80
81 # Add -I paths for system headers.  Individual KLD makefiles don't
82 # need any -I paths for this.  Similar defaults for .PATH can't be
83 # set because there are no standard paths for non-headers.
84 #
85 # We want traditional architecture paths like <i386/i386/blah.h>
86 # to work so we add -I@/arch
87 #
88 CFLAGS+=        -I. -I@ -I@/arch
89
90 # Add a -I path to standard headers like <stddef.h>.  Use a relative
91 # path to src/include if possible.  If the @ symlink hasn't been built
92 # yet, then we can't tell if the relative path exists.  Add both the
93 # potential relative path and an absolute path in that case.
94 .if exists(@)
95 .if exists(@/../include)
96 CFLAGS+=        -I@/../include
97 .else
98 CFLAGS+=        -I${DESTDIR}/usr/include
99 .endif
100 .else # !@
101 CFLAGS+=        -I@/../include -I${DESTDIR}/usr/include
102 .endif # @
103
104 .if defined(BUILDING_WITH_KERNEL) && \
105     exists(${BUILDING_WITH_KERNEL}/opt_global.h)
106 CFLAGS+=        -include ${BUILDING_WITH_KERNEL}/opt_global.h
107 .endif
108
109 CFLAGS+=        ${DEBUG_FLAGS}
110
111 .include <bsd.patch.mk>
112
113 OBJS+=  ${SRCS:N*.h:N*.patch:R:S/$/.o/g}
114
115 .if !defined(PROG)
116 PROG=   ${KMOD}.ko
117 .endif
118
119 ${PROG}: ${KMOD}.kld ${KMODDEPS}
120         ${LD} -Bshareable ${LDFLAGS} -o ${.TARGET} ${KMOD}.kld ${KMODDEPS}
121
122 .if defined(KMODDEPS)
123 .for dep in ${KMODDEPS}
124 CLEANFILES+=    ${dep} __${dep}_hack_dep.c
125
126 ${dep}:
127         touch __${dep}_hack_dep.c
128         ${CC} -shared ${CFLAGS} -o ${dep} __${dep}_hack_dep.c
129 .endfor
130 .endif
131
132 ${KMOD}.kld: ${OBJS}
133         ${LD} ${LDFLAGS} -r -o ${.TARGET} ${OBJS}
134
135 .if !defined(NOMAN)
136 .include <bsd.man.mk>
137 .endif
138
139 _ILINKS=@ arch machine
140 .if defined(ARCH)
141 _ILINKS+=${ARCH}
142 .endif
143
144 all: objwarn ${PROG}
145 .if !defined(NOMAN)
146 all: _manpages
147 .endif
148
149 beforedepend: ${_ILINKS}
150 # Ensure that the links exist without depending on it when it exists which
151 # causes all the modules to be rebuilt when the directory pointed to changes.
152 .for _link in ${_ILINKS}
153 .if !exists(${.OBJDIR}/${_link})
154 ${OBJS}: ${_link}
155 .endif
156 .endfor
157
158 # Search for kernel source tree in standard places.
159 .for _dir in ${.CURDIR}/../.. ${.CURDIR}/../../.. ${.CURDIR}/../../../.. /sys /usr/src/sys
160 .if !defined(SYSDIR) && exists(${_dir}/kern/)
161 SYSDIR= ${_dir}
162 .endif
163 .endfor
164 .if !defined(SYSDIR) || !exists(${SYSDIR}/kern)
165 .error "can't find kernel source tree"
166 .endif
167 S=      ${SYSDIR}
168
169 #       path=`(cd $$path && /bin/pwd)` ; 
170
171 ${_ILINKS}:
172         @case ${.TARGET} in \
173         arch) \
174                 path=${SYSDIR}/arch/${MACHINE_ARCH} ;; \
175         machine) \
176                 path=${SYSDIR}/arch/${MACHINE_ARCH}/include ;; \
177         @) \
178                 path=${SYSDIR} ;; \
179         arch_*) \
180                 path=${.CURDIR}/${MACHINE_ARCH} ;; \
181         esac ; \
182         ${ECHO} ${.TARGET} "->" $$path ; \
183         ${LN} -s $$path ${.TARGET}
184
185 CLEANFILES+= ${PROG} ${KMOD}.kld ${OBJS} ${_ILINKS} symb.tmp tmp.o
186
187 .if !target(install)
188
189 _INSTALLFLAGS:= ${INSTALLFLAGS}
190 .for ie in ${INSTALLFLAGS_EDIT}
191 _INSTALLFLAGS:= ${_INSTALLFLAGS${ie}}
192 .endfor
193
194 .if !target(realinstall)
195 realinstall: _kmodinstall
196 .ORDER: beforeinstall _kmodinstall
197 _kmodinstall:
198 .if defined(INSTALLSTRIPPEDMODULES)
199         ${INSTALL} -o ${KMODOWN} -g ${KMODGRP} -m ${KMODMODE} \
200             ${_INSTALLFLAGS} ${PROG} ${DESTDIR}${KMODDIR}
201         ${OBJCOPY} --strip-debug ${DESTDIR}${KMODDIR}/${PROG}
202 .else
203         ${INSTALL} -o ${KMODOWN} -g ${KMODGRP} -m ${KMODMODE} \
204             ${_INSTALLFLAGS} ${PROG} ${DESTDIR}${KMODDIR}
205 .endif
206 .endif # !target(realinstall)
207
208 .include <bsd.links.mk>
209
210 .if !defined(NOMAN)
211 realinstall: _maninstall
212 .ORDER: beforeinstall _maninstall
213 .endif
214
215 .endif # !target(install)
216
217 .if !target(load)
218 load:   ${PROG}
219         ${KMODLOAD} -v ./${KMOD}.ko
220 .endif
221
222 .if !target(unload)
223 unload:
224         ${KMODUNLOAD} -v ${KMOD}
225 .endif
226
227 .for _src in ${SRCS:Mopt_*.h} ${SRCS:Muse_*.h}
228 CLEANFILES+=    ${_src}
229 .if !target(${_src})
230 .if defined(BUILDING_WITH_KERNEL) && exists(${BUILDING_WITH_KERNEL}/${_src})
231 ${_src}: ${BUILDING_WITH_KERNEL}/${_src}
232         cp ${BUILDING_WITH_KERNEL}/${_src} ${.TARGET}
233 .else
234 ${_src}:
235         touch ${.TARGET}
236 .endif  # BUILDING_WITH_KERNEL
237 .endif
238 .endfor
239
240 MFILES?= kern/bus_if.m kern/device_if.m bus/iicbus/iicbb_if.m \
241     bus/iicbus/iicbus_if.m bus/isa/isa_if.m dev/netif/mii_layer/miibus_if.m \
242     bus/pccard/card_if.m bus/pccard/power_if.m bus/pci/pci_if.m \
243     bus/pci/pcib_if.m \
244     bus/ppbus/ppbus_if.m bus/smbus/smbus_if.m bus/usb/usb_if.m \
245     dev/sound/pcm/ac97_if.m dev/sound/pcm/channel_if.m \
246     dev/sound/pcm/feeder_if.m dev/sound/pcm/mixer_if.m \
247     libiconv/iconv_converter_if.m dev/agp/agp_if.m opencrypto/crypto_if.m
248
249 .for _srcsrc in ${MFILES}
250 .for _ext in c h
251 .for _src in ${SRCS:M${_srcsrc:T:R}.${_ext}}
252 CLEANFILES+=    ${_src}
253 .if !target(${_src})
254 ${_src}: @
255 .if exists(@)
256 ${_src}: @/tools/makeobjops.awk @/${_srcsrc}
257 .endif
258         awk -f @/tools/makeobjops.awk -- -${_ext} @/${_srcsrc}
259 .endif
260 .endfor # _src
261 .endfor # _ext
262 .endfor # _srcsrc
263
264 #.for _ext in c h
265 #.if ${SRCS:Mvnode_if.${_ext}} != ""
266 #CLEANFILES+=   vnode_if.${_ext}
267 #vnode_if.${_ext}: @
268 #.if exists(@)
269 #vnode_if.${_ext}: @/tools/vnode_if.awk @/kern/vnode_if.src
270 #.endif
271 #       awk -f @/tools/vnode_if.awk -- -${_ext} @/kern/vnode_if.src
272 #.endif
273 #.endfor
274
275 regress:
276
277 .include <bsd.dep.mk>
278
279 .if !exists(${DEPENDFILE})
280 ${OBJS}: ${SRCS:M*.h}
281 .endif
282
283 .include <bsd.obj.mk>
284 .include "bsd.kern.mk"
285
286 # Behaves like MODULE_OVERRIDE
287 .if defined(KLD_DEPS)
288 all: _kdeps_all
289 _kdeps_all: @
290 .for _mdep in ${KLD_DEPS}
291         cd ${SYSDIR}/${_mdep} && make all
292 .endfor
293 depend: _kdeps_depend
294 _kdeps_depend: @
295 .for _mdep in ${KLD_DEPS}
296         cd ${SYSDIR}/${_mdep} && make depend
297 .endfor
298 install: _kdeps_install
299 _kdeps_install: @
300 .for _mdep in ${KLD_DEPS}
301         cd ${SYSDIR}/${_mdep} && make install
302 .endfor
303 clean: _kdeps_clean
304 _kdeps_clean: @
305 .for _mdep in ${KLD_DEPS}
306         cd ${SYSDIR}/${_mdep} && make clean
307 .endfor
308 .endif