FetchDependencies.cmake 6.3 KB

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