Making zip archive relative to given path in non-recursive makefile

137 views Asked by At

I'm using an implementation of non-recursive make similar to the one covered here: http://evbergen.home.xs4all.nl/nonrecursive-make.html

Here's an example of the problem.

The main Makefile includes foo/Rules.mk. foo/Rules.mk contains the snippet:

# Here, d is bound to foo, the path to the current directory
$(d)/foo.zip: $(d)/bar
    zip -r $@ $^
    # This expands to the recipe: zip -r foo/foo.zip foo/bar

Unfortunately, this creates a zip archive containing foo/bar, but I need it to contain bar, that is, to make the archive relative to a given directory. cd does not work.

# DOES NOT WORK
$(d)/foo.zip: d := $(d)  # this makes the variable d work in the recipe
$(d)/foo.zip: $(d)/bar
    cd $(d); zip -r $@ $^
    # This expands to the recipe: cd foo; zip -r foo/foo.zip foo/bar

How can I make this work in the general case (d could be any path, the zip contains an arbitrary selection of files and subdirectories)?

2

There are 2 answers

0
darkfeline On

I came up with the following hack, may the programming gods forgive me.

x := $(d)/foo.zip  # targets
y := $(d)/bar  # prerequisites
$(x): x := $(x)
$(x): y := $(y)
$(x): d := $(d)
$(x): $(y)
    cd $(d); zip -r $(x:$(d)/%=%) $(y:$(d)/%=%)
    # Expands to cd foo; zip -r foo.zip bar
3
Chnossos On

Simply this ?

$(d)/foo.zip: $(d)/bar
    zip -r $(@:$(d)/%=%) $(<:$(d)/%=%) # Expands to zip -r foo.zip bar