|
1 | 1 | function(apply_patch_once patch_name target_dir patch_file) |
2 | | - set(mark_file "${target_dir}/.${patch_name}_patched") |
| 2 | + # Store marker in build dir so it is cleaned with the build directory, |
| 3 | + # avoiding stale markers when submodule sources are reset by git. |
| 4 | + set(mark_file "${CMAKE_CURRENT_BINARY_DIR}/.${patch_name}_patched") |
3 | 5 |
|
4 | 6 | if(EXISTS "${mark_file}") |
5 | | - #message(STATUS "Patch '${patch_name}' already applied to ${target_dir}, skipping.") |
6 | 7 | return() |
7 | 8 | endif() |
8 | 9 |
|
9 | 10 | if(NOT EXISTS "${patch_file}") |
10 | 11 | message(FATAL_ERROR "Patch file '${patch_file}' not found!") |
11 | 12 | endif() |
12 | 13 |
|
13 | | - #message(STATUS "Applying patch '${patch_name}' to ${target_dir} ...") |
14 | 14 | execute_process( |
15 | | - COMMAND git apply --ignore-space-change --ignore-whitespace "${patch_file}" |
16 | | - WORKING_DIRECTORY "${target_dir}" |
17 | | - RESULT_VARIABLE patch_result |
18 | | - OUTPUT_VARIABLE patch_stdout |
19 | | - ERROR_VARIABLE patch_stderr |
| 15 | + COMMAND git apply --check --ignore-space-change --ignore-whitespace "${patch_file}" |
| 16 | + WORKING_DIRECTORY "${target_dir}" |
| 17 | + RESULT_VARIABLE check_result |
| 18 | + OUTPUT_QUIET ERROR_QUIET |
20 | 19 | ) |
21 | 20 |
|
22 | | - if(NOT patch_result EQUAL 0) |
23 | | - message(FATAL_ERROR "Failed to apply patch '${patch_name}' to ${target_dir}:\n${patch_stderr}") |
| 21 | + if (check_result EQUAL 0) |
| 22 | + execute_process( |
| 23 | + COMMAND git apply --ignore-space-change --ignore-whitespace "${patch_file}" |
| 24 | + WORKING_DIRECTORY "${target_dir}" |
| 25 | + RESULT_VARIABLE patch_result |
| 26 | + OUTPUT_VARIABLE patch_stdout |
| 27 | + ERROR_VARIABLE patch_stderr |
| 28 | + ) |
| 29 | + if (NOT patch_result EQUAL 0) |
| 30 | + message(FATAL_ERROR "Failed to apply patch '${patch_name}' to ${target_dir}:\n${patch_stderr}") |
| 31 | + endif () |
24 | 32 | else() |
25 | | - #message(STATUS "Patch '${patch_name}' applied successfully:\n${patch_stdout}") |
26 | | - file(WRITE "${mark_file}" "patched") |
| 33 | + # Patch cannot be applied cleanly - verify it is already applied |
| 34 | + execute_process( |
| 35 | + COMMAND git apply --reverse --check --ignore-space-change --ignore-whitespace "${patch_file}" |
| 36 | + WORKING_DIRECTORY "${target_dir}" |
| 37 | + RESULT_VARIABLE reverse_result |
| 38 | + OUTPUT_QUIET ERROR_QUIET |
| 39 | + ) |
| 40 | + if (NOT reverse_result EQUAL 0) |
| 41 | + message(FATAL_ERROR "Patch '${patch_name}' cannot be applied to ${target_dir} and does not appear to be already applied.") |
| 42 | + endif () |
27 | 43 | endif() |
| 44 | + |
| 45 | + file(WRITE "${mark_file}" "patched") |
28 | 46 | endfunction() |
0 commit comments