cmakefilegnu-makeupx

Extending a GNU make implicit rule


I'm using the GNU make implicit rule for linking together my binary as follows:

foo : foo.o bar.o

However, I also want to be able to compile the resulting binary foo with UPX (by invoking upx ultra-brute foo) on it afterwards. I can, of course, do something like this:

foo : foo.o bar.o
    $(CC) $(LDFLAGS) $^ $(LDLIBS) -o $@
    upx --ultra-brute $@

However, this is basically forcing me to repeat the implicit rule, while all I really want to do is just also call upx ultra-brute after what it already does anyway. Is there a way to get what I want without basically having to manually write the implicit rule into the recipe anyway?


Solution

  • That invocation of upx modifies the file in-place, which means that if it fails or is interrupted, make won't know to run it again. In general you should avoid writing Makefile rules like that.

    If we split it up then it becomes easy:

    foo.fat: foo.o bar.o
    foo: foo.fat
        upx --ultra-brute -o $@ $<