1

What I want is:

Given an array of names, e.g., dependency1, dependency2, .., dependencyN:

  1. Append "_DEP_DIR" to each name, to form, e.g., dependency1_DEP_DIR, .., dependencyN_DEP_DIR. (XXX_DEP_DIR is predefined as a variable which points to the local disk path of each dependency.)

  2. Invoke a particular batch file(setup.bat) of each dependency.

What I tried is:

DEP_NAMES=dependency1 dependency2 dependency3 dependency4 dependency5 dependency6
DEP_DIRS=$(foreach name,$(DEP_NAMES),$(name)_DEP_DIR)

for dependency in $(DEP_DIRS); do \
    ECHO Copy $$dependency ; \
    ECHO $($$dependency)/installers/windows ; \
    "$($$dependency)/installers/windows/setup.bat" ; \
done

Problem

The first echo can successfully display the appended name, e.g., "dependency1_DEP_DIR". However, $($$dependency) doesn't work as expected, "/installers/windows" is printed out, not to say the following call to the batch file.

Toubleshooting

I guess the problem is that the value of my loop counter($$dependency) happens to be the name of a variable that I need to use($(..)). And the form($($$dependency)) is not right(or even not supported?)

Any one got any idea?

Also, if you guys can come up with other ways to meet my requirements which bypass this issue, happy to know that;)

Eric Z
  • 14,327
  • 7
  • 45
  • 69

1 Answers1

2

I see basically two possibilities: either doing everything inside the Makefile, or exporting the needed variables to the shell and expanding them there. The first case relies on foreach (BTW, the definition of DEP_DIRS could be simpler: DEP_DIRS=$(DEP_NAMES:=_DEP_DIR)), with something like

$(foreach dependency,$(DEP_DIRS),\
      echo "Copy $(dependency)"; \
      echo "dir is $($(dependency))"; \
 )

For the second case, you have to tell make that it must export the relevant variables to the shell (http://www.gnu.org/software/make/manual/html_node/Environment.html):

export dependency1_DEP_DIR=...
export dependency2_DEP_DIR=...
...

Then you can use a for loop, but obtaining the value of the final variable can be a bit more tricky (indirect expansion is not that easy in strictly POSIX env, see e.g. Lookup shell variables by name, indirectly)

for dependency in $(DEP_DIRS); do \
  echo "Copy $$dependency"; \
  echo "dir is `eval echo \\$$$$dependency`"; \
done
Community
  • 1
  • 1
Virgile
  • 9,724
  • 18
  • 42