xref: /netbsd-src/share/mk/bsd.kmodule.mk (revision 6a493d6bc668897c91594964a732d38505b70cbb)
1#	$NetBSD: bsd.kmodule.mk,v 1.41 2013/11/09 22:40:15 jmcneill 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
11realinstall:	kmodinstall
12
13KERN=		$S/kern
14
15CFLAGS+=	-ffreestanding ${COPTS}
16CPPFLAGS+=	-nostdinc -I. -I${.CURDIR} -isystem $S -isystem $S/arch
17CPPFLAGS+=	-isystem ${S}/../common/include
18CPPFLAGS+=	-D_KERNEL -D_LKM -D_MODULE -DSYSCTL_INCLUDE_DESCR
19
20# XXX until the kernel is fixed again...
21CFLAGS+=	-fno-strict-aliasing -Wno-pointer-sign
22
23# XXX This is a workaround for platforms that have relative relocations
24# that, when relocated by the module loader, result in addresses that
25# overflow the size of the relocation (e.g. R_PPC_REL24 in powerpc).
26# The real solution to this involves generating trampolines for those
27# relocations inside the loader and removing this workaround, as the
28# resulting code would be much faster.
29.if ${MACHINE_CPU} == "arm"
30CFLAGS+=	-fno-common -fno-unwind-tables
31.elif ${MACHINE_CPU} == "hppa"
32CFLAGS+=	-mlong-calls
33.elif ${MACHINE_CPU} == "powerpc"
34CFLAGS+=	-mlongcall
35.elif ${MACHINE_CPU} == "vax"
36CFLAGS+=	-fno-pic
37.endif
38
39.if ${MACHINE} == "sparc64"
40# force same memory model as rest of the kernel
41CFLAGS+=	-mcmodel=medlow
42.endif
43
44# evbppc needs some special help
45.if ${MACHINE} == "evbppc"
46
47. ifndef PPC_INTR_IMPL
48PPC_INTR_IMPL=\"powerpc/intr.h\"
49. endif
50. ifndef PPC_PCI_MACHDEP_IMPL
51PPC_PCI_MACHDEP_IMPL=\"powerpc/pci_machdep.h\"
52. endif
53CPPFLAGS+=      -DPPC_INTR_IMPL=${PPC_INTR_IMPL}
54CPPFLAGS+=      -DPPC_PCI_MACHDEP_IMPL=${DPPC_PCI_MACHDEP_IMPL}
55
56. ifdef PPC_IBM4XX
57CPPFLAGS+=      -DPPC_IBM4XX
58. elifdef PPC_BOOKE
59CPPFLAGS+=      -DPPC_BOOKE
60. else
61CPPFLAGS+=      -DPPC_OEA
62. endif
63
64.endif
65
66
67_YKMSRCS=	${SRCS:M*.[ly]:C/\..$/.c/} ${YHEADER:D${SRCS:M*.y:.y=.h}}
68DPSRCS+=	${_YKMSRCS}
69CLEANFILES+=	${_YKMSRCS}
70
71.if exists($S/../sys/modules/xldscripts/kmodule)
72KMODSCRIPT=	$S/../sys/modules/xldscripts/kmodule
73.else
74KMODSCRIPT=	${DESTDIR}/usr/libdata/ldscripts/kmodule
75.endif
76
77PROG?=		${KMOD}.kmod
78
79##### Build rules
80realall:	${PROG}
81
82.if (defined(USE_COMBINE) && ${USE_COMBINE} != "no" && !commands(${_P}) \
83   && !defined(NOCOMBINE.${_P}) && !defined(NOCOMBINE))
84.for f in ${SRCS:N*.h:N*.sh:N*.fth:C/\.[yl]$/.c/g}
85.if (${CPPFLAGS.$f:D1} == "1" || ${CPUFLAGS.$f:D2} == "2" \
86     || ${COPTS.$f:D3} == "3" || ${OBJCOPTS.$f:D4} == "4" \
87     || ${CXXFLAGS.$f:D5} == "5") \
88    || ("${f:M*.[cyl]}" == "" || commands(${f:R:S/$/.o/}))
89XOBJS+=		${f:R:S/$/.o/}
90.else
91XSRCS+=		${f}
92NODPSRCS+=	${f}
93.endif
94.endfor
95
96.if !empty(XOBJS)
97${XOBJS}:	${DPSRCS}
98.endif
99
100${PROG}: ${XOBJS} ${XSRCS} ${DPSRCS} ${DPADD}
101	${_MKTARGET_LINK}
102	${CC} ${LDFLAGS} -nostdlib -MD -combine -r -Wl,-T,${KMODSCRIPT},-d \
103		-o ${.TARGET} ${CFLAGS} ${CPPFLAGS} ${XOBJS} \
104		${XSRCS:@.SRC.@${.ALLSRC:M*.c:M*${.SRC.}}@:O:u} && \
105	echo '.-include "${KMOD}.d"' > .depend
106
107.else
108OBJS+=		${SRCS:N*.h:N*.sh:R:S/$/.o/g}
109
110${OBJS} ${LOBJS}: ${DPSRCS}
111
112.if ${MACHINE_CPU} == "arm"
113# The solution to limited branch space involves generating trampolines for
114# those relocations while creating the module, as the resulting code will
115# be much faster and simplifies the loader.
116ARCHDIR=	$S/modules/arch/${MACHINE_CPU}
117ASM_H=		$S/arch/${MACHINE_CPU}/include/asm.h
118CLEANFILES+=	tmp.o tmp.S ${KMOD}_tmp.o ${KMOD}_tramp.o ${KMOD}_tramp.S
119${KMOD}_tmp.o: ${OBJS} ${DPADD}
120	${_MKTARGET_LINK}
121	${LD} -r -o tmp.o ${OBJS}
122	${LD} -r \
123		`${OBJDUMP} --syms --reloc tmp.o | \
124			${TOOL_AWK} -f ${ARCHDIR}/kmodwrap.awk` \
125		 -o ${.TARGET} tmp.o
126
127${KMOD}_tramp.S: ${KMOD}_tmp.o ${ARCHDIR}/kmodtramp.awk ${ASM_H}
128	${_MKTARGET_CREATE}
129	${OBJDUMP} --syms --reloc ${KMOD}_tmp.o | \
130		 ${TOOL_AWK} -f ${ARCHDIR}/kmodtramp.awk \
131		 > tmp.S && \
132	mv tmp.S ${.TARGET}
133
134${PROG}: ${KMOD}_tmp.o ${KMOD}_tramp.o
135	${_MKTARGET_LINK}
136.if exists(${ARCHDIR}/kmodhide.awk)
137	${LD} -r -o tmp.o ${KMOD}_tmp.o ${KMOD}_tramp.o
138	${OBJCOPY} \
139		`${NM} tmp.o | ${TOOL_AWK} -f ${ARCHDIR}/kmodhide.awk` \
140		tmp.o ${.TARGET} && \
141	rm tmp.o
142.else
143	${LD} -r -o ${.TARGET} ${KMOD}_tmp.o ${KMOD}_tramp.o
144.endif
145.else
146${PROG}: ${OBJS} ${DPADD}
147	${_MKTARGET_LINK}
148	${CC} ${LDFLAGS} -nostdlib -r -Wl,-T,${KMODSCRIPT},-d \
149		-o ${.TARGET} ${OBJS}
150.endif
151.endif
152
153##### Install rules
154.if !target(kmodinstall)
155.if !defined(KMODULEDIR)
156_OSRELEASE!=	${HOST_SH} $S/conf/osrelease.sh -k
157# Ensure these are recorded properly in METALOG on unprived installes:
158KMODULEARCHDIR?= ${MACHINE}
159_INST_DIRS=	${DESTDIR}/stand/${KMODULEARCHDIR}
160_INST_DIRS+=	${DESTDIR}/stand/${KMODULEARCHDIR}/${_OSRELEASE}
161_INST_DIRS+=	${DESTDIR}/stand/${KMODULEARCHDIR}/${_OSRELEASE}/modules
162KMODULEDIR=	${DESTDIR}/stand/${KMODULEARCHDIR}/${_OSRELEASE}/modules/${KMOD}
163.endif
164_PROG:=		${KMODULEDIR}/${PROG} # installed path
165
166.if ${MKUPDATE} == "no"
167${_PROG}! ${PROG}					# install rule
168.if !defined(BUILD) && !make(all) && !make(${PROG})
169${_PROG}!	.MADE					# no build at install
170.endif
171.else
172${_PROG}: ${PROG}					# install rule
173.if !defined(BUILD) && !make(all) && !make(${PROG})
174${_PROG}:	.MADE					# no build at install
175.endif
176.endif
177	${_MKTARGET_INSTALL}
178	dirs=${_INST_DIRS:Q}; \
179	for d in $$dirs; do \
180		${INSTALL_DIR} $$d; \
181	done
182	${INSTALL_DIR} ${KMODULEDIR}
183	${INSTALL_FILE} -o ${KMODULEOWN} -g ${KMODULEGRP} -m ${KMODULEMODE} \
184		${.ALLSRC} ${.TARGET}
185
186kmodinstall::	${_PROG}
187.PHONY:		kmodinstall
188.PRECIOUS:	${_PROG}				# keep if install fails
189
190.undef _PROG
191.endif # !target(kmodinstall)
192
193##### Clean rules
194CLEANFILES+= a.out [Ee]rrs mklog core *.core ${PROG} ${OBJS} ${LOBJS}
195
196##### Custom rules
197lint: ${LOBJS}
198.if defined(LOBJS) && !empty(LOBJS)
199	${LINT} ${LINTFLAGS} ${LDFLAGS:C/-L[  ]*/-L/Wg:M-L*} ${LOBJS} ${LDADD}
200.endif
201
202##### Pull in related .mk logic
203LINKSOWN?= ${KMODULEOWN}
204LINKSGRP?= ${KMODULEGRP}
205LINKSMODE?= ${KMODULEMODE}
206.include <bsd.man.mk>
207.include <bsd.links.mk>
208.include <bsd.dep.mk>
209.include <bsd.clean.mk>
210
211.-include "$S/arch/${MACHINE_CPU}/include/Makefile.inc"
212.-include "$S/arch/${MACHINE}/include/Makefile.inc"
213