How to add and implement configure flags in Autotools?

Part of our research team program has auxiliary functionality provided by the ctemplate library. In our dated cluster, we cannot create software because of compilation, so I would like to separate this functionality and control whether it is enabled or not using the configure flag, for example --disable-ctemplate .

The software written in C ++ uses the Autotools build system - none of them have a lot of experience. I understand that to complete this task I need to do the following:

  • Add a new flag to the script configuration by creating a new AC_ARG_ENABLE entry in configure.ac .

  • Add a few #ifdef expressions (or possibly #ifndef ) around code that uses the ctemplate library, and around any code that calls this code.

I think the first step will look something like this:

 AC_ARG_ENABLE(ctemplate, [ --disable-ctemplate Disable HTML output], [case "${enableval}" in yes) ctemplate=false ;; no) ctemplate=true ;; *) AC_MSG_ERROR(bad value ${enableval} for --disable-ctemplate) ;; esac],[ctemplate=true]) AM_CONDITIONAL(NOCTEMPLATE, test x$ctemplate = xfalse) 

although I don’t know if the logic is correct, since I adapted this example from examples that used --enable-FLAG instead of --disable-FLAG .

In the second stage, I will wrap sections in preprocessor flags, for example

 #ifndef NOCTEMPLATE void Class::MethodUsingCtemplate(...) { ... } #endif 

Would it be right to β€œplug in” everything if I did configure --disable-ctemplate ?

In addition, does this ensure that the program is not included in the ctemplate library for compilation? If not, then all this is for nothing; You must prevent the compilation of the ctemplate library and dependent components.

I will repeat that I am not familiar with C ++ and Autotools; I took a naive first approach to solving this problem. If you have experience in this area, I would really appreciate your corrections and any explanations that you could offer.

+4
source share
3 answers

Here is the solution that I put together after reading the documentation, tutorials, help streams, mailing lists, etc., and just tried everything until they worked, I could explain why they worked.

In configure.ac I put the following lines of code

 # This adds the option of compiling without using the ctemplate library, # which has proved troublesome for compilation on some platforms AC_ARG_ENABLE(ctemplate, [ --disable-ctemplate Disable compilation with ctemplate and HTML output], [case "${enableval}" in yes | no ) WITH_CTEMPLATE="${enableval}" ;; *) AC_MSG_ERROR(bad value ${enableval} for --disable-ctemplate) ;; esac], [WITH_CTEMPLATE="yes"] ) dnl Make sure we register this option with Automake, so we know whether to dnl descend into ctemplate for more configuration or not AM_CONDITIONAL([WITH_CTEMPLATE], [test "x$WITH_CTEMPLATE" = "xyes"]) # Define CTEMPLATE in config.h if we're going to compile against it if test "x$WITH_CTEMPLATE" = "xyes"; then AC_DEFINE([CTEMPLATE], [], ["build using ctemplate library"]) AC_MSG_NOTICE([ctemplate will be used, HTML output enabled]) else AC_MSG_NOTICE([ctemplate will not be used, HTML output disabled]) fi 

In the next step, I changed Makefile.am at the top level to the following:

 if WITH_CTEMPLATE MAYBE_CTEMPLATE = ctemplate endif SUBDIRS = boost libgsl $(MAYBE_CTEMPLATE) libutil ... 

At the bottom level of Makefile.am s I added

 if WITH_CTEMPLATE # some change to the configuration else # some other change to the configuration endif 

Finally, I had to make sure that one of the key C ++ header files (included by other parts of the code) has the following:

 #ifdef HAVE_CONFIG_H # include "config.h" #endif 

config.h contains any new definitions created using AC_DEFINE , so this file should be included in the parts that check if the macro definition created by this route (or undefined) is defined.

It took me a long time and with disappointment, for my part; I can only hope that documenting this explanation here saves someone from the same fate.

+5
source

You are definitely on the right track. Autotools may seem very complicated at first, but one of the advantages is that there are thousands of projects that have already done what you want to do. All you have to do is find them.

Here is a link to google code search for AC_ARG_DISABLE . Go nuts.

+1
source

So that the assembly does not try to compile in the ctemplate subdirectory, you need to do something like:

  if CTEMPLATE
 SUBDIRS + = ctemplate
 endif

in Makefile.am

+1
source

All Articles