Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

After patch update cache fix #794

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 4 additions & 13 deletions cmake/dependencies/common.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +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)
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)

# 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})
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_command}
)
FetchContent_MakeAvailable(ImGui)
list(APPEND ADDITIONAL_LIB_INCLUDES ${imgui_SOURCE_DIR} ${imgui_SOURCE_DIR}/backends)
Expand Down Expand Up @@ -45,15 +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})
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}
PATCH_COMMAND ${stormlib_apply_patch_command}
)
FetchContent_MakeAvailable(StormLib)
list(APPEND ADDITIONAL_LIB_INCLUDES ${stormlib_SOURCE_DIR}/src)
Expand Down
61 changes: 61 additions & 0 deletions cmake/dependencies/git-patch.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# 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()

# 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)
check_patch()
endif()
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)
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}")
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()
Loading