Luiz E.
Luiz E.

Reputation: 7279

makefile not running commands and "eating" first letter of variable

I have a function like this:

build:
    git_branch="$(`git rev-parse --abbrev-ref HEAD`)"
    ifeq("$git_branch", 'development')
        tag="development"
    else("$git_branch", 'staging')
        tag="staging"
    else("$git_branch", 'master')
        tag="production"
    endif
    echo "tag is $(tag)"

when I run make build this is the output

git_branch=""
ifeq("it_branch", 'development')
/bin/sh: -c: line 0: syntax error near unexpected token `"it_branch",'
/bin/sh: -c: line 0: `ifeq("it_branch", 'development')'
make: *** [build] Error 2

so first, the git branch is empty, when it shouldn't. if I run this command on console, I get the branch name correctly, second, whats up with the variable being evaluated as it_branch instead git_branch?

Well, I'm kinda new to makefiles, I went through the docs and couldn't catch anything I'm possibly doing wrong

Edit

I changed to this:

git_branch:= $(shell git rev-parse --abbrev-ref HEAD)
ifeq ($(git_branch), "development")
    tag:= "development"
else($(git_branch), "staging")
    tag:= "staging"
else($(git_branch), "master")
    tag:= "production"
endif

build:
    echo "tag is $(tag)"
    echo "branch is $(git_branch)"

the tag is empty, but git_branch is right

Upvotes: 0

Views: 454

Answers (2)

user2371524
user2371524

Reputation:

Something like this should work:

git_branch:= $(shell git rev-parse --abbrev-ref HEAD)
ifeq ($(git_branch), development)
    tag:=development
endif
ifeq ($(git_branch), staging)
    tag:=staging
endif
ifeq ($(git_branch), master)
    tag:=production
endif

build:
    echo "tag is $(tag)"

Short explanation: You're confusing shell variables with make variables. Your logic for the make variables doesn't have to be inside a recipe, in fact, this doesn't make much sense because these things are interpreted by make (while the recipe is executed by a shell make launches).

In make, you have to enclose variable names in parantheses, otherwise it is assumed they only have one character.

Upvotes: 1

clemens
clemens

Reputation: 17722

If a makefile variable contains multiple characters you have to enclose the name with parentheses not quotes. Evaluate shell commands with shell:

git_branch=$(shell git rev-parse --abbrev-ref HEAD)
ifeq($(git_branch), 'development')

Upvotes: 1

Related Questions