mivkov
mivkov

Reputation: 553

autoconf - how to check for a (minimal) version of a library at configure time?

I want to add a check for a minimal version of a library that I need for my project at configure time.

The library itself stores its version in a struct library_name_version_struct, such that I could obtain the library version using the following code:

#include "library_name.h"

void main(void){
  printf("%s\n", library_name_version_struct.version);
}

which would give me the output

libraryMAJOR.MINOR.MICRO

I thought of trying to get autoconf to run that minimal code, capture the output, and then (at least as a start) just stupidly check whether the output string is in a list of permissible output strings that I specify. Something along the lines of

AC_MAGIC_COMMAND_THAT_I_DONT_KNOW( [[#include "library_name.h"], 
                              [printf("%s\n", library_name_version_struct.version);]],
                              [STORE_OUTPUT_IN_THIS_VARIABLE],
                              [Oh no something went really wrong])

case STORE_OUTPUT_IN_THIS_VARIABLE in
  library_name1.0.0 | library_name1.1.0 | (etc...) | library_name3.1.0)
     # we good
  ;;
  *)
     AC_MSG_ERROR([STORE_OUTPUT_IN_THIS_VARIABLE is not a permitted version])
  ;;
esac

Any other or better way of achieving this would also be very much appreciated. I just want to get this to work.

Upvotes: 0

Views: 149

Answers (1)

ndim
ndim

Reputation: 37837

The Autoconf macro you are looking for is AC_RUN_IFELSE.

AC_RUN_IFELSE([dnl
AC_LANG_PROGRAM([[
#include <stddef.h>
#include <string.h>
]], [[
  const char *known_good[] = {
    "library_name1.0.0",
    "library_name1.1.0",
    ...
    "library_name3.1.0",
    NULL
  }; 
  const char *version_string = library_name_version_struct.version;
  for (size_t i=0; known_good[i] != NULL; ++i) {
    if (strcmp(known_good[i], version_string) == 0) {
      return 0; /* we good */
    }
  }  
  return 1; /* we not good */
]])
], [dnl
  AC_MSG_RESULT([yesss])
], [dnl
  AC_MSG_RESULT([noooo])
  AC_MSG_ERROR([cross-compiling, library_name not found, error compiling with or linking against library_name, or unsupported library_name version])
])

However, there are at least two problems with using AC_RUN_IFELSE to check for the runtime value of library_name_version_struct.version:

  • AC_RUN_IFELSE cannot detect the library version when cross compiling.

  • The library version present at the time your binary program is built can differ from the library version the compiled program eventually runs with.

I would try to look for a different mechanism to check for the library version. Check for some minimum ABI version for compilation (e.g. with PKG_CHECK_MODULES if the library in question supports that), and then maybe an actual runtime check for inside the compiled program.

More details about or a concrete pointer to the library in question might enable a more useful answers.

Upvotes: 1

Related Questions