How to Handle File Existence Check in `configure.ac` When Cross Compiling on WSL

38 Views Asked by At

I am working on a project that requires conditional compilation based on the presence of certain files, specifically for integrating IPOPT (an optimization library). My development environment is Windows Subsystem for Linux (WSL), and I face cross-compilation challenges.

In my configure.ac, I have a custom macro IPOPT_ARG_CHECK that checks for the existence of IpStdCInterface.h as part of determining whether to compile with IPOPT support. Here's the relevant part of the macro:

AC_DEFUN([IPOPT_ARG_CHECK],
[
    ...
    AC_ARG_WITH([$1],
       [AS_HELP_STRING([--$2],
         [IPOPT installation directory @<:@default=check@:>@])],
       [$1=$withval],
       [$3=no])

    AS_IF([test "x$$3" = xno],
          [...],
          [AC_CHECK_FILE($$1/include/$coin/IpStdCInterface.h,
                         [],
                         [AC_MSG_FAILURE([--$2 option was specified but does not seem to point at a valid IPOPT installation])])])

    AM_CONDITIONAL([$8],[test "x$$3" != xno])
    ...
])

However, I get a warning when I run autoreconf:

configure.ac:242: warning: cannot check for file existence when cross compiling

I'm looking for guidance on the canonical way to perform this check in a cross-compilation-friendly manner.

2

There are 2 best solutions below

0
Foad S. Farimani On

I understand that AC_CHECK_FILE is not recommended for cross-compilation scenarios. Based on suggestions (here and here), I replaced it with a simple test -f check:

AS_IF([test -f "$$1/include/$coin/IpStdCInterface.h"],
      [AC_MSG_RESULT([found])],
      [AC_MSG_RESULT([not found])
       AC_MSG_ERROR([--$2 option was specified but does not seem to point at a valid IPOPT installation])])

And that resolved the issue.

However, I believe the canonical way to perform this check in a cross-compilation-friendly manner is to rewrite the IPOPT_ARG_CHECK function using the PKG_CHECK_MODULES macro from the pkg-config system. It seems to be a more robust and common approach to handling dependencies in configure.ac, especially for cross-compilation scenarios. This macro checks for the existence of a package and its version and sets up CFLAGS and LIBS variables accordingly, which can then be used in the Makefiles.

P.S. I had already mentioned this method in the question as a failed attempt, but then later, I realized that I had a syntax error, and by fixing that, this worked. I also wrote about this solution here.

2
John Bollinger On

I am working on a project that requires conditional compilation based on the presence of certain files, specifically for integrating IPOPT (an optimization library). [...] I face cross-compilation challenges.

In this particular case, that's because you have framed the problem in an unhelpful way. You don't just want to generically check for a file. Rather, you want to check whether a working (for your purposes) IPOPT library has been configured. You should do that via AC_CHECK_HEADER, AC_CHECK_LIB, etc, not via AC_CHECK_FILE.

If you're already performing such checks then your AC_CHECK_FILE is unneeded. If you're not performing them, then you probably should be. Especially for good cross-compilation support, as these will verify that the configured library is compatible with your cross toolchain.

AC_CHECK_FILE is almost never the right tool for the job. Any job.