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