When to use space or tab in Makefile?

后端 未结 1 1438
耶瑟儿~
耶瑟儿~ 2021-02-01 19:18

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\" ]; t         


        
相关标签:
1条回答
  • 2021-02-01 19:51

    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.

    0 讨论(0)
提交回复
热议问题