a3643ddef8e6f6d377e8a6f1a803b3fdf87431af
[metze/wireshark/wip.git] / make-version.pl
1 #!/usr/bin/perl -w
2 #
3 # Copyright 2004 Jörg Mayer (see AUTHORS file)
4 #
5 # Wireshark - Network traffic analyzer
6 # By Gerald Combs <gerald@wireshark.org>
7 # Copyright 1998 Gerald Combs
8 #
9 # This program is free software; you can redistribute it and/or
10 # modify it under the terms of the GNU General Public License
11 # as published by the Free Software Foundation; either version 2
12 # of the License, or (at your option) any later version.
13 #
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with this program; if not, write to the Free Software
21 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
22
23 # See below for usage
24 #
25 # If "version.conf" is present, it is parsed for configuration values.
26 # Possible values are:
27 #
28 #   enable       - Enable or disable versioning.  Zero (0) disables, nonzero
29 #                  enables.
30 #   svn_client   - Use svn client i.s.o. ugly internal SVN file hack
31 #   tortoise_svn - Use TortoiseSVN client instead of ugly internal SVN
32 #                  file hack
33 #   format       - A strftime() formatted string to use as a template for
34 #                  the version string. The sequence "%#" will substitute
35 #                  the number of commits for Git or the revision number
36 #                  for SVN.
37 #   pkg_enable   - Enable or disable local package versioning.
38 #   pkg_format   - Like "format", but used for the local package version.
39 #
40 # If run with the "-r" or "--set-release" argument the AC_INIT macro in
41 # configure.ac and the VERSION macro in config.nmake will have the
42 # pkg_format template appended to the version number. version.h will
43 # _not_ be generated if either argument is present.
44 #
45 # Default configuration:
46 #
47 # enable: 1
48 # git_client: 0
49 # svn_client: 0
50 # tortoise_svn: 0
51 # format: git %Y%m%d%H%M%S
52 # pkg_enable: 1
53 # pkg_format: -%#
54
55 # XXX - We're pretty dumb about the "%#" substitution, and about having
56 # spaces in the package format.
57
58 use strict;
59
60 use Time::Local;
61 use File::Basename;
62 use POSIX qw(strftime);
63 use Getopt::Long;
64 use Pod::Usage;
65 use IO::Handle;
66 use English;
67
68 my $version_file = 'version.h';
69 my $package_string = "";
70 my $vconf_file = 'version.conf';
71 my $vcs_name = "Git";
72 my $tortoise_file = "tortoise_template";
73 my $last_change = 0;
74 my $num_commits = 0;
75 my $commit_id = '';
76 my $repo_branch = "unknown";
77 my $git_executable = "git";
78 my $git_description = undef;
79 my $get_vcs = 0;
80 my $set_vcs = 0;
81 my $print_vcs = 0;
82 my $set_version = 0;
83 my $set_release = 0;
84 my %version_pref = (
85         "version_major" => 2,
86         "version_minor" => 1,
87         "version_micro" => 0,
88         "version_build" => 0,
89
90         "enable"        => 1,
91         "git_client"    => 0,   # set if .git found and .git/svn not found
92         "svn_client"    => 0,   # set if .svn found
93         "tortoise_svn"  => 0,
94         "format"        => "git %Y%m%d%H%M%S",
95
96         # Normal development builds
97         "pkg_enable" => 1,
98         "pkg_format" => "-%#",
99
100         # Development releases
101         #"pkg_enable" => 0,
102         #"pkg_format" => "",
103         );
104 my $srcdir = ".";
105 my $info_cmd = "";
106
107 # Ensure we run with correct locale
108 $ENV{LANG} = "C";
109 $ENV{LC_ALL} = "C";
110 $ENV{GIT_PAGER} = "";
111
112 # Attempt to get revision information from the repository.
113 sub read_repo_info {
114         my $line;
115         my $version_format = $version_pref{"format"};
116         my $package_format = "";
117         my $in_entries = 0;
118         my $svn_name;
119         my $repo_version;
120         my $do_hack = 1;
121         my $info_source = "Unknown";
122
123         # Make sure git is available.
124         if (!`$git_executable --version`) {
125                 print STDERR "Git unavailable. Git revision will be missing from version string.\n";
126                 return;
127         }
128
129         if ($version_pref{"pkg_enable"} > 0) {
130                 $package_format = $version_pref{"pkg_format"};
131         }
132
133         if (-d "$srcdir/.git" && ! -d "$srcdir/.git/svn") {
134                 $info_source = "Command line (git)";
135                 $version_pref{"git_client"} = 1;
136         } elsif (-d "$srcdir/.svn" or -d "$srcdir/../.svn") {
137                 $info_source = "Command line (svn info)";
138                 $info_cmd = "svn info $srcdir";
139                 $version_pref{"svn_client"} = 1;
140         } elsif (-d "$srcdir/.git/svn") {
141                 $info_source = "Command line (git-svn)";
142                 $info_cmd = "(cd $srcdir; $git_executable svn info)";
143         }
144
145         #Git can give us:
146         #
147         # A big ugly hash: git rev-parse HEAD
148         # 1ddc83849075addb0cac69a6fe3782f4325337b9
149         #
150         # A small ugly hash: git rev-parse --short HEAD
151         # 1ddc838
152         #
153         # The upstream branch path: git rev-parse --abbrev-ref --symbolic-full-name @{upstream}
154         # origin/master
155         #
156         # A version description: git describe --tags --dirty
157         # wireshark-1.8.12-15-g1ddc838
158         #
159         # Number of commits in this branch: git rev-list --count HEAD
160         # 48879
161         #
162         # Number of commits since 1.8.0: git rev-list --count 5e212d72ce098a7fec4332cbe6c22fcda796a018..HEAD
163         # 320
164         #
165         # Refs: git ls-remote code.wireshark.org:wireshark
166         # ea19c7f952ce9fc53fe4c223f1d9d6797346258b (r48972, changed version to 1.11.0)
167
168         if ($version_pref{"git_client"}) {
169                 eval {
170                         use warnings "all";
171                         no warnings "all";
172
173                         chomp($line = qx{$git_executable --git-dir="$srcdir"/.git log -1 --pretty=format:%at});
174                         if ($? == 0 && length($line) > 1) {
175                                 $last_change = $line;
176                         }
177
178                         # Commits since last annotated tag.
179                         chomp($line = qx{$git_executable --git-dir="$srcdir"/.git describe --long --always --match "v*"});
180                         if ($? == 0 && length($line) > 1) {
181                                 my @parts = split(/-/, $line);
182                                 $git_description = $line;
183                                 $num_commits = $parts[-2];
184                                 $commit_id = $parts[-1];
185                         }
186
187                         # This will break in some cases. Hopefully not during
188                         # official package builds.
189                         chomp($line = qx{$git_executable --git-dir="$srcdir"/.git rev-parse --abbrev-ref --symbolic-full-name \@\{upstream\}});
190                         if ($? == 0 && length($line) > 1) {
191                                 $repo_branch = basename($line);
192                         }
193
194                         1;
195                 };
196
197                 if ($last_change && $num_commits && $repo_branch) {
198                         $do_hack = 0;
199                 }
200         } elsif ($version_pref{"svn_client"}) {
201                 my $repo_root = undef;
202                 my $repo_url = undef;
203                 eval {
204                         use warnings "all";
205                         no warnings "all";
206                         $line = qx{$info_cmd};
207                         if (defined($line)) {
208                                 if ($line =~ /Last Changed Date: (\d{4})-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d)/) {
209                                         $last_change = timegm($6, $5, $4, $3, $2 - 1, $1);
210                                 }
211                                 if ($line =~ /Last Changed Rev: (\d+)/) {
212                                         $num_commits = $1;
213                                 }
214                                 if ($line =~ /URL: (\S+)/) {
215                                         $repo_url = $1;
216                                 }
217                                 if ($line =~ /Repository Root: (\S+)/) {
218                                         $repo_root = $1;
219                                 }
220                                 $vcs_name = "SVN";
221                         }
222                         1;
223                 };
224
225                 if ($repo_url && $repo_root && index($repo_url, $repo_root) == 0) {
226                         $repo_branch = substr($repo_url, length($repo_root));
227                 }
228
229                 if ($last_change && $num_commits && $repo_url && $repo_root) {
230                         $do_hack = 0;
231                 }
232         } elsif ($version_pref{"tortoise_svn"}) {
233                 # Dynamically generic template file needed by TortoiseSVN
234                 open(TORTOISE, ">$tortoise_file");
235                 print TORTOISE "#define VCSVERSION \"\$WCREV\$\"\r\n";
236                 print TORTOISE "#define VCSBRANCH \"\$WCURL\$\"\r\n";
237                 close(TORTOISE);
238
239                 $info_source = "Command line (SubWCRev)";
240                 $info_cmd = "SubWCRev $srcdir $tortoise_file $version_file";
241                 my $tortoise = system($info_cmd);
242                 if ($tortoise == 0) {
243                         $do_hack = 0;
244                 }
245                 $vcs_name = "SVN";
246
247                 #clean up the template file
248                 unlink($tortoise_file);
249         }
250
251         if ($num_commits == 0) {
252                 # Fall back to config.nmake
253                 $info_source = "Prodding config.nmake";
254                 my $filepath = "$srcdir/config.nmake";
255                 open(CFGNMAKE, "< $filepath") || die "Can't read $filepath!";
256                 while ($line = <CFGNMAKE>) {
257                         if ($line =~ /^VCS_REVISION=(\d+)/) {
258                                 $num_commits = $1;
259                                 $do_hack = 0;
260                                 last;
261                         }
262                 }
263                 close (CFGNMAKE);
264         }
265         if ($num_commits == 0 and -d "$srcdir/.git") {
266
267                 # Try git...
268                 eval {
269                         use warnings "all";
270                         no warnings "all";
271                         # If someone had properly tagged 1.9.0 we could also use
272                         # "git describe --abbrev=1 --tags HEAD"
273
274                         $info_cmd = "(cd $srcdir; $git_executable log --format='%b' -n 1)";
275                         $line = qx{$info_cmd};
276                         if (defined($line)) {
277                                 if ($line =~ /svn path=.*; revision=(\d+)/) {
278                                         $num_commits = $1;
279                                 }
280                         }
281                         $info_cmd = "(cd $srcdir; $git_executable log --format='%ad' -n 1 --date=iso)";
282                         $line = qx{$info_cmd};
283                         if (defined($line)) {
284                                 if ($line =~ /(\d{4})-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d)/) {
285                                         $last_change = timegm($6, $5, $4, $3, $2 - 1, $1);
286                                 }
287                         }
288                         $info_cmd = "(cd $srcdir; $git_executable branch)";
289                         $line = qx{$info_cmd};
290                         if (defined($line)) {
291                                 if ($line =~ /\* (\S+)/) {
292                                         $repo_branch = $1;
293                                 }
294                         }
295                         1;
296                         };
297         }
298         if ($num_commits == 0 and -d "$srcdir/.bzr") {
299
300                 # Try bzr...
301                 eval {
302                         use warnings "all";
303                         no warnings "all";
304                         $info_cmd = "(cd $srcdir; bzr log -l 1)";
305                         $line = qx{$info_cmd};
306                         if (defined($line)) {
307                                 if ($line =~ /timestamp: \S+ (\d{4})-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d)/) {
308                                         $last_change = timegm($6, $5, $4, $3, $2 - 1, $1);
309                                 }
310                                 if ($line =~ /svn revno: (\d+) \(on (\S+)\)/) {
311                                         $num_commits = $1;
312                                         $repo_branch = $2;
313                                 }
314                                 $vcs_name = "Bzr";
315                         }
316                         1;
317                         };
318         }
319
320
321         # 'svn info' failed or the user really wants us to dig around in .svn/entries
322         if ($do_hack) {
323                 # Start of ugly internal SVN file hack
324                 if (! open (ENTRIES, "< $srcdir/.svn/entries")) {
325                         print STDERR "Unable to open $srcdir/.svn/entries\n";
326                 } else {
327                         $info_source = "Prodding .svn";
328                         # We need to find out whether our parser can handle the entries file
329                         $line = <ENTRIES>;
330                         chomp $line;
331                         if ($line eq '<?xml version="1.0" encoding="utf-8"?>') {
332                                 $repo_version = "pre1.4";
333                         } elsif ($line =~ /^8$/) {
334                                 $repo_version = "1.4";
335                         } else {
336                                 $repo_version = "unknown";
337                         }
338
339                         if ($repo_version eq "pre1.4") {
340                                 # The entries schema is flat, so we can use regexes to parse its contents.
341                                 while ($line = <ENTRIES>) {
342                                         if ($line =~ /<entry$/ || $line =~ /<entry\s/) {
343                                                 $in_entries = 1;
344                                                 $svn_name = "";
345                                         }
346                                         if ($in_entries) {
347                                                 if ($line =~ /name="(.*)"/) { $svn_name = $1; }
348                                                 if ($line =~ /committed-date="(\d{4})-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)/) {
349                                                         $last_change = timegm($6, $5, $4, $3, $2 - 1, $1);
350                                                 }
351                                                 if ($line =~ /revision="(\d+)"/) { $num_commits = $1; }
352                                         }
353                                         if ($line =~ /\/>/) {
354                                                 if (($svn_name eq "" || $svn_name eq "svn:this_dir") &&
355                                                                 $last_change && $num_commits) {
356                                                         $in_entries = 0;
357                                                         last;
358                                                 }
359                                         }
360                                         # XXX - Fetch the repository root & URL
361                                 }
362                         }
363                         close ENTRIES;
364                 }
365         }
366
367         # If we picked up the revision and modification time,
368         # generate our strings.
369         if ($version_pref{"pkg_enable"}) {
370                 $version_format =~ s/%#/$num_commits/;
371                 $package_format =~ s/%#/$num_commits-$commit_id/;
372                 $package_string = strftime($package_format, gmtime($last_change));
373         }
374
375         if ($get_vcs) {
376                 print <<"Fin";
377 Commit distance : $num_commits
378 Commit ID       : $commit_id
379 Revision source : $info_source
380 Release stamp   : $package_string
381 Fin
382         } elsif ($print_vcs) {
383                 print new_version_h();
384         }
385 }
386
387
388 # Read CMakeLists.txt, then write it back out with updated "set(PROJECT_..._VERSION ...)
389 # lines
390 # set(GIT_REVISION 999)
391 # set(PROJECT_MAJOR_VERSION 1)
392 # set(PROJECT_MINOR_VERSION 99)
393 # set(PROJECT_PATCH_VERSION 0)
394 # set(PROJECT_VERSION_EXTENSION "-rc5")
395 sub update_cmakelists_txt
396 {
397         my $line;
398         my $contents = "";
399         my $version = "";
400         my $filepath = "$srcdir/CMakeLists.txt";
401         my $cmake_package_string = "\$ENV{WIRESHARK_VERSION_EXTRA}";
402
403         if ($package_string ne "") { $cmake_package_string = $package_string; }
404
405         return if (!$set_version && $package_string eq "");
406
407         open(CFGIN, "< $filepath") || die "Can't read $filepath!";
408         while ($line = <CFGIN>) {
409                 if ($line =~ /^set *\( *GIT_REVISION .*([\r\n]+)$/) {
410                         $line = sprintf("set(GIT_REVISION %d)$1", $num_commits);
411                 } elsif ($line =~ /^set *\( *PROJECT_MAJOR_VERSION .*([\r\n]+)$/) {
412                         $line = sprintf("set(PROJECT_MAJOR_VERSION %d)$1", $version_pref{"version_major"});
413                 } elsif ($line =~ /^set *\( *PROJECT_MINOR_VERSION .*([\r\n]+)$/) {
414                         $line = sprintf("set(PROJECT_MINOR_VERSION %d)$1", $version_pref{"version_minor"});
415                 } elsif ($line =~ /^set *\( *PROJECT_PATCH_VERSION .*([\r\n]+)$/) {
416                         $line = sprintf("set(PROJECT_PATCH_VERSION %d)$1", $version_pref{"version_micro"});
417                 } elsif ($line =~ /^set *\( *PROJECT_VERSION_EXTENSION.*([\r\n]+)$/) {
418                         $line = sprintf("set(PROJECT_VERSION_EXTENSION \"%s\")$1", $cmake_package_string);
419                 }
420                 $contents .= $line
421         }
422
423         open(CFGIN, "> $filepath") || die "Can't write $filepath!";
424         print(CFGIN $contents);
425         close(CFGIN);
426         print "$filepath has been updated.\n";
427 }
428
429 # Read configure.ac, then write it back out with an updated
430 # "AC_INIT" line.
431 sub update_configure_ac
432 {
433         my $line;
434         my $contents = "";
435         my $version = "";
436         my $filepath = "$srcdir/configure.ac";
437
438         return if (!$set_version && $package_string eq "");
439
440         open(CFGIN, "< $filepath") || die "Can't read $filepath!";
441         while ($line = <CFGIN>) {
442                 if ($line =~ /^m4_define\( *\[?version_major\]? *,.*([\r\n]+)$/) {
443                         $line = sprintf("m4_define([version_major], [%d])$1", $version_pref{"version_major"});
444                 } elsif ($line =~ /^m4_define\( *\[?version_minor\]? *,.*([\r\n]+)$/) {
445                         $line = sprintf("m4_define([version_minor], [%d])$1", $version_pref{"version_minor"});
446                 } elsif ($line =~ /^m4_define\( *\[?version_micro\]? *,.*([\r\n]+)$/) {
447                         $line = sprintf("m4_define([version_micro], [%d])$1", $version_pref{"version_micro"});
448                 } elsif ($line =~ /^m4_append\( *\[?version_micro_extra\]? *,.*([\r\n]+)$/) {
449                         $line = sprintf("m4_append([version_micro_extra], [%s])$1", $package_string);
450                 }
451                 $contents .= $line
452         }
453
454         open(CFGIN, "> $filepath") || die "Can't write $filepath!";
455         print(CFGIN $contents);
456         close(CFGIN);
457         print "$filepath has been updated.\n";
458 }
459
460 # Read config.nmake, then write it back out with an updated
461 # "VERSION" line.
462 sub update_config_nmake
463 {
464         my $line;
465         my $contents = "";
466         my $version = "";
467         my $filepath = "$srcdir/config.nmake";
468         my $win_package_string = "\$(WIRESHARK_VERSION_EXTRA)";
469
470         if ($package_string ne "") { $win_package_string = $package_string; }
471
472
473         open(CFGNMAKE, "< $filepath") || die "Can't read $filepath!";
474         while ($line = <CFGNMAKE>) {
475                 if ($line =~ /^VCS_REVISION=.*([\r\n]+)$/) {
476                         $line = sprintf("VCS_REVISION=%d$1", $num_commits);
477                 } elsif ($set_version && $line =~ /^VERSION_MAJOR=.*([\r\n]+)$/) {
478                         $line = sprintf("VERSION_MAJOR=%d$1", $version_pref{"version_major"});
479                 } elsif ($set_version && $line =~ /^VERSION_MINOR=.*([\r\n]+)$/) {
480                         $line = sprintf("VERSION_MINOR=%d$1", $version_pref{"version_minor"});
481                 } elsif ($set_version && $line =~ /^VERSION_MICRO=.*([\r\n]+)$/) {
482                         $line = sprintf("VERSION_MICRO=%d$1", $version_pref{"version_micro"});
483                 } elsif ($line =~ /^VERSION_EXTRA=.*([\r\n]+)$/) {
484                         $line = "VERSION_EXTRA=$win_package_string$1";
485                 }
486                 $contents .= $line
487         }
488
489         open(CFGNMAKE, "> $filepath") || die "Can't write $filepath!";
490         print(CFGNMAKE $contents);
491         close(CFGNMAKE);
492         print "$filepath has been updated.\n";
493 }
494
495 # Read docbook/asciidoc.conf, then write it back out with an updated
496 # wireshark-version replacement line.
497 sub update_release_notes
498 {
499         my $line;
500         my $contents = "";
501         my $version = "";
502         my $filepath = "$srcdir/docbook/asciidoc.conf";
503
504         return if (!$set_version);
505
506         open(ADOC_CONF, "< $filepath") || die "Can't read $filepath!";
507         while ($line = <ADOC_CONF>) {
508                 # wireshark-version:\[\]=1.9.1
509
510                 if ($line =~ /^wireshark-version:\\\[\\\]=.*([\r\n]+)$/) {
511                         $line = sprintf("wireshark-version:\\\[\\\]=%d.%d.%d$1",
512                                         $version_pref{"version_major"},
513                                         $version_pref{"version_minor"},
514                                         $version_pref{"version_micro"},
515                                        );
516                 }
517                 $contents .= $line
518         }
519
520         open(ADOC_CONF, "> $filepath") || die "Can't write $filepath!";
521         print(ADOC_CONF $contents);
522         close(ADOC_CONF);
523         print "$filepath has been updated.\n";
524 }
525
526 # Read debian/changelog, then write back out an updated version.
527 sub update_debian_changelog
528 {
529         my $line;
530         my $contents = "";
531         my $version = "";
532         my $filepath = "$srcdir/debian/changelog";
533
534         return if ($set_version == 0);
535
536         open(CHANGELOG, "< $filepath") || die "Can't read $filepath!";
537         while ($line = <CHANGELOG>) {
538                 if ($set_version && CHANGELOG->input_line_number() == 1) {
539                         $line = sprintf("wireshark (%d.%d.%d) unstable; urgency=low\n",
540                                         $version_pref{"version_major"},
541                                         $version_pref{"version_minor"},
542                                         $version_pref{"version_micro"},
543                                        );
544                 }
545                 $contents .= $line
546         }
547
548         open(CHANGELOG, "> $filepath") || die "Can't write $filepath!";
549         print(CHANGELOG $contents);
550         close(CHANGELOG);
551         print "$filepath has been updated.\n";
552 }
553
554 # Read Makefile.am for each library, then write back out an updated version.
555 sub update_automake_lib_releases
556 {
557         my $line;
558         my $contents = "";
559         my $version = "";
560         my $filedir;
561         my $filepath;
562
563         return if (!$set_version);
564
565         # The Libtool manual says
566         #   "If the library source code has changed at all since the last
567         #    update, then increment revision (‘c:r:a’ becomes ‘c:r+1:a’)."
568         # epan changes with each minor release, almost by definition. wiretap
569         # changes with *most* releases.
570         #
571         # http://www.gnu.org/software/libtool/manual/libtool.html#Updating-version-info
572         for $filedir ("epan", "wiretap") {      # "wsutil"
573                 $contents = "";
574                 $filepath = $filedir . "/Makefile.am";
575                 open(MAKEFILE_AM, "< $filepath") || die "Can't read $filepath!";
576                 while ($line = <MAKEFILE_AM>) {
577                         # libwireshark_la_LDFLAGS = -version-info 2:1:1 -export-symbols
578
579                         if ($line =~ /^(lib\w+_la_LDFLAGS.*version-info\s+\d+:)\d+(:\d+.*)/) {
580                                 $line = sprintf("$1%d$2\n", $version_pref{"version_micro"});
581                         }
582                         $contents .= $line
583                 }
584
585                 open(MAKEFILE_AM, "> $filepath") || die "Can't write $filepath!";
586                 print(MAKEFILE_AM $contents);
587                 close(MAKEFILE_AM);
588                 print "$filepath has been updated.\n";
589         }
590 }
591
592 # Read CMakeLists.txt for each library, then write back out an updated version.
593 sub update_cmake_lib_releases
594 {
595         my $line;
596         my $contents = "";
597         my $version = "";
598         my $filedir;
599         my $filepath;
600
601         return if (!$set_version);
602
603         for $filedir ("epan", "wiretap") {      # "wsutil"
604                 $contents = "";
605                 $filepath = $filedir . "/CMakeLists.txt";
606                 open(CMAKELISTS_TXT, "< $filepath") || die "Can't read $filepath!";
607                 while ($line = <CMAKELISTS_TXT>) {
608                         # set(FULL_SO_VERSION "0.0.0")
609
610                         if ($line =~ /^(set\s*\(\s*FULL_SO_VERSION\s+"\d+\.\d+\.)\d+(".*)/) {
611                                 $line = sprintf("$1%d$2\n", $version_pref{"version_micro"});
612                         }
613                         $contents .= $line
614                 }
615
616                 open(CMAKELISTS_TXT, "> $filepath") || die "Can't write $filepath!";
617                 print(CMAKELISTS_TXT $contents);
618                 close(CMAKELISTS_TXT);
619                 print "$filepath has been updated.\n";
620         }
621 }
622
623 # Update distributed files that contain any version information
624 sub update_versioned_files
625 {
626         &update_cmakelists_txt;
627         &update_configure_ac;
628         &update_config_nmake;
629         &update_release_notes;
630         &update_debian_changelog;
631         &update_automake_lib_releases;
632         &update_cmake_lib_releases;
633 }
634
635 sub new_version_h
636 {
637         my $VCS_REVISION;
638
639         if ($git_description) {
640                 $VCS_REVISION = "#define VCSVERSION \"" .
641                         $git_description . "\"\n" .
642                         "#define VCSBRANCH \"" . $repo_branch . "\"\n";
643         } elsif ($last_change && $num_commits) {
644                 $VCS_REVISION = "#define VCSVERSION \"" . $vcs_name . " Rev " .
645                         $num_commits . "\"\n" .
646                         "#define VCSBRANCH \"" . $repo_branch . "\"\n";
647         } else {
648                 $VCS_REVISION = "#define VCSVERSION \"" . $vcs_name .
649                         " Rev Unknown\"\n" .
650                         "#define VCSBRANCH \"unknown\"\n";
651         }
652
653         return $VCS_REVISION;
654 }
655
656 # Print the version control system's version to $version_file.
657 # Don't change the file if it is not needed.
658 sub print_VCS_REVISION
659 {
660         my $VCS_REVISION = new_version_h();
661         my $needs_update = 1;
662         if (open(OLDREV, "<$version_file")) {
663                 my $old_VCS_REVISION = <OLDREV> . <OLDREV>;
664                 if ($old_VCS_REVISION eq $VCS_REVISION) {
665                         $needs_update = 0;
666                 }
667                 close OLDREV;
668         }
669
670         if (! $set_vcs) { return; }
671
672         if ($needs_update) {
673                 # print "Updating $version_file so it contains:\n$VCS_REVISION";
674                 open(VER, ">$version_file") || die ("Cannot write to $version_file ($!)\n");
675                 print VER "$VCS_REVISION";
676                 close VER;
677                 print "$version_file has been updated.\n";
678         } else {
679                 print "$version_file unchanged.\n";
680         }
681 }
682
683 # Read values from the configuration file, if it exists.
684 sub get_config {
685         my $arg;
686         my $show_help = 0;
687
688         # Get our command-line args
689         # XXX - Do we need an option to undo --set-release?
690         GetOptions(
691                    "help|h", \$show_help,
692                    "get-vcs|get-svn|g", \$get_vcs,
693                    "set-vcs|set-svn|s", \$set_vcs,
694                    "git-bin", \$git_executable,
695                    "print-vcs", \$print_vcs,
696                    "set-version|v", \$set_version,
697                    "set-release|r|package-version|p", \$set_release
698                    ) || pod2usage(2);
699
700         if ($show_help) { pod2usage(1); }
701
702         if ( !( $show_help || $get_vcs || $set_vcs || $print_vcs || $set_version || $set_release ) ) {
703                 $set_vcs = 1;
704         }
705
706         if ($#ARGV >= 0) {
707                 $srcdir = $ARGV[0]
708         }
709
710         if (! open(FILE, "<$vconf_file")) {
711                 print STDERR "Version configuration file $vconf_file not "
712                 . "found.  Using defaults.\n";
713                 return 1;
714         }
715
716         while (<FILE>) {
717                 s/^\s+|\s+$//g; # chomp() may not handle CR
718                 next if (/^#/);
719                 next unless (/^(\w+)(:|=)\s*(\S.*)/);
720                 $version_pref{$1} = $3;
721         }
722         close FILE;
723         return 1;
724 }
725
726 ##
727 ## Start of code
728 ##
729
730 &get_config();
731
732 &read_repo_info();
733
734 &print_VCS_REVISION;
735
736 if ($set_version || $set_release) {
737         if ($set_version) {
738                 print "Generating version information\n";
739         }
740
741         if ($version_pref{"enable"} == 0) {
742                 print "Release information disabled in $vconf_file.\n";
743                 $set_release = 0;
744         }
745
746         if ($set_release) {
747                 print "Generating release information\n";
748         } else {
749                 print "Resetting release information\n";
750                 $num_commits = 0;
751                 $package_string = "";
752         }
753
754         &update_versioned_files;
755 }
756
757 __END__
758
759 =head1 NAM
760
761 make-version.pl - Get and set build-time version information for Wireshark
762
763 =head1 SYNOPSIS
764
765 make-version.pl [options] [source directory]
766
767   Options:
768
769     --help, -h                 This help message
770     --get-vcs, -g              Print the VCS revision and source.
771     --set-vcs, -s              Set the information in version.h
772     --print-vcs                Print the vcs version to standard output
773     --set-version, -v          Set the major, minor, and micro versions in
774                                configure.ac, config.nmake, debian/changelog,
775                                and docbook/asciidoc.conf.
776                                Resets the release information when used by
777                                itself.
778     --set-release, -r          Set the release information in configure.ac
779                                and config.nmake
780     --package-version, -p      Deprecated. Same as --set-release.
781
782 Options can be used in any combination. If none are specified B<--set-svn>
783 is assumed.
784
785 =cut
786
787 #
788 # Editor modelines  -  http://www.wireshark.org/tools/modelines.html
789 #
790 # Local variables:
791 # c-basic-offset: 8
792 # tab-width: 8
793 # indent-tabs-mode: t
794 # End:
795 #
796 # vi: set shiftwidth=8 tabstop=8 noexpandtab:
797 # :indentSize=8:tabSize=8:noTabs=false:
798 #
799 #