FreeBSD/Linux Kernel Cross Reference
sys/conf/kmod.mk
1 # From: @(#)bsd.prog.mk 5.26 (Berkeley) 6/25/91
2 # $FreeBSD$
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 "kmod.opts.mk"
73
74 # Search for kernel source tree in standard places.
75 .for _dir in ${.CURDIR}/../.. ${.CURDIR}/../../.. /sys /usr/src/sys
76 .if !defined(SYSDIR) && exists(${_dir}/kern/)
77 SYSDIR= ${_dir:tA}
78 .endif
79 .endfor
80 .if !defined(SYSDIR) || !exists(${SYSDIR}/kern/)
81 .error "can't find kernel source tree"
82 .endif
83
84 .SUFFIXES: .out .o .c .cc .cxx .C .y .l .s .S .m
85
86 # amd64 and mips use direct linking for kmod, all others use shared binaries
87 .if ${MACHINE_CPUARCH} != amd64 && ${MACHINE_CPUARCH} != mips
88 __KLD_SHARED=yes
89 .else
90 __KLD_SHARED=no
91 .endif
92
93 .if !empty(CFLAGS:M-O[23s]) && empty(CFLAGS:M-fno-strict-aliasing)
94 CFLAGS+= -fno-strict-aliasing
95 .endif
96 .if ${COMPILER_TYPE} == "gcc" && ${COMPILER_VERSION} < 50000
97 WERROR?= -Wno-error
98 .else
99 WERROR?= -Werror
100 .endif
101
102 LINUXKPI_GENSRCS+= \
103 bus_if.h \
104 device_if.h \
105 pci_if.h \
106 pci_iov_if.h \
107 vnode_if.h \
108 usb_if.h \
109 opt_usb.h \
110 opt_stack.h
111
112 CFLAGS+= ${WERROR}
113 CFLAGS+= -D_KERNEL
114 CFLAGS+= -DKLD_MODULE
115
116 # Don't use any standard or source-relative include directories.
117 NOSTDINC= -nostdinc
118 CFLAGS:= ${CFLAGS:N-I*} ${NOSTDINC} ${INCLMAGIC} ${CFLAGS:M-I*}
119 .if defined(KERNBUILDDIR)
120 CFLAGS+= -DHAVE_KERNEL_OPTION_HEADERS -include ${KERNBUILDDIR}/opt_global.h
121 .endif
122
123 # Add -I paths for system headers. Individual module makefiles don't
124 # need any -I paths for this. Similar defaults for .PATH can't be
125 # set because there are no standard paths for non-headers.
126 CFLAGS+= -I. -I${SYSDIR}
127
128 CFLAGS.gcc+= -finline-limit=${INLINE_LIMIT}
129 CFLAGS.gcc+= -fms-extensions
130 CFLAGS.gcc+= --param inline-unit-growth=100
131 CFLAGS.gcc+= --param large-function-growth=1000
132
133 # Disallow common variables, and if we end up with commons from
134 # somewhere unexpected, allocate storage for them in the module itself.
135 CFLAGS+= -fno-common
136 LDFLAGS+= -d -warn-common
137
138 .if defined(LINKER_FEATURES) && ${LINKER_FEATURES:Mbuild-id}
139 LDFLAGS+= -Wl,--build-id=sha1
140 .endif
141
142 CFLAGS+= ${DEBUG_FLAGS}
143 .if ${MACHINE_CPUARCH} == amd64
144 CFLAGS+= -fno-omit-frame-pointer -mno-omit-leaf-frame-pointer
145 .endif
146
147 .if ${MACHINE_CPUARCH} == "aarch64" || ${MACHINE_CPUARCH} == "riscv"
148 CFLAGS+= -fPIC
149 .endif
150
151 # Temporary workaround for PR 196407, which contains the fascinating details.
152 # Don't allow clang to use fpu instructions or registers in kernel modules.
153 .if ${MACHINE_CPUARCH} == arm
154 .if ${COMPILER_VERSION} < 30800
155 CFLAGS.clang+= -mllvm -arm-use-movt=0
156 .else
157 CFLAGS.clang+= -mno-movt
158 .endif
159 CFLAGS.clang+= -mfpu=none
160 CFLAGS+= -funwind-tables
161 .endif
162
163 .if ${MACHINE_CPUARCH} == powerpc
164 CFLAGS+= -mlongcall -fno-omit-frame-pointer
165 .endif
166
167 .if ${MACHINE_CPUARCH} == mips
168 CFLAGS+= -G0 -fno-pic -mno-abicalls -mlong-calls
169 .endif
170
171 .if defined(DEBUG) || defined(DEBUG_FLAGS)
172 CTFFLAGS+= -g
173 .endif
174
175 .if defined(FIRMWS)
176 ${KMOD:S/$/.c/}: ${SYSDIR}/tools/fw_stub.awk
177 ${AWK} -f ${SYSDIR}/tools/fw_stub.awk ${FIRMWS} -m${KMOD} -c${KMOD:S/$/.c/g} \
178 ${FIRMWARE_LICENSE:C/.+/-l/}${FIRMWARE_LICENSE}
179
180 SRCS+= ${KMOD:S/$/.c/}
181 CLEANFILES+= ${KMOD:S/$/.c/}
182
183 .for _firmw in ${FIRMWS}
184 ${_firmw:C/\:.*$/.fwo/:T}: ${_firmw:C/\:.*$//}
185 @${ECHO} ${_firmw:C/\:.*$//} ${.ALLSRC:M*${_firmw:C/\:.*$//}}
186 @if [ -e ${_firmw:C/\:.*$//} ]; then \
187 ${LD} -b binary --no-warn-mismatch ${_LDFLAGS} \
188 -m ${LD_EMULATION} -r -d \
189 -o ${.TARGET} ${_firmw:C/\:.*$//}; \
190 else \
191 ln -s ${.ALLSRC:M*${_firmw:C/\:.*$//}} ${_firmw:C/\:.*$//}; \
192 ${LD} -b binary --no-warn-mismatch ${_LDFLAGS} \
193 -m ${LD_EMULATION} -r -d \
194 -o ${.TARGET} ${_firmw:C/\:.*$//}; \
195 rm ${_firmw:C/\:.*$//}; \
196 fi
197
198 OBJS+= ${_firmw:C/\:.*$/.fwo/:T}
199 .endfor
200 .endif
201
202 # Conditionally include SRCS based on kernel config options.
203 .for _o in ${KERN_OPTS}
204 SRCS+=${SRCS.${_o}}
205 .endfor
206
207 OBJS+= ${SRCS:N*.h:R:S/$/.o/g}
208
209 .if !defined(PROG)
210 PROG= ${KMOD}.ko
211 .endif
212
213 .if !defined(DEBUG_FLAGS)
214 FULLPROG= ${PROG}
215 .else
216 FULLPROG= ${PROG}.full
217 ${PROG}: ${FULLPROG} ${PROG}.debug
218 ${OBJCOPY} --strip-debug --add-gnu-debuglink=${PROG}.debug \
219 ${FULLPROG} ${.TARGET}
220 ${PROG}.debug: ${FULLPROG}
221 ${OBJCOPY} --only-keep-debug ${FULLPROG} ${.TARGET}
222 .endif
223
224 .if ${__KLD_SHARED} == yes
225 ${FULLPROG}: ${KMOD}.kld
226 ${LD} -m ${LD_EMULATION} -Bshareable -znotext -znorelro ${_LDFLAGS} \
227 -o ${.TARGET} ${KMOD}.kld
228 .if !defined(DEBUG_FLAGS)
229 ${OBJCOPY} --strip-debug ${.TARGET}
230 .endif
231 .endif
232
233 EXPORT_SYMS?= NO
234 .if ${EXPORT_SYMS} != YES
235 CLEANFILES+= export_syms
236 .endif
237
238 .if ${__KLD_SHARED} == yes
239 ${KMOD}.kld: ${OBJS}
240 .else
241 ${FULLPROG}: ${OBJS}
242 .endif
243 ${LD} -m ${LD_EMULATION} ${_LDFLAGS} -r -d -o ${.TARGET} ${OBJS}
244 .if ${MK_CTF} != "no"
245 ${CTFMERGE} ${CTFFLAGS} -o ${.TARGET} ${OBJS}
246 .endif
247 .if defined(EXPORT_SYMS)
248 .if ${EXPORT_SYMS} != YES
249 .if ${EXPORT_SYMS} == NO
250 :> export_syms
251 .elif !exists(${.CURDIR}/${EXPORT_SYMS})
252 echo -n "${EXPORT_SYMS:@s@$s${.newline}@}" > export_syms
253 .else
254 grep -v '^#' < ${EXPORT_SYMS} > export_syms
255 .endif
256 ${AWK} -f ${SYSDIR}/conf/kmod_syms.awk ${.TARGET} \
257 export_syms | xargs -J% ${OBJCOPY} % ${.TARGET}
258 .endif
259 .endif # defined(EXPORT_SYMS)
260 .if !defined(DEBUG_FLAGS) && ${__KLD_SHARED} == no
261 ${OBJCOPY} --strip-debug ${.TARGET}
262 .endif
263
264 _ILINKS=machine
265 .if ${MACHINE} != ${MACHINE_CPUARCH} && ${MACHINE} != "arm64"
266 _ILINKS+=${MACHINE_CPUARCH}
267 .endif
268 .if ${MACHINE_CPUARCH} == "i386" || ${MACHINE_CPUARCH} == "amd64"
269 _ILINKS+=x86
270 .endif
271 CLEANFILES+=${_ILINKS}
272
273 all: ${PROG}
274
275 beforedepend: ${_ILINKS}
276 beforebuild: ${_ILINKS}
277
278 # Ensure that the links exist without depending on it when it exists which
279 # causes all the modules to be rebuilt when the directory pointed to changes.
280 .for _link in ${_ILINKS}
281 .if !exists(${.OBJDIR}/${_link})
282 OBJS_DEPEND_GUESS+= ${_link}
283 .endif
284 .endfor
285
286 .NOPATH: ${_ILINKS}
287
288 ${_ILINKS}:
289 @case ${.TARGET} in \
290 machine) \
291 path=${SYSDIR}/${MACHINE}/include ;; \
292 *) \
293 path=${SYSDIR}/${.TARGET:T}/include ;; \
294 esac ; \
295 path=`(cd $$path && /bin/pwd)` ; \
296 ${ECHO} ${.TARGET:T} "->" $$path ; \
297 ln -fhs $$path ${.TARGET:T}
298
299 CLEANFILES+= ${PROG} ${KMOD}.kld ${OBJS}
300
301 .if defined(DEBUG_FLAGS)
302 CLEANFILES+= ${FULLPROG} ${PROG}.debug
303 .endif
304
305 .if !target(install)
306
307 _INSTALLFLAGS:= ${INSTALLFLAGS}
308 .for ie in ${INSTALLFLAGS_EDIT}
309 _INSTALLFLAGS:= ${_INSTALLFLAGS${ie}}
310 .endfor
311
312 .if !target(realinstall)
313 KERN_DEBUGDIR?= ${DEBUGDIR}
314 realinstall: _kmodinstall
315 .ORDER: beforeinstall _kmodinstall
316 _kmodinstall: .PHONY
317 ${INSTALL} -T release -o ${KMODOWN} -g ${KMODGRP} -m ${KMODMODE} \
318 ${_INSTALLFLAGS} ${PROG} ${DESTDIR}${KMODDIR}/
319 .if defined(DEBUG_FLAGS) && !defined(INSTALL_NODEBUG) && ${MK_KERNEL_SYMBOLS} != "no"
320 ${INSTALL} -T debug -o ${KMODOWN} -g ${KMODGRP} -m ${KMODMODE} \
321 ${_INSTALLFLAGS} ${PROG}.debug ${DESTDIR}${KERN_DEBUGDIR}${KMODDIR}/
322 .endif
323
324 .include <bsd.links.mk>
325
326 .if !defined(NO_XREF)
327 afterinstall: _kldxref
328 .ORDER: realinstall _kldxref
329 .ORDER: _installlinks _kldxref
330 _kldxref: .PHONY
331 @if type kldxref >/dev/null 2>&1; then \
332 ${ECHO} kldxref ${DESTDIR}${KMODDIR}; \
333 kldxref ${DESTDIR}${KMODDIR}; \
334 fi
335 .endif
336 .endif # !target(realinstall)
337
338 .endif # !target(install)
339
340 .if !target(load)
341 load: ${PROG} .PHONY
342 ${KMODLOAD} -v ${.OBJDIR}/${PROG}
343 .endif
344
345 .if !target(unload)
346 unload: .PHONY
347 if ${KMODISLOADED} ${PROG} ; then ${KMODUNLOAD} -v ${PROG} ; fi
348 .endif
349
350 .if !target(reload)
351 reload: unload load .PHONY
352 .endif
353
354 .if defined(KERNBUILDDIR)
355 .PATH: ${KERNBUILDDIR}
356 CFLAGS+= -I${KERNBUILDDIR}
357 .for _src in ${SRCS:Mopt_*.h}
358 CLEANFILES+= ${_src}
359 .if !target(${_src})
360 ${_src}:
361 ln -sf ${KERNBUILDDIR}/${_src} ${.TARGET}
362 .endif
363 .endfor
364 .else
365 .for _src in ${SRCS:Mopt_*.h}
366 CLEANFILES+= ${_src}
367 .if !target(${_src})
368 ${_src}:
369 :> ${.TARGET}
370 .endif
371 .endfor
372 .endif
373
374 # Respect configuration-specific C flags.
375 CFLAGS+= ${ARCH_FLAGS} ${CONF_CFLAGS}
376
377 .if !empty(SRCS:Mvnode_if.c)
378 CLEANFILES+= vnode_if.c
379 vnode_if.c: ${SYSDIR}/tools/vnode_if.awk ${SYSDIR}/kern/vnode_if.src
380 ${AWK} -f ${SYSDIR}/tools/vnode_if.awk ${SYSDIR}/kern/vnode_if.src -c
381 .endif
382
383 .if !empty(SRCS:Mvnode_if.h)
384 CLEANFILES+= vnode_if.h vnode_if_newproto.h vnode_if_typedef.h
385 vnode_if.h vnode_if_newproto.h vnode_if_typedef.h: ${SYSDIR}/tools/vnode_if.awk \
386 ${SYSDIR}/kern/vnode_if.src
387 vnode_if.h: vnode_if_newproto.h vnode_if_typedef.h
388 ${AWK} -f ${SYSDIR}/tools/vnode_if.awk ${SYSDIR}/kern/vnode_if.src -h
389 vnode_if_newproto.h:
390 ${AWK} -f ${SYSDIR}/tools/vnode_if.awk ${SYSDIR}/kern/vnode_if.src -p
391 vnode_if_typedef.h:
392 ${AWK} -f ${SYSDIR}/tools/vnode_if.awk ${SYSDIR}/kern/vnode_if.src -q
393 .endif
394
395 # Build _if.[ch] from _if.m, and clean them when we're done.
396 # __MPATH defined in config.mk
397 _MFILES=${__MPATH:T:O}
398 _MPATH=${__MPATH:H:O:u}
399 .PATH.m: ${_MPATH}
400 .for _i in ${SRCS:M*_if.[ch]}
401 _MATCH=M${_i:R:S/$/.m/}
402 _MATCHES=${_MFILES:${_MATCH}}
403 .if !empty(_MATCHES)
404 CLEANFILES+= ${_i}
405 .endif
406 .endfor # _i
407 .m.c: ${SYSDIR}/tools/makeobjops.awk
408 ${AWK} -f ${SYSDIR}/tools/makeobjops.awk ${.IMPSRC} -c
409
410 .m.h: ${SYSDIR}/tools/makeobjops.awk
411 ${AWK} -f ${SYSDIR}/tools/makeobjops.awk ${.IMPSRC} -h
412
413 .for _i in mii pccard
414 .if !empty(SRCS:M${_i}devs.h)
415 CLEANFILES+= ${_i}devs.h
416 ${_i}devs.h: ${SYSDIR}/tools/${_i}devs2h.awk ${SYSDIR}/dev/${_i}/${_i}devs
417 ${AWK} -f ${SYSDIR}/tools/${_i}devs2h.awk ${SYSDIR}/dev/${_i}/${_i}devs
418 .endif
419 .endfor # _i
420
421 .if !empty(SRCS:Mbhnd_nvram_map.h)
422 CLEANFILES+= bhnd_nvram_map.h
423 bhnd_nvram_map.h: ${SYSDIR}/dev/bhnd/tools/nvram_map_gen.awk \
424 ${SYSDIR}/dev/bhnd/tools/nvram_map_gen.sh \
425 ${SYSDIR}/dev/bhnd/nvram/nvram_map
426 bhnd_nvram_map.h:
427 sh ${SYSDIR}/dev/bhnd/tools/nvram_map_gen.sh \
428 ${SYSDIR}/dev/bhnd/nvram/nvram_map -h
429 .endif
430
431 .if !empty(SRCS:Mbhnd_nvram_map_data.h)
432 CLEANFILES+= bhnd_nvram_map_data.h
433 bhnd_nvram_map_data.h: ${SYSDIR}/dev/bhnd/tools/nvram_map_gen.awk \
434 ${SYSDIR}/dev/bhnd/tools/nvram_map_gen.sh \
435 ${SYSDIR}/dev/bhnd/nvram/nvram_map
436 bhnd_nvram_map_data.h:
437 sh ${SYSDIR}/dev/bhnd/tools/nvram_map_gen.sh \
438 ${SYSDIR}/dev/bhnd/nvram/nvram_map -d
439 .endif
440
441 .if !empty(SRCS:Musbdevs.h)
442 CLEANFILES+= usbdevs.h
443 usbdevs.h: ${SYSDIR}/tools/usbdevs2h.awk ${SYSDIR}/dev/usb/usbdevs
444 ${AWK} -f ${SYSDIR}/tools/usbdevs2h.awk ${SYSDIR}/dev/usb/usbdevs -h
445 .endif
446
447 .if !empty(SRCS:Musbdevs_data.h)
448 CLEANFILES+= usbdevs_data.h
449 usbdevs_data.h: ${SYSDIR}/tools/usbdevs2h.awk ${SYSDIR}/dev/usb/usbdevs
450 ${AWK} -f ${SYSDIR}/tools/usbdevs2h.awk ${SYSDIR}/dev/usb/usbdevs -d
451 .endif
452
453 .if !empty(SRCS:Macpi_quirks.h)
454 CLEANFILES+= acpi_quirks.h
455 acpi_quirks.h: ${SYSDIR}/tools/acpi_quirks2h.awk ${SYSDIR}/dev/acpica/acpi_quirks
456 ${AWK} -f ${SYSDIR}/tools/acpi_quirks2h.awk ${SYSDIR}/dev/acpica/acpi_quirks
457 .endif
458
459 .if !empty(SRCS:Massym.s) || !empty(DPSRCS:Massym.s)
460 CLEANFILES+= assym.s genassym.o
461 DEPENDOBJS+= genassym.o
462 assym.s: genassym.o
463 assym.s: ${SYSDIR}/kern/genassym.sh
464 sh ${SYSDIR}/kern/genassym.sh genassym.o > ${.TARGET}
465 genassym.o: ${SYSDIR}/${MACHINE}/${MACHINE}/genassym.c
466 genassym.o: ${SRCS:Mopt_*.h}
467 ${CC} -c ${CFLAGS:N-flto:N-fno-common} -fcommon \
468 ${SYSDIR}/${MACHINE}/${MACHINE}/genassym.c
469 .endif
470
471 lint: ${SRCS}
472 ${LINT} ${LINTKERNFLAGS} ${CFLAGS:M-[DILU]*} ${.ALLSRC:M*.c}
473
474 CLEANDEPENDFILES+= ${_ILINKS}
475 # .depend needs include links so we remove them only together.
476 cleanilinks:
477 rm -f ${_ILINKS}
478
479 OBJS_DEPEND_GUESS+= ${SRCS:M*.h}
480 .if defined(KERNBUILDDIR)
481 OBJS_DEPEND_GUESS+= opt_global.h
482 .endif
483
484 .include <bsd.dep.mk>
485 .include <bsd.clang-analyze.mk>
486 .include <bsd.obj.mk>
487 .include "kern.mk"
Cache object: e93e9fae500005d9da0d736cbf61e9cc
|