1


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 Answers 2

0

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.

1
  • 1
    But ...are you sure this is working (first part)? As far as I can see I first get the error that the include file isn't found (which is right because the include file has to be created). Then the target which has as prerequisite the SRC_JS variable is build. This fails because the SRC_JS variable has not been set. Then the include file is created. At this point I would expect the make file system to include the created include file and to recreate the target with the SRC_JS prerequisite. Unfortunately it's not happening that way. Something wrong? Jan 11, 2013 at 9:55
0
include $(shell read_dirs.py -p $(PRODUCT) myxml.xml|sed s,$$,/config.mk,)
myjscode.js: myxml.xml
    js-compiler ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.