You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

113 lines
3.3 KiB

  1. #!/usr/bin/env bash
  2. set -ex
  3. g_script_path=$(dirname $(readlink -e $0))
  4. g_toplevel_path=$(pushd $g_script_path > /dev/null 2>&1; \
  5. git rev-parse --show-toplevel; \
  6. popd > /dev/null 2>&1)
  7. g_version=$(git tag -l v* | sort --version-sort | tail -1)
  8. g_version=${g_version#"v"}
  9. g_format="tar.gz"
  10. g_name="shadowsocks-libev"
  11. g_rpmbuild_topdir="${g_toplevel_path}/rpm"
  12. g_rpmbuild_conditions=
  13. show_help()
  14. {
  15. echo -e "`basename $0` [option] [argument]"
  16. echo
  17. echo -e "Options:"
  18. echo -e " -h show this help."
  19. echo -e " -v with argument version (${g_version} by default)."
  20. echo -e " -f with argument format (tar.gz by default) used by git archive."
  21. echo
  22. echo -e "Examples:"
  23. echo -e " to build base on version \`2.4.1' with format \`tar.xz', run:"
  24. echo -e " `basename $0` -f tar.xz -v 2.4.1"
  25. }
  26. version_greater_equal()
  27. {
  28. [ "$1" = $(printf "$1\n$2\n" | sort --version-sort | tail -1) ]
  29. }
  30. verify_options()
  31. {
  32. local completion_min_verion="2.6.0"
  33. local archive_format_supported_max_version="2.6.2"
  34. version_greater_equal ${g_version} ${completion_min_verion} \
  35. && g_rpmbuild_conditions="${g_rpmbuild_conditions} --with completion" || :
  36. if ! version_greater_equal ${archive_format_supported_max_version} ${g_version}; then
  37. g_rpmbuild_conditions="${g_rpmbuild_conditions} --with autogen"
  38. if [ "${g_format}" != "tar" ]; then
  39. echo -e "version(${g_version}) greater than ${archive_format_supported_max_version} can only use archive format \`tar'."
  40. echo -e "change format from \`${g_format}' to \`tar'"
  41. g_format="tar"
  42. fi
  43. fi
  44. }
  45. generate_tarball()
  46. {
  47. local tarball_name="${g_name}-${g_version}"
  48. local tarball_dir="${g_rpmbuild_topdir}/SOURCES"
  49. pushd ${g_toplevel_path}
  50. git archive "v${g_version}" \
  51. --format="${g_format}" \
  52. --prefix="${tarball_name}/" \
  53. -o "${tarball_dir}/${tarball_name}.${g_format}"
  54. git ls-tree -dr "v${g_version}" | grep commit \
  55. | while read eat_mod eat_type mod_sha mod_path; do \
  56. [ "${mod_path}" = "" ] && continue || :; \
  57. (pushd ${mod_path=} \
  58. && git archive ${mod_sha} \
  59. --prefix="${tarball_name}/${mod_path}/" \
  60. -o "${tarball_dir}/sub_mod.tar" \
  61. && tar --concatenate "${tarball_dir}/sub_mod.tar" \
  62. --file="${tarball_dir}/${tarball_name}.tar" \
  63. && rm "${tarball_dir}/sub_mod.tar" \
  64. && popd) \
  65. done
  66. }
  67. while getopts "hv:f:" opt
  68. do
  69. case ${opt} in
  70. h)
  71. show_help
  72. exit 0
  73. ;;
  74. v)
  75. if [ "${OPTARG}" = v* ]; then
  76. g_version=${OPTARG#"v"}
  77. else
  78. g_version=${OPTARG}
  79. fi
  80. ;;
  81. f)
  82. g_format=${OPTARG}
  83. ;;
  84. *)
  85. exit 1
  86. ;;
  87. esac
  88. done
  89. verify_options
  90. generate_tarball
  91. spec_path="${g_rpmbuild_topdir}/SPECS/shadowsocks-libev.spec"
  92. sed -e "s/^\(Version: \).*$/\1${g_version}/" \
  93. -e "s/^\(Source0: \).*$/\1${g_name}-${g_version}.${g_format}/" \
  94. "${spec_path}".in > "${spec_path}"
  95. rpmbuild -bb ${spec_path} \
  96. --define "%_topdir ${g_rpmbuild_topdir}" \
  97. ${g_rpmbuild_conditions} \