Advertisement
Guest User

Untitled

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