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