Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.77 KB | None | 0 0
  1. #!/usr/bin/env bash
  2.  
  3. FFMPEG_FALLBACK_VERSION=3.1.3
  4.  
  5. # Defining a toolchan directory's name according to the current OS.
  6. # Assume that proper version of NDK is installed.
  7. case "$OSTYPE" in
  8. darwin*) HOST_TAG="darwin-x86_64" ;;
  9. linux*) HOST_TAG="linux-x86_64" ;;
  10. msys)
  11. case "$(uname -m)" in
  12. x86_64) HOST_TAG="windows-x86_64" ;;
  13. i686) HOST_TAG="windows" ;;
  14. esac
  15. ;;
  16. esac
  17.  
  18. # Directories used by the script
  19. BASE_DIR="$( cd "$( dirname "$0" )" && pwd )"
  20. SOURCES_DIR=${BASE_DIR}/sources
  21. OUTPUT_DIR=${BASE_DIR}/output
  22. BUILD_DIR=${BASE_DIR}/build
  23. STATS_DIR=${BASE_DIR}/stats
  24.  
  25. # No incremental compilation here. Just drop what was built previously
  26. rm -rf ${BUILD_DIR}
  27. rm -rf ${STATS_DIR}
  28. rm -rf ${OUTPUT_DIR}
  29. mkdir -p ${STATS_DIR}
  30. mkdir -p ${OUTPUT_DIR}
  31. # Note: the 'source' folder wasn't actually deleted, just ensure it exists
  32. mkdir -p ${SOURCES_DIR}
  33.  
  34. # Utility function
  35. # Getting sources of a particular ffmpeg release.
  36. # Same argument (ffmpeg version) produces the same source set.
  37. function ensureSourcesTag() {
  38. FFMPEG_VERSION=$1
  39.  
  40. FFMPEG_SOURCES=${SOURCES_DIR}/ffmpeg-${FFMPEG_VERSION}
  41.  
  42. if [[ ! -d "$FFMPEG_SOURCES" ]]; then
  43. TARGET_FILE_NAME=ffmpeg-${FFMPEG_VERSION}.tar.bz2
  44. TARGET_FILE_PATH=${SOURCES_DIR}/${TARGET_FILE_NAME}
  45.  
  46. curl https://www.ffmpeg.org/releases/${TARGET_FILE_NAME} --output ${TARGET_FILE_PATH}
  47. tar xvjf ${TARGET_FILE_PATH} -C ${SOURCES_DIR}
  48. rm ${TARGET_FILE_PATH}
  49. fi
  50. }
  51.  
  52. # Utility function
  53. # Getting sources of a particular branch of ffmpeg's git repository.
  54. # Same argument (branch name) may produce different source set,
  55. # as the branch in origin repository may be updated in future.
  56. function ensureSourcesBranch() {
  57. BRANCH=$1
  58.  
  59. GIT_DIRECTORY=ffmpeg-git
  60.  
  61. FFMPEG_SOURCES=${SOURCES_DIR}/${GIT_DIRECTORY}
  62.  
  63. cd ${SOURCES_DIR}
  64.  
  65. if [[ ! -d "$FFMPEG_SOURCES" ]]; then
  66. git clone https://git.ffmpeg.org/ffmpeg.git ${GIT_DIRECTORY}
  67. fi
  68.  
  69. cd ${GIT_DIRECTORY}
  70. git checkout $BRANCH
  71. # Forcing the update of a branch
  72. git pull origin $BRANCH
  73.  
  74. # Additional logging to keep track of an exact commit to build
  75. echo "Commit to build:"
  76. git rev-parse HEAD
  77. cd ${BASE_DIR}
  78. }
  79.  
  80. # Utility function
  81. # Test if sources of the FFmpeg exist. If not - download them
  82. function ensureSources() {
  83. TYPE=$1
  84. SECOND_ARGUMENT=$2
  85.  
  86. case $TYPE in
  87. tag)
  88. echo "Using FFmpeg ${SECOND_ARGUMENT}"
  89. ensureSourcesTag ${SECOND_ARGUMENT}
  90. ;;
  91. branch)
  92. echo "Using FFmpeg git repository and its branch ${SECOND_ARGUMENT}"
  93. ensureSourcesBranch ${SECOND_ARGUMENT}
  94. ;;
  95. *)
  96. echo "Using FFmpeg ${FFMPEG_FALLBACK_VERSION}"
  97. ensureSourcesTag ${FFMPEG_FALLBACK_VERSION}
  98. ;;
  99. esac
  100. }
  101.  
  102. # Actual magic of configuring and compiling of FFmpeg for a certain ABIs.
  103. # Supported ABIs are: armeabi-v7a, arm64-v8a, x86 and x86_64
  104. function assemble() {
  105. cd ${FFMPEG_SOURCES}
  106.  
  107. ABI=$1
  108. API_LEVEL=$2
  109.  
  110. TOOLCHAIN_PATH=${ANDROID_NDK_HOME}/toolchains/llvm/prebuilt/${HOST_TAG}
  111. SYSROOT=${TOOLCHAIN_PATH}/sysroot
  112.  
  113. TARGET_TRIPLE_MACHINE_BINUTILS=
  114. TARGET_TRIPLE_MACHINE_CC=
  115. TARGET_TRIPLE_OS="android"
  116.  
  117. case $ABI in
  118. armeabi-v7a)
  119. #cc armv7a-linux-androideabi16-clang
  120. #binutils arm -linux-androideabi -ld
  121. TARGET_TRIPLE_MACHINE_BINUTILS=arm
  122. TARGET_TRIPLE_MACHINE_CC=armv7a
  123. TARGET_TRIPLE_OS=androideabi
  124. ;;
  125. arm64-v8a)
  126. #cc aarch64-linux-android21-clang
  127. #binutils aarch64-linux-android -ld
  128. TARGET_TRIPLE_MACHINE_BINUTILS=aarch64
  129. ;;
  130. x86)
  131. #cc i686-linux-android16-clang
  132. #binutils i686-linux-android -ld
  133. TARGET_TRIPLE_MACHINE_BINUTILS=i686
  134.  
  135. # Disabling assembler optimizations, because they have text relocations
  136. EXTRA_BUILD_CONFIGURATION_FLAGS=--disable-asm
  137. ;;
  138. x86_64)
  139. #cc x86_64-linux-android21-clang
  140. #binutils x86_64-linux-android -ld
  141. TARGET_TRIPLE_MACHINE_BINUTILS=x86_64
  142.  
  143. EXTRA_BUILD_CONFIGURATION_FLAGS=--yasmexe=${TOOLCHAIN_PATH}/bin/yasm
  144. ;;
  145. esac
  146.  
  147. # If the cc-specific variable isn't set, we fallback to binutils version
  148. [ -z "${TARGET_TRIPLE_MACHINE_CC}" ] && TARGET_TRIPLE_MACHINE_CC=${TARGET_TRIPLE_MACHINE_BINUTILS}
  149.  
  150. # Common prefix for ld, as, etc.
  151. CROSS_PREFIX=${TOOLCHAIN_PATH}/bin/${TARGET_TRIPLE_MACHINE_BINUTILS}-linux-${TARGET_TRIPLE_OS}-
  152.  
  153. # The name for compiler is slightly different, so it is defined separatly.
  154. CC=${TOOLCHAIN_PATH}/bin/${TARGET_TRIPLE_MACHINE_CC}-linux-${TARGET_TRIPLE_OS}${API_LEVEL}-clang
  155.  
  156. # Reading a list of video codecs to enable
  157. # DECODERS_TO_ENABLE=
  158. # while IFS= read -r line; do DECODERS_TO_ENABLE="${DECODERS_TO_ENABLE} --enable-decoder=$line"; done < ${BASE_DIR}/video_decoders_list.txt
  159.  
  160. # Everything that goes below ${EXTRA_BUILD_CONFIGURATION_FLAGS} is my project-specific.
  161. # You are free to enable/disable whatever you actually need.
  162. ./configure \
  163. --prefix=${BUILD_DIR}/${ABI} \
  164. --enable-cross-compile \
  165. --target-os=android \
  166. --arch=${TARGET_TRIPLE_MACHINE_BINUTILS} \
  167. --sysroot=${SYSROOT} \
  168. --cross-prefix=${CROSS_PREFIX} \
  169. --cc=${CC} \
  170. --extra-cflags="-O3 -fPIC" \
  171. --extra-cxxflags="-fPIC" \
  172. --enable-shared \
  173. --disable-static \
  174. ${EXTRA_BUILD_CONFIGURATION_FLAGS} \
  175. --disable-runtime-cpudetect \
  176. --disable-programs \
  177. --disable-postproc \
  178. --disable-doc \
  179. --disable-debug \
  180. --disable-avfilter \
  181. --disable-swresample \
  182. --disable-neon \
  183. --disable-indev=v4l2
  184.  
  185. make clean
  186. #make -j8
  187. make
  188. make install
  189.  
  190. # Saving stats about text relocation presence.
  191. # If the result file doesn't have 'TEXTREL' at all, then we are good.
  192. ${CROSS_PREFIX}readelf --dynamic ${BUILD_DIR}/${ABI}/lib/*.so | grep 'TEXTREL\|File' >> ${STATS_DIR}/text-relocations.txt
  193.  
  194. cd ${BASE_DIR}
  195. }
  196.  
  197. # Placing build *.so files into the /bin directory
  198. function installLibs() {
  199. BUILD_SUBDIR=$1
  200.  
  201. OUTPUT_SUBDIR=${OUTPUT_DIR}/lib/${BUILD_SUBDIR}
  202. CP_DIR=${BUILD_DIR}/${BUILD_SUBDIR}
  203.  
  204. mkdir -p ${OUTPUT_SUBDIR}
  205. cp ${CP_DIR}/lib/*.so ${OUTPUT_SUBDIR}
  206. }
  207.  
  208. function build() {
  209. ABI=$1
  210. ANDROID_API=$2
  211.  
  212. assemble ${ABI} ${ANDROID_API}
  213. installLibs ${ABI}
  214. }
  215.  
  216. # Placing build header files into the /bin directory.
  217. # Note, there is a only one such a folder since this headers are the same for all ABIs.
  218. # May not be true for different configurations though.
  219. function installHeaders() {
  220. cd ${BUILD_DIR}
  221. cd "$(ls -1 | head -n1)"
  222. cp -r include ${OUTPUT_DIR}
  223. cd ${BASE_DIR}
  224. }
  225.  
  226. # Actual work
  227.  
  228. ensureSources $1 $2
  229.  
  230. #build arm64-v8a 21
  231. #build armeabi-v7a 16
  232. #build x86_64 21
  233. build x86 16
  234.  
  235. installHeaders
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement