Minor fixes
[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
58 my $version_file = 'svnversion.h';
59 my $version_string = "";
60 my $package_string = "";
61 my $vconf_file = 'version.conf';
62 my $last = 0;
63 my $revision = 0;
64 my $pkg_version = 0;
65 my %version_pref = (
66         "enable"     => 1,
67         "format"     => "SVN %Y%m%d%H%M%S",
68         "pkg_format" => "-SVN-%#",
69         );
70
71
72 # Run "svn info".  Parse out the most recent modification time and the
73 # revision number.
74 sub read_svn_info {
75         my $line;
76         my $version_format = $version_pref{"format"};
77         my $package_format = $version_pref{"pkg_format"};
78         # If any other odd paths pop up, put them here.
79         my @svn_paths = ("", "c:/cygwin/lib/subversion/bin/");
80         my $svn_cmd;
81         my $svn_pid;
82
83         foreach $svn_cmd (@svn_paths) {
84                 $svn_cmd .= "svn info";
85                 if ($svn_pid = open(SVNINFO, $svn_cmd . " |")) {
86                         print ("Fetching version with command \"$svn_cmd\".\n");
87                         last;
88                 }
89         }
90         if (! defined($svn_pid)) {
91                 print ("Unable to get SVN info.\n");
92                 return;
93         }
94         while ($line = <SVNINFO>) {
95                 if ($line =~ /^Last Changed Date: (\d{4})-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d)/) {
96                         $last = timegm($6, $5, $4, $3, $2 - 1, $1);
97                 }
98                 if ($line =~ /^Revision: (\d+)/) {
99                         $revision = $1;
100                 }
101         }
102         close SVNINFO;
103
104         if ($last && $revision) {
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 }
112
113
114 # Read configure.in, then write it back out with an updated 
115 # "AM_INIT_AUTOMAKE" line.
116 sub update_configure_in
117 {
118         my $line;
119         my $contents = "";
120         my $version = "";
121         
122         return if ($package_string eq "");
123         
124         open(CFGIN, "< configure.in") || die "Can't read configure.in!";
125         while ($line = <CFGIN>) {
126                 if ($line =~ /^AM_INIT_AUTOMAKE\(ethereal, (\d+)\.(\d+).(\d+)/) {
127                         $line = "AM_INIT_AUTOMAKE\(ethereal, $1.$2.$3$package_string)\n";
128                 }
129                 $contents .= $line
130         }
131         
132         open(CFGIN, "> configure.in") || die "Can't write configure.in!";
133         print(CFGIN $contents);
134         close(CFGIN);
135         print "configure.in has been updated.\n";
136 }
137
138 # Read config.nmake, then write it back out with an updated 
139 # "VERSION" line.
140 sub update_config_nmake
141 {
142         my $line;
143         my $contents = "";
144         my $version = "";
145         
146         return if ($package_string eq "");
147         
148         open(CFGIN, "< config.nmake") || die "Can't read config.nmake!";
149         while ($line = <CFGIN>) {
150                 if ($line =~ /^VERSION=(\d+)\.(\d+).(\d+)/) {
151                         $line = "VERSION=$1.$2.$3$package_string\n";
152                 }
153                 $contents .= $line
154         }
155         
156         open(CFGIN, "> config.nmake") || die "Can't write config.nmake!";
157         print(CFGIN $contents);
158         close(CFGIN);
159         print "config.nmake has been updated.\n";
160 }
161
162
163
164 # Print the SVN version to $version_file.
165 # Don't change the file if it is not needed.
166 sub print_svn_version
167 {
168         my $svn_version;
169         my $needs_update = 1;
170
171         if ($pkg_version) { return; }
172
173         if ($last && $revision) {
174                 $svn_version = "#define SVNVERSION \"" . 
175                         $version_string . "\"\n";
176         } else {
177                 $svn_version = "/* #define SVNVERSION \"\" */\n";
178         }
179         if (open(OLDVER, "<$version_file")) {
180                 if (<OLDVER> eq $svn_version) {
181                         print "$version_file is up-to-date.\n";
182                         $needs_update = 0;
183                 }
184                 close OLDVER;
185         }
186
187         if ($needs_update == 1) {
188                 # print "Updating $version_file so it contains:\n$svn_version";
189                 open(VER, ">$version_file") || die ("Cannot write to $version_file ($!)\n");
190                 print VER "$svn_version";
191                 close VER;
192                 print "$version_file has been updated.\n";
193         }
194 }
195
196 # Read values from the configuration file, if it exists.
197 sub get_config {
198         my $arg;
199
200         # Get our command-line args
201         foreach $arg (@ARGV) {
202                 if ($arg eq "-p" || $arg eq "--package-version") {
203                         $pkg_version = 1;
204                 }
205         }
206
207
208         if (! open(FILE, "<$vconf_file")) {
209                 print STDERR "Version configuration file $vconf_file not "
210                 . "found.  Using defaults.\n";
211                 return 1;
212         }
213
214         while (<FILE>) {
215                 chomp;
216                 next if (/^#/);
217                 next unless (/^(\w+):\s+(\S.*)/);
218                 $version_pref{$1} = $2;
219         }
220         close FILE;
221         return 1;
222 }
223
224 ##
225 ## Start of code
226 ##
227
228 &get_config();
229
230 if (-d "./.svn") {
231         print "This is a build from SVN (or a SVN snapshot).\n";
232         &read_svn_info(".");
233         if ($pkg_version) {
234                 print "Generating package version.  Ignoring $version_file\n";
235                 &update_configure_in;
236                 &update_config_nmake;
237         } elsif ($version_pref{"enable"} == 0) {
238                 print "Version tag disabled in $vconf_file.\n";
239                 $last = 0;
240                 $revision = 0;
241         } else {
242                 print "SVN version tag will be computed.\n";
243         }
244 } else {
245         print "This is not a SVN build.\n";
246 }
247
248 &print_svn_version;
249
250 __END__