Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

[dc635d]: Misc / mkversion.pl Maximize Restore History

Download this file

mkversion.pl    87 lines (73 with data), 2.4 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/usr/bin/perl -w
#
# Copyright (c) 2003-2013 Rony Shapiro <ronys@users.sourceforge.net>.
# All rights reserved. Use of the code is allowed under the
# Artistic License 2.0 terms, as specified in the LICENSE file
# distributed with this code, or available from
# http://www.opensource.org/licenses/artistic-license-2.0.php
#
# A simple utility to generate a version.h file from a template,
# with keyword substitution.
# The named file is replaced iff the newley generated one's
# different, to avoid spurious recompilations.
#
# Usage: $0 template outfile
# (typically: $0 version.in version.h)
#
use strict;
use warnings;
use File::Copy;
sub usage {
print "Usage: $0 template outfile\n";
exit 1;
}
my $GIT = "/usr/bin/git";
die "Couldn't find $GIT" unless (-x $GIT);
&usage unless ($#ARGV == 1);
my $TEMPLATE = $ARGV[0];
my $OUTFILE = $ARGV[1];
my $VERSTRING = `$GIT describe --all --always --dirty=+ --long`;
chomp $VERSTRING;
# If string is of the form heads/master-0-g5f69087, drop everything
# to the left of the rightmost g. Otherwise, this is a branch/WIP, leave full
# info
$VERSTRING =~ s,^heads/master-0-,,;
#Now that we're done with the formalities, let's get to work:
my $TMPFILE = "/tmp/v$$";
my ($MAJOR, $MINOR, $REVISION);
open(TH, "<$TEMPLATE") || die "Couldn't read $TEMPLATE\n";
open(VH, ">$TMPFILE") || die "Couldn't open $TMPFILE for writing\n";
while (<TH>) {
if (m/^#define\s+MAJORVERSION\s+(.+)$/) {
$MAJOR=$1;
} elsif (m/^#define\s+MINORVERSION\s+(.+)$/) {
$MINOR=$1;
} elsif (m/^#define\s+REVISION\s+(.+)$/) {
$REVISION=$1;
}
if (m/^\#define\s+VCS_VERSION/) {
print VH "#define VCS_VERSION \"$VERSTRING\"\n";
} elsif (m/^\#define\s+LINUXPRODVER/) {
print VH "#define LINUXPRODVER $MAJOR, $MINOR, $REVISION, $VERSTRING\n";
} else {
print VH;
}
}
close(TH);
close(VH);
# Replace $OUTFILE with $TMPFILE iff:
# 1. Former doesn't exist
# OR
# 2. The two differ
if (!-e $OUTFILE) {
move($TMPFILE, $OUTFILE) || die "Couldn't move $TMPFILE to $OUTFILE: $!\n";
} else {
`/usr/bin/diff -q $TMPFILE $OUTFILE > /dev/null`;
if ($? != 0) {
unlink $OUTFILE || die "Couldn't remove old $OUTFILE\n";
move($TMPFILE, $OUTFILE) || die "Couldn't move $TMPFILE to $OUTFILE: $!\n";
} else { # no changes, cleanup
unlink $TMPFILE;
}
}
exit 0;