Home | History | Annotate | Line # | Download | only in mk
bsd.kmodule.mk revision 1.45
      1 #	$NetBSD: bsd.kmodule.mk,v 1.45 2014/09/19 17:45:42 matt 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 		-o ${.TARGET} ${CFLAGS} ${CPPFLAGS} ${XOBJS} \
    107 		${XSRCS:@.SRC.@${.ALLSRC:M*.c:M*${.SRC.}}@:O:u} && \
    108 	echo '.-include "${KMOD}.d"' > .depend
    109 
    110 .else
    111 OBJS+=		${SRCS:N*.h:N*.sh:R:S/$/.o/g}
    112 
    113 ${OBJS} ${LOBJS}: ${DPSRCS}
    114 
    115 .if ${MACHINE_CPU} == "arm"
    116 # The solution to limited branch space involves generating trampolines for
    117 # those relocations while creating the module, as the resulting code will
    118 # be much faster and simplifies the loader.
    119 ARCHDIR=	$S/modules/arch/${MACHINE_CPU}
    120 ASM_H=		$S/arch/${MACHINE_CPU}/include/asm.h
    121 CLEANFILES+=	tmp.o tmp.S ${KMOD}_tmp.o ${KMOD}_tramp.o ${KMOD}_tramp.S
    122 ${KMOD}_tmp.o: ${OBJS} ${DPADD}
    123 	${_MKTARGET_LINK}
    124 	${LD} -r -o tmp.o ${OBJS}
    125 	${LD} -r \
    126 		`${OBJDUMP} --syms --reloc tmp.o | \
    127 			${TOOL_AWK} -f ${ARCHDIR}/kmodwrap.awk` \
    128 		 -o ${.TARGET} tmp.o
    129 
    130 ${KMOD}_tramp.S: ${KMOD}_tmp.o ${ARCHDIR}/kmodtramp.awk ${ASM_H}
    131 	${_MKTARGET_CREATE}
    132 	${OBJDUMP} --syms --reloc ${KMOD}_tmp.o | \
    133 		 ${TOOL_AWK} -f ${ARCHDIR}/kmodtramp.awk \
    134 		 > tmp.S && \
    135 	mv tmp.S ${.TARGET}
    136 
    137 ${PROG}: ${KMOD}_tmp.o ${KMOD}_tramp.o
    138 	${_MKTARGET_LINK}
    139 .if exists(${ARCHDIR}/kmodhide.awk)
    140 	${LD} -r -o tmp.o ${KMOD}_tmp.o ${KMOD}_tramp.o
    141 	${OBJCOPY} \
    142 		`${NM} tmp.o | ${TOOL_AWK} -f ${ARCHDIR}/kmodhide.awk` \
    143 		tmp.o ${.TARGET} && \
    144 	rm tmp.o
    145 .else
    146 	${LD} -r -o ${.TARGET} ${KMOD}_tmp.o ${KMOD}_tramp.o
    147 .endif
    148 .else
    149 ${PROG}: ${OBJS} ${DPADD}
    150 	${_MKTARGET_LINK}
    151 	${CC} ${LDFLAGS} -nostdlib -r -Wl,-T,${KMODSCRIPT},-d \
    152 		-o ${.TARGET} ${OBJS}
    153 .endif
    154 .endif
    155 
    156 ##### Install rules
    157 .if !target(kmodinstall)
    158 .if !defined(KMODULEDIR)
    159 _OSRELEASE!=	${HOST_SH} $S/conf/osrelease.sh -k
    160 # Ensure these are recorded properly in METALOG on unprived installes:
    161 KMODULEARCHDIR?= ${MACHINE}
    162 _INST_DIRS=	${DESTDIR}/stand/${KMODULEARCHDIR}
    163 _INST_DIRS+=	${DESTDIR}/stand/${KMODULEARCHDIR}/${_OSRELEASE}
    164 _INST_DIRS+=	${DESTDIR}/stand/${KMODULEARCHDIR}/${_OSRELEASE}/modules
    165 KMODULEDIR=	${DESTDIR}/stand/${KMODULEARCHDIR}/${_OSRELEASE}/modules/${KMOD}
    166 .endif
    167 _PROG:=		${KMODULEDIR}/${PROG} # installed path
    168 
    169 .if ${MKUPDATE} == "no"
    170 ${_PROG}! ${PROG}					# install rule
    171 .if !defined(BUILD) && !make(all) && !make(${PROG})
    172 ${_PROG}!	.MADE					# no build at install
    173 .endif
    174 .else
    175 ${_PROG}: ${PROG}					# install rule
    176 .if !defined(BUILD) && !make(all) && !make(${PROG})
    177 ${_PROG}:	.MADE					# no build at install
    178 .endif
    179 .endif
    180 	${_MKTARGET_INSTALL}
    181 	dirs=${_INST_DIRS:Q}; \
    182 	for d in $$dirs; do \
    183 		${INSTALL_DIR} $$d; \
    184 	done
    185 	${INSTALL_DIR} ${KMODULEDIR}
    186 	${INSTALL_FILE} -o ${KMODULEOWN} -g ${KMODULEGRP} -m ${KMODULEMODE} \
    187 		${.ALLSRC} ${.TARGET}
    188 
    189 kmodinstall::	${_PROG}
    190 .PHONY:		kmodinstall
    191 .PRECIOUS:	${_PROG}				# keep if install fails
    192 
    193 .undef _PROG
    194 .endif # !target(kmodinstall)
    195 
    196 ##### Clean rules
    197 CLEANFILES+= a.out [Ee]rrs mklog core *.core ${PROG} ${OBJS} ${LOBJS}
    198 
    199 ##### Custom rules
    200 lint: ${LOBJS}
    201 .if defined(LOBJS) && !empty(LOBJS)
    202 	${LINT} ${LINTFLAGS} ${LDFLAGS:C/-L[  ]*/-L/Wg:M-L*} ${LOBJS} ${LDADD}
    203 .endif
    204 
    205 ##### Pull in related .mk logic
    206 LINKSOWN?= ${KMODULEOWN}
    207 LINKSGRP?= ${KMODULEGRP}
    208 LINKSMODE?= ${KMODULEMODE}
    209 .include <bsd.man.mk>
    210 .include <bsd.links.mk>
    211 .include <bsd.dep.mk>
    212 .include <bsd.clean.mk>
    213 
    214 .-include "$S/arch/${MACHINE_CPU}/include/Makefile.inc"
    215 .-include "$S/arch/${MACHINE}/include/Makefile.inc"
    216