Search code examples
makefilegnu-make

How can I define a dependency in Make if all files reside in different folders but with similar structure (language dependent)?


I want to use the tool msgfmt in a makefile to generate .mo files from .po sources. The .po sources are per language and reside in a folder structure like this:

$(projectpath)/locale/de/LC_MESSAGES
$(projectpath)/locale/fr/LC_MESSAGES
$(projectpath)/locale/en/LC_MESSAGES

The generated .mo files shall reside in a similar folder structure:

$(buildpath)/locale/de/LC_MESSAGES
$(buildpath)/locale/fr/LC_MESSAGES
$(buildpath)/locale/en/LC_MESSAGES

In the makefile, I have successfully created 2 variables for the sources and the targets:

PO_FILES := $(wildcard $(projectpath)/locale/*/LC_MESSAGES/*.po)
MO_FILES := $(subst $(projectpath)/locale/,$(buildpath)/locale/,$(subst .po,.mo,$(PO_FILES)))

and I am also able to create the target directories for the MO_FILES.

DIRS := $(sort $(dir $(MO_FILES)))

However, I am failing when defining the dependency to trigger the generation.

The following options didn't work:

%.mo:%.po
$(buildpath)/locale/%/LC_MESSAGES/%.mo: $(projectpath)/locale/%/LC_MESSAGES/%.po

I also tried a few other combination with same result. Make claims that it has no rule to make the target.


Solution

  • What about:

    PO_FILES := $(wildcard $(projectpath)/locale/*/LC_MESSAGES/*.po)
    MO_FILES := $(patsubst $(projectpath)/%.po,$(buildpath)/%.mo,$(PO_FILES))
    
    .PHONY: all
    all: $(MO_FILES)
    
    $(MO_FILES): $(buildpath)/%.mo: $(projectpath)/%.po
        mkdir -p $(@D)
        cp $< $@
    

    (with cp instead of msgfmt that I do not have).