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