http://bugs.ethereal.com/bugzilla/show_bug.cgi?id=377
[obnox/wireshark/wip.git] / make-version.pl
1 #!/usr/bin/perl -w
2 #
3 # Copyright 2004 Jörg Mayer (see AUTHORS file)
4 #
5 # $Id$
6 #
7 # Ethereal - Network traffic analyzer
8 # By Gerald Combs <gerald@ethereal.com>
9 # Copyright 1998 Gerald Combs
10 #
11 # This program is free software; you can redistribute it and/or
12 # modify it under the terms of the GNU General Public License
13 # as published by the Free Software Foundation; either version 2
14 # of the License, or (at your option) any later version.
15 #
16 # This program is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 # GNU General Public License for more details.
20 #
21 # You should have received a copy of the GNU General Public License
22 # along with this program; if not, write to the Free Software
23 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24
25 # usage:  ./make-version.pl [-p] [--package-version]
26 #
27 # If "version.conf" is present, it is parsed for configuration values.  
28 # Possible values are:
29 #
30 #   enable     - Enable or disable versioning.  Zero (0) disables, nonzero
31 #                enables.
32 #   format     - A strftime() formatted string to use as a template for
33 #                the version string.  The sequence "%#" will substitute
34 #                the SVN revision number.
35 #   pkg_format - Like "format", but used for the package version.
36 #
37 # If run with the "-p" or "--package-version" argument, the
38 # AM_INIT_AUTOMAKE macro in configure.in and the VERSION macro in
39 # config.nmake will have the pkg_format template appended to the 
40 # version number.  svnversion.h will _not_ be generated if either
41 # argument is present.
42 #
43 # Default configuration:
44 #
45 # enable: 1
46 # format: SVN %Y%m%d%H%M%S
47 # pkg_format: -SVN-%#
48 # am_init: 0
49
50 # XXX - We're pretty dumb about the "%#" substitution, and about having
51 # spaces in the package format.
52
53 use strict;
54
55 use Time::Local;
56 use POSIX qw(strftime);
57 use Getopt::Long;
58
59 my $version_file = 'svnversion.h';
60 my $version_string = "";
61 my $package_string = "";
62 my $vconf_file = 'version.conf';
63 my $last = 0;
64 my $revision = 0;
65 my $pkg_version = 0;
66 my %version_pref = (
67         "enable"     => 1,
68         "format"     => "SVN %Y%m%d%H%M%S",
69         "pkg_format" => "-SVN-%#",
70         );
71 my $srcdir = ".";
72
73
74 # Run "svn info".  Parse out the most recent modification time and the
75 # revision number.
76 sub read_svn_info {
77         my $line;
78         my $version_format = $version_pref{"format"};
79         my $package_format = $version_pref{"pkg_format"};
80         my $in_entries = 0;
81         my $svn_name;
82
83         if (! open (ENTRIES, "< $srcdir/.svn/entries")) {
84                 print ("Unable to get SVN info.\n");
85                 return;
86         }
87
88         # The entries schema is flat, so we can use regexes to parse its contents.
89         while ($line = <ENTRIES>) {
90                 if ($line =~ /<entry$/ || $line =~ /<entry\s/) {
91                         $in_entries = 1;
92                         $svn_name = "";
93                 }
94                 if ($in_entries) {
95                         if ($line =~ /name="(.*)"/) { $svn_name = $1; }
96                         if ($line =~ /committed-date="(\d{4})-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)/) {
97                                 $last = timegm($6, $5, $4, $3, $2 - 1, $1);
98                         }
99                         if ($line =~ /revision="(\d+)"/) { $revision = $1; }
100                 }
101                 if ($line =~ /\/>/) {
102                         if (($svn_name eq "" || $svn_name eq "svn:this_dir") &&
103                                         $last && $revision) {
104                                 $in_entries = 0;
105                                 $version_format =~ s/%#/$revision/;
106                                 $version_string = strftime($version_format, gmtime($last));
107
108                                 $package_format =~ s/%#/$revision/;
109                                 $package_string = strftime($package_format, gmtime($last));
110
111                                 last;
112                         }
113                 }
114         }
115         close ENTRIES;
116 }
117
118
119 # Read configure.in, then write it back out with an updated 
120 # "AM_INIT_AUTOMAKE" line.
121 sub update_configure_in
122 {
123         my $line;
124         my $contents = "";
125         my $version = "";
126         
127         return if ($package_string eq "");
128         
129         open(CFGIN, "< configure.in") || die "Can't read configure.in!";
130         while ($line = <CFGIN>) {
131                 if ($line =~ /^AM_INIT_AUTOMAKE\(ethereal, (\d+)\.(\d+).(\d+)/) {
132                         $line = "AM_INIT_AUTOMAKE\(ethereal, $1.$2.$3$package_string)\n";
133                 }
134                 $contents .= $line
135         }
136         
137         open(CFGIN, "> configure.in") || die "Can't write configure.in!";
138         print(CFGIN $contents);
139         close(CFGIN);
140         print "configure.in has been updated.\n";
141 }
142
143 # Read config.nmake, then write it back out with an updated 
144 # "VERSION" line.
145 sub update_config_nmake
146 {
147         my $line;
148         my $contents = "";
149         my $version = "";
150         
151         return if ($package_string eq "");
152         
153         open(CFGIN, "< config.nmake") || die "Can't read config.nmake!";
154         while ($line = <CFGIN>) {
155                 if ($line =~ /^VERSION=(\d+)\.(\d+).(\d+)/) {
156                         $line = "VERSION=$1.$2.$3$package_string\n";
157                 }
158                 $contents .= $line
159         }
160         
161         open(CFGIN, "> config.nmake") || die "Can't write config.nmake!";
162         print(CFGIN $contents);
163         close(CFGIN);
164         print "config.nmake has been updated.\n";
165 }
166
167
168
169 # Print the SVN version to $version_file.
170 # Don't change the file if it is not needed.
171 sub print_svn_version
172 {
173         my $svn_version;
174         my $needs_update = 1;
175
176         if ($pkg_version) { return; }
177
178         if ($last && $revision) {
179                 $svn_version = "#define SVNVERSION \"" . 
180                         $version_string . "\"\n";
181         } else {
182                 $svn_version = "/* #define SVNVERSION \"\" */\n";
183         }
184         if (open(OLDVER, "<$version_file")) {
185                 if (<OLDVER> eq $svn_version) {
186                         print "$version_file is up-to-date.\n";
187                         $needs_update = 0;
188                 }
189                 close OLDVER;
190         }
191
192         if ($needs_update == 1) {
193                 # print "Updating $version_file so it contains:\n$svn_version";
194                 open(VER, ">$version_file") || die ("Cannot write to $version_file ($!)\n");
195                 print VER "$svn_version";
196                 close VER;
197                 print "$version_file has been updated.\n";
198         }
199 }
200
201 # Read values from the configuration file, if it exists.
202 sub get_config {
203         my $arg;
204
205         # Get our command-line args
206         GetOptions("package-version", \$pkg_version);
207
208         if ($#ARGV >= 0) {
209                 $srcdir = $ARGV[0]
210         }
211
212
213         if (! open(FILE, "<$vconf_file")) {
214                 print STDERR "Version configuration file $vconf_file not "
215                 . "found.  Using defaults.\n";
216                 return 1;
217         }
218
219         while (<FILE>) {
220                 chomp;
221                 next if (/^#/);
222                 next unless (/^(\w+):\s+(\S.*)/);
223                 $version_pref{$1} = $2;
224         }
225         close FILE;
226         return 1;
227 }
228
229 ##
230 ## Start of code
231 ##
232
233 &get_config();
234
235 if (-d "./.svn") {
236         print "This is a build from SVN (or a SVN snapshot).\n";
237         &read_svn_info(".");
238         if ($pkg_version) {
239                 print "Generating package version.  Ignoring $version_file\n";
240                 &update_configure_in;
241                 &update_config_nmake;
242         } elsif ($version_pref{"enable"} == 0) {
243                 print "Version tag disabled in $vconf_file.\n";
244                 $last = 0;
245                 $revision = 0;
246         } else {
247                 print "SVN version tag will be computed.\n";
248         }
249 } else {
250         print "This is not a SVN build.\n";
251 }
252
253 &print_svn_version;
254
255 __END__