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