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