I am creating a makefile that uses the condition if and ifneq. I noticed that if I am using if, the next lines should be indented by spaces.
if [-d "$$d" ]; then <space><space><space> echo "file found"; fi;
But if I am using the ifneq command, the next lines had to be indented by tabs.
ifneq ($(strip $(USE_FILE)),NO) <tab>echo "file not to be used" endif
Space and tabs should not matter at all. But how come in makefile, there is a difference with space and tab?
In order for make to tell the difference between a recipe and things that are not a recipe, it uses TAB characters.
Tabs need less characters and are formatted by users (some display as 2, some as 4 spaces), what can make some code look bad on some other environment. Spaces consume more KBs but look equal everywhere.
Tabs allow you to switch between options in a program, separate documents, or web pages. The currently-selected tab is usually underlined or highlighted in a different color than the other tabs.
Answer. In most code editors, tabs are not the same as 2 spaces or 4 spaces by default. A tab is stored differently than spaces in the code. Tabs can be seen as a big “jump” in the text, while spaces are always 1 space each.
You have to understand that a makefile is really written in two completely different "languages", in one file.
Recipes (the commands that run compilers, echo, etc.) are written in shell script syntax.
The rest of the makefile that is not in a recipe is written in makefile syntax.
In order for make to tell the difference between a recipe and things that are not a recipe, it uses TAB characters. So, lines that begin with TAB are assumed to be part of a recipe (so they are shell scripts and passed to the shell for parsing), and lines that do not begin with TAB cannot be part of a recipe (so they cannot be shell scripts: they must be make syntax).
In your examples, if [ -d ...
is shell syntax. If it appears in a makefile it must be part of a recipe, and so must be preceded by a TAB; if make tries to interpret this as makefile syntax it will be an error. ifneq
is makefile syntax: if the shell tries to interpret this as a shell script it will be a syntax error, so it cannot be part of a recipe and must NOT be preceded by a TAB.
All other uses of indentation are optional and irrelevant (for example in your first example above you say "the next line should be indented by spaces"; that's just a convention and the script will work exactly the same way whether or not you indent it at all).
Now, there are some details which get tricky: backslash-escaped newlines, rule contexts, etc. but if you stick with the rule that all recipe lines are indented with a TAB and no non-recipe lines are indented with a TAB, you'll be OK.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With