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