Promi
Promi

Reputation: 318

How to define AM_CONDITIONAL and AC_DEFINE based on PKG_CHECK_MODULES result?

I am trying to write a configure.ac file to do these tasks:

  1. The configure script should accept a --with-libuv parameter.

  2. A variable with_libuv should be set to either yes, no or check (with check being the default value when nothing was passed on the command line).

  3. When with_libuv == "yes" a mandatory PKG_CHECK_MODULES check for libuv >= 1.9.0 should be done and HAVE_LIBUV = 1 should be set on success (On error configure should abort).

  4. When with_libuv == "no" nothing should be checked,

  5. When with_libuv == "false" an optional PKG_CHECK_MODULES check (for the same library as in 3.) should be done and HAVE_LIBUV should be set to either 0 or 1 accordingly.

  6. If with_libuv != "no" && HAVE_LIBUV == 1 AC_DEFINE should set -DUSE_LIBUV and AM_CONDITIONAL should set USE_LIBUV as a conditional for automake.

  7. If not with_libuv != "no" && HAVE_LIBUV == 1 the preprocessor directive should not be set and the AM_CONDITIONAL should be set to 0.

I have figured out how to do steps 1-5, but I am struggeling with 6 and 7.

Here is my current attempt:

AC_INIT(
  [mumble-pluginbot-plusplus],
  [0.5],
  [https://github.com/promi/mumble-pluginbot-plusplus/issues],
  [],
  [https://github.com/promi/mumble-pluginbot-plusplus])

AC_CONFIG_MACRO_DIR([m4])
AM_INIT_AUTOMAKE([foreign])
AM_SILENT_RULES([yes])

AC_PROG_CXX
LT_INIT

# Make sure that pkg-config is installed!
# The PKG_CHECK_MODULES macro prints a horrible error message when
# pkg-config is not installed at autogen time.                   
#
# It is also required when the first PKG_CHECK_MODULES is inside a conditional
PKG_PROG_PKG_CONFIG

PKG_CHECK_MODULES(OPUS, [opus >= 1.1])
PKG_CHECK_MODULES(OPENSSL, [openssl])
PKG_CHECK_MODULES(PROTOBUF, [protobuf])
PKG_CHECK_MODULES(MPDCLIENT, [libmpdclient])

AC_ARG_WITH(
  [libuv],
  [AS_HELP_STRING([--with-libuv], [support efficient MPD status polling @<:@default=check@:>@])],
  [],
  [with_libuv=check])

# if     --with-libuv    -> it must be installed
# elseif --without-libuv -> do nothing
# else                   -> check whether it is installed
AS_CASE(
  ["$with_libuv"],
  [yes], [PKG_CHECK_MODULES(UV, [libuv >= 1.9.0], [HAVE_LIBUV=1])],
  [no],  [],
         [PKG_CHECK_MODULES(UV, [libuv >= 1.9.0], [HAVE_LIBUV=1], [HAVE_LIBUV=0])])

if test "$with_libuv" != no -a "x$HAVE_LIBUV" -eq x1; then
   AM_CONDITIONAL([USE_LIBUV], [1])
   AC_DEFINE([USE_LIBUV], [1], [Define when libuv should be used.])
else
   AM_CONDITIONAL([USE_LIBUV], [0])
fi

#AC_CONFIG_HEADERS([src/config.h])
AC_CONFIG_FILES([Makefile])

AC_OUTPUT

The problematic part is this:

if test "$with_libuv" != no -a "x$HAVE_LIBUV" -eq x1; then
   AM_CONDITIONAL([USE_LIBUV], [1])
   AC_DEFINE([USE_LIBUV], [1], [Define when libuv should be used.])
else
   AM_CONDITIONAL([USE_LIBUV], [0])
fi

Here is an excerpt from the configure output:

checking pkg-config is at least version 0.9.0... yes
checking for OPUS... yes
checking for OPENSSL... yes
checking for PROTOBUF... yes
checking for MPDCLIENT... yes
checking for UV... yes
./configure: line 16467: test: x1: integer expression expected
./configure: line 16480: 0: command not found
checking that generated files are newer than configure... done

How can I implement steps 6 and 7 in a way that actually works?

Upvotes: 1

Views: 5939

Answers (3)

ldav1s
ldav1s

Reputation: 16305

You aren't aborting when yes fails (step 3). AM_CONDITIONAL should always be run. Step 6 says -DUSE_LIBUV, but your existing code will add -DUSE_LIBUV=1 to DEFS. Portable shell scripting considers test -a broken, so you shouldn't be using that. Your no case is equivalent to the check (or false) case where the search fails.

AS_CASE(
   ["$with_libuv"],
   [yes], [PKG_CHECK_MODULES(UV, [libuv >= 1.9.0], [HAVE_LIBUV=1],
                            [AC_MSG_ERROR("libuv >= 1.9.0 is not installed")])],
   [no],  [HAVE_LIBUV=0],
   [PKG_CHECK_MODULES(UV, [libuv >= 1.9.0], [HAVE_LIBUV=1],[HAVE_LIBUV=0])])

AS_IF([test "x$HAVE_LIBUV" = x1], [AC_DEFINE([USE_LIBUV])])
AM_CONDITIONAL([USE_LIBUV], [test "x$HAVE_LIBUV" = x1])

Upvotes: 4

Promi
Promi

Reputation: 318

This should do the trick:

if test "$with_libuv" != no -a "x$HAVE_LIBUV" == x1; then
   AC_DEFINE([USE_LIBUV], [1], [Define when libuv should be used.])
fi
AM_CONDITIONAL([USE_LIBUV], [test "$with_libuv" != no -a "x$HAVE_LIBUV" == x1])

It's a bit ugly, because the test is executed twice, but it seems to work ok.

Upvotes: 1

uzsolt
uzsolt

Reputation: 6027

I think you should use = instead of -eq. The -eq is a relation between integers - the x1 isn't integer!

if test "$with_libuv" != no -a "x$HAVE_LIBUV" -eq x1; then

replace to

if test "$with_libuv" != no -a "x$HAVE_LIBUV" = x1; then

Upvotes: 1

Related Questions