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