krisharmas krisharmas - 3 months ago 8
Linux Question

How to not break the makefile if patch skips the patch?

I want to apply a patch within a makefile. This works fine if the patch has not yet been applied. However, if I try to make after the original file has been patched already it causes the makefile to exit before finishing.

Makefile --

all:
echo "starting patch"
patch -N < patchfiles/foo.patch
echo "patched"


Results after trying to run after file is already patched --

usr-mbp:makefile usr$ make
echo "starting patch"
starting patch
patch -N < patchfiles/foo.patch
patching file foo
Reversed (or previously applied) patch detected! Skipping patch.
1 out of 1 hunk ignored -- saving rejects to file foo.rej
make: *** [all] Error 1


I figured using the -N option would simply skip the patch and not cause any errors. Apparently I was wrong. Any ideas on how to fix the makefile so it doesn't interpret the skipped patch as an error?

Thanks!

EDIT:

If I want to cd into a directory before the patch then using -patch does not work. This is the result:

Makefile:

all:
echo "starting..."
cd tmp && \
-patch -N < ../patchfiles/Makefile.linux-p3041-3_0.patch
echo "finished."


Results:

usr-mbp:makefile usr$ make
echo "starting..."
starting...
cd tmp && \
-patch -N < ../patchfiles/Makefile.linux-p3041-3_0.patch
/bin/sh: line 1: -patch: command not found
make: *** [all] Error 127

Answer

Adding a minus in front of a command causes Make to ignore its exit status.

-patch -N < patchfiles/foo.patch

You really must think this through, though; what if the command could also fail for reasons you do want to detect? Maybe add a couple of sanity checks before or after the actual patch command.