plaes
plaes

Reputation: 32716

Copy list of files to specific paths with Makefile

Scratching my head for a while now, but I would like to copy an arbitrary list of files with paths to under specified path in system.

File layout:

data/a/file1.ext1
data/b/randomfile.ext2
data/c/file3.ext3
data/c/subdir/randomfile.2

Running make -f Makefile deploy DESTDIR=/path/to/somewhere copies those files to:

$(DESTDIR)/a/file1.ext1
$(DESTDIR)/b/randomfile.ext2
$(DESTDIR)/c/file3.ext3
$(DESTDIR)/c/subdir/randomfile.2

Makefile:

$FILES = \
    a/file1.ext1 \
    b/randomfile.ext2 \
    c/file3.ext3 \
    c/subdir/randomfile.2

ifneq ($(filter env_check,$(MAKECMDGOALS)),$())
  ifndef DESTDIR
    $(error DESTDIR not defined)
  endif
endif

# lots of currently broken rules :(
# check whether target directory has certain structure
# check whether all the files listed in $(FILES) are in repository

Upvotes: 1

Views: 4156

Answers (1)

blackghost
blackghost

Reputation: 1825

Are you looking for something like this?

FILES := ...
DST_FILES := $(addprefix $(DESTDIR)/,$(FILES))

ifneq ($(filter env_check,$(MAKECMDGOALS)),$())
  ifndef DESTDIR
    $(error DESTDIR not defined)
  endif
endif

all: $(DST_FILES)

$(DST_FILES) : ${DESTDIR}/% : %
    @echo "$< ==> $@"
    @[[ -e $< ]] || (echo "some error for $<" && false)
    @mkdir -p $(dir $@)
    @cp  $< $@

[Edit]: Although the version somewhat worked, I still needed to do following adjustments:

  • Files in source repository are stored under data directory - fixed by using $addprefix call
  • When file in $(DESTDIR) already existed, it was never copied - used the .FORCE target. (Another option would be --always-make commandline option).

Eventually, the working Makefile looks like that:

# File are stored under data/
FILES= \
    foo/file1.ext \
    bar/file2.txe \
    bar/dir/file3.txt

ifneq ($(filter env_check,$(MAKECMDGOALS)),$())
  ifndef DESTDIR
    $(error DESTDIR not defined)
  endif
endif

.PHONY: deploy help

help:
    @echo "Deploy stuff"

# Check whether certain directories in the output are present
env_check:
    @test -d $(DESTDIR)/WEB-INF -a -d $(DESTDIR)/META-INF || \
        ( echo "DESTDIR: \"$(DESTDIR)\" is not proper deployment path" && exit 1 )

DST_FILES := $(addprefix $(DESTDIR)/, $(FILES))

# We need to add our path prefix to local files and FORCE to always do the copying
$(DST_FILES) : $(addprefix $(DESTDIR), %) : $(addprefix data,%) .FORCE
    @cp -pv $< $@

.FORCE:

deploy: env_check $(DST_FILES)
    @echo "Deployment done..."

Upvotes: 1

Related Questions