1# $MirOS: src/share/mk/bsd.lkm.mk,v 1.11 2008/12/10 21:46:26 tg Exp $
2# $OpenBSD: bsd.lkm.mk,v 1.19 2003/05/20 22:49:13 millert Exp $
3
4.if exists(${.CURDIR}/../Makefile.inc)
5.  include "${.CURDIR}/../Makefile.inc"
6.endif
7
8.ifndef	BSD_OWN_MK
9.  include <bsd.own.mk>
10.endif
11
12.SUFFIXES: .out .o .c .cc .C .y .l .s .8 .7 .6 .5 .4 .3 .2 .1 .0
13
14.if !${COPTS:M-fhonour-copts} || !${CFLAGS:M-fhonour-copts}
15CFLAGS+=	${COPTS}
16.endif
17KERNBASE?=	${BSDSRCDIR}/kern
18.if !${CPPFLAGS:M-D_LKM}
19CFLAGS+=	-ffreestanding
20CPPFLAGS+=	-D_KERNEL -D_LKM -I${BSDSRCDIR}/sys -I${BSDSRCDIR}/sys/arch \
21		-I/usr/include/syskern -I${KERNBASE}/include
22.endif
23.if ${WARNINGS:L} == "yes"
24CFLAGS+=	${CDIAGFLAGS}
25.endif
26
27LDFLAGS+=	-r
28.if defined(LKM) && !empty(LKM)
29SRCS?=		${LKM}.c
30MAN?=		${LKM}.1
31.  if !empty(SRCS:N*.h:N*.sh)
32OBJS+=		${SRCS:N*.h:N*.sh:R:S/$/.o/g}
33LOBJS+=		${LSRCS:.c=.ln} ${SRCS:M*.c:.c=.ln}
34.  endif
35POSTINSTALL?=	${LKM}install
36POSTUNINSTALL?=	${LKM}uninstall
37CLEANFILES+=	${POSTINSTALL} ${POSTUNINSTALL} ${LKM}_init.sh ${LKM}_done.sh
38
39.  if defined(OBJS) && !empty(OBJS)
40${LKM}.ko: ${OBJS} ${DPADD}
41	${LD} ${LDFLAGS} -o ${.TARGET} ${OBJS} ${LDADD}
42.  endif
43.endif	# def/not empty LKM
44
45.MAIN: all
46all: ${LKM}.ko ${LKM}_init.sh ${LKM}_done.sh _SUBDIRUSE
47
48.if exists(${.CURDIR}/${POSTINSTALL})
49all ${LKM}_init.sh: ${POSTINSTALL}
50
51${POSTINSTALL}: ${.CURDIR}/${POSTINSTALL}
52	${INSTALL} ${INSTALL_COPY} -m ${BINMODE} $> $@
53.endif
54
55.if exists(${.CURDIR}/${POSTUNINSTALL})
56all ${LKM}_done.sh: ${POSTUNINSTALL}
57
58${POSTUNINSTALL}: ${.CURDIR}/${POSTUNINSTALL}
59	${INSTALL} ${INSTALL_COPY} -m ${BINMODE} $> $@
60.endif
61
62${LKM}_init.sh: ${LKM}.ko
63	print '#!/bin/sh\ncd $$(dirname "$0")' >$@
64	print -n 'exec /sbin/modload $$* ' >>$@
65	if [[ -x ${POSTINSTALL} ]]; then \
66		print -- '-o ${LKM} -p ${POSTINSTALL} ${LKM}.ko'; \
67	else \
68		print -- '-o ${LKM} ${LKM}.ko'; \
69	fi >>$@
70
71${LKM}_done.sh: ${LKM}.ko
72	print '#!/bin/sh\ncd $$(dirname "$0")' >$@
73	print 'rm -f ${LKM}' >>$@
74	print -n 'exec /sbin/modunload ' >>$@
75	if [[ -x ${POSTUNINSTALL} ]]; then \
76		print -- '-p ${POSTUNINSTALL} -n ${LKM}'; \
77	else \
78		print -- '-n ${LKM}'; \
79	fi >>$@
80
81.if !target(clean)
82clean: _SUBDIRUSE
83	rm -f a.out [Ee]rrs mklog core *.core \
84	    ${LKM} ${LKM}.ko ${OBJS} ${LOBJS} ${CLEANFILES}
85.endif
86
87cleandir: _SUBDIRUSE clean
88
89.if !target(install)
90.  if !target(beforeinstall)
91beforeinstall:
92.  endif
93.  if !target(afterinstall)
94afterinstall:
95.  endif
96
97.  if !target(realinstall)
98realinstall:
99.    if defined(LKM) && !empty(LKM)
100	${INSTALL} ${INSTALL_COPY} -o ${LKMOWN} -g ${LKMGRP} -m ${LKMMODE} \
101	    ${LKM}.ko ${DESTDIR}${LKMDIR}/
102.      if exists(${.CURDIR}/${POSTINSTALL})
103	${INSTALL} ${INSTALL_COPY} -o ${LKMOWN} -g ${LKMGRP} -m ${BINMODE} \
104	    ${.CURDIR}/${POSTINSTALL} ${DESTDIR}${LKMDIR}/
105.      endif
106.      if exists(${.CURDIR}/${POSTUNINSTALL})
107	${INSTALL} ${INSTALL_COPY} -o ${LKMOWN} -g ${LKMGRP} -m ${BINMODE} \
108	    ${.CURDIR}/${POSTUNINSTALL} ${DESTDIR}${LKMDIR}/
109.      endif
110	${INSTALL} ${INSTALL_COPY} -o ${LKMOWN} -g ${LKMGRP} -m ${BINMODE} \
111	    ${LKM}_init.sh ${LKM}_done.sh ${DESTDIR}${LKMDIR}/
112.    endif
113.  endif
114
115load: ${LKM}_init.sh
116	@${MKSH} -x $> -d -v
117
118unload: ${LKM}_done.sh
119	@${MKSH} -x $>
120
121install: maninstall _SUBDIRUSE
122.  if defined(LINKS) && !empty(LINKS)
123	@set ${LINKS}; \
124	while test $$# -ge 2; do \
125		l=${DESTDIR}${LKMDIR}/$$1; \
126		shift; \
127		t=${DESTDIR}${LKMDIR}/$$1; \
128		shift; \
129		print -r -- $$t -\> $$l; \
130		rm -f $$t; \
131		ln $$l $$t || cp $$l $$t; \
132	done; true
133.  endif
134
135maninstall: afterinstall
136afterinstall: realinstall
137realinstall: beforeinstall
138.endif
139
140.if !target(lint)
141lint: ${LOBJS}
142.  if defined(LOBJS) && !empty(LOBJS)
143	@env CC=${_ORIG_CC:Q} ${LINT} ${LINTFLAGS} ${LDFLAGS:M-L*} \
144	    ${LOBJS} ${LDADD}
145.  endif
146.endif
147
148.if ${NOMAN:L} == "no"
149.  include <bsd.man.mk>
150.endif
151
152.include <bsd.obj.mk>
153.include <bsd.dep.mk>
154.include <bsd.subdir.mk>
155.include <bsd.sys.mk>
156
157.PHONY: load unload
158