Don't clobber release information in configure.in.
[metze/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 # See below for usage
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 local package versioning.
37 #   pkg_format - Like "format", but used for the local package version.
38 #
39 # If run with the "-r" or "--set-release" argument the AC_INIT macro in
40 # configure.in and the VERSION macro in config.nmake will have the
41 # pkg_format template appended to the version number. svnversion.h will
42 # _not_ be generated if either argument is present.
43 #
44 # Default configuration:
45 #
46 # enable: 1
47 # svn_client: 1
48 # format: SVN %Y%m%d%H%M%S
49 # pkg_enable: 1
50 # pkg_format: -SVN-%#
51
52 # XXX - We're pretty dumb about the "%#" substitution, and about having
53 # spaces in the package format.
54
55 use strict;
56
57 use Time::Local;
58 use POSIX qw(strftime);
59 use Getopt::Long;
60 use Pod::Usage;
61 use IO::Handle;
62 use English;
63
64 my $version_file = 'svnversion.h';
65 my $package_string = "";
66 my $vconf_file = 'version.conf';
67 my $tortoise_file = "tortoise_template";
68 my $last_change = 0;
69 my $revision = 0;
70 my $repo_path = "unknown";
71 my $get_svn = 0;
72 my $set_version = 0;
73 my $set_release = 0;
74 my %version_pref = (
75         "version_major" => 1,
76         "version_minor" => 7,
77         "version_micro" => 1,
78         "version_build" => 0,
79
80         "enable"        => 1,
81         "svn_client"    => 1,
82         "tortoise_svn"  => 0,
83         "format"        => "SVN %Y%m%d%H%M%S",
84         "is_release"    => 0,
85
86         # Normal development builds
87         "pkg_enable" => 1,
88         "pkg_format" => "-SVN-%#",
89
90         # Development releases
91         #"pkg_enable" => 0,
92         #"pkg_format" => "",
93         );
94 my $srcdir = ".";
95 my $svn_info_cmd = "";
96
97 $ENV{LANG} = "C";  # Ensure we run with correct locale
98
99 # Run "svn info".  Parse out the most recent modification time and the
100 # revision number.
101 sub read_svn_info {
102         my $line;
103         my $version_format = $version_pref{"format"};
104         my $package_format = "";
105         my $in_entries = 0;
106         my $svn_name;
107         my $repo_version;
108         my $repo_root = undef;
109         my $repo_url = undef;
110         my $do_hack = 1;
111         my $info_source = "Unknown";
112
113         if ($version_pref{"pkg_enable"}) {
114                 $package_format = $version_pref{"pkg_format"};
115         }
116
117         if (-d "$srcdir/.svn") {
118                 $info_source = "Command line (svn info)";
119                 $svn_info_cmd = "svn info $srcdir";
120         } elsif (-d "$srcdir/.git/svn") {
121                 $info_source = "Command line (git-svn)";
122                 $svn_info_cmd = "(cd $srcdir; git svn info)";
123         }
124
125         if ($version_pref{"svn_client"}) {
126                 eval {
127                         use warnings "all";
128                         no warnings "all";
129                         $line = qx{$svn_info_cmd};
130                         if (defined($line)) {
131                                 if ($line =~ /Last Changed Date: (\d{4})-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d)/) {
132                                         $last_change = timegm($6, $5, $4, $3, $2 - 1, $1);
133                                 }
134                                 if ($line =~ /Last Changed Rev: (\d+)/) {
135                                         $revision = $1;
136                                 }
137                                 if ($line =~ /URL: (\S+)/) {
138                                         $repo_url = $1;
139                                 }
140                                 if ($line =~ /Repository Root: (\S+)/) {
141                                         $repo_root = $1;
142                                 }
143                         }
144                         1;
145                 };
146
147                 if ($last_change && $revision && $repo_url && $repo_root) {
148                         $do_hack = 0;
149                 }
150         } elsif ($version_pref{"tortoise_svn"}) {
151                 # Dynamically generic template file needed by TortoiseSVN
152                 open(TORTOISE, ">$tortoise_file");
153                 print TORTOISE "#define SVNVERSION \"\$WCREV\$\"\r\n";
154                 print TORTOISE "#define SVNPATH \"\$WCURL\$\"\r\n";
155                 close(TORTOISE);
156
157                 $info_source = "Command line (SubWCRev)";
158                 $svn_info_cmd = "SubWCRev $srcdir $tortoise_file $version_file";
159                 my $tortoise = system($svn_info_cmd);
160                 if ($tortoise == 0) {
161                         $do_hack = 0;
162                 }
163
164                 #clean up the template file
165                 unlink($tortoise_file);
166         }
167
168         if ($revision == 0) {
169                 # Fall back to config.nmake
170                 $info_source = "Prodding config.nmake";
171                 my $filepath = "config.nmake";
172                 open(CFGNMAKE, "< $filepath") || die "Can't read $filepath!";
173                 while ($line = <CFGNMAKE>) {
174                         if ($line =~ /^SVN_REVISION=(\d+)/) {
175                                 $revision = $1;
176                                 $do_hack = 0;
177                                 last;
178                         }
179                 }
180                 close (CFGNMAKE);
181         }
182
183         # 'svn info' failed or the user really wants us to dig around in .svn/entries
184         if ($do_hack) {
185                 # Start of ugly internal SVN file hack
186                 if (! open (ENTRIES, "< $srcdir/.svn/entries")) {
187                         print ("Unable to open $srcdir/.svn/entries\n");
188                 } else {
189                         $info_source = "Prodding .svn";
190                         # We need to find out whether our parser can handle the entries file
191                         $line = <ENTRIES>;
192                         chomp $line;
193                         if ($line eq '<?xml version="1.0" encoding="utf-8"?>') {
194                                 $repo_version = "pre1.4";
195                         } elsif ($line =~ /^8$/) {
196                                 $repo_version = "1.4";
197                         } else {
198                                 $repo_version = "unknown";
199                         }
200
201                         if ($repo_version eq "pre1.4") {
202                                 # The entries schema is flat, so we can use regexes to parse its contents.
203                                 while ($line = <ENTRIES>) {
204                                         if ($line =~ /<entry$/ || $line =~ /<entry\s/) {
205                                                 $in_entries = 1;
206                                                 $svn_name = "";
207                                         }
208                                         if ($in_entries) {
209                                                 if ($line =~ /name="(.*)"/) { $svn_name = $1; }
210                                                 if ($line =~ /committed-date="(\d{4})-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)/) {
211                                                         $last_change = timegm($6, $5, $4, $3, $2 - 1, $1);
212                                                 }
213                                                 if ($line =~ /revision="(\d+)"/) { $revision = $1; }
214                                         }
215                                         if ($line =~ /\/>/) {
216                                                 if (($svn_name eq "" || $svn_name eq "svn:this_dir") &&
217                                                                 $last_change && $revision) {
218                                                         $in_entries = 0;
219                                                         last;
220                                                 }
221                                         }
222                                         # XXX - Fetch the repository root & URL
223                                 }
224                         }
225                         close ENTRIES;
226                 }
227         }
228
229         # If we picked up the revision and modification time,
230         # generate our strings.
231         if ($revision && $last_change) {
232                 $version_format =~ s/%#/$revision/;
233                 $package_format =~ s/%#/$revision/;
234                 $package_string = strftime($package_format, gmtime($last_change));
235         }
236
237         if ($repo_url && $repo_root && index($repo_url, $repo_root) == 0) {
238                 $repo_path = substr($repo_url, length($repo_root));
239         }
240
241         if ($get_svn) {
242                 print <<"Fin";
243 SVN revision    : $revision
244 Revision source : $info_source
245 Release stamp   : $package_string
246 Fin
247         }
248 }
249
250
251 # Read configure.in, then write it back out with an updated
252 # "AC_INIT" line.
253 sub update_configure_in
254 {
255         my $line;
256         my $contents = "";
257         my $version = "";
258         my $filepath = "configure.in";
259
260         return if (!$set_version && $package_string eq "");
261
262         open(CFGIN, "< $filepath") || die "Can't read $filepath!";
263         while ($line = <CFGIN>) {
264                 if ($line =~ /^AC_INIT\(wireshark, (\d+)\.(\d+).(\d+)/) {
265                         $line = sprintf("AC_INIT\(wireshark, %d.%d.%d%s)\n",
266                                         $set_version ? $version_pref{"version_major"} : $1,
267                                         $set_version ? $version_pref{"version_minor"} : $2,
268                                         $set_version ? $version_pref{"version_micro"} : $3,
269                                         $package_string
270                                        );
271
272                 }
273                 $contents .= $line
274         }
275
276         open(CFGIN, "> $filepath") || die "Can't write $filepath!";
277         print(CFGIN $contents);
278         close(CFGIN);
279         print "$filepath has been updated.\n";
280 }
281
282 # Read config.nmake, then write it back out with an updated
283 # "VERSION" line.
284 sub update_config_nmake
285 {
286         my $line;
287         my $contents = "";
288         my $version = "";
289         my $filepath = "config.nmake";
290
291         open(CFGNMAKE, "< $filepath") || die "Can't read $filepath!";
292         while ($line = <CFGNMAKE>) {
293                 if ($line =~ /^SVN_REVISION=/) {
294                         $line = sprintf("SVN_REVISION=%d\n", $revision);
295                 } elsif ($set_version && $line =~ /^VERSION_MAJOR=/) {
296                         $line = sprintf("VERSION_MAJOR=%d\n", $version_pref{"version_major"});
297                 } elsif ($set_version && $line =~ /^VERSION_MINOR=/) {
298                         $line = sprintf("VERSION_MINOR=%d\n", $version_pref{"version_minor"});
299                 } elsif ($set_version && $line =~ /^VERSION_MICRO=/) {
300                         $line = sprintf("VERSION_MICRO=%d\n", $version_pref{"version_micro"});
301                 } elsif ($line =~ /^VERSION_EXTRA=/) {
302                         $line = "VERSION_EXTRA=$package_string\n";
303                 }
304                 $contents .= $line
305         }
306
307         open(CFGNMAKE, "> $filepath") || die "Can't write $filepath!";
308         print(CFGNMAKE $contents);
309         close(CFGNMAKE);
310         print "$filepath has been updated.\n";
311 }
312
313 # Read docbook/release_notes.xml, then write it back out with an updated
314 # "WiresharkCurrentVersion" line.
315 sub update_release_notes
316 {
317         my $line;
318         my $contents = "";
319         my $version = "";
320         my $filepath = "docbook/release-notes.xml";
321
322         return if (!$set_version);
323
324         open(RELNOTES, "< $filepath") || die "Can't read $filepath!";
325         while ($line = <RELNOTES>) {
326                 #   <!ENTITY WiresharkCurrentVersion "1.7.1">
327
328                 if ($line =~ /<\!ENTITY\s+WiresharkCurrentVersion\s+/) {
329                         $line = sprintf("<!ENTITY WiresharkCurrentVersion \"%d.%d.%d\">\n",
330                                         $version_pref{"version_major"},
331                                         $version_pref{"version_minor"},
332                                         $version_pref{"version_micro"},
333                                        );
334                 }
335                 $contents .= $line
336         }
337
338         open(RELNOTES, "> $filepath") || die "Can't write $filepath!";
339         print(RELNOTES $contents);
340         close(RELNOTES);
341         print "$filepath has been updated.\n";
342 }
343
344 # Read debian/changelog, then write back out an updated version.
345 sub update_debian_changelog
346 {
347         my $line;
348         my $contents = "";
349         my $version = "";
350         my $filepath = "debian/changelog";
351
352         return if ($set_version == 0);
353
354         open(CHANGELOG, "< $filepath") || die "Can't read $filepath!";
355         while ($line = <CHANGELOG>) {
356                 if ($set_version && CHANGELOG->input_line_number() == 1) {
357                         $line = sprintf("wireshark (%d.%d.%d) unstable; urgency=low\n",
358                                         $version_pref{"version_major"},
359                                         $version_pref{"version_minor"},
360                                         $version_pref{"version_micro"},
361                                        );
362                 }
363                 $contents .= $line
364         }
365
366         open(CHANGELOG, "> $filepath") || die "Can't write $filepath!";
367         print(CHANGELOG $contents);
368         close(CHANGELOG);
369         print "$filepath has been updated.\n";
370 }
371
372 # Update distributed files that contain any version information
373 sub update_versioned_files
374 {
375         &update_configure_in;
376         &update_config_nmake;
377         &update_release_notes;
378         &update_debian_changelog;
379 }
380
381 # Print the SVN version to $version_file.
382 # Don't change the file if it is not needed.
383 sub print_svn_revision
384 {
385         my $svn_revision;
386         my $needs_update = 1;
387
388         if ($last_change && $revision) {
389                 $svn_revision = "#define SVNVERSION \"SVN Rev " .
390                         $revision . "\"\n" .
391                         "#define SVNPATH \"" . $repo_path . "\"\n";
392         } else {
393                 $svn_revision = "#define SVNVERSION \"SVN Rev Unknown\"\n" .
394                         "#define SVNPATH \"unknown\"\n";
395         }
396         if (open(OLDREV, "<$version_file")) {
397                 my $old_svn_revision = <OLDREV> . <OLDREV>;
398                 if ($old_svn_revision eq $svn_revision) {
399                         $needs_update = 0;
400                 }
401                 close OLDREV;
402         }
403
404         if (! $set_version && ! $set_release) { return; }
405
406         if ($needs_update) {
407                 # print "Updating $version_file so it contains:\n$svn_revision";
408                 open(VER, ">$version_file") || die ("Cannot write to $version_file ($!)\n");
409                 print VER "$svn_revision";
410                 close VER;
411                 print "$version_file has been updated.\n";
412         } else {
413                 print "$version_file unchanged.\n";
414         }
415 }
416
417 # Read values from the configuration file, if it exists.
418 sub get_config {
419         my $arg;
420         my $show_help = 0;
421
422         # Get our command-line args
423         # XXX - Do we need an option to undo --set-release?
424         GetOptions(
425                    "help|h", \$show_help,
426                    "get-svn|g", \$get_svn,
427                    "set-version|v", \$set_version,
428                    "set-release|r|package-version|p", \$set_release
429                    ) || pod2usage(2);
430
431         if ($show_help) { pod2usage(1); }
432
433         if ( !( $show_help || $get_svn || $set_release ) ) {
434                 $set_version = 1;
435         }
436
437         if ($#ARGV >= 0) {
438                 $srcdir = $ARGV[0]
439         }
440
441         if (! open(FILE, "<$vconf_file")) {
442                 print STDERR "Version configuration file $vconf_file not "
443                 . "found.  Using defaults.\n";
444                 return 1;
445         }
446
447         while (<FILE>) {
448                 chomp;
449                 next if (/^#/);
450                 next unless (/^(\w+)(:|=)\s*(\S.*)/);
451                 $version_pref{$1} = $3;
452         }
453         close FILE;
454         return 1;
455 }
456
457 ##
458 ## Start of code
459 ##
460
461 &get_config();
462
463 &read_svn_info();
464
465 &print_svn_revision;
466
467 if ($set_version || $set_release) {
468         if ($set_version) {
469                 print "Generating version information\n";
470         }
471
472         if ($version_pref{"enable"} == 0) {
473                 print "Release information disabled in $vconf_file.\n";
474                 $set_release = 0;
475         }
476
477         if ($set_release) {
478                 print "Generating release information\n";
479         }
480
481         &update_versioned_files;
482 }
483
484 __END__
485
486 =head1 NAM
487
488 make-version.pl - Get and set build-time version information for Wireshark
489
490 =head1 SYNOPSIS
491
492 make-version.pl [options] [source directory]
493
494   Options:
495
496     --help, -h                 This help message
497     --get-svn, -g              Print the SVN revision and source.
498     --set-version, -v          Set the major, minor, and micro versions.
499                                Resets the release information when used by
500                                itself.
501     --set-release, -r          Set the release information.
502     --package-version, -p      Deprecated. Same as --set-release.
503
504 Options can be used in any combination. If none are specified B<--set-version>
505 is assumed.