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