From f2a31fc362c3485afdc8a60b2a2ccccccf091796 Mon Sep 17 00:00:00 2001 From: Reini Urban Date: Sat, 9 Mar 2024 16:18:41 +0100 Subject: [PATCH] update version-gen helper which understands tags --- .gitignore | 2 +- Makefile.am | 2 +- build-aux/git-version-gen | 232 ++++++++++++++++++++++++++++++++++++++ build-aux/version-gen.sh | 186 ------------------------------ configure.ac | 2 +- 5 files changed, 235 insertions(+), 189 deletions(-) create mode 100755 build-aux/git-version-gen delete mode 100755 build-aux/version-gen.sh diff --git a/.gitignore b/.gitignore index 95b4783e2..8e5bca1f4 100644 --- a/.gitignore +++ b/.gitignore @@ -46,7 +46,6 @@ Makefile.in *.gcda *.gcov cover_db -/build-* # # Top-level generic files @@ -71,6 +70,7 @@ TAGS /libssp-0.dll.m32 /libssp-0.dll.m64 /.build-* +/.rb.sh # More generated files compile_commands.json diff --git a/Makefile.am b/Makefile.am index 183af18b5..22d3ac512 100644 --- a/Makefile.am +++ b/Makefile.am @@ -57,7 +57,7 @@ SAFEC_INFRA = \ ${top_srcdir}/build-aux/autogen.sh \ ${top_srcdir}/build-aux/msys2.bat \ ${top_srcdir}/build-aux/smoke.sh \ - $(top_srcdir)/build-aux/version-gen.sh \ + $(top_srcdir)/build-aux/git-version-gen \ $(top_srcdir)/doc/libc-overview.md \ $(top_srcdir)/scripts \ $(top_srcdir)/.version \ diff --git a/build-aux/git-version-gen b/build-aux/git-version-gen new file mode 100755 index 000000000..82175d08e --- /dev/null +++ b/build-aux/git-version-gen @@ -0,0 +1,232 @@ +#!/bin/sh +# Print a version string. +scriptversion=2024-03-09.15; # UTC + +# Copyright (C) 2007-2018 Free Software Foundation, Inc. +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +# This script is derived from GIT-VERSION-GEN from GIT: https://git-scm.com/. +# It may be run two ways: +# - from a git repository in which the "git describe" command below +# produces useful output (thus requiring at least one signed tag) +# - from a non-git-repo directory containing a .tarball-version file, which +# presumes this script is invoked like "./git-version-gen .tarball-version". + +# In order to use intra-version strings in your project, you will need two +# separate generated version string files: +# +# .tarball-version - present only in a distribution tarball, and not in +# a checked-out repository. Created with contents that were learned at +# the last time autoconf was run, and used by git-version-gen. Must not +# be present in either $(srcdir) or $(builddir) for git-version-gen to +# give accurate answers during normal development with a checked out tree, +# but must be present in a tarball when there is no version control system. +# Therefore, it cannot be used in any dependencies. GNUmakefile has +# hooks to force a reconfigure at distribution time to get the value +# correct, without penalizing normal development with extra reconfigures. +# +# .version - present in a checked-out repository and in a distribution +# tarball. Usable in dependencies, particularly for files that don't +# want to depend on config.h but do want to track version changes. +# Delete this file prior to any autoconf run where you want to rebuild +# files to pick up a version string change; and leave it stale to +# minimize rebuild time after unrelated changes to configure sources. +# +# As with any generated file in a VC'd directory, you should add +# /.version to .gitignore, so that you don't accidentally commit it. +# .tarball-version is never generated in a VC'd directory, so needn't +# be listed there. +# +# Use the following line in your configure.ac, so that $(VERSION) will +# automatically be up-to-date each time configure is run (and note that +# since configure.ac no longer includes a version string, Makefile rules +# should not depend on configure.ac for version updates). +# +# AC_INIT([GNU project], +# m4_esyscmd([build-aux/git-version-gen .tarball-version]), +# [bug-project@example]) +# +# Then use the following lines in your Makefile.am, so that .version +# will be present for dependencies, and so that .version and +# .tarball-version will exist in distribution tarballs. +# +# EXTRA_DIST = $(top_srcdir)/.version +# BUILT_SOURCES = $(top_srcdir)/.version +# $(top_srcdir)/.version: +# echo $(VERSION) > $@-t && mv $@-t $@ +# dist-hook: +# echo $(VERSION) > $(distdir)/.tarball-version + + +me=$0 + +version="git-version-gen $scriptversion + +Copyright 2011 Free Software Foundation, Inc. +There is NO warranty. You may redistribute this software +under the terms of the GNU General Public License. +For more information about these matters, see the files named COPYING." + +usage="\ +Usage: $me [OPTION]... \$srcdir/.tarball-version [TAG-NORMALIZATION-SED-SCRIPT] +Print a version string. + +Options: + + --prefix PREFIX prefix of git tags (default 'v') + --fallback VERSION + fallback version to use if \"git --version\" fails + + --help display this help and exit + --version output version information and exit + +Running without arguments will suffice in most cases." + +prefix= +fallback= + +while test $# -gt 0; do + case $1 in + --help) echo "$usage"; exit 0;; + --version) echo "$version"; exit 0;; + --prefix) shift; prefix=${1?};; + --fallback) shift; fallback=${1?};; + -*) + echo "$0: Unknown option '$1'." >&2 + echo "$0: Try '--help' for more information." >&2 + exit 1;; + *) + if test "x$tarball_version_file" = x; then + tarball_version_file="$1" + elif test "x$tag_sed_script" = x; then + tag_sed_script="$1" + else + echo "$0: extra non-option argument '$1'." >&2 + exit 1 + fi;; + esac + shift +done + +if test "x$tarball_version_file" = x; then + echo "$usage" + exit 1 +fi + +tag_sed_script="${tag_sed_script:-s/-/_/g}" + +nl=' +' + +# Avoid meddling by environment variable of the same name. +v= +v_from_git= + +# First see if there is a tarball-only version file. +# then try "git describe", then default. +if test -f $tarball_version_file +then + v=`cat $tarball_version_file` || v= + case $v in + *$nl*) v= ;; # reject multi-line output + [0-9]*) ;; + *) v= ;; + esac + test "x$v" = x \ + && echo "$0: WARNING: $tarball_version_file is missing or damaged" 1>&2 +fi + +if test "x$v" != x +then + : # use $v +# Otherwise, if there is at least one git commit involving the working +# directory, and "git describe" output looks sensible, use that to +# derive a version string. +elif test "`git log -1 --pretty=format:x . 2>&1`" = x \ + && v=`git describe --tags --abbrev=4 --match="$prefix*" HEAD 2>/dev/null \ + || git describe --tags --abbrev=4 HEAD 2>/dev/null` \ + && v=`printf '%s\n' "$v" | sed "$tag_sed_script"` \ + && case $v in + $prefix[0-9]*) ;; + *) (exit 1) ;; + esac +then + # Is this a new git that lists number of commits since the last + # tag or the previous older version that did not? + # Newer: v6.10_77_g0f8faeb + # Older: v6.10_g0f8faeb + vprefix=`expr "X$v" : 'X\(.*\)_g[^_]*$'` || vprefix=$v + case $vprefix in + *-*) : git describe is probably okay three part flavor ;; + *) + : git describe is older two part flavor + # Recreate the number of commits and rewrite such that the + # result is the same as if we were using the newer version + # of git describe. + vtag=`echo "$v" | sed 's/_.*//'` + commit_list=`git rev-list "$vtag"..HEAD 2>/dev/null` \ + || { commit_list=failed; + echo "$0: WARNING: git rev-list failed" 1>&2; } + numcommits=`echo "$commit_list" | wc -l` + v=`echo "$v" | sed "s/\(.*\)_\(.*\)/\1_$numcommits_\2/"`; + test "$commit_list" = failed && v=UNKNOWN + ;; + esac + + # Change the penultimate "_" to ".", for version-comparing tools. + # Remove the "g" to save a byte. + # The rpm version must not contain a - + v=`echo "$v" | sed 's/_\([^_]*\)_g\([^_]*\)$/.\1_\2/'`; + v_from_git=1 +elif test "x$fallback" = x || git --version >/dev/null 2>&1; then + v=UNKNOWN +else + v=$fallback +fi + +if test "x$v" = "xUNKNOWN"; then + v=`git describe --long --tags --dirty --always` +fi + +v=`echo "$v" |sed "s/^$prefix//"` + +# Test whether to append the "_dirty" suffix only if the version +# string we're using came from git. I.e., skip the test if it's "UNKNOWN" +# or if it came from .tarball-version. +if test "x$v_from_git" != x; then + # Don't declare a version "dirty" merely because a timestamp has changed. + git update-index --refresh > /dev/null 2>&1 + + dirty=`exec 2>/dev/null;git diff-index --name-only HEAD` || dirty= + case "$dirty" in + '') ;; + *) # Append the suffix only if there isn't one already. + case $v in + *_dirty) ;; + *) v="${v}_dirty" ;; + esac ;; + esac +fi + +# Omit the trailing newline, so that m4_esyscmd can use the result directly. +printf %s "$v" + +# Local variables: +# eval: (add-hook 'before-save-hook 'time-stamp) +# time-stamp-start: "scriptversion=" +# time-stamp-format: "%:y-%02m-%02d.%02H" +# time-stamp-time-zone: "UTC0" +# time-stamp-end: "; # UTC" +# End: diff --git a/build-aux/version-gen.sh b/build-aux/version-gen.sh deleted file mode 100755 index f7042d52b..000000000 --- a/build-aux/version-gen.sh +++ /dev/null @@ -1,186 +0,0 @@ -#!/bin/sh -# -# version-gen.sh - figure out package version number -# -# 2012 Jonathan Toppins -# -# Copyright (c) 2012 by Cisco Systems, Inc -# All rights reserved. -# -# Permission is hereby granted, free of charge, to any person -# obtaining a copy of this software and associated documentation -# files (the "Software"), to deal in the Software without -# restriction, including without limitation the rights to use, -# copy, modify, merge, publish, distribute, sublicense, and/or -# sell copies of the Software, and to permit persons to whom the -# Software is furnished to do so, subject to the following -# conditions: -# -# The above copyright notice and this permission notice shall be -# included in all copies or substantial portions of the Software. -# -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES -# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT -# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, -# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR -# OTHER DEALINGS IN THE SOFTWARE. - -# Get latest version number of the code, either from the repository or -# from a file stored in the source distribution. Prefer the repository -# generated version over the version stored in the file and print the -# version to stdout. -# -# Parameters -# ---------- -# tarball-version - file name of the version file released with a source -# distribution. -# sed-normalization - sed normalization script passed to sed via '-e' -# -# Version Numbering -# ----------------- -# Version number format: Major.Minor.Patch[-dirty] -# where -dirty denotes uncommitted changes. -# -# Detailed Description -# -------------------- -# Calculates the current version number. If possible, this is the -# output of 'git describe', modified to conform with the versioning -# scheme specified above. If 'git describe' returns an error -# (most likely because we're in an unpacked copy of a release tarball, -# rather than in a git working copy), then we fall back on reading the -# contents of the tarball-version file passed in via the command line. -# -# Note that tarball-version file should *not* be checked into git; -# please add it to your top-level .gitignore file. -# -# To have this script work with autotools you will want to add the following. -# To you configure.ac file: -# -# AC_INIT([project-name], -# m4_esyscmd([path/to/version-gen.sh tarball-version]), -# [bug-email@example.com]) -# -# Add the following to your Makefile.am, so that .version will be present -# for dependencies, and so that .version and .tarball-version will exist -# in distribution tarballs. -# -# EXTRA_DIST = $(top_srcdir)/.version -# BUILT_SOURCES = $(top_srcdir)/.version -# $(top_srcdir)/.version: -# echo $(VERSION) > $@-t && mv $@-t $@ -# dist-hook: -# echo $(VERSION) > $(distdir)/.tarball-version -# - -# Unit Tests: -# Note: Tests must be preformed with a clean work tree otherwise not all -# tests will pass. -# -# Test 1 -# > git tag -a -m "test" "test1.2" HEAD && \ -# test "1.2" = "$(version-gen.sh --prefix test .test-version)" && \ -# echo "Test 1 - PASSED" || echo "Test 1 - FAILED"; git tag -d "test1.2" -# -# Test 2 -# > git tag -a -m "test" "test1.2" HEAD^ && \ -# version-gen.sh --prefix test .test-version | grep -E "^1.2.1-.*$" | \ -# grep -E -v "dirty$" && \ -# echo "Test 2 - PASSED" || echo "Test 2 - FAILED"; git tag -d "test1.2" -# -# Test 3 -# > git tag -a -m "test" "test1.2" HEAD^ && \ -# echo "test" >>README && \ -# version-gen.sh --prefix test .test-version | \ -# grep -E "^1.2.1-.*-dirty$" && \ -# echo "Test 3 - PASSED" || echo "Test 3 - FAILED"; \ -# git tag -d "test1.2"; git checkout -- README -# - -SCRIPTVERSION=2017-08-24 - -prog=$0 - -version="version-gen.sh v$SCRIPTVERSION - -Copyright (c) 2012 by Cisco Systems, Inc -All rights reserved. There is NO warranty. You may redistribute this -software under the terms of the MIT License. -For more information about these matters, see the file name COPYING." - -usage=" -Usage: $prog [OPTIONS] \$srcdir/.tarball-version [TAG-NORMALIZATION-SED-SCRIPT] - -Print a version string. - -Options: - - --prefix prefix of git tags (default 'v') - --abbrev value passed to the --abbrev option of git describe - (default 4) - - --help this output and exit - --version the version of this script and exit -" - -prefix=v -abbrev=6 - -while test $# -gt 0; do - case $1 in - --help|-h) echo "$usage"; exit 0;; - --version|-v) echo "$version"; exit 0;; - --prefix) shift; prefix="$1";; - --abbrev) shift; abbrev="$1";; - -*) - echo "$prog: Unknown option '$1'." >&2 - echo "$prog: Try '--help' for more information." >&2 - exit 1;; - *) - if test -z "$tarball_version_file"; then - tarball_version_file="$1" - elif test -z "$tag_sed_script"; then - tag_sed_script="$1" - else - echo "$prog: extra non-option argument '$1'." >&2 - exit 1 - fi;; - esac - shift -done - -if test -z "$tarball_version_file"; then - echo "$usage" - exit 1 -fi - -#export GIT_DIR=$(dirname $tarball_version_file)/.git -tag_sed_script="${tag_sed_script:-s/\(.?*\)-\(.*\)-\(.*\)/\1.\2-\3/}" -nl=' -' -v= - -if test -f "$tarball_version_file"; then - v=$(cat $tarball_version_file) || v='' - case $v in - *$nl*) v= ;; # reject multi-line output - [0-9]*) ;; - *) v= ;; - esac - test -z "$v" \ - && echo "$prog: WARNING: $tarball_version_file is missing or damaged" 1>&2 -fi - -# read git version, if the repo has uncommitted changes append -# '-dirty' to version. -if test -z "$v"; then - v=$(git describe --long --tags --always --match "$prefix*" --abbrev=$abbrev \ - --dirty 2>/dev/null) || v=UNKNOWN - v=$(echo "$v" | sed "$tag_sed_script" | sed "s/^$prefix//") -fi - -echo "$v" | tr -d "$nl" -test "UNKNOWN" = "$v" && exit 1 -exit 0 diff --git a/configure.ac b/configure.ac index cda1726cd..adf2e3c15 100644 --- a/configure.ac +++ b/configure.ac @@ -35,7 +35,7 @@ AC_PREREQ([2.69]) # information on the package AC_INIT([Safe C Library], - [m4_esyscmd(build-aux/version-gen.sh .tarball-version)], + [m4_esyscmd(build-aux/git-version-gen .tarball-version)], [https://github.com/rurban/safeclib/issues], [safeclib], [http://github.com/rurban/safeclib/])