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