1
votes


I want to conditionally include makefiles depending on the contents of a xml file and product.
My xml file (myxml.xml):

<product1>
  <component1 dirname=dir1 />
  <component2 dirname=dir2 />
</product1>
<product2>
  <component1 dirname=dir1 />
  <component3 dirname=dir3 />
</product2>

In dir1 I have a config.mk file which looks like this:

SRC_JS+=dir1/file1.js
SRC_JS+=dir1/file2.js
SRC_HTML+=dir1/file3.html

Same for dir2 and dir3 which are all subdirs of the makefile dir.

Now I want depending on the specified product include certain config.mk files according to what is in the xml file.
So my makefile:

myjscode.js: myxml.xml
  read_dirs.py -p $PRODUCT $< > mydirs.txt
  while read mydir; do \
    include $$mydir/config.mk ; \
  done < mydirs.txt;
  js-compiler $(addprexis --js ,$SRC_JS) -output $@

Can this be done? FYI: I'm using bash and the read_dirs.py script outputs a 'one-line-per-directory' list.

2

2 Answers

0
votes

First the easy part. The include directive cannot be in a command in a rule. Modify read_dirs.py to produce something like this (when PRODUCT="product2"):

include dir1/config.mk
include dir3/config.mk

Then in your makefile:

mydirs.mk: myxml.xml
    read_dirs.py -p $PRODUCT $< > $@

include mydirs.mk

There is no need for anything else to depend on mydirs.mk; since this makefile includes it, Make will rebuild it if necessary.

Now the hard part. I presume you want the choice of included makefiles to change not just when myxml.xml changes, but also when PRODUCT changes. But PRODUCT is not a file, so we have some choices. We could have a file product.mk that contains the PRODUCT variable. Or instead of mydirs.mk we could have a different file for each PRODUCT. Or we could force the building of mydirs.mk every time. It depends on how you set PRODUCT and what your priorities are.

0
votes
include $(shell read_dirs.py -p $(PRODUCT) myxml.xml|sed s,$$,/config.mk,)
myjscode.js: myxml.xml
    js-compiler ...