Skip to content

Commit b998949

Browse files
committed
[patch_repo] allow script to detect if patches have been applied already
In the current implementation, re-running builds fails as git apply fails to apply patches to _deps/* due to the patches already being applied. This patch fixes that by detecting if patches have been applied using git apply --reverse checks. Signed-off-by: Shreeyash Pandey <shrpand@qti.qualcomm.com>
1 parent 3b8cc89 commit b998949

3 files changed

Lines changed: 27 additions & 10 deletions

File tree

qualcomm-software/cmake/patch_repo.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def main():
2525
"--method",
2626
choices=["am", "apply"],
2727
default="apply",
28-
help="Git command to use. git am will add each patch as a commit, whereas git apply will leave patched changes staged.",
28+
help="Git command to use. git am will add each patch as a commit, whereas git apply will leave patched changes staged and skip patches that are already applied.",
2929
)
3030
parser.add_argument(
3131
"--reset",
@@ -114,7 +114,9 @@ def main():
114114
if args.three_way:
115115
apply_check_args.append("--3way")
116116
apply_check_args.append(str(current_patch))
117-
p_check = subprocess.run(apply_check_args)
117+
p_check = subprocess.run(
118+
apply_check_args, capture_output=True, text=True
119+
)
118120

119121
if p_check.returncode == 0:
120122
# Patch will apply.
@@ -129,7 +131,23 @@ def main():
129131
p = subprocess.run(apply_args, check=True)
130132
applied_patches.append(current_patch)
131133
else:
132-
# Patch won't apply.
134+
# Patch does not apply cleanly. Check if it is already applied
135+
# by attempting a reverse check.
136+
reverse_check_args = git_cmd + [
137+
"apply",
138+
"--ignore-whitespace",
139+
"--reverse",
140+
"--check",
141+
str(current_patch),
142+
]
143+
p_rev = subprocess.run(
144+
reverse_check_args, capture_output=True, text=True
145+
)
146+
if p_rev.returncode == 0:
147+
print(f"Skipping {current_patch.name} (already applied).")
148+
continue
149+
150+
# Patch is not already applied and cannot be applied cleanly.
133151
print(f"Unable to apply {current_patch.name}")
134152
if args.restore_on_fail:
135153
# Remove any patches that have already been applied.

qualcomm-software/docs/building.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ Generally, these patches are automatically applied, except for llvm-project whic
5050
patched manually. The below command assumes you are in the `cpullvm-toolchain` directory:
5151

5252
```
53-
python3 qualcomm-software/cmake/patch_repo.py --method apply qualcomm-software/patches/llvm-project
53+
python3 qualcomm-software/cmake/patch_repo.py qualcomm-software/patches/llvm-project
5454
```
5555

5656
Other projects (eld, picolibc, etc.) are checked out and patched automatically. If you prefer, you can check

qualcomm-software/docs/developing.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,12 @@ and libc++/libc++abi/libunwind libraries for the variant.
7575
### Re-running CMake
7676
Unless you have manually checked out and patched CPULLVM's various dependencies (see "Manually checking out
7777
and patching dependencies" below), CPULLVM's CMake will automatically checkout and patch the dependencies'
78-
Git repositories. Note that it will *always* try to patch these repos, even if the patches have already been
79-
applied. So, if you rerun CMake (either by manually invoking it or by modifying a file in some way that CMake
80-
automatically reruns when rebuilding), you'll likely encounter errors.
78+
Git repositories. The patching script uses `git am` and will skip patches that have already been applied,
79+
so re-running CMake is safe in the common case.
8180

82-
To work around this, after the first time you invoke CMake, you can include `-DFETCHCONTENT_FULLY_DISCONNECTED=ON`
83-
in your CMake command to tell it to stop updating and patching. Worst case, you can also manually remove the
84-
cloned repostories (located in `<build>/_deps`).
81+
If you encounter issues with re-running CMake (for example, after a failed or partial patch application),
82+
you can include `-DFETCHCONTENT_FULLY_DISCONNECTED=ON` in your CMake command to tell it to stop updating
83+
and patching. Worst case, you can also manually remove the cloned repositories (located in `<build>/_deps`).
8584

8685
### Manually checking out and patching dependencies
8786
It is also possible to manually checkout CPULLVM's dependencies. If you do so, you must ensure the

0 commit comments

Comments
 (0)