xref: /netbsd-src/share/mk/bsd.kmodule.mk (revision ca453df649ce9db45b64d73678ba06cbccf9aa11)
1#	$NetBSD: bsd.kmodule.mk,v 1.31 2011/07/14 18:12:35 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
20
21# XXX until the kernel is fixed again...
22.if defined(HAVE_GCC) || 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.elif ${MACHINE_CPU} == "vax"
37CFLAGS+=	-fno-pic
38.endif
39
40# evbppc needs some special help
41.if ${MACHINE} == "evbppc"
42
43. ifndef PPC_INTR_IMPL
44PPC_INTR_IMPL=\"powerpc/intr.h\"
45. endif
46. ifndef PPC_PCI_MACHDEP_IMPL
47PPC_PCI_MACHDEP_IMPL=\"powerpc/pci_machdep.h\"
48. endif
49CPPFLAGS+=      -DPPC_INTR_IMPL=${PPC_INTR_IMPL}
50CPPFLAGS+=      -DPPC_PCI_MACHDEP_IMPL=${DPPC_PCI_MACHDEP_IMPL}
51
52. ifdef PPC_IBM4XX
53CPPFLAGS+=      -DPPC_IBM4XX
54. elifdef PPC_BOOKE
55CPPFLAGS+=      -DPPC_BOOKE
56. else
57CPPFLAGS+=      -DPPC_OEA
58. endif
59
60.endif
61
62
63_YKMSRCS=	${SRCS:M*.[ly]:C/\..$/.c/} ${YHEADER:D${SRCS:M*.y:.y=.h}}
64DPSRCS+=	${_YKMSRCS}
65CLEANFILES+=	${_YKMSRCS}
66
67.if exists($S/../sys/modules/xldscripts/kmodule)
68KMODSCRIPT=	$S/../sys/modules/xldscripts/kmodule
69.else
70KMODSCRIPT=	${DESTDIR}/usr/libdata/ldscripts/kmodule
71.endif
72
73OBJS+=		${SRCS:N*.h:N*.sh:R:S/$/.o/g}
74PROG?=		${KMOD}.kmod
75
76##### Build rules
77realall:	${PROG}
78
79${OBJS} ${LOBJS}: ${DPSRCS}
80
81${PROG}: ${OBJS} ${DPADD}
82	${_MKTARGET_LINK}
83	${CC} ${LDFLAGS} -nostdlib -r -Wl,-T,${KMODSCRIPT},-d \
84		-o ${.TARGET} ${OBJS}
85
86##### Install rules
87.if !target(kmodinstall)
88.if !defined(KMODULEDIR)
89_OSRELEASE!=	${HOST_SH} $S/conf/osrelease.sh
90# Ensure these are recorded properly in METALOG on unprived installes:
91KMODULEARCHDIR?= ${MACHINE}
92_INST_DIRS=	${DESTDIR}/stand/${KMODULEARCHDIR}
93_INST_DIRS+=	${DESTDIR}/stand/${KMODULEARCHDIR}/${_OSRELEASE}
94_INST_DIRS+=	${DESTDIR}/stand/${KMODULEARCHDIR}/${_OSRELEASE}/modules
95KMODULEDIR=	${DESTDIR}/stand/${KMODULEARCHDIR}/${_OSRELEASE}/modules/${KMOD}
96.endif
97_PROG:=		${KMODULEDIR}/${PROG} # installed path
98
99.if ${MKUPDATE} == "no"
100${_PROG}! ${PROG}					# install rule
101.if !defined(BUILD) && !make(all) && !make(${PROG})
102${_PROG}!	.MADE					# no build at install
103.endif
104.else
105${_PROG}: ${PROG}					# install rule
106.if !defined(BUILD) && !make(all) && !make(${PROG})
107${_PROG}:	.MADE					# no build at install
108.endif
109.endif
110	${_MKTARGET_INSTALL}
111	dirs=${_INST_DIRS:Q}; \
112	for d in $$dirs; do \
113		${INSTALL_DIR} $$d; \
114	done
115	${INSTALL_DIR} ${KMODULEDIR}
116	${INSTALL_FILE} -o ${KMODULEOWN} -g ${KMODULEGRP} -m ${KMODULEMODE} \
117		${.ALLSRC} ${.TARGET}
118
119kmodinstall::	${_PROG}
120.PHONY:		kmodinstall
121.PRECIOUS:	${_PROG}				# keep if install fails
122
123.undef _PROG
124.endif # !target(kmodinstall)
125
126##### Clean rules
127cleankmod: .PHONY
128	rm -f a.out [Ee]rrs mklog core *.core \
129		${PROG} ${OBJS} ${LOBJS} ${CLEANFILES}
130
131##### Custom rules
132lint: ${LOBJS}
133.if defined(LOBJS) && !empty(LOBJS)
134	${LINT} ${LINTFLAGS} ${LDFLAGS:C/-L[  ]*/-L/Wg:M-L*} ${LOBJS} ${LDADD}
135.endif
136
137##### Pull in related .mk logic
138LINKSOWN?= ${KMODULEOWN}
139LINKSGRP?= ${KMODULEGRP}
140LINKSMODE?= ${KMODULEMODE}
141.include <bsd.man.mk>
142.include <bsd.links.mk>
143.include <bsd.dep.mk>
144
145.-include "$S/arch/${MACHINE_CPU}/include/Makefile.inc"
146.-include "$S/arch/${MACHINE}/include/Makefile.inc"
147