1# Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
2# file Copyright.txt or https://cmake.org/licensing for details.
3
4#[=======================================================================[.rst:
5FindPatch
6---------
7
8.. versionadded:: 3.10
9
10The module defines the following variables:
11
12``Patch_EXECUTABLE``
13  Path to patch command-line executable.
14``Patch_FOUND``
15  True if the patch command-line executable was found.
16
17The following :prop_tgt:`IMPORTED` targets are also defined:
18
19``Patch::patch``
20  The command-line executable.
21
22Example usage:
23
24.. code-block:: cmake
25
26   find_package(Patch)
27   if(Patch_FOUND)
28     message("Patch found: ${Patch_EXECUTABLE}")
29   endif()
30#]=======================================================================]
31
32set(_doc "Patch command line executable")
33set(_patch_path )
34
35if(CMAKE_HOST_WIN32)
36  set(_patch_path
37    "$ENV{LOCALAPPDATA}/Programs/Git/bin"
38    "$ENV{LOCALAPPDATA}/Programs/Git/usr/bin"
39    "$ENV{APPDATA}/Programs/Git/bin"
40    "$ENV{APPDATA}/Programs/Git/usr/bin"
41    )
42endif()
43
44# First search the PATH
45find_program(Patch_EXECUTABLE
46  NAMES patch
47  PATHS ${_patch_path}
48  DOC ${_doc}
49  )
50
51if(CMAKE_HOST_WIN32)
52  # Now look for installations in Git/ directories under typical installation
53  # prefixes on Windows.
54  find_program(Patch_EXECUTABLE
55    NAMES patch
56    PATH_SUFFIXES Git/usr/bin Git/bin GnuWin32/bin
57    DOC ${_doc}
58    )
59endif()
60
61if(Patch_EXECUTABLE AND NOT TARGET Patch::patch)
62  add_executable(Patch::patch IMPORTED)
63  set_property(TARGET Patch::patch PROPERTY IMPORTED_LOCATION ${Patch_EXECUTABLE})
64endif()
65
66unset(_patch_path)
67unset(_doc)
68
69include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
70find_package_handle_standard_args(Patch
71                                  REQUIRED_VARS Patch_EXECUTABLE)
72