How are Makefile variables calculated?

TobyG

I'm new to using Makefiles & am puzzled by something…

I have the following in a Makefile…

USER_ID := $(id -u)
USER_ID_ESC := $$(id -u)

MAKEFILE := $(realpath $(lastword $(MAKEFILE_LIST)))
ROOT_DIR := $(dir $(MAKEFILE))


x:
    echo $${USER_ID}
    echo ${USER_ID}
    echo $(USER_ID)
    echo $${USER_ID_ESC}
    echo ${USER_ID_ESC}
    echo $(USER_ID_ESC)
    echo $${ROOT_DIR}
    echo ${ROOT_DIR}
    echo $(ROOT_DIR)
    exit 0

Which give the output…

echo ${USER_ID}

echo

echo

echo ${USER_ID_ESC}

echo $(id -u)
502
echo $(id -u)
502
echo ${ROOT_DIR}

echo /Users/toby/src/bitbucket.org/limtool/commissioning-tool-api/
/Users/toby/src/bitbucket.org/limtool/commissioning-tool-api/
echo /Users/toby/src/bitbucket.org/limtool/commissioning-tool-api/
/Users/toby/src/bitbucket.org/limtool/commissioning-tool-api/
exit 0

I understand the use of double $ to escape the use of $, but what I don't understand why using a single $ for realpath & dir commands works, but using it for the id command doesn't.

Can someone help me out in understanding?

Thanks

Tim

realpath and dir are GNU make functions, id isn't. If you want to use a shell function inside a Makefile, use $(shell id -u). realpath and dir aren't escaped, they are correctly interpreted by GNU make.

See GNU make file name functions as well as GNU make shell function manual pages.

When you write $(id -u) in a Makefile, make will try to look if it's a function or a variable, and since it's none of these it will not expand to anything, hence the empty echos.

Now the reason why your ID was correctly written when you did escape the dollar sign is mysterious and probably due to your shell. What happened is that make didn't try to expand it since the $ is escaped, so echo $(id -u) was sent to the system. You can try it directly in your shell, you should obtain the same result.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to introduce variables in Makefile

From Dev

How to add new calculated variables to a data frame

From Dev

How to set environment variables in makefile?

From Dev

How to export environment variables in Makefile

From Dev

How to set env variables in Makefile to run tests?

From Dev

How to display the value of all the variables in Makefile

From Dev

How to set multiple variables in target makefile

From Dev

How to use long variables name in Makefile?

From Dev

How to display a message + images based on a calculated chance depending on the variables value

From Dev

openmdao: how is the 'rel' step size calculated for a vector input of design variables?

From Dev

Adding together calculated variables

From Dev

Animation not working with calculated CSS variables

From Dev

Concatenating strings and calculated variables in table

From Dev

How to pass parameters to a makefile recipe, or resolve variables at parse time

From Dev

Makefile- How to define variables in a target that creates other targets?

From Dev

How to take variables from another Makefile in shell script

From Dev

How to have variables of one included makefile available in another makefile included later?

From Dev

Using bash variables in Makefile

From Dev

Understanding Makefile Syntax and Variables

From Dev

Setting environment variables in a makefile

From Dev

Makefile adding spaces to variables

From Dev

Dollars in Makefile environment variables

From Dev

Global Makefile with scoped variables

From Dev

Conditional variables in a makefile

From Dev

Passing Variables in Makefile

From Dev

Global Makefile with scoped variables

From Dev

Conditional variables in a makefile

From Dev

Makefile adding spaces to variables

From Dev

Generic Makefile disregarding variables

Related Related

  1. 1

    How to introduce variables in Makefile

  2. 2

    How to add new calculated variables to a data frame

  3. 3

    How to set environment variables in makefile?

  4. 4

    How to export environment variables in Makefile

  5. 5

    How to set env variables in Makefile to run tests?

  6. 6

    How to display the value of all the variables in Makefile

  7. 7

    How to set multiple variables in target makefile

  8. 8

    How to use long variables name in Makefile?

  9. 9

    How to display a message + images based on a calculated chance depending on the variables value

  10. 10

    openmdao: how is the 'rel' step size calculated for a vector input of design variables?

  11. 11

    Adding together calculated variables

  12. 12

    Animation not working with calculated CSS variables

  13. 13

    Concatenating strings and calculated variables in table

  14. 14

    How to pass parameters to a makefile recipe, or resolve variables at parse time

  15. 15

    Makefile- How to define variables in a target that creates other targets?

  16. 16

    How to take variables from another Makefile in shell script

  17. 17

    How to have variables of one included makefile available in another makefile included later?

  18. 18

    Using bash variables in Makefile

  19. 19

    Understanding Makefile Syntax and Variables

  20. 20

    Setting environment variables in a makefile

  21. 21

    Makefile adding spaces to variables

  22. 22

    Dollars in Makefile environment variables

  23. 23

    Global Makefile with scoped variables

  24. 24

    Conditional variables in a makefile

  25. 25

    Passing Variables in Makefile

  26. 26

    Global Makefile with scoped variables

  27. 27

    Conditional variables in a makefile

  28. 28

    Makefile adding spaces to variables

  29. 29

    Generic Makefile disregarding variables

HotTag

Archive