CodeKingPlusPlus
CodeKingPlusPlus

Reputation: 16081

GNU Makefile macro rules

In GNU Makfiles can there be leading whitespace before a macro definition? and what type of whitespace is allowed (tabs, newlines, space...)

thanks!

Upvotes: 1

Views: 732

Answers (2)

MadScientist
MadScientist

Reputation: 100946

A macro can have any amount of whitespace before the name of the macro; it is ignored.

Whitespace can consist of spaces and TABs, in most cases.

There is one exception: if you are in a "rule context", the first character of the line cannot be a TAB (if it is, then the line is considered to be part of the recipe for the rule rather than a make macro assignment).

The definition of a "rule context" is surprising to most people: note that comments, blank lines, and even ifdef sections which are not taken will not end a "rule context": lines beginning with a TAB after that are still considered part of the previous rule. Only the appearance of a new macro assignment (or a new rule) will end the previous rule.

So for example:

foo: bar
        @cp $< $@

# now we have some comments

ifeq (true,false)
BAR := and an assignment that is not parsed
endif

# and some more comments

             FOO = and a macro starting with TAB + space

(assuming the indentation here begins with TAB not spaces) The last assignment of "FOO" is in a rule context and will be considered part of the recipe for "foo", not a new macro assignment.

All in all, it's better to simply never, ever use a TAB in a makefile unless it is introducing a recipe line.

Upvotes: 5

CodeKingPlusPlus
CodeKingPlusPlus

Reputation: 16081

After a lot of searching:

From GNU make:

"A variable name may be any sequence of characters not containing ‘:’, ‘#’, ‘=’, or leading or trailing whitespace."

Upvotes: 0

Related Questions