1*22ce4affSfengbojiang#!/bin/sh
2*22ce4affSfengbojiang
3*22ce4affSfengbojiang#
4*22ce4affSfengbojiang# CDDL HEADER START
5*22ce4affSfengbojiang#
6*22ce4affSfengbojiang# This file and its contents are supplied under the terms of the
7*22ce4affSfengbojiang# Common Development and Distribution License ("CDDL"), version 1.0.
8*22ce4affSfengbojiang# You may only use this file in accordance with the terms of version
9*22ce4affSfengbojiang# 1.0 of the CDDL.
10*22ce4affSfengbojiang#
11*22ce4affSfengbojiang# A full copy of the text of the CDDL should have accompanied this
12*22ce4affSfengbojiang# source.  A copy of the CDDL is also available via the Internet at
13*22ce4affSfengbojiang# http://www.illumos.org/license/CDDL.
14*22ce4affSfengbojiang#
15*22ce4affSfengbojiang# CDDL HEADER END
16*22ce4affSfengbojiang#
17*22ce4affSfengbojiang
18*22ce4affSfengbojiang# Copyright (c) 2018 by Delphix. All rights reserved.
19*22ce4affSfengbojiang# Copyright (c) 2018 by Matthew Thode. All rights reserved.
20*22ce4affSfengbojiang
21*22ce4affSfengbojiang#
22*22ce4affSfengbojiang# Generate zfs_gitrev.h.  Note that we need to do this for every
23*22ce4affSfengbojiang# invocation of `make`, including for incremental builds.  Therefore we
24*22ce4affSfengbojiang# can't use a zfs_gitrev.h.in file which would be processed only when
25*22ce4affSfengbojiang# `configure` is run.
26*22ce4affSfengbojiang#
27*22ce4affSfengbojiang
28*22ce4affSfengbojiangset -e -u
29*22ce4affSfengbojiang
30*22ce4affSfengbojiangdist=no
31*22ce4affSfengbojiangdistdir=.
32*22ce4affSfengbojiangwhile getopts D: flag
33*22ce4affSfengbojiangdo
34*22ce4affSfengbojiang	case $flag in
35*22ce4affSfengbojiang		\?) echo "Usage: $0 [-D distdir] [file]" >&2; exit 1;;
36*22ce4affSfengbojiang		D)  dist=yes; distdir=${OPTARG};;
37*22ce4affSfengbojiang	esac
38*22ce4affSfengbojiangdone
39*22ce4affSfengbojiangshift $((OPTIND - 1))
40*22ce4affSfengbojiang
41*22ce4affSfengbojiangtop_srcdir="$(dirname "$0")/.."
42*22ce4affSfengbojiangGITREV="${1:-include/zfs_gitrev.h}"
43*22ce4affSfengbojiang
44*22ce4affSfengbojiang# GITREV should be a relative path (relative to top_builddir or distdir)
45*22ce4affSfengbojiangcase "${GITREV}" in
46*22ce4affSfengbojiang	/*) echo "Error: ${GITREV} should be a relative path" >&2
47*22ce4affSfengbojiang	    exit 1;;
48*22ce4affSfengbojiangesac
49*22ce4affSfengbojiang
50*22ce4affSfengbojiangZFS_GITREV=$({ cd "${top_srcdir}" &&
51*22ce4affSfengbojiang	git describe --always --long --dirty 2>/dev/null; } || :)
52*22ce4affSfengbojiang
53*22ce4affSfengbojiangif [ "x${ZFS_GITREV}" = x ]
54*22ce4affSfengbojiangthen
55*22ce4affSfengbojiang	# If the source directory is not a git repository, check if the file
56*22ce4affSfengbojiang	# already exists (in the source)
57*22ce4affSfengbojiang	if [ -f "${top_srcdir}/${GITREV}" ]
58*22ce4affSfengbojiang	then
59*22ce4affSfengbojiang		ZFS_GITREV="$(sed -n \
60*22ce4affSfengbojiang			'1s/^#define[[:blank:]]ZFS_META_GITREV "\([^"]*\)"$/\1/p' \
61*22ce4affSfengbojiang			"${top_srcdir}/${GITREV}")"
62*22ce4affSfengbojiang	fi
63*22ce4affSfengbojiangelif [ ${dist} = yes ]
64*22ce4affSfengbojiangthen
65*22ce4affSfengbojiang	# Append -dist when creating distributed sources from a git repository
66*22ce4affSfengbojiang	ZFS_GITREV="${ZFS_GITREV}-dist"
67*22ce4affSfengbojiangfi
68*22ce4affSfengbojiangZFS_GITREV=${ZFS_GITREV:-unknown}
69*22ce4affSfengbojiang
70*22ce4affSfengbojiangGITREVTMP="${GITREV}~"
71*22ce4affSfengbojiangprintf '#define\tZFS_META_GITREV "%s"\n' "${ZFS_GITREV}" >"${GITREVTMP}"
72*22ce4affSfengbojiangGITREV="${distdir}/${GITREV}"
73*22ce4affSfengbojiangif cmp -s "${GITREV}" "${GITREVTMP}"
74*22ce4affSfengbojiangthen
75*22ce4affSfengbojiang	rm -f "${GITREVTMP}"
76*22ce4affSfengbojiangelse
77*22ce4affSfengbojiang	mv -f "${GITREVTMP}" "${GITREV}"
78*22ce4affSfengbojiangfi
79