manpagez: man pages & more
info make
Home | html | info | man

File: make.info,  Node: Parsing Makefiles,  Next: Secondary Expansion,  Prev: Makefiles.php">Reading Makefiles,  Up: Makefiles

3.8 How Makefiles Are Parsed
============================

GNU 'make' parses makefiles line-by-line.  Parsing proceeds using the
following steps:

  1. Read in a full logical line, including backslash-escaped lines
     (*note Splitting Long Lines: Splitting Lines.).

  2. Remove comments (*note What Makefiles Contain: Makefile Contents.).

  3. If the line begins with the recipe prefix character and we are in a
     rule context, add the line to the current recipe and read the next
     line (*note Recipe Syntax::).

  4. Expand elements of the line which appear in an _immediate_
     expansion context (*note How 'make' Reads a Makefile: Reading
     Makefiles.).

  5. Scan the line for a separator character, such as ':' or '=', to
     determine whether the line is a macro assignment or a rule (*note
     Recipe Syntax::).

  6. Internalize the resulting operation and read the next line.

   An important consequence of this is that a macro can expand to an
entire rule, _if it is one line long_.  This will work:

     myrule = target : ; echo built

     $(myrule)

   However, this will not work because 'make' does not re-split lines
after it has expanded them:

     define myrule
     target:
             echo built
     endef

     $(myrule)

   The above makefile results in the definition of a target 'target'
with prerequisites 'echo' and 'built', as if the makefile contained
'target: echo built', rather than a rule with a recipe.  Newlines still
present in a line after expansion is complete are ignored as normal
whitespace.

   In order to properly expand a multi-line macro you must use the
'eval' function: this causes the 'make' parser to be run on the results
of the expanded macro (*note Eval Function::).

© manpagez.com 2000-2024
Individual documents may contain additional copyright information.