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