File size: 3,724 Bytes
6b0231d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#! /usr/bin/env bash

# Copyright 2020, Mark Callow
# SPDX-License-Identifier: Apache-2.0

# Script to generate version number files using the output of git
# describe.

# A file of this name will be created in the subdir (object) for which a
# version number is being generated.
VF=version.h
DEF_VER=v4.0

LF='
'

# Change directory the root of the Git repo.
depth=..
cd $(dirname $0)/$depth

function genversion() {
  # Try git-describe, then default.
  if [ -d ${GIT_DIR:-.git} -o -f .git ]; then
    if [ -z "$1" ]; then
      # Get version number for HEAD revision of repo.
      VN=$(git describe --match "v[0-9]*" HEAD 2>/dev/null)
    else
      # Get release tag containing the object identified in # $1.
      VN=$(git describe --contains --match "v[0-9]*" --exclude "*-beta*" --exclude "*-rc[0-9]*" $(git rev-list -1 HEAD $1) 2>/dev/null)
      if [ -z "$VN" ]; then
        # No containing release tag. Look for an RC tag.
        VN=$(git describe --contains --match "v[0-9]*" --exclude "*-beta*" $(git rev-list -1 HEAD $1) 2>/dev/null)
      fi
      if [ -z "$VN" ]; then
        # No containing RC tag. Look for a beta tag.
        VN=$(git describe --contains --match "v[0-9]*" $(git rev-list -1 HEAD $1) 2>/dev/null)
      fi
      if [ -z "$VN" ]; then
        # No containing tag. Get version number a previous tag.
        VN=$(git describe --match "v[0-9]*" $(git rev-list -1 HEAD $1) 2>/dev/null)
      fi
      if [ -z "$VN" ]; then
        # No tags at all, best we can do is tag the commit.
        VN="$DEF_VER-$(git rev-parse --short HEAD 2>/dev/null)"
      fi
    fi
    case "$VN" in
    *$LF*) (exit 1) ;;
    v[0-9]*)
        git update-index -q --refresh
        test -z "$(git diff-index --name-only HEAD --)" ||
        VN="$VN-dirty" ;;
    esac
  else
      VN="$DEF_VER"
  fi
}

# Write the version file, if the new version is different than the
# existing one.
function setfile() {
  local VC
  local verstring=$1_VERSION
  if [ $dry_run -eq 1 ]; then
      echo $verstring $VN
      return
  fi
  if [ -r $2 ]; then
    VC=$(grep $verstring $2 | sed -e "s/^#define $verstring //")
  else
    VC=unset
  fi
  if [ $force -eq 1 -o "$VN" != "$VC" ]; then
    echo "/*" > $2
    if [ "$1" = "LIBKTX" ]; then
      (echo "// [API version]"
      echo "@par API Version"
      echo "$DEF_VER"
      echo "// [API version]") >> $2
    fi
    (echo "// [Code version]"
    echo "$VN"
    echo "// [Code version]"
    echo "*/"
    echo "#define $verstring $VN"
    echo "#define $1_DEFAULT_VERSION $DEF_VER.__default__") >> $2
  fi
}

# Figure out the object name and write its version file.
function writeversion() {
  # Extract version from git if it is not passed via command line
  if [ -z "$VN" ]; then
    genversion $1
  fi
  local vfp;
  if [ -z "$1" ]; then
    vfp=$VF
  else
    vfp=$1/$VF
  fi
  if [ "$1" = "lib" ]; then
    ON=LIBKTX
    setfile $ON $1/$VF
  else
    ON=$(basename $1)
    ON=$(echo $ON | tr [:lower:] [:upper:])
    setfile $ON $1/$VF
  fi
}

function usage() {
    echo "Usage: $0 [-a] [-f] [-n] [-v version] [-o <outfile>] [<object>]"
    exit 1
}

force=0
dry_run=0
write_all=0;
args=$(getopt afnv:o: $*)

set -- $args
for i; do
  case "$i" in
    -a) write_all=1;
        shift;;
    -f) force=1;
        shift;;
    -n) dry_run=1;
        shift;;
    -v) VN=$2; shift; shift;;
    -o) VF=$2; shift; shift;;
    --) shift; break;;
  esac
done

case $# in
  0) if [ $write_all -ne 1 ]; then usage; fi;;
  1) OBJ=$1;;
  2) usage;;
esac

if [ $write_all -eq 1 ]; then
  for i in lib tools/*; do
    if [ -d $i -a "$i" != "tools/package" ]; then
      writeversion $i
    fi
  done
else
  writeversion $OBJ
fi

# vim:ai:ts=4:sts=4:sw=2:expandtab:textwidth=70