Wednesday, January 21, 2009

Writing Makefiles

The information that tells make how to recompile a system comes from reading a data base called the makefile.

  1. Makefile Contents: What makefiles contain.
  2. Makefile Names: How to name your makefile.
  3. Include: How one makefile can use another makefile.
  4. MAKEFILES Variable: The environment can specify extra makefiles.
  5. MAKEFILE_LIST Variable: Discover which makefiles have been read.
  6. Special Variables: Other special variables.
  7. Remaking Makefiles: How makefiles get remade.
  8. Overriding Makefiles: How to override part of one makefile with another makefile.
  9. Reading Makefiles: How makefiles are parsed.
  10. Secondary Expansion: How and when secondary expansion is performed.

1 What makefiles containtents

Makefiles contain five kinds of things:
  • An explicit rule targets, prerequisites, commands
  • An implicit rule It describes how a target may depend on a file with a name similar to the target and gives commands to create or update such a target.
  • A variable definition
  • A directive
    1. Reading another makefile (include)
    2. Deciding (based on the values of variables) whether to use or ignore a part of the makefile
  • '#' starts comments

2 What Name to Give Your Makefile

By default, when make looks for the makefile, it tries the following names, in order: GNUmakefile, makefile and Makefile. Other make programs look for makefile and Makefile, but not GNUmakefile.

If you want to use a nonstandard name for your makefile, you can specify the makefile name with the `-f' or `--file' option. If you use more than one `-f' or `--file' option, you can specify several makefiles. All the makefiles are effectively concatenated in the order specified. The default makefile names GNUmakefile, makefile and Makefile are not checked automatically if you specify `-f' or `--file'.

3 Including Other Makefiles

The directive is a line in the makefile that looks like this:
     include filenames... [comment starting with `#' is allowed at the end of the line. ]
For example, if you have three .mk files, a.mk, b.mk, and c.mk, and $(bar) expands to bish bash, then the following expression
     include foo *.mk $(bar)
is equivalent to
     include foo a.mk b.mk c.mk bish bash
One occasion for using include directives is when several programs, handled by individual makefiles in various directories, need to use a common set of variable definitions (see Setting Variables) or pattern rules (see Defining and Redefining Pattern Rules).
Another such occasion is when you want to generate prerequisites from source files automatically; the prerequisites can be put in a file that is included by the main makefile. This practice is generally cleaner than that of somehow appending the prerequisites to the end of the main makefile as has been traditionally done with other versions of make. See Automatic Prerequisites.
If the specified name does not start with a slash, and the file is not found in the current directory, several other directories are searched. First, any directories you have specified with the `-I' or `--include-dir' option are searched (see Summary of Options). Then the following directories (if they exist) are searched, in this order: prefix/include (normally /usr/local/include 1), /usr/gnu/include, /usr/local/include, /usr/include.
If an included makefile cannot be found in any of these directories, a warning message is generated, but it is not an immediately fatal error; processing of the makefile containing the include continues. Once it has finished reading makefiles, make will try to remake any that are out of date or don't exist. See How Makefiles Are Remade. Only after it has tried to find a way to remake a makefile and failed, will make diagnose the missing makefile as a fatal error.
If you want make to simply ignore a makefile which does not exist and cannot be remade, with no error message, use the -include directive instead of include, like this:
  -include filenames...
This acts like include in every way except that there is no error (not even a warning) if any of the filenames do not exist. For compatibility with some other make implementations, sinclude is another name for -include.

4 The Variable MAKEFILES

If the environment variable MAKEFILES is defined, make considers its value as a list of names (separated by whitespace) of additional makefiles to be read before the others.

5 The Variable MAKEFILE_LIST

As make reads various makefiles, including any obtained from the MAKEFILES variable, the command line, the default files, or from include directives, their names will be automatically appended to the MAKEFILE_LIST variable. They are added right before make begins to parse them.
This means that if the first thing a makefile does is to examine the last word in this variable, it will be the name of the current makefile. Once the current makefile has used include, however, the last word will be the just-included makefile.
If a makefile named Makefile has this content:
     name1 := $(lastword $(MAKEFILE_LIST))



    include inc.mk



    name2 := $(lastword $(MAKEFILE_LIST))



    all:

          @echo name1 = $(name1)

          @echo name2 = $(name2)
then you would expect to see this output:
     name1 = Makefile
     name2 = inc.mk

6 Other Special Variables

DEFAULT_GOAL MAKE_RESTARTS VARIABLES FEATURES INCLUDE_DIRS




No comments: