如果补丁跳过补丁,如何不破坏makefile?

时间:2014-02-21 07:46:57

标签: linux makefile patch

我想在makefile中应用补丁。如果尚未应用补丁,则此工作正常。但是,如果我尝试在原始文件已经修补之后进行修改,则会导致makefile在完成之前退出。

Makefile -

all:
        echo "starting patch"
        patch -N < patchfiles/foo.patch
        echo "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

我认为使用-N选项只会跳过补丁而不会导致任何错误。显然我错了。有关如何修复makefile的任何想法,以便它不会将跳过的补丁解释为错误吗?

谢谢!

编辑:

如果我想在补丁之前进入目录,那么使用-patch不起作用。这是结果:

生成文件:

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

结果:

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

2 个答案:

答案 0 :(得分:3)

我遇到了一个问题,试图做类似的事情而不是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

答案 1 :(得分:1)

在命令causes Make to ignore its exit status前添加减号。

-patch -N < patchfiles/foo.patch

但你必须考虑到这一点;如果命令也可能因为你想要检测的原因而失败怎么办?也许在实际的补丁命令之前或之后添加一些健全性检查。