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