0

Here is a simplified use case of my Makefile. What I want to do is use the names of these files and create the respective .d and .hpp files. However, for some reason, I am unable to do it.

XML_DATA_FILES := a.xml b.xml c.xml d.xml

build:
    @for var in $(XML_DATA_FILES); do \
        echo "$(basename $$var)";\
        $(eval var1 = $(basename $$var)) \
        echo "$(join var1,.hpp)"; \
        echo "$(join var1,.d)"; \
    done

The output that I get when I run make is as follows

a.xml
var1.hpp
var1.d
b.xml
var1.hpp
var1.d
c.xml
var1.hpp
var1.d
d.xml
var1.hpp
var1.d

But what I want is a.d, a.hpp and so on for all the four xml files input. I have already referred to this question and GNU Manual but it hasnt helped so far.How can I achieve this?

Community
  • 1
  • 1
Recker
  • 1,915
  • 25
  • 55

1 Answers1

1

There're a number of problems here :). But, fundamentally you cannot combine make functions like basename and eval inside a shell loop like for and expect it to do anything useful. Make always expands the entire recipe for all make variables and function FIRST, then it passes the entire expanded string to the shell to run, then it waits for the shell to finish.

Consider: how would the shell, running its for loop, communicate the current value of var back up to make each time through the shell's loop so that make could run the proper functions etc.? It's just not possible.

You need to write your entire loop using only shell constructs, plus simple make variables that have the same value throughout the recipe.

However, this is useless as a makefile since you just have one target that does everything. Why not just write a shell script? There's no point to using make for this. If you want to write it the make way, you'll need to declare the targets and prerequisites and create pattern rules, like this:

XML_DATA_FILES := a.xml b.xml c.xml d.xml

OUTPUTS := $(foreach X,$(XML_DATA_FILES:.xml=),$X.d $X.hpp)

build: $(OUTPUTS)

%.d %.hpp: %.xml
        echo "$*.d $*.hpp"

Of course since you don't say exactly what the real commands do I can't be sure this is correct; if you actually have two different commands, one that builds the .d file and one that builds the .hpp file, you should create two different pattern rules.

MadScientist
  • 92,819
  • 9
  • 109
  • 136
  • I ended up writing a Perl script and just provided the paths to xml files...and Perl took care of everything else.... :) :) :) – Recker Dec 08 '15 at 17:47