version.sh 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #!/usr/bin/env bash
  2. # tools/version.sh
  3. #
  4. # Licensed to the Apache Software Foundation (ASF) under one or more
  5. # contributor license agreements. See the NOTICE file distributed with
  6. # this work for additional information regarding copyright ownership. The
  7. # ASF licenses this file to you under the Apache License, Version 2.0 (the
  8. # "License"); you may not use this file except in compliance with the
  9. # License. You may obtain a copy of the License at
  10. #
  11. # http://www.apache.org/licenses/LICENSE-2.0
  12. #
  13. # Unless required by applicable law or agreed to in writing, software
  14. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  16. # License for the specific language governing permissions and limitations
  17. # under the License.
  18. #
  19. WD=$(dirname $0)
  20. # Get command line parameters
  21. USAGE="USAGE: $0 [-d|-h] [-b <build>] [-v <major.minor.patch>] <outfile-path>"
  22. ADVICE="Try '$0 -h' for more information"
  23. unset VERSION
  24. unset BUILD
  25. unset OUTFILE
  26. while [ ! -z "$1" ]; do
  27. case $1 in
  28. -b )
  29. shift
  30. BUILD=$1
  31. ;;
  32. -d )
  33. set -x
  34. ;;
  35. -v )
  36. shift
  37. VERSION=$1
  38. ;;
  39. -h )
  40. echo "$0 is a tool for generation of proper version files for the NuttX build"
  41. echo ""
  42. echo $USAGE
  43. echo ""
  44. echo "Where:"
  45. echo " -b <build>"
  46. echo " Use this build identification string. Default: use GIT build ID"
  47. echo " NOTE: GIT build information may not be available in a snapshot"
  48. echo " -d"
  49. echo " Enable script debug"
  50. echo " -h"
  51. echo " show this help message and exit"
  52. echo " -v <major.minor.patch>"
  53. echo " The NuttX version number expressed as a major, minor and patch"
  54. echo " number separated by a period"
  55. echo " <outfile-path>"
  56. echo " The full path to the version file to be created"
  57. exit 0
  58. ;;
  59. * )
  60. break
  61. ;;
  62. esac
  63. shift
  64. done
  65. OUTFILE=$1
  66. if [ -z ${VERSION} ] ; then
  67. VERSION=`git -C ${WD} describe --match "nuttx-*" 2>/dev/null | tail -1 | cut -d'-' -f2`
  68. # If the VERSION does not match X.Y.Z, retrieve version from the tag
  69. if [[ ! ${VERSION} =~ ([0-9]+)\.([0-9]+)\.([0-9]+) ]] ; then
  70. VERSION=`git -C ${WD} -c 'versionsort.suffix=-' tag --sort=v:refname | grep -E "nuttx-[0-9]+\.[0-9]+\.[0-9]+" | tail -1 | cut -d'-' -f2-`
  71. fi
  72. fi
  73. # Make sure we know what is going on
  74. if [ -z ${VERSION} ] ; then
  75. echo "Missing versioning information. Using the dummy value. (0.0.0)"
  76. VERSION="0.0.0"
  77. fi
  78. if [ -z ${OUTFILE} ] ; then
  79. echo "Missing path to the output file"
  80. echo $USAGE
  81. echo $ADVICE
  82. exit 2
  83. fi
  84. # Get the major, minor and patch version numbers
  85. MAJOR=`echo ${VERSION} | cut -d'.' -f1`
  86. if [ "X${MAJOR}" = "X${VERSION}" ]; then
  87. echo "Missing minor version number"
  88. echo $USAGE
  89. echo $ADVICE
  90. exit 3
  91. fi
  92. MINOR=`echo ${VERSION} | cut -d'.' -f2`
  93. if [ "X${MAJOR}.${MINOR}" = "X${VERSION}" ]; then
  94. echo "Missing patch version number"
  95. echo $USAGE
  96. echo $ADVICE
  97. exit 4
  98. fi
  99. PATCH=`echo ${VERSION} | grep -Eo "[0-9]+\.[0-9]+\.[0-9]+" | cut -d'.' -f3`
  100. # Get GIT information (if not provided on the command line)
  101. if [ -z "${BUILD}" ]; then
  102. BUILD=`git -C ${WD} log --oneline -1 | cut -d' ' -f1 2>/dev/null`
  103. if [ -z "${BUILD}" ]; then
  104. echo "GIT version information is not available"
  105. exit 5
  106. fi
  107. if [ -n "`git -C ${WD} diff-index --name-only HEAD | head -1`" ]; then
  108. BUILD=${BUILD}-dirty
  109. fi
  110. fi
  111. # Write a version file into the NuttX directory. The syntax of file is such that it
  112. # may be sourced by a bash script or included by a Makefile.
  113. echo "#!/bin/bash" >${OUTFILE}
  114. echo "" >>${OUTFILE}
  115. echo "CONFIG_VERSION_STRING=\"${VERSION}\"" >>${OUTFILE}
  116. echo "CONFIG_VERSION_MAJOR=${MAJOR}" >>${OUTFILE}
  117. echo "CONFIG_VERSION_MINOR=${MINOR}" >>${OUTFILE}
  118. echo "CONFIG_VERSION_PATCH=${PATCH}" >>${OUTFILE}
  119. echo "CONFIG_VERSION_BUILD=\"${BUILD}\"" >>${OUTFILE}