Handle the case where there are no IDBs before the first non-SHB/non-IDB
[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 # 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_svn = 0;
73 my $set_version = 0;
74 my $set_release = 0;
75 my %version_pref = (
76         "version_major" => 1,
77         "version_minor" => 7,
78         "version_micro" => 1,
79         "version_build" => 0,
80
81         "enable"        => 1,
82         "svn_client"    => 1,
83         "tortoise_svn"  => 0,
84         "format"        => "SVN %Y%m%d%H%M%S",
85         "is_release"    => 0,
86
87         # Normal development builds
88         "pkg_enable" => 1,
89         "pkg_format" => "-SVN-%#",
90
91         # Development releases
92         #"pkg_enable" => 0,
93         #"pkg_format" => "",
94         );
95 my $srcdir = ".";
96 my $svn_info_cmd = "";
97
98 $ENV{LANG} = "C";  # Ensure we run with correct locale
99
100 # Run "svn info".  Parse out the most recent modification time and the
101 # revision number.
102 sub read_svn_info {
103         my $line;
104         my $version_format = $version_pref{"format"};
105         my $package_format = "";
106         my $in_entries = 0;
107         my $svn_name;
108         my $repo_version;
109         my $repo_root = undef;
110         my $repo_url = undef;
111         my $do_hack = 1;
112         my $info_source = "Unknown";
113
114         if ($version_pref{"pkg_enable"}) {
115                 $package_format = $version_pref{"pkg_format"};
116         }
117
118         if (-d "$srcdir/.svn") {
119                 $info_source = "Command line (svn info)";
120                 $svn_info_cmd = "svn info $srcdir";
121         } elsif (-d "$srcdir/.git/svn") {
122                 $info_source = "Command line (git-svn)";
123                 $svn_info_cmd = "(cd $srcdir; git svn info)";
124         }
125
126         if ($version_pref{"svn_client"}) {
127                 eval {
128                         use warnings "all";
129                         no warnings "all";
130                         $line = qx{$svn_info_cmd};
131                         if (defined($line)) {
132                                 if ($line =~ /Last Changed Date: (\d{4})-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d)/) {
133                                         $last_change = timegm($6, $5, $4, $3, $2 - 1, $1);
134                                 }
135                                 if ($line =~ /Last Changed Rev: (\d+)/) {
136                                         $revision = $1;
137                                 }
138                                 if ($line =~ /URL: (\S+)/) {
139                                         $repo_url = $1;
140                                 }
141                                 if ($line =~ /Repository Root: (\S+)/) {
142                                         $repo_root = $1;
143                                 }
144                         }
145                         1;
146                 };
147
148                 if ($last_change && $revision && $repo_url && $repo_root) {
149                         $do_hack = 0;
150                 }
151         } elsif ($version_pref{"tortoise_svn"}) {
152                 # Dynamically generic template file needed by TortoiseSVN
153                 open(TORTOISE, ">$tortoise_file");
154                 print TORTOISE "#define SVNVERSION \"\$WCREV\$\"\r\n";
155                 print TORTOISE "#define SVNPATH \"\$WCURL\$\"\r\n";
156                 close(TORTOISE);
157
158                 $info_source = "Command line (SubWCRev)";
159                 $svn_info_cmd = "SubWCRev $srcdir $tortoise_file $version_file";
160                 my $tortoise = system($svn_info_cmd);
161                 if ($tortoise == 0) {
162                         $do_hack = 0;
163                 }
164
165                 #clean up the template file
166                 unlink($tortoise_file);
167         }
168
169         if ($revision == 0) {
170                 # Fall back to config.nmake
171                 $info_source = "Prodding config.nmake";
172                 my $filepath = "$srcdir/config.nmake";
173                 open(CFGNMAKE, "< $filepath") || die "Can't read $filepath!";
174                 while ($line = <CFGNMAKE>) {
175                         if ($line =~ /^SVN_REVISION=(\d+)/) {
176                                 $revision = $1;
177                                 $do_hack = 0;
178                                 last;
179                         }
180                 }
181                 close (CFGNMAKE);
182         }
183
184         # 'svn info' failed or the user really wants us to dig around in .svn/entries
185         if ($do_hack) {
186                 # Start of ugly internal SVN file hack
187                 if (! open (ENTRIES, "< $srcdir/.svn/entries")) {
188                         print ("Unable to open $srcdir/.svn/entries\n");
189                 } else {
190                         $info_source = "Prodding .svn";
191                         # We need to find out whether our parser can handle the entries file
192                         $line = <ENTRIES>;
193                         chomp $line;
194                         if ($line eq '<?xml version="1.0" encoding="utf-8"?>') {
195                                 $repo_version = "pre1.4";
196                         } elsif ($line =~ /^8$/) {
197                                 $repo_version = "1.4";
198                         } else {
199                                 $repo_version = "unknown";
200                         }
201
202                         if ($repo_version eq "pre1.4") {
203                                 # The entries schema is flat, so we can use regexes to parse its contents.
204                                 while ($line = <ENTRIES>) {
205                                         if ($line =~ /<entry$/ || $line =~ /<entry\s/) {
206                                                 $in_entries = 1;
207                                                 $svn_name = "";
208                                         }
209                                         if ($in_entries) {
210                                                 if ($line =~ /name="(.*)"/) { $svn_name = $1; }
211                                                 if ($line =~ /committed-date="(\d{4})-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)/) {
212                                                         $last_change = timegm($6, $5, $4, $3, $2 - 1, $1);
213                                                 }
214                                                 if ($line =~ /revision="(\d+)"/) { $revision = $1; }
215                                         }
216                                         if ($line =~ /\/>/) {
217                                                 if (($svn_name eq "" || $svn_name eq "svn:this_dir") &&
218                                                                 $last_change && $revision) {
219                                                         $in_entries = 0;
220                                                         last;
221                                                 }
222                                         }
223                                         # XXX - Fetch the repository root & URL
224                                 }
225                         }
226                         close ENTRIES;
227                 }
228         }
229
230         # If we picked up the revision and modification time,
231         # generate our strings.
232         if ($revision && $last_change) {
233                 $version_format =~ s/%#/$revision/;
234                 $package_format =~ s/%#/$revision/;
235                 $package_string = strftime($package_format, gmtime($last_change));
236         }
237
238         if ($repo_url && $repo_root && index($repo_url, $repo_root) == 0) {
239                 $repo_path = substr($repo_url, length($repo_root));
240         }
241
242         if ($get_svn) {
243                 print <<"Fin";
244 SVN revision    : $revision
245 Revision source : $info_source
246 Release stamp   : $package_string
247 Fin
248         }
249 }
250
251
252 # Read configure.in, then write it back out with an updated
253 # "AC_INIT" line.
254 sub update_configure_in
255 {
256         my $line;
257         my $contents = "";
258         my $version = "";
259         my $filepath = "$srcdir/configure.in";
260
261         return if (!$set_version && $package_string eq "");
262
263         open(CFGIN, "< $filepath") || die "Can't read $filepath!";
264         while ($line = <CFGIN>) {
265                 if ($line =~ /^AC_INIT\(wireshark, (\d+)\.(\d+).(\d+).*([\r\n]+)$/) {
266                         $line = sprintf("AC_INIT\(wireshark, %d.%d.%d%s)$4",
267                                         $set_version ? $version_pref{"version_major"} : $1,
268                                         $set_version ? $version_pref{"version_minor"} : $2,
269                                         $set_version ? $version_pref{"version_micro"} : $3,
270                                         $package_string
271                                        );
272
273                 }
274                 $contents .= $line
275         }
276
277         open(CFGIN, "> $filepath") || die "Can't write $filepath!";
278         print(CFGIN $contents);
279         close(CFGIN);
280         print "$filepath has been updated.\n";
281 }
282
283 # Read config.nmake, then write it back out with an updated
284 # "VERSION" line.
285 sub update_config_nmake
286 {
287         my $line;
288         my $contents = "";
289         my $version = "";
290         my $filepath = "$srcdir/config.nmake";
291
292         open(CFGNMAKE, "< $filepath") || die "Can't read $filepath!";
293         while ($line = <CFGNMAKE>) {
294                 if ($line =~ /^SVN_REVISION=.*([\r\n]+)$/) {
295                         $line = sprintf("SVN_REVISION=%d$1", $revision);
296                 } elsif ($set_version && $line =~ /^VERSION_MAJOR=.*([\r\n]+)$/) {
297                         $line = sprintf("VERSION_MAJOR=%d$1", $version_pref{"version_major"});
298                 } elsif ($set_version && $line =~ /^VERSION_MINOR=.*([\r\n]+)$/) {
299                         $line = sprintf("VERSION_MINOR=%d$1", $version_pref{"version_minor"});
300                 } elsif ($set_version && $line =~ /^VERSION_MICRO=.*([\r\n]+)$/) {
301                         $line = sprintf("VERSION_MICRO=%d$1", $version_pref{"version_micro"});
302                 } elsif ($line =~ /^VERSION_EXTRA=.*([\r\n]+)$/) {
303                         $line = "VERSION_EXTRA=$package_string$1";
304                 }
305                 $contents .= $line
306         }
307
308         open(CFGNMAKE, "> $filepath") || die "Can't write $filepath!";
309         print(CFGNMAKE $contents);
310         close(CFGNMAKE);
311         print "$filepath has been updated.\n";
312 }
313
314 # Read docbook/release_notes.xml, then write it back out with an updated
315 # "WiresharkCurrentVersion" line.
316 sub update_release_notes
317 {
318         my $line;
319         my $contents = "";
320         my $version = "";
321         my $filepath = "$srcdir/docbook/release-notes.xml";
322
323         return if (!$set_version);
324
325         open(RELNOTES, "< $filepath") || die "Can't read $filepath!";
326         while ($line = <RELNOTES>) {
327                 #   <!ENTITY WiresharkCurrentVersion "1.7.1">
328
329                 if ($line =~ /<\!ENTITY\s+WiresharkCurrentVersion\s+.*([\r\n]+)$/) {
330                         $line = sprintf("<!ENTITY WiresharkCurrentVersion \"%d.%d.%d\">$1",
331                                         $version_pref{"version_major"},
332                                         $version_pref{"version_minor"},
333                                         $version_pref{"version_micro"},
334                                        );
335                 }
336                 $contents .= $line
337         }
338
339         open(RELNOTES, "> $filepath") || die "Can't write $filepath!";
340         print(RELNOTES $contents);
341         close(RELNOTES);
342         print "$filepath has been updated.\n";
343 }
344
345 # Read debian/changelog, then write back out an updated version.
346 sub update_debian_changelog
347 {
348         my $line;
349         my $contents = "";
350         my $version = "";
351         my $filepath = "$srcdir/debian/changelog";
352
353         return if ($set_version == 0);
354
355         open(CHANGELOG, "< $filepath") || die "Can't read $filepath!";
356         while ($line = <CHANGELOG>) {
357                 if ($set_version && CHANGELOG->input_line_number() == 1) {
358                         $line = sprintf("wireshark (%d.%d.%d) unstable; urgency=low\n",
359                                         $version_pref{"version_major"},
360                                         $version_pref{"version_minor"},
361                                         $version_pref{"version_micro"},
362                                        );
363                 }
364                 $contents .= $line
365         }
366
367         open(CHANGELOG, "> $filepath") || die "Can't write $filepath!";
368         print(CHANGELOG $contents);
369         close(CHANGELOG);
370         print "$filepath has been updated.\n";
371 }
372
373 # Read debian/wireshark-common.files, then write back out an updated version.
374 # The libraries updated here MUST match the updates made by update_lib_releases
375 # below. We should do this automatically.
376 sub update_debian_wcf
377 {
378         my $line;
379         my $contents = "";
380         my $version = "";
381         my $filepath = "$srcdir/debian/wireshark-common.files";
382
383         return if (!$set_version);
384
385         open(DWCF, "< $filepath") || die "Can't read $filepath!";
386         while ($line = <DWCF>) {
387                 # /usr/lib/wireshark/libwireshark.so.1.1.0
388
389                 if ($line =~ qr{^(/usr/lib/wireshark/lib(wireshark|wiretap).so\.\d+\.\d+\.)\d+$}) {
390                         $line = sprintf("$1%d\n", $version_pref{"version_micro"});
391                 }
392                 $contents .= $line
393         }
394
395         open(DWCF, "> $filepath") || die "Can't write $filepath!";
396         print(DWCF $contents);
397         close(DWCF);
398         print "$filepath has been updated.\n";
399 }
400
401 # Read Makefile.am for each library, then write back out an updated version.
402 sub update_lib_releases
403 {
404         my $line;
405         my $contents = "";
406         my $version = "";
407         my $filedir;
408         my $filepath;
409
410         return if (!$set_version);
411
412         # The Libtool manual says
413         #   "If the library source code has changed at all since the last
414         #    update, then increment revision (‘c:r:a’ becomes ‘c:r+1:a’)."
415         # epan changes with each minor release, almost by definition. wiretap
416         # changes with *most* releases.
417         #
418         # http://www.gnu.org/software/libtool/manual/libtool.html#Updating-version-info
419         for $filedir ("epan", "wiretap") {      # "wsutil"
420                 $contents = "";
421                 $filepath = $filedir . "/Makefile.am";
422                 open(MAKEFILE_AM, "< $filepath") || die "Can't read $filepath!";
423                 while ($line = <MAKEFILE_AM>) {
424                         # libwireshark_la_LDFLAGS = -version-info 2:1:1 -export-symbols
425
426                         if ($line =~ /^(lib\w+_la_LDFLAGS.*version-info\s+\d+:)\d+(:\d+.*)/) {
427                                 $line = sprintf("$1%d$2\n", $version_pref{"version_micro"});
428                         }
429                         $contents .= $line
430                 }
431
432                 open(MAKEFILE_AM, "> $filepath") || die "Can't write $filepath!";
433                 print(MAKEFILE_AM $contents);
434                 close(MAKEFILE_AM);
435                 print "$filepath has been updated.\n";
436         }
437 }
438
439 # Update distributed files that contain any version information
440 sub update_versioned_files
441 {
442         &update_configure_in;
443         &update_config_nmake;
444         &update_release_notes;
445         &update_debian_changelog;
446         &update_debian_wcf;
447         &update_lib_releases;
448 }
449
450 # Print the SVN version to $version_file.
451 # Don't change the file if it is not needed.
452 sub print_svn_revision
453 {
454         my $svn_revision;
455         my $needs_update = 1;
456
457         if ($last_change && $revision) {
458                 $svn_revision = "#define SVNVERSION \"SVN Rev " .
459                         $revision . "\"\n" .
460                         "#define SVNPATH \"" . $repo_path . "\"\n";
461         } else {
462                 $svn_revision = "#define SVNVERSION \"SVN Rev Unknown\"\n" .
463                         "#define SVNPATH \"unknown\"\n";
464         }
465         if (open(OLDREV, "<$version_file")) {
466                 my $old_svn_revision = <OLDREV> . <OLDREV>;
467                 if ($old_svn_revision eq $svn_revision) {
468                         $needs_update = 0;
469                 }
470                 close OLDREV;
471         }
472
473         if (! $set_svn) { return; }
474
475         if ($needs_update) {
476                 # print "Updating $version_file so it contains:\n$svn_revision";
477                 open(VER, ">$version_file") || die ("Cannot write to $version_file ($!)\n");
478                 print VER "$svn_revision";
479                 close VER;
480                 print "$version_file has been updated.\n";
481         } else {
482                 print "$version_file unchanged.\n";
483         }
484 }
485
486 # Read values from the configuration file, if it exists.
487 sub get_config {
488         my $arg;
489         my $show_help = 0;
490
491         # Get our command-line args
492         # XXX - Do we need an option to undo --set-release?
493         GetOptions(
494                    "help|h", \$show_help,
495                    "get-svn|g", \$get_svn,
496                    "set-svn|s", \$set_svn,
497                    "set-version|v", \$set_version,
498                    "set-release|r|package-version|p", \$set_release
499                    ) || pod2usage(2);
500
501         if ($show_help) { pod2usage(1); }
502
503         if ( !( $show_help || $get_svn || $set_svn || $set_version || $set_release ) ) {
504                 $set_svn = 1;
505         }
506
507         if ($#ARGV >= 0) {
508                 $srcdir = $ARGV[0]
509         }
510
511         if (! open(FILE, "<$vconf_file")) {
512                 print STDERR "Version configuration file $vconf_file not "
513                 . "found.  Using defaults.\n";
514                 return 1;
515         }
516
517         while (<FILE>) {
518                 chomp;
519                 next if (/^#/);
520                 next unless (/^(\w+)(:|=)\s*(\S.*)/);
521                 $version_pref{$1} = $3;
522         }
523         close FILE;
524         return 1;
525 }
526
527 ##
528 ## Start of code
529 ##
530
531 &get_config();
532
533 &read_svn_info();
534
535 &print_svn_revision;
536
537 if ($set_version || $set_release) {
538         if ($set_version) {
539                 print "Generating version information\n";
540         }
541
542         if ($version_pref{"enable"} == 0) {
543                 print "Release information disabled in $vconf_file.\n";
544                 $set_release = 0;
545         }
546
547         if ($set_release) {
548                 print "Generating release information\n";
549         } else {
550                 print "Resetting release information\n";
551                 $revision = 0;
552                 $package_string = "";
553         }
554
555         &update_versioned_files;
556 }
557
558 __END__
559
560 =head1 NAM
561
562 make-version.pl - Get and set build-time version information for Wireshark
563
564 =head1 SYNOPSIS
565
566 make-version.pl [options] [source directory]
567
568   Options:
569
570     --help, -h                 This help message
571     --get-svn, -g              Print the SVN revision and source.
572     --set-svn, -s              Set the information in svnversion.h
573     --set-version, -v          Set the major, minor, and micro versions in
574                                configure.in, config.nmake, debian/changelog,
575                                and docbook/release_notes.xml.
576                                Resets the release information when used by
577                                itself.
578     --set-release, -r          Set the release information in configure.in
579                                and config.nmake
580     --package-version, -p      Deprecated. Same as --set-release.
581
582 Options can be used in any combination. If none are specified B<--set-svn>
583 is assumed.