autoconf
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

How to write a conditional test in configure.ac


From: Svante Signell
Subject: How to write a conditional test in configure.ac
Date: Thu, 12 Jan 2012 23:58:24 +0100

Hello.

Consider the code snippet below. How to modify it to get the GNU version
of baseline if defined (in my case GNU/Hurd). The macro 
AC_USE_SYSTEM_EXTENSIONS is stated earlier in configure.ac.

Something like:
>>>if defined _GNU_SOURCE<<<
<check for a working GNU basename>
...
    [AC_TRY_RUN([
#include <stdio.h>
#include <stdlib.h>
#ifdef HAVE_GNU_SOURCE
# define _GNU_SOURCE 1
# include <string.h>
#endif
<same tests as below>
...
>>>else<<<
<use the code below>
>>>endif<<<

(Or shorter if possible)
Thanks in advance (so much to learn about auto*...)

>From configure.ac:
# COMPAT_FUNC_BASENAME
# --------------------
# Check for working basename() function.
AC_CHECK_HEADERS([libgen.h])
AC_DEFUN([COMPAT_FUNC_BASENAME], [
  AC_DEFINE([NEED_BASENAME], 1,
            [Define if you want to use the basename function])
  AC_CHECK_HEADERS([libgen.h])
  AC_CACHE_CHECK([for working basename],
    [compat_cv_func_basename_works],

    [AC_TRY_RUN([
#include <stdio.h>
#include <stdlib.h>
#ifdef HAVE_LIBGEN_H
# include <libgen.h>
#endif

typedef struct {
  char *test;
  char *result;
} test_t;

const test_t tests[] = {
  { "/usr/local/foo", "foo" },
  { "/usr/local/foo/", "foo" },
  { NULL, NULL }
};

int main() {
  char test1[1024];
  int i;

  for (i = 0; tests[i].test; i++) {
    strcpy(test1, tests[i].test);
    if (strcmp(basename(test1), tests[i].result) ||
        strcmp(test1, tests[i].test))
      exit(1);
  }

  exit(0);
}
],
      [compat_cv_func_basename_works=yes],
      [compat_cv_func_basename_works=no],
      [compat_cv_func_basename_works=no]
    )]
  )
  if test "$compat_cv_func_basename_works" = "yes"; then
    AC_DEFINE([HAVE_BASENAME], 1,
              [Define if your system has a working basename])
  else
    AC_LIBOBJ([basename])
  fi
])





reply via email to

[Prev in Thread] Current Thread [Next in Thread]