From 77a2099470eea13391fdcaf82ae656f94f790b1e Mon Sep 17 00:00:00 2001 From: lightmanLP Date: Mon, 20 Jan 2025 19:02:46 +0700 Subject: [PATCH 1/5] apply patch with reset --- cmake/dependencies/common.cmake | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/cmake/dependencies/common.cmake b/cmake/dependencies/common.cmake index 21ebd54d7..480a489a9 100644 --- a/cmake/dependencies/common.cmake +++ b/cmake/dependencies/common.cmake @@ -13,11 +13,13 @@ set(imgui_fixes_and_config_patch_file ${CMAKE_CURRENT_SOURCE_DIR}/cmake/dependen # Applies the patch or checks if it has already been applied successfully previously. Will error otherwise. set(imgui_apply_patch_if_needed git apply ${imgui_fixes_and_config_patch_file} ${git_hide_output} || git apply --reverse --check ${imgui_fixes_and_config_patch_file}) +# Resets code and reapply patch, if old (potentially incompatible) patch applied +set(imgui_apply_patch_if_needed_with_reset ${imgui_apply_patch_if_needed} || git diff --check && (git reset --hard || ${imgui_apply_patch_if_needed})) FetchContent_Declare( ImGui GIT_REPOSITORY https://github.com/ocornut/imgui.git GIT_TAG v1.91.6-docking - PATCH_COMMAND ${imgui_apply_patch_if_needed} + PATCH_COMMAND ${imgui_apply_patch_if_needed_with_reset} ) FetchContent_MakeAvailable(ImGui) list(APPEND ADDITIONAL_LIB_INCLUDES ${imgui_SOURCE_DIR} ${imgui_SOURCE_DIR}/backends) From 34ec5dad4166dbd5cfdb87c09b8c3d46af5dbcaf Mon Sep 17 00:00:00 2001 From: lightmanLP Date: Mon, 20 Jan 2025 20:26:50 +0700 Subject: [PATCH 2/5] same for stormlib --- cmake/dependencies/common.cmake | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/cmake/dependencies/common.cmake b/cmake/dependencies/common.cmake index 480a489a9..8c83585ba 100644 --- a/cmake/dependencies/common.cmake +++ b/cmake/dependencies/common.cmake @@ -50,12 +50,14 @@ if(NOT EXCLUDE_MPQ_SUPPORT) # Applies the patch or checks if it has already been applied successfully previously. Will error otherwise. set(stormlib_apply_patch_if_needed git apply ${stormlib_patch_file} ${git_hide_output} || git apply --reverse --check ${stormlib_patch_file}) + # Resets code and reapply patch, if old (potentially incompatible) patch applied + set(stormlib_apply_patch_if_needed_with_reset ${stormlib_apply_patch_if_needed} || git diff --check && (git reset --hard || ${stormlib_apply_patch_if_needed})) FetchContent_Declare( StormLib GIT_REPOSITORY https://github.com/ladislav-zezula/StormLib.git GIT_TAG v9.25 - PATCH_COMMAND ${stormlib_apply_patch_if_needed} + PATCH_COMMAND ${stormlib_apply_patch_if_needed_with_reset} ) FetchContent_MakeAvailable(StormLib) list(APPEND ADDITIONAL_LIB_INCLUDES ${stormlib_SOURCE_DIR}/src) From 800ea160b85905b6c2cbb7e18067f99c529d9e85 Mon Sep 17 00:00:00 2001 From: lightmanLP Date: Mon, 20 Jan 2025 20:58:58 +0700 Subject: [PATCH 3/5] fix --- cmake/dependencies/common.cmake | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/cmake/dependencies/common.cmake b/cmake/dependencies/common.cmake index 8c83585ba..aca61fe25 100644 --- a/cmake/dependencies/common.cmake +++ b/cmake/dependencies/common.cmake @@ -14,7 +14,8 @@ set(imgui_fixes_and_config_patch_file ${CMAKE_CURRENT_SOURCE_DIR}/cmake/dependen # Applies the patch or checks if it has already been applied successfully previously. Will error otherwise. set(imgui_apply_patch_if_needed git apply ${imgui_fixes_and_config_patch_file} ${git_hide_output} || git apply --reverse --check ${imgui_fixes_and_config_patch_file}) # Resets code and reapply patch, if old (potentially incompatible) patch applied -set(imgui_apply_patch_if_needed_with_reset ${imgui_apply_patch_if_needed} || git diff --check && (git reset --hard || ${imgui_apply_patch_if_needed})) +set(imgui_apply_patch_if_needed_with_reset ${imgui_apply_patch_if_needed} || (git status --porcelain && git reset --hard && (${imgui_apply_patch_if_needed}))) + FetchContent_Declare( ImGui GIT_REPOSITORY https://github.com/ocornut/imgui.git @@ -51,7 +52,7 @@ if(NOT EXCLUDE_MPQ_SUPPORT) # Applies the patch or checks if it has already been applied successfully previously. Will error otherwise. set(stormlib_apply_patch_if_needed git apply ${stormlib_patch_file} ${git_hide_output} || git apply --reverse --check ${stormlib_patch_file}) # Resets code and reapply patch, if old (potentially incompatible) patch applied - set(stormlib_apply_patch_if_needed_with_reset ${stormlib_apply_patch_if_needed} || git diff --check && (git reset --hard || ${stormlib_apply_patch_if_needed})) + set(stormlib_apply_patch_if_needed_with_reset ${stormlib_apply_patch_if_needed} || (git status --porcelain && git reset --hard && (${stormlib_apply_patch_if_needed}))) FetchContent_Declare( StormLib From 6fe543fe9d7a3fad08bdc696b854637bc54a1e4c Mon Sep 17 00:00:00 2001 From: lightmanLP Date: Sat, 1 Feb 2025 11:53:05 +0700 Subject: [PATCH 4/5] patch via cmake script --- cmake/dependencies/common.cmake | 22 ++--------- cmake/dependencies/git-patch.cmake | 59 ++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 18 deletions(-) create mode 100644 cmake/dependencies/git-patch.cmake diff --git a/cmake/dependencies/common.cmake b/cmake/dependencies/common.cmake index aca61fe25..28a8719ba 100644 --- a/cmake/dependencies/common.cmake +++ b/cmake/dependencies/common.cmake @@ -2,25 +2,15 @@ include(FetchContent) find_package(OpenGL QUIET) -# When using the Visual Studio generator, it is necessary to suppress stderr output entirely so it does not interrupt the patch command. -# Redirecting to nul is used here instead of the `--quiet` flag, as that flag was only recently introduced in git 2.25.0 (Jan 2022) -if (CMAKE_GENERATOR MATCHES "Visual Studio") - set(git_hide_output 2> nul) -endif() - #=================== ImGui =================== set(imgui_fixes_and_config_patch_file ${CMAKE_CURRENT_SOURCE_DIR}/cmake/dependencies/patches/imgui-fixes-and-config.patch) - -# Applies the patch or checks if it has already been applied successfully previously. Will error otherwise. -set(imgui_apply_patch_if_needed git apply ${imgui_fixes_and_config_patch_file} ${git_hide_output} || git apply --reverse --check ${imgui_fixes_and_config_patch_file}) -# Resets code and reapply patch, if old (potentially incompatible) patch applied -set(imgui_apply_patch_if_needed_with_reset ${imgui_apply_patch_if_needed} || (git status --porcelain && git reset --hard && (${imgui_apply_patch_if_needed}))) +set(imgui_apply_patch_command ${CMAKE_COMMAND} -Dpatch_file=${imgui_fixes_and_config_patch_file} -Dwith_reset=TRUE -P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/dependencies/git-patch.cmake) FetchContent_Declare( ImGui GIT_REPOSITORY https://github.com/ocornut/imgui.git GIT_TAG v1.91.6-docking - PATCH_COMMAND ${imgui_apply_patch_if_needed_with_reset} + PATCH_COMMAND ${imgui_apply_patch_command} ) FetchContent_MakeAvailable(ImGui) list(APPEND ADDITIONAL_LIB_INCLUDES ${imgui_SOURCE_DIR} ${imgui_SOURCE_DIR}/backends) @@ -48,17 +38,13 @@ target_include_directories(ImGui PUBLIC ${imgui_SOURCE_DIR} ${imgui_SOURCE_DIR}/ # ========= StormLib ============= if(NOT EXCLUDE_MPQ_SUPPORT) set(stormlib_patch_file ${CMAKE_CURRENT_SOURCE_DIR}/cmake/dependencies/patches/stormlib-optimizations.patch) - - # Applies the patch or checks if it has already been applied successfully previously. Will error otherwise. - set(stormlib_apply_patch_if_needed git apply ${stormlib_patch_file} ${git_hide_output} || git apply --reverse --check ${stormlib_patch_file}) - # Resets code and reapply patch, if old (potentially incompatible) patch applied - set(stormlib_apply_patch_if_needed_with_reset ${stormlib_apply_patch_if_needed} || (git status --porcelain && git reset --hard && (${stormlib_apply_patch_if_needed}))) + set(stormlib_apply_patch_command ${CMAKE_COMMAND} -Dpatch_file=${stormlib_patch_file} -Dwith_reset=TRUE -P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/dependencies/git-patch.cmake) FetchContent_Declare( StormLib GIT_REPOSITORY https://github.com/ladislav-zezula/StormLib.git GIT_TAG v9.25 - PATCH_COMMAND ${stormlib_apply_patch_if_needed_with_reset} + PATCH_COMMAND ${stormlib_apply_patch_command} ) FetchContent_MakeAvailable(StormLib) list(APPEND ADDITIONAL_LIB_INCLUDES ${stormlib_SOURCE_DIR}/src) diff --git a/cmake/dependencies/git-patch.cmake b/cmake/dependencies/git-patch.cmake new file mode 100644 index 000000000..e315d32a4 --- /dev/null +++ b/cmake/dependencies/git-patch.cmake @@ -0,0 +1,59 @@ +# In variables: patch_file, with_reset + +function(patch) + execute_process( + COMMAND git apply ${patch_file} + RESULT_VARIABLE ret + ERROR_QUIET + ) + set(ret ${ret} PARENT_SCOPE) +endfunction() + +function(check_patch) + execute_process( + COMMAND git apply --reverse --check ${patch_file} + RESULT_VARIABLE ret + ERROR_QUIET + ) + set(ret ${ret} PARENT_SCOPE) +endfunction() + +function(patch_if_needed) + patch() + if(NOT ret EQUAL 0) + check_patch() + endif() + set(ret ${ret} PARENT_SCOPE) +endfunction() + +function(patch_if_needed_with_reset) + patch_if_needed() + if(NOT ret EQUAL 0) + message(STATUS "Failed to patch in current state, clearing changes to reapply") + execute_process( + COMMAND git status --porcelain + RESULT_VARIABLE is_changed + ) + if(NOT is_changed EQUAL 0) + message(WARNING "Patch inapplyable in clean state") + set(ret 1) + else() + execute_process(COMMAND git reset --hard) + patch_if_needed() + endif() + endif() + set(ret ${ret} PARENT_SCOPE) +endfunction() + +message(STATUS "Trying to apply patch ${patch_file}") +message(STATUS "${CMAKE_CURRENT_LIST_DIR}") +if(with_reset) + patch_if_needed_with_reset() +else() + patch_if_needed() +endif() +if(NOT ret EQUAL 0) + message(FATAL_ERROR "Failed to apply patch ${patch_file}") +else() + message(STATUS "Successfully patched with ${patch_file}") +endif() From 1f47ec6de73a1d0275ff37ba85f269e632ec664f Mon Sep 17 00:00:00 2001 From: lightmanLP Date: Mon, 3 Feb 2025 08:46:49 +0700 Subject: [PATCH 5/5] restore comments & cleanup --- cmake/dependencies/git-patch.cmake | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/cmake/dependencies/git-patch.cmake b/cmake/dependencies/git-patch.cmake index e315d32a4..deb3e35da 100644 --- a/cmake/dependencies/git-patch.cmake +++ b/cmake/dependencies/git-patch.cmake @@ -18,6 +18,7 @@ function(check_patch) set(ret ${ret} PARENT_SCOPE) endfunction() +# Applies the patch or checks if it has already been applied successfully previously. Will error otherwise. function(patch_if_needed) patch() if(NOT ret EQUAL 0) @@ -26,6 +27,7 @@ function(patch_if_needed) set(ret ${ret} PARENT_SCOPE) endfunction() +# Resets code and reapply patch, if old (potentially incompatible) patch applied function(patch_if_needed_with_reset) patch_if_needed() if(NOT ret EQUAL 0) @@ -46,12 +48,12 @@ function(patch_if_needed_with_reset) endfunction() message(STATUS "Trying to apply patch ${patch_file}") -message(STATUS "${CMAKE_CURRENT_LIST_DIR}") if(with_reset) patch_if_needed_with_reset() else() patch_if_needed() endif() + if(NOT ret EQUAL 0) message(FATAL_ERROR "Failed to apply patch ${patch_file}") else()