Skip to content

Commit 4ad3227

Browse files
authored
Change: [Makefile] replace custom git-detection with 'findversion.sh' (#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.
1 parent d884385 commit 4ad3227

File tree

3 files changed

+142
-41
lines changed

3 files changed

+142
-41
lines changed

.gitignore

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ Makefile.local
44
# Generated files
55
Makefile.vcs
66
Makefile.gfx
7-
.version
87
custom_tags.txt
98
.nmlcache
109

Makefile

+30-40
Original file line numberDiff line numberDiff line change
@@ -150,50 +150,40 @@ _E ?= @echo
150150
#
151151
################################################################
152152

153-
include Makefile.vcs
153+
# Always run version detection, so we always have an accurate modified
154+
# flag
155+
REPO_VERSIONS := $(shell AWK="$(AWK)" "./findversion.sh")
156+
REPO_MODIFIED := $(shell echo "$(REPO_VERSIONS)" | cut -f 3 -d' ')
157+
158+
# Use autodetected revisions
159+
REPO_VERSION := $(shell echo "$(REPO_VERSIONS)" | cut -f 1 -d' ')
160+
REPO_DATE := $(shell echo "$(REPO_VERSIONS)" | cut -f 2 -d' ')
161+
REPO_HASH := $(shell echo "$(REPO_VERSIONS)" | cut -f 4 -d' ')
162+
163+
# Days of commit since 2000-01-01. REPO_DATE is in format YYYYMMDD.
164+
REPO_DATE_YEAR := $(shell echo "${REPO_DATE}" | cut -b1-4)
165+
REPO_DATE_MONTH := $(shell echo "${REPO_DATE}" | cut -b5-6 | sed s/^0//)
166+
REPO_DATE_DAY := $(shell echo "${REPO_DATE}" | cut -b7-8 | sed s/^0//)
167+
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)")
154168

155-
ifneq ($(GIT),)
156-
157-
# Hash
158-
REPO_HASH := $(shell $(GIT) rev-parse HEAD)
159-
160-
# Date
161-
REPO_DATE := $(shell $(GIT) show -s --format="%ci" | cut -f1 -d" ")
162-
163-
# Version = Most recent tag, optionally followed by the number of commits since, current commit and dirty or broken indicators.
164-
REPO_VERSION := $(shell $(GIT) describe --tags --dirty --broken)
169+
# The version reported to OpenTTD. Usually days since 2000 + branch offset
170+
NEWGRF_VERSION := $(shell let x="$(REPO_DAYS_SINCE_2000) + 65536 * $(REPO_BRANCH_VERSION)"; echo "$$x")
165171

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

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

183-
endif
184-
185-
# Days of commit since 2000-1-1 00-00
186-
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)")
187-
188-
# Filename addition, if we're not building the default branch
189-
REPO_BRANCH_STRING := $(filter-out $(DEFAULT_BRANCH_NAME)-, $(REPO_BRANCH)-)
190-
191-
# The version reported to OpenTTD. Usually days since 2000 + branch offset
192-
NEWGRF_VERSION := $(shell let x="$(REPO_DAYS_SINCE_2000) + 65536 * $(REPO_BRANCH_VERSION)"; echo "$$x")
193-
194-
# The title consists of name and version
195-
REPO_TITLE := $(REPO_NAME) $(REPO_VERSION)
196-
197187
distclean:: clean
198188
maintainer-clean:: distclean
199189

@@ -307,7 +297,6 @@ custom_tags.txt: Makefile.vcs
307297
$(_V) echo "FILENAME :$(GRF_FILES)" >> $@
308298
$(_V) echo "REPO_DATE :$(REPO_DATE)" >> $@
309299
$(_V) echo "REPO_HASH :$(REPO_HASH)" >> $@
310-
$(_V) echo "REPO_BRANCH :$(REPO_BRANCH)" >> $@
311300
$(_V) echo "NEWGRF_VERSION :$(NEWGRF_VERSION)" >> $@
312301
$(_V) echo "DAYS_SINCE_2K :$(REPO_DAYS_SINCE_2000)" >> $@
313302

@@ -419,7 +408,7 @@ endif
419408
#
420409
################################################################
421410

422-
FILE_VERSION_STRING := $(REPO_BRANCH_STRING)$(REPO_VERSION)
411+
FILE_VERSION_STRING := $(REPO_VERSION)
423412
DIR_NAME := $(PROJECT_FILENAME)-$(FILE_VERSION_STRING)
424413
TAR_FILENAME := $(DIR_NAME).tar
425414

@@ -550,15 +539,16 @@ bundle_src: $(TAR_FILENAME_SRC)
550539

551540
$(DIR_NAME_SRC): $(MD5_SRC_FILENAME)
552541
$(_E) "[BUNDLE SRC] $@"
553-
ifneq ($(REPO_MODIFIED),)
542+
ifneq ($(REPO_MODIFIED),0)
554543
$(_E) "Cannot create source bundle with uncommitted changes! Aborting."
555544
$(_V) false
556545
else
557546
$(_V) if [ -e $@ ]; then rm -rf $@; fi
558547
$(_V) mkdir $@
559548
$(_V) $(GIT) archive --format=tar HEAD | tar xf - -C $@/
560549
$(_V) rm -rf $@/.git $@/.gitignore $@/.devzone $@/.github $@/scripts/make_changelog.sh
561-
$(_V) cp $(CP_FLAGS) $(MD5_SRC_FILENAME) Makefile.vcs $@
550+
$(_V) ./findversion.sh > $@/.ottdrev
551+
$(_V) cp $(CP_FLAGS) $(MD5_SRC_FILENAME) $@
562552
$(_V) echo "# Disable VCS version detection" > $@/Makefile.dist
563553
$(_V) echo "GIT =" >> $@/Makefile.dist
564554
endif

findversion.sh

+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
#!/bin/sh
2+
3+
# This file is part of OpenTTD.
4+
# 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.
5+
# 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.
6+
# 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/>.
7+
8+
9+
# Arguments given? Show help text.
10+
if [ "$#" != "0" ]; then
11+
cat <<EOF
12+
Usage: ./findversion.sh
13+
Finds the current revision and if the code is modified.
14+
15+
Output: <VERSION>\t<ISODATE>\t<MODIFIED>\t<HASH>
16+
VERSION
17+
a string describing what version of the code the current checkout is
18+
based on.
19+
This also includes the commit date, an indication of whether the checkout
20+
was modified and which branch was checked out. This value is not
21+
guaranteed to be sortable, but is mainly meant for identifying the
22+
revision and user display.
23+
24+
If no revision identifier could be found, this is left empty.
25+
ISODATE
26+
the commit date of the revision this checkout is based on.
27+
The commit date may differ from the author date.
28+
This can be used to decide upon the age of the source.
29+
30+
If no timestamp could be found, this is left empty.
31+
MODIFIED
32+
Whether (the src directory of) this checkout is modified or not. A
33+
value of 0 means not modified, a value of 2 means it was modified.
34+
35+
A value of 1 means that the modified status is unknown, because this
36+
is not an git checkout for example.
37+
38+
HASH
39+
the git revision hash
40+
41+
By setting the AWK environment variable, a caller can determine which
42+
version of "awk" is used. If nothing is set, this script defaults to
43+
"awk".
44+
EOF
45+
exit 1;
46+
fi
47+
48+
# Allow awk to be provided by the caller.
49+
if [ -z "$AWK" ]; then
50+
AWK=awk
51+
fi
52+
53+
# Find out some dirs
54+
cd `dirname "$0"`
55+
ROOT_DIR=`pwd`
56+
57+
# Determine if we are using a modified version
58+
# Assume the dir is not modified
59+
MODIFIED="0"
60+
if [ -d "$ROOT_DIR/.git" ] || [ -f "$ROOT_DIR/.git" ]; then
61+
# We are a git checkout
62+
# Refresh the index to make sure file stat info is in sync, then look for modifications
63+
git update-index --refresh >/dev/null
64+
if [ -n "`git diff-index HEAD`" ]; then
65+
MODIFIED="2"
66+
fi
67+
HASH=`LC_ALL=C git rev-parse --verify HEAD 2>/dev/null`
68+
SHORTHASH=`echo ${HASH} | cut -c1-10`
69+
ISODATE=`LC_ALL=C git show -s --pretty='format:%ci' HEAD | "$AWK" '{ gsub("-", "", $1); print $1 }'`
70+
BRANCH="`git symbolic-ref -q HEAD 2>/dev/null | sed 's@.*/@@'`"
71+
TAG="`git name-rev --name-only --tags --no-undefined HEAD 2>/dev/null | sed 's@\^0$@@'`"
72+
73+
if [ "$MODIFIED" -eq "0" ]; then
74+
hashprefix="-g"
75+
elif [ "$MODIFIED" -eq "2" ]; then
76+
hashprefix="-m"
77+
else
78+
hashprefix="-u"
79+
fi
80+
81+
if [ -n "$TAG" ]; then
82+
VERSION="${TAG}"
83+
ISTAG="1"
84+
if [ -n "`echo \"${TAG}\" | grep \"^[0-9.]*$\"`" ]; then
85+
ISSTABLETAG="1"
86+
else
87+
ISSTABLETAG="0"
88+
fi
89+
else
90+
VERSION="${ISODATE}-${BRANCH}${hashprefix}${SHORTHASH}"
91+
ISTAG="0"
92+
ISSTABLETAG="0"
93+
fi
94+
95+
elif [ -f "$ROOT_DIR/.ottdrev" ]; then
96+
# We are an exported source bundle
97+
cat $ROOT_DIR/.ottdrev
98+
exit
99+
else
100+
# We don't know
101+
MODIFIED="1"
102+
HASH=""
103+
SHORTHASH=""
104+
BRANCH=""
105+
ISODATE=""
106+
TAG=""
107+
VERSION=""
108+
ISTAG="0"
109+
ISSTABLETAG="0"
110+
fi
111+
112+
echo "$VERSION $ISODATE $MODIFIED $HASH $ISTAG $ISSTABLETAG"

0 commit comments

Comments
 (0)