xref: /freebsd/contrib/elftoolchain/libelftc/make-toolchain-version (revision acc1a9ef8333c798c210fa94be6af4d5fe2dd794)
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 3299 2016-01-09 19:58:46Z 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 .svn -o -d ../.svn ]; then	# FreeBSD and SF.Net sources.
68    versionstring=" svn:"$(svnversion)
69elif [ -d CVS ]; then		# NetBSD.
70    versionstring=" cvs:unknown"
71else				# DragonFlyBSD.
72    versionstring=" git:"$(git describe --all --dirty --long 2> /dev/null)
73
74    # Cannot determine an in-tree version number.
75    if [ $? -ne 0 ]; then
76	versionstring=""
77    fi
78fi
79
80cd ${curdir} || usage "Cannot change back to ${curdir}."
81
82#
83# Only replace the source file if its content has changed.
84#
85tmpfile=`mktemp ${TMPDIR:-/tmp}/MV.XXXXXXX`
86trap "rm -f ${tmpfile};" 0 1 2 3 15
87
88cat > ${tmpfile} <<EOF
89/* WARNING: Generated by "${progname}". */
90
91#include <sys/types.h>
92#include <libelftc.h>
93
94const char *
95elftc_version(void)
96{
97	return "${elftcname} ${version} ${buildhost}${versionstring}";
98}
99EOF
100
101if ! cmp -s ${tmpfile} ${versionfile}; then
102    echo "@ ${progname}: building \"${versionfile}\"."
103    cp ${tmpfile} ${versionfile} || exit ${?}
104fi
105