fae429613c7e558bb0b68c70417971460124cb90
[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) {
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
212
213         # 'svn info' failed or the user really wants us to dig around in .svn/entries
214         if ($do_hack) {
215                 # Start of ugly internal SVN file hack
216                 if (! open (ENTRIES, "< $srcdir/.svn/entries")) {
217                         print ("Unable to open $srcdir/.svn/entries\n");
218                 } else {
219                         $info_source = "Prodding .svn";
220                         # We need to find out whether our parser can handle the entries file
221                         $line = <ENTRIES>;
222                         chomp $line;
223                         if ($line eq '<?xml version="1.0" encoding="utf-8"?>') {
224                                 $repo_version = "pre1.4";
225                         } elsif ($line =~ /^8$/) {
226                                 $repo_version = "1.4";
227                         } else {
228                                 $repo_version = "unknown";
229                         }
230
231                         if ($repo_version eq "pre1.4") {
232                                 # The entries schema is flat, so we can use regexes to parse its contents.
233                                 while ($line = <ENTRIES>) {
234                                         if ($line =~ /<entry$/ || $line =~ /<entry\s/) {
235                                                 $in_entries = 1;
236                                                 $svn_name = "";
237                                         }
238                                         if ($in_entries) {
239                                                 if ($line =~ /name="(.*)"/) { $svn_name = $1; }
240                                                 if ($line =~ /committed-date="(\d{4})-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)/) {
241                                                         $last_change = timegm($6, $5, $4, $3, $2 - 1, $1);
242                                                 }
243                                                 if ($line =~ /revision="(\d+)"/) { $revision = $1; }
244                                         }
245                                         if ($line =~ /\/>/) {
246                                                 if (($svn_name eq "" || $svn_name eq "svn:this_dir") &&
247                                                                 $last_change && $revision) {
248                                                         $in_entries = 0;
249                                                         last;
250                                                 }
251                                         }
252                                         # XXX - Fetch the repository root & URL
253                                 }
254                         }
255                         close ENTRIES;
256                 }
257         }
258
259         # If we picked up the revision and modification time,
260         # generate our strings.
261         if ($revision && $last_change) {
262                 $version_format =~ s/%#/$revision/;
263                 $package_format =~ s/%#/$revision/;
264                 $package_string = strftime($package_format, gmtime($last_change));
265         }
266
267         if ($repo_url && $repo_root && index($repo_url, $repo_root) == 0) {
268                 $repo_path = substr($repo_url, length($repo_root));
269         }
270
271         if ($get_svn) {
272                 print <<"Fin";
273 SVN revision    : $revision
274 Revision source : $info_source
275 Release stamp   : $package_string
276 Fin
277         }
278 }
279
280
281 # Read configure.in, then write it back out with an updated
282 # "AC_INIT" line.
283 sub update_configure_ac
284 {
285         my $line;
286         my $contents = "";
287         my $version = "";
288         my $filepath = "$srcdir/configure.ac";
289
290         return if (!$set_version && $package_string eq "");
291
292         open(CFGIN, "< $filepath") || die "Can't read $filepath!";
293         while ($line = <CFGIN>) {
294                 if ($line =~ /^AC_INIT\(wireshark, (\d+)\.(\d+).(\d+) *,(.*[\r\n]+)$/) {
295                         $line = sprintf("AC_INIT\(wireshark, %d.%d.%d%s,$4",
296                                         $set_version ? $version_pref{"version_major"} : $1,
297                                         $set_version ? $version_pref{"version_minor"} : $2,
298                                         $set_version ? $version_pref{"version_micro"} : $3,
299                                         $package_string
300                                        );
301
302                 }
303                 $contents .= $line
304         }
305
306         open(CFGIN, "> $filepath") || die "Can't write $filepath!";
307         print(CFGIN $contents);
308         close(CFGIN);
309         print "$filepath has been updated.\n";
310 }
311
312 # Read config.nmake, then write it back out with an updated
313 # "VERSION" line.
314 sub update_config_nmake
315 {
316         my $line;
317         my $contents = "";
318         my $version = "";
319         my $filepath = "$srcdir/config.nmake";
320
321         open(CFGNMAKE, "< $filepath") || die "Can't read $filepath!";
322         while ($line = <CFGNMAKE>) {
323                 if ($line =~ /^SVN_REVISION=.*([\r\n]+)$/) {
324                         $line = sprintf("SVN_REVISION=%d$1", $revision);
325                 } elsif ($set_version && $line =~ /^VERSION_MAJOR=.*([\r\n]+)$/) {
326                         $line = sprintf("VERSION_MAJOR=%d$1", $version_pref{"version_major"});
327                 } elsif ($set_version && $line =~ /^VERSION_MINOR=.*([\r\n]+)$/) {
328                         $line = sprintf("VERSION_MINOR=%d$1", $version_pref{"version_minor"});
329                 } elsif ($set_version && $line =~ /^VERSION_MICRO=.*([\r\n]+)$/) {
330                         $line = sprintf("VERSION_MICRO=%d$1", $version_pref{"version_micro"});
331                 } elsif ($line =~ /^VERSION_EXTRA=.*([\r\n]+)$/) {
332                         $line = "VERSION_EXTRA=$package_string$1";
333                 }
334                 $contents .= $line
335         }
336
337         open(CFGNMAKE, "> $filepath") || die "Can't write $filepath!";
338         print(CFGNMAKE $contents);
339         close(CFGNMAKE);
340         print "$filepath has been updated.\n";
341 }
342
343 # Read docbook/release_notes.xml, then write it back out with an updated
344 # "WiresharkCurrentVersion" line.
345 sub update_release_notes
346 {
347         my $line;
348         my $contents = "";
349         my $version = "";
350         my $filepath = "$srcdir/docbook/release-notes.xml";
351
352         return if (!$set_version);
353
354         open(RELNOTES, "< $filepath") || die "Can't read $filepath!";
355         while ($line = <RELNOTES>) {
356                 #   <!ENTITY WiresharkCurrentVersion "1.7.1">
357
358                 if ($line =~ /<\!ENTITY\s+WiresharkCurrentVersion\s+.*([\r\n]+)$/) {
359                         $line = sprintf("<!ENTITY WiresharkCurrentVersion \"%d.%d.%d\">$1",
360                                         $version_pref{"version_major"},
361                                         $version_pref{"version_minor"},
362                                         $version_pref{"version_micro"},
363                                        );
364                 }
365                 $contents .= $line
366         }
367
368         open(RELNOTES, "> $filepath") || die "Can't write $filepath!";
369         print(RELNOTES $contents);
370         close(RELNOTES);
371         print "$filepath has been updated.\n";
372 }
373
374 # Read debian/changelog, then write back out an updated version.
375 sub update_debian_changelog
376 {
377         my $line;
378         my $contents = "";
379         my $version = "";
380         my $filepath = "$srcdir/debian/changelog";
381
382         return if ($set_version == 0);
383
384         open(CHANGELOG, "< $filepath") || die "Can't read $filepath!";
385         while ($line = <CHANGELOG>) {
386                 if ($set_version && CHANGELOG->input_line_number() == 1) {
387                         $line = sprintf("wireshark (%d.%d.%d) unstable; urgency=low\n",
388                                         $version_pref{"version_major"},
389                                         $version_pref{"version_minor"},
390                                         $version_pref{"version_micro"},
391                                        );
392                 }
393                 $contents .= $line
394         }
395
396         open(CHANGELOG, "> $filepath") || die "Can't write $filepath!";
397         print(CHANGELOG $contents);
398         close(CHANGELOG);
399         print "$filepath has been updated.\n";
400 }
401
402 # Read debian/wireshark-common.files, then write back out an updated version.
403 # The libraries updated here MUST match the updates made by update_lib_releases
404 # below. We should do this automatically.
405 sub update_debian_wcf
406 {
407         my $line;
408         my $contents = "";
409         my $version = "";
410         my $filepath = "$srcdir/debian/wireshark-common.files";
411
412         return if (!$set_version);
413
414         open(DWCF, "< $filepath") || die "Can't read $filepath!";
415         while ($line = <DWCF>) {
416                 # /usr/lib/wireshark/libwireshark.so.1.1.0
417
418                 if ($line =~ qr{^(/usr/lib/wireshark/lib(wireshark|wiretap).so\.\d+\.\d+\.)\d+$}) {
419                         $line = sprintf("$1%d\n", $version_pref{"version_micro"});
420                 }
421                 $contents .= $line
422         }
423
424         open(DWCF, "> $filepath") || die "Can't write $filepath!";
425         print(DWCF $contents);
426         close(DWCF);
427         print "$filepath has been updated.\n";
428 }
429
430 # Read Makefile.am for each library, then write back out an updated version.
431 sub update_lib_releases
432 {
433         my $line;
434         my $contents = "";
435         my $version = "";
436         my $filedir;
437         my $filepath;
438
439         return if (!$set_version);
440
441         # The Libtool manual says
442         #   "If the library source code has changed at all since the last
443         #    update, then increment revision (‘c:r:a’ becomes ‘c:r+1:a’)."
444         # epan changes with each minor release, almost by definition. wiretap
445         # changes with *most* releases.
446         #
447         # http://www.gnu.org/software/libtool/manual/libtool.html#Updating-version-info
448         for $filedir ("epan", "wiretap") {      # "wsutil"
449                 $contents = "";
450                 $filepath = $filedir . "/Makefile.am";
451                 open(MAKEFILE_AM, "< $filepath") || die "Can't read $filepath!";
452                 while ($line = <MAKEFILE_AM>) {
453                         # libwireshark_la_LDFLAGS = -version-info 2:1:1 -export-symbols
454
455                         if ($line =~ /^(lib\w+_la_LDFLAGS.*version-info\s+\d+:)\d+(:\d+.*)/) {
456                                 $line = sprintf("$1%d$2\n", $version_pref{"version_micro"});
457                         }
458                         $contents .= $line
459                 }
460
461                 open(MAKEFILE_AM, "> $filepath") || die "Can't write $filepath!";
462                 print(MAKEFILE_AM $contents);
463                 close(MAKEFILE_AM);
464                 print "$filepath has been updated.\n";
465         }
466 }
467
468 # Update distributed files that contain any version information
469 sub update_versioned_files
470 {
471         &update_configure_ac;
472         &update_config_nmake;
473         &update_release_notes;
474         &update_debian_changelog;
475         &update_debian_wcf;
476         &update_lib_releases;
477 }
478
479 # Print the SVN version to $version_file.
480 # Don't change the file if it is not needed.
481 sub print_svn_revision
482 {
483         my $svn_revision;
484         my $needs_update = 1;
485
486         if ($last_change && $revision) {
487                 $svn_revision = "#define SVNVERSION \"SVN Rev " .
488                         $revision . "\"\n" .
489                         "#define SVNPATH \"" . $repo_path . "\"\n";
490         } else {
491                 $svn_revision = "#define SVNVERSION \"SVN Rev Unknown\"\n" .
492                         "#define SVNPATH \"unknown\"\n";
493         }
494         if (open(OLDREV, "<$version_file")) {
495                 my $old_svn_revision = <OLDREV> . <OLDREV>;
496                 if ($old_svn_revision eq $svn_revision) {
497                         $needs_update = 0;
498                 }
499                 close OLDREV;
500         }
501
502         if (! $set_svn) { return; }
503
504         if ($needs_update) {
505                 # print "Updating $version_file so it contains:\n$svn_revision";
506                 open(VER, ">$version_file") || die ("Cannot write to $version_file ($!)\n");
507                 print VER "$svn_revision";
508                 close VER;
509                 print "$version_file has been updated.\n";
510         } else {
511                 print "$version_file unchanged.\n";
512         }
513 }
514
515 # Read values from the configuration file, if it exists.
516 sub get_config {
517         my $arg;
518         my $show_help = 0;
519
520         # Get our command-line args
521         # XXX - Do we need an option to undo --set-release?
522         GetOptions(
523                    "help|h", \$show_help,
524                    "get-svn|g", \$get_svn,
525                    "set-svn|s", \$set_svn,
526                    "set-version|v", \$set_version,
527                    "set-release|r|package-version|p", \$set_release
528                    ) || pod2usage(2);
529
530         if ($show_help) { pod2usage(1); }
531
532         if ( !( $show_help || $get_svn || $set_svn || $set_version || $set_release ) ) {
533                 $set_svn = 1;
534         }
535
536         if ($#ARGV >= 0) {
537                 $srcdir = $ARGV[0]
538         }
539
540         if (! open(FILE, "<$vconf_file")) {
541                 print STDERR "Version configuration file $vconf_file not "
542                 . "found.  Using defaults.\n";
543                 return 1;
544         }
545
546         while (<FILE>) {
547                 chomp;
548                 next if (/^#/);
549                 next unless (/^(\w+)(:|=)\s*(\S.*)/);
550                 $version_pref{$1} = $3;
551         }
552         close FILE;
553         return 1;
554 }
555
556 ##
557 ## Start of code
558 ##
559
560 &get_config();
561
562 &read_svn_info();
563
564 &print_svn_revision;
565
566 if ($set_version || $set_release) {
567         if ($set_version) {
568                 print "Generating version information\n";
569         }
570
571         if ($version_pref{"enable"} == 0) {
572                 print "Release information disabled in $vconf_file.\n";
573                 $set_release = 0;
574         }
575
576         if ($set_release) {
577                 print "Generating release information\n";
578         } else {
579                 print "Resetting release information\n";
580                 $revision = 0;
581                 $package_string = "";
582         }
583
584         &update_versioned_files;
585 }
586
587 __END__
588
589 =head1 NAM
590
591 make-version.pl - Get and set build-time version information for Wireshark
592
593 =head1 SYNOPSIS
594
595 make-version.pl [options] [source directory]
596
597   Options:
598
599     --help, -h                 This help message
600     --get-svn, -g              Print the SVN revision and source.
601     --set-svn, -s              Set the information in svnversion.h
602     --set-version, -v          Set the major, minor, and micro versions in
603                                configure.ac, config.nmake, debian/changelog,
604                                and docbook/release_notes.xml.
605                                Resets the release information when used by
606                                itself.
607     --set-release, -r          Set the release information in configure.ac
608                                and config.nmake
609     --package-version, -p      Deprecated. Same as --set-release.
610
611 Options can be used in any combination. If none are specified B<--set-svn>
612 is assumed.