Tweak to work with SunOS /bin/sh.
[sbcl.git] / generate-version.sh
1 #!/bin/sh
2 # Not a shell script, but something intended to be sourced from shell scripts
3 git_available_p() {
4     # Check that (1) we have git (2) this is a git tree.
5     if `which git >/dev/null 2>/dev/null && git describe >/dev/null 2>/dev/null`
6     then
7         # Check that some of the newer git versions we use are supported.
8         if [ "0" != `git rev-list HEAD --not HEAD --count 2> /dev/null` ]
9         then
10             echo "Too old a git installation."
11             echo
12             echo "Your git doesn't support --count option with git rev-list,"
13             echo "which SBCL build requires. Git 1.7.2 or later should work."
14             exit 1
15         else
16             true
17         fi
18     else
19         false
20     fi
21 }
22
23 generate_version() {
24     if [ -f version.lisp-expr -a ! git_available_p ]
25     then
26         # Relase tarball, leave version.lisp-expr alone.
27         return
28     elif [ ! git_available_p ]
29     then
30         echo "Can't run 'git describe' and version.lisp-expr is missing." >&2
31         echo "To fix this, either install git or create a fake version.lisp-expr file." >&2
32         echo "You can create a fake version.lisp-expr file like this:" >&2
33         echo "    \$ echo '\"1.0.99.999\"' > version.lisp-expr" >&2
34         exit 1
35     fi
36     # Build it.
37     version_head=`git rev-parse HEAD`
38     if [ -z "$SBCL_BUILDING_RELEASE_FROM" ]
39     then
40         version_root="origin/master"
41     else
42         version_root="$SBCL_BUILDING_RELEASE_FROM"
43     fi
44     version_base=`git rev-parse "$version_root"`
45     version_tag=`git describe --tags --match="sbcl*" --abbrev=0 $version_base`
46     version_release=`echo $version_tag | sed -e 's/sbcl[_-]//' | sed -e 's/_/\./g'`
47     version_n_root=`git rev-list $version_base --not $version_tag --count`
48     version_n_branch=`git rev-list HEAD --not $version_base --count`
49     if [ -z "$NO_GIT_HASH_IN_VERSION" ]
50     then
51         version_hash="-`git rev-parse --short $version_head`"
52     else
53         version_hash=""
54     fi
55     if git diff HEAD --no-ext-diff --quiet --exit-code
56     then
57         version_dirty=""
58     else
59         version_dirty="-dirty"
60     fi
61     # Now that we have all the pieces, put them together.
62     cat >version.lisp-expr <<EOF
63 ;;; This file is auto-generated using generate-version.sh. Every time
64 ;;; you re-run make.sh, this file will be overwritten if you are
65 ;;; working from a Git checkout.
66 EOF
67     if [ "$version_base" = "$version_head" ]
68     then
69         if [ "0" = "$version_n_root" ]
70         then
71             printf "\"%s%s\"\n" \
72                 $version_release $version_dirty >>version.lisp-expr
73         else
74             printf "\"%s.%s%s%s\"\n" \
75                 $version_release $version_n_root \
76                 $version_hash $version_dirty >>version.lisp-expr
77         fi
78     else
79         echo "base=$version_base"
80         echo "head=$version_head"
81         version_branchname=`git describe --contains --all HEAD`
82         printf "\"%s.%s.%s.%s%s%s\"\n" \
83             $version_release $version_n_root \
84             $version_branchname $version_n_branch \
85             $version_hash $version_dirty >>version.lisp-expr
86     fi
87 }