Skip to content
Advertisement

Makefiles with source / object files in different directories

I have a project with the following directory structure:

JavaScript

A file in the $doc/tests directory e.g test1.rst is created from $tests/test1/test1.e. I’m having problems with the makefile specifying that the source files are in $tests/*/*.e and the destination files are in $doc/tests/*.rst.

I’ve seen several similar questions but haven’t been able to workout the correct makefile syntax.

This makefile works for a single file example

JavaScript

I’m having trouble when using the full list of objects i.e.

JavaScript

test1.rst is generated 3 times and test2,3.rst are ignored. The $(SOURCES) and $(OBJECTS) are correct. I suspect that $< does not iterate over the (SOURCES)

Here is some of the output from make -d

JavaScript

Advertisement

Answer

(This question looks very familiar– I’d almost swear that one essentially the same has been asked and answered.)

Let’s take this in stages. We could write the rules one at a time:

JavaScript

but that’s tedious. It’s the kind of situation that cries out for a wildcard solution, such as a pattern rule, but one of Make’s serious shortcomings is its crude handling of wildcards. A pattern rule in which the wildcard is repeated:

JavaScript

is not allowed. But we could write the rules using eval:

JavaScript

Then instead of writing all of those eval statements, we can delegate that job to foreach:

JavaScript

Then instead of writing that list of tests, we can delegate that to wildcard, and use the same list to construct a list of target files:

JavaScript

Putting all of these together is straightforward, but this answer is getting long.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement