do-compile-ffmpeg.sh 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. #! /usr/bin/env bash
  2. #
  3. # Copyright (C) 2013-2014 Bilibili
  4. # Copyright (C) 2013-2014 Zhang Rui <bbcallen@gmail.com>
  5. #
  6. # Licensed under the Apache License, Version 2.0 (the "License");
  7. # you may not use this file except in compliance with the License.
  8. # You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS,
  14. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17. #
  18. # This script is based on projects below
  19. # https://github.com/kolyvan/kxmovie
  20. # https://github.com/yixia/FFmpeg-Android
  21. # http://git.videolan.org/?p=vlc-ports/android.git;a=summary
  22. # https://github.com/kewlbear/FFmpeg-iOS-build-script/
  23. #--------------------
  24. echo "===================="
  25. echo "[*] check host"
  26. echo "===================="
  27. set -e
  28. #--------------------
  29. # include
  30. #--------------------
  31. # common defines
  32. FF_ARCH=$1
  33. FF_BUILD_OPT=$2
  34. echo "FF_ARCH=$FF_ARCH"
  35. echo "FF_BUILD_OPT=$FF_BUILD_OPT"
  36. if [ -z "$FF_ARCH" ]; then
  37. echo "You must specific an architecture 'armv7, armv7s, arm64, i386, x86_64, ...'.\n"
  38. exit 1
  39. fi
  40. FF_BUILD_ROOT=`pwd`
  41. FF_TAGET_OS="darwin"
  42. # ffmpeg build params
  43. export COMMON_FF_CFG_FLAGS=
  44. source $FF_BUILD_ROOT/../config/module.sh
  45. FFMPEG_CFG_FLAGS=
  46. FFMPEG_CFG_FLAGS="$FFMPEG_CFG_FLAGS $COMMON_FF_CFG_FLAGS"
  47. # Optimization options (experts only):
  48. # FFMPEG_CFG_FLAGS="$FFMPEG_CFG_FLAGS --disable-armv5te"
  49. # FFMPEG_CFG_FLAGS="$FFMPEG_CFG_FLAGS --disable-armv6"
  50. # FFMPEG_CFG_FLAGS="$FFMPEG_CFG_FLAGS --disable-armv6t2"
  51. # Advanced options (experts only):
  52. FFMPEG_CFG_FLAGS="$FFMPEG_CFG_FLAGS --enable-cross-compile"
  53. # --disable-symver may indicate a bug
  54. # FFMPEG_CFG_FLAGS="$FFMPEG_CFG_FLAGS --disable-symver"
  55. # Developer options (useful when working on FFmpeg itself):
  56. FFMPEG_CFG_FLAGS="$FFMPEG_CFG_FLAGS --disable-stripping"
  57. ##
  58. FFMPEG_CFG_FLAGS="$FFMPEG_CFG_FLAGS --arch=$FF_ARCH"
  59. FFMPEG_CFG_FLAGS="$FFMPEG_CFG_FLAGS --target-os=$FF_TAGET_OS"
  60. FFMPEG_CFG_FLAGS="$FFMPEG_CFG_FLAGS --enable-static"
  61. FFMPEG_CFG_FLAGS="$FFMPEG_CFG_FLAGS --disable-shared"
  62. FFMPEG_EXTRA_CFLAGS=
  63. # i386, x86_64
  64. FFMPEG_CFG_FLAGS_SIMULATOR=
  65. FFMPEG_CFG_FLAGS_SIMULATOR="$FFMPEG_CFG_FLAGS_SIMULATOR --disable-asm"
  66. FFMPEG_CFG_FLAGS_SIMULATOR="$FFMPEG_CFG_FLAGS_SIMULATOR --disable-mmx"
  67. FFMPEG_CFG_FLAGS_SIMULATOR="$FFMPEG_CFG_FLAGS_SIMULATOR --assert-level=2"
  68. # armv7, armv7s, arm64
  69. FFMPEG_CFG_FLAGS_ARM=
  70. FFMPEG_CFG_FLAGS_ARM="$FFMPEG_CFG_FLAGS_ARM --enable-pic"
  71. FFMPEG_CFG_FLAGS_ARM="$FFMPEG_CFG_FLAGS_ARM --enable-neon"
  72. FFMPEG_CFG_FLAGS_ARM="$FFMPEG_CFG_FLAGS_ARM --disable-asm"
  73. case "$FF_BUILD_OPT" in
  74. debug)
  75. FFMPEG_CFG_FLAGS_ARM="$FFMPEG_CFG_FLAGS_ARM --disable-optimizations"
  76. FFMPEG_CFG_FLAGS_ARM="$FFMPEG_CFG_FLAGS_ARM --enable-debug"
  77. FFMPEG_CFG_FLAGS_ARM="$FFMPEG_CFG_FLAGS_ARM --disable-small"
  78. ;;
  79. *)
  80. FFMPEG_CFG_FLAGS_ARM="$FFMPEG_CFG_FLAGS_ARM --enable-optimizations"
  81. FFMPEG_CFG_FLAGS_ARM="$FFMPEG_CFG_FLAGS_ARM --enable-debug"
  82. FFMPEG_CFG_FLAGS_ARM="$FFMPEG_CFG_FLAGS_ARM --enable-small"
  83. ;;
  84. esac
  85. echo "build_root: $FF_BUILD_ROOT"
  86. #--------------------
  87. echo "===================="
  88. echo "[*] check gas-preprocessor"
  89. echo "===================="
  90. FF_TOOLS_ROOT="$FF_BUILD_ROOT/../extra"
  91. export PATH="$FF_TOOLS_ROOT/gas-preprocessor:$PATH"
  92. echo "gasp: $FF_TOOLS_ROOT/gas-preprocessor/gas-preprocessor.pl"
  93. #--------------------
  94. echo "===================="
  95. echo "[*] config arch $FF_ARCH"
  96. echo "===================="
  97. FF_BUILD_NAME="unknown"
  98. FF_XCRUN_PLATFORM="iPhoneOS"
  99. FF_XCRUN_OSVERSION=
  100. FF_GASPP_EXPORT=
  101. FF_DEP_OPENSSL_INC=
  102. FF_DEP_OPENSSL_LIB=
  103. FF_XCODE_BITCODE=
  104. if [ "$FF_ARCH" = "i386" ]; then
  105. FF_BUILD_NAME="ffmpeg-i386"
  106. FF_BUILD_NAME_OPENSSL=openssl-i386
  107. FF_XCRUN_PLATFORM="iPhoneSimulator"
  108. FF_XCRUN_OSVERSION="-mios-simulator-version-min=6.0"
  109. FFMPEG_CFG_FLAGS="$FFMPEG_CFG_FLAGS $FFMPEG_CFG_FLAGS_SIMULATOR"
  110. elif [ "$FF_ARCH" = "x86_64" ]; then
  111. FF_BUILD_NAME="ffmpeg-x86_64"
  112. FF_BUILD_NAME_OPENSSL=openssl-x86_64
  113. FF_XCRUN_PLATFORM="iPhoneSimulator"
  114. FF_XCRUN_OSVERSION="-mios-simulator-version-min=7.0"
  115. FFMPEG_CFG_FLAGS="$FFMPEG_CFG_FLAGS $FFMPEG_CFG_FLAGS_SIMULATOR"
  116. elif [ "$FF_ARCH" = "armv7" ]; then
  117. FF_BUILD_NAME="ffmpeg-armv7"
  118. FF_BUILD_NAME_OPENSSL=openssl-armv7
  119. FF_XCRUN_OSVERSION="-miphoneos-version-min=6.0"
  120. FF_XCODE_BITCODE="-fembed-bitcode"
  121. FFMPEG_CFG_FLAGS="$FFMPEG_CFG_FLAGS $FFMPEG_CFG_FLAGS_ARM"
  122. # FFMPEG_CFG_CPU="--cpu=cortex-a8"
  123. elif [ "$FF_ARCH" = "armv7s" ]; then
  124. FF_BUILD_NAME="ffmpeg-armv7s"
  125. FF_BUILD_NAME_OPENSSL=openssl-armv7s
  126. FFMPEG_CFG_CPU="--cpu=swift"
  127. FF_XCRUN_OSVERSION="-miphoneos-version-min=6.0"
  128. FF_XCODE_BITCODE="-fembed-bitcode"
  129. FFMPEG_CFG_FLAGS="$FFMPEG_CFG_FLAGS $FFMPEG_CFG_FLAGS_ARM"
  130. elif [ "$FF_ARCH" = "arm64" ]; then
  131. FF_BUILD_NAME="ffmpeg-arm64"
  132. FF_BUILD_NAME_OPENSSL=openssl-arm64
  133. FF_XCRUN_OSVERSION="-miphoneos-version-min=7.0"
  134. FF_XCODE_BITCODE="-fembed-bitcode"
  135. FFMPEG_CFG_FLAGS="$FFMPEG_CFG_FLAGS $FFMPEG_CFG_FLAGS_ARM"
  136. FF_GASPP_EXPORT="GASPP_FIX_XCODE5=1"
  137. else
  138. echo "unknown architecture $FF_ARCH";
  139. exit 1
  140. fi
  141. echo "build_name: $FF_BUILD_NAME"
  142. echo "platform: $FF_XCRUN_PLATFORM"
  143. echo "osversion: $FF_XCRUN_OSVERSION"
  144. #--------------------
  145. echo "===================="
  146. echo "[*] make ios toolchain $FF_BUILD_NAME"
  147. echo "===================="
  148. FF_BUILD_SOURCE="$FF_BUILD_ROOT/$FF_BUILD_NAME"
  149. FF_BUILD_PREFIX="$FF_BUILD_ROOT/build/$FF_BUILD_NAME/output"
  150. FFMPEG_CFG_FLAGS="$FFMPEG_CFG_FLAGS --prefix=$FF_BUILD_PREFIX"
  151. mkdir -p $FF_BUILD_PREFIX
  152. echo "build_source: $FF_BUILD_SOURCE"
  153. echo "build_prefix: $FF_BUILD_PREFIX"
  154. #--------------------
  155. echo "\n--------------------"
  156. echo "[*] configurate ffmpeg"
  157. echo "--------------------"
  158. FF_XCRUN_SDK=`echo $FF_XCRUN_PLATFORM | tr '[:upper:]' '[:lower:]'`
  159. FF_XCRUN_CC="xcrun -sdk $FF_XCRUN_SDK clang"
  160. FFMPEG_CFG_FLAGS="$FFMPEG_CFG_FLAGS $FFMPEG_CFG_CPU"
  161. FFMPEG_CFLAGS=
  162. FFMPEG_CFLAGS="$FFMPEG_CFLAGS -arch $FF_ARCH"
  163. FFMPEG_CFLAGS="$FFMPEG_CFLAGS $FF_XCRUN_OSVERSION"
  164. FFMPEG_CFLAGS="$FFMPEG_CFLAGS $FFMPEG_EXTRA_CFLAGS"
  165. FFMPEG_CFLAGS="$FFMPEG_CFLAGS $FF_XCODE_BITCODE"
  166. FFMPEG_LDFLAGS="$FFMPEG_CFLAGS"
  167. FFMPEG_DEP_LIBS=
  168. #--------------------
  169. echo "\n--------------------"
  170. echo "[*] check OpenSSL"
  171. echo "----------------------"
  172. FFMPEG_DEP_OPENSSL_INC=$FF_BUILD_ROOT/build/$FF_BUILD_NAME_OPENSSL/output/include
  173. FFMPEG_DEP_OPENSSL_LIB=$FF_BUILD_ROOT/build/$FF_BUILD_NAME_OPENSSL/output/lib
  174. #--------------------
  175. # with openssl
  176. if [ -f "${FFMPEG_DEP_OPENSSL_LIB}/libssl.a" ]; then
  177. FFMPEG_CFG_FLAGS="$FFMPEG_CFG_FLAGS --enable-openssl"
  178. FFMPEG_CFLAGS="$FFMPEG_CFLAGS -I${FFMPEG_DEP_OPENSSL_INC}"
  179. FFMPEG_DEP_LIBS="$FFMPEG_CFLAGS -L${FFMPEG_DEP_OPENSSL_LIB} -lssl -lcrypto"
  180. fi
  181. #--------------------
  182. echo "\n--------------------"
  183. echo "[*] configure"
  184. echo "----------------------"
  185. if [ ! -d $FF_BUILD_SOURCE ]; then
  186. echo ""
  187. echo "!! ERROR"
  188. echo "!! Can not find FFmpeg directory for $FF_BUILD_NAME"
  189. echo "!! Run 'sh init-ios.sh' first"
  190. echo ""
  191. exit 1
  192. fi
  193. # xcode configuration
  194. export DEBUG_INFORMATION_FORMAT=dwarf-with-dsym
  195. cd $FF_BUILD_SOURCE
  196. if [ -f "./config.h" ]; then
  197. echo 'reuse configure'
  198. else
  199. echo "config: $FFMPEG_CFG_FLAGS $FF_XCRUN_CC"
  200. ./configure \
  201. $FFMPEG_CFG_FLAGS \
  202. --cc="$FF_XCRUN_CC" \
  203. $FFMPEG_CFG_CPU \
  204. --extra-cflags="$FFMPEG_CFLAGS" \
  205. --extra-cxxflags="$FFMPEG_CFLAGS" \
  206. --extra-ldflags="$FFMPEG_LDFLAGS $FFMPEG_DEP_LIBS"
  207. make clean
  208. fi
  209. #--------------------
  210. echo "\n--------------------"
  211. echo "[*] compile ffmpeg"
  212. echo "--------------------"
  213. cp config.* $FF_BUILD_PREFIX
  214. make -j3 $FF_GASPP_EXPORT
  215. make install
  216. mkdir -p $FF_BUILD_PREFIX/include/libffmpeg
  217. cp -f config.h $FF_BUILD_PREFIX/include/libffmpeg/config.h