71

Why is it necessary even though everything is specified in a makefile?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mask
  • 33,129
  • 48
  • 101
  • 125

3 Answers3

67

Typically the configure script when run will:

  • Check some details about the machine on which the software is going to be installed. This script checks for lots of dependencies on your system. For the particular software to work properly, it may be requiring a lot of things to be existing on your machine already. If any of the major requirements are missing on your system, the configure script would exit and you cannot proceed with the installation, until you get those required things.

  • Create the Makefile to be used in the next step.

codaddict
  • 445,704
  • 82
  • 492
  • 529
  • 4
    Never know makefile was created by configure before:) – Mask Mar 27 '10 at 14:20
  • 2
    It also provides an interface to configure (aptly) compilation options. `./configure --help` will (usually?) give a list of available options. – intuited Mar 27 '10 at 14:38
35

It runs a script which typically produces makefiles and "configure.h".

The script is written in the lanugage "m4" which is a macro language. The top level macros are found in autoconf.ac or (in older systems) autoconf.in. These expand containing lower level macros which in turn expand into actual tests which create small programs or tasks to check what kind of system you have.

For example AC_CHECK_HEADER([myheader.h], ...) might generate a tiny C program like:

#include "myheader.h"
int main(int argc, char** argv) {
  return 0;
}

If the program compiles, the check is considered "passing" otherwise it "fails". The status of such checks often gets reflected in the config.h file. On a passing check, you might find a line in config.h that looks like:

#define HAVE_MYHEADER_H 1

while on a test that fails, it might look like

#define HAVE_MYHEADER_H 0

When configured to work with autoconf in AM_INIT_AUTOMAKE macro, the Makefile can also reference the results of the tests if the variable containing the test result is exported. So if a needed library is located a few different typical locations, or the syntax of "what works" with one of your standard tools (like tar, ar, etc) is different, or the preferred tool is not available, the Makefile will be able to still build the project properly using the different library locations, the different tool syntax, or a different set of tools.

So when dealing with an Autotools project (configure / make / make install) the Makefile really doesn't contain everything necessary to build the project, it's generated from the Makefile.in template to specifically match your system when you type "configure".

Edwin Buck
  • 69,361
  • 7
  • 100
  • 138
  • I noticed these `configure` files are extremely huge,are they written manually? – Mask Mar 28 '10 at 03:43
  • 11
    No, the configure script is an expansion (also using the m4 language) of directives which are put in the configure.ac (or in older systems configure.in) file. Running autoconf will generate the configure script from configure.ac, but sometimes you need to patch up the macro libraries. When you need to do that, run aclocal and it will try to verify that all the macros are properly defined (and useable for the next autoconf execution). – Edwin Buck Mar 29 '10 at 04:51
  • Everyone I know just seemed to pick up this stuff along the way without ever formally learning it. Is it actually taught in universities? – TrojanName Jun 28 '11 at 09:19
  • 3
    The core ideas necessary are taught in Universities, however, they are taught in different non-build contexts. Often few people think to apply them to a build system until their build system starts to fail in some way. That said, there's learning the theories behind build systems, idempotence, (dependency) mappings, (platform) set analysis, verification (aka testing), etc. Most people leave that knowledge at the door when they shop for a tool, which is then the only thing they care about. It is akin to learning how to use MS Word over learning how to write. – Edwin Buck Jun 28 '11 at 15:55
7

A configure script builds the Makefile from a template, substituting in things like where you want to install the code and what definitions are needed for the (C, C++, Fortran, ...) compiler that is used to build the program. Theoretically it could all be done in one step, except that there's that many different configurations possible that it's easier to do in stages. (For example, if building a large program with a multi-core machine available, you might want to specify that a certain number of parallel compilations happen, which is not the concern of how things are configured.)

Donal Fellows
  • 133,037
  • 18
  • 149
  • 215