3

In the below code:

GREEN := "\e[1;33m]"
NCOLOR := "\e[0m"

INFO := @bash -c '\
  printf $(GREEN); \
  echo "=> $$1"; \
  printf $(NCOLOR)' VALUE

ifeq (tag, $(firstword $(MAKECMDGOALS)))

    ifneq (2, $(words $(MAKECMDGOALS)))
        $(error Wrong number of tag arguments)
    endif

    LIST_ARGS := $(wordlist 2,$(words $(MAKECMDGOALS)), $(MAKECMDGOALS))

    VERSION := $(word 1, $(LIST_ARGS))
    COMMIT_HASH := $(word 2, $(LIST_ARGS))

    ifeq ($(LIST_ARGS),)
        $(error You must specify a tag)
    endif

    $(eval $(LIST_ARGS):;@:)
endif

Indentation is done using tabs

Error: Makefile:12: *** commands commence before first target. Stop. occurs after running make tag

If I remove Line 11, 12 and 13 it works.

Line 24 is space indented, rest all is TAB indented


How to resolve this indentation error?

overexchange
  • 15,768
  • 30
  • 152
  • 347
  • I cannot reproduce this behavior by putting this in a makefile and running `make`. Maybe some particular make command line is needed? Please provide more details. – MadScientist Jan 30 '20 at 22:49
  • Is that the simplest version of the makefile that produces the error? And are there any tabs in it? – Beta Jan 30 '20 at 23:17
  • @Beta Complete code is using tabs for indentation. No this is not the simplest version of Makefile. I just pasted the problem code – overexchange Jan 30 '20 at 23:33
  • @MadScientist Now you should be able to reproduce – overexchange Jan 30 '20 at 23:42
  • 2
    Generally this happens when you have a tab-indented line which is not a make statement (assignment, condition, function etc.) before any target, so make does not know what to relate it to. Please double check your whole Makefile, as the part you posted does not reproduce the issue. – raspy Jan 30 '20 at 23:55
  • @raspy does reproduce, for the part of code posted – overexchange Jan 31 '20 at 00:04
  • @raspy Line 24 is space indented and if I remove Line 11, 12, 13 it works – overexchange Jan 31 '20 at 00:08
  • As mentioned above and in e.g. the first answer to [this question](https://stackoverflow.com/questions/4713663/gnu-make-yields-commands-commence-before-first-target-error), `make` considers any line beginning with a tab to be part of a recipe. You must use spaces to indent your `Makefile`. – Virgile Jan 31 '20 at 07:42
  • Does this answer your question? [GNU make yields "commands commence before first target" error](https://stackoverflow.com/questions/4713663/gnu-make-yields-commands-commence-before-first-target-error) – Virgile Jan 31 '20 at 07:43
  • @Virgile do I need space indent every line? If yes, how many spaces? – overexchange Jan 31 '20 at 09:08
  • @overexchange As long as you don't use tab, `make` is not concerned about spaces. It's thus a matter of readability for you (and anyone who will have to maintain this `Makefile`). You might get some help from your favorite editor/IDE if it knows about `Makefile` syntax. – Virgile Jan 31 '20 at 10:04
  • @Virgile what does "part of a recipe" mean? Am not good in English – overexchange Jan 31 '20 at 10:06
  • It's the list of commands for a given target (at least in GNU make). See https://www.gnu.org/software/make/manual/html_node/Rule-Syntax.html#Rule-Syntax for more information – Virgile Jan 31 '20 at 12:19
  • You forgot to say in your question that you ran it with `make tag`. If I run just `make`, I get no errors (well, I get an error `No targets` since no targets are defined). – MadScientist Jan 31 '20 at 13:30
  • @MadScientist Yes, I ran `make tag` and then I get above error. – overexchange Jan 31 '20 at 17:33
  • @Virgile It is working now, after spacing Line 12 and rest all the lines with TAB – overexchange Jan 31 '20 at 17:46

0 Answers0