xref: /freebsd/sys/conf/kmod.mk (revision ed277dc979d8cdbee62fcb3d6e11e41c79c99ca9)
1639a2e50SGarrett Wollman#	From: @(#)bsd.prog.mk	5.26 (Berkeley) 6/25/91
27f3dea24SPeter Wemm# $FreeBSD$
353996fadSWolfram Schneider#
4fe3cac87SNick Hibma# The include file <bsd.kmod.mk> handles installing Kernel Loadable Device
5fe3cac87SNick Hibma# drivers (KLD's).
653996fadSWolfram Schneider#
753996fadSWolfram Schneider#
853996fadSWolfram Schneider# +++ variables +++
953996fadSWolfram Schneider#
1053996fadSWolfram Schneider# CLEANFILES	Additional files to remove for the clean and cleandir targets.
1153996fadSWolfram Schneider#
1253996fadSWolfram Schneider# DISTRIBUTION  Name of distribution. [bin]
1353996fadSWolfram Schneider#
14fe3cac87SNick Hibma# KMOD          The name of the kernel module to build.
1553996fadSWolfram Schneider#
163bdfa9e5SDavid E. O'Brien# KMODDIR	Base path for kernel modules (see kld(4)). [/boot/kernel]
1753996fadSWolfram Schneider#
18fe3cac87SNick Hibma# KMODOWN	KLD owner. [${BINOWN}]
1953996fadSWolfram Schneider#
20fe3cac87SNick Hibma# KMODGRP	KLD group. [${BINGRP}]
2153996fadSWolfram Schneider#
22fe3cac87SNick Hibma# KMODMODE	KLD mode. [${BINMODE}]
2353996fadSWolfram Schneider#
24fe3cac87SNick Hibma# LINKS		The list of KLD links; should be full pathnames, the
2553996fadSWolfram Schneider#               linked-to file coming first, followed by the linked
2653996fadSWolfram Schneider#               file.  The files are hard-linked.  For example, to link
2771395925SPeter Wemm#               /modules/master and /modules/meister, use:
2853996fadSWolfram Schneider#
2971395925SPeter Wemm#			LINKS=  /modules/master /modules/meister
3053996fadSWolfram Schneider#
31fe3cac87SNick Hibma# KMODLOAD	Command to load a kernel module [/sbin/kldload]
327bc830bbSWolfram Schneider#
33fe3cac87SNick Hibma# KMODUNLOAD	Command to unload a kernel module [/sbin/kldunload]
347bc830bbSWolfram Schneider#
35fe3cac87SNick Hibma# NOMAN		KLD does not have a manual page if set.
3653996fadSWolfram Schneider#
37fe3cac87SNick Hibma# PROG          The name of the kernel module to build.
387bc830bbSWolfram Schneider#		If not supplied, ${KMOD}.o is used.
3953996fadSWolfram Schneider#
4053996fadSWolfram Schneider# SRCS          List of source files
4153996fadSWolfram Schneider#
4253996fadSWolfram Schneider# SUBDIR        A list of subdirectories that should be built as well.
4353996fadSWolfram Schneider#               Each of the targets will execute the same target in the
4453996fadSWolfram Schneider#               subdirectories.
4553996fadSWolfram Schneider#
462336a7eaSBruce Evans# SYMLINKS	Same as LINKS, except it creates symlinks and the
472336a7eaSBruce Evans#		linked-to pathname may be relative.
482336a7eaSBruce Evans#
4953996fadSWolfram Schneider# DESTDIR, DISTDIR are set by other Makefiles (e.g. bsd.own.mk)
5053996fadSWolfram Schneider#
517fecffecSDoug Rabson# MFILES	Optionally a list of interfaces used by the module.
527fecffecSDoug Rabson#		This file contains a default list of interfaces.
5353996fadSWolfram Schneider#
5453996fadSWolfram Schneider# +++ targets +++
5553996fadSWolfram Schneider#
5653996fadSWolfram Schneider#       distribute:
5753996fadSWolfram Schneider#               This is a variant of install, which will
5853996fadSWolfram Schneider#               put the stuff into the right "distribution".
5953996fadSWolfram Schneider#
6053996fadSWolfram Schneider# 	install:
6153996fadSWolfram Schneider#               install the program and its manual pages; if the Makefile
6253996fadSWolfram Schneider#               does not itself define the target install, the targets
6353996fadSWolfram Schneider#               beforeinstall and afterinstall may also be used to cause
6453996fadSWolfram Schneider#               actions immediately before and after the install target
6553996fadSWolfram Schneider#		is executed.
6653996fadSWolfram Schneider#
6753996fadSWolfram Schneider# 	load:
68fe3cac87SNick Hibma#		Load KLD.
6953996fadSWolfram Schneider#
7053996fadSWolfram Schneider# 	unload:
71fe3cac87SNick Hibma#		Unload KLD.
7253996fadSWolfram Schneider#
7353996fadSWolfram Schneider# bsd.obj.mk: clean, cleandir and obj
74161e8999SBruce Evans# bsd.dep.mk: cleandepend, depend and tags
7553996fadSWolfram Schneider# bsd.man.mk: maninstall
7653996fadSWolfram Schneider#
77639a2e50SGarrett Wollman
78fe3cac87SNick HibmaKMODLOAD?=	/sbin/kldload
79fe3cac87SNick HibmaKMODUNLOAD?=	/sbin/kldunload
807bc830bbSWolfram Schneider
81e0301e5dSPeter Wemm.if !target(__initialized__)
82e0301e5dSPeter Wemm__initialized__:
83639a2e50SGarrett Wollman.if exists(${.CURDIR}/../Makefile.inc)
84639a2e50SGarrett Wollman.include "${.CURDIR}/../Makefile.inc"
85639a2e50SGarrett Wollman.endif
86e0301e5dSPeter Wemm.endif
87639a2e50SGarrett Wollman
88639a2e50SGarrett Wollman.SUFFIXES: .out .o .c .cc .cxx .C .y .l .s .S
89639a2e50SGarrett Wollman
90c4473420SPeter WemmCFLAGS+=	${COPTS} -D_KERNEL ${CWARNFLAGS}
918fe644b2SMike SmithCFLAGS+=	-DKLD_MODULE
924a2d2630SBruce Evans
93021886ffSBruce Evans# Don't use any standard or source-relative include directories.
94021886ffSBruce Evans# Since -nostdinc will annull any previous -I paths, we repeat all
95021886ffSBruce Evans# such paths after -nostdinc.  It doesn't seem to be possible to
96021886ffSBruce Evans# add to the front of `make' variable.
97021886ffSBruce Evans_ICFLAGS:=	${CFLAGS:M-I*}
9826dac111SPoul-Henning KampCFLAGS+=	-nostdinc -I- ${INCLMAGIC} ${_ICFLAGS}
99021886ffSBruce Evans
100fe3cac87SNick Hibma# Add -I paths for system headers.  Individual KLD makefiles don't
101021886ffSBruce Evans# need any -I paths for this.  Similar defaults for .PATH can't be
102021886ffSBruce Evans# set because there are no standard paths for non-headers.
1037e820aaaSMike SmithCFLAGS+=	-I. -I@ -I@/dev
1044a2d2630SBruce Evans
105a0b845feSBruce Evans# Add a -I path to standard headers like <stddef.h>.  Use a relative
106a0b845feSBruce Evans# path to src/include if possible.  If the @ symlink hasn't been built
107a0b845feSBruce Evans# yet, then we can't tell if the relative path exists.  Add both the
108a0b845feSBruce Evans# potential relative path and an absolute path in that case.
109a0b845feSBruce Evans.if exists(@)
110a0b845feSBruce Evans.if exists(@/../include)
111a0b845feSBruce EvansCFLAGS+=	-I@/../include
112a0b845feSBruce Evans.else
113b3e17ba2SSatoshi AsamiCFLAGS+=	-I${DESTDIR}/usr/include
114b3e17ba2SSatoshi Asami.endif
115a0b845feSBruce Evans.else # !@
116a0b845feSBruce EvansCFLAGS+=	-I@/../include -I${DESTDIR}/usr/include
117a0b845feSBruce Evans.endif # @
11856bac51bSPoul-Henning Kamp
1199e4be63dSBruce EvansCFLAGS+=	${DEBUG_FLAGS}
1209e4be63dSBruce Evans
12171395925SPeter Wemm.if ${OBJFORMAT} == elf
122fdbdd3a0SPeter WemmCLEANFILES+=	setdef0.c setdef1.c setdefs.h
123fdbdd3a0SPeter WemmCLEANFILES+=	setdef0.o setdef1.o
124fdbdd3a0SPeter Wemm.endif
125fdbdd3a0SPeter Wemm
126639a2e50SGarrett WollmanOBJS+=  ${SRCS:N*.h:R:S/$/.o/g}
127639a2e50SGarrett Wollman
128639a2e50SGarrett Wollman.if !defined(PROG)
12990f60fc9SMike SmithPROG=	${KMOD}.ko
13090f60fc9SMike Smith.endif
131639a2e50SGarrett Wollman
13209be11baSPeter Wemm${PROG}: ${KMOD}.kld
133fdbdd3a0SPeter Wemm.if ${OBJFORMAT} == elf
1348e739d9fSMarcel Moolenaar	perl5 @/kern/gensetdefs.pl ${KMOD}.kld
135fdbdd3a0SPeter Wemm	${CC} ${CFLAGS} -c setdef0.c
136fdbdd3a0SPeter Wemm	${CC} ${CFLAGS} -c setdef1.c
13709be11baSPeter Wemm	${LD} -Bshareable ${LDFLAGS} -o ${.TARGET} setdef0.o ${KMOD}.kld setdef1.o
138fdbdd3a0SPeter Wemm.else
13909be11baSPeter Wemm	${LD} -Bshareable ${LDFLAGS} -o ${.TARGET} ${KMOD}.kld
14009be11baSPeter Wemm.endif
14109be11baSPeter Wemm
14209be11baSPeter Wemm${KMOD}.kld: ${OBJS}
1439e0f2a9fSPeter Wemm	${LD} ${LDFLAGS} -r -o ${.TARGET} ${OBJS}
144fdbdd3a0SPeter Wemm
1455ce6c3cfSWolfram Schneider.if !defined(NOMAN)
1465ce6c3cfSWolfram Schneider.include <bsd.man.mk>
1475ce6c3cfSWolfram Schneider.if !defined(_MANPAGES) || empty(_MANPAGES)
148639a2e50SGarrett WollmanMAN1=	${KMOD}.4
149639a2e50SGarrett Wollman.endif
150639a2e50SGarrett Wollman
1515ce6c3cfSWolfram Schneider.elif !target(maninstall)
152cd9a2f5cSJordan K. Hubbardmaninstall: _SUBDIR
1535ce6c3cfSWolfram Schneiderall-man:
1545ce6c3cfSWolfram Schneider.endif
1555ce6c3cfSWolfram Schneider
1564a2d2630SBruce Evans_ILINKS=@ machine
157639a2e50SGarrett Wollman
1584a2d2630SBruce Evans.MAIN: all
15947da63c3SBruce Evansall: objwarn ${PROG} all-man _SUBDIR
1604a2d2630SBruce Evans
161f3cc9575SWarner Losh# Ensure that the links exist without depending on it when it exists which
162f3cc9575SWarner Losh# causes all the modules to be rebuilt when the directory pointed to changes.
163f3cc9575SWarner Losh.for _link in ${_ILINKS}
164f3cc9575SWarner Losh.if !exists(${.OBJDIR}/${_link})
165f3cc9575SWarner Losh${OBJS}: ${_link}
166f3cc9575SWarner Loshbeforedepend: ${_link}
1672fbfac27SPeter Wemm	@rm -f .depend
168f3cc9575SWarner Losh.endif
169f3cc9575SWarner Losh.endfor
1704a2d2630SBruce Evans
1719a8631b3SWarner Losh# Search for kernel source tree in standard places.
1729a8631b3SWarner Losh.for _dir in ${.CURDIR}/../.. ${.CURDIR}/../../.. /sys /usr/src/sys
1739a8631b3SWarner Losh.if !defined(SYSDIR) && exists(${_dir}/kern/)
1749a8631b3SWarner LoshSYSDIR=	${_dir}
1759a8631b3SWarner Losh.endif
1769a8631b3SWarner Losh.endfor
177aabafca0SJason Evans.if !defined(SYSDIR) || !exists(${SYSDIR}/kern)
1789a8631b3SWarner Losh.error "can't find kernel source tree"
1799a8631b3SWarner Losh.endif
1809a8631b3SWarner Losh
1814a2d2630SBruce Evans${_ILINKS}:
1829a8631b3SWarner Losh	@case ${.TARGET} in \
18308a04a89SPeter Wemm	machine) \
1849a8631b3SWarner Losh		path=${SYSDIR}/${MACHINE_ARCH}/include ;; \
18508a04a89SPeter Wemm	@) \
1869a8631b3SWarner Losh		path=${SYSDIR} ;; \
18708a04a89SPeter Wemm	esac ; \
18808a04a89SPeter Wemm	path=`(cd $$path && /bin/pwd)` ; \
18908a04a89SPeter Wemm	${ECHO} ${.TARGET} "->" $$path ; \
19008a04a89SPeter Wemm	ln -s $$path ${.TARGET}
1914a2d2630SBruce Evans
1929e0f2a9fSPeter WemmCLEANFILES+= ${PROG} ${KMOD}.kld ${OBJS} ${_ILINKS} symb.tmp tmp.o
193639a2e50SGarrett Wollman
194639a2e50SGarrett Wollman.if !target(install)
195639a2e50SGarrett Wollman.if !target(beforeinstall)
196639a2e50SGarrett Wollmanbeforeinstall:
197639a2e50SGarrett Wollman.endif
198639a2e50SGarrett Wollman.if !target(afterinstall)
199639a2e50SGarrett Wollmanafterinstall:
200639a2e50SGarrett Wollman.endif
201639a2e50SGarrett Wollman
202e902c1bbSTim Vanderhoek_INSTALLFLAGS:=	${INSTALLFLAGS}
203e902c1bbSTim Vanderhoek.for ie in ${INSTALLFLAGS_EDIT}
204e902c1bbSTim Vanderhoek_INSTALLFLAGS:=	${_INSTALLFLAGS${ie}}
205e902c1bbSTim Vanderhoek.endfor
206e902c1bbSTim Vanderhoek
207cd9a2f5cSJordan K. Hubbardrealinstall: _SUBDIR
2085ce6c3cfSWolfram Schneider	${INSTALL} ${COPY} -o ${KMODOWN} -g ${KMODGRP} -m ${KMODMODE} \
209e902c1bbSTim Vanderhoek	    ${_INSTALLFLAGS} ${PROG} ${DESTDIR}${KMODDIR}
210639a2e50SGarrett Wollman.if defined(LINKS) && !empty(LINKS)
211639a2e50SGarrett Wollman	@set ${LINKS}; \
212639a2e50SGarrett Wollman	while test $$# -ge 2; do \
213639a2e50SGarrett Wollman		l=${DESTDIR}$$1; \
214639a2e50SGarrett Wollman		shift; \
215639a2e50SGarrett Wollman		t=${DESTDIR}$$1; \
216639a2e50SGarrett Wollman		shift; \
217639a2e50SGarrett Wollman		${ECHO} $$t -\> $$l; \
2182336a7eaSBruce Evans		ln -f $$l $$t; \
2192336a7eaSBruce Evans	done; true
2202336a7eaSBruce Evans.endif
2212336a7eaSBruce Evans.if defined(SYMLINKS) && !empty(SYMLINKS)
2222336a7eaSBruce Evans	@set ${SYMLINKS}; \
2232336a7eaSBruce Evans	while test $$# -ge 2; do \
2242336a7eaSBruce Evans		l=$$1; \
2252336a7eaSBruce Evans		shift; \
2262336a7eaSBruce Evans		t=${DESTDIR}$$1; \
2272336a7eaSBruce Evans		shift; \
2282336a7eaSBruce Evans		${ECHO} $$t -\> $$l; \
2292336a7eaSBruce Evans		ln -fs $$l $$t; \
230639a2e50SGarrett Wollman	done; true
231639a2e50SGarrett Wollman.endif
232639a2e50SGarrett Wollman
233cd9a2f5cSJordan K. Hubbardinstall: afterinstall _SUBDIR
234639a2e50SGarrett Wollman.if !defined(NOMAN)
235639a2e50SGarrett Wollmanafterinstall: realinstall maninstall
236639a2e50SGarrett Wollman.else
237639a2e50SGarrett Wollmanafterinstall: realinstall
238639a2e50SGarrett Wollman.endif
239639a2e50SGarrett Wollmanrealinstall: beforeinstall
240639a2e50SGarrett Wollman.endif
241639a2e50SGarrett Wollman
242d9584d76SJordan K. HubbardDISTRIBUTION?=	bin
24355ff8fb1SPoul-Henning Kamp.if !target(distribute)
244cd9a2f5cSJordan K. Hubbarddistribute: _SUBDIR
245398ac038SJordan K. Hubbard.for dist in ${DISTRIBUTION}
246398ac038SJordan K. Hubbard	cd ${.CURDIR} ; $(MAKE) install DESTDIR=${DISTDIR}/${dist} SHARED=copies
247398ac038SJordan K. Hubbard.endfor
24855ff8fb1SPoul-Henning Kamp.endif
24955ff8fb1SPoul-Henning Kamp
250639a2e50SGarrett Wollman.if !target(load)
2519a8631b3SWarner Loshload:	${PROG}
25248bd8a66SColeman Kane	${KMODLOAD} -v ./${KMOD}.ko
253639a2e50SGarrett Wollman.endif
254639a2e50SGarrett Wollman
255639a2e50SGarrett Wollman.if !target(unload)
256fe3cac87SNick Hibmaunload:
2579a8631b3SWarner Losh	${KMODUNLOAD} -v ${KMOD}
25808a04a89SPeter Wemm.endif
2591aa57be3SGarrett Wollman
2604fea6704SBruce Evans.for _src in ${SRCS:Mopt_*.h}
2614fea6704SBruce EvansCLEANFILES+=	${_src}
2624fea6704SBruce Evans.if !target(${_src})
2634fea6704SBruce Evans${_src}:
2644fea6704SBruce Evans	touch ${.TARGET}
2654fea6704SBruce Evans.endif
2664fea6704SBruce Evans.endfor
2674fea6704SBruce Evans
2687fecffecSDoug RabsonMFILES?= kern/bus_if.m kern/device_if.m dev/iicbus/iicbb_if.m \
269647b247cSPeter Wemm    dev/iicbus/iicbus_if.m isa/isa_if.m dev/mii/miibus_if.m \
2707e820aaaSMike Smith    dev/pccard/card_if.m dev/pccard/power_if.m dev/pci/pci_if.m \
2717e820aaaSMike Smith    dev/pci/pcib_if.m dev/ppbus/ppbus_if.m dev/smbus/smbus_if.m \
2720f55ac6cSCameron Grant    dev/usb/usb_if.m dev/sound/pcm/ac97_if.m dev/sound/pcm/channel_if.m \
273ed277dc9SPeter Wemm    dev/sound/pcm/feeder_if.m dev/sound/pcm/mixer_if.m pci/agp_if.m
2747fecffecSDoug Rabson
2757fecffecSDoug Rabson.for _srcsrc in ${MFILES}
2764fea6704SBruce Evans.for _ext in c h
2774fea6704SBruce Evans.for _src in ${SRCS:M${_srcsrc:T:R}.${_ext}}
2784fea6704SBruce EvansCLEANFILES+=	${_src}
2794fea6704SBruce Evans.if !target(${_src})
280f3cc9575SWarner Losh.if !exists(@)
2814fea6704SBruce Evans${_src}: @
282f3cc9575SWarner Losh.endif
2834fea6704SBruce Evans.if exists(@)
284c0de5879SDoug Rabson${_src}: @/kern/makeobjops.pl @/${_srcsrc}
2854fea6704SBruce Evans.endif
286c0de5879SDoug Rabson	perl @/kern/makeobjops.pl -${_ext} @/${_srcsrc}
2874fea6704SBruce Evans.endif
2884fea6704SBruce Evans.endfor # _src
2894fea6704SBruce Evans.endfor # _ext
2904fea6704SBruce Evans.endfor # _srcsrc
2914fea6704SBruce Evans
2929029b644SPeter Wemm.for _ext in c h
2939029b644SPeter Wemm.if ${SRCS:Mvnode_if.${_ext}} != ""
2949029b644SPeter WemmCLEANFILES+=	vnode_if.${_ext}
295f3cc9575SWarner Losh.if !exists(@)
2969029b644SPeter Wemmvnode_if.${_ext}: @
297f3cc9575SWarner Losh.endif
2984fea6704SBruce Evans.if exists(@)
2993b073b53SPeter Wemmvnode_if.${_ext}: @/kern/vnode_if.pl @/kern/vnode_if.src
3004fea6704SBruce Evans.endif
3013b073b53SPeter Wemm	perl @/kern/vnode_if.pl -${_ext} @/kern/vnode_if.src
3024fea6704SBruce Evans.endif
3039029b644SPeter Wemm.endfor
3041aa57be3SGarrett Wollman
30571395925SPeter Wemmregress:
30669b87d3eSEivind Eklund
30768e00b92SJordan K. Hubbard.include <bsd.dep.mk>
30847da63c3SBruce Evans
30947da63c3SBruce Evans.if !exists(${DEPENDFILE})
310589d9341SBruce Evans${OBJS}: ${SRCS:M*.h}
31147da63c3SBruce Evans.endif
31247da63c3SBruce Evans
313afba897fSBruce Evans.include <bsd.obj.mk>
314afba897fSBruce Evans
3159e72552fSBruce Evans.include <bsd.kern.mk>
316