Skip to content

Commit 522e7aa

Browse files
marshallwardadcroft
authored andcommitted
Autoconf: Fortran testing of C bindings
This patch fixes some issues with testing of C bindings in Fortran. Specifically, some tests are using a C compiler which may be unconfigured, causing unexpected errors. The autoconf script now uses the Fortran compiler to test these bindings, rather than using the C compiler to test for their existence. A new macro (AX_FC_CHECK_BIND_C) was added to run these tests. This achieves the actual goal (test of Fortran binding) on top of the original goal (availability of C function), while ensuring that the actual compiler of interest (FC) is used in the test. Two C-based tests are still present in the script for testing the size of jmp_buf and sigjmp_buf. The C compiler is now configured with the AX_MPI macro, and is only used to determine the size of these structs.
1 parent d46de87 commit 522e7aa

File tree

3 files changed

+61
-12
lines changed

3 files changed

+61
-12
lines changed

ac/configure.ac

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,9 @@ AC_FC_SRCEXT(f90)
8888
# - This can cause standard AC_PROG_FC tests to fail if FCFLAGS is configured
8989
# with flags from another compiler.
9090
# - I do not yet know how to resolve this possible issue.
91-
AX_MPI([],
92-
[AC_MSG_ERROR([Could not find MPI launcher.])])
91+
AX_MPI([], [
92+
AC_MSG_ERROR([Could not find MPI launcher.])
93+
])
9394

9495

9596
# Explicitly replace FC and LD with MPI wrappers
@@ -233,13 +234,13 @@ AC_CONFIG_COMMANDS(Makefile.dep, [make depend])
233234

234235

235236
# POSIX verification tests
236-
AC_LANG_PUSH([C])
237237

238238
# These symbols may be defined as macros, making them inaccessible by Fortran.
239-
# The following exist in BSD and Linux, so we just test for them.
240-
AC_CHECK_FUNC([setjmp], [], [AC_MSG_ERROR([Could not find setjmp.])])
241-
AC_CHECK_FUNC([longjmp], [], [AC_MSG_ERROR([Could not find longjmp.])])
242-
AC_CHECK_FUNC([siglongjmp], [], [AC_MSG_ERROR([Could not find siglongjmp.])])
239+
# These three exist in modern BSD and Linux libc, so we just confirm them.
240+
# But one day, we many need to handle them more carefully.
241+
AX_FC_CHECK_BIND_C([setjmp], [], [AC_MSG_ERROR([Could not find setjmp.])])
242+
AX_FC_CHECK_BIND_C([longjmp], [], [AC_MSG_ERROR([Could not find longjmp.])])
243+
AX_FC_CHECK_BIND_C([siglongjmp], [], [AC_MSG_ERROR([Could not find siglongjmp.])])
243244

244245
# Determine the sigsetjmp symbol. If missing, then point to sigsetjmp_missing.
245246
#
@@ -248,14 +249,20 @@ AC_CHECK_FUNC([siglongjmp], [], [AC_MSG_ERROR([Could not find siglongjmp.])])
248249
# __sigsetjmp glibc (Linux)
249250
SIGSETJMP="sigsetjmp_missing"
250251
for sigsetjmp_fn in sigsetjmp __sigsetjmp; do
251-
AC_CHECK_FUNC([${sigsetjmp_fn}], [
252+
AX_FC_CHECK_BIND_C([${sigsetjmp_fn}], [
252253
SIGSETJMP=${sigsetjmp_fn}
253254
break
254255
])
255256
done
256257
AC_DEFINE_UNQUOTED([SIGSETJMP_NAME], ["${SIGSETJMP}"])
257258

258-
# Determine the size of jmp_buf and sigjmp_buf
259+
# Verify the size of nonlocal jump buffer structs
260+
# NOTE: This requires C compiler, but can it be done with a Fortran compiler?
261+
AC_LANG_PUSH([C])
262+
263+
AX_MPI([], [AC_MSG_ERROR([Could not find MPI launcher.])])
264+
AC_SUBST([CC], [$MPICC])
265+
259266
AC_CHECK_SIZEOF([jmp_buf], [], [#include <setjmp.h>])
260267
AC_CHECK_SIZEOF([sigjmp_buf], [], [#include <setjmp.h>])
261268

ac/m4/ax_fc_check_bind_c.m4

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
dnl AX_FC_CHECK_C_LIB(FUNCTION,
2+
dnl [ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND],
3+
dnl [OTHER-LDFLAGS], [OTHER-LIBS])
4+
dnl
5+
dnl This macro checks if a C binding is available to the compiler.
6+
dnl
7+
dnl Equivalently, it checks if the Fortran compiler can see a C function.
8+
dnl
9+
dnl Results are cached in `ax_fc_cv_bind_c_FUNCTION`.
10+
dnl
11+
AC_DEFUN([AX_FC_CHECK_BIND_C], [
12+
AS_VAR_PUSHDEF([ax_fc_Bind_C], [ax_fc_cv_bind_c_$1])
13+
m4_ifval([$4],
14+
[ax_fc_bind_c_msg_LDFLAGS=" with $4"],
15+
[ax_fc_bind_c_msg_LDFLAGS=""]
16+
)
17+
AC_CACHE_CHECK(
18+
[if $FC can bind $1$ax_fc_bind_c_msg_LDFLAGS], [ax_fc_cv_bind_c_$1], [
19+
ax_fc_check_bind_c_save_LDFLAGS=$LDFLAGS
20+
LDFLAGS="$4 $LDFLAGS"
21+
ax_fc_check_bind_c_save_LIBS=$LIBS
22+
LIBS="$5 $LIBS"
23+
AC_LINK_IFELSE(
24+
[AC_LANG_PROGRAM([],[dnl
25+
dnl begin code block
26+
interface
27+
subroutine test() bind(c, name="$1")
28+
end subroutine test
29+
end interface
30+
call test])
31+
dnl end code block
32+
],
33+
[AS_VAR_SET([ax_fc_Bind_C], [yes])],
34+
[AS_VAR_SET([ax_fc_Bind_C], [no])]
35+
)
36+
LDFLAGS=$ax_fc_check_bind_c_save_LDFLAGS
37+
LIBS=$ax_fc_check_bind_c_save_LIBS
38+
]
39+
)
40+
AS_VAR_IF([ax_fc_Bind_C], [yes], [$2], [$3])
41+
AS_VAR_POPDEF([ax_fc_Bind_C])
42+
])

ac/m4/ax_fc_check_c_lib.m4

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ dnl AX_FC_CHECK_C_LIB(LIBRARY, FUNCTION,
22
dnl [ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND],
33
dnl [OTHER-LDFLAGS], [OTHER-LIBS])
44
dnl
5-
dnl This macro checks if a C binding is available to the compiler.
6-
dnl
7-
dnl Equivalently, it checks if the Fortran compiler can see a C function.
5+
dnl This macro checks if a C library can be referenced by a Fortran compiler.
86
dnl
97
dnl Results are cached in `ax_fc_cv_c_lib_LIBRARY_FUNCTION`.
108
dnl
9+
dnl NOTE: Might be possible to rewrite this to use `AX_FC_CHECK_BIND_C`.
10+
dnl
1111
AC_DEFUN([AX_FC_CHECK_C_LIB], [
1212
AS_VAR_PUSHDEF([ax_fc_C_Lib], [ax_fc_cv_c_lib_$1_$2])
1313
m4_ifval([$5],

0 commit comments

Comments
 (0)