FetchDependencies.cmake 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. add_definitions(-DEAE_VERSION=133)
  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 TOKEN)
  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. if(NOT DEFINED DD_TOKEN)
  76. set(DD_TOKEN plex-dependencies)
  77. endif()
  78. set(BASE_URL "https://nightlies.plex.tv/directdl/${DD_TOKEN}/${DD_NAME}/${DD_BUILD_NUMBER}")
  79. set(DEP_DIR ${DEPENDENCY_UNTAR_DIR}/${DD_ARCHSTR}-${DD_NAME}/${DD_BUILD_NUMBER})
  80. set(HASH_FILENAME ${DD_NAME}-${DD_BUILD_NUMBER}-hash.txt)
  81. get_content_of_url(URL ${BASE_URL}/hash.txt CONTENT_VAR DEP_HASH FILENAME ${HASH_FILENAME} ${DD_ALWAYS_DOWNLOAD})
  82. if(NOT DEP_HASH)
  83. message(FATAL_ERROR "Failed to get hash for dependencies. Abort abort abort...")
  84. endif()
  85. message(STATUS "Dependency hash is: ${DEP_HASH}")
  86. if(DD_DEPHASH_VAR)
  87. set(${DD_DEPHASH_VAR} ${DEP_HASH} PARENT_SCOPE)
  88. endif()
  89. set(DEP_DIRNAME "${DD_ARTIFACTNAME}-${DD_ARCHSTR}-${DD_VARIANT}-${DEP_HASH}")
  90. set(DEP_FILENAME ${DEP_DIRNAME}.tbz2)
  91. set(${DD_DIRECTORY} ${DEP_DIR}/${DEP_DIRNAME} PARENT_SCOPE)
  92. set(${DD_DEP_HASH} ${DEP_HASH} PARENT_SCOPE)
  93. set(DEP_URL "${BASE_URL}/${DEP_FILENAME}")
  94. get_content_of_url(URL ${DEP_URL}.sha.txt CONTENT_VAR CONTENT_HASH)
  95. string(SUBSTRING "${CONTENT_HASH}" 0 40 CONTENT_HASH)
  96. if(NOT EXISTS ${DEP_DIR}/${DEP_DIRNAME}/_FINISHED)
  97. message(STATUS "Clearing out old dependencies ...")
  98. execute_process(COMMAND ${CMAKE_COMMAND} -E remove_directory ${DEPENDENCY_UNTAR_DIR}/${DD_ARCHSTR}-${DD_NAME})
  99. file(MAKE_DIRECTORY ${DEP_DIR})
  100. if(EXISTS ${DEPENDENCY_CACHE_DIR}/${DEP_FILENAME})
  101. message(STATUS "Checking checksum of file ${DEP_FILENAME}")
  102. file(SHA1 ${DEPENDENCY_CACHE_DIR}/${DEP_FILENAME} CURRENT_SHA1)
  103. endif()
  104. if(NOT CURRENT_SHA1 STREQUAL CONTENT_HASH)
  105. message(STATUS "Downloading ${DEP_FILENAME}...")
  106. file(
  107. DOWNLOAD ${DEP_URL} ${DEPENDENCY_CACHE_DIR}/${DEP_FILENAME}
  108. SHOW_PROGRESS
  109. STATUS DEP_STATUS
  110. LOG DEP_LOG
  111. )
  112. list(GET DEP_STATUS 0 DEP_SUCCESS)
  113. if(NOT DEP_SUCCESS EQUAL 0)
  114. list(GET DEP_STATUS 1 DEP_ERROR)
  115. file(REMOVE ${DEPENDENCY_CACHE_DIR}/${DEP_FILENAME})
  116. message(FATAL_ERROR "Failed to download ${DEP_URL}: ${DEP_ERROR}\n${DEP_LOG}")
  117. endif()
  118. message(STATUS "Checking checksum of file ${DEP_FILENAME}")
  119. file(SHA1 ${DEPENDENCY_CACHE_DIR}/${DEP_FILENAME} CURRENT_SHA1)
  120. if(NOT CURRENT_SHA1 STREQUAL CONTENT_HASH)
  121. file(REMOVE ${DEPENDENCY_CACHE_DIR}/${DEP_FILENAME})
  122. message(FATAL_ERROR "Failed to verify hash of dependencies, expected: ${CONTENT_HASH} actual: ${CURRENT_SHA1}")
  123. endif()
  124. endif()
  125. message(STATUS "Unpacking ${DEP_FILENAME}...")
  126. execute_process(
  127. COMMAND ${CMAKE_COMMAND} -E tar xjf ${DEPENDENCY_CACHE_DIR}/${DEP_FILENAME}
  128. WORKING_DIRECTORY ${DEP_DIR}
  129. RESULT_VARIABLE UNPACK_RESULT
  130. )
  131. if(NOT UNPACK_RESULT EQUAL 0)
  132. message(FATAL_ERROR "Failed to unpack deps..")
  133. endif(NOT UNPACK_RESULT EQUAL 0)
  134. if(APPLE AND DD_DYLIB_SCRIPT_PATH)
  135. message(STATUS "Fixing install library names...${DEP_DIR}/${DEP_DIRNAME}")
  136. execute_process(
  137. COMMAND ${DD_DYLIB_SCRIPT_PATH} ${DEP_DIR}/${DEP_DIRNAME}
  138. WORKING_DIRECTORY ${DEP_DIR}
  139. RESULT_VARIABLE DYLIB_RESULT
  140. )
  141. if(NOT DYLIB_RESULT EQUAL 0)
  142. message(FATAL_ERROR "Failed to run ${DD_DYLIB_SCRIPT_PATH}")
  143. endif()
  144. message(STATUS "Done")
  145. endif()
  146. if(EXISTS ${DEP_DIR}/${DEP_DIRNAME}/etc)
  147. message(STATUS "Removing etc in dependency bundle")
  148. file(REMOVE_RECURSE ${DEP_DIR}/${DEP_DIRNAME}/etc)
  149. endif(EXISTS ${DEP_DIR}/${DEP_DIRNAME}/etc)
  150. file(WRITE "${DEP_DIR}/${DEP_DIRNAME}/_FINISHED" "Dummy")
  151. else(NOT EXISTS ${DEP_DIR}/${DEP_DIRNAME}/_FINISHED)
  152. message(STATUS "Directory ${DEP_DIR}/${DEP_DIRNAME} already exists, remove it to redownload")
  153. endif(NOT EXISTS ${DEP_DIR}/${DEP_DIRNAME}/_FINISHED)
  154. endfunction(download_deps DD_NAME)