Merge pull request #117 from riscv/multicore_debug
[riscv-isa-sim.git] / scripts / vcs-version.sh
1 #!/bin/bash
2 #=========================================================================
3 # vcs-version.sh [options] [src-dir]
4 #=========================================================================
5 #
6 # -h Display this message
7 # -v Verbose mode
8 #
9 # This script will create a version string by querying a version control
10 # system. The string is appropriate for use in installations and
11 # distributions. Currently this script assumes we are using git as our
12 # version control system but it would be possible to check and see if we
13 # are using an alternative version control system and create a version
14 # string appropriately.
15 #
16 # The script uses git describe plus a few other git commands to create a
17 # version strings in the following format:
18 #
19 # X.Y[-Z-gN][-dirty]
20 #
21 # where X is the major release, Y is the minor release, Z is the number
22 # of commits since the X.Y release, N is an eight digit abbreviated SHA
23 # hash of the most recent commit and the dirty suffix is appended when
24 # the working directory used to create the installation or distribution
25 # is not a pristine checkout. Here are some example version strings:
26 #
27 # 0.0 : initial import
28 # 0.0-3-g99ef6933 : 3rd commit since initial import (N=99ef6933)
29 # 1.0 : release 1.0
30 # 1.1-12-g3487ab12 : 12th commit since release 1.1 (N=3487ab12)
31 # 1.1-12-g3487ab12-dirty : 12th commit since release 1.1 (N=3487ab12)
32 #
33 # The last example is from a dirty working directory. To find the last
34 # release, the script looks for the last tag (does not need to be an
35 # annotated tag, but probably should be) which matches the format rel-*.
36 # If there is no such tag in the history, then the script uses 0.0 as
37 # the release number and counts the total number of commits since the
38 # original import for the commit count.
39 #
40 # If the current directory is not within the working directory, then the
41 # path to the source directory should be supplied on the command line.
42 #
43 # Author : Christopher Batten
44 # Date : August 5, 2009
45
46 set -e
47
48 #-------------------------------------------------------------------------
49 # Command line parsing
50 #-------------------------------------------------------------------------
51
52 if ( test "$1" = "-h" ); then
53 echo ""
54 sed -n '3p' $0 | sed -e 's/#//'
55 sed -n '5,/^$/p' $0 | sed -e 's/#//'
56 exit 1
57 fi
58
59 # Source directory command line option
60
61 src_dir="."
62 if ( test -n "$1" ); then
63 src_dir="$1"
64 fi
65
66 #-------------------------------------------------------------------------
67 # Verify source directory
68 #-------------------------------------------------------------------------
69 # If the source directory is not a git working directory output a
70 # question mark. A distribution will not be in a working directory, but
71 # the build system should be structured such that this script is not
72 # executed (and instead the version information should probably come
73 # from configure). If the user does not specify a source directory use
74 # the current directory.
75
76 if !( git rev-parse --is-inside-work-tree &> /dev/null ); then
77 echo "?"
78 exit 1;
79 fi
80
81 top_dir=`git rev-parse --show-cdup`
82 cd ./${top_dir}
83
84 #-------------------------------------------------------------------------
85 # Create the version string
86 #-------------------------------------------------------------------------
87 # See if we can do a describe based on a tag and if not use a default
88 # release number of 0.0 so that we always get canonical version number
89
90 if ( git describe --tags --match "rel-*" &> /dev/null ); then
91 ver_str=`git describe --tags --match "rel-*" | sed 's/rel-//'`
92 else
93 ver_num="0.0"
94 ver_commits=`git rev-list --all | wc -l | tr -d " "`
95 ver_sha=`git describe --tags --match "rel-*" --always`
96 ver_str="${ver_num}-${ver_commits}-g${ver_sha}"
97 fi
98
99 # Add a dirty suffix if working directory is dirty
100
101 if !( git diff --quiet ); then
102 ver_str="${ver_str}-dirty"
103 else
104 untracked=`git ls-files --directory --exclude-standard --others -t`
105 if ( test -n "${untracked}" ); then
106 ver_str="${ver_str}-dirty"
107 fi
108 fi
109
110 # Output the final version string
111
112 echo "${ver_str}"
113
114 # Final exit status
115
116 exit 0;
117