CPM.cmake icon indicating copy to clipboard operation
CPM.cmake copied to clipboard

Adding PATCHES keyword.

Open ScottBailey opened this issue 2 months ago • 8 comments

This PR makes the following changes:

  • PATCHES multi-value keyword added to CPMAddPackage()
  • updated README for the new command
  • updated cmake-format for the new command
  • added a simple positive path integration test

Example usage:

CPMAddPackage(                                                                                                                                   
  NAME     src_lib                                                                                                                               
  URL      file:///home/bailey/research/CPM/src_lib.tar                                                                                          
  URL_HASH SHA256=0ce4bb954871b9d0250e5edfdd288dc44b295db60b2b3b832668eab52763e99b                                                               
  PATCHES                                                                                                                                        
    000-srclib-2.patch                                                                                                                           
    000-srclib-3.patch                                                                                                                           
)                                                                                                                                                

Patches are applied in the order listed using the patch executable. In case of a Windows host, if patch.exe is not immediately found, a second search for patch.exe alongside git.exe is performed.


This code change is essentially syntactical sugar. We take the element(s) from PATCHES and put them together with &&s behind a PATCH_COMMAND that is added to CPM_ARGS_UNPARSED_ARGUMENTS.

This functionality is available manually, but it's quite ugly.

The above command written manually looks something like this:

find_program(PATCH_EXECUTABLE pexe ...)
CPMAddPackage(
...
  PATCH_COMMAND "${pexe}\\\;-p\\\;<\\\;${CMAKE_CURRENT_LIST_DIR}/000-srclib-2.patch\\\;&&\\\;${pexe}\\\;-p\\\;<\\\;${CMAKE_CURRENT_LIST_DIR}/000-srclib-3.patch"
...
)

ScottBailey avatar Apr 23 '24 19:04 ScottBailey

@ScottBailey I tried this

For my own setup it works as expected: It applies patches

But with docker linux I got a bug : It doesn't apply patches.

Here's the branch with my attempt to test it. Try docker build . . After some time of compiling cmake, git and zip it shall configure cmake code and give error at generating. When it gives error at generating, it means it didn't apply patches

I don't know why in the docker image it fails and on my native machine it works correctly. We need further investigation.

Gerodote avatar Apr 23 '24 22:04 Gerodote

Hm, it seems removing CPM_SOURCE_CACHE triggers the error

Gerodote avatar Apr 23 '24 22:04 Gerodote

I don't know why in the docker image it fails and on my native machine it works correctly. We need further investigation.

Cool, thanks! If I have time, I'll investigate tomorrow.

Truthfully, patching in CMake has a lot of problems. It's not going to work in all cases. Where it fails, PATCH_COMMAND wouldn't be any better.

Ultimately, it makes dependency management much more convenient and intuitive.

ScottBailey avatar Apr 23 '24 22:04 ScottBailey

I tried some debugging when it happens ( when CPM_SOURCE_CACHE is not set)

It seems to not invoke your function because of an if statement

Gerodote avatar Apr 23 '24 23:04 Gerodote

It seems to not invoke your function because of an if statement

Good catch, I think I may have fixed it in 311cd3f.

ScottBailey avatar Apr 24 '24 01:04 ScottBailey

It seems to not invoke your function because of an if statement

Good catch, I think I may have fixed it in 311cd3f.

Yep, it seems to work now. Good job!

Gerodote avatar Apr 24 '24 11:04 Gerodote

Will close #494

ScottBailey avatar Apr 25 '24 13:04 ScottBailey

The PATCHES command is currently a gaping hole in CMake's fetch_content functionality. This PR addresses that in the simplest way possible.

@Gerodote Please feel free to review and comment on the change. @TheLartians @iboB Please review and comment on this change as time allows.

ScottBailey avatar Apr 25 '24 14:04 ScottBailey