Whamcloud - gitweb
LU-13485 libcfs: Parallel configure tests for libcfs
[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         rm -f build/conftest.i
15         MODULE_TARGET="M"
16         makerule="$PWD/build"
17         LB_LINUX_TRY_MAKE([], [],
18                 [$makerule LUSTRE_KERNEL_TEST=conftest.i],
19                 [test -s build/conftest.i],
20                 [lb_cv_module_target="M54"], [
21         MODULE_TARGET="M"
22         makerule="_module_$PWD/build"
23         LB_LINUX_TRY_MAKE([], [],
24                 [$makerule LUSTRE_KERNEL_TEST=conftest.i],
25                 [test -s build/conftest.i],
26                 [lb_cv_module_target="M"], [
27         MODULE_TARGET="M"
28         makerule=""
29         LB_LINUX_TRY_MAKE([], [],
30                 [$makerule LUSTRE_KERNEL_TEST=conftest.i],
31                 [test -s build/conftest.i],
32                 [lb_cv_module_target="M58"], [
33                         AC_MSG_ERROR([kernel module make failed; check config.log for details])
34         ])])])
35 ])
36 AS_IF([test -z "$lb_cv_module_target"],
37         [AC_MSG_ERROR([unknown external module build target])],
38 [test "x$lb_cv_module_target" = "xM54"],
39         [makerule="$PWD/build"
40         lb_cv_module_target="M"],
41 [test "x$lb_cv_module_target" = "xM58"],
42         [makerule=""
43         lb_cv_module_target="M"],
44 [test "x$lb_cv_module_target" = "xM"],
45         [makerule="_module_$PWD/build"])
46 MODULE_TARGET=$lb_cv_module_target
47 AC_SUBST(MODULE_TARGET)
48 ])
49
50 #
51 # LB_LINUX_UTSRELEASE
52 #
53 # Determine the Linux kernel version string from the utsrelease
54 #
55 AC_DEFUN([LB_LINUX_UTSRELEASE], [
56 AC_CACHE_CHECK([for Linux kernel utsrelease], lb_cv_utsrelease, [
57 lb_cv_utsrelease=""
58 utsrelease1=$LINUX_OBJ/include/generated/utsrelease.h
59 utsrelease2=$LINUX_OBJ/include/linux/utsrelease.h
60 utsrelease3=$LINUX_OBJ/include/linux/version.h
61 AS_IF([test -r $utsrelease1 && fgrep -q UTS_RELEASE $utsrelease1],
62         [utsrelease=$utsrelease1],
63 [test -r $utsrelease2 && fgrep -q UTS_RELEASE $utsrelease2],
64         [utsrelease=$utsrelease2],
65 [test -r $utsrelease3 && fgrep -q UTS_RELEASE $utsrelease3],
66         [utsrelease=$utsrelease3])
67 AS_IF([test -n "$utsrelease"],
68         [lb_cv_utsrelease=$(awk -F \" '/ UTS_RELEASE / { print [$]2 }' $utsrelease)],
69         [AC_MSG_ERROR([
70
71 Cannot find UTS_RELEASE definition.
72
73 This is often provided by the kernel-devel package.
74 ])
75         ])
76 ])
77 AS_IF([test -z "$lb_cv_utsrelease"],
78         [AC_MSG_ERROR([Cannot determine Linux kernel version.])])
79 LINUXRELEASE=$lb_cv_utsrelease
80 AC_SUBST(LINUXRELEASE)
81 ])
82
83 #
84 # LB_LINUX_RELEASE
85 #
86 # get the release version of linux
87 #
88 AC_DEFUN([LB_LINUX_RELEASE], [
89         LB_LINUX_UTSRELEASE
90
91         # Define default states
92         RHEL_KERNEL="no"
93         SUSE_KERNEL="no"
94         UBUNTU_KERNEL="no"
95         OPENEULER_KERNEL="no"
96         # And if any of the above kernels has been detected yet
97         KERNEL_FOUND="no"
98
99         # Check for RedHat first (no need to check KERNEL_FOUND
100         AC_CACHE_CHECK([for RedHat kernel release number], lb_cv_rhel_kernel_version, [
101                 lb_cv_rhel_kernel_version=""
102                 AS_IF([fgrep -q RHEL_RELEASE $LINUX_OBJ/include/$VERSION_HDIR/version.h], [
103                         lb_cv_rhel_kernel_version=$(awk '/ RHEL_MAJOR / { print [$]3 }' \
104                                 $LINUX_OBJ/include/$VERSION_HDIR/version.h)$(awk \
105                                 '/ RHEL_MINOR / { print [$]3 }' \
106                                 $LINUX_OBJ/include/$VERSION_HDIR/version.h)
107                 ])
108         ])
109         AS_IF([test -n "$lb_cv_rhel_kernel_version"], [
110                 RHEL_KERNEL="yes"
111                 KERNEL_FOUND="yes"
112                 RHEL_RELEASE_NO=$lb_cv_rhel_kernel_version
113         ])
114
115         # Check for SuSE
116         AS_IF([test "x$KERNEL_FOUND" = "xno"], [
117                 LB_CHECK_CONFIG([SUSE_KERNEL], [
118                         SUSE_KERNEL="yes"
119                         KERNEL_FOUND="yes"
120                 ], [])
121         ])
122
123         # Check for Ubuntu
124         AS_IF([test "x$KERNEL_FOUND" = "xno"], [
125                 AC_CACHE_CHECK([for Ubuntu kernel signature], lb_cv_ubuntu_kernel_sig, [
126                         lb_cv_ubuntu_kernel_sig="no"
127                         AS_IF([fgrep -q "CONFIG_VERSION_SIGNATURE \"Ubuntu" $LINUX_OBJ/include/generated/autoconf.h], [
128                                 lb_cv_ubuntu_kernel_sig="yes"
129                         ])
130                 ])
131                 AS_IF([test "x$lb_cv_ubuntu_kernel_sig" = "xyes"], [
132                         UBUNTU_KERNEL="yes"
133                         KERNEL_FOUND="yes"
134                 ])
135         ])
136
137         # Check for a ELRepo -ml kernel on RHEL 7/8
138         AS_IF([test "x$KERNEL_FOUND" = "xno"], [
139                 AC_CACHE_CHECK([for ELRepo -ml kernel signature on CentOS],
140                                 lb_cv_mainline_kernel_sig, [
141                         lb_cv_mainline_kernel_sig="no"
142                         AS_IF([fgrep -q '.el7.' $LINUX_OBJ/include/generated/utsrelease.h], [
143                                 lb_cv_mainline_kernel_sig="yes"
144                         ])
145                         AS_IF([fgrep -q '.el8.' $LINUX_OBJ/include/generated/utsrelease.h], [
146                                 lb_cv_mainline_kernel_sig="yes"
147                         ])
148                 ])
149                 AS_IF([test "x$lb_cv_mainline_kernel_sig" = "xyes"], [
150                         RHEL_KERNEL="yes"
151                         KERNEL_FOUND="yes"
152                 ])
153         ])
154
155         # Check for openEuler
156         AS_IF([test "x$KERNEL_FOUND" = "xno"], [
157                 AC_CACHE_CHECK([for openEuler kernel signature], lb_cv_openeuler_kernel_sig, [
158                         lb_cv_openeuler_kernel_sig="no"
159                         AS_IF([fgrep -q 'openEuler' $LINUX_OBJ/include/linux/kabi.h], [
160                                 lb_cv_openeuler_kernel_sig="yes"
161                         ])
162                 ])
163                 AS_IF([test "x$lb_cv_openeuler_kernel_sig" = "xyes"], [
164                         OPENEULER_KERNEL="yes"
165                         KERNEL_FOUND="yes"
166                 ])
167         ])
168
169         # If still no kernel was found, a warning is issued
170         AS_IF([test "x$KERNEL_FOUND" = "xno"], [
171                 AC_MSG_WARN([Kernel Distro seems to be neither RedHat, SuSE, openEuler nor Ubuntu])
172         ])
173
174         AC_MSG_CHECKING([for Linux kernel module package directory])
175         AC_ARG_WITH([kmp-moddir],
176                 AS_HELP_STRING([--with-kmp-moddir=string],
177                         [set the kmod updates or extra directory]),
178                 [KMP_MODDIR=$withval
179                  IN_KERNEL=''],[
180                 AS_IF([test x$RHEL_KERNEL = xyes], [KMP_MODDIR="extra/kernel"],
181                       [test x$OPENEULER_KERNEL = xyes], [KMP_MODDIR="extra/kernel"],
182                       [test x$SUSE_KERNEL = xyes], [KMP_MODDIR="updates/kernel"],
183                       [test x$UBUNTU_KERNEL = xyes], [KMP_MODDIR="updates/kernel"],
184                       [AC_MSG_WARN([Kernel Distro seems to be neither RedHat, SuSE, openEuler nor Ubuntu])]
185                 )
186                 IN_KERNEL="${PACKAGE}"])
187         AC_MSG_RESULT($KMP_MODDIR)
188
189         moduledir="/lib/modules/${LINUXRELEASE}/${KMP_MODDIR}"
190
191         modulefsdir="${moduledir}/fs/${IN_KERNEL}"
192         AC_SUBST(modulefsdir)
193
194         modulenetdir="${moduledir}/net/${IN_KERNEL}"
195         AC_SUBST(modulenetdir)
196
197         AC_SUBST(KMP_MODDIR)
198 ])
199
200 #
201 # LB_LINUX_SYMVERFILE
202 #
203 # SLES 9 uses a different name for this file - unsure about vanilla kernels
204 # around this version, but it matters for servers only.
205 AC_DEFUN([LB_LINUX_SYMVERFILE], [
206 AC_CACHE_CHECK([for the name of module symbol version file], lb_cv_module_symvers, [
207 AS_IF([grep -q Modules.symvers $LINUX/scripts/Makefile.modpost],
208         [lb_cv_module_symvers=Modules.symvers],
209         [lb_cv_module_symvers=Module.symvers])
210 ])
211 SYMVERFILE=$lb_cv_module_symvers
212 AC_SUBST(SYMVERFILE)
213 ])
214
215 #
216 # LB_ARG_REPLACE_PATH(PACKAGE, PATH)
217 #
218 AC_DEFUN([LB_ARG_REPLACE_PATH], [
219 new_configure_args=
220 eval set -- $ac_configure_args
221 for arg; do
222         case $arg in
223                 --with-[$1]=*)
224                         arg=--with-[$1]=[$2] ;;
225                 *\'*)
226                         arg=$(printf %s\n ["$arg"] | sed "s/'/'\\\\\\\\''/g") ;;
227         esac
228         dnl AS_VAR_APPEND([new_configure_args], [" '$arg'"])
229         new_configure_args="$new_configure_args '$arg'"
230 done
231 ac_configure_args=$new_configure_args
232 ])
233
234 #
235 # __LB_ARG_CANON_PATH
236 #
237 # this is the work-horse of the next function
238 #
239 AC_DEFUN([__LB_ARG_CANON_PATH], [
240         [$3]=$(readlink -f $with_$2)
241         LB_ARG_REPLACE_PATH([$1], $[$3])
242 ])
243
244 #
245 # LB_ARG_CANON_PATH
246 #
247 # a front-end for the above function that transforms - and . in the
248 # PACKAGE portion of --with-PACKAGE into _ suitable for variable names
249 #
250 AC_DEFUN([LB_ARG_CANON_PATH], [
251         __LB_ARG_CANON_PATH([$1], m4_translit([$1], [-.], [__]), [$2])
252 ])
253
254 #
255 # LB_LINUX_PATH
256 #
257 # Find paths for linux, handling kernel-source rpms
258 #
259 AC_DEFUN([LB_LINUX_PATH], [
260 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
261         AS_IF([readlink -q -e $DEFAULT_LINUX >/dev/null], [break])
262 done
263 if test "$DEFAULT_LINUX" = "/lib/modules/$(uname -r)/source"; then
264         PATHS="/lib/modules/$(uname -r)/build"
265 else
266         PATHS="/usr/src/linux-headers-$(uname -r)"
267 fi
268 PATHS+=" $DEFAULT_LINUX"
269 for DEFAULT_LINUX_OBJ in $PATHS; do
270         AS_IF([readlink -q -e $DEFAULT_LINUX_OBJ >/dev/null], [break])
271 done
272
273 AC_MSG_CHECKING([for Linux sources])
274 AC_ARG_WITH([linux],
275         AS_HELP_STRING([--with-linux=path],
276                        [set path to Linux source (default=/lib/modules/$(uname -r)/{source,build},/usr/src/linux)]),
277         [LB_ARG_CANON_PATH([linux], [LINUX])
278         DEFAULT_LINUX_OBJ=$LINUX],
279         [LINUX=$DEFAULT_LINUX])
280 AC_MSG_RESULT([$LINUX])
281
282 # -------- check for linux --------
283 LB_CHECK_FILE([$LINUX], [],
284         [AC_MSG_ERROR([Kernel source $LINUX could not be found.])])
285
286 # -------- linux objects (for 2.6) --
287 AC_MSG_CHECKING([for Linux objects])
288 AC_ARG_WITH([linux-obj],
289         AS_HELP_STRING([--with-linux-obj=path],
290                         [set path to Linux objects (default=/lib/modules/$(uname -r)/build,/usr/src/linux)]),
291         [LB_ARG_CANON_PATH([linux-obj], [LINUX_OBJ])],
292         [LINUX_OBJ=$DEFAULT_LINUX_OBJ])
293 AC_MSG_RESULT([$LINUX_OBJ])
294
295 ## -------- with linux should point to <kernel>-common on Debian
296 AS_IF([test ${LINUX} == ${LINUX_OBJ} -a ${LINUX} == $(realpath ${LINUX})],[
297         this_arch=$(realpath ${LINUX} | sed 's/-/\n/g' | tail -1)
298         linux_headers_common=$(realpath ${LINUX}|sed "s/-${this_arch}\$/-common/g")
299         AS_IF([test "${this_arch}" != common],[
300                 _cah="${linux_headers_common}/include/linux/compiler_attributes.h"
301                 _cgh="${linux_headers_common}/include/linux/compiler-gcc.h"
302                 AS_IF([test -f "${_cah}" -o -f "${_cgh}"],[
303                         AC_MSG_WARN([Setting LINUX to ${linux_headers_common} was ${LINUX}])
304                         LINUX=${linux_headers_common}])
305                 ])
306         ])
307 AC_SUBST(LINUX)
308 AC_SUBST(LINUX_OBJ)
309 # -------- check for .config --------
310 AC_ARG_WITH([linux-config],
311         [AS_HELP_STRING([--with-linux-config=path],
312                         [set path to Linux .conf (default=$LINUX_OBJ/.config)])],
313         [LB_ARG_CANON_PATH([linux-config], [LINUX_CONFIG])],
314         [LINUX_CONFIG=$LINUX_OBJ/.config])
315
316 # -------- check if .config exists --
317 LB_CHECK_FILE([$LINUX_CONFIG], [],
318         [AC_MSG_ERROR([
319
320 Kernel config could not be found.
321 ])
322 ])
323 AC_SUBST(LINUX_CONFIG)
324
325 LB_CHECK_FILE([/boot/kernel.h],
326         [KERNEL_SOURCE_HEADER='/boot/kernel.h'],
327         [LB_CHECK_FILE([/var/adm/running-kernel.h],
328                 [KERNEL_SOURCE_HEADER='/var/adm/running-kernel.h'])])
329
330 AC_ARG_WITH([kernel-source-header],
331         AS_HELP_STRING([--with-kernel-source-header=path],
332                         [Use a different kernel version header.]),
333         [LB_ARG_CANON_PATH([kernel-source-header], [KERNEL_SOURCE_HEADER])])
334
335 # ----------- make dep run? ------------------
336 # at 2.6.19 # $LINUX/include/linux/config.h is removed
337 # and at more old has only one line include <autoconf.h>
338 #
339 LB_CHECK_FILE([$LINUX_OBJ/include/generated/autoconf.h],
340         [AUTOCONF_HDIR=generated],
341         [LB_CHECK_FILE([$LINUX_OBJ/include/linux/autoconf.h],
342                 [AUTOCONF_HDIR=linux],
343                 [AC_MSG_ERROR([Run make config in $LINUX.])])])
344 AC_SUBST(AUTOCONF_HDIR)
345
346 LB_CHECK_FILE([$LINUX_OBJ/include/linux/version.h],
347         [VERSION_HDIR=linux],
348         [LB_CHECK_FILE([$LINUX_OBJ/include/generated/uapi/linux/version.h],
349                 [VERSION_HDIR=generated/uapi/linux],
350                 [AC_MSG_ERROR([Run make config in $LINUX.])])])
351 AC_SUBST(VERSION_HDIR)
352
353 # ----------- kconfig.h exists ---------------
354 # kernel 3.1, $LINUX/include/linux/kconfig.h is added
355 # see kernel commit 2a11c8ea20bf850b3a2c60db8c2e7497d28aba99
356 #
357 LB_CHECK_FILE([$LINUX/include/linux/kconfig.h],
358               [CONFIG_INCLUDE=$LINUX/include/linux/kconfig.h],
359               [CONFIG_INCLUDE=include/$AUTOCONF_HDIR/autoconf.h])
360 AC_SUBST(CONFIG_INCLUDE)
361
362 # ------------ rhconfig.h includes runtime-generated bits --
363 # RedHat kernel-source checks
364
365 # we know this exists after the check above.  if the user
366 # tarred up the tree and ran make dep etc. in it, then
367 # version.h gets overwritten with a standard linux one.
368
369 AS_IF([grep rhconfig $LINUX_OBJ/include/$VERSION_HDIR/version.h >/dev/null], [
370         # This is a clean kernel-source tree, we need to
371         # enable extensive workarounds to get this to build modules
372         LB_CHECK_FILE([$KERNEL_SOURCE_HEADER], [
373                 AS_IF([test $KERNEL_SOURCE_HEADER = '/boot/kernel.h'],
374                         [AC_MSG_WARN([
375
376 Using /boot/kernel.h from RUNNING kernel.
377
378 If this is not what you want, use --with-kernel-source-header.
379 Consult build/README.kernel-source for details.
380 ])
381                 ])],
382                 [AC_MSG_ERROR([
383
384 $KERNEL_SOURCE_HEADER not found.
385
386 Consult build/README.kernel-source for details.
387 ])
388                 ])
389         EXTRA_KCFLAGS="-include $KERNEL_SOURCE_HEADER $EXTRA_KCFLAGS"
390 ])
391
392 # this is needed before we can build modules
393 LB_LINUX_VERSION
394
395 # --- check that we can build modules at all
396 LB_CHECK_COMPILE([that modules can be built at all], build_modules,
397         [], [], [], [
398         AC_MSG_ERROR([
399
400 Kernel modules cannot be built. Consult config.log for details.
401
402 If you are trying to build with a kernel-source rpm,
403 consult build/README.kernel-source
404 ])
405 ])
406
407 LB_LINUX_RELEASE
408 ]) # end of LB_LINUX_PATH
409
410 #
411 # LC_MODULE_LOADING
412 #
413 # after 2.6.28 CONFIG_KMOD is removed, and only CONFIG_MODULES remains
414 # so we test if request_module is implemented or not
415 AC_DEFUN([LC_MODULE_LOADING], [
416 AC_CACHE_CHECK([if Linux kernel module loading is possible], lb_cv_module_loading, [
417 LB_LINUX_TRY_MAKE([
418         #include <linux/kmod.h>
419 ], [
420         int myretval=ENOSYS ;
421         return myretval;
422 ], [
423         $makerule LUSTRE_KERNEL_TEST=conftest.i
424 ], [dnl
425         grep request_module build/conftest.i |dnl
426                 grep -v `grep "int myretval=" build/conftest.i |dnl
427                         cut -d= -f2 | cut -d" "  -f1`dnl
428                 >/dev/null dnl
429 ], [lb_cv_module_loading="yes"], [lb_cv_module_loading="no"])
430 ])
431 AS_IF([test "$lb_cv_module_loading" = yes],
432         [AC_DEFINE(HAVE_MODULE_LOADING_SUPPORT, 1,
433                 [kernel module loading is possible])],
434         [AC_MSG_WARN([
435
436 Kernel module loading support is highly recommended.
437
438 ])
439         ])
440 ])
441
442 AC_DEFUN([LB_PROG_LINUX_SRC], [
443         LB2_SRC_CHECK_CONFIG([MODULES])
444         LB2_SRC_CHECK_CONFIG([MODVERSIONS])
445 ])
446
447 AC_DEFUN([LB_PROG_LINUX_RESULTS], [
448         LB2_TEST_CHECK_CONFIG([MODULES], [], [AC_MSG_ERROR(
449                 [module support is required to build Lustre kernel modules.])
450         ])
451         LB2_TEST_CHECK_CONFIG([MODVERSIONS],[],[])
452 ])
453
454 #
455 # LB_PROG_LINUX
456 #
457 # linux tests
458 #
459 AC_DEFUN([LB_PROG_LINUX], [
460 LB_LINUX_PATH
461 LB_LINUX_SYMVERFILE
462 # 2.6.28
463 LC_MODULE_LOADING
464 ])
465
466 #
467 # LB_USES_DPKG
468 #
469 # Determine if the target is a dpkg system or rpm
470 #
471 AC_DEFUN([LB_USES_DPKG], [
472 AC_CACHE_CHECK([if this distro uses dpkg], lb_cv_uses_dpkg, [
473 lb_cv_uses_dpkg="no"
474 AS_CASE([$(which dpkg 2>/dev/null)],[*/dpkg], [lb_cv_uses_dpkg="yes"])
475 ])
476 uses_dpkg=$lb_cv_uses_dpkg
477 ])
478
479 #
480 # LB_CHECK_EXPORT
481 #
482 # check symbol exported or not
483 # $1 - symbol
484 # $2 - file(s) for find.
485 # $3 - do 'yes'
486 # $4 - do 'no'
487 #
488 # 2.6 based kernels - put modversion info into $LINUX/Module.modvers or check
489 #
490 AC_DEFUN([LB_CHECK_EXPORT], [
491 AS_VAR_PUSHDEF([lb_export], [lb_cv_export_$1])dnl
492 AC_CACHE_CHECK([if Linux kernel exports '$1'], lb_export, [
493 AS_VAR_SET([lb_export], [no])
494 AS_IF([grep -q -E '[[[:space:]]]$1[[[:space:]]]' $LINUX_OBJ/$SYMVERFILE 2>/dev/null],
495         [AS_VAR_SET([lb_export], [yes])],
496         [for file in $2; do
497                 AS_IF([grep -q -E "EXPORT_SYMBOL.*\($1\)" "$LINUX/$file" 2>/dev/null], [
498                         AS_VAR_SET([lb_export], [yes])
499                         break
500                 ])
501         done])
502 ])
503 AS_VAR_IF([lb_export], [yes], [$3], [$4])[]dnl
504 AS_VAR_POPDEF([lb_export])dnl
505 ]) # LB_CHECK_EXPORT
506
507 #
508 # LB_CHECK_CONFIG
509 #
510 # check if a given config option is defined
511 # $1 - CONFIG_<name>
512 # $2 - do 'yes'
513 # $3 - do 'no'
514 #
515 AC_DEFUN([LB_CHECK_CONFIG], [
516 LB_CHECK_COMPILE([if Linux kernel was built with CONFIG_$1],
517 config_$1, [
518         #include <$AUTOCONF_HDIR/autoconf.h>
519 ], [
520         #ifndef CONFIG_$1
521         #error CONFIG_$1 not #defined
522         #endif
523 ], [$2], [$3])
524 ]) # LB_CHECK_CONFIG
525
526 #
527 # LB_CHECK_CONFIG_IM
528 #
529 # check if a given config option is builtin or as module
530 # $1 - CONFIG_<name> or CONFIG_<name>_MODULE
531 # $2 - do 'yes'
532 # $3 - do 'no'
533 #
534 AC_DEFUN([LB_CHECK_CONFIG_IM], [
535 LB_CHECK_COMPILE([if Linux kernel was built with CONFIG_$1 in or as module],
536 config_im_$1, [
537         #include <$AUTOCONF_HDIR/autoconf.h>
538 ], [
539         #if !(defined(CONFIG_$1) || defined(CONFIG_$1_MODULE))
540         #error CONFIG_$1 and CONFIG_$1_MODULE not #defined
541         #endif
542 ], [$2], [$3])
543 ]) # LB_CHECK_CONFIG_IM
544
545 #
546 # these are like AC_TRY_COMPILE, but try to build modules against the
547 # kernel, inside the build directory
548 #
549
550 #
551 # LB_LANG_PROGRAM(C)([PROLOGUE], [BODY])
552 # --------------------------------------
553 #
554 m4_define([LB_LANG_PROGRAM],
555 [
556 #include <linux/kernel.h>
557 #include <linux/module.h>
558
559 #if defined(NEED_LOCKDEP_IS_HELD_DISCARD_CONST) \
560  && defined(CONFIG_LOCKDEP) \
561  && defined(lockdep_is_held)
562 #undef lockdep_is_held
563         #define lockdep_is_held(lock) \
564                 lock_is_held((struct lockdep_map *)&(lock)->dep_map)
565 #endif
566
567 $1
568 int
569 main (void)
570 {
571 dnl Do *not* indent the following line: there may be CPP directives.
572 dnl Don't move the `;' right after for the same reason.
573 $2
574   ;
575   return 0;
576 };
577 MODULE_LICENSE("GPL");])
578
579 #
580 # LB_LINUX_COMPILE_IFELSE
581 #
582 # like AC_COMPILE_IFELSE
583 #
584 # $1 - AC_LANG_SOURCE()
585 # $2 - make target
586 # $3 - check command
587 # $4 - do 'yes'
588 # $5 - do 'no'
589 #
590 AC_DEFUN([LB_LINUX_COMPILE_IFELSE],
591 [m4_ifvaln([$1], [AC_LANG_CONFTEST([AC_LANG_SOURCE([$1])])])dnl
592 rm -f build/conftest.o build/conftest.mod.c build/conftest.ko
593 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$ZFS/include/os/linux/kernel -I$ZFS/include/os/linux/spl -I$ZFS/include/os/linux/zfs -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])],
594         [$4],
595         [_AC_MSG_LOG_CONFTEST
596 m4_ifvaln([$5],[$5])dnl])
597 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
598 ])
599
600 #
601 # LB_LINUX_TRY_COMPILE
602 #
603 # like AC_TRY_COMPILE
604 #
605 AC_DEFUN([LB_LINUX_TRY_COMPILE], [
606 LB_LINUX_COMPILE_IFELSE(
607         [AC_LANG_SOURCE([LB_LANG_PROGRAM([[$1]], [[$2]])])],
608         [modules], [test -s build/conftest.o],
609         [$3], [$4])
610 ])
611
612 #
613 # LB_LINUX_TRY_MAKE
614 #
615 # like LB_LINUX_TRY_COMPILE, but with different arguments
616 #
617 AC_DEFUN([LB_LINUX_TRY_MAKE], [
618 LB_LINUX_COMPILE_IFELSE(
619         [AC_LANG_SOURCE([LB_LANG_PROGRAM([[$1]], [[$2]])])],
620         [$3], [$4], [$5], [$6])
621 ])
622
623 #
624 # LB_CHECK_COMPILE
625 # $1 - checking message
626 # $2 - variable name
627 # $3 - header
628 # $4 - body
629 # $5 - do 'yes'
630 # $6 - do 'no'
631 #
632 AC_DEFUN([LB_CHECK_COMPILE], [
633 AS_VAR_PUSHDEF([lb_compile], [lb_cv_compile_$2])dnl
634 AC_CACHE_CHECK([$1], lb_compile, [
635         LB_LINUX_TRY_COMPILE([$3], [$4],
636                 [AS_VAR_SET([lb_compile], [yes])],
637                 [AS_VAR_SET([lb_compile], [no])])
638 ])
639 AS_VAR_IF([lb_compile], [yes], [$5], [$6])[]dnl
640 AS_VAR_POPDEF([lb_compile])dnl
641 ]) # LB_CHECK_COMPILE
642
643 #
644 # LB_CHECK_LINUX_HEADER
645 #
646 # Like AC_CHECK_HEADER but checks for a kernel-space header
647 #
648 AC_DEFUN([LB_CHECK_LINUX_HEADER], [
649         AS_VAR_PUSHDEF([lb_header], [lb_cv_header_$1])
650         AC_CACHE_CHECK([for $1], lb_header, [
651                 LB_LINUX_COMPILE_IFELSE([LB_LANG_PROGRAM([@%:@include <$1>])],
652                         [modules], [test -s build/conftest.o],
653                         [AS_VAR_SET([lb_header], [yes])],
654                         [AS_VAR_SET([lb_header], [no])])
655         ])
656         AS_VAR_IF([lb_header], [yes], [$2], [$3])
657         AS_VAR_POPDEF([lb_header])
658 ]) # LB_CHECK_LINUX_HEADER
659
660 # ------------------------------------------------------------------------------
661 # Support 2 stage: parallel compile then checked test results
662 # Heavily inspired by OpenZFS
663
664 # When converting tests from an LB_CHECK_COMPILE to use an
665 # LB2_LINUX_TEST_SRC and LB2_LINUX_TEST_RESULT pairing the primary
666 # consideration if dependencies on the results of LB2_LINUX_TEST_RESULT
667 # or LB2_TEST_CHECK_CONFIG[_IM] are used by annother compile test.
668 # Such dependencies should be avoided without a clear understanding
669 # of when the test result is actually available.
670 # The serial LB_CHECK_* test produces immediate results
671 # where as the LB2_ variant is not available until an intermediate
672 # LB2_LINUX_TEST_COMPILE_ALL has been executed.
673 #
674 # Currently all of the non-conditional LB_CHECK_COMPILE and
675 # LB_CHECK_CONFIG[_IM] cases are executed before any of the
676 # LB2_LINUX_TEST_SRC, or LB2_SRC_CHECK_CONFIG[_IM] code snippets
677 # are written. [Reference LB_CONFIG_MODULES for more details]
678 #
679 # This allows such LB2_*_SRC_* test cases to rely on
680 # prior LB_CHECK_* results, provided such results are preserved
681 # in the global configure name space. Likewise an LB2_LINUX_TEST_RESULT
682 # or LB2_TEST_CHECK_CONFIG[_IM] may conditionally execute a further
683 # LB_CHECK_* if required. Although it would be typcially be faster
684 # to run a few compile tests and conditionally ignore the results
685 # the reduced configure time may not always out weight the added
686 # complexity.
687 #
688 # It is worth noting that the LB_CHECK_COMPILE macros second argument
689 # is used as the configure test cache name. While is has been important
690 # that this name be globally unique it is even more important in the
691 # parallel case. The is somewhat alleviated by the sanity check in the
692 # LB2_LINUX_TEST_RESULT macro as it will abort with an error if
693 # the cache name does not exist. However care should be taken
694 # that the cache name is a globally unique identifier.
695 #
696 # Non-unique cases that are otherwise correct can be identified in the
697 # configure output when a results include ".. (cached)". This indicates
698 # either a duplicate LB_CHECK_* execution which is not critical or a
699 # duplicate identifier which needs to be corrected.
700 #
701 # At this point the number of LB_ and LB2_ examples should be
702 # sufficient for the casual developer to perform cut-paste-modify
703 # without serious confusion or complication.
704 # ------------------------------------------------------------------------------
705
706 AC_DEFUN([LB2_LINUX_CONFTEST_C], [
707 TEST_DIR=${TEST_DIR:-${ac_pwd}/_lpb}
708 test -d ${TEST_DIR}/$2 || mkdir -p ${TEST_DIR}/$2
709 cat confdefs.h - <<_EOF >${TEST_DIR}/$2/$2.c
710 $1
711 _EOF
712 ])
713
714 #
715 # LB2_LINUX_CONFTEST_MAKEFILE
716 #
717 # $1 - *unique* test case name
718 # $2 - additional build flags (ccflags)
719 # $3 - external kernel includes for lnet o2ib|gni
720 # $4 - extra symbol in Psuedo.symvers (optional)
721 #
722 AC_DEFUN([LB2_LINUX_CONFTEST_MAKEFILE], [
723         TEST_DIR=${TEST_DIR:-${ac_pwd}/_lpb}
724         test -d ${TEST_DIR} || mkdir -p ${TEST_DIR}
725         test -d ${TEST_DIR}/$1 || mkdir -p ${TEST_DIR}/$1
726
727         file=${TEST_DIR}/$1/Makefile
728         EXT_INCLUDE="$3"
729         EXT_SYMBOL="$4"
730         PSYM_FILE=""
731         if test "${EXT_SYMBOL}x" != "x" ; then
732                 PSYM_FILE=${TEST_DIR}/$1/Psuedo.symvers
733                 echo -e "0x12345678\t${EXT_SYMBOL}\tvmlinux\tEXPORT_SYMBOL\t" > ${PSYM_FILE}
734         fi
735         XTRA_SYM=
736         if test "x$O2IBPATH" != "x"; then
737                 if test "x$O2IBPATH" != "x$LINUX_OBJ"; then
738                         XTRA_SYM="$O2IBPATH/Module.symvers"
739                 fi
740         fi
741
742         cat - <<_EOF >$file
743 # Example command line to manually build source
744 # make modules -C $LINUX_OBJ $ARCH_UM M=${TEST_DIR}/$1
745
746 ${LD:+LD="$LD"}
747 CC=$CC
748 ZINC=${ZFS}
749 SINC=${SPL}
750 ZOBJ=${ZFS_OBJ}
751 SOBJ=${SPL_OBJ}
752 PSYM=${PSYM_FILE}
753
754 LINUXINCLUDE  = $EXT_INCLUDE
755 LINUXINCLUDE += -I$LINUX/arch/$SUBARCH/include
756 LINUXINCLUDE += -Iinclude -Iarch/$SUBARCH/include/generated
757 LINUXINCLUDE += -I$LINUX/include
758 LINUXINCLUDE += -Iinclude2
759 LINUXINCLUDE += -I$LINUX/include/uapi
760 LINUXINCLUDE += -Iinclude/generated
761 LINUXINCLUDE += -I$LINUX/arch/$SUBARCH/include/uapi
762 LINUXINCLUDE += -Iarch/$SUBARCH/include/generated/uapi
763 LINUXINCLUDE += -I$LINUX/include/uapi -Iinclude/generated/uapi
764 ifneq (\$(SOBJ),)
765 LINUXINCLUDE += -include \$(SOBJ)/spl_config.h
766 endif
767 ifneq (\$(ZOBJ),)
768 LINUXINCLUDE += -include \$(ZOBJ)/zfs_config.h
769 endif
770 ifneq (\$(SINC),)
771 LINUXINCLUDE += -I\$(SINC)/include
772 endif
773 ifneq (\$(ZINC),)
774 LINUXINCLUDE += -I\$(ZINC) -I\$(ZINC)/include
775 ifneq (\$(SINC),)
776 LINUXINCLUDE += -I\$(SINC)
777 else
778 LINUXINCLUDE += -I\$(ZINC)/include/spl
779 endif
780 endif
781 LINUXINCLUDE += -include $CONFIG_INCLUDE
782 KBUILD_EXTRA_SYMBOLS += ${ZFS_OBJ:+$ZFS_OBJ/Module.symvers}
783 KBUILD_EXTRA_SYMBOLS += ${XTRA_SYM}
784 ifneq (\$(PSYM),)
785 KBUILD_EXTRA_SYMBOLS += \$(PSYM)
786 endif
787
788 ccflags-y := -Werror-implicit-function-declaration
789 _EOF
790
791         # Additional custom CFLAGS as requested.
792         m4_ifval($2, [echo "ccflags-y += $2" >>$file], [])
793
794         # Test case source
795         echo "obj-m := $1.o" >>$file
796         echo "obj-m += $1/" >>${TEST_DIR}/Makefile
797 ])
798
799 #
800 # LB2_LINUX_TEST_COMPILE
801 #
802 # Perform a full compile excluding the final modpost phase.
803 # $1 - flavor
804 # $2 - dirname
805 #
806 AC_DEFUN([LB2_LINUX_TEST_COMPILE], [
807         D="$(realpath [$2])"
808         L="$D/build.log.$1"
809         J=${TEST_JOBS:-$(nproc)}
810
811         AC_MSG_NOTICE([Making $1 in $D])
812         AC_MSG_NOTICE([KBUILD_MODPOST_NOFINAL="yes" make modules -k -j${J} -C $LINUX_OBJ $ARCH_UM M=${D}])
813
814         AC_TRY_COMMAND([KBUILD_MODPOST_NOFINAL="yes"
815                 make modules -k -j${J} -C $LINUX_OBJ $ARCH_UM M=${D} >${L} 2>&1])
816         AS_IF([test -f ${L}],
817               [AS_IF([test -f $2/Makefile],
818                      [mv $2/Makefile $2/Makefile.compile.$1])],
819               [AC_MSG_ERROR([*** Unable to compile test source ... $3])
820         ])
821 ])
822
823 #
824 # Perform the compilation of the test cases in two phases.
825 #
826 # Phase 1) attempt to build the object files for all of the tests
827 #          defined by the LB2_LINUX_TEST_SRC macro.
828 #
829 # Phase 2) disable all tests which failed the initial compilation.
830 #
831 # This allows us efficiently build the test cases in parallel while
832 # remaining resilient to build failures which are expected when
833 # detecting the available kernel interfaces.
834 #
835 # The maximum allowed parallelism can be controlled by setting the
836 # TEST_JOBS environment variable which defaults to $(nproc).
837 #
838 AC_DEFUN([LB2_LINUX_TEST_COMPILE_ALL], [
839         TEST_DIR=${TEST_DIR:-${ac_pwd}/_lpb}
840         # Phase 1 - Compilation only, final linking is skipped.
841         LB2_LINUX_TEST_COMPILE([$1], [${TEST_DIR}], [$2])
842         for dir in $(awk '/^obj-m/ { print [$]3 }' \
843             ${TEST_DIR}/Makefile.compile.$1); do
844                 name=${dir%/}
845                 touch ${TEST_DIR}/$name/$name.tested
846         done
847 ])
848
849 #
850 # LB2_LINUX_TEST_SRC
851 #
852 # $1 - *unique* name
853 # $2 - global
854 # $3 - source
855 # $4 - extra cflags
856 # $5 - external include paths
857 # $6 - fake symvers entry
858 #
859 # NOTICE as all of the test cases are compiled in parallel tests may not
860 # depend on the results other tests.
861 # Each test needs resolve any external dependencies at the time the program
862 # source is generated.
863 #
864 AC_DEFUN([LB2_LINUX_TEST_SRC], [
865         TEST_DIR=${TEST_DIR:-${ac_pwd}/_lpb}
866         AS_VAR_PUSHDEF([lb_test], [lb_cv_test_$1])
867         # Skip test write and build steps if the result is already known.
868         LB2_LINUX_CONFTEST_C([LB_LANG_PROGRAM([[$2]], [[$3]])], [$1_pc])
869         LB2_LINUX_CONFTEST_MAKEFILE([$1_pc], [$4], [$5], [$6])
870         AS_VAR_POPDEF([lb_test])
871 ])
872
873 #
874 # LB2_LINUX_TEST_RESULT
875 #
876 # $1 - *unique* name matching the LB2_LINUX_TEST_SRC macro
877 # $2 - run on success (valid .ko generated)
878 # $3 - run on failure (unable to compile)
879 #
880 AC_DEFUN([LB2_LINUX_TEST_RESULT],[
881         TEST_DIR=${TEST_DIR:-${ac_pwd}/_lpb}
882         AS_VAR_PUSHDEF([lb_test], [lb_cv_test_$1])
883         D="$(realpath ${TEST_DIR})"
884         T=${D}/$1_pc
885         O=${T}/$1_pc
886         AS_IF([test -d ${T}], [
887                 # test source exists, was the compile test run?
888                 AS_IF([test -f ${O}.tested],[
889                         # compile test was run.
890                 ],[
891                         # compile test was NOT run. Re-compile everything.
892                         AC_MSG_NOTICE([** Rebuilding all tests **])
893
894                         J=${TEST_JOBS:-$(nproc)}
895                         for mf in $(ls -1 ${TEST_DIR}/Makefile.compile.*); do
896                                 ln -sf $mf ${D}/Makefile
897                                 KBUILD_MODPOST_NOFINAL="yes"
898                                 make modules -k -j${J} -C $LINUX_OBJ $ARCH_UM M=${D} >> rebuild.log 2>&1
899
900                                 for dir in $(awk '/^obj-m/ { print [$]3 }' ${D}/$mf); do
901                                         name=${dir%/}
902                                         AC_MSG_NOTICE([touch ${D}/$name/$name.tested])
903                                         touch ${D}/$name/$name.tested
904                                 done
905                                 rm ${D}/Makefile
906                         done
907                         # compile test was NOT run. Re-compile everything.
908                 ])
909         ],[
910                 # test source does not exist:
911                 AC_MSG_ERROR([
912 *** No matching source for the "$1" test, check that
913 *** both the test source and result macros refer to the same name.
914                 ])
915         ])
916         # Abort if key does not exist
917         AS_IF([test -f ${O}.tested], [],
918                 [AC_MSG_ERROR([*** Compile test for $1 was not run.])])
919         # If test was compiled and if we got an object ...
920         AS_IF([test -f ${O}.o], [touch ${O}.ko])
921         # key is valid. Cache should be valid, set the variable
922         AC_CACHE_CHECK([for $1], lb_test,
923                 AS_IF([test -f ${O}.ko],
924                         AS_VAR_SET([lb_test], [yes]),
925                         AS_VAR_SET([lb_test], [no])))
926         # Read the variable and run the caller's actions for yes (arg2) or no (arg3)
927         AS_VAR_IF([lb_test], [yes], $2, $3)
928         AS_VAR_POPDEF([lb_test])
929 ]) # LB2_LINUX_TEST_RESULT
930
931 #
932 # LB2_SRC_CHECK_CONFIG
933 #
934 # generate source to check if a given config option is defined
935 #
936 #   $1 - CONFIG_<name>
937 #
938 AC_DEFUN([LB2_SRC_CHECK_CONFIG], [
939         LB2_LINUX_TEST_SRC([config_$1], [
940                 #include <$AUTOCONF_HDIR/autoconf.h>
941         ], [
942                 #ifndef CONFIG_$1
943                 #error CONFIG_$1 not #defined
944                 #endif
945         ])
946 ]) # LB2_SRC_CHECK_CONFIG
947
948 #
949 # LB2_TEST_CHECK_CONFIG
950 #
951 # test result compile test from LB2_SRC_CHECK_CONFIG
952 #
953 #   $1 - CONFIG_<name>
954 #   $2 - do 'yes'
955 #   $3 - do 'no'
956 #
957 AC_DEFUN([LB2_TEST_CHECK_CONFIG], [
958         AC_MSG_CHECKING([if Linux kernel was built with CONFIG_$1])
959         LB2_LINUX_TEST_RESULT([config_$1], [
960                 $2
961         ],[
962                 $3
963         ])
964 ]) # LB2_TEST_CHECK_CONFIG
965
966 #
967 # LB2_SRC_CHECK_CONFIG_IM
968 #
969 # generate source to check if a given config option is defined as built-in
970 # or as a module.
971 #
972 #    $1 - CONFIG_<name> or CONFIG_<name>_MODULE
973 #
974 AC_DEFUN([LB2_SRC_CHECK_CONFIG_IM], [
975         LB2_LINUX_TEST_SRC([config_im_$1], [
976                 #include <$AUTOCONF_HDIR/autoconf.h>
977         ], [
978                 #if !(defined(CONFIG_$1) || defined(CONFIG_$1_MODULE))
979                 #error CONFIG_$1 and CONFIG_$1_MODULE not #defined
980                 #endif
981         ])
982 ]) # LB2_SRC_CHECK_CONFIG_IM
983
984
985 #
986 # LB2_TEST_CHECK_CONFIG_IM
987 #
988 # test result compile test from LB2_SRC_CHECK_CONFIG_IM
989 #
990 #   $1 - CONFIG_<name>
991 #   $2 - do 'yes'
992 #   $3 - do 'no'
993 #
994 AC_DEFUN([LB2_TEST_CHECK_CONFIG_IM], [
995         AC_MSG_CHECKING([if Linux kernel enabled CONFIG_$1 as built-in or module])
996         LB2_LINUX_TEST_RESULT([config_im_$1], [
997                 $2
998         ],[
999                 $3
1000         ])
1001 ]) # LB2_TEST_CHECK_CONFIG_IM