FetchDependencies.cmake 6.4 KB

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