Skip to content

fix: Patch commands fail trying to re-apply the same patch twice#664

Open
BohdanBuinich wants to merge 2 commits into
cpm-cmake:masterfrom
BohdanBuinich:feat/fix_patch_command
Open

fix: Patch commands fail trying to re-apply the same patch twice#664
BohdanBuinich wants to merge 2 commits into
cpm-cmake:masterfrom
BohdanBuinich:feat/fix_patch_command

Conversation

@BohdanBuinich

@BohdanBuinich BohdanBuinich commented Jun 3, 2025

Copy link
Copy Markdown

Fix repeated patch application

Problem

PATCHES used to build a shell-style PATCH_COMMAND that applied patch files with input redirection.

That made patching fragile:

  • repeated configure runs could try to apply the same patch again;
  • patch paths with spaces were harder to handle safely;
  • CPM had no explicit record that a patch set had already been applied;
  • changing a patch file at the same path could still look like the same patch command.

What changed

Patch handling now generates a small CMake script and uses it as the PATCH_COMMAND.

The script applies patches from the dependency source directory without shell redirection. For each patch, it first checks whether the patch can apply normally. If not, it checks whether the patch is already present by running a reverse dry-run.

This makes repeated configure runs safe:

  • apply the patch if it is not applied yet;
  • skip it if the same patch set was already completed;
  • accept it if the source tree already contains the patch changes;
  • fail clearly if the patch is neither applicable nor already applied.

Main idea

Patch state is now stored next to the dependency source tree:

.cpm_patches/<patch_set_hash>.stamp

The patch-set hash includes the patch executable, patch arguments, working-directory behavior, patch file paths, and patch file contents.

That means changing a patch file creates a new patch set, even if the file path stays the same.

Fixes #618

@BohdanBuinich

Copy link
Copy Markdown
Author

@TheLartians, Could you please review this change and let me know if you agree with it?

@aaeberharter

Copy link
Copy Markdown

I pulled CPM.cmake from the PR and it works for me.
cmake version 3.31.6-msvc6 (bundled with Visual Studio 2022)

Thank you

@aaeberharter

Copy link
Copy Markdown

PATCH_OPTIONS are mentioned in README.md but are not used in CPM.cmake. After this PR is merged we should discuss how to add meaningful patch options.

@BohdanBuinich BohdanBuinich force-pushed the feat/fix_patch_command branch from 5060d97 to 3c3ebf1 Compare June 16, 2025 09:10
@edunad

edunad commented Jun 19, 2025

Copy link
Copy Markdown

Any updates? I'm using the temp fix of SET after the first time cmake runs, but that doesn't work 100% of the time, would be awesome to get this merged soon 💖

@BohdanBuinich

Copy link
Copy Markdown
Author

@TheLartians, what do you think about this fix? Or do you think it’s too messy and have a better idea for handling it?

@BohdanBuinich BohdanBuinich force-pushed the feat/fix_patch_command branch 3 times, most recently from 07e5668 to 3e2d5e1 Compare May 6, 2026 22:10
@BohdanBuinich BohdanBuinich force-pushed the feat/fix_patch_command branch from c6b657b to 2ef2b1e Compare June 2, 2026 09:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Patch commands fail trying to re-apply the same patch twice

3 participants