Peeter Joot
Peeter Joot

Reputation: 8260

how to fix gnu make rule using vpath to copy files into a different directory when that directory is ./?

Based on Beta's answer in gnu make copy many files to a single location, I had a working rule to copy figures from different paths into a ./figures/ subdir in my build directory

ORIG_FILE_DIRS += ..
LOCAL_FILES += figures/myfig.png
DESTDIR := figures

all : $(LOCAL_FILES)

vpath %.png $(ORIG_FILE_DIRS)

$(DESTDIR)/%.png: %.png
  mkdir -p $(DESTDIR)
  cp $< $@

This works nicely provided figures is not ./ -- For example, modifying this to attempt to copy latex .sty files (which I need in the current working directory) from an alternate path, I'd tried:

ORIG_FILE_DIRS += ..
LOCAL_FILES += mycommon.sty
DESTDIR := ./

all : $(LOCAL_FILES)

vpath %.sty $(ORIG_FILE_DIRS)

$(filter %.sty,$(LOCAL_FILES)) : $(DESTDIR)/%.sty: %.sty
  mkdir -p $(DESTDIR)
  cp $< $@

(the filter is an attempt to be explicit since I have a few other .sty files in ./)

This gives me

make: Circular mycommon.sty <- mycommon.sty dependency dropped.
make: Nothing to be done for `all'.

I'm unsure how to resolve this. I'd tried an explicit path for the dependency:

$(filter %.sty,$(LOCAL_FILES)) : $(DESTDIR)/%.sty: ../%.sty
  mkdir -p $(DESTDIR)
  cp $< $@

but still get a circular dependency (for ../mycommon.sty) doing so.

Upvotes: 0

Views: 435

Answers (2)

Beta
Beta

Reputation: 99094

My answer to the other question was kind of a hack, and so is this.

There are several ways to do it, none perfect. Probably the least ugly is to use a temporary directory:

ORIG_FILE_DIRS += ...
DESTDIR := figures
TEMPDIR := tempdir
TEMP_FILES += $(TEMPDIR)/fig1.png

all : $(TEMP_FILES)
        mv $^ $(DESTDIR)

vpath %.png $(ORIG_FILE_DIRS)

$(TEMPDIR)/%.png: %.png
        mkdir -p $(TEMPDIR)
        cp $< $@

Moving files is much faster than copying them (O(1)), so speed won't suffer. Just don't do anything silly, like using tempdir as your destination.

Upvotes: 1

Peeter Joot
Peeter Joot

Reputation: 8260

I can just rename the originals: give them a different suffix, and use a plain old suffix rule.

Upvotes: 1

Related Questions