package.sh 6.6 KB

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