FetchDependencies.cmake 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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 ${JELLYFIN_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
  59. ARTIFACTNAME
  60. VARIANT
  61. DEPHASH_VAR
  62. ARCHSTR
  63. DYLIB_SCRIPT_PATH
  64. BASE_URL
  65. TOKEN)
  66. cmake_parse_arguments(DD "NO_HASH_FILE" "${ARGS}" "" ${ARGN})
  67. if(NOT DEFINED DD_VARIANT)
  68. set(DD_VARIANT "release")
  69. endif(NOT DEFINED DD_VARIANT)
  70. if(NOT DEFINED DD_ARTIFACTNAME)
  71. set(DD_ARTIFACTNAME ${DD_NAME})
  72. endif(NOT DEFINED DD_ARTIFACTNAME)
  73. if(NOT DEFINED DD_BUILD_NUMBER)
  74. set(DD_BUILD_NUMBER "latest")
  75. endif(NOT DEFINED DD_BUILD_NUMBER)
  76. if(NOT DEFINED DD_ARCHSTR)
  77. set(DD_ARCHSTR ${ARCHSTR})
  78. endif(NOT DEFINED DD_ARCHSTR)
  79. if(DD_BUILD_NUMBER STREQUAL latest)
  80. set(DD_ALWAYS_DOWNLOAD ALWAYS)
  81. endif()
  82. if(NOT DEFINED DD_TOKEN)
  83. set(DD_TOKEN jellyfin-dependencies)
  84. endif()
  85. if(NOT DEFINED DD_BASE_URL)
  86. set(DD_BASE_URL "")
  87. endif()
  88. set(DEP_DIR ${DEPENDENCY_UNTAR_DIR}/${DD_ARCHSTR}-${DD_NAME}/${DD_BUILD_NUMBER})
  89. get_cmake_property(cacheVars CACHE_VARIABLES)
  90. foreach(var ${cacheVars})
  91. if(("${${var}}" MATCHES "^${DEPENDENCY_UNTAR_DIR}/${DD_ARCHSTR}-${DD_NAME}/[0-9]+") AND
  92. (NOT ("${${var}}" MATCHES "^${DEP_DIR}")))
  93. unset(${var} CACHE)
  94. endif()
  95. endforeach()
  96. if(NOT DD_NO_HASH_FILE)
  97. set(HASH_FILENAME ${DD_NAME}-${DD_BUILD_NUMBER}-hash.txt)
  98. get_content_of_url(URL ${DD_BASE_URL}/hash.txt CONTENT_VAR DEP_HASH FILENAME ${HASH_FILENAME} ${DD_ALWAYS_DOWNLOAD})
  99. if(NOT DEP_HASH)
  100. message(FATAL_ERROR "Failed to get hash for dependencies. Abort abort abort...")
  101. endif()
  102. message(STATUS "Dependency hash is: ${DEP_HASH}")
  103. if(DD_DEPHASH_VAR)
  104. set(${DD_DEPHASH_VAR} ${DEP_HASH} PARENT_SCOPE)
  105. endif()
  106. set(DEP_DIRNAME "${DD_ARTIFACTNAME}-${DD_ARCHSTR}-${DD_VARIANT}-${DEP_HASH}")
  107. set(DEP_FILENAME ${DEP_DIRNAME}.tbz2)
  108. set(${DD_DEP_HASH} ${DEP_HASH} PARENT_SCOPE)
  109. set(DEP_URL "${DD_BASE_URL}/${DEP_FILENAME}")
  110. set(CONTENT_HASH_URL ${DEP_URL}.sha.txt)
  111. else()
  112. set(DEP_DIRNAME "${DD_ARTIFACTNAME}-${DD_VARIANT}")
  113. set(DEP_FILENAME "${DEP_DIRNAME}.tar.xz")
  114. set(DEP_URL "${DD_BASE_URL}/${DEP_FILENAME}")
  115. set(CONTENT_HASH_URL ${DEP_URL}.sha1)
  116. endif()
  117. set(${DD_DIRECTORY} ${DEP_DIR}/${DEP_DIRNAME} PARENT_SCOPE)
  118. get_content_of_url(URL ${CONTENT_HASH_URL} CONTENT_VAR CONTENT_HASH)
  119. string(SUBSTRING "${CONTENT_HASH}" 0 40 CONTENT_HASH)
  120. if(NOT EXISTS ${DEP_DIR}/${DEP_DIRNAME}/_FINISHED)
  121. message(STATUS "Clearing out old dependencies ...")
  122. execute_process(COMMAND ${CMAKE_COMMAND} -E remove_directory ${DEPENDENCY_UNTAR_DIR}/${DD_ARCHSTR}-${DD_NAME})
  123. file(MAKE_DIRECTORY ${DEP_DIR})
  124. if(EXISTS ${DEPENDENCY_CACHE_DIR}/${DEP_FILENAME})
  125. message(STATUS "Checking checksum of file ${DEP_FILENAME}")
  126. file(SHA1 ${DEPENDENCY_CACHE_DIR}/${DEP_FILENAME} CURRENT_SHA1)
  127. endif()
  128. if(NOT CURRENT_SHA1 STREQUAL CONTENT_HASH)
  129. message(STATUS "Downloading ${DEP_FILENAME}...")
  130. file(
  131. DOWNLOAD ${DEP_URL} ${DEPENDENCY_CACHE_DIR}/${DEP_FILENAME}
  132. SHOW_PROGRESS
  133. STATUS DEP_STATUS
  134. LOG DEP_LOG
  135. )
  136. list(GET DEP_STATUS 0 DEP_SUCCESS)
  137. if(NOT DEP_SUCCESS EQUAL 0)
  138. list(GET DEP_STATUS 1 DEP_ERROR)
  139. file(REMOVE ${DEPENDENCY_CACHE_DIR}/${DEP_FILENAME})
  140. message(FATAL_ERROR "Failed to download ${DEP_URL}: ${DEP_ERROR}\n${DEP_LOG}")
  141. endif()
  142. message(STATUS "Checking checksum of file ${DEP_FILENAME}")
  143. file(SHA1 ${DEPENDENCY_CACHE_DIR}/${DEP_FILENAME} CURRENT_SHA1)
  144. if(NOT CURRENT_SHA1 STREQUAL CONTENT_HASH)
  145. file(REMOVE ${DEPENDENCY_CACHE_DIR}/${DEP_FILENAME})
  146. message(FATAL_ERROR "Failed to verify hash of dependencies, expected: ${CONTENT_HASH} actual: ${CURRENT_SHA1}")
  147. endif()
  148. endif()
  149. message(STATUS "Unpacking ${DEP_FILENAME}...")
  150. execute_process(
  151. COMMAND ${CMAKE_COMMAND} -E tar xjf ${DEPENDENCY_CACHE_DIR}/${DEP_FILENAME}
  152. WORKING_DIRECTORY ${DEP_DIR}
  153. RESULT_VARIABLE UNPACK_RESULT
  154. )
  155. if(NOT UNPACK_RESULT EQUAL 0)
  156. message(FATAL_ERROR "Failed to unpack deps..")
  157. endif(NOT UNPACK_RESULT EQUAL 0)
  158. if(APPLE AND DD_DYLIB_SCRIPT_PATH)
  159. message(STATUS "Fixing install library names...${DEP_DIR}/${DEP_DIRNAME}")
  160. execute_process(
  161. COMMAND ${DD_DYLIB_SCRIPT_PATH} ${DEP_DIR}/${DEP_DIRNAME}
  162. WORKING_DIRECTORY ${DEP_DIR}
  163. RESULT_VARIABLE DYLIB_RESULT
  164. )
  165. if(NOT DYLIB_RESULT EQUAL 0)
  166. message(FATAL_ERROR "Failed to run ${DD_DYLIB_SCRIPT_PATH}")
  167. endif()
  168. message(STATUS "Done")
  169. endif()
  170. if(EXISTS ${DEP_DIR}/${DEP_DIRNAME}/etc)
  171. message(STATUS "Removing etc in dependency bundle")
  172. file(REMOVE_RECURSE ${DEP_DIR}/${DEP_DIRNAME}/etc)
  173. endif(EXISTS ${DEP_DIR}/${DEP_DIRNAME}/etc)
  174. file(WRITE "${DEP_DIR}/${DEP_DIRNAME}/_FINISHED" "Dummy")
  175. else(NOT EXISTS ${DEP_DIR}/${DEP_DIRNAME}/_FINISHED)
  176. message(STATUS "Directory ${DEP_DIR}/${DEP_DIRNAME} already exists, remove it to redownload")
  177. endif(NOT EXISTS ${DEP_DIR}/${DEP_DIRNAME}/_FINISHED)
  178. endfunction(download_deps DD_NAME)
  179. function(download_dep_file DD_NAME)
  180. set(ARGS DIRECTORY BUILD_NUMBER ARTIFACTNAME TOKEN FILENAME DST_FILENAME)
  181. cmake_parse_arguments(DD "" "${ARGS}" "" ${ARGN})
  182. if(NOT DEFINED DD_ARTIFACTNAME)
  183. set(DD_ARTIFACTNAME ${DD_NAME})
  184. endif(NOT DEFINED DD_ARTIFACTNAME)
  185. if(NOT DEFINED DD_BUILD_NUMBER)
  186. set(DD_BUILD_NUMBER "latest")
  187. endif(NOT DEFINED DD_BUILD_NUMBER)
  188. if(DD_BUILD_NUMBER STREQUAL latest)
  189. set(DD_ALWAYS_DOWNLOAD ALWAYS)
  190. endif()
  191. if(NOT DEFINED DD_TOKEN)
  192. set(DD_TOKEN jellyfin-dependencies)
  193. endif()
  194. set(BASE_URL "")
  195. set(DEP_URL "${BASE_URL}/${DD_FILENAME}")
  196. if(NOT EXISTS ${DEPENDENCY_CACHE_DIR}/${DD_FILENAME})
  197. message(STATUS "Downloading ${DD_FILENAME}...")
  198. file(
  199. DOWNLOAD ${DEP_URL} ${DEPENDENCY_CACHE_DIR}/${DD_FILENAME}
  200. SHOW_PROGRESS
  201. STATUS DEP_STATUS
  202. LOG DEP_LOG
  203. )
  204. list(GET DEP_STATUS 0 DEP_SUCCESS)
  205. if(NOT DEP_SUCCESS EQUAL 0)
  206. list(GET DEP_STATUS 1 DEP_ERROR)
  207. file(REMOVE ${DEPENDENCY_CACHE_DIR}/${DD_FILENAME})
  208. message(FATAL_ERROR "Failed to download ${DEP_URL}: ${DEP_ERROR}\n${DEP_LOG}")
  209. endif()
  210. endif()
  211. #file(COPY ${DEPENDENCY_CACHE_DIR}/${FILENAME} ${DST_FILENAME})
  212. endfunction()