Whamcloud - gitweb
LU-13783 procfs: fix improper prop_ops fields
[fs/lustre-release.git] / config / lustre-build-linux.m4
1 #
2 # LB_LINUX_VERSION
3 #
4 # Set things accordingly for a linux kernel
5 #
6 AC_DEFUN([LB_LINUX_VERSION], [
7 KMODEXT=".ko"
8 AC_SUBST(KMODEXT)
9
10 makerule="$PWD/build"
11 AC_CACHE_CHECK([for external module build target], lb_cv_module_target,
12 [
13         lb_cv_module_target=""
14         MODULE_TARGET="SUBDIRS"
15         rm -f build/conftest.i
16         LB_LINUX_TRY_MAKE([], [],
17                 [$makerule LUSTRE_KERNEL_TEST=conftest.i],
18                 [test -s build/conftest.i],
19                 [lb_cv_module_target="SUBDIRS"],[
20         MODULE_TARGET="M"
21         makerule="$PWD/build/"
22         LB_LINUX_TRY_MAKE([], [],
23                 [$makerule LUSTRE_KERNEL_TEST=conftest.i],
24                 [test -s build/conftest.i],
25                 [lb_cv_module_target="M54"], [
26         MODULE_TARGET="M"
27         makerule="_module_$PWD/build"
28         LB_LINUX_TRY_MAKE([], [],
29                 [$makerule LUSTRE_KERNEL_TEST=conftest.i],
30                 [test -s build/conftest.i],
31                 [lb_cv_module_target="M"], [
32         MODULE_TARGET="M"
33         makerule=""
34         LB_LINUX_TRY_MAKE([], [],
35                 [$makerule LUSTRE_KERNEL_TEST=conftest.i],
36                 [test -s build/conftest.i],
37                 [lb_cv_module_target="M58"], [
38                         AC_MSG_ERROR([kernel module make failed; check config.log for details])
39         ])])])])
40 ])
41 AS_IF([test -z "$lb_cv_module_target"],
42         [AC_MSG_ERROR([unknown external module build target])],
43 [test "x$lb_cv_module_target" = "xM54"],
44         [makerule="$PWD/build"
45         lb_cv_module_target="M"],
46 [test "x$lb_cv_module_target" = "xM58"],
47         [makerule=""
48         lb_cv_module_target="M"],
49 [test "x$lb_cv_module_target" = "xM"],
50         [makerule="_module_$PWD/build"])
51 MODULE_TARGET=$lb_cv_module_target
52 AC_SUBST(MODULE_TARGET)
53 ])
54
55 #
56 # LB_LINUX_UTSRELEASE
57 #
58 # Determine the Linux kernel version string from the utsrelease
59 #
60 AC_DEFUN([LB_LINUX_UTSRELEASE], [
61 AC_CACHE_CHECK([for Linux kernel utsrelease], lb_cv_utsrelease, [
62 lb_cv_utsrelease=""
63 utsrelease1=$LINUX_OBJ/include/generated/utsrelease.h
64 utsrelease2=$LINUX_OBJ/include/linux/utsrelease.h
65 utsrelease3=$LINUX_OBJ/include/linux/version.h
66 AS_IF([test -r $utsrelease1 && fgrep -q UTS_RELEASE $utsrelease1],
67         [utsrelease=$utsrelease1],
68 [test -r $utsrelease2 && fgrep -q UTS_RELEASE $utsrelease2],
69         [utsrelease=$utsrelease2],
70 [test -r $utsrelease3 && fgrep -q UTS_RELEASE $utsrelease3],
71         [utsrelease=$utsrelease3])
72 AS_IF([test -n "$utsrelease"],
73         [lb_cv_utsrelease=$(awk -F \" '/ UTS_RELEASE / { print [$]2 }' $utsrelease)],
74         [AC_MSG_ERROR([
75
76 Cannot find UTS_RELEASE definition.
77
78 This is often provided by the kernel-devel package.
79 ])
80         ])
81 ])
82 AS_IF([test -z "$lb_cv_utsrelease"],
83         [AC_MSG_ERROR([Cannot determine Linux kernel version.])])
84 LINUXRELEASE=$lb_cv_utsrelease
85 AC_SUBST(LINUXRELEASE)
86 ])
87
88 #
89 # LB_LINUX_RELEASE
90 #
91 # get the release version of linux
92 #
93 AC_DEFUN([LB_LINUX_RELEASE], [
94         LB_LINUX_UTSRELEASE
95
96         # Define default states
97         RHEL_KERNEL="no"
98         SUSE_KERNEL="no"
99         UBUNTU_KERNEL="no"
100         DEBIAN_KERNEL="no"
101         # And if any of the above kernels has been detected yet
102         KERNEL_FOUND="no"
103
104         # Check for RedHat first (no need to check KERNEL_FOUND
105         AC_CACHE_CHECK([for RedHat kernel release number], lb_cv_rhel_kernel_version, [
106                 lb_cv_rhel_kernel_version=""
107                 AS_IF([fgrep -q RHEL_RELEASE $LINUX_OBJ/include/$VERSION_HDIR/version.h], [
108                         lb_cv_rhel_kernel_version=$(awk '/ RHEL_MAJOR / { print [$]3 }' \
109                                 $LINUX_OBJ/include/$VERSION_HDIR/version.h)$(awk \
110                                 '/ RHEL_MINOR / { print [$]3 }' \
111                                 $LINUX_OBJ/include/$VERSION_HDIR/version.h)
112                 ])
113         ])
114         AS_IF([test -n "$lb_cv_rhel_kernel_version"], [
115                 RHEL_KERNEL="yes"
116                 KERNEL_FOUND="yes"
117                 RHEL_RELEASE_NO=$lb_cv_rhel_kernel_version
118         ])
119
120         # Check for SuSE
121         AS_IF([test "x$KERNEL_FOUND" = "xno"], [
122                 LB_CHECK_CONFIG([SUSE_KERNEL], [
123                         SUSE_KERNEL="yes"
124                         KERNEL_FOUND="yes"
125                 ], [])
126         ])
127
128         # Check for Ubuntu
129         AS_IF([test "x$KERNEL_FOUND" = "xno"], [
130                 AC_CACHE_CHECK([for Ubuntu kernel signature], lb_cv_ubuntu_kernel_sig, [
131                         lb_cv_ubuntu_kernel_sig="no"
132                         AS_IF([fgrep -q "CONFIG_VERSION_SIGNATURE \"Ubuntu" $LINUX_OBJ/include/generated/autoconf.h], [
133                                 lb_cv_ubuntu_kernel_sig="yes"
134                         ])
135                 ])
136                 AS_IF([test "x$lb_cv_ubuntu_kernel_sig" = "xyes"], [
137                         UBUNTU_KERNEL="yes"
138                         KERNEL_FOUND="yes"
139                 ])
140         ])
141
142         # Check for Debian
143         AS_IF([test "x$KERNEL_FOUND" = "xno"], [
144                 AC_CACHE_CHECK([for Debian kernel signature], lb_cv_debian_kernel_sig, [
145                         lb_cv_debian_kernel_sig="no"
146                         AS_IF([grep -q "LINUX_PACKAGE_ID\s*\"\s*Debian" $LINUX_OBJ/include/generated/package.h], [
147                                 lb_cv_debian_kernel_sig="yes"
148                         ])
149                 ])
150                 AS_IF([test "x$lb_cv_debian_kernel_sig" = "xyes"], [
151                         DEBIAN_KERNEL="yes"
152                         KERNEL_FOUND="yes"
153                 ])
154         ])
155
156         # Check for a ELRepo -ml kernel on RHEL 7/8
157         AS_IF([test "x$KERNEL_FOUND" = "xno"], [
158                 AC_CACHE_CHECK([for ELRepo -ml kernel signature on CentOS],
159                                 lb_cv_mainline_kernel_sig, [
160                         lb_cv_mainline_kernel_sig="no"
161                         AS_IF([fgrep -q '.el7.' $LINUX_OBJ/include/generated/utsrelease.h], [
162                                 lb_cv_mainline_kernel_sig="yes"
163                         ])
164                         AS_IF([fgrep -q '.el8.' $LINUX_OBJ/include/generated/utsrelease.h], [
165                                 lb_cv_mainline_kernel_sig="yes"
166                         ])
167                 ])
168                 AS_IF([test "x$lb_cv_mainline_kernel_sig" = "xyes"], [
169                         RHEL_KERNEL="yes"
170                         KERNEL_FOUND="yes"
171                 ])
172         ])
173
174         # If still no kernel was found, a warning is issued
175         AS_IF([test "x$KERNEL_FOUND" = "xno"], [
176                 AC_MSG_WARN([Kernel Distro seems to be neither RedHat, SuSE, Ubuntu nor Debian])
177         ])
178
179         AC_MSG_CHECKING([for Linux kernel module package directory])
180         AC_ARG_WITH([kmp-moddir],
181                 AC_HELP_STRING([--with-kmp-moddir=string],
182                         [set the kmod updates or extra directory]),
183                 [KMP_MODDIR=$withval
184                  IN_KERNEL=''],[
185                 AS_IF([test x$RHEL_KERNEL = xyes], [KMP_MODDIR="extra/kernel"],
186                       [test x$SUSE_KERNEL = xyes], [KMP_MODDIR="updates/kernel"],
187                       [test x$UBUNTU_KERNEL = xyes], [KMP_MODDIR="updates/kernel"],
188                       [test x$DEBIAN_KERNEL = xyes], [KMP_MODDIR="updates/kernel"],
189                       [AC_MSG_WARN([Kernel Distro seems to be neither RedHat, SuSE, Ubuntu nor Debian])]
190                 )
191                 IN_KERNEL="${PACKAGE}"])
192         AC_MSG_RESULT($KMP_MODDIR)
193
194         moduledir="/lib/modules/${LINUXRELEASE}/${KMP_MODDIR}"
195
196         modulefsdir="${moduledir}/fs/${IN_KERNEL}"
197         AC_SUBST(modulefsdir)
198
199         modulenetdir="${moduledir}/net/${IN_KERNEL}"
200         AC_SUBST(modulenetdir)
201
202         AC_SUBST(KMP_MODDIR)
203 ])
204
205 #
206 # LB_LINUX_SYMVERFILE
207 #
208 # SLES 9 uses a different name for this file - unsure about vanilla kernels
209 # around this version, but it matters for servers only.
210 AC_DEFUN([LB_LINUX_SYMVERFILE], [
211 AC_CACHE_CHECK([for the name of module symbol version file], lb_cv_module_symvers, [
212 AS_IF([grep -q Modules.symvers $LINUX/scripts/Makefile.modpost],
213         [lb_cv_module_symvers=Modules.symvers],
214         [lb_cv_module_symvers=Module.symvers])
215 ])
216 SYMVERFILE=$lb_cv_module_symvers
217 AC_SUBST(SYMVERFILE)
218 ])
219
220 #
221 # LB_ARG_REPLACE_PATH(PACKAGE, PATH)
222 #
223 AC_DEFUN([LB_ARG_REPLACE_PATH], [
224 new_configure_args=
225 eval set -- $ac_configure_args
226 for arg; do
227         case $arg in
228                 --with-[$1]=*)
229                         arg=--with-[$1]=[$2] ;;
230                 *\'*)
231                         arg=$(printf %s\n ["$arg"] | sed "s/'/'\\\\\\\\''/g") ;;
232         esac
233         dnl AS_VAR_APPEND([new_configure_args], [" '$arg'"])
234         new_configure_args="$new_configure_args '$arg'"
235 done
236 ac_configure_args=$new_configure_args
237 ])
238
239 #
240 # __LB_ARG_CANON_PATH
241 #
242 # this is the work-horse of the next function
243 #
244 AC_DEFUN([__LB_ARG_CANON_PATH], [
245         [$3]=$(readlink -f $with_$2)
246         LB_ARG_REPLACE_PATH([$1], $[$3])
247 ])
248
249 #
250 # LB_ARG_CANON_PATH
251 #
252 # a front-end for the above function that transforms - and . in the
253 # PACKAGE portion of --with-PACKAGE into _ suitable for variable names
254 #
255 AC_DEFUN([LB_ARG_CANON_PATH], [
256         __LB_ARG_CANON_PATH([$1], m4_translit([$1], [-.], [__]), [$2])
257 ])
258
259 #
260 # LB_LINUX_PATH
261 #
262 # Find paths for linux, handling kernel-source rpms
263 #
264 AC_DEFUN([LB_LINUX_PATH], [
265 for DEFAULT_LINUX in /usr/src/linux-source-* /lib/modules/$(uname -r)/{source,build} /usr/src/linux* $(find /usr/src/kernels/ -maxdepth 1 -name @<:@0-9@:>@\* | xargs -r ls -d | tail -n 1); do
266         AS_IF([readlink -q -e $DEFAULT_LINUX >/dev/null], [break])
267 done
268 if test "$DEFAULT_LINUX" = "/lib/modules/$(uname -r)/source"; then
269         PATHS="/lib/modules/$(uname -r)/build"
270 else
271         PATHS="/usr/src/linux-headers-$(uname -r)"
272 fi
273 PATHS+=" $DEFAULT_LINUX"
274 for DEFAULT_LINUX_OBJ in $PATHS; do
275         AS_IF([readlink -q -e $DEFAULT_LINUX_OBJ >/dev/null], [break])
276 done
277
278 AC_MSG_CHECKING([for Linux sources])
279 AC_ARG_WITH([linux],
280         AC_HELP_STRING([--with-linux=path],
281                        [set path to Linux source (default=/lib/modules/$(uname -r)/{source,build},/usr/src/linux)]),
282         [LB_ARG_CANON_PATH([linux], [LINUX])
283         DEFAULT_LINUX_OBJ=$LINUX],
284         [LINUX=$DEFAULT_LINUX])
285 AC_MSG_RESULT([$LINUX])
286 AC_SUBST(LINUX)
287
288 # -------- check for linux --------
289 LB_CHECK_FILE([$LINUX], [],
290         [AC_MSG_ERROR([Kernel source $LINUX could not be found.])])
291
292 # -------- linux objects (for 2.6) --
293 AC_MSG_CHECKING([for Linux objects])
294 AC_ARG_WITH([linux-obj],
295         AC_HELP_STRING([--with-linux-obj=path],
296                         [set path to Linux objects (default=/lib/modules/$(uname -r)/build,/usr/src/linux)]),
297         [LB_ARG_CANON_PATH([linux-obj], [LINUX_OBJ])],
298         [LINUX_OBJ=$DEFAULT_LINUX_OBJ])
299 AC_MSG_RESULT([$LINUX_OBJ])
300 AC_SUBST(LINUX_OBJ)
301
302 # -------- check for .config --------
303 AC_ARG_WITH([linux-config],
304         [AC_HELP_STRING([--with-linux-config=path],
305                         [set path to Linux .conf (default=$LINUX_OBJ/.config)])],
306         [LB_ARG_CANON_PATH([linux-config], [LINUX_CONFIG])],
307         [LINUX_CONFIG=$LINUX_OBJ/.config])
308
309 # -------- check if .config exists --
310 LB_CHECK_FILE([$LINUX_CONFIG], [],
311         [AC_MSG_ERROR([
312
313 Kernel config could not be found.
314 ])
315 ])
316 AC_SUBST(LINUX_CONFIG)
317
318 LB_CHECK_FILE([/boot/kernel.h],
319         [KERNEL_SOURCE_HEADER='/boot/kernel.h'],
320         [LB_CHECK_FILE([/var/adm/running-kernel.h],
321                 [KERNEL_SOURCE_HEADER='/var/adm/running-kernel.h'])])
322
323 AC_ARG_WITH([kernel-source-header],
324         AC_HELP_STRING([--with-kernel-source-header=path],
325                         [Use a different kernel version header.]),
326         [LB_ARG_CANON_PATH([kernel-source-header], [KERNEL_SOURCE_HEADER])])
327
328 # ----------- make dep run? ------------------
329 # at 2.6.19 # $LINUX/include/linux/config.h is removed
330 # and at more old has only one line include <autoconf.h>
331 #
332 LB_CHECK_FILE([$LINUX_OBJ/include/generated/autoconf.h],
333         [AUTOCONF_HDIR=generated],
334         [LB_CHECK_FILE([$LINUX_OBJ/include/linux/autoconf.h],
335                 [AUTOCONF_HDIR=linux],
336                 [AC_MSG_ERROR([Run make config in $LINUX.])])])
337 AC_SUBST(AUTOCONF_HDIR)
338
339 LB_CHECK_FILE([$LINUX_OBJ/include/linux/version.h],
340         [VERSION_HDIR=linux],
341         [LB_CHECK_FILE([$LINUX_OBJ/include/generated/uapi/linux/version.h],
342                 [VERSION_HDIR=generated/uapi/linux],
343                 [AC_MSG_ERROR([Run make config in $LINUX.])])])
344 AC_SUBST(VERSION_HDIR)
345
346 # ----------- kconfig.h exists ---------------
347 # kernel 3.1, $LINUX/include/linux/kconfig.h is added
348 # see kernel commit 2a11c8ea20bf850b3a2c60db8c2e7497d28aba99
349 #
350 LB_CHECK_FILE([$LINUX/include/linux/kconfig.h],
351               [CONFIG_INCLUDE=$LINUX/include/linux/kconfig.h],
352               [CONFIG_INCLUDE=include/$AUTOCONF_HDIR/autoconf.h])
353 AC_SUBST(CONFIG_INCLUDE)
354
355 # ------------ rhconfig.h includes runtime-generated bits --
356 # RedHat kernel-source checks
357
358 # we know this exists after the check above.  if the user
359 # tarred up the tree and ran make dep etc. in it, then
360 # version.h gets overwritten with a standard linux one.
361
362 AS_IF([grep rhconfig $LINUX_OBJ/include/$VERSION_HDIR/version.h >/dev/null], [
363         # This is a clean kernel-source tree, we need to
364         # enable extensive workarounds to get this to build modules
365         LB_CHECK_FILE([$KERNEL_SOURCE_HEADER], [
366                 AS_IF([test $KERNEL_SOURCE_HEADER = '/boot/kernel.h'],
367                         [AC_MSG_WARN([
368
369 Using /boot/kernel.h from RUNNING kernel.
370
371 If this is not what you want, use --with-kernel-source-header.
372 Consult build/README.kernel-source for details.
373 ])
374                 ])],
375                 [AC_MSG_ERROR([
376
377 $KERNEL_SOURCE_HEADER not found.
378
379 Consult build/README.kernel-source for details.
380 ])
381                 ])
382         EXTRA_KCFLAGS="-include $KERNEL_SOURCE_HEADER $EXTRA_KCFLAGS"
383 ])
384
385 # this is needed before we can build modules
386 LB_LINUX_VERSION
387
388 # --- check that we can build modules at all
389 LB_CHECK_COMPILE([that modules can be built at all], build_modules,
390         [], [], [], [
391         AC_MSG_ERROR([
392
393 Kernel modules cannot be built. Consult config.log for details.
394
395 If you are trying to build with a kernel-source rpm,
396 consult build/README.kernel-source
397 ])
398 ])
399
400 LB_LINUX_RELEASE
401 ]) # end of LB_LINUX_PATH
402
403 #
404 # LC_MODULE_LOADING
405 #
406 # after 2.6.28 CONFIG_KMOD is removed, and only CONFIG_MODULES remains
407 # so we test if request_module is implemented or not
408 AC_DEFUN([LC_MODULE_LOADING], [
409 AC_CACHE_CHECK([if Linux kernel module loading is possible], lb_cv_module_loading, [
410 LB_LINUX_TRY_MAKE([
411         #include <linux/kmod.h>
412 ], [
413         int myretval=ENOSYS ;
414         return myretval;
415 ], [
416         $makerule LUSTRE_KERNEL_TEST=conftest.i
417 ], [dnl
418         grep request_module build/conftest.i |dnl
419                 grep -v `grep "int myretval=" build/conftest.i |dnl
420                         cut -d= -f2 | cut -d" "  -f1`dnl
421                 >/dev/null dnl
422 ], [lb_cv_module_loading="yes"], [lb_cv_module_loading="no"])
423 ])
424 AS_IF([test "$lb_cv_module_loading" = yes],
425         [AC_DEFINE(HAVE_MODULE_LOADING_SUPPORT, 1,
426                 [kernel module loading is possible])],
427         [AC_MSG_WARN([
428
429 Kernel module loading support is highly recommended.
430
431 ])
432         ])
433 ])
434
435 #
436 # LB_PROG_LINUX
437 #
438 # linux tests
439 #
440 AC_DEFUN([LB_PROG_LINUX], [
441 LB_LINUX_PATH
442 LB_LINUX_SYMVERFILE
443
444 LB_CHECK_CONFIG([MODULES], [], [
445         AC_MSG_ERROR([
446
447 module support is required to build Lustre kernel modules.
448 ])
449         ])
450
451 LB_CHECK_CONFIG([MODVERSIONS])
452
453 LB_CHECK_CONFIG([KALLSYMS], [], [
454         AC_MSG_ERROR([
455
456 Lustre requires that CONFIG_KALLSYMS is enabled in your kernel.
457 ])
458         ])
459
460 # 2.6.28
461 LC_MODULE_LOADING
462 ])
463
464 #
465 # LB_USES_DPKG
466 #
467 # Determine if the target is a dpkg system or rpm
468 #
469 AC_DEFUN([LB_USES_DPKG], [
470 AC_CACHE_CHECK([if this distro uses dpkg], lb_cv_uses_dpkg, [
471 lb_cv_uses_dpkg="no"
472 AS_CASE([$(which dpkg 2>/dev/null)],[*/dpkg], [lb_cv_uses_dpkg="yes"])
473 ])
474 uses_dpkg=$lb_cv_uses_dpkg
475 ])
476
477 #
478 # LB_CHECK_EXPORT
479 #
480 # check symbol exported or not
481 # $1 - symbol
482 # $2 - file(s) for find.
483 # $3 - do 'yes'
484 # $4 - do 'no'
485 #
486 # 2.6 based kernels - put modversion info into $LINUX/Module.modvers or check
487 #
488 AC_DEFUN([LB_CHECK_EXPORT], [
489 AS_VAR_PUSHDEF([lb_export], [lb_cv_export_$1])dnl
490 AC_CACHE_CHECK([if Linux kernel exports '$1'], lb_export, [
491 AS_VAR_SET([lb_export], [no])
492 AS_IF([grep -q -E '[[[:space:]]]$1[[[:space:]]]' $LINUX_OBJ/$SYMVERFILE 2>/dev/null],
493         [AS_VAR_SET([lb_export], [yes])],
494         [for file in $2; do
495                 AS_IF([grep -q -E "EXPORT_SYMBOL.*\($1\)" "$LINUX/$file" 2>/dev/null], [
496                         AS_VAR_SET([lb_export], [yes])
497                         break
498                 ])
499         done])
500 ])
501 AS_VAR_IF([lb_export], [yes], [$3], [$4])[]dnl
502 AS_VAR_POPDEF([lb_export])dnl
503 ]) # LB_CHECK_EXPORT
504
505 #
506 # LB_CHECK_CONFIG
507 #
508 # check if a given config option is defined
509 # $1 - CONFIG_<name>
510 # $2 - do 'yes'
511 # $3 - do 'no'
512 #
513 AC_DEFUN([LB_CHECK_CONFIG], [
514 LB_CHECK_COMPILE([if Linux kernel was built with CONFIG_$1],
515 config_$1, [
516         #include <$AUTOCONF_HDIR/autoconf.h>
517 ], [
518         #ifndef CONFIG_$1
519         #error CONFIG_$1 not #defined
520         #endif
521 ], [$2], [$3])
522 ]) # LB_CHECK_CONFIG
523
524 #
525 # LB_CHECK_CONFIG_IM
526 #
527 # check if a given config option is builtin or as module
528 # $1 - CONFIG_<name> or CONFIG_<name>_MODULE
529 # $2 - do 'yes'
530 # $3 - do 'no'
531 #
532 AC_DEFUN([LB_CHECK_CONFIG_IM], [
533 LB_CHECK_COMPILE([if Linux kernel was built with CONFIG_$1 in or as module],
534 config_im_$1, [
535         #include <$AUTOCONF_HDIR/autoconf.h>
536 ], [
537         #if !(defined(CONFIG_$1) || defined(CONFIG_$1_MODULE))
538         #error CONFIG_$1 and CONFIG_$1_MODULE not #defined
539         #endif
540 ], [$2], [$3])
541 ]) # LB_CHECK_CONFIG_IM
542
543 #
544 # these are like AC_TRY_COMPILE, but try to build modules against the
545 # kernel, inside the build directory
546 #
547
548 #
549 # LB_LANG_PROGRAM(C)([PROLOGUE], [BODY])
550 # --------------------------------------
551 #
552 m4_define([LB_LANG_PROGRAM],
553 [
554 #include <linux/kernel.h>
555 #include <linux/module.h>
556
557 #if defined(NEED_LOCKDEP_IS_HELD_DISCARD_CONST) \
558  && defined(CONFIG_LOCKDEP) \
559  && defined(lockdep_is_held)
560 #undef lockdep_is_held
561         #define lockdep_is_held(lock) \
562                 lock_is_held((struct lockdep_map *)&(lock)->dep_map)
563 #endif
564
565 $1
566 int
567 main (void)
568 {
569 dnl Do *not* indent the following line: there may be CPP directives.
570 dnl Don't move the `;' right after for the same reason.
571 $2
572   ;
573   return 0;
574 };
575 MODULE_LICENSE("GPL");])
576
577 #
578 # LB_LINUX_COMPILE_IFELSE
579 #
580 # like AC_COMPILE_IFELSE
581 #
582 # $1 - AC_LANG_SOURCE()
583 # $2 - make target
584 # $3 - check command
585 # $4 - do 'yes'
586 # $5 - do 'no'
587 #
588 AC_DEFUN([LB_LINUX_COMPILE_IFELSE],
589 [m4_ifvaln([$1], [AC_LANG_CONFTEST([AC_LANG_SOURCE([$1])])])dnl
590 rm -f build/conftest.o build/conftest.mod.c build/conftest.ko
591 SUBARCH=$(echo $target_cpu | sed -e 's/powerpc.*/powerpc/' -e 's/ppc.*/powerpc/' -e 's/x86_64/x86/' -e 's/i.86/x86/' -e 's/k1om/x86/' -e 's/aarch64.*/arm64/' -e 's/armv7.*/arm/')
592 AS_IF([AC_TRY_COMMAND(cp conftest.c build && make -d [$2] LDFLAGS= ${LD:+LD="$LD"} CC="$CC" -f $PWD/build/Makefile LUSTRE_LINUX_CONFIG=$LINUX_CONFIG LINUXINCLUDE="$EXTRA_CHECK_INCLUDE -I$LINUX/arch/$SUBARCH/include -Iinclude -Iarch/$SUBARCH/include/generated -I$LINUX/include -Iinclude2 -I$LINUX/include/uapi -Iinclude/generated -I$LINUX/arch/$SUBARCH/include/uapi -Iarch/$SUBARCH/include/generated/uapi -I$LINUX/include/uapi -Iinclude/generated/uapi ${SPL_OBJ:+-include $SPL_OBJ/spl_config.h} ${ZFS_OBJ:+-include $ZFS_OBJ/zfs_config.h} ${SPL:+-I$SPL/include } ${ZFS:+-I$ZFS -I$ZFS/include -I${SPL:-$ZFS/include/spl}} -include $CONFIG_INCLUDE" KBUILD_EXTRA_SYMBOLS="${ZFS_OBJ:+$ZFS_OBJ/Module.symvers} $KBUILD_EXTRA_SYMBOLS" -o tmp_include_depends -o scripts -o include/config/MARKER -C $LINUX_OBJ EXTRA_CFLAGS="-Werror-implicit-function-declaration $EXTRA_KCFLAGS" $MODULE_TARGET=$PWD/build) >/dev/null && AC_TRY_COMMAND([$3])],
593         [$4],
594         [_AC_MSG_LOG_CONFTEST
595 m4_ifvaln([$5],[$5])dnl])
596 rm -f build/conftest.o build/conftest.mod.c build/conftest.mod.o build/conftest.ko m4_ifval([$1], [build/conftest.c conftest.c])[]dnl
597 ])
598
599 #
600 # LB_LINUX_TRY_COMPILE
601 #
602 # like AC_TRY_COMPILE
603 #
604 AC_DEFUN([LB_LINUX_TRY_COMPILE], [
605 LB_LINUX_COMPILE_IFELSE(
606         [AC_LANG_SOURCE([LB_LANG_PROGRAM([[$1]], [[$2]])])],
607         [modules], [test -s build/conftest.o],
608         [$3], [$4])
609 ])
610
611 #
612 # LB_LINUX_TRY_MAKE
613 #
614 # like LB_LINUX_TRY_COMPILE, but with different arguments
615 #
616 AC_DEFUN([LB_LINUX_TRY_MAKE], [
617 LB_LINUX_COMPILE_IFELSE(
618         [AC_LANG_SOURCE([LB_LANG_PROGRAM([[$1]], [[$2]])])],
619         [$3], [$4], [$5], [$6])
620 ])
621
622 #
623 # LB_CHECK_COMPILE
624 # $1 - checking message
625 # $2 - variable name
626 # $3 - header
627 # $4 - body
628 # $5 - do 'yes'
629 # $6 - do 'no'
630 #
631 AC_DEFUN([LB_CHECK_COMPILE], [
632 AS_VAR_PUSHDEF([lb_compile], [lb_cv_compile_$2])dnl
633 AC_CACHE_CHECK([$1], lb_compile, [
634         LB_LINUX_TRY_COMPILE([$3], [$4],
635                 [AS_VAR_SET([lb_compile], [yes])],
636                 [AS_VAR_SET([lb_compile], [no])])
637 ])
638 AS_VAR_IF([lb_compile], [yes], [$5], [$6])[]dnl
639 AS_VAR_POPDEF([lb_compile])dnl
640 ]) # LB_CHECK_COMPILE
641
642 #
643 # LB_CHECK_LINUX_HEADER
644 #
645 # Like AC_CHECK_HEADER but checks for a kernel-space header
646 #
647 m4_define([LB_CHECK_LINUX_HEADER], [
648 AS_VAR_PUSHDEF([lb_header], [lb_cv_header_$1])dnl
649 AC_CACHE_CHECK([for $1], lb_header, [
650         LB_LINUX_COMPILE_IFELSE([LB_LANG_PROGRAM([@%:@include <$1>])],
651                 [modules], [test -s build/conftest.o],
652                 [AS_VAR_SET([lb_header], [yes])],
653                 [AS_VAR_SET([lb_header], [no])])
654 ])
655 AS_VAR_IF([lb_header], [yes], [$2], [$3])[]dnl
656 AS_VAR_POPDEF([lb_header])dnl
657 ]) # LB_CHECK_LINUX_HEADER
658
659 # ------------------------------------------------------------------------------
660 # Support 2 stage: parallel compile then checked test results
661 # Heavily inspired by OpenZFS
662
663 AC_DEFUN([LB2_LINUX_CONFTEST_C], [
664 test -d ${TEST_DIR}/$2 || mkdir -p ${TEST_DIR}/$2
665 cat confdefs.h - <<_EOF >${TEST_DIR}/$2/$2.c
666 $1
667 _EOF
668 ])
669
670 #
671 # LB2_LINUX_CONFTEST_MAKEFILE
672 #
673 # $1 - *unique* test case name
674 # $2 - additional build flags (ccflags)
675 # $3 - external kernel includes for lnet o2ib|gni
676 #
677 AC_DEFUN([LB2_LINUX_CONFTEST_MAKEFILE], [
678         test -d ${TEST_DIR} || mkdir -p ${TEST_DIR}
679         test -d ${TEST_DIR}/$1 || mkdir -p ${TEST_DIR}/$1
680
681         file=${TEST_DIR}/$1/Makefile
682         EXT_INCLUDE="$3"
683
684         cat - <<_EOF >$file
685 # Example command line to manually build source
686 # make modules -C $LINUX_OBJ $ARCH_UM M=${TEST_DIR}/$1
687
688 ${LD:+LD="$LD"}
689 CC=$CC
690 ZINC=${ZFS}
691 SINC=${SPL}
692 ZOBJ=${ZFS_OBJ}
693 SOBJ=${SPL_OBJ}
694
695 LINUXINCLUDE  = $EXT_INCLUDE
696 LINUXINCLUDE += -I$LINUX/arch/$SUBARCH/include
697 LINUXINCLUDE += -Iinclude -Iarch/$SUBARCH/include/generated
698 LINUXINCLUDE += -I$LINUX/include
699 LINUXINCLUDE += -Iinclude2
700 LINUXINCLUDE += -I$LINUX/include/uapi
701 LINUXINCLUDE += -Iinclude/generated
702 LINUXINCLUDE += -I$LINUX/arch/$SUBARCH/include/uapi
703 LINUXINCLUDE += -Iarch/$SUBARCH/include/generated/uapi
704 LINUXINCLUDE += -I$LINUX/include/uapi -Iinclude/generated/uapi
705 ifneq (\$(SOBJ),)
706 LINUXINCLUDE += -include \$(SOBJ)/spl_config.h
707 endif
708 ifneq (\$(ZOBJ),)
709 LINUXINCLUDE += -include \$(ZOBJ)/zfs_config.h
710 endif
711 ifneq (\$(SINC),)
712 LINUXINCLUDE += -I\$(SINC)/include
713 endif
714 ifneq (\$(ZINC),)
715 LINUXINCLUDE += -I\$(ZINC) -I\$(ZINC)/include
716 ifneq (\$(SINC),)
717 LINUXINCLUDE += -I\$(SINC)
718 else
719 LINUXINCLUDE += -I\$(ZINC)/include/spl
720 endif
721 endif
722 LINUXINCLUDE += -include $CONFIG_INCLUDE
723 KBUILD_EXTRA_SYMBOLS=${ZFS_OBJ:+$ZFS_OBJ/Module.symvers}
724
725 ccflags-y := -Werror-implicit-function-declaration
726 _EOF
727
728         # Additional custom CFLAGS as requested.
729         m4_ifval($2, [echo "ccflags-y += $2" >>$file], [])
730
731         # Test case source
732         echo "obj-m := $1.o" >>$file
733         echo "obj-m += $1/" >>${TEST_DIR}/Makefile
734 ])
735
736
737 #
738 # LB2_LINUX_COMPILE
739 #
740 # $1 - build dir
741 # $2 - test command
742 # $3 - pass command
743 # $4 - fail command
744 #
745 # Used internally by LB2_LINUX_TEST_COMPILE
746 #
747 AC_DEFUN([LB2_LINUX_COMPILE], [
748         AC_TRY_COMMAND([
749             KBUILD_MODPOST_NOFINAL="yes"
750             make modules -k -j$TEST_JOBS -C $LINUX_OBJ $ARCH_UM
751             M=$1 >$1/build.log 2>&1])
752         AS_IF([AC_TRY_COMMAND([$2])], [$3], [$4])
753 ])
754
755 #
756 # LB2_LINUX_TEST_COMPILE
757 #
758 # Perform a full compile excluding the final modpost phase.
759 # $1 - flavor
760 # $2 - dirname
761 #
762 AC_DEFUN([LB2_LINUX_TEST_COMPILE], [
763         LB2_LINUX_COMPILE([$2], [test -f $2/build.log], [
764                 mv $2/Makefile $2/Makefile.compile.$1
765                 mv $2/build.log $2/build.log.$1
766         ],[
767                 AC_MSG_ERROR([
768         *** Unable to compile test source to determine kernel interfaces.])
769         ])
770 ])
771
772 #
773 # Perform the compilation of the test cases in two phases.
774 #
775 # Phase 1) attempt to build the object files for all of the tests
776 #          defined by the LB2_LINUX_TEST_SRC macro.
777 #
778 # Phase 2) disable all tests which failed the initial compilation.
779 #
780 # This allows us efficiently build the test cases in parallel while
781 # remaining resilient to build failures which are expected when
782 # detecting the available kernel interfaces.
783 #
784 # The maximum allowed parallelism can be controlled by setting the
785 # TEST_JOBS environment variable which defaults to $(nproc).
786 #
787 AC_DEFUN([LB2_LINUX_TEST_COMPILE_ALL], [
788         # Phase 1 - Compilation only, final linking is skipped.
789         LB2_LINUX_TEST_COMPILE([$1], [${TEST_DIR}])
790
791         for dir in $(awk '/^obj-m/ { print [$]3 }' \
792             ${TEST_DIR}/Makefile.compile.$1); do
793                 name=${dir%/}
794                 AS_IF([test -f ${TEST_DIR}/$name/$name.o], [
795                         touch ${TEST_DIR}/$name/$name.ko
796                 ])
797         done
798 ])
799
800 #
801 # LB2_LINUX_TEST_SRC
802 #
803 # $1 - *unique* name
804 # $2 - global
805 # $3 - source
806 # $4 - extra cflags
807 # $5 - external include paths
808 #
809 # NOTICE as all of the test cases are compiled in parallel tests may not
810 # depend on the results other tests.
811 # Each test needs resolve any external dependencies at the time the program
812 # source is generated.
813 #
814 AC_DEFUN([LB2_LINUX_TEST_SRC], [
815         LB2_LINUX_CONFTEST_C([LB_LANG_PROGRAM([[$2]], [[$3]])], [$1_pc])
816         LB2_LINUX_CONFTEST_MAKEFILE([$1_pc], [$4], [$5])
817 ])
818
819 #
820 # LB2_LINUX_TEST_RESULT
821 #
822 # $1 - *unique* name matching the LB2_LINUX_TEST_SRC macro
823 # $2 - run on success (valid .ko generated)
824 # $3 - run on failure (unable to compile)
825 #
826 AC_DEFUN([LB2_LINUX_TEST_RESULT], [
827         AS_IF([test -d ${TEST_DIR}/$1_pc], [
828                 AS_IF([test -f ${TEST_DIR}/$1_pc/$1_pc.ko], [$2], [$3])
829         ], [
830                 AC_MSG_ERROR([
831         *** No matching source for the "$1" test, check that
832         *** both the test source and result macros refer to the same name.
833                 ])
834         ])
835 ])
836