aboutsummaryrefslogtreecommitdiff
path: root/cmake/modules/buildtools/FindPatch.cmake
blob: 4fbf5a6dbdc877346bdee7ab7f48c58563b37d0b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#.rst:
# FindPatch
# ----------
# Finds patch executable
# Windows platforms will download patch zip from mirrors if not found.
#
# This will define the following variables::
#
# PATCH_EXECUTABLE - patch executable

if(CMAKE_HOST_WIN32)

  find_package(Git)
  if(Git_FOUND)
    get_filename_component(GIT_DIR ${GIT_EXECUTABLE} DIRECTORY)
  endif()

  find_program(PATCH_EXE NAMES patch.exe HINTS "${GIT_DIR}/.." PATH_SUFFIXES usr/bin)
else()
  # Freebsd patch is insufficient (too old) look for gnu patch first
  find_program(PATCH_EXE NAMES gpatch patch)
endif()

if(CMAKE_HOST_WIN32 AND NOT PATCH_EXE)
  # Set mirror for potential patch binary download
  if(NOT KODI_MIRROR)
    set(KODI_MIRROR "http://mirrors.kodi.tv")
  endif()

  set(PATCH_ARCHIVE_NAME "patch-2.7.6-bin")
  set(PATCH_ARCHIVE "${PATCH_ARCHIVE_NAME}.zip")
  set(PATCH_URL "${KODI_MIRROR}/build-deps/win32/${PATCH_ARCHIVE}")
  set(PATCH_DOWNLOAD ${TARBALL_DIR}/${PATCH_ARCHIVE})

  # download the archive containing patch.exe
  message(STATUS "Downloading patch utility from ${PATCH_URL}...")
  file(DOWNLOAD "${PATCH_URL}" "${PATCH_DOWNLOAD}" STATUS PATCH_DL_STATUS LOG PATCH_LOG SHOW_PROGRESS)
  list(GET PATCH_DL_STATUS 0 PATCH_RETCODE)
  if(NOT PATCH_RETCODE EQUAL 0)
    message(FATAL_ERROR "ERROR downloading ${PATCH_URL} - status: ${PATCH_DL_STATUS} log: ${PATCH_LOG}")
  endif()

  # CORE_BUILD_DIR may not exist as yet, so create just in case
  if(NOT EXISTS ${CMAKE_BINARY_DIR}/${CORE_BUILD_DIR})
    file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/${CORE_BUILD_DIR})
  endif()

  # extract the archive containing patch.exe
  execute_process(COMMAND ${CMAKE_COMMAND} -E tar xzvf ${PATCH_DOWNLOAD}
                  WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/${CORE_BUILD_DIR})

  # make sure the extraction worked and that patch.exe is there
  set(PATCH_PATH ${CMAKE_BINARY_DIR}/${CORE_BUILD_DIR}/${PATCH_ARCHIVE_NAME})
  if(NOT EXISTS "${PATCH_PATH}/bin/patch.exe")
    message(FATAL_ERROR "ERROR extracting patch utility from ${PATCH_PATH}")
  endif()

  # copy patch.exe into the output directory
  file(INSTALL "${PATCH_PATH}/bin/patch.exe" DESTINATION ${CMAKE_BINARY_DIR}/${CORE_BUILD_DIR}/bin)
  # copy patch depends
  file(GLOB PATCH_BINARIES ${PATCH_PATH}/bin/*.dll)
  if(NOT "${PATCH_BINARIES}" STREQUAL "")
    file(INSTALL ${PATCH_BINARIES} DESTINATION ${CMAKE_BINARY_DIR}/${CORE_BUILD_DIR}/bin)
  endif()

  # make sure that cmake can find the copied patch.exe
  find_program(PATCH_EXE NAMES patch.exe HINTS ${CMAKE_BINARY_DIR}/${CORE_BUILD_DIR}/bin REQUIRED)
endif()

include(FindPackageHandleStandardArgs)

set(FPHSA_NAME_MISMATCHED 1) # Suppress warnings, see https://cmake.org/cmake/help/v3.17/module/FindPackageHandleStandardArgs.html
find_package_handle_standard_args(PATCH REQUIRED_VARS PATCH_EXE)
unset(FPHSA_NAME_MISMATCHED)

if(PATCH_FOUND)
  set(PATCH_EXECUTABLE "${PATCH_EXE}")
  mark_as_advanced(PATCH_EXE)
endif()