1

Locally I'm usin GNU Make 3.8 on MacOS (i386-apple-darwin11.3.0)

I'm trying to generate a bunch of test files based on the files in a directory.

Right now I'm able to grab the basename of the files, but I cant figure out how to store them in a variable so I can generate another file with the basename

TEST_FILES = lib/*.js

build-test:
    @mkdir -p tests
    -@for file in $(TEST_FILES); do \
        echo $$file; \
        MYNAMES = $(basename $$file .js) \
        echo $MYNAMES; \
    done

I want to then create a bunch of files called $MYNAME.log using output from a STDOUT stream.

I'm stuck on getting the names into the variable, the solutions I've found so far are throwing errors for me when I try to implement them

Community
  • 1
  • 1
qodeninja
  • 10,946
  • 30
  • 98
  • 152
  • It's not clear what you're trying to do, how you're trying to do it or whether you're using a good approach. Suppose you have `lib/foo.js` and `lib/bar.js`, what then? – Beta Oct 08 '13 at 01:56
  • @beta should make a file called test/foo.log test/bar.log – qodeninja Oct 08 '13 at 16:30

1 Answers1

4

This line here

echo $MYNAMES; \

will substitute in the value of the make variable M, as if you wrote $(M)YNAMES. You want

echo $$MYNAMES; \

instead.

This line

   MYNAMES = $(basename $$file .js) \

cannot work - you are using a make command basename which is executed only once before the subshell is invoked to execute the rule, whereas file is a shell variable which changes each iteration of the loop. You probably meant $$(basename $$file) instead, which will be passed to the shell as "$(basename $file)". The shell will insert the results of running the basename command.

Make sure that you do want to store them in a shell variable, and not as a make variable. It's essential to understand the difference.

Gavin Smith
  • 3,076
  • 1
  • 19
  • 25
  • 1
    In short: You need to escape every $ in a Makefile that is meant to be used in the shell script. – Coroos Oct 08 '13 at 08:00
  • @Gavin_Smith I tried that method too -- MYNAMES = $$(basename $$file); \ echo $MYNAMES; \ -- then I get "/bin/bash: MYNAMES: command not found" – qodeninja Oct 08 '13 at 16:51
  • There should be no space between the variable name and the `=` sign (this is different syntax from that for assigning make variables). – Gavin Smith Oct 08 '13 at 17:21
  • @GavinSmith ok now I'm getting no errors but how do I use the variable now? like how do I echo the contents of it, currently it shows empty – qodeninja Oct 08 '13 at 19:22
  • Reread my answer and previous comments - if you've quoted what you're doing accurately then the answer has already been given. – Gavin Smith Oct 08 '13 at 20:36
  • 1
    Another mistake you might be making is executing multiple commands. Each line (not ending with \\) in a make recipe is run using a separate shell, and environment variables are not shared therebetween. To avoid this, use \ at the end of lines to pass it all to one shell sub-process. – Gavin Smith Oct 09 '13 at 16:10