xref: /minix3/share/mk/bsd.kmodule.mk (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc#	$NetBSD: bsd.kmodule.mk,v 1.55 2015/07/09 14:50:08 matt Exp $
29152e1c5SLionel Sambuc
39152e1c5SLionel Sambuc# We are not building this with PIE
49152e1c5SLionel SambucMKPIE=no
59152e1c5SLionel Sambuc
69152e1c5SLionel Sambuc.include <bsd.init.mk>
79152e1c5SLionel Sambuc.include <bsd.klinks.mk>
89152e1c5SLionel Sambuc.include <bsd.sys.mk>
99152e1c5SLionel Sambuc
109152e1c5SLionel Sambuc##### Basic targets
119152e1c5SLionel Sambucrealinstall:	kmodinstall
129152e1c5SLionel Sambuc
139152e1c5SLionel SambucKERN=		$S/kern
14*0a6a1f1dSLionel SambucMKLDSCRIPT?=	no
159152e1c5SLionel Sambuc
169152e1c5SLionel SambucCFLAGS+=	-ffreestanding ${COPTS}
179152e1c5SLionel SambucCPPFLAGS+=	-nostdinc -I. -I${.CURDIR} -isystem $S -isystem $S/arch
189152e1c5SLionel SambucCPPFLAGS+=	-isystem ${S}/../common/include
199152e1c5SLionel SambucCPPFLAGS+=	-D_KERNEL -D_LKM -D_MODULE -DSYSCTL_INCLUDE_DESCR
209152e1c5SLionel Sambuc
219152e1c5SLionel Sambuc# XXX until the kernel is fixed again...
229152e1c5SLionel SambucCFLAGS+=	-fno-strict-aliasing -Wno-pointer-sign
239152e1c5SLionel Sambuc
249152e1c5SLionel Sambuc# XXX This is a workaround for platforms that have relative relocations
259152e1c5SLionel Sambuc# that, when relocated by the module loader, result in addresses that
269152e1c5SLionel Sambuc# overflow the size of the relocation (e.g. R_PPC_REL24 in powerpc).
279152e1c5SLionel Sambuc# The real solution to this involves generating trampolines for those
289152e1c5SLionel Sambuc# relocations inside the loader and removing this workaround, as the
299152e1c5SLionel Sambuc# resulting code would be much faster.
309152e1c5SLionel Sambuc.if ${MACHINE_CPU} == "arm"
3184d9c625SLionel SambucCFLAGS+=	-fno-common -fno-unwind-tables
3284d9c625SLionel Sambuc.elif ${MACHINE_CPU} == "hppa"
339152e1c5SLionel SambucCFLAGS+=	-mlong-calls
349152e1c5SLionel Sambuc.elif ${MACHINE_CPU} == "powerpc"
35*0a6a1f1dSLionel SambucCFLAGS+=	${${ACTIVE_CC} == "gcc":? -mlongcall :}
369152e1c5SLionel Sambuc.elif ${MACHINE_CPU} == "vax"
379152e1c5SLionel SambucCFLAGS+=	-fno-pic
38*0a6a1f1dSLionel Sambuc.elif ${MACHINE_CPU} == "riscv"
39*0a6a1f1dSLionel SambucCFLAGS+=	-fPIC -Wa,-fno-pic
40*0a6a1f1dSLionel Sambuc.elif ${MACHINE_ARCH} == "mips64eb" && !defined(BSD_MK_COMPAT_FILE)
41*0a6a1f1dSLionel SambucCFLAGS+=	-mabi=64
42*0a6a1f1dSLionel SambucLDFLAGS+=	-Wl,-m,elf64btsmip
43*0a6a1f1dSLionel Sambuc.elif ${MACHINE_ARCH} == "mips64el" && !defined(BSD_MK_COMPAT_FILE)
44*0a6a1f1dSLionel SambucCFLAGS+=	-mabi=64
45*0a6a1f1dSLionel SambucLDFLAGS+=	-Wl,-m,elf64ltsmip
469152e1c5SLionel Sambuc.endif
479152e1c5SLionel Sambuc
48*0a6a1f1dSLionel Sambuc.if ${MACHINE_CPU} == "sparc64"
4984d9c625SLionel Sambuc# force same memory model as rest of the kernel
50*0a6a1f1dSLionel SambucCFLAGS+=	${${ACTIVE_CC} == "gcc":? -mcmodel=medlow :}
51*0a6a1f1dSLionel SambucCFLAGS+=	${${ACTIVE_CC} == "clang":? -mcmodel=small :}
5284d9c625SLionel Sambuc.endif
5384d9c625SLionel Sambuc
549152e1c5SLionel Sambuc# evbppc needs some special help
559152e1c5SLionel Sambuc.if ${MACHINE} == "evbppc"
569152e1c5SLionel Sambuc
579152e1c5SLionel Sambuc. ifndef PPC_INTR_IMPL
589152e1c5SLionel SambucPPC_INTR_IMPL=\"powerpc/intr.h\"
599152e1c5SLionel Sambuc. endif
609152e1c5SLionel Sambuc. ifndef PPC_PCI_MACHDEP_IMPL
619152e1c5SLionel SambucPPC_PCI_MACHDEP_IMPL=\"powerpc/pci_machdep.h\"
629152e1c5SLionel Sambuc. endif
639152e1c5SLionel SambucCPPFLAGS+=      -DPPC_INTR_IMPL=${PPC_INTR_IMPL}
649152e1c5SLionel SambucCPPFLAGS+=      -DPPC_PCI_MACHDEP_IMPL=${DPPC_PCI_MACHDEP_IMPL}
659152e1c5SLionel Sambuc
669152e1c5SLionel Sambuc. ifdef PPC_IBM4XX
679152e1c5SLionel SambucCPPFLAGS+=      -DPPC_IBM4XX
689152e1c5SLionel Sambuc. elifdef PPC_BOOKE
699152e1c5SLionel SambucCPPFLAGS+=      -DPPC_BOOKE
709152e1c5SLionel Sambuc. else
719152e1c5SLionel SambucCPPFLAGS+=      -DPPC_OEA
729152e1c5SLionel Sambuc. endif
739152e1c5SLionel Sambuc
749152e1c5SLionel Sambuc.endif
759152e1c5SLionel Sambuc
769152e1c5SLionel Sambuc
779152e1c5SLionel Sambuc_YKMSRCS=	${SRCS:M*.[ly]:C/\..$/.c/} ${YHEADER:D${SRCS:M*.y:.y=.h}}
789152e1c5SLionel SambucDPSRCS+=	${_YKMSRCS}
799152e1c5SLionel SambucCLEANFILES+=	${_YKMSRCS}
809152e1c5SLionel Sambuc
819152e1c5SLionel Sambuc.if exists($S/../sys/modules/xldscripts/kmodule)
82*0a6a1f1dSLionel SambucKMODSCRIPTSRC=	$S/../sys/modules/xldscripts/kmodule
839152e1c5SLionel Sambuc.else
84*0a6a1f1dSLionel SambucKMODSCRIPTSRC=	${DESTDIR}/usr/libdata/ldscripts/kmodule
85*0a6a1f1dSLionel Sambuc.endif
86*0a6a1f1dSLionel Sambuc.if ${MKLDSCRIPT} == "yes"
87*0a6a1f1dSLionel SambucKMODSCRIPT=	kldscript
88*0a6a1f1dSLionel SambucMKLDSCRIPTSH=
89*0a6a1f1dSLionel Sambuc.else
90*0a6a1f1dSLionel SambucKMODSCRIPT=	${KMODSCRIPTSRC}
919152e1c5SLionel Sambuc.endif
929152e1c5SLionel Sambuc
939152e1c5SLionel SambucPROG?=		${KMOD}.kmod
949152e1c5SLionel Sambuc
959152e1c5SLionel Sambuc##### Build rules
969152e1c5SLionel Sambucrealall:	${PROG}
979152e1c5SLionel Sambuc
989152e1c5SLionel Sambuc.if (defined(USE_COMBINE) && ${USE_COMBINE} != "no" && !commands(${_P}) \
999152e1c5SLionel Sambuc   && !defined(NOCOMBINE.${_P}) && !defined(NOCOMBINE))
1009152e1c5SLionel Sambuc.for f in ${SRCS:N*.h:N*.sh:N*.fth:C/\.[yl]$/.c/g}
1019152e1c5SLionel Sambuc.if (${CPPFLAGS.$f:D1} == "1" || ${CPUFLAGS.$f:D2} == "2" \
1029152e1c5SLionel Sambuc     || ${COPTS.$f:D3} == "3" || ${OBJCOPTS.$f:D4} == "4" \
1039152e1c5SLionel Sambuc     || ${CXXFLAGS.$f:D5} == "5") \
1049152e1c5SLionel Sambuc    || ("${f:M*.[cyl]}" == "" || commands(${f:R:S/$/.o/}))
1059152e1c5SLionel SambucXOBJS+=		${f:R:S/$/.o/}
1069152e1c5SLionel Sambuc.else
1079152e1c5SLionel SambucXSRCS+=		${f}
1089152e1c5SLionel SambucNODPSRCS+=	${f}
1099152e1c5SLionel Sambuc.endif
1109152e1c5SLionel Sambuc.endfor
1119152e1c5SLionel Sambuc
1129152e1c5SLionel Sambuc.if !empty(XOBJS)
1139152e1c5SLionel Sambuc${XOBJS}:	${DPSRCS}
1149152e1c5SLionel Sambuc.endif
1159152e1c5SLionel Sambuc
116*0a6a1f1dSLionel Sambuc.if ${MKLDSCRIPT} == "yes"
117*0a6a1f1dSLionel Sambuc${KMODSCRIPT}: ${KMODSCRIPTSRC} ${XOBJS} $S/conf/mkldscript.sh
118*0a6a1f1dSLionel Sambuc	@rm -f ${.TARGET}
119*0a6a1f1dSLionel Sambuc	@OBJDUMP=${OBJDUMP} ${HOST_SH} $S/conf/mkldscript.sh \
120*0a6a1f1dSLionel Sambuc	    -t ${KMODSCRIPTSRC} ${XOBJS} > ${.TARGET}
121*0a6a1f1dSLionel Sambuc.endif
122*0a6a1f1dSLionel Sambuc
123*0a6a1f1dSLionel Sambuc${PROG}: ${XOBJS} ${XSRCS} ${DPSRCS} ${DPADD} ${KMODSCRIPT}
1249152e1c5SLionel Sambuc	${CC} ${LDFLAGS} -nostdlib -MD -combine -r -Wl,-T,${KMODSCRIPT},-d \
125*0a6a1f1dSLionel Sambuc		-Wl,-Map=${.TARGET}.map \
1269152e1c5SLionel Sambuc		-o ${.TARGET} ${CFLAGS} ${CPPFLAGS} ${XOBJS} \
1279152e1c5SLionel Sambuc		${XSRCS:@.SRC.@${.ALLSRC:M*.c:M*${.SRC.}}@:O:u} && \
1289152e1c5SLionel Sambuc	echo '.-include "${KMOD}.d"' > .depend
1299152e1c5SLionel Sambuc
1309152e1c5SLionel Sambuc.else
1319152e1c5SLionel SambucOBJS+=		${SRCS:N*.h:N*.sh:R:S/$/.o/g}
1329152e1c5SLionel Sambuc
1339152e1c5SLionel Sambuc${OBJS} ${LOBJS}: ${DPSRCS}
1349152e1c5SLionel Sambuc
135*0a6a1f1dSLionel Sambuc.if ${MKLDSCRIPT} == "yes"
136*0a6a1f1dSLionel Sambuc${KMODSCRIPT}: ${KMODSCRIPTSRC} ${OBJS} $S/conf/mkldscript.sh
137*0a6a1f1dSLionel Sambuc	@rm -f ${.TARGET}
138*0a6a1f1dSLionel Sambuc	@OBJDUMP=${OBJDUMP} ${HOST_SH} $S/conf/mkldscript.sh \
139*0a6a1f1dSLionel Sambuc	    -t ${KMODSCRIPTSRC} ${OBJS} > ${.TARGET}
140*0a6a1f1dSLionel Sambuc.endif
141*0a6a1f1dSLionel Sambuc
14284d9c625SLionel Sambuc.if ${MACHINE_CPU} == "arm"
14384d9c625SLionel Sambuc# The solution to limited branch space involves generating trampolines for
14484d9c625SLionel Sambuc# those relocations while creating the module, as the resulting code will
14584d9c625SLionel Sambuc# be much faster and simplifies the loader.
14684d9c625SLionel SambucARCHDIR=	$S/modules/arch/${MACHINE_CPU}
14784d9c625SLionel SambucASM_H=		$S/arch/${MACHINE_CPU}/include/asm.h
14884d9c625SLionel SambucCLEANFILES+=	tmp.o tmp.S ${KMOD}_tmp.o ${KMOD}_tramp.o ${KMOD}_tramp.S
14984d9c625SLionel Sambuc${KMOD}_tmp.o: ${OBJS} ${DPADD}
15084d9c625SLionel Sambuc	${_MKTARGET_LINK}
15184d9c625SLionel Sambuc	${LD} -r -o tmp.o ${OBJS}
15284d9c625SLionel Sambuc	${LD} -r \
15384d9c625SLionel Sambuc		`${OBJDUMP} --syms --reloc tmp.o | \
15484d9c625SLionel Sambuc			${TOOL_AWK} -f ${ARCHDIR}/kmodwrap.awk` \
15584d9c625SLionel Sambuc		 -o ${.TARGET} tmp.o
15684d9c625SLionel Sambuc
15784d9c625SLionel Sambuc${KMOD}_tramp.S: ${KMOD}_tmp.o ${ARCHDIR}/kmodtramp.awk ${ASM_H}
15884d9c625SLionel Sambuc	${_MKTARGET_CREATE}
15984d9c625SLionel Sambuc	${OBJDUMP} --syms --reloc ${KMOD}_tmp.o | \
16084d9c625SLionel Sambuc		 ${TOOL_AWK} -f ${ARCHDIR}/kmodtramp.awk \
16184d9c625SLionel Sambuc		 > tmp.S && \
16284d9c625SLionel Sambuc	mv tmp.S ${.TARGET}
16384d9c625SLionel Sambuc
16484d9c625SLionel Sambuc${PROG}: ${KMOD}_tmp.o ${KMOD}_tramp.o
16584d9c625SLionel Sambuc	${_MKTARGET_LINK}
16684d9c625SLionel Sambuc.if exists(${ARCHDIR}/kmodhide.awk)
167*0a6a1f1dSLionel Sambuc	${LD} -r -Map=${.TARGET}.map \
168*0a6a1f1dSLionel Sambuc	    -o tmp.o ${KMOD}_tmp.o ${KMOD}_tramp.o
16984d9c625SLionel Sambuc	${OBJCOPY} \
17084d9c625SLionel Sambuc		`${NM} tmp.o | ${TOOL_AWK} -f ${ARCHDIR}/kmodhide.awk` \
17184d9c625SLionel Sambuc		tmp.o ${.TARGET} && \
17284d9c625SLionel Sambuc	rm tmp.o
17384d9c625SLionel Sambuc.else
174*0a6a1f1dSLionel Sambuc	${LD} -r -Map=${.TARGET}.map \
175*0a6a1f1dSLionel Sambuc	    -o ${.TARGET} ${KMOD}_tmp.o ${KMOD}_tramp.o
17684d9c625SLionel Sambuc.endif
17784d9c625SLionel Sambuc.else
178*0a6a1f1dSLionel Sambuc${PROG}: ${OBJS} ${DPADD} ${KMODSCRIPT}
1799152e1c5SLionel Sambuc	${_MKTARGET_LINK}
1809152e1c5SLionel Sambuc	${CC} ${LDFLAGS} -nostdlib -r -Wl,-T,${KMODSCRIPT},-d \
181*0a6a1f1dSLionel Sambuc		-Wl,-Map=${.TARGET}.map \
1829152e1c5SLionel Sambuc		-o ${.TARGET} ${OBJS}
1839152e1c5SLionel Sambuc.endif
18484d9c625SLionel Sambuc.endif
1859152e1c5SLionel Sambuc
1869152e1c5SLionel Sambuc##### Install rules
1879152e1c5SLionel Sambuc.if !target(kmodinstall)
1889152e1c5SLionel Sambuc.if !defined(KMODULEDIR)
1899152e1c5SLionel Sambuc_OSRELEASE!=	${HOST_SH} $S/conf/osrelease.sh -k
1909152e1c5SLionel Sambuc# Ensure these are recorded properly in METALOG on unprived installes:
1919152e1c5SLionel SambucKMODULEARCHDIR?= ${MACHINE}
1929152e1c5SLionel Sambuc_INST_DIRS=	${DESTDIR}/stand/${KMODULEARCHDIR}
1939152e1c5SLionel Sambuc_INST_DIRS+=	${DESTDIR}/stand/${KMODULEARCHDIR}/${_OSRELEASE}
1949152e1c5SLionel Sambuc_INST_DIRS+=	${DESTDIR}/stand/${KMODULEARCHDIR}/${_OSRELEASE}/modules
1959152e1c5SLionel SambucKMODULEDIR=	${DESTDIR}/stand/${KMODULEARCHDIR}/${_OSRELEASE}/modules/${KMOD}
1969152e1c5SLionel Sambuc.endif
1979152e1c5SLionel Sambuc_PROG:=		${KMODULEDIR}/${PROG} # installed path
1989152e1c5SLionel Sambuc
1999152e1c5SLionel Sambuc.if ${MKUPDATE} == "no"
2009152e1c5SLionel Sambuc${_PROG}! ${PROG}					# install rule
2019152e1c5SLionel Sambuc.if !defined(BUILD) && !make(all) && !make(${PROG})
2029152e1c5SLionel Sambuc${_PROG}!	.MADE					# no build at install
2039152e1c5SLionel Sambuc.endif
2049152e1c5SLionel Sambuc.else
2059152e1c5SLionel Sambuc${_PROG}: ${PROG}					# install rule
2069152e1c5SLionel Sambuc.if !defined(BUILD) && !make(all) && !make(${PROG})
2079152e1c5SLionel Sambuc${_PROG}:	.MADE					# no build at install
2089152e1c5SLionel Sambuc.endif
2099152e1c5SLionel Sambuc.endif
2109152e1c5SLionel Sambuc	${_MKTARGET_INSTALL}
2119152e1c5SLionel Sambuc	dirs=${_INST_DIRS:Q}; \
2129152e1c5SLionel Sambuc	for d in $$dirs; do \
2139152e1c5SLionel Sambuc		${INSTALL_DIR} $$d; \
2149152e1c5SLionel Sambuc	done
2159152e1c5SLionel Sambuc	${INSTALL_DIR} ${KMODULEDIR}
2169152e1c5SLionel Sambuc	${INSTALL_FILE} -o ${KMODULEOWN} -g ${KMODULEGRP} -m ${KMODULEMODE} \
2179152e1c5SLionel Sambuc		${.ALLSRC} ${.TARGET}
2189152e1c5SLionel Sambuc
2199152e1c5SLionel Sambuckmodinstall::	${_PROG}
2209152e1c5SLionel Sambuc.PHONY:		kmodinstall
2219152e1c5SLionel Sambuc.PRECIOUS:	${_PROG}				# keep if install fails
2229152e1c5SLionel Sambuc
2239152e1c5SLionel Sambuc.undef _PROG
2249152e1c5SLionel Sambuc.endif # !target(kmodinstall)
2259152e1c5SLionel Sambuc
2269152e1c5SLionel Sambuc##### Clean rules
2279152e1c5SLionel SambucCLEANFILES+= a.out [Ee]rrs mklog core *.core ${PROG} ${OBJS} ${LOBJS}
228*0a6a1f1dSLionel SambucCLEANFILES+= ${PROG}.map
229*0a6a1f1dSLionel Sambuc.if ${MKLDSCRIPT} == "yes"
230*0a6a1f1dSLionel SambucCLEANFILES+= kldscript
231*0a6a1f1dSLionel Sambuc.endif
2329152e1c5SLionel Sambuc
2339152e1c5SLionel Sambuc##### Custom rules
2349152e1c5SLionel Sambuclint: ${LOBJS}
2359152e1c5SLionel Sambuc.if defined(LOBJS) && !empty(LOBJS)
2369152e1c5SLionel Sambuc	${LINT} ${LINTFLAGS} ${LDFLAGS:C/-L[  ]*/-L/Wg:M-L*} ${LOBJS} ${LDADD}
2379152e1c5SLionel Sambuc.endif
2389152e1c5SLionel Sambuc
2399152e1c5SLionel Sambuc##### Pull in related .mk logic
2409152e1c5SLionel SambucLINKSOWN?= ${KMODULEOWN}
2419152e1c5SLionel SambucLINKSGRP?= ${KMODULEGRP}
2429152e1c5SLionel SambucLINKSMODE?= ${KMODULEMODE}
2439152e1c5SLionel Sambuc.include <bsd.man.mk>
2449152e1c5SLionel Sambuc.include <bsd.links.mk>
2459152e1c5SLionel Sambuc.include <bsd.dep.mk>
2469152e1c5SLionel Sambuc.include <bsd.clean.mk>
2479152e1c5SLionel Sambuc
2489152e1c5SLionel Sambuc.-include "$S/arch/${MACHINE_CPU}/include/Makefile.inc"
2499152e1c5SLionel Sambuc.-include "$S/arch/${MACHINE}/include/Makefile.inc"
250