Search code examples
makefilegnu-make

How to instruct Make to copy the i-th file of a list to the i-th path of another list?


I have two lists, both with N elements:

SRC = a/b/foo  a/b/bar  a/b/moo
DST = a/c/fii  a/c/bir  a/c/mee

Is there a way to set a rule that takes the i-th element of SRC as the prerequisite of the i-th element of DST?

I just need to copy, so what I am thinking of is something like

the_i-th_element_of_DST: the_i-th_element_of_SRC
    cp $< $@

As usual, I would like it not to copy if the destination is updated already.


Solution

  • No, there's nothing like that built-in. You will have to do some magic.

    Here's an example that might work:

    # Create an explicit rule for each destination file
    $(DST):
            cp $< $@
    
    # Now create a prerequisite relationship between each DST and SRC
    # using a recursive user-defined macro
    
    MAKE_PREREQ = $(if $1,$(eval $(firstword $1): $(firstword $2))\
        $(call MAKE_PREREQ,$(wordlist 2,$(words $1),$1),$(wordlist 2,$(words $2),$2)))
    
    $(call MAKE_PREREQ,$(DST),$(SRC))
    

    (note I didn't test this)

    I believe this will fail if any of the filenames in SRC or DST contain commas.