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