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