Skip to content
Advertisement

makefile substitute target pattern twice in its dependency

I have the following project architecture

/
├── makefile
└── doc/
    ├── makefile
    └── uml/
        ├── uml1/
        │   ├── uml1.plantuml
        |   └── res/
        |       ├── ressource1
        |       └── ressource2
        └── uml2/
            ├── uml2.plantuml
            └── res/
                ├── ressource1
                └── ressource2

My aim is to generate a png image for each directory present into doc/uml into build/doc/uml

The parent makefile is the following one

BUILD_PATH?=./build

doc:
    ${MAKE} -C $@ $@ ADOC_OUTPUT_DIR=$(abspath ${BUILD_PATH}/doc)

clean:
    rm -rf build

.PHONY: doc clean

And the submakefile is the following one

ADOC_OUTPUT_DIR ?= build
UML_OUTPUT_DIR = ${ADOC_OUTPUT_DIR}/uml

UML = $(notdir $(wildcard uml/*))
UML_TARGETS = $(foreach uml, ${UML}, ${UML_OUTPUT_DIR}/${uml}.png)

doc: uml

uml: ${UML_OUTPUT_DIR} ${UML_TARGETS}

${UML_TARGETS}: ${UML_OUTPUT_DIR}/%.png : uml/%/%.plantuml
    plantuml $< -o $(@D)

###################################
##### Directory creation part #####
###################################

${UML_OUTPUT_DIR}:
    mkdir -p $@

.PHONY: doc uml ${UML_TARGETS} ${UML_OUTPUT_DIR}

However it’s failing when I’m launching it with make doc

$ make doc
make -C doc doc ADOC_OUTPUT_DIR=/home/julien/test_uml/build/doc
make[1] : Entering directory « /home/julien/test_uml/doc »
make[1]: ***  No rule to make target « uml/gui_state_diagram/%.plantuml », needed by « /home/julien/test_uml/build/doc/uml/gui_state_diagram.png ». Stop.
make[1] : Exiting directory « /home/julien/test_uml/doc »
makefile:12 : the recipe for target « doc » failed
make: *** [doc] Erreur 2

It seems that the problem comes from the fact that ‘%’ can’t be replaced twice on the dependency.

I can’t find a way to achieve my goal without modifying the project architecture (I would like to keep it as close as it is now)

Is there a way to replace the pattern ‘%’ twice ?

Is there a better way to achieve my goal than trying to replace the uml name twice in the target’s dependency ?

Advertisement

Answer

You can also use secondary expansion:

.SECONDEXPANSION:
${UML_TARGETS}: ${UML_OUTPUT_DIR}/%.png : uml/$$*/$$*.plantuml
        plantuml $< -o $(@D)
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement