f12f8bc590a0bd35c35a99eb7e20808b067e4432
[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 CMakeLists.txt 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" => 3,
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 my $devnull = File::Spec->devnull();
109
110 # Ensure we run with correct locale
111 $ENV{LANG} = "C";
112 $ENV{LC_ALL} = "C";
113 $ENV{GIT_PAGER} = "";
114
115 sub print_diag {
116         print STDERR @_ if $verbose;
117 }
118
119 # Attempt to get revision information from the repository.
120 sub read_repo_info {
121         my $line;
122         my $version_format = $version_pref{"format"};
123         my $package_format = "";
124         my $in_entries = 0;
125         my $svn_name;
126         my $repo_version;
127         my $do_hack = 1;
128         my $info_source = "Unknown";
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 (-e "$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 = "cd $srcdir; svn info";
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 and -e "$srcdir/.git") {
259
260                 # Try git...
261                 eval {
262                         use warnings "all";
263                         no warnings "all";
264                         # If someone had properly tagged 1.9.0 we could also use
265                         # "git describe --abbrev=1 --tags HEAD"
266
267                         $info_cmd = "(cd $srcdir; $git_executable log --format='%b' -n 1)";
268                         $line = qx{$info_cmd};
269                         if (defined($line)) {
270                                 if ($line =~ /svn path=.*; revision=(\d+)/) {
271                                         $num_commits = $1;
272                                 }
273                         }
274                         $info_cmd = "(cd $srcdir; $git_executable log --format='%ad' -n 1 --date=iso)";
275                         $line = qx{$info_cmd};
276                         if (defined($line)) {
277                                 if ($line =~ /(\d{4})-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d)/) {
278                                         $last_change = timegm($6, $5, $4, $3, $2 - 1, $1);
279                                 }
280                         }
281                         $info_cmd = "(cd $srcdir; $git_executable branch)";
282                         $line = qx{$info_cmd};
283                         if (defined($line)) {
284                                 if ($line =~ /\* (\S+)/) {
285                                         $repo_branch = $1;
286                                 }
287                         }
288                         1;
289                         };
290         }
291         if ($num_commits == 0 and -d "$srcdir/.bzr") {
292
293                 # Try bzr...
294                 eval {
295                         use warnings "all";
296                         no warnings "all";
297                         $info_cmd = "(cd $srcdir; bzr log -l 1)";
298                         $line = qx{$info_cmd};
299                         if (defined($line)) {
300                                 if ($line =~ /timestamp: \S+ (\d{4})-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d)/) {
301                                         $last_change = timegm($6, $5, $4, $3, $2 - 1, $1);
302                                 }
303                                 if ($line =~ /svn revno: (\d+) \(on (\S+)\)/) {
304                                         $num_commits = $1;
305                                         $repo_branch = $2;
306                                 }
307                                 $vcs_name = "Bzr";
308                         }
309                         1;
310                         };
311         }
312
313
314         # 'svn info' failed or the user really wants us to dig around in .svn/entries
315         if ($do_hack) {
316                 # Start of ugly internal SVN file hack
317                 if (! open (ENTRIES, "< $srcdir/.svn/entries")) {
318                         print STDERR "Unable to open $srcdir/.svn/entries\n";
319                 } else {
320                         $info_source = "Prodding .svn";
321                         # We need to find out whether our parser can handle the entries file
322                         $line = <ENTRIES>;
323                         chomp $line;
324                         if ($line eq '<?xml version="1.0" encoding="utf-8"?>') {
325                                 $repo_version = "pre1.4";
326                         } elsif ($line =~ /^8$/) {
327                                 $repo_version = "1.4";
328                         } else {
329                                 $repo_version = "unknown";
330                         }
331
332                         if ($repo_version eq "pre1.4") {
333                                 # The entries schema is flat, so we can use regexes to parse its contents.
334                                 while ($line = <ENTRIES>) {
335                                         if ($line =~ /<entry$/ || $line =~ /<entry\s/) {
336                                                 $in_entries = 1;
337                                                 $svn_name = "";
338                                         }
339                                         if ($in_entries) {
340                                                 if ($line =~ /name="(.*)"/) { $svn_name = $1; }
341                                                 if ($line =~ /committed-date="(\d{4})-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)/) {
342                                                         $last_change = timegm($6, $5, $4, $3, $2 - 1, $1);
343                                                 }
344                                                 if ($line =~ /revision="(\d+)"/) { $num_commits = $1; }
345                                         }
346                                         if ($line =~ /\/>/) {
347                                                 if (($svn_name eq "" || $svn_name eq "svn:this_dir") &&
348                                                                 $last_change && $num_commits) {
349                                                         $in_entries = 0;
350                                                         last;
351                                                 }
352                                         }
353                                         # XXX - Fetch the repository root & URL
354                                 }
355                         }
356                         close ENTRIES;
357                 }
358         }
359
360         # If we picked up the revision and modification time,
361         # generate our strings.
362         if ($version_pref{"pkg_enable"}) {
363                 $version_format =~ s/%#/$num_commits/;
364                 $package_format =~ s/%#/$num_commits-$commit_id/;
365                 $package_string = strftime($package_format, gmtime($last_change));
366         }
367
368         if ($get_vcs) {
369                 print <<"Fin";
370 Commit distance : $num_commits
371 Commit ID       : $commit_id
372 Revision source : $info_source
373 Release stamp   : $package_string
374 Fin
375         } elsif ($print_vcs) {
376                 print new_version_h();
377         }
378 }
379
380
381 # Read CMakeLists.txt, then write it back out with updated "set(PROJECT_..._VERSION ...)
382 # lines
383 # set(GIT_REVISION 999)
384 # set(PROJECT_MAJOR_VERSION 1)
385 # set(PROJECT_MINOR_VERSION 99)
386 # set(PROJECT_PATCH_VERSION 0)
387 # set(PROJECT_VERSION_EXTENSION "-rc5")
388 sub update_cmakelists_txt
389 {
390         my $line;
391         my $contents = "";
392         my $version = "";
393         my $filepath = "$srcdir/CMakeLists.txt";
394
395         return if (!$set_version && $package_string eq "");
396
397         open(CFGIN, "< $filepath") || die "Can't read $filepath!";
398         while ($line = <CFGIN>) {
399                 if ($line =~ /^set *\( *GIT_REVISION .*([\r\n]+)$/) {
400                         $line = sprintf("set(GIT_REVISION %d)$1", $num_commits);
401                 } elsif ($line =~ /^set *\( *PROJECT_MAJOR_VERSION .*([\r\n]+)$/) {
402                         $line = sprintf("set(PROJECT_MAJOR_VERSION %d)$1", $version_pref{"version_major"});
403                 } elsif ($line =~ /^set *\( *PROJECT_MINOR_VERSION .*([\r\n]+)$/) {
404                         $line = sprintf("set(PROJECT_MINOR_VERSION %d)$1", $version_pref{"version_minor"});
405                 } elsif ($line =~ /^set *\( *PROJECT_PATCH_VERSION .*([\r\n]+)$/) {
406                         $line = sprintf("set(PROJECT_PATCH_VERSION %d)$1", $version_pref{"version_micro"});
407                 } elsif ($line =~ /^set *\( *PROJECT_VERSION_EXTENSION .*([\r\n]+)$/) {
408                         $line = sprintf("set(PROJECT_VERSION_EXTENSION \"%s\")$1", $package_string);
409                 }
410                 $contents .= $line
411         }
412
413         open(CFGIN, "> $filepath") || die "Can't write $filepath!";
414         print(CFGIN $contents);
415         close(CFGIN);
416         print "$filepath has been updated.\n";
417 }
418
419 # Read configure.ac, then write it back out with an updated
420 # "AC_INIT" line.
421 sub update_configure_ac
422 {
423         my $line;
424         my $contents = "";
425         my $version = "";
426         my $filepath = "$srcdir/configure.ac";
427
428         return if (!$set_version && $package_string eq "");
429
430         open(CFGIN, "< $filepath") || die "Can't read $filepath!";
431         while ($line = <CFGIN>) {
432                 if ($line =~ /^m4_define\( *\[?version_major\]? *,.*([\r\n]+)$/) {
433                         $line = sprintf("m4_define([version_major], [%d])$1", $version_pref{"version_major"});
434                 } elsif ($line =~ /^m4_define\( *\[?version_minor\]? *,.*([\r\n]+)$/) {
435                         $line = sprintf("m4_define([version_minor], [%d])$1", $version_pref{"version_minor"});
436                 } elsif ($line =~ /^m4_define\( *\[?version_micro\]? *,.*([\r\n]+)$/) {
437                         $line = sprintf("m4_define([version_micro], [%d])$1", $version_pref{"version_micro"});
438                 } elsif ($line =~ /^m4_define\( *\[?version_extra\]? *,.*([\r\n]+)$/) {
439                         $line = sprintf("m4_define([version_extra], [%s])$1", $package_string);
440                 }
441                 $contents .= $line
442         }
443
444         open(CFGIN, "> $filepath") || die "Can't write $filepath!";
445         print(CFGIN $contents);
446         close(CFGIN);
447         print "$filepath has been updated.\n";
448 }
449
450 # Read docbook/asciidoc.conf, then write it back out with an updated
451 # wireshark-version replacement line.
452 sub update_release_notes
453 {
454         my $line;
455         my $contents = "";
456         my $version = "";
457         my $filepath = "$srcdir/docbook/asciidoc.conf";
458
459         open(ADOC_CONF, "< $filepath") || die "Can't read $filepath!";
460         while ($line = <ADOC_CONF>) {
461                 # wireshark-version:\[\]=1.9.1
462
463                 if ($line =~ /^wireshark-version=.*([\r\n]+)$/) {
464                         $line = sprintf("wireshark-version=%d.%d.%d$1",
465                                         $version_pref{"version_major"},
466                                         $version_pref{"version_minor"},
467                                         $version_pref{"version_micro"},
468                                        );
469                 }
470                 $contents .= $line
471         }
472
473         open(ADOC_CONF, "> $filepath") || die "Can't write $filepath!";
474         print(ADOC_CONF $contents);
475         close(ADOC_CONF);
476         print "$filepath has been updated.\n";
477 }
478
479 # Read debian/changelog, then write back out an updated version.
480 sub update_debian_changelog
481 {
482         my $line;
483         my $contents = "";
484         my $version = "";
485         my $filepath = "$srcdir/debian/changelog";
486
487         open(CHANGELOG, "< $filepath") || die "Can't read $filepath!";
488         while ($line = <CHANGELOG>) {
489                 if ($set_version && CHANGELOG->input_line_number() == 1) {
490                         $line = sprintf("wireshark (%d.%d.%d) unstable; urgency=low\n",
491                                         $version_pref{"version_major"},
492                                         $version_pref{"version_minor"},
493                                         $version_pref{"version_micro"},
494                                        );
495                 }
496                 $contents .= $line
497         }
498
499         open(CHANGELOG, "> $filepath") || die "Can't write $filepath!";
500         print(CHANGELOG $contents);
501         close(CHANGELOG);
502         print "$filepath has been updated.\n";
503 }
504
505 # Read Makefile.am for each library, then write back out an updated version.
506 sub update_automake_lib_releases
507 {
508         my $line;
509         my $contents = "";
510         my $version = "";
511         my $filedir;
512         my $filepath;
513
514         # The Libtool manual says
515         #   "If the library source code has changed at all since the last
516         #    update, then increment revision (‘c:r:a’ becomes ‘c:r+1:a’)."
517         # epan changes with each minor release, almost by definition. wiretap
518         # changes with *most* releases.
519         #
520         # http://www.gnu.org/software/libtool/manual/libtool.html#Updating-version-info
521         for $filedir ("epan", "wiretap") {      # "wsutil"
522                 $contents = "";
523                 $filepath = $filedir . "/Makefile.am";
524                 open(MAKEFILE_AM, "< $filepath") || die "Can't read $filepath!";
525                 while ($line = <MAKEFILE_AM>) {
526                         # libwireshark_la_LDFLAGS = -version-info 2:1:1 -export-symbols
527
528                         if ($line =~ /^(lib\w+_la_LDFLAGS.*version-info\s+\d+:)\d+(:\d+.*)/) {
529                                 $line = sprintf("$1%d$2\n", $version_pref{"version_micro"});
530                         }
531                         $contents .= $line
532                 }
533
534                 open(MAKEFILE_AM, "> $filepath") || die "Can't write $filepath!";
535                 print(MAKEFILE_AM $contents);
536                 close(MAKEFILE_AM);
537                 print "$filepath has been updated.\n";
538         }
539 }
540
541 # Read CMakeLists.txt for each library, then write back out an updated version.
542 sub update_cmake_lib_releases
543 {
544         my $line;
545         my $contents = "";
546         my $version = "";
547         my $filedir;
548         my $filepath;
549
550         for $filedir ("epan", "wiretap") {      # "wsutil"
551                 $contents = "";
552                 $filepath = $filedir . "/CMakeLists.txt";
553                 open(CMAKELISTS_TXT, "< $filepath") || die "Can't read $filepath!";
554                 while ($line = <CMAKELISTS_TXT>) {
555                         # set(FULL_SO_VERSION "0.0.0")
556
557                         if ($line =~ /^(set\s*\(\s*FULL_SO_VERSION\s+"\d+\.\d+\.)\d+(".*)/) {
558                                 $line = sprintf("$1%d$2\n", $version_pref{"version_micro"});
559                         }
560                         $contents .= $line
561                 }
562
563                 open(CMAKELISTS_TXT, "> $filepath") || die "Can't write $filepath!";
564                 print(CMAKELISTS_TXT $contents);
565                 close(CMAKELISTS_TXT);
566                 print "$filepath has been updated.\n";
567         }
568 }
569
570 # Update distributed files that contain any version information
571 sub update_versioned_files
572 {
573         # Matches CMakeLists.txt
574         printf "GR: %d, MaV: %d, MiV: %d, PL: %d, EV: %s\n",
575                 $num_commits, $version_pref{"version_major"},
576                 $version_pref{"version_minor"}, $version_pref{"version_micro"},
577                 $package_string;
578         &update_cmakelists_txt;
579         &update_configure_ac;
580         if ($set_version) {
581                 &update_release_notes;
582                 &update_debian_changelog;
583                 &update_automake_lib_releases;
584                 &update_cmake_lib_releases;
585         }
586 }
587
588 sub new_version_h
589 {
590         my $VCS_REVISION;
591         my $set_header = shift @_;
592
593         if (!$set_header) {
594                 $VCS_REVISION = "/* #undef VCSVERSION */\n";
595                 $VCS_REVISION .= "/* #undef VCSBRANCH */\n";
596                 return $VCS_REVISION;
597         }
598
599         if ($git_description) {
600                 $VCS_REVISION = "#define VCSVERSION \"" .
601                         $git_description . "\"\n" .
602                         "#define VCSBRANCH \"" . $repo_branch . "\"\n";
603         } elsif ($last_change && $num_commits) {
604                 $VCS_REVISION = "#define VCSVERSION \"" . $vcs_name . " Rev " .
605                         $num_commits . "\"\n" .
606                         "#define VCSBRANCH \"" . $repo_branch . "\"\n";
607         } else {
608                 $VCS_REVISION = "#define VCSVERSION \"" . $vcs_name .
609                         " Rev Unknown\"\n" .
610                         "#define VCSBRANCH \"unknown\"\n";
611         }
612
613         return $VCS_REVISION;
614 }
615
616 # Print the version control system's version to $version_file.
617 # Don't change the file if it is not needed.
618 sub print_VCS_REVISION
619 {
620         my $VCS_REVISION;
621         my $needs_update = 1;
622         my $set_header = 1;
623         my $git_cdir;
624
625         chomp($git_cdir = qx{git --git-dir="$srcdir/.git" rev-parse --git-common-dir 2> $devnull});
626         if ($git_cdir && -f "$git_cdir/wireshark-disable-versioning") {
627                 print_diag "Header versioning disabled using git override.\n";
628                 $set_header = 0;
629         }
630
631         $VCS_REVISION = new_version_h($set_header);
632         if (open(OLDREV, "<$version_file")) {
633                 my $old_VCS_REVISION = <OLDREV> . <OLDREV>;
634                 if ($old_VCS_REVISION eq $VCS_REVISION) {
635                         $needs_update = 0;
636                 }
637                 close OLDREV;
638         }
639
640         if (! $set_vcs) { return; }
641
642         if ($needs_update) {
643                 # print "Updating $version_file so it contains:\n$VCS_REVISION";
644                 open(VER, ">$version_file") || die ("Cannot write to $version_file ($!)\n");
645                 print VER "$VCS_REVISION";
646                 close VER;
647                 print "$version_file has been updated.\n";
648         } elsif (!$set_header) {
649                 print "$version_file disabled.\n";
650         } else {
651                 print "$version_file unchanged.\n";
652         }
653 }
654
655 # Read values from the configuration file, if it exists.
656 sub get_config {
657         my $arg;
658         my $show_help = 0;
659
660         # Get our command-line args
661         # XXX - Do we need an option to undo --set-release?
662         GetOptions(
663                    "help|h", \$show_help,
664                    "get-vcs|get-svn|g", \$get_vcs,
665                    "set-vcs|set-svn|s", \$set_vcs,
666                    "git-bin", \$git_executable,
667                    "print-vcs", \$print_vcs,
668                    "set-version|v", \$set_version,
669                    "set-release|r|package-version|p", \$set_release,
670                    "verbose", \$verbose
671                    ) || pod2usage(2);
672
673         if ($show_help) { pod2usage(1); }
674
675         if ( !( $show_help || $get_vcs || $set_vcs || $print_vcs || $set_version || $set_release ) ) {
676                 $set_vcs = 1;
677         }
678
679         if ($#ARGV >= 0) {
680                 $srcdir = $ARGV[0]
681         }
682
683         if (! open(FILE, "<$vconf_file")) {
684                 print_diag "Version configuration file $vconf_file not "
685                 . "found. Using defaults.\n";
686                 return 1;
687         }
688
689         while (<FILE>) {
690                 s/^\s+|\s+$//g; # chomp() may not handle CR
691                 next if (/^#/);
692                 next unless (/^(\w+)(:|=)\s*(\S.*)/);
693                 $version_pref{$1} = $3;
694         }
695         close FILE;
696         return 1;
697 }
698
699 ##
700 ## Start of code
701 ##
702
703 &get_config();
704
705 &read_repo_info();
706
707 &print_VCS_REVISION;
708
709 if ($set_version || $set_release) {
710         if ($set_version) {
711                 print "Generating version information.\n";
712         }
713
714         if ($version_pref{"enable"} == 0) {
715                 print "Release information disabled in $vconf_file.\n";
716                 $set_release = 0;
717         }
718
719         if ($set_release) {
720                 print "Generating release information.\n";
721         } else {
722                 print "Resetting release information\n";
723                 $num_commits = 0;
724                 $package_string = "";
725         }
726
727         &update_versioned_files;
728 }
729
730 __END__
731
732 =head1 NAM
733
734 make-version.pl - Get and set build-time version information for Wireshark
735
736 =head1 SYNOPSIS
737
738 make-version.pl [options] [source directory]
739
740   Options:
741
742     --help, -h                 This help message
743     --get-vcs, -g              Print the VCS revision and source.
744     --set-vcs, -s              Set the information in version.h
745     --print-vcs                Print the vcs version to standard output
746     --set-version, -v          Set the major, minor, and micro versions in
747                                the top-level CMakeLists.txt, configure.ac,
748                                docbook/asciidoc.conf, debian/changelog,
749                                the Makefile.am for all libraries, and the
750                                CMakeLists.txt for all libraries.
751                                Resets the release information when used by
752                                itself.
753     --set-release, -r          Set the release information in the top-level
754                                CMakeLists.txt, configure.ac
755     --package-version, -p      Deprecated. Same as --set-release.
756     --verbose                  Print diagnostic messages to STDERR.
757
758 Options can be used in any combination. If none are specified B<--set-vcs>
759 is assumed.
760
761 =cut
762
763 #
764 # Editor modelines  -  http://www.wireshark.org/tools/modelines.html
765 #
766 # Local variables:
767 # c-basic-offset: 8
768 # tab-width: 8
769 # indent-tabs-mode: t
770 # End:
771 #
772 # vi: set shiftwidth=8 tabstop=8 noexpandtab:
773 # :indentSize=8:tabSize=8:noTabs=false:
774 #
775 #