Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 358
krisharmas Avatar asked Feb 21 '14 07:02

krisharmas


1 Answers

I came across your question trying to do a similar thing but not with make.

I'm not a make person, but here is the logic you'd want to:

  • Check if patch has been fully applied
    • Skip it if it is already applied
    • Apply it otherwise

-

# If we could reverse the patch, then it has already been applied; skip it
if patch --dry-run --reverse --force < patchfiles/foo.patch >/dev/null 2>&1; then
  echo "Patch already applied - skipping."
else # patch not yet applied
  echo "Patching..."
  patch -Ns < patchfiles/foo.patch || echo "Patch failed" >&2 && exit 1
fi
like image 94
Tom Hale Avatar answered Nov 14 '22 22:11

Tom Hale