From Ivan Lawrow: Added IEEE 802.15.4-2003 AES-CCM security modes
[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 # Update distributed files that contain any version information
374 sub update_versioned_files
375 {
376         &update_configure_in;
377         &update_config_nmake;
378         &update_release_notes;
379         &update_debian_changelog;
380 }
381
382 # Print the SVN version to $version_file.
383 # Don't change the file if it is not needed.
384 sub print_svn_revision
385 {
386         my $svn_revision;
387         my $needs_update = 1;
388
389         if ($last_change && $revision) {
390                 $svn_revision = "#define SVNVERSION \"SVN Rev " .
391                         $revision . "\"\n" .
392                         "#define SVNPATH \"" . $repo_path . "\"\n";
393         } else {
394                 $svn_revision = "#define SVNVERSION \"SVN Rev Unknown\"\n" .
395                         "#define SVNPATH \"unknown\"\n";
396         }
397         if (open(OLDREV, "<$version_file")) {
398                 my $old_svn_revision = <OLDREV> . <OLDREV>;
399                 if ($old_svn_revision eq $svn_revision) {
400                         $needs_update = 0;
401                 }
402                 close OLDREV;
403         }
404
405         if (! $set_svn) { return; }
406
407         if ($needs_update) {
408                 # print "Updating $version_file so it contains:\n$svn_revision";
409                 open(VER, ">$version_file") || die ("Cannot write to $version_file ($!)\n");
410                 print VER "$svn_revision";
411                 close VER;
412                 print "$version_file has been updated.\n";
413         } else {
414                 print "$version_file unchanged.\n";
415         }
416 }
417
418 # Read values from the configuration file, if it exists.
419 sub get_config {
420         my $arg;
421         my $show_help = 0;
422
423         # Get our command-line args
424         # XXX - Do we need an option to undo --set-release?
425         GetOptions(
426                    "help|h", \$show_help,
427                    "get-svn|g", \$get_svn,
428                    "set-svn|s", \$set_svn,
429                    "set-version|v", \$set_version,
430                    "set-release|r|package-version|p", \$set_release
431                    ) || pod2usage(2);
432
433         if ($show_help) { pod2usage(1); }
434
435         if ( !( $show_help || $get_svn || $set_svn || $set_version || $set_release ) ) {
436                 $set_svn = 1;
437         }
438
439         if ($#ARGV >= 0) {
440                 $srcdir = $ARGV[0]
441         }
442
443         if (! open(FILE, "<$vconf_file")) {
444                 print STDERR "Version configuration file $vconf_file not "
445                 . "found.  Using defaults.\n";
446                 return 1;
447         }
448
449         while (<FILE>) {
450                 chomp;
451                 next if (/^#/);
452                 next unless (/^(\w+)(:|=)\s*(\S.*)/);
453                 $version_pref{$1} = $3;
454         }
455         close FILE;
456         return 1;
457 }
458
459 ##
460 ## Start of code
461 ##
462
463 &get_config();
464
465 &read_svn_info();
466
467 &print_svn_revision;
468
469 if ($set_version || $set_release) {
470         if ($set_version) {
471                 print "Generating version information\n";
472         }
473
474         if ($version_pref{"enable"} == 0) {
475                 print "Release information disabled in $vconf_file.\n";
476                 $set_release = 0;
477         }
478
479         if ($set_release) {
480                 print "Generating release information\n";
481         } else {
482                 print "Resetting release information\n";
483                 $revision = 0;
484                 $package_string = "";
485         }
486
487         &update_versioned_files;
488 }
489
490 __END__
491
492 =head1 NAM
493
494 make-version.pl - Get and set build-time version information for Wireshark
495
496 =head1 SYNOPSIS
497
498 make-version.pl [options] [source directory]
499
500   Options:
501
502     --help, -h                 This help message
503     --get-svn, -g              Print the SVN revision and source.
504     --set-svn, -s              Set the information in svnversion.h
505     --set-version, -v          Set the major, minor, and micro versions in
506                                configure.in, config.nmake, debian/changelog,
507                                and docbook/release_notes.xml.
508                                Resets the release information when used by
509                                itself.
510     --set-release, -r          Set the release information in configure.in
511                                and config.nmake
512     --package-version, -p      Deprecated. Same as --set-release.
513
514 Options can be used in any combination. If none are specified B<--set-svn>
515 is assumed.