Update the information about getting Xcode, and note that
[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 # Wireshark - Network traffic analyzer
8 # By Gerald Combs <gerald@wireshark.org>
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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 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 #   svn_client - Use svn client i.s.o. ugly internal SVN file hack
33 #   format     - A strftime() formatted string to use as a template for
34 #                the version string.  The sequence "%#" will substitute
35 #                the SVN revision number.
36 #   pkg_enable - Enable or disable package versioning.
37 #   pkg_format - Like "format", but used for the package version.
38 #
39 # If run with the "-p" or "--package-version" argument, the
40 # AC_INIT macro in configure.in and the VERSION macro in
41 # config.nmake will have the pkg_format template appended to the 
42 # version number.  svnversion.h will _not_ be generated if either
43 # argument is present.
44 #
45 # Default configuration:
46 #
47 # enable: 1
48 # svn_client: 1
49 # format: SVN %Y%m%d%H%M%S
50 # pkg_enable: 1
51 # pkg_format: -SVN-%#
52
53 # XXX - We're pretty dumb about the "%#" substitution, and about having
54 # spaces in the package format.
55
56 use strict;
57
58 use Time::Local;
59 use POSIX qw(strftime);
60 use Getopt::Long;
61
62 my $version_file = 'svnversion.h';
63 my $package_string = "";
64 my $vconf_file = 'version.conf';
65 my $last_change = 0;
66 my $revision = 0;
67 my $repo_path = "unknown";
68 my $pkg_version = 0;
69 my %version_pref = (
70         "enable"     => 1,
71         "svn_client" => 1,
72         "format"     => "SVN %Y%m%d%H%M%S",
73
74         # Normal development builds
75         "pkg_enable" => 1,
76         "pkg_format" => "-SVN-%#",
77
78         # Development releases
79         #"pkg_enable" => 0,
80         #"pkg_format" => "",
81         );
82 my $srcdir = ".";
83 my $svn_info_cmd = "";
84
85 $ENV{LANG} = "C";  # Ensure we run with correct locale
86
87 # Run "svn info".  Parse out the most recent modification time and the
88 # revision number.
89 sub read_svn_info {
90         my $line;
91         my $version_format = $version_pref{"format"};
92         my $package_format = "";
93         my $in_entries = 0;
94         my $svn_name;
95         my $repo_version;
96         my $repo_root = undef;
97         my $repo_url = undef;
98         my $do_hack = 1;
99
100         if ($version_pref{"pkg_enable"}) {
101                 $package_format = $version_pref{"pkg_format"};
102         }
103
104         if ($version_pref{"svn_client"}) {
105                 eval {
106                         use warnings "all";
107                         no warnings "all";
108                         $line = qx{$svn_info_cmd};
109                         if (defined($line)) {
110                                 if ($line =~ /Last Changed Date: (\d{4})-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d)/) {
111                                         $last_change = timegm($6, $5, $4, $3, $2 - 1, $1);
112                                 }
113                                 if ($line =~ /Last Changed Rev: (\d+)/) {
114                                         $revision = $1;
115                                 }
116                                 if ($line =~ /URL: (\S+)/) {
117                                         $repo_url = $1;
118                                 }
119                                 if ($line =~ /Repository Root: (\S+)/) {
120                                         $repo_root = $1;
121                                 }
122                         }
123                         1;
124                 };
125
126                 if ($last_change && $revision && $repo_url && $repo_root) {
127                         $do_hack = 0;
128                 }
129         }
130
131         # 'svn info' failed or the user really wants us to dig around in .svn/entries
132         if ($do_hack) {
133                 # Start of ugly internal SVN file hack
134                 if (! open (ENTRIES, "< $srcdir/.svn/entries")) {
135                         print ("Unable to open $srcdir/.svn/entries\n");
136                 } else {
137                         # We need to find out whether our parser can handle the entries file
138                         $line = <ENTRIES>;
139                         chomp $line;
140                         if ($line eq '<?xml version="1.0" encoding="utf-8"?>') {
141                                 $repo_version = "pre1.4";
142                         } elsif ($line =~ /^8$/) {
143                                 $repo_version = "1.4";
144                         } else {
145                                 $repo_version = "unknown";
146                         }
147
148                         if ($repo_version eq "pre1.4") {
149                                 # The entries schema is flat, so we can use regexes to parse its contents.
150                                 while ($line = <ENTRIES>) {
151                                         if ($line =~ /<entry$/ || $line =~ /<entry\s/) {
152                                                 $in_entries = 1;
153                                                 $svn_name = "";
154                                         }
155                                         if ($in_entries) {
156                                                 if ($line =~ /name="(.*)"/) { $svn_name = $1; }
157                                                 if ($line =~ /committed-date="(\d{4})-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)/) {
158                                                         $last_change = timegm($6, $5, $4, $3, $2 - 1, $1);
159                                                 }
160                                                 if ($line =~ /revision="(\d+)"/) { $revision = $1; }
161                                         }
162                                         if ($line =~ /\/>/) {
163                                                 if (($svn_name eq "" || $svn_name eq "svn:this_dir") &&
164                                                                 $last_change && $revision) {
165                                                         $in_entries = 0;
166                                                         last;
167                                                 }
168                                         }
169                                         # XXX - Fetch the repository root & URL
170                                 }
171                         }
172                         close ENTRIES;
173                 }
174         }
175
176         # If we picked up the revision and modification time, 
177         # generate our strings.
178         if ($revision && $last_change) {
179                 $version_format =~ s/%#/$revision/;
180                 $package_format =~ s/%#/$revision/;
181                 $package_string = strftime($package_format, gmtime($last_change));
182         }
183         
184         if ($repo_url && $repo_root && index($repo_url, $repo_root) == 0) {
185                 $repo_path = substr($repo_url, length($repo_root));
186         }
187 }
188
189
190 # Read configure.in, then write it back out with an updated 
191 # "AC_INIT" line.
192 sub update_configure_in
193 {
194         my $line;
195         my $contents = "";
196         my $version = "";
197         
198         return if ($package_string eq "");
199         
200         open(CFGIN, "< configure.in") || die "Can't read configure.in!";
201         while ($line = <CFGIN>) {
202                 if ($line =~ /^AC_INIT\(wireshark, (\d+)\.(\d+).(\d+)/) {
203                         $line = "AC_INIT\(wireshark, $1.$2.$3$package_string)\n";
204                 }
205                 $contents .= $line
206         }
207         
208         open(CFGIN, "> configure.in") || die "Can't write configure.in!";
209         print(CFGIN $contents);
210         close(CFGIN);
211         print "configure.in has been updated.\n";
212 }
213
214 # Read config.nmake, then write it back out with an updated 
215 # "VERSION" line.
216 sub update_config_nmake
217 {
218         my $line;
219         my $contents = "";
220         my $version = "";
221         my $update_ve = 0;
222         
223         if ($package_string ne "") { $update_ve = 1; };
224         
225         open(CFGIN, "< config.nmake") || die "Can't read config.nmake!";
226         while ($line = <CFGIN>) {
227                 if ($update_ve && $line =~ /^VERSION_EXTRA=/) {
228                         $line = "VERSION_EXTRA=$package_string\n";
229                 }
230                 if ($line =~ /^VERSION_BUILD=/ && int($revision) > 0) {
231                         $line = "VERSION_BUILD=$revision\n";
232                 }
233                 $contents .= $line
234         }
235         
236         open(CFGIN, "> config.nmake") || die "Can't write config.nmake!";
237         print(CFGIN $contents);
238         close(CFGIN);
239         print "config.nmake has been updated.\n";
240 }
241
242
243
244 # Print the SVN version to $version_file.
245 # Don't change the file if it is not needed.
246 sub print_svn_version
247 {
248         my $svn_version;
249         my $needs_update = 1;
250
251         if ($pkg_version) { return; }
252
253         if ($last_change && $revision) {
254                 $svn_version = "#define SVNVERSION \"SVN Rev " . 
255                         $revision . "\"\n" .
256                         "#define SVNPATH \"" . $repo_path . "\"\n";
257         } else {
258                 $svn_version = "\n";
259         }
260         if (open(OLDVER, "<$version_file")) {
261                 my $old_svn_version = <OLDVER> . <OLDVER>;
262                 if ($old_svn_version eq $svn_version) {
263                         $needs_update = 0;
264                 }
265                 close OLDVER;
266         }
267
268         if ($needs_update == 1) {
269                 # print "Updating $version_file so it contains:\n$svn_version";
270                 open(VER, ">$version_file") || die ("Cannot write to $version_file ($!)\n");
271                 print VER "$svn_version";
272                 close VER;
273                 print "$version_file has been updated.\n";
274         } else {
275                 print "$version_file is up-to-date.\n";
276         }
277 }
278
279 # Read values from the configuration file, if it exists.
280 sub get_config {
281         my $arg;
282
283         # Get our command-line args
284         GetOptions("package-version", \$pkg_version);
285
286         if ($#ARGV >= 0) {
287                 $srcdir = $ARGV[0]
288         }
289
290
291         if (! open(FILE, "<$vconf_file")) {
292                 print STDERR "Version configuration file $vconf_file not "
293                 . "found.  Using defaults.\n";
294                 return 1;
295         }
296
297         while (<FILE>) {
298                 chomp;
299                 next if (/^#/);
300                 next unless (/^(\w+)(:|=)\s*(\S.*)/);
301                 $version_pref{$1} = $3;
302         }
303         close FILE;
304         return 1;
305 }
306
307 ##
308 ## Start of code
309 ##
310
311 &get_config();
312
313 if (-d "$srcdir/.svn") {
314         $svn_info_cmd = "svn info $srcdir";
315 } elsif (-d "$srcdir/.git/svn") {
316         $svn_info_cmd = "git svn info $srcdir";
317 }
318
319 if ($svn_info_cmd) {
320         print "This is a build from SVN (or a SVN snapshot).\n";
321         &read_svn_info();
322         if ($pkg_version) {
323                 print "Generating package version.  Ignoring $version_file\n";
324                 &update_configure_in;
325                 &update_config_nmake;
326         } elsif ($version_pref{"enable"} == 0) {
327                 print "Version tag disabled in $vconf_file.\n";
328                 $last_change = 0;
329                 $revision = 0;
330         } else {
331                 print "SVN version tag will be computed.\n";
332         }
333 } else {
334         print "This is not a SVN build.\n";
335                 $last_change = 0;
336                 $revision = 0;
337 }
338
339 &print_svn_version;
340
341 __END__