diff options
author | Klaus Aehlig <aehlig@google.com> | 2018-06-13 05:08:16 -0700 |
---|---|---|
committer | Copybara-Service <copybara-piper@google.com> | 2018-06-13 05:10:15 -0700 |
commit | 348225e4d25b9259489c1ed66eb7eca7612cddcc (patch) | |
tree | f8e6aff632032128fa8d750b4364615c59759d44 | |
parent | 8f5b096c0aeef427c08398e6c3ecf8befec7fded (diff) |
Skylark repositories: support additional arguments for the patch tool
Instead of hard-coding "-p0", allow the arguments for that patch tool
to be overridden. In particular, this supports the use case of patches
generated with `git format-patch` which are to be read as `-p1`.
Improves on #5379.
Closes #4974 as superseded.
Change-Id: I809fde14beab21d8a755ba4f1706b602bae3c1bb
PiperOrigin-RevId: 200373909
-rwxr-xr-x | src/test/shell/bazel/external_patching_test.sh | 56 | ||||
-rw-r--r-- | tools/build_defs/repo/git.bzl | 3 | ||||
-rw-r--r-- | tools/build_defs/repo/http.bzl | 2 | ||||
-rw-r--r-- | tools/build_defs/repo/utils.bzl | 6 |
4 files changed, 66 insertions, 1 deletions
diff --git a/src/test/shell/bazel/external_patching_test.sh b/src/test/shell/bazel/external_patching_test.sh index 820b85963b..b9d6fd7b4f 100755 --- a/src/test/shell/bazel/external_patching_test.sh +++ b/src/test/shell/bazel/external_patching_test.sh @@ -493,4 +493,60 @@ EOF expect_not_log "BAD" } +test_git_format_patch() { + EXTREPODIR=`pwd` + + # Verify that a patch in the style of git-format-patch(1) can be handled. + mkdir main + cd main + ls -al + cat > 0001-foo.sh-remove-dragons.patch <<'EOF' +From a8c0f9248dd85feac9d08b017776b5aedd1e7be8 Mon Sep 17 00:00:00 2001 +From: Klaus Aehlig <aehlig@google.com> +Date: Wed, 13 Jun 2018 11:32:39 +0200 +Subject: [PATCH] foo.sh: remove dragons + +--- + foo.sh | 2 +- + 1 file changed, 1 insertion(+), 1 deletion(-) + +diff --git a/foo.sh b/foo.sh +index 1f4c41e..9d548ff 100644 +--- a/foo.sh ++++ b/foo.sh +@@ -1,3 +1,3 @@ + #!/usr/bin/env sh + +-echo Here be dragons... ++echo New version of foo.sh, no more dangerous animals... +-- +2.18.0.rc1.244.gcf134e6275-goog + +EOF + cat > WORKSPACE <<EOF +load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") +http_archive( + name="ext", + strip_prefix="ext-0.1.2", + urls=["file://${EXTREPODIR}/ext.zip"], + build_file_content="exports_files([\"foo.sh\"])", + patches = ["//:0001-foo.sh-remove-dragons.patch"], + patch_args = ["-p1"], + patch_cmds = ["find . -name '*.sh' -exec sed -i.orig '1s|#!/usr/bin/env sh\$|/bin/sh\$|' {} +"], +) +EOF + cat > BUILD <<'EOF' +genrule( + name = "foo", + outs = ["foo.sh"], + srcs = ["@ext//:foo.sh"], + cmd = "cp $< $@; chmod u+x $@", + executable = True, +) +EOF + bazel build :foo.sh + foopath=`bazel info bazel-genfiles`/foo.sh + grep -q 'New version' $foopath || fail "expected patch to be applied" +} + run_suite "external patching tests" diff --git a/tools/build_defs/repo/git.bzl b/tools/build_defs/repo/git.bzl index 1ea5804d56..a46330542c 100644 --- a/tools/build_defs/repo/git.bzl +++ b/tools/build_defs/repo/git.bzl @@ -102,6 +102,7 @@ _common_attrs = { "strip_prefix": attr.string(default = ""), "patches": attr.label_list(default = []), "patch_tool": attr.string(default = "patch"), + "patch_args": attr.string_list(default = ["-p0"]), "patch_cmds": attr.string_list(default = []), } @@ -161,6 +162,7 @@ Args: patches: A list of files that are to be applied as patches after extracting the archive. patch_tool: the patch(1) utility to use. + patch_args: arguments given to the patch tool, defaults to ["-p0"] patch_cmds: sequence of commands to be applied after patches are applied. """ @@ -196,5 +198,6 @@ Args: patches: A list of files that are to be applied as patches after extracting the archive. patch_tool: the patch(1) utility to use. + patch_args: arguments given to the patch tool, defaults to ["-p0"] patch_cmds: sequence of commands to be applied after patches are applied. """ diff --git a/tools/build_defs/repo/http.bzl b/tools/build_defs/repo/http.bzl index 02879f4b7e..fffe226d7f 100644 --- a/tools/build_defs/repo/http.bzl +++ b/tools/build_defs/repo/http.bzl @@ -119,6 +119,7 @@ _http_archive_attrs = { "build_file_content": attr.string(), "patches": attr.label_list(default = []), "patch_tool": attr.string(default = "patch"), + "patch_args": attr.string_list(default = ["-p0"]), "patch_cmds": attr.string_list(default = []), "workspace_file": attr.label(), "workspace_file_content": attr.string(), @@ -239,6 +240,7 @@ Args: patches: A list of files that are to be applied as patches after extracting the archive. patch_tool: the patch(1) utility to use. + patch_args: arguments given to the patch tool, defaults to ["-p0"] patch_cmds: sequence of commands to be applied after patches are applied. """ diff --git a/tools/build_defs/repo/utils.bzl b/tools/build_defs/repo/utils.bzl index b76bac960e..4839b3e73b 100644 --- a/tools/build_defs/repo/utils.bzl +++ b/tools/build_defs/repo/utils.bzl @@ -66,9 +66,13 @@ def patch(ctx): """Implementation of patching an already extracted repository""" bash_exe = ctx.os.environ["BAZEL_SH"] if "BAZEL_SH" in ctx.os.environ else "bash" for patchfile in ctx.attr.patches: - command = "{patchtool} -p0 < {patchfile}".format( + command = "{patchtool} {patch_args} < {patchfile}".format( patchtool = ctx.attr.patch_tool, patchfile = ctx.path(patchfile), + patch_args = " ".join([ + "'%s'" % arg + for arg in ctx.attr.patch_args + ]), ) st = ctx.execute([bash_exe, "-c", command]) if st.return_code: |