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