adding autoconf tests

Andrew Tridgell tridge at samba.anu.edu.au
Thu Jul 30 06:48:25 GMT 1998


Many of the Samba developers probaly have never written autoconf
configure code. In this mail I will describe how to add a new test to
Samba. The example I will use is adding a test to see if the
__FUNCTION__ macro is supported by the compiler. This came up with
Chris's proposal to revamp the Debug1() code to produce better
formatted and more useful output.

If you are a Samba developer then please keep this email handy!

The first thing to do is to work out what test you actually want to
perform. In general it is a bad idea to test for a particular OS as
otherwise you will not enable the feature on other OSes that have
it. Instead you want to test for the feature itself. Sometimes this
can be tricky, but it is worth it. 

Next, work out how you would test for this feature manually. Would you
test that a bit of code compiles? Just test for the existance of a
heaer file? Test that a bit of code runs correctly? Look for a
library? 

In this example we want to check whether a lump of code compiles. The
code would be something like:

     printf("%s\n", __FUNCTION__)

if that compiles then we know the compiler supports the magic
__FUNCTION__ macro.

Now we need to edit 2 files. The first is configure.in. We choose an
appropriate place (maybe near a similar test) and add the following by
looking at an existing test:

 echo $ac_n "checking for __FUNCTION__ macro ... $ac_c"
 AC_TRY_COMPILE([#include <stdio.h>], [printf("%s\n", __FUNCTION__);],
 echo yes;AC_DEFINE(HAVE_FUNCTION_MACRO), 
 echo no)

I know the syntax is weird, you just have to put up with it. Base your
code on an existing test.

Then as this is a "special" test (ie. not a built in autoconf test
lile looking for a function or a header file) we need to add the
following line to acconfig.h:

 #undef HAVE_FUNCTION_MACRO

Next you run:

 autoheader
 autoconf

assuming you have autoconf installed ...

you will get piles of warnings about cross compiling. Ignore them
unless you feel like sending me patches to support cross compiling. I
was too lazy to do it and don't have a cross-compiler environment to
test in anyway.

Next, run ./configure (perhaps after "rm config.cache"). Does it test
correctly? Look in config.log for any error messages.

Finally, you can add code in Samba that does:

#ifdef HAVE_FUNCTION_MACRO
 some_code();
#else
 other_code();
#endif /* HAVE_FUNCTION_MACRO */

if you find yourself adding that code in more than 1 or two places
then you should consider using a function instead. We don't want Samba
to become a pile of #ifdef spaghetti.

Some other comments:

- If your test is complex then write a full C program to test the
  feature and put the test in the tests/ directory. Follow the
  examples in that directory for how to do this. Look at the existing
  configure.in tests for how to call these functions from autoconf. 

- if you want to add the autoconf guff to support caching of all the
  configure variables then please do so, and send me patches. I don't
  consider it important enough to spend time on.

- if the thing you want to test for isn't easily automated or some
  people might not want it then use a --with-XXX option. See the
  examples in configure.in.

- we don't use automake, and probably won't ever do so.

Read the autoconf info pages if you need more detail than what I have
given.

	Cheers, Tridge


More information about the samba-technical mailing list