1a85fe12eSEd Maste#!/bin/sh
2a85fe12eSEd Maste#
3a85fe12eSEd Maste# This script generates a project-wide version identifier for use by
4a85fe12eSEd Maste# the `elftc_version()' API.
5a85fe12eSEd Maste#
6*d003e0d7SEd Maste# $Id: make-toolchain-version 3731 2019-04-06 14:28:34Z jkoshy $
7a85fe12eSEd Maste
8a85fe12eSEd Maste#
9a85fe12eSEd Maste# Defaults.
10a85fe12eSEd Maste#
11a85fe12eSEd Mastebuildhost=`uname -s`
12a85fe12eSEd Masteelftcname="elftoolchain"
13a85fe12eSEd Masteoptions="e:h:o:r:t:"
14a85fe12eSEd Mastetop=""
15a85fe12eSEd Masteversion="HEAD"
16a85fe12eSEd Masteversionfile="elftc_version.c"
17a85fe12eSEd Masteprogname=`basename ${0}`
18a85fe12eSEd Maste
19a85fe12eSEd Masteusage()
20a85fe12eSEd Maste{
21a85fe12eSEd Maste    exec >&2
22a85fe12eSEd Maste
23a85fe12eSEd Maste    # Print a message, if supplied.
24a85fe12eSEd Maste    if [ -n "${*}" ]; then echo "##${@}"; fi
25a85fe12eSEd Maste
26a85fe12eSEd Maste    echo "Usage: ${progname} [options]"
27a85fe12eSEd Maste    echo "	Generate a toolchain-wide version number"
28a85fe12eSEd Maste    echo "	-e PROJECTNAME Set the project name [default: ${elftcname}]."
29a85fe12eSEd Maste    echo "	-h HOSTOS      Set the build OS [default: ${buildhost}]."
30a85fe12eSEd Maste    echo "	-o OUTPUT      Set the output file [default: ${versionfile}]."
31a85fe12eSEd Maste    echo "	-r VERSION     Set the version string [default: ${version}]."
32a85fe12eSEd Maste    echo "	-t TOPDIR      Set the top-of-tree directory [required]."
33a85fe12eSEd Maste    exit 1
34a85fe12eSEd Maste}
35a85fe12eSEd Maste
36*d003e0d7SEd Maste# Determine the revision number for the source tree.
37*d003e0d7SEd Maste#
38*d003e0d7SEd Maste# - If CVS is detected, we use the string `unknown'.
39*d003e0d7SEd Maste# - If SVN is detected, we use the `svninfo' tool to determine the
40*d003e0d7SEd Maste#   in-tree revision number.
41*d003e0d7SEd Maste# - Otherwise, we use `git --describe'.
42*d003e0d7SEd Masteget_revision_string()
43*d003e0d7SEd Maste{
44*d003e0d7SEd Maste    v="unknown:unknown"
45*d003e0d7SEd Maste    if [ -d CVS ]; then     # Look for CVS (NetBSD).
46*d003e0d7SEd Maste        v="cvs:unknown"
47*d003e0d7SEd Maste    elif [ -d .svn ]; then  # An SVN checkout (SourceForge or FreeBSD).
48*d003e0d7SEd Maste        svnversion="$(svnversion 2>/dev/null)"
49*d003e0d7SEd Maste        if [ -n "${svnversion}" ]; then
50*d003e0d7SEd Maste            v="svn:${svnversion}"
51*d003e0d7SEd Maste        fi
52*d003e0d7SEd Maste    else        # Try git (DragonflyBSD).
53*d003e0d7SEd Maste        gitversion="$(git describe --all --dirty --long 2> /dev/null)"
54*d003e0d7SEd Maste        if [ -n "${gitversion}" ]; then
55*d003e0d7SEd Maste            v="git:${gitversion}"
56*d003e0d7SEd Maste	fi
57*d003e0d7SEd Maste    fi
58*d003e0d7SEd Maste
59*d003e0d7SEd Maste    echo "${v}"
60*d003e0d7SEd Maste}
61*d003e0d7SEd Maste
62a85fe12eSEd Maste#
63a85fe12eSEd Maste# Parse options.
64a85fe12eSEd Maste#
65a85fe12eSEd Maste
66a85fe12eSEd Mastewhile getopts ${options} option
67a85fe12eSEd Mastedo
68a85fe12eSEd Maste    case ${option} in
69a85fe12eSEd Maste	'e') elftcname="${OPTARG}"   ;;
70a85fe12eSEd Maste	'h') buildhost="${OPTARG}"   ;;
71a85fe12eSEd Maste	'o') versionfile="${OPTARG}" ;;
72a85fe12eSEd Maste	'r') version="${OPTARG}"      ;;
73a85fe12eSEd Maste	't') top="${OPTARG}"         ;;
74a85fe12eSEd Maste	'?') usage                   ;;
75a85fe12eSEd Maste    esac
76a85fe12eSEd Mastedone
77a85fe12eSEd Maste
78a85fe12eSEd Maste[ -n "${top}" ] || usage
79a85fe12eSEd Maste
80a85fe12eSEd Mastecurdir=`pwd`
81a85fe12eSEd Mastecd ${top} || usage "ERROR: Cannot change directory to \"${top}\"."
82a85fe12eSEd Maste
83*d003e0d7SEd Maste# Determine the in-tree revision number.
84*d003e0d7SEd Masteversionstring="$(get_revision_string)" || {
85*d003e0d7SEd Maste    echo "ERROR: cannot determine a revision number." 1>&2;
86b6b6f9ccSEd Maste    exit 1
87*d003e0d7SEd Maste}
88a85fe12eSEd Maste
89a85fe12eSEd Mastecd ${curdir} || usage "Cannot change back to ${curdir}."
90a85fe12eSEd Maste
91a85fe12eSEd Maste#
92a85fe12eSEd Maste# Only replace the source file if its content has changed.
93a85fe12eSEd Maste#
94a85fe12eSEd Mastetmpfile=`mktemp ${TMPDIR:-/tmp}/MV.XXXXXXX`
95a85fe12eSEd Mastetrap "rm -f ${tmpfile};" 0 1 2 3 15
96a85fe12eSEd Maste
97a85fe12eSEd Mastecat > ${tmpfile} <<EOF
98a85fe12eSEd Maste/* WARNING: Generated by "${progname}". */
99a85fe12eSEd Maste
100a85fe12eSEd Maste#include <sys/types.h>
101a85fe12eSEd Maste#include <libelftc.h>
102a85fe12eSEd Maste
103a85fe12eSEd Masteconst char *
104a85fe12eSEd Masteelftc_version(void)
105a85fe12eSEd Maste{
106a85fe12eSEd Maste	return "${elftcname} ${version} ${buildhost} ${versionstring}";
107a85fe12eSEd Maste}
108a85fe12eSEd MasteEOF
109a85fe12eSEd Maste
110a85fe12eSEd Masteif ! cmp -s ${tmpfile} ${versionfile}; then
111a85fe12eSEd Maste    echo "@ ${progname}: building \"${versionfile}\"."
112a85fe12eSEd Maste    cp ${tmpfile} ${versionfile} || exit ${?}
113a85fe12eSEd Mastefi
114