]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/elftoolchain/libelftc/make-toolchain-version
Update to ELF Tool Chain r3475
[FreeBSD/FreeBSD.git] / contrib / elftoolchain / libelftc / make-toolchain-version
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 #
11 buildhost=`uname -s`
12 elftcname="elftoolchain"
13 options="e:h:o:r:t:"
14 top=""
15 version="HEAD"
16 versionfile="elftc_version.c"
17 progname=`basename ${0}`
18
19 usage()
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
40 while getopts ${options} option
41 do
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
50 done
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
64 curdir=`pwd`
65 cd ${top} || usage "ERROR: Cannot change directory to \"${top}\"."
66
67 if [ -d CVS ]; then             # Look for CVS (NetBSD).
68     versionstring=" cvs:unknown"
69 else                            # 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
79 fi
80
81 if [ -z "${versionstring}" ]; then
82     echo "ERROR: cannot determine a revision number." 1>&2
83     exit 1
84 fi
85
86 cd ${curdir} || usage "Cannot change back to ${curdir}."
87
88 #
89 # Only replace the source file if its content has changed.
90 #
91 tmpfile=`mktemp ${TMPDIR:-/tmp}/MV.XXXXXXX`
92 trap "rm -f ${tmpfile};" 0 1 2 3 15
93
94 cat > ${tmpfile} <<EOF
95 /* WARNING: Generated by "${progname}". */
96
97 #include <sys/types.h>
98 #include <libelftc.h>
99
100 const char *
101 elftc_version(void)
102 {
103         return "${elftcname} ${version} ${buildhost}${versionstring}";
104 }
105 EOF
106
107 if ! cmp -s ${tmpfile} ${versionfile}; then
108     echo "@ ${progname}: building \"${versionfile}\"."
109     cp ${tmpfile} ${versionfile} || exit ${?}
110 fi