Whamcloud - gitweb
LU-8367 osp: wait for precreate on reformatted OST
[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
266 # -------- check for linux --------
267 LB_CHECK_FILE([$LINUX], [],
268         [AC_MSG_ERROR([Kernel source $LINUX could not be found.])])
269
270 # -------- linux objects (for 2.6) --
271 AC_MSG_CHECKING([for Linux objects])
272 AC_ARG_WITH([linux-obj],
273         AS_HELP_STRING([--with-linux-obj=path],
274                         [set path to Linux objects (default=/lib/modules/$(uname -r)/build,/usr/src/linux)]),
275         [LB_ARG_CANON_PATH([linux-obj], [LINUX_OBJ])],
276         [LINUX_OBJ=$DEFAULT_LINUX_OBJ])
277 AC_MSG_RESULT([$LINUX_OBJ])
278
279 ## -------- with linux should point to <kernel>-common on Debian
280 AS_IF([test ${LINUX} == ${LINUX_OBJ} -a ${LINUX} == $(realpath ${LINUX})],[
281         this_arch=$(realpath ${LINUX} | sed 's/-/\n/g' | tail -1)
282         linux_headers_common=$(realpath ${LINUX}|sed "s/-${this_arch}\$/-common/g")
283         AS_IF([test "${this_arch}" != common],[
284                 _cah="${linux_headers_common}/include/linux/compiler_attributes.h"
285                 _cgh="${linux_headers_common}/include/linux/compiler-gcc.h"
286                 AS_IF([test -f "${_cah}" -o -f "${_cgh}"],[
287                         AC_MSG_WARN([Setting LINUX to ${linux_headers_common} was ${LINUX}])
288                         LINUX=${linux_headers_common}])
289                 ])
290         ])
291 AC_SUBST(LINUX)
292 AC_SUBST(LINUX_OBJ)
293 # -------- check for .config --------
294 AC_ARG_WITH([linux-config],
295         [AS_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         AS_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 AC_DEFUN([LB_PROG_LINUX_SRC], [
427         LB2_SRC_CHECK_CONFIG([MODULES])
428         LB2_SRC_CHECK_CONFIG([MODVERSIONS])
429 ])
430
431 AC_DEFUN([LB_PROG_LINUX_RESULTS], [
432         LB2_TEST_CHECK_CONFIG([MODULES], [], [AC_MSG_ERROR(
433                 [module support is required to build Lustre kernel modules.])
434         ])
435         LB2_TEST_CHECK_CONFIG([MODVERSIONS],[],[])
436 ])
437
438 #
439 # LB_PROG_LINUX
440 #
441 # linux tests
442 #
443 AC_DEFUN([LB_PROG_LINUX], [
444 LB_LINUX_PATH
445 LB_LINUX_SYMVERFILE
446 # 2.6.28
447 LC_MODULE_LOADING
448 ])
449
450 #
451 # LB_USES_DPKG
452 #
453 # Determine if the target is a dpkg system or rpm
454 #
455 AC_DEFUN([LB_USES_DPKG], [
456 AC_CACHE_CHECK([if this distro uses dpkg], lb_cv_uses_dpkg, [
457 lb_cv_uses_dpkg="no"
458 AS_CASE([$(which dpkg 2>/dev/null)],[*/dpkg], [lb_cv_uses_dpkg="yes"])
459 ])
460 uses_dpkg=$lb_cv_uses_dpkg
461 ])
462
463 #
464 # LB_CHECK_EXPORT
465 #
466 # check symbol exported or not
467 # $1 - symbol
468 # $2 - file(s) for find.
469 # $3 - do 'yes'
470 # $4 - do 'no'
471 #
472 # 2.6 based kernels - put modversion info into $LINUX/Module.modvers or check
473 #
474 AC_DEFUN([LB_CHECK_EXPORT], [
475 AS_VAR_PUSHDEF([lb_export], [lb_cv_export_$1])dnl
476 AC_CACHE_CHECK([if Linux kernel exports '$1'], lb_export, [
477 AS_VAR_SET([lb_export], [no])
478 AS_IF([grep -q -E '[[[:space:]]]$1[[[:space:]]]' $LINUX_OBJ/$SYMVERFILE 2>/dev/null],
479         [AS_VAR_SET([lb_export], [yes])],
480         [for file in $2; do
481                 AS_IF([grep -q -E "EXPORT_SYMBOL.*\($1\)" "$LINUX/$file" 2>/dev/null], [
482                         AS_VAR_SET([lb_export], [yes])
483                         break
484                 ])
485         done])
486 ])
487 AS_VAR_IF([lb_export], [yes], [$3], [$4])[]dnl
488 AS_VAR_POPDEF([lb_export])dnl
489 ]) # LB_CHECK_EXPORT
490
491 #
492 # LB_CHECK_CONFIG
493 #
494 # check if a given config option is defined
495 # $1 - CONFIG_<name>
496 # $2 - do 'yes'
497 # $3 - do 'no'
498 #
499 AC_DEFUN([LB_CHECK_CONFIG], [
500 LB_CHECK_COMPILE([if Linux kernel was built with CONFIG_$1],
501 config_$1, [
502         #include <$AUTOCONF_HDIR/autoconf.h>
503 ], [
504         #ifndef CONFIG_$1
505         #error CONFIG_$1 not #defined
506         #endif
507 ], [$2], [$3])
508 ]) # LB_CHECK_CONFIG
509
510 #
511 # LB_CHECK_CONFIG_IM
512 #
513 # check if a given config option is builtin or as module
514 # $1 - CONFIG_<name> or CONFIG_<name>_MODULE
515 # $2 - do 'yes'
516 # $3 - do 'no'
517 #
518 AC_DEFUN([LB_CHECK_CONFIG_IM], [
519 LB_CHECK_COMPILE([if Linux kernel was built with CONFIG_$1 in or as module],
520 config_im_$1, [
521         #include <$AUTOCONF_HDIR/autoconf.h>
522 ], [
523         #if !(defined(CONFIG_$1) || defined(CONFIG_$1_MODULE))
524         #error CONFIG_$1 and CONFIG_$1_MODULE not #defined
525         #endif
526 ], [$2], [$3])
527 ]) # LB_CHECK_CONFIG_IM
528
529 #
530 # these are like AC_TRY_COMPILE, but try to build modules against the
531 # kernel, inside the build directory
532 #
533
534 #
535 # LB_LANG_PROGRAM(C)([PROLOGUE], [BODY])
536 # --------------------------------------
537 #
538 m4_define([LB_LANG_PROGRAM],
539 [
540 #include <linux/kernel.h>
541 #include <linux/module.h>
542
543 #if defined(NEED_LOCKDEP_IS_HELD_DISCARD_CONST) \
544  && defined(CONFIG_LOCKDEP) \
545  && defined(lockdep_is_held)
546 #undef lockdep_is_held
547         #define lockdep_is_held(lock) \
548                 lock_is_held((struct lockdep_map *)&(lock)->dep_map)
549 #endif
550
551 $1
552 int
553 main (void)
554 {
555 dnl Do *not* indent the following line: there may be CPP directives.
556 dnl Don't move the `;' right after for the same reason.
557 $2
558   ;
559   return 0;
560 };
561 MODULE_LICENSE("GPL");])
562
563 #
564 # LB_LINUX_COMPILE_IFELSE
565 #
566 # like AC_COMPILE_IFELSE
567 #
568 # $1 - AC_LANG_SOURCE()
569 # $2 - make target
570 # $3 - check command
571 # $4 - do 'yes'
572 # $5 - do 'no'
573 #
574 AC_DEFUN([LB_LINUX_COMPILE_IFELSE],
575 [m4_ifvaln([$1], [AC_LANG_CONFTEST([AC_LANG_SOURCE([$1])])])dnl
576 rm -f build/conftest.o build/conftest.mod.c build/conftest.ko
577 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])],
578         [$4],
579         [_AC_MSG_LOG_CONFTEST
580 m4_ifvaln([$5],[$5])dnl])
581 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
582 ])
583
584 #
585 # LB_LINUX_TRY_COMPILE
586 #
587 # like AC_TRY_COMPILE
588 #
589 AC_DEFUN([LB_LINUX_TRY_COMPILE], [
590 LB_LINUX_COMPILE_IFELSE(
591         [AC_LANG_SOURCE([LB_LANG_PROGRAM([[$1]], [[$2]])])],
592         [modules], [test -s build/conftest.o],
593         [$3], [$4])
594 ])
595
596 #
597 # LB_LINUX_TRY_MAKE
598 #
599 # like LB_LINUX_TRY_COMPILE, but with different arguments
600 #
601 AC_DEFUN([LB_LINUX_TRY_MAKE], [
602 LB_LINUX_COMPILE_IFELSE(
603         [AC_LANG_SOURCE([LB_LANG_PROGRAM([[$1]], [[$2]])])],
604         [$3], [$4], [$5], [$6])
605 ])
606
607 #
608 # LB_CHECK_COMPILE
609 # $1 - checking message
610 # $2 - variable name
611 # $3 - header
612 # $4 - body
613 # $5 - do 'yes'
614 # $6 - do 'no'
615 #
616 AC_DEFUN([LB_CHECK_COMPILE], [
617 AS_VAR_PUSHDEF([lb_compile], [lb_cv_compile_$2])dnl
618 AC_CACHE_CHECK([$1], lb_compile, [
619         LB_LINUX_TRY_COMPILE([$3], [$4],
620                 [AS_VAR_SET([lb_compile], [yes])],
621                 [AS_VAR_SET([lb_compile], [no])])
622 ])
623 AS_VAR_IF([lb_compile], [yes], [$5], [$6])[]dnl
624 AS_VAR_POPDEF([lb_compile])dnl
625 ]) # LB_CHECK_COMPILE
626
627 #
628 # LB_CHECK_LINUX_HEADER
629 #
630 # Like AC_CHECK_HEADER but checks for a kernel-space header
631 #
632 AC_DEFUN([LB_CHECK_LINUX_HEADER], [
633         AS_VAR_PUSHDEF([lb_header], [lb_cv_header_$1])
634         AC_CACHE_CHECK([for $1], lb_header, [
635                 LB_LINUX_COMPILE_IFELSE([LB_LANG_PROGRAM([@%:@include <$1>])],
636                         [modules], [test -s build/conftest.o],
637                         [AS_VAR_SET([lb_header], [yes])],
638                         [AS_VAR_SET([lb_header], [no])])
639         ])
640         AS_VAR_IF([lb_header], [yes], [$2], [$3])
641         AS_VAR_POPDEF([lb_header])
642 ]) # LB_CHECK_LINUX_HEADER
643
644 # ------------------------------------------------------------------------------
645 # Support 2 stage: parallel compile then checked test results
646 # Heavily inspired by OpenZFS
647
648 # When converting tests from an LB_CHECK_COMPILE to use an
649 # LB2_LINUX_TEST_SRC and LB2_LINUX_TEST_RESULT pairing the primary
650 # consideration if dependencies on the results of LB2_LINUX_TEST_RESULT
651 # or LB2_TEST_CHECK_CONFIG[_IM] are used by annother compile test.
652 # Such dependencies should be avoided without a clear understanding
653 # of when the test result is actually available.
654 # The serial LB_CHECK_* test produces immediate results
655 # where as the LB2_ variant is not available until an intermediate
656 # LB2_LINUX_TEST_COMPILE_ALL has been executed.
657 #
658 # Currently all of the non-conditional LB_CHECK_COMPILE and
659 # LB_CHECK_CONFIG[_IM] cases are executed before any of the
660 # LB2_LINUX_TEST_SRC, or LB2_SRC_CHECK_CONFIG[_IM] code snippets
661 # are written. [Reference LB_CONFIG_MODULES for more details]
662 #
663 # This allows such LB2_*_SRC_* test cases to rely on
664 # prior LB_CHECK_* results, provided such results are preserved
665 # in the global configure name space. Likewise an LB2_LINUX_TEST_RESULT
666 # or LB2_TEST_CHECK_CONFIG[_IM] may conditionally execute a further
667 # LB_CHECK_* if required. Although it would be typcially be faster
668 # to run a few compile tests and conditionally ignore the results
669 # the reduced configure time may not always out weight the added
670 # complexity.
671 #
672 # It is worth noting that the LB_CHECK_COMPILE macros second argument
673 # is used as the configure test cache name. While is has been important
674 # that this name be globally unique it is even more important in the
675 # parallel case. The is somewhat alleviated by the sanity check in the
676 # LB2_LINUX_TEST_RESULT macro as it will abort with an error if
677 # the cache name does not exist. However care should be taken
678 # that the cache name is a globally unique identifier.
679 #
680 # Non-unique cases that are otherwise correct can be identified in the
681 # configure output when a results include ".. (cached)". This indicates
682 # either a duplicate LB_CHECK_* execution which is not critical or a
683 # duplicate identifier which needs to be corrected.
684 #
685 # At this point the number of LB_ and LB2_ examples should be
686 # sufficient for the casual developer to perform cut-paste-modify
687 # without serious confusion or complication.
688 # ------------------------------------------------------------------------------
689
690 AC_DEFUN([LB2_LINUX_CONFTEST_C], [
691 TEST_DIR=${TEST_DIR:-${ac_pwd}/_lpb}
692 test -d ${TEST_DIR}/$2 || mkdir -p ${TEST_DIR}/$2
693 cat confdefs.h - <<_EOF >${TEST_DIR}/$2/$2.c
694 $1
695 _EOF
696 ])
697
698 #
699 # LB2_LINUX_CONFTEST_MAKEFILE
700 #
701 # $1 - *unique* test case name
702 # $2 - additional build flags (ccflags)
703 # $3 - external kernel includes for lnet o2ib|gni
704 # $4 - extra symbol in Psuedo.symvers (optional)
705 #
706 AC_DEFUN([LB2_LINUX_CONFTEST_MAKEFILE], [
707         TEST_DIR=${TEST_DIR:-${ac_pwd}/_lpb}
708         test -d ${TEST_DIR} || mkdir -p ${TEST_DIR}
709         test -d ${TEST_DIR}/$1 || mkdir -p ${TEST_DIR}/$1
710
711         file=${TEST_DIR}/$1/Makefile
712         EXT_INCLUDE="$3"
713         EXT_SYMBOL="$4"
714         PSYM_FILE=""
715         if test "${EXT_SYMBOL}x" != "x" ; then
716                 PSYM_FILE=${TEST_DIR}/$1/Psuedo.symvers
717                 echo -e "0x12345678\t${EXT_SYMBOL}\tvmlinux\tEXPORT_SYMBOL\t" > ${PSYM_FILE}
718         fi
719         XTRA_SYM=
720         if test "x$O2IBPATH" != "x"; then
721                 if test "x$O2IBPATH" != "x$LINUX_OBJ"; then
722                         XTRA_SYM="$O2IBPATH/Module.symvers"
723                 fi
724         fi
725
726         cat - <<_EOF >$file
727 # Example command line to manually build source
728 # make modules -C $LINUX_OBJ $ARCH_UM M=${TEST_DIR}/$1
729
730 ${LD:+LD="$LD"}
731 CC=$CC
732 ZINC=${ZFS}
733 SINC=${SPL}
734 ZOBJ=${ZFS_OBJ}
735 SOBJ=${SPL_OBJ}
736 PSYM=${PSYM_FILE}
737
738 LINUXINCLUDE  = $EXT_INCLUDE
739 LINUXINCLUDE += -I$LINUX/arch/$SUBARCH/include
740 LINUXINCLUDE += -Iinclude -Iarch/$SUBARCH/include/generated
741 LINUXINCLUDE += -I$LINUX/include
742 LINUXINCLUDE += -Iinclude2
743 LINUXINCLUDE += -I$LINUX/include/uapi
744 LINUXINCLUDE += -Iinclude/generated
745 LINUXINCLUDE += -I$LINUX/arch/$SUBARCH/include/uapi
746 LINUXINCLUDE += -Iarch/$SUBARCH/include/generated/uapi
747 LINUXINCLUDE += -I$LINUX/include/uapi -Iinclude/generated/uapi
748 ifneq (\$(SOBJ),)
749 LINUXINCLUDE += -include \$(SOBJ)/spl_config.h
750 endif
751 ifneq (\$(ZOBJ),)
752 LINUXINCLUDE += -include \$(ZOBJ)/zfs_config.h
753 endif
754 ifneq (\$(SINC),)
755 LINUXINCLUDE += -I\$(SINC)/include
756 endif
757 ifneq (\$(ZINC),)
758 LINUXINCLUDE += -I\$(ZINC) -I\$(ZINC)/include
759 ifneq (\$(SINC),)
760 LINUXINCLUDE += -I\$(SINC)
761 else
762 LINUXINCLUDE += -I\$(ZINC)/include/spl
763 endif
764 endif
765 LINUXINCLUDE += -include $CONFIG_INCLUDE
766 KBUILD_EXTRA_SYMBOLS += ${ZFS_OBJ:+$ZFS_OBJ/Module.symvers}
767 KBUILD_EXTRA_SYMBOLS += ${XTRA_SYM}
768 ifneq (\$(PSYM),)
769 KBUILD_EXTRA_SYMBOLS += \$(PSYM)
770 endif
771
772 ccflags-y := -Werror-implicit-function-declaration
773 _EOF
774
775         # Additional custom CFLAGS as requested.
776         m4_ifval($2, [echo "ccflags-y += $2" >>$file], [])
777
778         # Test case source
779         echo "obj-m := $1.o" >>$file
780         echo "obj-m += $1/" >>${TEST_DIR}/Makefile
781 ])
782
783 #
784 # LB2_LINUX_TEST_COMPILE
785 #
786 # Perform a full compile excluding the final modpost phase.
787 # $1 - flavor
788 # $2 - dirname
789 #
790 AC_DEFUN([LB2_LINUX_TEST_COMPILE], [
791         D="$(realpath [$2])"
792         L="$D/build.log.$1"
793         J=${TEST_JOBS:-$(nproc)}
794
795         AC_MSG_NOTICE([Making $1 in $D])
796         AC_MSG_NOTICE([KBUILD_MODPOST_NOFINAL="yes" make modules -k -j${J} -C $LINUX_OBJ $ARCH_UM M=${D}])
797
798         AC_TRY_COMMAND([KBUILD_MODPOST_NOFINAL="yes"
799                 make modules -k -j${J} -C $LINUX_OBJ $ARCH_UM M=${D} >${L} 2>&1])
800         AS_IF([test -f ${L}],
801               [AS_IF([test -f $2/Makefile],
802                      [mv $2/Makefile $2/Makefile.compile.$1])],
803               [AC_MSG_ERROR([*** Unable to compile test source ... $3])
804         ])
805 ])
806
807 #
808 # Perform the compilation of the test cases in two phases.
809 #
810 # Phase 1) attempt to build the object files for all of the tests
811 #          defined by the LB2_LINUX_TEST_SRC macro.
812 #
813 # Phase 2) disable all tests which failed the initial compilation.
814 #
815 # This allows us efficiently build the test cases in parallel while
816 # remaining resilient to build failures which are expected when
817 # detecting the available kernel interfaces.
818 #
819 # The maximum allowed parallelism can be controlled by setting the
820 # TEST_JOBS environment variable which defaults to $(nproc).
821 #
822 AC_DEFUN([LB2_LINUX_TEST_COMPILE_ALL], [
823         TEST_DIR=${TEST_DIR:-${ac_pwd}/_lpb}
824         # Phase 1 - Compilation only, final linking is skipped.
825         LB2_LINUX_TEST_COMPILE([$1], [${TEST_DIR}], [$2])
826         for dir in $(awk '/^obj-m/ { print [$]3 }' \
827             ${TEST_DIR}/Makefile.compile.$1); do
828                 name=${dir%/}
829                 touch ${TEST_DIR}/$name/$name.tested
830         done
831 ])
832
833 #
834 # LB2_LINUX_TEST_SRC
835 #
836 # $1 - *unique* name
837 # $2 - global
838 # $3 - source
839 # $4 - extra cflags
840 # $5 - external include paths
841 # $6 - fake symvers entry
842 #
843 # NOTICE as all of the test cases are compiled in parallel tests may not
844 # depend on the results other tests.
845 # Each test needs resolve any external dependencies at the time the program
846 # source is generated.
847 #
848 AC_DEFUN([LB2_LINUX_TEST_SRC], [
849         TEST_DIR=${TEST_DIR:-${ac_pwd}/_lpb}
850         AS_VAR_PUSHDEF([lb_test], [lb_cv_test_$1])
851         # Skip test write and build steps if the result is already known.
852         LB2_LINUX_CONFTEST_C([LB_LANG_PROGRAM([[$2]], [[$3]])], [$1_pc])
853         LB2_LINUX_CONFTEST_MAKEFILE([$1_pc], [$4], [$5], [$6])
854         AS_VAR_POPDEF([lb_test])
855 ])
856
857 #
858 # LB2_LINUX_TEST_RESULT
859 #
860 # $1 - *unique* name matching the LB2_LINUX_TEST_SRC macro
861 # $2 - run on success (valid .ko generated)
862 # $3 - run on failure (unable to compile)
863 #
864 AC_DEFUN([LB2_LINUX_TEST_RESULT],[
865         TEST_DIR=${TEST_DIR:-${ac_pwd}/_lpb}
866         AS_VAR_PUSHDEF([lb_test], [lb_cv_test_$1])
867         D="$(realpath ${TEST_DIR})"
868         T=${D}/$1_pc
869         O=${T}/$1_pc
870         AS_IF([test -d ${T}], [
871                 # test source exists, was the compile test run?
872                 AS_IF([test -f ${O}.tested],[
873                         # compile test was run.
874                 ],[
875                         # compile test was NOT run. Re-compile everything.
876                         AC_MSG_NOTICE([** Rebuilding all tests **])
877
878                         J=${TEST_JOBS:-$(nproc)}
879                         for mf in $(ls -1 ${TEST_DIR}/Makefile.compile.*); do
880                                 ln -sf $mf ${D}/Makefile
881                                 KBUILD_MODPOST_NOFINAL="yes"
882                                 make modules -k -j${J} -C $LINUX_OBJ $ARCH_UM M=${D} >> rebuild.log 2>&1
883
884                                 for dir in $(awk '/^obj-m/ { print [$]3 }' ${D}/$mf); do
885                                         name=${dir%/}
886                                         AC_MSG_NOTICE([touch ${D}/$name/$name.tested])
887                                         touch ${D}/$name/$name.tested
888                                 done
889                                 rm ${D}/Makefile
890                         done
891                         # compile test was NOT run. Re-compile everything.
892                 ])
893         ],[
894                 # test source does not exist:
895                 AC_MSG_ERROR([
896 *** No matching source for the "$1" test, check that
897 *** both the test source and result macros refer to the same name.
898                 ])
899         ])
900         # Abort if key does not exist
901         AS_IF([test -f ${O}.tested], [],
902                 [AC_MSG_ERROR([*** Compile test for $1 was not run.])])
903         # If test was compiled and if we got an object ...
904         AS_IF([test -f ${O}.o], [touch ${O}.ko])
905         # key is valid. Cache should be valid, set the variable
906         AC_CACHE_CHECK([for $1], lb_test,
907                 AS_IF([test -f ${O}.ko],
908                         AS_VAR_SET([lb_test], [yes]),
909                         AS_VAR_SET([lb_test], [no])))
910         # Read the variable and run the caller's actions for yes (arg2) or no (arg3)
911         AS_VAR_IF([lb_test], [yes], $2, $3)
912         AS_VAR_POPDEF([lb_test])
913 ]) # LB2_LINUX_TEST_RESULT
914
915 #
916 # LB2_SRC_CHECK_CONFIG
917 #
918 # generate source to check if a given config option is defined
919 #
920 #   $1 - CONFIG_<name>
921 #
922 AC_DEFUN([LB2_SRC_CHECK_CONFIG], [
923         LB2_LINUX_TEST_SRC([config_$1], [
924                 #include <$AUTOCONF_HDIR/autoconf.h>
925         ], [
926                 #ifndef CONFIG_$1
927                 #error CONFIG_$1 not #defined
928                 #endif
929         ])
930 ]) # LB2_SRC_CHECK_CONFIG
931
932 #
933 # LB2_TEST_CHECK_CONFIG
934 #
935 # test result compile test from LB2_SRC_CHECK_CONFIG
936 #
937 #   $1 - CONFIG_<name>
938 #   $2 - do 'yes'
939 #   $3 - do 'no'
940 #
941 AC_DEFUN([LB2_TEST_CHECK_CONFIG], [
942         AC_MSG_CHECKING([if Linux kernel was built with CONFIG_$1])
943         LB2_LINUX_TEST_RESULT([config_$1], [
944                 $2
945         ],[
946                 $3
947         ])
948 ]) # LB2_TEST_CHECK_CONFIG
949
950 #
951 # LB2_SRC_CHECK_CONFIG_IM
952 #
953 # generate source to check if a given config option is defined as built-in
954 # or as a module.
955 #
956 #    $1 - CONFIG_<name> or CONFIG_<name>_MODULE
957 #
958 AC_DEFUN([LB2_SRC_CHECK_CONFIG_IM], [
959         LB2_LINUX_TEST_SRC([config_im_$1], [
960                 #include <$AUTOCONF_HDIR/autoconf.h>
961         ], [
962                 #if !(defined(CONFIG_$1) || defined(CONFIG_$1_MODULE))
963                 #error CONFIG_$1 and CONFIG_$1_MODULE not #defined
964                 #endif
965         ])
966 ]) # LB2_SRC_CHECK_CONFIG_IM
967
968
969 #
970 # LB2_TEST_CHECK_CONFIG_IM
971 #
972 # test result compile test from LB2_SRC_CHECK_CONFIG_IM
973 #
974 #   $1 - CONFIG_<name>
975 #   $2 - do 'yes'
976 #   $3 - do 'no'
977 #
978 AC_DEFUN([LB2_TEST_CHECK_CONFIG_IM], [
979         AC_MSG_CHECKING([if Linux kernel enabled CONFIG_$1 as built-in or module])
980         LB2_LINUX_TEST_RESULT([config_im_$1], [
981                 $2
982         ],[
983                 $3
984         ])
985 ]) # LB2_TEST_CHECK_CONFIG_IM