Skip to content

Commit

Permalink
Change: [Makefile] replace custom git-detection with 'findversion.sh' (
Browse files Browse the repository at this point in the history
…#25)

'findversion.sh' is a script from the OpenTTD repository. This means
OpenGFX now acts the same as OpenTTD in terms of version, filename,
etc.

Makefile.vcs is now only used to know when to retrigger targets, and
no longer part of the source tarballs.
  • Loading branch information
TrueBrain committed Feb 23, 2020
1 parent d884385 commit 4ad3227
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 41 deletions.
1 change: 0 additions & 1 deletion .gitignore
Expand Up @@ -4,7 +4,6 @@ Makefile.local
# Generated files
Makefile.vcs
Makefile.gfx
.version
custom_tags.txt
.nmlcache

Expand Down
70 changes: 30 additions & 40 deletions Makefile
Expand Up @@ -150,50 +150,40 @@ _E ?= @echo
#
################################################################

include Makefile.vcs
# Always run version detection, so we always have an accurate modified
# flag
REPO_VERSIONS := $(shell AWK="$(AWK)" "./findversion.sh")
REPO_MODIFIED := $(shell echo "$(REPO_VERSIONS)" | cut -f 3 -d' ')

# Use autodetected revisions
REPO_VERSION := $(shell echo "$(REPO_VERSIONS)" | cut -f 1 -d' ')
REPO_DATE := $(shell echo "$(REPO_VERSIONS)" | cut -f 2 -d' ')
REPO_HASH := $(shell echo "$(REPO_VERSIONS)" | cut -f 4 -d' ')

# Days of commit since 2000-01-01. REPO_DATE is in format YYYYMMDD.
REPO_DATE_YEAR := $(shell echo "${REPO_DATE}" | cut -b1-4)
REPO_DATE_MONTH := $(shell echo "${REPO_DATE}" | cut -b5-6 | sed s/^0//)
REPO_DATE_DAY := $(shell echo "${REPO_DATE}" | cut -b7-8 | sed s/^0//)
REPO_DAYS_SINCE_2000 := $(shell $(PYTHON) -c "from datetime import date; print( (date($(REPO_DATE_YEAR),$(REPO_DATE_MONTH),$(REPO_DATE_DAY))-date(2000,1,1)).days)")

ifneq ($(GIT),)

# Hash
REPO_HASH := $(shell $(GIT) rev-parse HEAD)

# Date
REPO_DATE := $(shell $(GIT) show -s --format="%ci" | cut -f1 -d" ")

# Version = Most recent tag, optionally followed by the number of commits since, current commit and dirty or broken indicators.
REPO_VERSION := $(shell $(GIT) describe --tags --dirty --broken)
# The version reported to OpenTTD. Usually days since 2000 + branch offset
NEWGRF_VERSION := $(shell let x="$(REPO_DAYS_SINCE_2000) + 65536 * $(REPO_BRANCH_VERSION)"; echo "$$x")

# Branch name
REPO_BRANCH := $(shell $(GIT) rev-parse --abbrev-ref HEAD)
# The title consists of name and version
REPO_TITLE := $(REPO_NAME) $(REPO_VERSION)

# Makefile.vcs contains all the data depending on the version reported by GIT.
# It is renewed *before* processing any real targets, *only* if the version of the working copy changes.
#
# Everything that uses the version strings (to compile them into some file), should have Makefile.vcs as prerequisite.
#
# Have a file which, if modified, triggers recompiling targets that use these
# variables.
Makefile.vcs: FORCE
$(_E) "[VCS] $@"
$(_V) echo "REPO_HASH = $(REPO_HASH)" >> $@.new
$(_V) echo "REPO_DATE = $(REPO_DATE)" >> $@.new
$(_V) echo "REPO_VERSION = $(REPO_VERSION)" >> $@.new
$(_V) echo "REPO_BRANCH = $(REPO_BRANCH)" >> $@.new
$(_V) echo "REPO_HASH = $(REPO_HASH)" >> $@.new
$(_V) echo "REPO_DATE = $(REPO_DATE)" >> $@.new
$(_V) echo "REPO_VERSION = $(REPO_VERSION)" >> $@.new
$(_V) echo "NEWGRF_VERSION = $(NEWGRF_VERSION)" >> $@.new
$(_V) echo "REPO_DAYS_SINCE_2000 = $(REPO_DAYS_SINCE_2000)" >> $@.new
$(_V) cmp -s $@.new $@ || cp $@.new $@
$(_V) -rm -f $@.new

endif

# Days of commit since 2000-1-1 00-00
REPO_DAYS_SINCE_2000 := $(shell $(PYTHON) -c "from datetime import date; print( (date(`echo "${REPO_DATE}" | sed s/-/,/g | sed s/,0/,/g`)-date(2000,1,1)).days)")

# Filename addition, if we're not building the default branch
REPO_BRANCH_STRING := $(filter-out $(DEFAULT_BRANCH_NAME)-, $(REPO_BRANCH)-)

# The version reported to OpenTTD. Usually days since 2000 + branch offset
NEWGRF_VERSION := $(shell let x="$(REPO_DAYS_SINCE_2000) + 65536 * $(REPO_BRANCH_VERSION)"; echo "$$x")

# The title consists of name and version
REPO_TITLE := $(REPO_NAME) $(REPO_VERSION)

distclean:: clean
maintainer-clean:: distclean

Expand Down Expand Up @@ -307,7 +297,6 @@ custom_tags.txt: Makefile.vcs
$(_V) echo "FILENAME :$(GRF_FILES)" >> $@
$(_V) echo "REPO_DATE :$(REPO_DATE)" >> $@
$(_V) echo "REPO_HASH :$(REPO_HASH)" >> $@
$(_V) echo "REPO_BRANCH :$(REPO_BRANCH)" >> $@
$(_V) echo "NEWGRF_VERSION :$(NEWGRF_VERSION)" >> $@
$(_V) echo "DAYS_SINCE_2K :$(REPO_DAYS_SINCE_2000)" >> $@

Expand Down Expand Up @@ -419,7 +408,7 @@ endif
#
################################################################

FILE_VERSION_STRING := $(REPO_BRANCH_STRING)$(REPO_VERSION)
FILE_VERSION_STRING := $(REPO_VERSION)
DIR_NAME := $(PROJECT_FILENAME)-$(FILE_VERSION_STRING)
TAR_FILENAME := $(DIR_NAME).tar

Expand Down Expand Up @@ -550,15 +539,16 @@ bundle_src: $(TAR_FILENAME_SRC)

$(DIR_NAME_SRC): $(MD5_SRC_FILENAME)
$(_E) "[BUNDLE SRC] $@"
ifneq ($(REPO_MODIFIED),)
ifneq ($(REPO_MODIFIED),0)
$(_E) "Cannot create source bundle with uncommitted changes! Aborting."
$(_V) false
else
$(_V) if [ -e $@ ]; then rm -rf $@; fi
$(_V) mkdir $@
$(_V) $(GIT) archive --format=tar HEAD | tar xf - -C $@/
$(_V) rm -rf $@/.git $@/.gitignore $@/.devzone $@/.github $@/scripts/make_changelog.sh
$(_V) cp $(CP_FLAGS) $(MD5_SRC_FILENAME) Makefile.vcs $@
$(_V) ./findversion.sh > $@/.ottdrev
$(_V) cp $(CP_FLAGS) $(MD5_SRC_FILENAME) $@
$(_V) echo "# Disable VCS version detection" > $@/Makefile.dist
$(_V) echo "GIT =" >> $@/Makefile.dist
endif
Expand Down
112 changes: 112 additions & 0 deletions findversion.sh
@@ -0,0 +1,112 @@
#!/bin/sh

# This file is part of OpenTTD.
# OpenTTD 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, version 2.
# OpenTTD 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 OpenTTD. If not, see <http://www.gnu.org/licenses/>.


# Arguments given? Show help text.
if [ "$#" != "0" ]; then
cat <<EOF
Usage: ./findversion.sh
Finds the current revision and if the code is modified.
Output: <VERSION>\t<ISODATE>\t<MODIFIED>\t<HASH>
VERSION
a string describing what version of the code the current checkout is
based on.
This also includes the commit date, an indication of whether the checkout
was modified and which branch was checked out. This value is not
guaranteed to be sortable, but is mainly meant for identifying the
revision and user display.
If no revision identifier could be found, this is left empty.
ISODATE
the commit date of the revision this checkout is based on.
The commit date may differ from the author date.
This can be used to decide upon the age of the source.
If no timestamp could be found, this is left empty.
MODIFIED
Whether (the src directory of) this checkout is modified or not. A
value of 0 means not modified, a value of 2 means it was modified.
A value of 1 means that the modified status is unknown, because this
is not an git checkout for example.
HASH
the git revision hash
By setting the AWK environment variable, a caller can determine which
version of "awk" is used. If nothing is set, this script defaults to
"awk".
EOF
exit 1;
fi

# Allow awk to be provided by the caller.
if [ -z "$AWK" ]; then
AWK=awk
fi

# Find out some dirs
cd `dirname "$0"`
ROOT_DIR=`pwd`

# Determine if we are using a modified version
# Assume the dir is not modified
MODIFIED="0"
if [ -d "$ROOT_DIR/.git" ] || [ -f "$ROOT_DIR/.git" ]; then
# We are a git checkout
# Refresh the index to make sure file stat info is in sync, then look for modifications
git update-index --refresh >/dev/null
if [ -n "`git diff-index HEAD`" ]; then
MODIFIED="2"
fi
HASH=`LC_ALL=C git rev-parse --verify HEAD 2>/dev/null`
SHORTHASH=`echo ${HASH} | cut -c1-10`
ISODATE=`LC_ALL=C git show -s --pretty='format:%ci' HEAD | "$AWK" '{ gsub("-", "", $1); print $1 }'`
BRANCH="`git symbolic-ref -q HEAD 2>/dev/null | sed 's@.*/@@'`"
TAG="`git name-rev --name-only --tags --no-undefined HEAD 2>/dev/null | sed 's@\^0$@@'`"

if [ "$MODIFIED" -eq "0" ]; then
hashprefix="-g"
elif [ "$MODIFIED" -eq "2" ]; then
hashprefix="-m"
else
hashprefix="-u"
fi

if [ -n "$TAG" ]; then
VERSION="${TAG}"
ISTAG="1"
if [ -n "`echo \"${TAG}\" | grep \"^[0-9.]*$\"`" ]; then
ISSTABLETAG="1"
else
ISSTABLETAG="0"
fi
else
VERSION="${ISODATE}-${BRANCH}${hashprefix}${SHORTHASH}"
ISTAG="0"
ISSTABLETAG="0"
fi

elif [ -f "$ROOT_DIR/.ottdrev" ]; then
# We are an exported source bundle
cat $ROOT_DIR/.ottdrev
exit
else
# We don't know
MODIFIED="1"
HASH=""
SHORTHASH=""
BRANCH=""
ISODATE=""
TAG=""
VERSION=""
ISTAG="0"
ISSTABLETAG="0"
fi

echo "$VERSION $ISODATE $MODIFIED $HASH $ISTAG $ISSTABLETAG"

0 comments on commit 4ad3227

Please sign in to comment.