0

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)?

darkfeline
  • 9,404
  • 5
  • 31
  • 32

2 Answers2

0

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
darkfeline
  • 9,404
  • 5
  • 31
  • 32
0

Simply this ?

$(d)/foo.zip: $(d)/bar
    zip -r $(@:$(d)/%=%) $(<:$(d)/%=%) # Expands to zip -r foo.zip bar
Chnossos
  • 9,971
  • 4
  • 28
  • 40
  • Doesn't work for subdirectories, I need a general solution. `$(d)/foo.zip: $(d)/bar/baz`, zip containing `bar/baz`? – darkfeline Jun 23 '15 at 02:18
  • Ok so if I get it right, you just need to remove the `$(d)/` part for each path ? Try my edit then (you got it right on this with your hack). – Chnossos Jun 23 '15 at 06:15
  • I tried that before and it didn't work, and now I realize what the problem is. You need to add `$(d)/foo.zip: d := $(d)` (See the non-recursive make link to see how `d` works). Also replace `<` with `^` for the general case. – darkfeline Jun 23 '15 at 22:17