make: Multi-Line
1
1 6.8 Defining Multi-Line Variables
1 =================================
1
1 Another way to set the value of a variable is to use the 'define'
1 directive. This directive has an unusual syntax which allows newline
1 characters to be included in the value, which is convenient for defining
11 both canned sequences of commands (⇒Defining Canned Recipes Canned
Recipes.), and also sections of makefile syntax to use with 'eval'
1 (⇒Eval Function).
1
1 The 'define' directive is followed on the same line by the name of
1 the variable being defined and an (optional) assignment operator, and
1 nothing more. The value to give the variable appears on the following
1 lines. The end of the value is marked by a line containing just the
1 word 'endef'. Aside from this difference in syntax, 'define' works just
1 like any other variable definition. The variable name may contain
1 function and variable references, which are expanded when the directive
1 is read to find the actual variable name to use.
1
1 You may omit the variable assignment operator if you prefer. If
1 omitted, 'make' assumes it to be '=' and creates a recursively-expanded
1 variable (⇒The Two Flavors of Variables Flavors.). When using a
1 '+=' operator, the value is appended to the previous value as with any
1 other append operation: with a single space separating the old and new
1 values.
1
1 You may nest 'define' directives: 'make' will keep track of nested
1 directives and report an error if they are not all properly closed with
1 'endef'. Note that lines beginning with the recipe prefix character are
1 considered part of a recipe, so any 'define' or 'endef' strings
1 appearing on such a line will not be considered 'make' directives.
1
1 define two-lines =
1 echo foo
1 echo $(bar)
1 endef
1
1 The value in an ordinary assignment cannot contain a newline; but the
1 newlines that separate the lines of the value in a 'define' become part
1 of the variable's value (except for the final newline which precedes the
1 'endef' and is not considered part of the value).
1
1 When used in a recipe, the previous example is functionally
1 equivalent to this:
1
1 two-lines = echo foo; echo $(bar)
1
1 since two commands separated by semicolon behave much like two separate
1 shell commands. However, note that using two separate lines means
1 'make' will invoke the shell twice, running an independent sub-shell for
1 each line. ⇒Recipe Execution Execution.
1
1 If you want variable definitions made with 'define' to take
1 precedence over command-line variable definitions, you can use the
1 'override' directive together with 'define':
1
1 override define two-lines =
1 foo
1 $(bar)
1 endef
1
1 ⇒The 'override' Directive Override Directive.
1