version.sh 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #!/bin/sh
  2. ##
  3. ## Copyright (c) 2010 The WebM project authors. All Rights Reserved.
  4. ##
  5. ## Use of this source code is governed by a BSD-style license
  6. ## that can be found in the LICENSE file in the root of the source
  7. ## tree. An additional intellectual property rights grant can be found
  8. ## in the file PATENTS. All contributing project authors may
  9. ## be found in the AUTHORS file in the root of the source tree.
  10. ##
  11. for opt in "$@"; do
  12. optval="${opt#*=}"
  13. case "$opt" in
  14. --bare) bare=true ;;
  15. *) break ;;
  16. esac
  17. shift
  18. done
  19. source_path=${1:-.}
  20. out_file=${2}
  21. id=${3:-VERSION_STRING}
  22. git_version_id=""
  23. if [ -e "${source_path}/.git" ]; then
  24. # Source Path is a git working copy. Check for local modifications.
  25. # Note that git submodules may have a file as .git, not a directory.
  26. export GIT_DIR="${source_path}/.git"
  27. git_version_id=`git describe --match=v[0-9]* 2>/dev/null`
  28. fi
  29. changelog_version=""
  30. for p in "${source_path}" "${source_path}/.."; do
  31. if [ -z "$git_version_id" -a -f "${p}/CHANGELOG" ]; then
  32. changelog_version=`head -n1 "${p}/CHANGELOG" | awk '{print $2}'`
  33. changelog_version="${changelog_version}"
  34. break
  35. fi
  36. done
  37. version_str="${changelog_version}${git_version_id}"
  38. bare_version=${version_str#v}
  39. major_version=${bare_version%%.*}
  40. bare_version=${bare_version#*.}
  41. minor_version=${bare_version%%.*}
  42. bare_version=${bare_version#*.}
  43. patch_version=${bare_version%%-*}
  44. bare_version=${bare_version#${patch_version}}
  45. extra_version=${bare_version##-}
  46. #since they'll be used as integers below make sure they are or force to 0
  47. for v in major_version minor_version patch_version; do
  48. if eval echo \$$v |grep -E -q '[^[:digit:]]'; then
  49. eval $v=0
  50. fi
  51. done
  52. if [ ${bare} ]; then
  53. echo "${changelog_version}${git_version_id}" > $$.tmp
  54. else
  55. cat<<EOF>$$.tmp
  56. // This file is generated. Do not edit.
  57. #define VERSION_MAJOR $major_version
  58. #define VERSION_MINOR $minor_version
  59. #define VERSION_PATCH $patch_version
  60. #define VERSION_EXTRA "$extra_version"
  61. #define VERSION_PACKED ((VERSION_MAJOR<<16)|(VERSION_MINOR<<8)|(VERSION_PATCH))
  62. #define ${id}_NOSP "${version_str}"
  63. #define ${id} " ${version_str}"
  64. EOF
  65. fi
  66. if [ -n "$out_file" ]; then
  67. diff $$.tmp ${out_file} >/dev/null 2>&1 || cat $$.tmp > ${out_file}
  68. else
  69. cat $$.tmp
  70. fi
  71. rm $$.tmp