Remove debugging output.
[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
79         open(SVNINFO, "svn info |") || die("Unable to get SVN info!");
80         while ($line = <SVNINFO>) {
81                 if ($line =~ /^Last Changed Date: (\d{4})-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d)/) {
82                         $last = timegm($6, $5, $4, $3, $2 - 1, $1);
83                 }
84                 if ($line =~ /^Revision: (\d+)/) {
85                         $revision = $1;
86                 }
87         }
88         close SVNINFO;
89
90         if ($last && $revision) {
91                 $version_format =~ s/%#/$revision/;
92                 $version_string = strftime($version_format, gmtime($last));
93
94                 $package_format =~ s/%#/$revision/;
95                 $package_string = strftime($package_format, gmtime($last));
96         }
97 }
98
99
100 # Read configure.in, then write it back out with an updated 
101 # "AM_INIT_AUTOMAKE" line.
102 sub update_configure_in
103 {
104         my $line;
105         my $contents = "";
106         my $version = "";
107         
108         return if ($package_string eq "");
109         
110         open(CFGIN, "< configure.in") || die "Can't read configure.in!";
111         while ($line = <CFGIN>) {
112                 if ($line =~ /^AM_INIT_AUTOMAKE\(ethereal, (\d+)\.(\d+).(\d+)/) {
113                         $line = "AM_INIT_AUTOMAKE\(ethereal, $1.$2.$3$package_string)\n";
114                 }
115                 $contents .= $line
116         }
117         
118         open(CFGIN, "> configure.in") || die "Can't write configure.in!";
119         print(CFGIN $contents);
120         close(CFGIN);
121         print "configure.in has been updated.\n";
122 }
123
124 # Read config.nmake, then write it back out with an updated 
125 # "VERSION" line.
126 sub update_config_nmake
127 {
128         my $line;
129         my $contents = "";
130         my $version = "";
131         
132         return if ($package_string eq "");
133         
134         open(CFGIN, "< config.nmake") || die "Can't read config.nmake!";
135         while ($line = <CFGIN>) {
136                 if ($line =~ /^VERSION=(\d+)\.(\d+).(\d+)/) {
137                         $line = "VERSION=$1.$2.$3$package_string\n";
138                 }
139                 $contents .= $line
140         }
141         
142         open(CFGIN, "> config.nmake") || die "Can't write config.nmake!";
143         print(CFGIN $contents);
144         close(CFGIN);
145         print "config.nmake has been updated.\n";
146 }
147
148
149
150 # Print the SVN version to $version_file.
151 # Don't change the file if it is not needed.
152 sub print_svn_version
153 {
154         my $svn_version;
155         my $needs_update = 1;
156
157         if ($pkg_version) { return; }
158
159         if ($last && $revision) {
160                 $svn_version = "#define SVNVERSION \"" . 
161                         $version_string . "\"\n";
162         } else {
163                 $svn_version = "/* #define SVNVERSION \"\" */\n";
164         }
165         if (open(OLDVER, "<$version_file")) {
166                 if (<OLDVER> eq $svn_version) {
167                         print "$version_file is up-to-date.\n";
168                         $needs_update = 0;
169                 }
170                 close OLDVER;
171         }
172
173         if ($needs_update == 1) {
174                 # print "Updating $version_file so it contains:\n$svn_version";
175                 open(VER, ">$version_file") || die ("Cannot write to $version_file ($!)\n");
176                 print VER "$svn_version";
177                 close VER;
178                 print "$version_file has been updated.\n";
179         }
180 }
181
182 # Read values from the configuration file, if it exists.
183 sub get_config {
184         my $arg;
185
186         # Get our command-line args
187         foreach $arg (@ARGV) {
188                 if ($arg eq "-p" || $arg eq "--package-version") {
189                         $pkg_version = 1;
190                 }
191         }
192
193
194         if (! open(FILE, "<$vconf_file")) {
195                 print STDERR "Version configuration file $vconf_file not "
196                 . "found.  Using defaults.\n";
197                 return 1;
198         }
199
200         while (<FILE>) {
201                 chomp;
202                 next if (/^#/);
203                 next unless (/^(\w+):\s+(\S.*)/);
204                 $version_pref{$1} = $2;
205         }
206         close FILE;
207         return 1;
208 }
209
210 ##
211 ## Start of code
212 ##
213
214 &get_config();
215
216 if (-d "./.svn") {
217         print "This is a build from SVN (or a SVN snapshot).\n";
218         &read_svn_info(".");
219         if ($pkg_version) {
220                 print "Generating package version.  Ignoring $version_file\n";
221                 &update_configure_in;
222                 &update_config_nmake;
223         } elsif ($version_pref{"enable"} == 0) {
224                 print "Version tag disabled in $vconf_file.\n";
225         } else {
226                 print "SVN version tag will be computed.\n";
227         }
228 } else {
229         print "This is not a SVN build.\n";
230 }
231
232 &print_svn_version;
233
234 __END__