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