FetchDependencies.cmake 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. include(CMakeParseArguments)
  2. set(DEPENDENCY_CACHE_DIR ${CMAKE_BINARY_DIR}/dependencies CACHE PATH "Cache downloaded deps in this directory")
  3. set(DEPENDENCY_UNTAR_DIR ${CMAKE_BINARY_DIR}/dependencies CACHE PATH "Where to untar deps")
  4. if(APPLE)
  5. set(ARCHSTR "darwin-x86_64")
  6. elseif(WIN32)
  7. set(OS "windows-i386")
  8. set(ARCHSTR "windows-i386")
  9. elseif(UNIX)
  10. set(ARCHSTR ${PLEX_BUILD_TARGET})
  11. endif(APPLE)
  12. function(get_content_of_url)
  13. set(ARGS URL CONTENT_VAR FILENAME)
  14. cmake_parse_arguments(CU "ALWAYS" "${ARGS}" "" ${ARGN})
  15. if(NOT DEFINED CU_FILENAME)
  16. get_filename_component(CU_FILENAME ${CU_URL} NAME)
  17. endif(NOT DEFINED CU_FILENAME)
  18. if(EXISTS ${DEPENDENCY_CACHE_DIR}/${CU_FILENAME})
  19. if(CU_ALWAYS)
  20. file(REMOVE ${DEPENDENCY_CACHE_DIR}/${CU_FILENAME})
  21. else()
  22. file(STRINGS ${DEPENDENCY_CACHE_DIR}/${CU_FILENAME} CVAR LIMIT_COUNT 1)
  23. endif()
  24. endif()
  25. if(NOT CVAR)
  26. message(STATUS "Downloading ${CU_URL} to ${CU_FILENAME}...")
  27. file(
  28. DOWNLOAD ${CU_URL} ${DEPENDENCY_CACHE_DIR}/${CU_FILENAME}
  29. STATUS DL_STATUS
  30. LOG DL_LOG
  31. )
  32. list(GET DL_STATUS 0 SUCCESS)
  33. if(NOT SUCCESS EQUAL 0)
  34. list(GET DL_STATUS 1 ERROR_MESSAGE)
  35. file(REMOVE ${DEPENDENCY_CACHE_DIR}/${CU_FILENAME})
  36. message(FATAL_ERROR "Failed to download ${CU_URL}: ${ERROR_MESSAGE}\n${DL_LOG}")
  37. endif(NOT SUCCESS EQUAL 0)
  38. get_content_of_url(URL ${CU_URL} CONTENT_VAR CVAR FILENAME ${CU_FILENAME})
  39. endif()
  40. set(${CU_CONTENT_VAR} ${CVAR} PARENT_SCOPE)
  41. endfunction(get_content_of_url)
  42. function(download_deps DD_NAME)
  43. set(ARGS DIRECTORY BUILD_NUMBER ARTIFACTNAME VARIANT DEPHASH_VAR ARCHSTR DYLIB_SCRIPT_PATH)
  44. cmake_parse_arguments(DD "" "${ARGS}" "" ${ARGN})
  45. if(NOT DEFINED DD_VARIANT)
  46. set(DD_VARIANT "release")
  47. endif(NOT DEFINED DD_VARIANT)
  48. if(NOT DEFINED DD_ARTIFACTNAME)
  49. set(DD_ARTIFACTNAME ${DD_NAME})
  50. endif(NOT DEFINED DD_ARTIFACTNAME)
  51. if(NOT DEFINED DD_BUILD_NUMBER)
  52. set(DD_BUILD_NUMBER "latest")
  53. endif(NOT DEFINED DD_BUILD_NUMBER)
  54. if(NOT DEFINED DD_ARCHSTR)
  55. set(DD_ARCHSTR ${ARCHSTR})
  56. endif(NOT DEFINED DD_ARCHSTR)
  57. if(DD_BUILD_NUMBER STREQUAL latest)
  58. set(DD_ALWAYS_DOWNLOAD ALWAYS)
  59. endif()
  60. set(BASE_URL "https://nightlies.plex.tv/directdl/plex-dependencies/${DD_NAME}/${DD_BUILD_NUMBER}")
  61. set(DEP_DIR ${DEPENDENCY_UNTAR_DIR}/${DD_ARCHSTR}-${DD_NAME}/${DD_BUILD_NUMBER})
  62. set(HASH_FILENAME ${DD_NAME}-${DD_BUILD_NUMBER}-hash.txt)
  63. get_content_of_url(URL ${BASE_URL}/hash.txt CONTENT_VAR DEP_HASH FILENAME ${HASH_FILENAME} ${DD_ALWAYS_DOWNLOAD})
  64. if(NOT DEP_HASH)
  65. message(FATAL_ERROR "Failed to get hash for dependencies. Abort abort abort...")
  66. endif()
  67. message(STATUS "Dependency hash is: ${DEP_HASH}")
  68. if(DD_DEPHASH_VAR)
  69. set(${DD_DEPHASH_VAR} ${DEP_HASH} PARENT_SCOPE)
  70. endif()
  71. set(DEP_DIRNAME "${DD_ARTIFACTNAME}-${DD_ARCHSTR}-${DD_VARIANT}-${DEP_HASH}")
  72. set(DEP_FILENAME ${DEP_DIRNAME}.tbz2)
  73. set(${DD_DIRECTORY} ${DEP_DIR}/${DEP_DIRNAME} PARENT_SCOPE)
  74. set(${DD_DEP_HASH} ${DEP_HASH} PARENT_SCOPE)
  75. set(DEP_URL "${BASE_URL}/${DEP_FILENAME}")
  76. get_content_of_url(URL ${DEP_URL}.sha.txt CONTENT_VAR CONTENT_HASH)
  77. string(SUBSTRING "${CONTENT_HASH}" 0 40 CONTENT_HASH)
  78. if(NOT EXISTS ${DEP_DIR}/${DEP_DIRNAME}/_FINISHED)
  79. message(STATUS "Clearing out old dependencies ...")
  80. execute_process(COMMAND ${CMAKE_COMMAND} -E remove_directory ${DEPENDENCY_UNTAR_DIR}/${DD_ARCHSTR}-${DD_NAME})
  81. file(MAKE_DIRECTORY ${DEP_DIR})
  82. if(EXISTS ${DEPENDENCY_CACHE_DIR}/${DEP_FILENAME})
  83. message(STATUS "Checking checksum of file ${DEP_FILENAME}")
  84. file(SHA1 ${DEPENDENCY_CACHE_DIR}/${DEP_FILENAME} CURRENT_SHA1)
  85. endif()
  86. if(NOT CURRENT_SHA1 STREQUAL CONTENT_HASH)
  87. message(STATUS "Downloading ${DEP_FILENAME}...")
  88. file(
  89. DOWNLOAD ${DEP_URL} ${DEPENDENCY_CACHE_DIR}/${DEP_FILENAME}
  90. SHOW_PROGRESS
  91. STATUS DEP_STATUS
  92. LOG DEP_LOG
  93. )
  94. list(GET DEP_STATUS 0 DEP_SUCCESS)
  95. if(NOT DEP_SUCCESS EQUAL 0)
  96. list(GET DEP_STATUS 1 DEP_ERROR)
  97. file(REMOVE ${DEPENDENCY_CACHE_DIR}/${DEP_FILENAME})
  98. message(FATAL_ERROR "Failed to download ${DEP_URL}: ${DEP_ERROR}\n${DEP_LOG}")
  99. endif()
  100. message(STATUS "Checking checksum of file ${DEP_FILENAME}")
  101. file(SHA1 ${DEPENDENCY_CACHE_DIR}/${DEP_FILENAME} CURRENT_SHA1)
  102. if(NOT CURRENT_SHA1 STREQUAL CONTENT_HASH)
  103. file(REMOVE ${DEPENDENCY_CACHE_DIR}/${DEP_FILENAME})
  104. message(FATAL_ERROR "Failed to verify hash of dependencies, expected: ${CONTENT_HASH} actual: ${CURRENT_SHA1}")
  105. endif()
  106. endif()
  107. message(STATUS "Unpacking ${DEP_FILENAME}...")
  108. execute_process(
  109. COMMAND ${CMAKE_COMMAND} -E tar xjf ${DEPENDENCY_CACHE_DIR}/${DEP_FILENAME}
  110. WORKING_DIRECTORY ${DEP_DIR}
  111. RESULT_VARIABLE UNPACK_RESULT
  112. )
  113. if(NOT UNPACK_RESULT EQUAL 0)
  114. message(FATAL_ERROR "Failed to unpack deps..")
  115. endif(NOT UNPACK_RESULT EQUAL 0)
  116. if(APPLE AND DD_DYLIB_SCRIPT_PATH)
  117. message(STATUS "Fixing install library names...${DEP_DIR}/${DEP_DIRNAME}")
  118. execute_process(
  119. COMMAND ${DD_DYLIB_SCRIPT_PATH} ${DEP_DIR}/${DEP_DIRNAME}
  120. WORKING_DIRECTORY ${DEP_DIR}
  121. RESULT_VARIABLE DYLIB_RESULT
  122. )
  123. if(NOT DYLIB_RESULT EQUAL 0)
  124. message(FATAL_ERROR "Failed to run ${DD_DYLIB_SCRIPT_PATH}")
  125. endif()
  126. message(STATUS "Done")
  127. endif()
  128. if(EXISTS ${DEP_DIR}/${DEP_DIRNAME}/etc)
  129. message(STATUS "Removing etc in dependency bundle")
  130. file(REMOVE_RECURSE ${DEP_DIR}/${DEP_DIRNAME}/etc)
  131. endif(EXISTS ${DEP_DIR}/${DEP_DIRNAME}/etc)
  132. file(WRITE "${DEP_DIR}/${DEP_DIRNAME}/_FINISHED" "Dummy")
  133. else(NOT EXISTS ${DEP_DIR}/${DEP_DIRNAME}/_FINISHED)
  134. message(STATUS "Directory ${DEP_DIR}/${DEP_DIRNAME} already exists, remove it to redownload")
  135. endif(NOT EXISTS ${DEP_DIR}/${DEP_DIRNAME}/_FINISHED)
  136. endfunction(download_deps DD_NAME)