Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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 = $(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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading