2
votes

I have a C library, GPTL, which is built with libtool. (https://github.com/jmrosinski/GPTL)

I have a Fortran wrapper library, GPTL-fortran, which calls the C library. (https://github.com/NOAA-GSD/GPTL-fortran)

I have a third github repo, GPTL-all. (https://github.com/NOAA-GSD/GPTL-all). I would like to use AC_CONFIG_SUBDIR to have GPTL-all build both the C and the fortran libraries.

The problem is that the Fortran library depends on the C library. When building separately, the C library is built and installed first, then the Fortran library is built with CPPFLAGS and LDFLAGS set to point to the location of the installed C library.

Is there a way to achieve this with a combined package that installs the C and Fortran libraries?

Here's what I have so far:

# This is the autoconf file for GPTL-all, a combined C and Fortran
# library distribution.

AC_PREREQ([2.69])
AC_INIT([GPTL-all], [1.0.0], [[email protected]])

# Find out about the host we're building on.
AC_CANONICAL_HOST

# Find out about the target we're building for.
AC_CANONICAL_TARGET

# Initialize automake.
AM_INIT_AUTOMAKE([foreign subdir-objects])

# Keep macros in an m4 directory.
AC_CONFIG_MACRO_DIR([m4])

# Set up libtool.
LT_PREREQ([2.4])
LT_INIT()

AC_CONFIG_FILES([Makefile])

AC_CONFIG_SUBDIRS([GPTL
                   GPTL-fortran])
AC_OUTPUT

But this fails. When I run configure, it runs the C library configure just fine. But the fortran library configure fails because it checks for the presence of the C library:

checking for GPTLinitialize in -lgptl... no
configure: error: Can't find or link to the GPTL C library.
configure: error: ./configure failed for GPTL-fortran

How can I get GPTL-fortran to depend on GPTL?

1

1 Answers

0
votes

I did this by adding a new option to the Fortran library build:

# When built as part of the combined C/Fortran library distribution,
# the fortran library needs to be built with
# --enable-package-build. This tells the fortran library where to find
# the C library.
AC_ARG_ENABLE([package-build],
  [AS_HELP_STRING([--enable-package-build],
    [Set internally for package builds, should not be used by user.])])
test "x$enable_package_build" = xyes || enable_package_build=no
AM_CONDITIONAL([BUILD_PACKAGE], [test "x$enable_package_build" = xyes])

# Find the GPTL C library, unless this is a combined C/Fortran library
# build.
if test $enable_package_build = no; then
   AC_CHECK_LIB([gptl], [GPTLinitialize], [],
                        [AC_MSG_ERROR([Can't find or link to the GPTL C library.])])
fi

When launching this configure from the combined library configure, I add this extra option:

# Add this arg for the fortran build, to tell it to use the C library
# we just built.
ac_configure_args="$ac_configure_args --enable-package-build"

# Build the GPTL Fortran library.
AC_CONFIG_SUBDIRS([GPTL-fortran])

And in the GPTL-fortran test directory Makefile.am I added the following:

# For combined C/Fortran builds, find the C library.
if BUILD_PACKAGE
LDADD = ${top_builddir}/../GPTL/src/libgptl.la
endif

So when the package build is happening, it looks in ../GPTL/src for the GPTL library, and for non-package builds, the GPTL C library is located in configure.ac.