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