Skip to content

Commit a712d52

Browse files
committed
try to fix 3rd party patch
1 parent b605aec commit a712d52

File tree

1 file changed

+30
-12
lines changed

1 file changed

+30
-12
lines changed

cmake/utils.cmake

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,46 @@
11
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")
35

46
if(EXISTS "${mark_file}")
5-
#message(STATUS "Patch '${patch_name}' already applied to ${target_dir}, skipping.")
67
return()
78
endif()
89

910
if(NOT EXISTS "${patch_file}")
1011
message(FATAL_ERROR "Patch file '${patch_file}' not found!")
1112
endif()
1213

13-
#message(STATUS "Applying patch '${patch_name}' to ${target_dir} ...")
1414
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
2019
)
2120

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 ()
2432
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 ()
2743
endif()
44+
45+
file(WRITE "${mark_file}" "patched")
2846
endfunction()

0 commit comments

Comments
 (0)