Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is conditional statement in Makefile valid syntax

I have the following Makefile

~/w/i/craft-api git:develop ❯❯❯ cat Makefile                                                                                     ⏎ ✱ ◼
test:
    echo "TODO: write tests"
generate-toc:
    if ! [ -x "$(command -v doctoc)" ]; then
        echo "Missing doctoc. Run 'npm install doctoc -g' first"
    else
        doctoc ./README.md
    fi

I'm encountering this error

~/w/i/craft-api git:develop ❯❯❯ make generate-toc                                                                                  ✱ ◼
if ! [ -x "" ]; then
/bin/sh: -c: line 1: syntax error: unexpected end of file
make: *** [generate-toc] Error 2

What is incorrect in my Makefile syntax / usage?

edit 1

Adding line-continuing backslashes doesn't appear to fix the issue:

~/w/i/craft-api git:develop ❯❯❯ cat Makefile                                                                                     ⏎ ✱ ◼
test:
    echo "TODO: write tests"
generate-toc:
    if ! [ -x "$(command -v doctoc)" ]; then \
      echo "Missing doctoc. Run 'npm install doctoc -g' first" \
    else \
        doctoc ./README.md \
    fi
~/w/i/craft-api git:develop ❯❯❯ make generate-toc                                                                                  ✱ ◼
if ! [ -x "" ]; then \
      echo "Missing doctoc. Run 'npm install doctoc -g' first" \
    else \
        doctoc ./README.md \
    fi
/bin/sh: -c: line 1: syntax error: unexpected end of file
make: *** [generate-toc] Error 2
like image 865
Casey Flynn Avatar asked Mar 11 '16 03:03

Casey Flynn


People also ask

Can we use Ifdef in makefile?

The lines of the makefile following the ifneq are obeyed if the two arguments do not match; otherwise they are ignored. The ifdef directive begins the conditional, and specifies the condition. It contains single argument. If the given argument is true then condition becomes true.

What is := in makefile?

= defines a recursively-expanded variable. := defines a simply-expanded variable.

What is eval in makefile?

The eval function is very special: it allows you to define new makefile constructs that are not constant; which are the result of evaluating other variables and functions. The argument to the eval function is expanded, then the results of that expansion are parsed as makefile syntax.


1 Answers

Every line is treated as a separate command and is passed to a different shell instance. You can use \ continuations to combine all of the lines so make knows to pass them as one long string to a single shell. This removes the newlines so you also need to add ; at the end of each command.

if ! [ -x "$$(command -v doctoc)" ]; then \
    echo "Missing doctoc. Run 'npm install doctoc -g' first"; \
else \
    doctoc ./README.md; \
fi

You'll also want to escape the $, otherwise make will interpret it rather than the shell.

like image 167
John Kugelman Avatar answered Oct 13 '22 01:10

John Kugelman