Question

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
Was it helpful?

Solution 2

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.

OTHER TIPS

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top