When to use a space or tab in a makefile?

I am creating a makefile that uses the if and ifneq condition. I noticed that if I use if, the following lines should be indented with spaces.

if [-d "$$d" ]; then <space><space><space> echo "file found"; fi; 

But if I use the ifneq command, the following lines should fall back from the tabs.

 ifneq ($(strip $(USE_FILE)),NO) <tab>echo "file not to be used" endif 

Space and tabs should not matter. But how to do it in a makefile, is there a difference with space and tab?

+6
source share
1 answer

You should understand that a make file is really written in two completely different β€œlanguages” in one file.

Recipes (commands that execute compilers, echoes, etc.) are written in the script shell syntax.

The rest of the makefile that is not in the recipe is written in the syntax of the makefile.

To make it, to tell the difference between a recipe and things that are not a recipe, it uses the TAB characters. So, lines starting with TAB are considered part of the recipe (therefore, they are shell scripts and passed to the shell for parsing), and lines that do not begin with TAB cannot be part of the recipe (therefore, they cannot be shell scripts: they must be syntax).

In your examples, if [ -d ... is the shell syntax. If it appears in the makefile, it must be part of the recipe, and therefore must be preceded by TAB; if make tries to interpret this as makefile syntax, it will be an error. ifneq is the makefile syntax: if the shell tries to interpret it as a shell script, it will be a syntax error, so it cannot be part of the recipe and should NOT precede TAB.

All other indentation applications are optional and irrelevant (for example, in the first example above, you say that the next line should be indented with spaces), that only the convention and script will work exactly the same way or you won't indent at all).

Now there are some details that become complex: backslash-escaped newline characters, rule contexts, etc., but if you adhere to the rule that all recipe lines are indented with TAB, and non-recipe lines indented with TAB, all will be fine.

+8
source

Source: https://habr.com/ru/post/982978/


All Articles