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