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