Monday, March 8, 2010

Add new option in configure script in autobuild system

Example:
To get a new option "--with-unit-test" when you run "./configure --help"

Optional Packages:
  --with-unit-test        Create binary to unit test the library package
                          (default is no)

add the following block in "configure.ac"

AC_MSG_CHECKING([--with-unit-test])
AC_ARG_WITH(unit-test,
        AC_HELP_STRING([--with-unit-test], [Create binary to unit test the library package (default is no)]),
        [unittest="yes"],
        [unittest="no"]
)
AM_CONDITIONAL(UNIT_TEST, test "$unittest" = "yes")
AC_MSG_RESULT([$unittest])


and you Makefile.am will look like this,

bindir = $(prefix)/usr/local/bin
libdir = $(prefix)/usr/local/lib

if UNIT_TEST
bin_PROGRAMS = unittest
unittest_SOURCES = source1.c \
                      Source2.c
unittest_CFLAGS = -I@top_srcdir@/include \
                     -DUNIT_TEST
unittest_LDFLAGS =
else
lib_LTLIBRARIES           = libunittest.la
libunittest_la_SOURCES = source2.c \
                         source3.c
libunittest_la_CFLAGS = -I@top_srcdir@/include
libunittest_la_LDFLAGS = -version-info 0:1:0
endif


and take care of UNIT_TEST macro check in the source code.