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