package.sh 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. #!/bin/bash
  2. echo "通用打包脚本,--help查看参数"
  3. # Usage:
  4. # bash package.sh [--help]
  5. # option arm, whether build for arm, requires ubuntu12.
  6. # user can config the following configs, then package.
  7. INSTALL=/usr/local/srs
  8. # whether build for arm, only for ubuntu12.
  9. help=NO
  10. X86_X64=NO
  11. ARM=NO
  12. PI=NO
  13. MIPS=NO
  14. #
  15. EMBEDED=NO
  16. JOBS=1
  17. #
  18. SRS_TAG=
  19. ##################################################################################
  20. ##################################################################################
  21. ##################################################################################
  22. # parse options.
  23. for option
  24. do
  25. case "$option" in
  26. -*=*)
  27. value=`echo "$option" | sed -e 's|[-_a-zA-Z0-9/]*=||'`
  28. option=`echo "$option" | awk -F '=' '{print $1}'`
  29. ;;
  30. *) value="" ;;
  31. esac
  32. case "$option" in
  33. -h) help=yes ;;
  34. --help) help=yes ;;
  35. --x86-x64) X86_X64=YES ;;
  36. --x86-64) X86_X64=YES ;;
  37. --mips) MIPS=YES ;;
  38. --arm) ARM=YES ;;
  39. --pi) PI=YES ;;
  40. --jobs) JOBS=$value ;;
  41. --tag) SRS_TAG=$value ;;
  42. *)
  43. echo "$0: error: invalid option \"$option\", @see $0 --help"
  44. exit 1
  45. ;;
  46. esac
  47. done
  48. if [ $help = yes ]; then
  49. cat << END
  50. --help Print this message
  51. --x86-x64 For x86-x64 platform, configure/make/package.
  52. --x86-64 Alias for --x86-x64.
  53. --arm For arm cross-build platform, configure/make/package.
  54. --mips For mips cross-build platform, configure/make/package.
  55. --pi For pi platform, configure/make/package.
  56. --jobs Set the configure and make jobs.
  57. --tag Set the version in zip file.
  58. END
  59. exit 0
  60. fi
  61. # embeded(arm/mips)
  62. if [ $ARM = YES ]; then EMBEDED=YES; fi
  63. if [ $MIPS = YES ]; then EMBEDED=YES; fi
  64. # discover the current work dir, the log and access.
  65. echo "argv[0]=$0"
  66. if [[ ! -f $0 ]]; then
  67. echo "directly execute the scripts on shell.";
  68. work_dir=`pwd`
  69. else
  70. echo "execute scripts in file: $0";
  71. work_dir=`dirname $0`; work_dir=`(cd ${work_dir} && pwd)`
  72. fi
  73. work_dir=`(cd ${work_dir}/.. && pwd)`
  74. product_dir=$work_dir
  75. build_objs=${work_dir}/objs
  76. package_dir=${build_objs}/package
  77. log="${build_objs}/logs/package.`date +%s`.log" && . ${product_dir}/scripts/_log.sh && check_log
  78. ret=$?; if [[ $ret -ne 0 ]]; then exit $ret; fi
  79. # check lsb_release
  80. lsb_release -a >/dev/null 2>&1
  81. ret=$?; if [[ $ret -ne 0 ]]; then
  82. failed_msg "lsb_release not found. "
  83. failed_msg "to install on centos/debian(ubuntu/respberry-pi):";
  84. failed_msg " sudo yum install -y lsb-release";
  85. failed_msg " sudo aptitude install -y lsb-release";
  86. failed_msg "or centos7:"
  87. failed_msg " sudo yum install -y redhat-lsb"
  88. exit $ret;
  89. fi
  90. # check os version
  91. os_name=`lsb_release --id|awk '{print $3}'` &&
  92. os_release=`lsb_release --release|awk '{print $2}'` &&
  93. os_major_version=`echo $os_release|awk -F '.' '{print $1}'` &&
  94. os_machine=`uname -i`
  95. ret=$?; if [[ $ret -ne 0 ]]; then failed_msg "lsb_release get os info failed."; exit $ret; fi
  96. ok_msg "target os is ${os_name}-${os_major_version} ${os_release} ${os_machine}"
  97. # for raspberry-pi
  98. # use rasberry-pi instead all release
  99. if [ $PI = YES ]; then
  100. uname -a|grep "raspberrypi"; if [[ 0 -eq $? ]]; then os_name="RaspberryPi"; fi
  101. if [[ "Raspbian" == $os_name ]]; then os_name="RaspberryPi"; fi
  102. # check the cpu machine
  103. if [[ "unknown" == $os_machine ]]; then os_machine=`uname -m`; fi
  104. fi
  105. ok_msg "real os is ${os_name}-${os_major_version} ${os_release} ${os_machine}"
  106. # build srs
  107. # @see https://github.com/ossrs/srs/wiki/v1_CN_Build
  108. ok_msg "start build srs, ARM: $ARM, MIPS: $MIPS, PI: $PI, X86_64: $X86_X64, JOBS: $JOBS, TAG: $SRS_TAG"
  109. if [ $ARM = YES ]; then
  110. (
  111. cd $work_dir &&
  112. ./configure --arm --jobs=$JOBS --prefix=$INSTALL --build-tag=${os_name}${os_major_version} && make
  113. ) >> $log 2>&1
  114. elif [ $MIPS = YES ]; then
  115. (
  116. cd $work_dir &&
  117. ./configure --mips --jobs=$JOBS --prefix=$INSTALL --build-tag=${os_name}${os_major_version} && make
  118. ) >> $log 2>&1
  119. elif [ $PI = YES ]; then
  120. (
  121. cd $work_dir &&
  122. ./configure --pi --jobs=$JOBS --prefix=$INSTALL --build-tag=${os_name}${os_major_version} && make
  123. ) >> $log 2>&1
  124. elif [ $X86_X64 = YES ]; then
  125. (
  126. cd $work_dir &&
  127. ./configure --x86-x64 --jobs=$JOBS --prefix=$INSTALL --build-tag=${os_name}${os_major_version} && make
  128. ) >> $log 2>&1
  129. else
  130. failed_msg "invalid option, must be --x86-x64/--arm/--mips/--pi, see --help"; exit 1;
  131. fi
  132. ret=$?; if [[ 0 -ne ${ret} ]]; then failed_msg "build srs failed"; exit $ret; fi
  133. ok_msg "build srs success"
  134. # install srs
  135. ok_msg "start install srs"
  136. (
  137. cd $work_dir && rm -rf $package_dir && make DESTDIR=$package_dir install
  138. ) >> $log 2>&1
  139. ret=$?; if [[ 0 -ne ${ret} ]]; then failed_msg "install srs failed"; exit $ret; fi
  140. ok_msg "install srs success"
  141. # copy extra files to package.
  142. ok_msg "start copy extra files to package"
  143. (
  144. cp $work_dir/scripts/install.sh $package_dir/INSTALL &&
  145. sed -i "s|^INSTALL=.*|INSTALL=${INSTALL}|g" $package_dir/INSTALL &&
  146. mkdir -p $package_dir/scripts &&
  147. cp $work_dir/scripts/_log.sh $package_dir/scripts/_log.sh &&
  148. chmod +x $package_dir/INSTALL
  149. ) >> $log 2>&1
  150. ret=$?; if [[ 0 -ne ${ret} ]]; then failed_msg "copy extra files failed"; exit $ret; fi
  151. ok_msg "copy extra files success"
  152. # detect for arm.
  153. if [ $ARM = YES ]; then
  154. arm_cpu=`arm-linux-gnueabi-readelf --arch-specific ${build_objs}/srs|grep Tag_CPU_arch:|awk '{print $2}'`
  155. os_machine=arm${arm_cpu}cpu
  156. fi
  157. if [ $MIPS = YES ]; then
  158. os_machine=mips
  159. fi
  160. ok_msg "machine: $os_machine"
  161. # generate zip dir and zip filename
  162. srs_version=$SRS_TAG
  163. if [[ $srs_version == '' ]]; then
  164. if [ $EMBEDED = YES ]; then
  165. srs_version_major=`cat $work_dir/src/core/srs_core.hpp| grep '#define VERSION_MAJOR'| awk '{print $3}'|xargs echo` &&
  166. srs_version_minor=`cat $work_dir/src/core/srs_core.hpp| grep '#define VERSION_MINOR'| awk '{print $3}'|xargs echo` &&
  167. srs_version_revision=`cat $work_dir/src/core/srs_core.hpp| grep '#define VERSION_REVISION'| awk '{print $3}'|xargs echo` &&
  168. srs_version=$srs_version_major.$srs_version_minor.$srs_version_revision
  169. else
  170. srs_version=`${build_objs}/srs -v 2>/dev/stdout 1>/dev/null`
  171. fi
  172. ret=$?; if [[ 0 -ne ${ret} ]]; then failed_msg "get srs version failed"; exit $ret; fi
  173. fi
  174. ok_msg "get srs version $srs_version"
  175. zip_dir="SRS-${os_name}${os_major_version}-${os_machine}-${srs_version}"
  176. ret=$?; if [[ 0 -ne ${ret} ]]; then failed_msg "generate zip filename failed"; exit $ret; fi
  177. ok_msg "target zip filename $zip_dir"
  178. # zip package.
  179. ok_msg "start zip package"
  180. (
  181. mv $package_dir ${build_objs}/${zip_dir} &&
  182. cd ${build_objs} && rm -rf ${zip_dir}.zip && zip -q -r ${zip_dir}.zip ${zip_dir} &&
  183. mv ${build_objs}/${zip_dir} $package_dir
  184. ) >> $log 2>&1
  185. ret=$?; if [[ 0 -ne ${ret} ]]; then failed_msg "zip package failed"; exit $ret; fi
  186. ok_msg "zip package success"
  187. ok_msg "srs package success"
  188. echo ""
  189. echo "package: ${build_objs}/${zip_dir}.zip"
  190. echo "install:"
  191. echo " unzip -q ${zip_dir}.zip &&"
  192. echo " cd ${zip_dir} &&"
  193. echo " sudo bash INSTALL"
  194. exit 0