xref: /netbsd-src/share/mk/bsd.kmodule.mk (revision c2f76ff004a2cb67efe5b12d97bd3ef7fe89e18d)
1#	$NetBSD: bsd.kmodule.mk,v 1.27 2011/01/02 19:24:47 matt Exp $
2
3# We are not building this with PIE
4MKPIE=no
5
6.include <bsd.init.mk>
7.include <bsd.klinks.mk>
8.include <bsd.sys.mk>
9
10##### Basic targets
11clean:		cleankmod
12realinstall:	kmodinstall
13
14KERN=		$S/kern
15
16CFLAGS+=	-ffreestanding ${COPTS}
17CPPFLAGS+=	-nostdinc -I. -I${.CURDIR} -isystem $S -isystem $S/arch
18CPPFLAGS+=	-isystem ${S}/../common/include
19CPPFLAGS+=	-D_KERNEL -D_LKM -D_MODULE
20CPPFLAGS+=	-std=gnu99
21
22# XXX until the kernel is fixed again...
23.if (defined(HAVE_GCC) && ${HAVE_GCC} == 4) || defined(HAVE_PCC)
24CFLAGS+=	-fno-strict-aliasing -Wno-pointer-sign
25.endif
26
27# XXX This is a workaround for platforms that have relative relocations
28# that, when relocated by the module loader, result in addresses that
29# overflow the size of the relocation (e.g. R_PPC_REL24 in powerpc).
30# The real solution to this involves generating trampolines for those
31# relocations inside the loader and removing this workaround, as the
32# resulting code would be much faster.
33.if ${MACHINE_CPU} == "arm"
34CFLAGS+=	-mlong-calls
35.elif ${MACHINE_CPU} == "powerpc"
36CFLAGS+=	-mlongcall
37.endif
38
39_YKMSRCS=	${SRCS:M*.[ly]:C/\..$/.c/} ${YHEADER:D${SRCS:M*.y:.y=.h}}
40DPSRCS+=	${_YKMSRCS}
41CLEANFILES+=	${_YKMSRCS}
42
43.if exists($S/../sys/modules/xldscripts/kmodule)
44KMODSCRIPT=	$S/../sys/modules/xldscripts/kmodule
45.else
46KMODSCRIPT=	${DESTDIR}/usr/libdata/ldscripts/kmodule
47.endif
48
49OBJS+=		${SRCS:N*.h:N*.sh:R:S/$/.o/g}
50PROG?=		${KMOD}.kmod
51
52##### Build rules
53realall:	${PROG}
54
55${OBJS} ${LOBJS}: ${DPSRCS}
56
57${PROG}: ${OBJS} ${DPADD}
58	${_MKTARGET_LINK}
59	${CC} ${LDFLAGS} -nostdlib -r -Wl,-T,${KMODSCRIPT},-d \
60		-o ${.TARGET} ${OBJS}
61
62##### Install rules
63.if !target(kmodinstall)
64.if !defined(KMODULEDIR)
65_OSRELEASE!=	${HOST_SH} $S/conf/osrelease.sh
66# Ensure these are recorded properly in METALOG on unprived installes:
67_INST_DIRS=	${DESTDIR}/stand/${MACHINE}
68_INST_DIRS+=	${DESTDIR}/stand/${MACHINE}/${_OSRELEASE}
69_INST_DIRS+=	${DESTDIR}/stand/${MACHINE}/${_OSRELEASE}/modules
70KMODULEDIR=	${DESTDIR}/stand/${MACHINE}/${_OSRELEASE}/modules/${KMOD}
71.endif
72_PROG:=		${KMODULEDIR}/${PROG} # installed path
73
74.if ${MKUPDATE} == "no"
75${_PROG}! ${PROG}					# install rule
76.if !defined(BUILD) && !make(all) && !make(${PROG})
77${_PROG}!	.MADE					# no build at install
78.endif
79.else
80${_PROG}: ${PROG}					# install rule
81.if !defined(BUILD) && !make(all) && !make(${PROG})
82${_PROG}:	.MADE					# no build at install
83.endif
84.endif
85	${_MKTARGET_INSTALL}
86	dirs=${_INST_DIRS:Q}; \
87	for d in $$dirs; do \
88		${INSTALL_DIR} $$d; \
89	done
90	${INSTALL_DIR} ${KMODULEDIR}
91	${INSTALL_FILE} -o ${KMODULEOWN} -g ${KMODULEGRP} -m ${KMODULEMODE} \
92		${.ALLSRC} ${.TARGET}
93
94kmodinstall::	${_PROG}
95.PHONY:		kmodinstall
96.PRECIOUS:	${_PROG}				# keep if install fails
97
98.undef _PROG
99.endif # !target(kmodinstall)
100
101##### Clean rules
102cleankmod: .PHONY
103	rm -f a.out [Ee]rrs mklog core *.core \
104		${PROG} ${OBJS} ${LOBJS} ${CLEANFILES}
105
106##### Custom rules
107lint: ${LOBJS}
108.if defined(LOBJS) && !empty(LOBJS)
109	${LINT} ${LINTFLAGS} ${LDFLAGS:C/-L[  ]*/-L/Wg:M-L*} ${LOBJS} ${LDADD}
110.endif
111
112##### Pull in related .mk logic
113LINKSOWN?= ${KMODULEOWN}
114LINKSGRP?= ${KMODULEGRP}
115LINKSMODE?= ${KMODULEMODE}
116.include <bsd.man.mk>
117.include <bsd.links.mk>
118.include <bsd.dep.mk>
119
120.-include "$S/arch/${MACHINE_CPU}/include/Makefile.inc"
121.-include "$S/arch/${MACHINE}/include/Makefile.inc"
122