Next Up Previous Contents
Next: 6.3 Conditionally Building Components, II
Up: 6 Miscellaneous hints and tips
Previous: 6.1 Forcing automake's choice of linking language
[ID index][Keyword index]

6.2 Conditionally building components, I

It is occasionally useful to build code conditionally, depending on features of the environment. About the only place where this is necessary, however, is when building a target of a configure dependency (see Appendix A.16), where a library (for example) must be built only if it is not available on the platform already.

There is no way to decide not to build a component, so instead we arrange the configuration of the component in such a way that if the component's code is not required, then the component trivially builds and installs nothing, or nothing more than a stamp file.

This is true for the jpeg component, which is a configure dependency of startcl, and located in the repository in thirdparty/ijg/jpeg. The important parts of jpeg's configure.ac are as follows:


AC_CHECK_HEADER([jpeglib.h],
  [MAIN_TARGET=jpeglib-stamp],
  [
  (
    cd src
    echo ./configure --prefix=$prefix --cache-file=config.cache
    ./configure --prefix=$prefix --cache-file=config.cache
  )
  MAIN_TARGET=jpeglib
])
AC_SUBST(MAIN_TARGET)

STAR_SPECIAL_INSTALL_COMMAND([
    if test $(MAIN_TARGET) = jpeglib; then
      cd src;
      for d in bin lib include man/man1;
        do $(mkdir_p) $$DESTDIR$(prefix)/$$d;
      done;
      $(MAKE) DESTDIR=$$DESTDIR install install-lib;
    fi])
The first part wraps the actual jpeglib configuration code (see Section 4.5.3 for the rationale for that code) inside an autoconf test for the jpeglib.h include file. If it finds it, there is nothing to do; if it doesn't then it does actually configure the package. The decision it makes is recorded in the substituted variable @MAIN_TARGET@ and its corresponding Makefile variable $(MAIN_TARGET).

Note that the STAR_SPECIAL_INSTALL_COMMAND is expanded at a different time from the header test, with the effect that it is effective in both conditions, when the header file is there and when it isn't. Thus the command inside it must be written with this in mind, and explicitly test the value of $(MAIN_TARGET).

The Makefile.am has corresponding support, the important parts of which are as follows.


all-local: $(MAIN_TARGET)

jpeglib:
        cd src; $(MAKE) all

jpeglib-stamp:
        rm -f $@
        { date; \
          echo "jpeglib.h found in system -- no need to build our own"; } >$@
We can't override the `all' target, but instead use automake's `all-local' hook which, if it is present, is built at the same time as `all'. Thus depending on the value of the variable $(MAIN_TARGET), as determined and substituted by configure.ac, we build either the jpeglib software, or a stamp file. The install command in the configure.ac file above then avoids installing anything if $(MAIN_TARGET) is not the string jpeglib.


Next Up Previous Contents
Next: 6.3 Conditionally Building Components, II
Up: 6 Miscellaneous hints and tips
Previous: 6.1 Forcing automake's choice of linking language
[ID index][Keyword index]
The Starlink Build System
Starlink System Note 78
Norman Gray, Peter W Draper, Mark B Taylor, Steven E Rankin
11 April 2005. Release snapshot: $Revision: 1.116 $. Last updated 28 May 2006