summaryrefslogtreecommitdiffstats
path: root/rtemstoolkit/elftoolchain/libelftc/make-toolchain-version
blob: 0cdf370cc32b8e804a6d25b6a6ce295acc242b73 (plain) (blame)
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
#!/bin/sh
#
# This script generates a project-wide version identifier for use by
# the `elftc_version()' API.
#
# $Id: make-toolchain-version 3414 2016-02-16 22:55:28Z jkoshy $

#
# Defaults.
#
buildhost=`uname -s`
elftcname="elftoolchain"
options="e:h:o:r:t:"
top=""
version="HEAD"
versionfile="elftc_version.c"
progname=`basename ${0}`

usage()
{
    exec >&2

    # Print a message, if supplied.
    if [ -n "${*}" ]; then echo "##${@}"; fi

    echo "Usage: ${progname} [options]"
    echo "	Generate a toolchain-wide version number"
    echo "	-e PROJECTNAME Set the project name [default: ${elftcname}]."
    echo "	-h HOSTOS      Set the build OS [default: ${buildhost}]."
    echo "	-o OUTPUT      Set the output file [default: ${versionfile}]."
    echo "	-r VERSION     Set the version string [default: ${version}]."
    echo "	-t TOPDIR      Set the top-of-tree directory [required]."
    exit 1
}

#
# Parse options.
#

while getopts ${options} option
do
    case ${option} in
	'e') elftcname="${OPTARG}"   ;;
	'h') buildhost="${OPTARG}"   ;;
	'o') versionfile="${OPTARG}" ;;
	'r') version="${OPTARG}"      ;;
	't') top="${OPTARG}"         ;;
	'?') usage                   ;;
    esac
done

[ -n "${top}" ] || usage

# Try to determine the in-tree revision number.
#
# This script attempts to handle the case where our sources have been
# incorporated into an operating system's base sources.
#
# - If SVN is detected, we use the `svninfo' tool to determine the
#   in-tree revision number.
# - If CVS is detected, we use the string `unknown'.
# - Otherwise, we use `git --describe'.

curdir=`pwd`
cd ${top} || usage "ERROR: Cannot change directory to \"${top}\"."

if [ -d CVS ]; then		# Look for CVS (NetBSD).
    versionstring=" cvs:unknown"
else				# Try git (DragonFlyBSD).
    gitversion="$(git describe --all --dirty --long 2> /dev/null)"
    if [ -n "${gitversion}" ]; then
        versionstring=" git:${gitversion}"
    else   # Assume an SVN checkout (SourceForge or FreeBSD).
        svnversion="$(svnversion)"
        if [ -n "${svnversion}" ]; then
	    versionstring=" svn:$(svnversion)"
        fi
    fi
fi

if [ -z "${versionstring}" ]; then
    echo "ERROR: cannot determine a revision number." 1>&2
    exit 1
fi

cd ${curdir} || usage "Cannot change back to ${curdir}."

#
# Only replace the source file if its content has changed.
#
tmpfile=`mktemp ${TMPDIR:-/tmp}/MV.XXXXXXX`
trap "rm -f ${tmpfile};" 0 1 2 3 15

cat > ${tmpfile} <<EOF
/* WARNING: Generated by "${progname}". */

#include <sys/types.h>
#include <libelftc.h>

const char *
elftc_version(void)
{
	return "${elftcname} ${version} ${buildhost}${versionstring}";
}
EOF

if ! cmp -s ${tmpfile} ${versionfile}; then
    echo "@ ${progname}: building \"${versionfile}\"."
    cp ${tmpfile} ${versionfile} || exit ${?}
fi