xref: /freebsd/sys/contrib/openzfs/scripts/make_gitrev.sh (revision 1603881667360c015f6685131f2f25474fa67a72)
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
28eda14cbcSMatt Macyset -e -u
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};;
37eda14cbcSMatt Macy	esac
38eda14cbcSMatt Macydone
39eda14cbcSMatt Macyshift $((OPTIND - 1))
40eda14cbcSMatt Macy
41eda14cbcSMatt Macytop_srcdir="$(dirname "$0")/.."
42eda14cbcSMatt MacyGITREV="${1:-include/zfs_gitrev.h}"
43eda14cbcSMatt Macy
44eda14cbcSMatt Macy# GITREV should be a relative path (relative to top_builddir or distdir)
45eda14cbcSMatt Macycase "${GITREV}" in
46eda14cbcSMatt Macy	/*) echo "Error: ${GITREV} should be a relative path" >&2
47eda14cbcSMatt Macy	    exit 1;;
48eda14cbcSMatt Macyesac
49eda14cbcSMatt Macy
50eda14cbcSMatt MacyZFS_GITREV=$({ cd "${top_srcdir}" &&
51eda14cbcSMatt Macy	git describe --always --long --dirty 2>/dev/null; } || :)
52eda14cbcSMatt Macy
53eda14cbcSMatt Macyif [ "x${ZFS_GITREV}" = x ]
54eda14cbcSMatt Macythen
55eda14cbcSMatt Macy	# If the source directory is not a git repository, check if the file
56eda14cbcSMatt Macy	# already exists (in the source)
57eda14cbcSMatt Macy	if [ -f "${top_srcdir}/${GITREV}" ]
58eda14cbcSMatt Macy	then
59*16038816SMartin Matuska		ZFS_GITREV=$(sed -n \
60eda14cbcSMatt Macy			'1s/^#define[[:blank:]]ZFS_META_GITREV "\([^"]*\)"$/\1/p' \
61*16038816SMartin Matuska			"${top_srcdir}/${GITREV}")
62eda14cbcSMatt Macy	fi
63eda14cbcSMatt Macyelif [ ${dist} = yes ]
64eda14cbcSMatt Macythen
65eda14cbcSMatt Macy	# Append -dist when creating distributed sources from a git repository
66eda14cbcSMatt Macy	ZFS_GITREV="${ZFS_GITREV}-dist"
67eda14cbcSMatt Macyfi
68eda14cbcSMatt MacyZFS_GITREV=${ZFS_GITREV:-unknown}
69eda14cbcSMatt Macy
70eda14cbcSMatt MacyGITREVTMP="${GITREV}~"
71eda14cbcSMatt Macyprintf '#define\tZFS_META_GITREV "%s"\n' "${ZFS_GITREV}" >"${GITREVTMP}"
72eda14cbcSMatt MacyGITREV="${distdir}/${GITREV}"
73eda14cbcSMatt Macyif cmp -s "${GITREV}" "${GITREVTMP}"
74eda14cbcSMatt Macythen
75eda14cbcSMatt Macy	rm -f "${GITREVTMP}"
76eda14cbcSMatt Macyelse
77eda14cbcSMatt Macy	mv -f "${GITREVTMP}" "${GITREV}"
78eda14cbcSMatt Macyfi
79