Home | History | Annotate | Line # | Download | only in mk
bsd.kmodule.mk revision 1.83
      1  1.83     lukem #	$NetBSD: bsd.kmodule.mk,v 1.83 2023/06/03 08:52:56 lukem Exp $
      2  1.14      elad 
      3  1.14      elad # We are not building this with PIE
      4  1.14      elad MKPIE=no
      5   1.1        ad 
      6   1.1        ad .include <bsd.init.mk>
      7   1.1        ad .include <bsd.klinks.mk>
      8  1.57  christos 
      9  1.57  christos .if ${MKCTF:Uno} == "yes"
     10  1.57  christos CFLAGS+=	-g
     11  1.57  christos # Only need symbols for ctf, strip them after converting to CTF
     12  1.57  christos CTFFLAGS=	-L VERSION
     13  1.58  christos CTFMFLAGS=	-t -L VERSION
     14  1.62   hannken # Keep symbols if built with "-g"
     15  1.66  christos .if !empty(COPTS:M*-g*) || ${MKDEBUG:Uno} == "yes"
     16  1.66  christos CTFFLAGS+=     -g
     17  1.66  christos CTFMFLAGS+=    -g
     18  1.62   hannken .endif
     19  1.57  christos .endif
     20  1.57  christos 
     21   1.7        ad .include <bsd.sys.mk>
     22   1.1        ad 
     23   1.1        ad ##### Basic targets
     24   1.1        ad realinstall:	kmodinstall
     25   1.1        ad 
     26   1.1        ad KERN=		$S/kern
     27  1.47  christos MKLDSCRIPT?=	no
     28   1.1        ad 
     29   1.6        ad CFLAGS+=	-ffreestanding ${COPTS}
     30   1.1        ad CPPFLAGS+=	-nostdinc -I. -I${.CURDIR} -isystem $S -isystem $S/arch
     31   1.1        ad CPPFLAGS+=	-isystem ${S}/../common/include
     32  1.69     skrll CPPFLAGS+=	-D_KERNEL -D_MODULE -DSYSCTL_INCLUDE_DESCR
     33  1.81  riastrad CPPFLAGS+=	${${MKDTRACE:Uno} != "no" :? -DKDTRACE_HOOKS :}
     34   1.1        ad 
     35  1.72  christos CWARNFLAGS.clang+=	-Wno-error=constant-conversion
     36  1.59     joerg 
     37   1.1        ad # XXX until the kernel is fixed again...
     38  1.78  riastrad CFLAGS+=	-fno-strict-aliasing
     39  1.78  riastrad CWARNFLAGS+=	-Wno-pointer-sign -Wno-attributes
     40  1.80  riastrad CWARNFLAGS+=	-Wno-type-limits
     41  1.83     lukem CWARNFLAGS.gcc+=	${CC_WNO_ADDRESS_OF_PACKED_MEMBER}
     42  1.79  riastrad CWARNFLAGS.clang+=	-Wno-error=address-of-packed-member
     43   1.1        ad 
     44  1.24      jmmv # XXX This is a workaround for platforms that have relative relocations
     45  1.24      jmmv # that, when relocated by the module loader, result in addresses that
     46  1.24      jmmv # overflow the size of the relocation (e.g. R_PPC_REL24 in powerpc).
     47  1.24      jmmv # The real solution to this involves generating trampolines for those
     48  1.24      jmmv # relocations inside the loader and removing this workaround, as the
     49  1.24      jmmv # resulting code would be much faster.
     50  1.63  jmcneill .if ${MACHINE_CPU} == "aarch64"
     51  1.63  jmcneill CFLAGS+=	-march=armv8-a+nofp+nosimd
     52  1.63  jmcneill .elif ${MACHINE_CPU} == "arm"
     53  1.41  jmcneill CFLAGS+=	-fno-common -fno-unwind-tables
     54  1.38     skrll .elif ${MACHINE_CPU} == "hppa"
     55  1.68     skrll CFLAGS+=	-mlong-calls -mno-space-regs -mfast-indirect-calls
     56  1.24      jmmv .elif ${MACHINE_CPU} == "powerpc"
     57  1.44     joerg CFLAGS+=	${${ACTIVE_CC} == "gcc":? -mlongcall :}
     58  1.71       rin CFLAGS+=	${${ACTIVE_CC} == "gcc" && ${HAVE_GCC:U0} >= 9:? -mno-pltseq :}
     59  1.31      matt .elif ${MACHINE_CPU} == "vax"
     60  1.31      matt CFLAGS+=	-fno-pic
     61  1.45      matt .elif ${MACHINE_CPU} == "riscv"
     62  1.82     skrll CFLAGS+=	-mcmodel=medany
     63  1.54      matt .elif ${MACHINE_ARCH} == "mips64eb" && !defined(BSD_MK_COMPAT_FILE)
     64  1.53      matt CFLAGS+=	-mabi=64
     65  1.76    simonb AFLAGS+=	-mabi=64
     66  1.53      matt LDFLAGS+=	-Wl,-m,elf64btsmip
     67  1.54      matt .elif ${MACHINE_ARCH} == "mips64el" && !defined(BSD_MK_COMPAT_FILE)
     68  1.53      matt CFLAGS+=	-mabi=64
     69  1.76    simonb AFLAGS+=	-mabi=64
     70  1.53      matt LDFLAGS+=	-Wl,-m,elf64ltsmip
     71  1.24      jmmv .endif
     72  1.24      jmmv 
     73  1.75    simonb .if ${MACHINE_CPU} == "mips"
     74  1.75    simonb # We can't use -msym32 with -mlong-calls as -msym32 forces all addresses
     75  1.75    simonb # to be 32-bit which defeats the whole purpose of long calls.
     76  1.75    simonb CFLAGS+=	-mlong-calls
     77  1.75    simonb .endif
     78  1.75    simonb 
     79  1.43  nakayama .if ${MACHINE_CPU} == "sparc64"
     80  1.40    martin # force same memory model as rest of the kernel
     81  1.42     joerg CFLAGS+=	${${ACTIVE_CC} == "gcc":? -mcmodel=medlow :}
     82  1.42     joerg CFLAGS+=	${${ACTIVE_CC} == "clang":? -mcmodel=small :}
     83  1.40    martin .endif
     84  1.40    martin 
     85  1.28       mrg # evbppc needs some special help
     86  1.28       mrg .if ${MACHINE} == "evbppc"
     87  1.28       mrg 
     88  1.28       mrg . ifndef PPC_INTR_IMPL
     89  1.28       mrg PPC_INTR_IMPL=\"powerpc/intr.h\"
     90  1.28       mrg . endif
     91  1.28       mrg . ifndef PPC_PCI_MACHDEP_IMPL
     92  1.28       mrg PPC_PCI_MACHDEP_IMPL=\"powerpc/pci_machdep.h\"
     93  1.28       mrg . endif
     94  1.73       rin CPPFLAGS+=	-DPPC_INTR_IMPL=${PPC_INTR_IMPL}
     95  1.73       rin CPPFLAGS+=	-DPPC_PCI_MACHDEP_IMPL=${DPPC_PCI_MACHDEP_IMPL}
     96  1.28       mrg 
     97  1.28       mrg . ifdef PPC_IBM4XX
     98  1.73       rin CPPFLAGS+=	-DPPC_IBM4XX
     99  1.28       mrg . elifdef PPC_BOOKE
    100  1.73       rin CPPFLAGS+=	-DPPC_BOOKE
    101  1.74       rin . elif ${MACHINE_ARCH} == "powerpc64"
    102  1.74       rin CPPFLAGS+=	-DPPC_OEA64
    103  1.28       mrg . else
    104  1.73       rin CPPFLAGS+=	-DPPC_OEA
    105  1.28       mrg . endif
    106  1.28       mrg 
    107  1.28       mrg .endif
    108  1.28       mrg 
    109  1.28       mrg 
    110   1.1        ad _YKMSRCS=	${SRCS:M*.[ly]:C/\..$/.c/} ${YHEADER:D${SRCS:M*.y:.y=.h}}
    111   1.1        ad DPSRCS+=	${_YKMSRCS}
    112   1.1        ad CLEANFILES+=	${_YKMSRCS}
    113   1.7        ad 
    114  1.21     skrll .if exists($S/../sys/modules/xldscripts/kmodule)
    115  1.47  christos KMODSCRIPTSRC=	$S/../sys/modules/xldscripts/kmodule
    116   1.7        ad .else
    117  1.47  christos KMODSCRIPTSRC=	${DESTDIR}/usr/libdata/ldscripts/kmodule
    118  1.47  christos .endif
    119  1.47  christos .if ${MKLDSCRIPT} == "yes"
    120  1.47  christos KMODSCRIPT=	kldscript
    121  1.52  christos MKLDSCRIPTSH=	
    122  1.47  christos .else
    123  1.47  christos KMODSCRIPT=	${KMODSCRIPTSRC}
    124   1.7        ad .endif
    125   1.1        ad 
    126   1.1        ad PROG?=		${KMOD}.kmod
    127  1.66  christos .if ${MKDEBUG:Uno} != "no" && !defined(NODEBUG) && !commands(${PROG}) && \
    128  1.66  christos     empty(SRCS:M*.sh)
    129  1.66  christos PROGDEBUG:=      ${PROG}.debug
    130  1.66  christos .endif  
    131   1.1        ad 
    132   1.1        ad ##### Build rules
    133  1.67       rin realall:	${PROG} ${PROGDEBUG}
    134   1.1        ad 
    135  1.35      matt OBJS+=		${SRCS:N*.h:N*.sh:R:S/$/.o/g}
    136  1.35      matt 
    137   1.1        ad ${OBJS} ${LOBJS}: ${DPSRCS}
    138   1.1        ad 
    139  1.52  christos .if ${MKLDSCRIPT} == "yes"
    140  1.52  christos ${KMODSCRIPT}: ${KMODSCRIPTSRC} ${OBJS} $S/conf/mkldscript.sh
    141  1.52  christos 	@rm -f ${.TARGET}
    142  1.52  christos 	@OBJDUMP=${OBJDUMP} ${HOST_SH} $S/conf/mkldscript.sh \
    143  1.52  christos 	    -t ${KMODSCRIPTSRC} ${OBJS} > ${.TARGET}
    144  1.52  christos .endif
    145  1.52  christos 
    146  1.39      matt .if ${MACHINE_CPU} == "arm"
    147  1.39      matt # The solution to limited branch space involves generating trampolines for
    148  1.39      matt # those relocations while creating the module, as the resulting code will
    149  1.39      matt # be much faster and simplifies the loader.
    150  1.39      matt ARCHDIR=	$S/modules/arch/${MACHINE_CPU}
    151  1.39      matt ASM_H=		$S/arch/${MACHINE_CPU}/include/asm.h
    152  1.39      matt CLEANFILES+=	tmp.o tmp.S ${KMOD}_tmp.o ${KMOD}_tramp.o ${KMOD}_tramp.S
    153  1.39      matt ${KMOD}_tmp.o: ${OBJS} ${DPADD}
    154  1.39      matt 	${_MKTARGET_LINK}
    155  1.39      matt 	${LD} -r -o tmp.o ${OBJS}
    156  1.39      matt 	${LD} -r \
    157  1.77  christos 		$$(${OBJDUMP} --syms --reloc tmp.o | \
    158  1.77  christos 			${TOOL_AWK} -f ${ARCHDIR}/kmodwrap.awk) \
    159  1.39      matt 		 -o ${.TARGET} tmp.o
    160  1.39      matt 
    161  1.39      matt ${KMOD}_tramp.S: ${KMOD}_tmp.o ${ARCHDIR}/kmodtramp.awk ${ASM_H}
    162  1.39      matt 	${_MKTARGET_CREATE}
    163  1.39      matt 	${OBJDUMP} --syms --reloc ${KMOD}_tmp.o | \
    164  1.39      matt 		 ${TOOL_AWK} -f ${ARCHDIR}/kmodtramp.awk \
    165  1.39      matt 		 > tmp.S && \
    166  1.60  christos 	${MV} tmp.S ${.TARGET}
    167  1.39      matt 
    168  1.39      matt ${PROG}: ${KMOD}_tmp.o ${KMOD}_tramp.o
    169  1.39      matt 	${_MKTARGET_LINK}
    170  1.39      matt .if exists(${ARCHDIR}/kmodhide.awk)
    171  1.46  uebayasi 	${LD} -r -Map=${.TARGET}.map \
    172  1.46  uebayasi 	    -o tmp.o ${KMOD}_tmp.o ${KMOD}_tramp.o
    173  1.39      matt 	${OBJCOPY} \
    174  1.77  christos 		$$(${NM} tmp.o | ${TOOL_AWK} -f ${ARCHDIR}/kmodhide.awk) \
    175  1.39      matt 		tmp.o ${.TARGET} && \
    176  1.39      matt 	rm tmp.o
    177  1.39      matt .else
    178  1.46  uebayasi 	${LD} -r -Map=${.TARGET}.map \
    179  1.46  uebayasi 	    -o ${.TARGET} ${KMOD}_tmp.o ${KMOD}_tramp.o
    180  1.39      matt .endif
    181  1.39      matt .else
    182  1.52  christos ${PROG}: ${OBJS} ${DPADD} ${KMODSCRIPT}
    183  1.55      matt 	${_MKTARGET_LINK}
    184  1.26    martin 	${CC} ${LDFLAGS} -nostdlib -r -Wl,-T,${KMODSCRIPT},-d \
    185  1.46  uebayasi 		-Wl,-Map=${.TARGET}.map \
    186  1.22      matt 		-o ${.TARGET} ${OBJS}
    187  1.35      matt .endif
    188  1.58  christos .if defined(CTFMERGE)
    189  1.58  christos 	${CTFMERGE} ${CTFMFLAGS} -o ${.TARGET} ${OBJS}
    190  1.58  christos .endif
    191   1.1        ad 
    192  1.66  christos .if defined(PROGDEBUG)
    193  1.66  christos ${PROGDEBUG}: ${PROG}
    194  1.66  christos 	${_MKTARGET_CREATE}
    195  1.66  christos 	(  ${OBJCOPY} --only-keep-debug ${PROG} ${PROGDEBUG} \
    196  1.66  christos 	&& ${OBJCOPY} --strip-debug -p -R .gnu_debuglink \
    197  1.66  christos 		--add-gnu-debuglink=${PROGDEBUG} ${PROG} \
    198  1.66  christos 	) || (rm -f ${PROGDEBUG}; false)
    199  1.66  christos .endif
    200  1.66  christos 
    201   1.1        ad ##### Install rules
    202   1.1        ad .if !target(kmodinstall)
    203   1.7        ad .if !defined(KMODULEDIR)
    204  1.64  christos .if ${KERNEL_DIR:Uno} == "yes"
    205  1.66  christos _INST_DIRS=	/netbsd
    206  1.66  christos _INST_DIRS+=	/netbsd/modules
    207  1.66  christos KMODULEDIR=	/netbsd/modules/${KMOD}
    208  1.64  christos .else
    209   1.9        he # Ensure these are recorded properly in METALOG on unprived installes:
    210  1.65  christos _OSRELEASE!=	${HOST_SH} $S/conf/osrelease.sh -k
    211  1.28       mrg KMODULEARCHDIR?= ${MACHINE}
    212  1.66  christos _INST_DIRS=	/stand/${KMODULEARCHDIR}
    213  1.66  christos _INST_DIRS+=	/stand/${KMODULEARCHDIR}/${_OSRELEASE}
    214  1.66  christos _INST_DIRS+=	/stand/${KMODULEARCHDIR}/${_OSRELEASE}/modules
    215  1.66  christos KMODULEDIR=	/stand/${KMODULEARCHDIR}/${_OSRELEASE}/modules/${KMOD}
    216  1.10    dyoung .endif
    217  1.64  christos .endif
    218   1.1        ad 
    219  1.66  christos _INST_DIRS+=	${KMODULEDIR}
    220  1.66  christos _PROG:=		${DESTDIR}${KMODULEDIR}/${PROG} # installed path
    221  1.66  christos 
    222  1.66  christos .if defined(PROGDEBUG)
    223  1.66  christos .for i in ${_INST_DIRS}
    224  1.66  christos _DEBUG_INST_DIRS += ${DEBUGDIR}${i}
    225  1.66  christos .endfor
    226  1.66  christos _INST_DIRS += ${_DEBUG_INST_DIRS}
    227  1.66  christos _PROGDEBUG:=	${DESTDIR}${DEBUGDIR}${KMODULEDIR}/${PROG}.debug
    228  1.66  christos .endif
    229  1.66  christos 
    230  1.66  christos .for _P P in ${_PROG} ${PROG} ${_PROGDEBUG} ${PROGDEBUG}
    231   1.1        ad .if ${MKUPDATE} == "no"
    232  1.66  christos ${_P}! ${P}					# install rule
    233  1.66  christos .if !defined(BUILD) && !make(all) && !make(${P})
    234  1.66  christos ${_P}!	.MADE					# no build at install
    235   1.1        ad .endif
    236   1.1        ad .else
    237  1.66  christos ${_P}: ${P}					# install rule
    238  1.66  christos .if !defined(BUILD) && !make(all) && !make(${P})
    239  1.66  christos ${_P}:	.MADE					# no build at install
    240   1.1        ad .endif
    241   1.1        ad .endif
    242   1.1        ad 	${_MKTARGET_INSTALL}
    243  1.16   hubertf 	dirs=${_INST_DIRS:Q}; \
    244  1.16   hubertf 	for d in $$dirs; do \
    245  1.66  christos 		${INSTALL_DIR} ${DESTDIR}$$d; \
    246   1.9        he 	done
    247  1.15        ad 	${INSTALL_FILE} -o ${KMODULEOWN} -g ${KMODULEGRP} -m ${KMODULEMODE} \
    248   1.1        ad 		${.ALLSRC} ${.TARGET}
    249   1.1        ad 
    250  1.66  christos kmodinstall::	${_P}
    251   1.1        ad .PHONY:		kmodinstall
    252  1.66  christos .PRECIOUS:	${_P}				# keep if install fails
    253  1.66  christos .endfor
    254   1.1        ad 
    255  1.66  christos .undef _PPROG
    256  1.66  christos .undef _PPROGDEBUG
    257   1.1        ad .endif # !target(kmodinstall)
    258   1.1        ad 
    259   1.1        ad ##### Clean rules
    260  1.32       apb CLEANFILES+= a.out [Ee]rrs mklog core *.core ${PROG} ${OBJS} ${LOBJS}
    261  1.66  christos CLEANFILES+= ${PROGDEBUG}
    262  1.46  uebayasi CLEANFILES+= ${PROG}.map
    263  1.47  christos .if ${MKLDSCRIPT} == "yes"
    264  1.47  christos CLEANFILES+= kldscript
    265  1.47  christos .endif
    266   1.1        ad 
    267   1.1        ad ##### Custom rules
    268   1.1        ad lint: ${LOBJS}
    269   1.1        ad .if defined(LOBJS) && !empty(LOBJS)
    270   1.1        ad 	${LINT} ${LINTFLAGS} ${LDFLAGS:C/-L[  ]*/-L/Wg:M-L*} ${LOBJS} ${LDADD}
    271   1.1        ad .endif
    272   1.1        ad 
    273   1.1        ad ##### Pull in related .mk logic
    274  1.19       apb LINKSOWN?= ${KMODULEOWN}
    275  1.19       apb LINKSGRP?= ${KMODULEGRP}
    276  1.19       apb LINKSMODE?= ${KMODULEMODE}
    277   1.1        ad .include <bsd.man.mk>
    278   1.1        ad .include <bsd.links.mk>
    279   1.1        ad .include <bsd.dep.mk>
    280  1.32       apb .include <bsd.clean.mk>
    281   1.1        ad 
    282   1.1        ad .-include "$S/arch/${MACHINE_CPU}/include/Makefile.inc"
    283   1.1        ad .-include "$S/arch/${MACHINE}/include/Makefile.inc"
    284