FreeBSD/Linux Kernel Cross Reference
sys/conf/kmod.mk
1 # From: @(#)bsd.prog.mk 5.26 (Berkeley) 6/25/91
2 # $FreeBSD: releng/11.0/sys/conf/kmod.mk 301284 2016-06-03 19:25:36Z bdrewery $
3 #
4 # The include file <bsd.kmod.mk> handles building and installing loadable
5 # kernel modules.
6 #
7 #
8 # +++ variables +++
9 #
10 # CLEANFILES Additional files to remove for the clean and cleandir targets.
11 #
12 # EXPORT_SYMS A list of symbols that should be exported from the module,
13 # or the name of a file containing a list of symbols, or YES
14 # to export all symbols. If not defined, no symbols are
15 # exported.
16 #
17 # KMOD The name of the kernel module to build.
18 #
19 # KMODDIR Base path for kernel modules (see kld(4)). [/boot/kernel]
20 #
21 # KMODOWN Module file owner. [${BINOWN}]
22 #
23 # KMODGRP Module file group. [${BINGRP}]
24 #
25 # KMODMODE Module file mode. [${BINMODE}]
26 #
27 # KMODLOAD Command to load a kernel module [/sbin/kldload]
28 #
29 # KMODUNLOAD Command to unload a kernel module [/sbin/kldunload]
30 #
31 # KMODISLOADED Command to check whether a kernel module is
32 # loaded [/sbin/kldstat -q -n]
33 #
34 # PROG The name of the kernel module to build.
35 # If not supplied, ${KMOD}.ko is used.
36 #
37 # SRCS List of source files.
38 #
39 # FIRMWS List of firmware images in format filename:shortname:version
40 #
41 # FIRMWARE_LICENSE
42 # Set to the name of the license the user has to agree on in
43 # order to use this firmware. See /usr/share/doc/legal
44 #
45 # DESTDIR The tree where the module gets installed. [not set]
46 #
47 # +++ targets +++
48 #
49 # install:
50 # install the kernel module; if the Makefile
51 # does not itself define the target install, the targets
52 # beforeinstall and afterinstall may also be used to cause
53 # actions immediately before and after the install target
54 # is executed.
55 #
56 # load:
57 # Load a module.
58 #
59 # unload:
60 # Unload a module.
61 #
62 # reload:
63 # Unload if loaded, then load.
64 #
65
66 AWK?= awk
67 KMODLOAD?= /sbin/kldload
68 KMODUNLOAD?= /sbin/kldunload
69 KMODISLOADED?= /sbin/kldstat -q -n
70 OBJCOPY?= objcopy
71
72 .include <bsd.init.mk>
73 # Grab all the options for a kernel build. For backwards compat, we need to
74 # do this after bsd.own.mk.
75 .include "kern.opts.mk"
76 .include <bsd.compiler.mk>
77 .include "config.mk"
78
79 # Search for kernel source tree in standard places.
80 .for _dir in ${.CURDIR}/../.. ${.CURDIR}/../../.. /sys /usr/src/sys
81 .if !defined(SYSDIR) && exists(${_dir}/kern/)
82 SYSDIR= ${_dir:tA}
83 .endif
84 .endfor
85 .if !defined(SYSDIR) || !exists(${SYSDIR}/kern/)
86 .error "can't find kernel source tree"
87 .endif
88
89 .SUFFIXES: .out .o .c .cc .cxx .C .y .l .s .S .m
90
91 # amd64 and mips use direct linking for kmod, all others use shared binaries
92 .if ${MACHINE_CPUARCH} != amd64 && ${MACHINE_CPUARCH} != mips
93 __KLD_SHARED=yes
94 .else
95 __KLD_SHARED=no
96 .endif
97
98 .if !empty(CFLAGS:M-O[23s]) && empty(CFLAGS:M-fno-strict-aliasing)
99 CFLAGS+= -fno-strict-aliasing
100 .endif
101 WERROR?= -Werror
102 CFLAGS+= ${WERROR}
103 CFLAGS+= -D_KERNEL
104 CFLAGS+= -DKLD_MODULE
105
106 # Don't use any standard or source-relative include directories.
107 NOSTDINC= -nostdinc
108 CFLAGS:= ${CFLAGS:N-I*} ${NOSTDINC} ${INCLMAGIC} ${CFLAGS:M-I*}
109 .if defined(KERNBUILDDIR)
110 CFLAGS+= -DHAVE_KERNEL_OPTION_HEADERS -include ${KERNBUILDDIR}/opt_global.h
111 .endif
112
113 # Add -I paths for system headers. Individual module makefiles don't
114 # need any -I paths for this. Similar defaults for .PATH can't be
115 # set because there are no standard paths for non-headers.
116 CFLAGS+= -I. -I${SYSDIR}
117
118 CFLAGS.gcc+= -finline-limit=${INLINE_LIMIT}
119 CFLAGS.gcc+= -fms-extensions
120 CFLAGS.gcc+= --param inline-unit-growth=100
121 CFLAGS.gcc+= --param large-function-growth=1000
122
123 # Disallow common variables, and if we end up with commons from
124 # somewhere unexpected, allocate storage for them in the module itself.
125 CFLAGS+= -fno-common
126 LDFLAGS+= -d -warn-common
127
128 CFLAGS+= ${DEBUG_FLAGS}
129 .if ${MACHINE_CPUARCH} == amd64
130 CFLAGS+= -fno-omit-frame-pointer -mno-omit-leaf-frame-pointer
131 .endif
132
133 .if ${MACHINE_CPUARCH} == "aarch64" || ${MACHINE_CPUARCH} == "riscv"
134 CFLAGS+= -fPIC
135 .endif
136
137 # Temporary workaround for PR 196407, which contains the fascinating details.
138 # Don't allow clang to use fpu instructions or registers in kernel modules.
139 .if ${MACHINE_CPUARCH} == arm
140 .if ${COMPILER_VERSION} < 30800
141 CFLAGS.clang+= -mllvm -arm-use-movt=0
142 .else
143 CFLAGS.clang+= -mno-movt
144 .endif
145 CFLAGS.clang+= -mfpu=none
146 CFLAGS+= -funwind-tables
147 .endif
148
149 .if ${MACHINE_CPUARCH} == powerpc
150 CFLAGS+= -mlongcall -fno-omit-frame-pointer
151 .endif
152
153 .if ${MACHINE_CPUARCH} == mips
154 CFLAGS+= -G0 -fno-pic -mno-abicalls -mlong-calls
155 .endif
156
157 .if defined(DEBUG) || defined(DEBUG_FLAGS)
158 CTFFLAGS+= -g
159 .endif
160
161 .if defined(FIRMWS)
162 ${KMOD:S/$/.c/}: ${SYSDIR}/tools/fw_stub.awk
163 ${AWK} -f ${SYSDIR}/tools/fw_stub.awk ${FIRMWS} -m${KMOD} -c${KMOD:S/$/.c/g} \
164 ${FIRMWARE_LICENSE:C/.+/-l/}${FIRMWARE_LICENSE}
165
166 SRCS+= ${KMOD:S/$/.c/}
167 CLEANFILES+= ${KMOD:S/$/.c/}
168
169 .for _firmw in ${FIRMWS}
170 ${_firmw:C/\:.*$/.fwo/:T}: ${_firmw:C/\:.*$//}
171 @${ECHO} ${_firmw:C/\:.*$//} ${.ALLSRC:M*${_firmw:C/\:.*$//}}
172 @if [ -e ${_firmw:C/\:.*$//} ]; then \
173 ${LD} -b binary --no-warn-mismatch ${_LDFLAGS} \
174 -r -d -o ${.TARGET} ${_firmw:C/\:.*$//}; \
175 else \
176 ln -s ${.ALLSRC:M*${_firmw:C/\:.*$//}} ${_firmw:C/\:.*$//}; \
177 ${LD} -b binary --no-warn-mismatch ${_LDFLAGS} \
178 -r -d -o ${.TARGET} ${_firmw:C/\:.*$//}; \
179 rm ${_firmw:C/\:.*$//}; \
180 fi
181
182 OBJS+= ${_firmw:C/\:.*$/.fwo/:T}
183 .endfor
184 .endif
185
186 # Conditionally include SRCS based on kernel config options.
187 .for _o in ${KERN_OPTS}
188 SRCS+=${SRCS.${_o}}
189 .endfor
190
191 OBJS+= ${SRCS:N*.h:R:S/$/.o/g}
192
193 .if !defined(PROG)
194 PROG= ${KMOD}.ko
195 .endif
196
197 .if !defined(DEBUG_FLAGS)
198 FULLPROG= ${PROG}
199 .else
200 FULLPROG= ${PROG}.full
201 ${PROG}: ${FULLPROG} ${PROG}.debug
202 ${OBJCOPY} --strip-debug --add-gnu-debuglink=${PROG}.debug \
203 ${FULLPROG} ${.TARGET}
204 ${PROG}.debug: ${FULLPROG}
205 ${OBJCOPY} --only-keep-debug ${FULLPROG} ${.TARGET}
206 .endif
207
208 .if ${__KLD_SHARED} == yes
209 ${FULLPROG}: ${KMOD}.kld
210 .if ${MACHINE_CPUARCH} != "aarch64"
211 ${LD} -Bshareable ${_LDFLAGS} -o ${.TARGET} ${KMOD}.kld
212 .else
213 #XXXKIB Relocatable linking in aarch64 ld from binutils 2.25.1 does
214 # not work. The linker corrupts the references to the external
215 # symbols which are defined by other object in the linking set
216 # and should therefore loose the GOT entry. The problem seems
217 # to be fixed in the binutils-gdb git HEAD as of 2015-10-04. Hack
218 # below allows to get partially functioning modules for now.
219 ${LD} -Bshareable ${_LDFLAGS} -o ${.TARGET} ${OBJS}
220 .endif
221 .if !defined(DEBUG_FLAGS)
222 ${OBJCOPY} --strip-debug ${.TARGET}
223 .endif
224 .endif
225
226 EXPORT_SYMS?= NO
227 .if ${EXPORT_SYMS} != YES
228 CLEANFILES+= export_syms
229 .endif
230
231 .if ${__KLD_SHARED} == yes
232 ${KMOD}.kld: ${OBJS}
233 .else
234 ${FULLPROG}: ${OBJS}
235 .endif
236 ${LD} ${_LDFLAGS} -r -d -o ${.TARGET} ${OBJS}
237 .if ${MK_CTF} != "no"
238 ${CTFMERGE} ${CTFFLAGS} -o ${.TARGET} ${OBJS}
239 .endif
240 .if defined(EXPORT_SYMS)
241 .if ${EXPORT_SYMS} != YES
242 .if ${EXPORT_SYMS} == NO
243 :> export_syms
244 .elif !exists(${.CURDIR}/${EXPORT_SYMS})
245 echo ${EXPORT_SYMS} > export_syms
246 .else
247 grep -v '^#' < ${EXPORT_SYMS} > export_syms
248 .endif
249 ${AWK} -f ${SYSDIR}/conf/kmod_syms.awk ${.TARGET} \
250 export_syms | xargs -J% ${OBJCOPY} % ${.TARGET}
251 .endif
252 .endif
253 .if !defined(DEBUG_FLAGS) && ${__KLD_SHARED} == no
254 ${OBJCOPY} --strip-debug ${.TARGET}
255 .endif
256
257 _ILINKS=machine
258 .if ${MACHINE} != ${MACHINE_CPUARCH} && ${MACHINE} != "arm64"
259 _ILINKS+=${MACHINE_CPUARCH}
260 .endif
261 .if ${MACHINE_CPUARCH} == "i386" || ${MACHINE_CPUARCH} == "amd64"
262 _ILINKS+=x86
263 .endif
264 CLEANFILES+=${_ILINKS}
265
266 all: ${PROG}
267
268 beforedepend: ${_ILINKS}
269 beforebuild: ${_ILINKS}
270
271 # Ensure that the links exist without depending on it when it exists which
272 # causes all the modules to be rebuilt when the directory pointed to changes.
273 .for _link in ${_ILINKS}
274 .if !exists(${.OBJDIR}/${_link})
275 ${OBJS}: ${_link}
276 .endif
277 .endfor
278
279 .NOPATH: ${_ILINKS}
280
281 ${_ILINKS}:
282 @case ${.TARGET} in \
283 machine) \
284 path=${SYSDIR}/${MACHINE}/include ;; \
285 *) \
286 path=${SYSDIR}/${.TARGET:T}/include ;; \
287 esac ; \
288 path=`(cd $$path && /bin/pwd)` ; \
289 ${ECHO} ${.TARGET:T} "->" $$path ; \
290 ln -fhs $$path ${.TARGET:T}
291
292 CLEANFILES+= ${PROG} ${KMOD}.kld ${OBJS}
293
294 .if defined(DEBUG_FLAGS)
295 CLEANFILES+= ${FULLPROG} ${PROG}.debug
296 .endif
297
298 .if !target(install)
299
300 _INSTALLFLAGS:= ${INSTALLFLAGS}
301 .for ie in ${INSTALLFLAGS_EDIT}
302 _INSTALLFLAGS:= ${_INSTALLFLAGS${ie}}
303 .endfor
304
305 .if !target(realinstall)
306 KERN_DEBUGDIR?= ${DEBUGDIR}
307 realinstall: _kmodinstall
308 .ORDER: beforeinstall _kmodinstall
309 _kmodinstall: .PHONY
310 ${INSTALL} -T release -o ${KMODOWN} -g ${KMODGRP} -m ${KMODMODE} \
311 ${_INSTALLFLAGS} ${PROG} ${DESTDIR}${KMODDIR}/
312 .if defined(DEBUG_FLAGS) && !defined(INSTALL_NODEBUG) && ${MK_KERNEL_SYMBOLS} != "no"
313 ${INSTALL} -T debug -o ${KMODOWN} -g ${KMODGRP} -m ${KMODMODE} \
314 ${_INSTALLFLAGS} ${PROG}.debug ${DESTDIR}${KERN_DEBUGDIR}${KMODDIR}/
315 .endif
316
317 .include <bsd.links.mk>
318
319 .if !defined(NO_XREF)
320 afterinstall: _kldxref
321 .ORDER: realinstall _kldxref
322 .ORDER: _installlinks _kldxref
323 _kldxref: .PHONY
324 @if type kldxref >/dev/null 2>&1; then \
325 ${ECHO} kldxref ${DESTDIR}${KMODDIR}; \
326 kldxref ${DESTDIR}${KMODDIR}; \
327 fi
328 .endif
329 .endif # !target(realinstall)
330
331 .endif # !target(install)
332
333 .if !target(load)
334 load: ${PROG} .PHONY
335 ${KMODLOAD} -v ${.OBJDIR}/${PROG}
336 .endif
337
338 .if !target(unload)
339 unload: .PHONY
340 if ${KMODISLOADED} ${PROG} ; then ${KMODUNLOAD} -v ${PROG} ; fi
341 .endif
342
343 .if !target(reload)
344 reload: unload load .PHONY
345 .endif
346
347 .if defined(KERNBUILDDIR)
348 .PATH: ${KERNBUILDDIR}
349 CFLAGS+= -I${KERNBUILDDIR}
350 .for _src in ${SRCS:Mopt_*.h}
351 CLEANFILES+= ${_src}
352 .if !target(${_src})
353 ${_src}:
354 ln -sf ${KERNBUILDDIR}/${_src} ${.TARGET}
355 .endif
356 .endfor
357 .else
358 .for _src in ${SRCS:Mopt_*.h}
359 CLEANFILES+= ${_src}
360 .if !target(${_src})
361 ${_src}:
362 :> ${.TARGET}
363 .endif
364 .endfor
365 .endif
366
367 # Respect configuration-specific C flags.
368 CFLAGS+= ${CONF_CFLAGS}
369
370 .if !empty(SRCS:Mvnode_if.c)
371 CLEANFILES+= vnode_if.c
372 vnode_if.c: ${SYSDIR}/tools/vnode_if.awk ${SYSDIR}/kern/vnode_if.src
373 ${AWK} -f ${SYSDIR}/tools/vnode_if.awk ${SYSDIR}/kern/vnode_if.src -c
374 .endif
375
376 .if !empty(SRCS:Mvnode_if.h)
377 CLEANFILES+= vnode_if.h vnode_if_newproto.h vnode_if_typedef.h
378 vnode_if.h vnode_if_newproto.h vnode_if_typedef.h: ${SYSDIR}/tools/vnode_if.awk \
379 ${SYSDIR}/kern/vnode_if.src
380 vnode_if.h: vnode_if_newproto.h vnode_if_typedef.h
381 ${AWK} -f ${SYSDIR}/tools/vnode_if.awk ${SYSDIR}/kern/vnode_if.src -h
382 vnode_if_newproto.h:
383 ${AWK} -f ${SYSDIR}/tools/vnode_if.awk ${SYSDIR}/kern/vnode_if.src -p
384 vnode_if_typedef.h:
385 ${AWK} -f ${SYSDIR}/tools/vnode_if.awk ${SYSDIR}/kern/vnode_if.src -q
386 .endif
387
388 # Build _if.[ch] from _if.m, and clean them when we're done.
389 # __MPATH defined in config.mk
390 _MFILES=${__MPATH:T:O}
391 _MPATH=${__MPATH:H:O:u}
392 .PATH.m: ${_MPATH}
393 .for _i in ${SRCS:M*_if.[ch]}
394 _MATCH=M${_i:R:S/$/.m/}
395 _MATCHES=${_MFILES:${_MATCH}}
396 .if !empty(_MATCHES)
397 CLEANFILES+= ${_i}
398 .endif
399 .endfor # _i
400 .m.c: ${SYSDIR}/tools/makeobjops.awk
401 ${AWK} -f ${SYSDIR}/tools/makeobjops.awk ${.IMPSRC} -c
402
403 .m.h: ${SYSDIR}/tools/makeobjops.awk
404 ${AWK} -f ${SYSDIR}/tools/makeobjops.awk ${.IMPSRC} -h
405
406 .for _i in mii pccard
407 .if !empty(SRCS:M${_i}devs.h)
408 CLEANFILES+= ${_i}devs.h
409 ${_i}devs.h: ${SYSDIR}/tools/${_i}devs2h.awk ${SYSDIR}/dev/${_i}/${_i}devs
410 ${AWK} -f ${SYSDIR}/tools/${_i}devs2h.awk ${SYSDIR}/dev/${_i}/${_i}devs
411 .endif
412 .endfor # _i
413
414 .if !empty(SRCS:Mbhnd_nvram_map.h)
415 CLEANFILES+= bhnd_nvram_map.h
416 bhnd_nvram_map.h: ${SYSDIR}/dev/bhnd/tools/nvram_map_gen.awk \
417 ${SYSDIR}/dev/bhnd/tools/nvram_map_gen.sh \
418 ${SYSDIR}/dev/bhnd/nvram/nvram_map
419 bhnd_nvram_map.h:
420 sh ${SYSDIR}/dev/bhnd/tools/nvram_map_gen.sh \
421 ${SYSDIR}/dev/bhnd/nvram/nvram_map -h
422 .endif
423
424 .if !empty(SRCS:Mbhnd_nvram_map_data.h)
425 CLEANFILES+= bhnd_nvram_map_data.h
426 bhnd_nvram_map_data.h: ${SYSDIR}/dev/bhnd/tools/nvram_map_gen.awk \
427 ${SYSDIR}/dev/bhnd/tools/nvram_map_gen.sh \
428 ${SYSDIR}/dev/bhnd/nvram/nvram_map
429 bhnd_nvram_map_data.h:
430 sh ${SYSDIR}/dev/bhnd/tools/nvram_map_gen.sh \
431 ${SYSDIR}/dev/bhnd/nvram/nvram_map -d
432 .endif
433
434 .if !empty(SRCS:Musbdevs.h)
435 CLEANFILES+= usbdevs.h
436 usbdevs.h: ${SYSDIR}/tools/usbdevs2h.awk ${SYSDIR}/dev/usb/usbdevs
437 ${AWK} -f ${SYSDIR}/tools/usbdevs2h.awk ${SYSDIR}/dev/usb/usbdevs -h
438 .endif
439
440 .if !empty(SRCS:Musbdevs_data.h)
441 CLEANFILES+= usbdevs_data.h
442 usbdevs_data.h: ${SYSDIR}/tools/usbdevs2h.awk ${SYSDIR}/dev/usb/usbdevs
443 ${AWK} -f ${SYSDIR}/tools/usbdevs2h.awk ${SYSDIR}/dev/usb/usbdevs -d
444 .endif
445
446 .if !empty(SRCS:Macpi_quirks.h)
447 CLEANFILES+= acpi_quirks.h
448 acpi_quirks.h: ${SYSDIR}/tools/acpi_quirks2h.awk ${SYSDIR}/dev/acpica/acpi_quirks
449 ${AWK} -f ${SYSDIR}/tools/acpi_quirks2h.awk ${SYSDIR}/dev/acpica/acpi_quirks
450 .endif
451
452 .if !empty(SRCS:Massym.s)
453 CLEANFILES+= assym.s genassym.o
454 assym.s: genassym.o
455 .if defined(KERNBUILDDIR)
456 genassym.o: opt_global.h
457 .endif
458 assym.s: ${SYSDIR}/kern/genassym.sh
459 sh ${SYSDIR}/kern/genassym.sh genassym.o > ${.TARGET}
460 genassym.o: ${SYSDIR}/${MACHINE}/${MACHINE}/genassym.c
461 genassym.o: ${SRCS:Mopt_*.h}
462 ${CC} -c ${CFLAGS:N-fno-common} \
463 ${SYSDIR}/${MACHINE}/${MACHINE}/genassym.c
464 .endif
465
466 lint: ${SRCS}
467 ${LINT} ${LINTKERNFLAGS} ${CFLAGS:M-[DILU]*} ${.ALLSRC:M*.c}
468
469 .if defined(KERNBUILDDIR)
470 ${OBJS}: opt_global.h
471 .endif
472
473 CLEANDEPENDFILES+= ${_ILINKS}
474 # .depend needs include links so we remove them only together.
475 cleanilinks:
476 rm -f ${_ILINKS}
477
478 OBJS_DEPEND_GUESS+= ${SRCS:M*.h}
479
480 .include <bsd.dep.mk>
481 .include <bsd.clang-analyze.mk>
482 .include <bsd.obj.mk>
483 .include "kern.mk"
Cache object: d59425443745f7bce0175a1a152a82fc
|