|
| 1 | +# Copyright (C) 2018-2026 Intel Corporation |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | +# |
| 4 | + |
| 5 | +# |
| 6 | +# ov_apply_patch( |
| 7 | +# PATCH <absolute-path-to-.patch-file> |
| 8 | +# [WORKING_DIRECTORY <dir>] -- directory passed to 'git apply'; defaults to CMAKE_SOURCE_DIR |
| 9 | +# ) |
| 10 | +# |
| 11 | +# Applies a local patch file via 'git apply' during the CMake configure phase. |
| 12 | +# Idempotent: uses 'git apply --reverse --check' to detect whether the patch is |
| 13 | +# already applied and skips the apply step if so. |
| 14 | +# Relies on GIT_FOUND / GIT_EXECUTABLE, which are set by find_package(Git) in version.cmake. |
| 15 | +# |
| 16 | +function(ov_apply_patch) |
| 17 | + cmake_parse_arguments(ARG "" "PATCH;WORKING_DIRECTORY" "" ${ARGN}) |
| 18 | + |
| 19 | + if(NOT DEFINED ARG_PATCH OR ARG_PATCH STREQUAL "") |
| 20 | + message(FATAL_ERROR "ov_apply_patch: PATCH argument is required") |
| 21 | + endif() |
| 22 | + |
| 23 | + if(NOT EXISTS "${ARG_PATCH}") |
| 24 | + message(FATAL_ERROR "ov_apply_patch: patch file does not exist: ${ARG_PATCH}") |
| 25 | + endif() |
| 26 | + |
| 27 | + if(NOT DEFINED ARG_WORKING_DIRECTORY OR ARG_WORKING_DIRECTORY STREQUAL "") |
| 28 | + set(ARG_WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}") |
| 29 | + endif() |
| 30 | + |
| 31 | + if(NOT IS_DIRECTORY "${ARG_WORKING_DIRECTORY}") |
| 32 | + message(FATAL_ERROR "ov_apply_patch: working directory does not exist: ${ARG_WORKING_DIRECTORY}") |
| 33 | + endif() |
| 34 | + |
| 35 | + # GIT_FOUND / GIT_EXECUTABLE are set by find_package(Git) in version.cmake, |
| 36 | + # but may be absent (e.g. Git not on PATH at configure time on Windows). |
| 37 | + # Re-run find_package locally as a fallback — it is cheap and cached by CMake. |
| 38 | + if(NOT GIT_FOUND) |
| 39 | + find_package(Git QUIET) |
| 40 | + endif() |
| 41 | + if(NOT GIT_FOUND) |
| 42 | + message(FATAL_ERROR |
| 43 | + "ov_apply_patch: Git is required to apply patches but was not found.\n" |
| 44 | + " Patch: ${ARG_PATCH}") |
| 45 | + endif() |
| 46 | + |
| 47 | + get_filename_component(_patch_display "${ARG_PATCH}" NAME) |
| 48 | + |
| 49 | + # Check whether the patch is already applied by attempting a dry-run reverse. |
| 50 | + # Exit code 0 → reverse applies cleanly → patch IS already applied → skip. |
| 51 | + # Exit code ≠ 0 → reverse fails → patch is NOT yet applied → apply it. |
| 52 | + execute_process( |
| 53 | + COMMAND "${GIT_EXECUTABLE}" -c core.autocrlf=false apply |
| 54 | + --reverse --check |
| 55 | + --ignore-space-change |
| 56 | + --ignore-whitespace |
| 57 | + "${ARG_PATCH}" |
| 58 | + WORKING_DIRECTORY "${ARG_WORKING_DIRECTORY}" |
| 59 | + RESULT_VARIABLE _ov_reverse_rc |
| 60 | + OUTPUT_QUIET |
| 61 | + ERROR_QUIET |
| 62 | + ) |
| 63 | + |
| 64 | + if(_ov_reverse_rc EQUAL 0) |
| 65 | + return() |
| 66 | + endif() |
| 67 | + |
| 68 | + # Patch not yet applied — apply it now. |
| 69 | + # -c core.autocrlf=false: patch files on Windows may have CRLF line endings; |
| 70 | + # disabling autocrlf makes git apply agnostic of the patch file's line endings. |
| 71 | + execute_process( |
| 72 | + COMMAND "${GIT_EXECUTABLE}" -c core.autocrlf=false apply |
| 73 | + --ignore-space-change |
| 74 | + --ignore-whitespace |
| 75 | + "${ARG_PATCH}" |
| 76 | + WORKING_DIRECTORY "${ARG_WORKING_DIRECTORY}" |
| 77 | + RESULT_VARIABLE _ov_patch_rc |
| 78 | + ERROR_VARIABLE _ov_patch_err |
| 79 | + OUTPUT_QUIET |
| 80 | + ) |
| 81 | + |
| 82 | + if(NOT _ov_patch_rc EQUAL 0) |
| 83 | + message(FATAL_ERROR |
| 84 | + "ov_apply_patch: failed to apply '${_patch_display}':\n${_ov_patch_err}") |
| 85 | + endif() |
| 86 | + |
| 87 | + message(STATUS "Applied patch: ${_patch_display}") |
| 88 | +endfunction() |
0 commit comments