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