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