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