xref: /freebsd-src/sys/contrib/openzfs/scripts/make_gitrev.sh (revision e92ffd9b626833ebdbf2742c8ffddc6cd94b963e)
1eda14cbcSMatt Macy#!/bin/sh
2eda14cbcSMatt Macy
3eda14cbcSMatt Macy#
4eda14cbcSMatt Macy# CDDL HEADER START
5eda14cbcSMatt Macy#
6eda14cbcSMatt Macy# This file and its contents are supplied under the terms of the
7eda14cbcSMatt Macy# Common Development and Distribution License ("CDDL"), version 1.0.
8eda14cbcSMatt Macy# You may only use this file in accordance with the terms of version
9eda14cbcSMatt Macy# 1.0 of the CDDL.
10eda14cbcSMatt Macy#
11eda14cbcSMatt Macy# A full copy of the text of the CDDL should have accompanied this
12eda14cbcSMatt Macy# source.  A copy of the CDDL is also available via the Internet at
13eda14cbcSMatt Macy# http://www.illumos.org/license/CDDL.
14eda14cbcSMatt Macy#
15eda14cbcSMatt Macy# CDDL HEADER END
16eda14cbcSMatt Macy#
17eda14cbcSMatt Macy
18eda14cbcSMatt Macy# Copyright (c) 2018 by Delphix. All rights reserved.
19eda14cbcSMatt Macy# Copyright (c) 2018 by Matthew Thode. All rights reserved.
20eda14cbcSMatt Macy
21eda14cbcSMatt Macy#
22eda14cbcSMatt Macy# Generate zfs_gitrev.h.  Note that we need to do this for every
23eda14cbcSMatt Macy# invocation of `make`, including for incremental builds.  Therefore we
24eda14cbcSMatt Macy# can't use a zfs_gitrev.h.in file which would be processed only when
25eda14cbcSMatt Macy# `configure` is run.
26eda14cbcSMatt Macy#
27eda14cbcSMatt Macy
28*e92ffd9bSMartin Matuskaset -eu
29eda14cbcSMatt Macy
30eda14cbcSMatt Macydist=no
31eda14cbcSMatt Macydistdir=.
32eda14cbcSMatt Macywhile getopts D: flag
33eda14cbcSMatt Macydo
34eda14cbcSMatt Macy	case $flag in
35eda14cbcSMatt Macy		\?) echo "Usage: $0 [-D distdir] [file]" >&2; exit 1;;
36eda14cbcSMatt Macy		D)  dist=yes; distdir=${OPTARG};;
37*e92ffd9bSMartin Matuska		*)  ;;
38eda14cbcSMatt Macy	esac
39eda14cbcSMatt Macydone
40eda14cbcSMatt Macyshift $((OPTIND - 1))
41eda14cbcSMatt Macy
42eda14cbcSMatt Macytop_srcdir="$(dirname "$0")/.."
43eda14cbcSMatt MacyGITREV="${1:-include/zfs_gitrev.h}"
44eda14cbcSMatt Macy
45eda14cbcSMatt Macy# GITREV should be a relative path (relative to top_builddir or distdir)
46eda14cbcSMatt Macycase "${GITREV}" in
47eda14cbcSMatt Macy	/*) echo "Error: ${GITREV} should be a relative path" >&2
48eda14cbcSMatt Macy	    exit 1;;
49*e92ffd9bSMartin Matuska	*) ;;
50eda14cbcSMatt Macyesac
51eda14cbcSMatt Macy
52eda14cbcSMatt MacyZFS_GITREV=$({ cd "${top_srcdir}" &&
53eda14cbcSMatt Macy	git describe --always --long --dirty 2>/dev/null; } || :)
54eda14cbcSMatt Macy
553ff01b23SMartin Matuskaif [ -z "${ZFS_GITREV}" ]
56eda14cbcSMatt Macythen
57eda14cbcSMatt Macy	# If the source directory is not a git repository, check if the file
58eda14cbcSMatt Macy	# already exists (in the source)
59eda14cbcSMatt Macy	if [ -f "${top_srcdir}/${GITREV}" ]
60eda14cbcSMatt Macy	then
6116038816SMartin Matuska		ZFS_GITREV=$(sed -n \
62eda14cbcSMatt Macy			'1s/^#define[[:blank:]]ZFS_META_GITREV "\([^"]*\)"$/\1/p' \
6316038816SMartin Matuska			"${top_srcdir}/${GITREV}")
64eda14cbcSMatt Macy	fi
65*e92ffd9bSMartin Matuskaelif [ "${dist}" = yes ]
66eda14cbcSMatt Macythen
67eda14cbcSMatt Macy	# Append -dist when creating distributed sources from a git repository
68eda14cbcSMatt Macy	ZFS_GITREV="${ZFS_GITREV}-dist"
69eda14cbcSMatt Macyfi
70eda14cbcSMatt MacyZFS_GITREV=${ZFS_GITREV:-unknown}
71eda14cbcSMatt Macy
72eda14cbcSMatt MacyGITREVTMP="${GITREV}~"
73eda14cbcSMatt Macyprintf '#define\tZFS_META_GITREV "%s"\n' "${ZFS_GITREV}" >"${GITREVTMP}"
74eda14cbcSMatt MacyGITREV="${distdir}/${GITREV}"
75eda14cbcSMatt Macyif cmp -s "${GITREV}" "${GITREVTMP}"
76eda14cbcSMatt Macythen
77eda14cbcSMatt Macy	rm -f "${GITREVTMP}"
78eda14cbcSMatt Macyelse
79eda14cbcSMatt Macy	mv -f "${GITREVTMP}" "${GITREV}"
80eda14cbcSMatt Macyfi
81