FetchDependencies.cmake 7.0 KB

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