Add option to disable version.h
[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 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 (-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         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         open(CHANGELOG, "< $filepath") || die "Can't read $filepath!";
540         while ($line = <CHANGELOG>) {
541                 if ($set_version && CHANGELOG->input_line_number() == 1) {
542                         $line = sprintf("wireshark (%d.%d.%d) unstable; urgency=low\n",
543                                         $version_pref{"version_major"},
544                                         $version_pref{"version_minor"},
545                                         $version_pref{"version_micro"},
546                                        );
547                 }
548                 $contents .= $line
549         }
550
551         open(CHANGELOG, "> $filepath") || die "Can't write $filepath!";
552         print(CHANGELOG $contents);
553         close(CHANGELOG);
554         print "$filepath has been updated.\n";
555 }
556
557 # Read Makefile.am for each library, then write back out an updated version.
558 sub update_automake_lib_releases
559 {
560         my $line;
561         my $contents = "";
562         my $version = "";
563         my $filedir;
564         my $filepath;
565
566         # The Libtool manual says
567         #   "If the library source code has changed at all since the last
568         #    update, then increment revision (‘c:r:a’ becomes ‘c:r+1:a’)."
569         # epan changes with each minor release, almost by definition. wiretap
570         # changes with *most* releases.
571         #
572         # http://www.gnu.org/software/libtool/manual/libtool.html#Updating-version-info
573         for $filedir ("epan", "wiretap") {      # "wsutil"
574                 $contents = "";
575                 $filepath = $filedir . "/Makefile.am";
576                 open(MAKEFILE_AM, "< $filepath") || die "Can't read $filepath!";
577                 while ($line = <MAKEFILE_AM>) {
578                         # libwireshark_la_LDFLAGS = -version-info 2:1:1 -export-symbols
579
580                         if ($line =~ /^(lib\w+_la_LDFLAGS.*version-info\s+\d+:)\d+(:\d+.*)/) {
581                                 $line = sprintf("$1%d$2\n", $version_pref{"version_micro"});
582                         }
583                         $contents .= $line
584                 }
585
586                 open(MAKEFILE_AM, "> $filepath") || die "Can't write $filepath!";
587                 print(MAKEFILE_AM $contents);
588                 close(MAKEFILE_AM);
589                 print "$filepath has been updated.\n";
590         }
591 }
592
593 # Read CMakeLists.txt for each library, then write back out an updated version.
594 sub update_cmake_lib_releases
595 {
596         my $line;
597         my $contents = "";
598         my $version = "";
599         my $filedir;
600         my $filepath;
601
602         for $filedir ("epan", "wiretap") {      # "wsutil"
603                 $contents = "";
604                 $filepath = $filedir . "/CMakeLists.txt";
605                 open(CMAKELISTS_TXT, "< $filepath") || die "Can't read $filepath!";
606                 while ($line = <CMAKELISTS_TXT>) {
607                         # set(FULL_SO_VERSION "0.0.0")
608
609                         if ($line =~ /^(set\s*\(\s*FULL_SO_VERSION\s+"\d+\.\d+\.)\d+(".*)/) {
610                                 $line = sprintf("$1%d$2\n", $version_pref{"version_micro"});
611                         }
612                         $contents .= $line
613                 }
614
615                 open(CMAKELISTS_TXT, "> $filepath") || die "Can't write $filepath!";
616                 print(CMAKELISTS_TXT $contents);
617                 close(CMAKELISTS_TXT);
618                 print "$filepath has been updated.\n";
619         }
620 }
621
622 # Update distributed files that contain any version information
623 sub update_versioned_files
624 {
625         &update_cmakelists_txt;
626         &update_configure_ac;
627         &update_config_nmake;
628         if ($set_version) {
629                 &update_release_notes;
630                 &update_debian_changelog;
631                 &update_automake_lib_releases;
632                 &update_cmake_lib_releases;
633         }
634 }
635
636 sub new_version_h
637 {
638         my $VCS_REVISION;
639         my $set_header = shift @_;
640
641         if (!$set_header) {
642                 $VCS_REVISION = "/* #undef VCSVERSION */\n";
643                 $VCS_REVISION .= "/* #undef VCSBRANCH */\n";
644                 return $VCS_REVISION;
645         }
646
647         if ($git_description) {
648                 $VCS_REVISION = "#define VCSVERSION \"" .
649                         $git_description . "\"\n" .
650                         "#define VCSBRANCH \"" . $repo_branch . "\"\n";
651         } elsif ($last_change && $num_commits) {
652                 $VCS_REVISION = "#define VCSVERSION \"" . $vcs_name . " Rev " .
653                         $num_commits . "\"\n" .
654                         "#define VCSBRANCH \"" . $repo_branch . "\"\n";
655         } else {
656                 $VCS_REVISION = "#define VCSVERSION \"" . $vcs_name .
657                         " Rev Unknown\"\n" .
658                         "#define VCSBRANCH \"unknown\"\n";
659         }
660
661         return $VCS_REVISION;
662 }
663
664 # Print the version control system's version to $version_file.
665 # Don't change the file if it is not needed.
666 sub print_VCS_REVISION
667 {
668         my $VCS_REVISION;
669         my $needs_update = 1;
670         my $set_header = 1;
671         my $git_cdir;
672
673         chomp($git_cdir = qx{git --git-dir="$srcdir/.git" rev-parse --git-common-dir 2> $devnull});
674         if ($git_cdir && -f "$git_cdir/wireshark-disable-versioning") {
675                 print_diag "Header versioning disabled using git override.\n";
676                 $set_header = 0;
677         }
678
679         $VCS_REVISION = new_version_h($set_header);
680         if (open(OLDREV, "<$version_file")) {
681                 my $old_VCS_REVISION = <OLDREV> . <OLDREV>;
682                 if ($old_VCS_REVISION eq $VCS_REVISION) {
683                         $needs_update = 0;
684                 }
685                 close OLDREV;
686         }
687
688         if (! $set_vcs) { return; }
689
690         if ($needs_update) {
691                 # print "Updating $version_file so it contains:\n$VCS_REVISION";
692                 open(VER, ">$version_file") || die ("Cannot write to $version_file ($!)\n");
693                 print VER "$VCS_REVISION";
694                 close VER;
695                 print "$version_file has been updated.\n";
696         } elsif (!$set_header) {
697                 print "$version_file disabled.\n";
698         } else {
699                 print "$version_file unchanged.\n";
700         }
701 }
702
703 # Read values from the configuration file, if it exists.
704 sub get_config {
705         my $arg;
706         my $show_help = 0;
707
708         # Get our command-line args
709         # XXX - Do we need an option to undo --set-release?
710         GetOptions(
711                    "help|h", \$show_help,
712                    "get-vcs|get-svn|g", \$get_vcs,
713                    "set-vcs|set-svn|s", \$set_vcs,
714                    "git-bin", \$git_executable,
715                    "print-vcs", \$print_vcs,
716                    "set-version|v", \$set_version,
717                    "set-release|r|package-version|p", \$set_release,
718                    "verbose", \$verbose
719                    ) || pod2usage(2);
720
721         if ($show_help) { pod2usage(1); }
722
723         if ( !( $show_help || $get_vcs || $set_vcs || $print_vcs || $set_version || $set_release ) ) {
724                 $set_vcs = 1;
725         }
726
727         if ($#ARGV >= 0) {
728                 $srcdir = $ARGV[0]
729         }
730
731         if (! open(FILE, "<$vconf_file")) {
732                 print_diag "Version configuration file $vconf_file not "
733                 . "found. Using defaults.\n";
734                 return 1;
735         }
736
737         while (<FILE>) {
738                 s/^\s+|\s+$//g; # chomp() may not handle CR
739                 next if (/^#/);
740                 next unless (/^(\w+)(:|=)\s*(\S.*)/);
741                 $version_pref{$1} = $3;
742         }
743         close FILE;
744         return 1;
745 }
746
747 ##
748 ## Start of code
749 ##
750
751 &get_config();
752
753 &read_repo_info();
754
755 &print_VCS_REVISION;
756
757 if ($set_version || $set_release) {
758         if ($set_version) {
759                 print "Generating version information\n";
760         }
761
762         if ($version_pref{"enable"} == 0) {
763                 print "Release information disabled in $vconf_file.\n";
764                 $set_release = 0;
765         }
766
767         if ($set_release) {
768                 print "Generating release information\n";
769         } else {
770                 print "Resetting release information\n";
771                 $num_commits = 0;
772                 $package_string = "";
773         }
774
775         &update_versioned_files;
776 }
777
778 __END__
779
780 =head1 NAM
781
782 make-version.pl - Get and set build-time version information for Wireshark
783
784 =head1 SYNOPSIS
785
786 make-version.pl [options] [source directory]
787
788   Options:
789
790     --help, -h                 This help message
791     --get-vcs, -g              Print the VCS revision and source.
792     --set-vcs, -s              Set the information in version.h
793     --print-vcs                Print the vcs version to standard output
794     --set-version, -v          Set the major, minor, and micro versions in
795                                the top-level CMakeLists.txt, configure.ac,
796                                config.nmake, docbook/asciidoc.conf,
797                                debian/changelog, the Makefile.am for all
798                                libraries, and the CMakeLists.txt for all
799                                libraries.
800                                Resets the release information when used by
801                                itself.
802     --set-release, -r          Set the release information in the top-level
803                                CMakeLists.txt, configure.ac, and
804                                config.nmake.
805     --package-version, -p      Deprecated. Same as --set-release.
806     --verbose                  Print diagnostic messages to STDERR.
807
808 Options can be used in any combination. If none are specified B<--set-vcs>
809 is assumed.
810
811 =cut
812
813 #
814 # Editor modelines  -  http://www.wireshark.org/tools/modelines.html
815 #
816 # Local variables:
817 # c-basic-offset: 8
818 # tab-width: 8
819 # indent-tabs-mode: t
820 # End:
821 #
822 # vi: set shiftwidth=8 tabstop=8 noexpandtab:
823 # :indentSize=8:tabSize=8:noTabs=false:
824 #
825 #