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

I want to apply a patch in a makefile. This works great if the patch is not yet applied. However, if I try to do it after the source file has already been fixed, it will force the makefile to exit before completion.

Makefile -

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

The results after trying to run after the file are already fixed -

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 that using the -N option would just skip the patch and not cause any errors. Apparently, I was wrong. Any ideas on how to fix the makefile so that it doesn't interpret the missing patch as an error?

Thank!

EDIT:

If I want to write cd to the 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
+4
source share
2

Make .

-patch -N < patchfiles/foo.patch

; , , ? , .

+1

, , make.

make, , :

  • ,
    • ,

-

# 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
+2

Source: https://habr.com/ru/post/1528047/


All Articles