Setting Makefile variable to result of command in rule

Stephen Rasku

I am trying to assign the result of a command to a variable in GNU make. It works if I do it outside the rule:

$ cat stack.mk
GIT_BRANCH=$(shell git rev-parse --abbrev-ref HEAD)

all:
    @echo Git branch is $(GIT_BRANCH)

$ make -f stack.mk all
Git branch is dev

But not if I put it in the rule body:

$ cat stack.mk
all:
    export GIT_BRANCH=$(shell git rev-parse --abbrev-ref HEAD)
    @echo Git branch is $(GIT_BRANCH)

$ make -f stack.mk all
export GIT_BRANCH=dev
Git branch is

Is it possible to assign variables in a rule. At this point I would like to assign the results of a couple of git commands to shell/Makefile variables.

merlin2011

Yes, if you are trying to set a Makefile variable, you can do it using eval function.

all:
    $(eval GIT_BRANCH=$(shell git rev-parse --abbrev-ref HEAD))
    echo Git branch is $(GIT_BRANCH)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Put command result in variable within makefile target

From Dev

Executing Shell command in Makefile rule

From Dev

Executing Shell command in Makefile rule

From Dev

Run a command in GNU and BSD Makefile and store the result into a variable

From Dev

Run a command in GNU and BSD Makefile and store the result into a variable

From Dev

GNU Make - Set MAKEFILE variable from shell command output within a rule/target

From Dev

Rule to set a variable in Makefile not working as expected

From Dev

Use "at variable" ($@) on the right hand side of a rule in makefile

From Dev

Assign result of command in a variable

From Dev

Store the result of a command in a variable

From Dev

Change a make variable, and call another rule, from a recipe in same Makefile?

From Dev

Forcing certain environment variable to be defined when a specific Makefile rule is invoked

From Dev

output awk command result to variable

From Dev

Store Result of SVN Command in Variable

From Dev

awk assign command result to a variable

From Dev

Makefile multi line variable, single command

From Dev

Setting variable to result of Google Visualization Data Aggregation

From Dev

Encountered an issue setting the value of a variable inside a Makefile function

From Dev

Setting and using an environment variable in the same command

From Dev

Setting an environment variable before a command in bash

From Dev

Setting enviroment variable in CircleCI using a command

From Dev

Why is setting a variable before a command legal in bash?

From Dev

Does the command execute automatically when setting it to a variable?

From Dev

bash command not found when setting a variable

From Dev

Setting enviroment variable in CircleCI using a command

From Dev

Error "command not found" when setting value to variable

From Dev

Makefile generic target rule

From Dev

Complex pattern rule in Makefile

From Dev

Makefile generic rule not working

Related Related

  1. 1

    Put command result in variable within makefile target

  2. 2

    Executing Shell command in Makefile rule

  3. 3

    Executing Shell command in Makefile rule

  4. 4

    Run a command in GNU and BSD Makefile and store the result into a variable

  5. 5

    Run a command in GNU and BSD Makefile and store the result into a variable

  6. 6

    GNU Make - Set MAKEFILE variable from shell command output within a rule/target

  7. 7

    Rule to set a variable in Makefile not working as expected

  8. 8

    Use "at variable" ($@) on the right hand side of a rule in makefile

  9. 9

    Assign result of command in a variable

  10. 10

    Store the result of a command in a variable

  11. 11

    Change a make variable, and call another rule, from a recipe in same Makefile?

  12. 12

    Forcing certain environment variable to be defined when a specific Makefile rule is invoked

  13. 13

    output awk command result to variable

  14. 14

    Store Result of SVN Command in Variable

  15. 15

    awk assign command result to a variable

  16. 16

    Makefile multi line variable, single command

  17. 17

    Setting variable to result of Google Visualization Data Aggregation

  18. 18

    Encountered an issue setting the value of a variable inside a Makefile function

  19. 19

    Setting and using an environment variable in the same command

  20. 20

    Setting an environment variable before a command in bash

  21. 21

    Setting enviroment variable in CircleCI using a command

  22. 22

    Why is setting a variable before a command legal in bash?

  23. 23

    Does the command execute automatically when setting it to a variable?

  24. 24

    bash command not found when setting a variable

  25. 25

    Setting enviroment variable in CircleCI using a command

  26. 26

    Error "command not found" when setting value to variable

  27. 27

    Makefile generic target rule

  28. 28

    Complex pattern rule in Makefile

  29. 29

    Makefile generic rule not working

HotTag

Archive