CMake: Make PROJECT_VERSION_EXTENSION code simpler
[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" => 1,
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 = "cd $srcdir; svn info";
146                 $version_pref{"svn_client"} = 1;
147         } elsif (-d "$srcdir/.git/svn") {
148                 $info_source = "Command line (git-svn)";
149                 $info_cmd = "(cd $srcdir; $git_executable svn info)";
150         }
151
152         #Git can give us:
153         #
154         # A big ugly hash: git rev-parse HEAD
155         # 1ddc83849075addb0cac69a6fe3782f4325337b9
156         #
157         # A small ugly hash: git rev-parse --short HEAD
158         # 1ddc838
159         #
160         # The upstream branch path: git rev-parse --abbrev-ref --symbolic-full-name @{upstream}
161         # origin/master
162         #
163         # A version description: git describe --tags --dirty
164         # wireshark-1.8.12-15-g1ddc838
165         #
166         # Number of commits in this branch: git rev-list --count HEAD
167         # 48879
168         #
169         # Number of commits since 1.8.0: git rev-list --count 5e212d72ce098a7fec4332cbe6c22fcda796a018..HEAD
170         # 320
171         #
172         # Refs: git ls-remote code.wireshark.org:wireshark
173         # ea19c7f952ce9fc53fe4c223f1d9d6797346258b (r48972, changed version to 1.11.0)
174
175         if ($version_pref{"git_client"}) {
176                 eval {
177                         use warnings "all";
178                         no warnings "all";
179
180                         chomp($line = qx{$git_executable --git-dir="$srcdir"/.git log -1 --pretty=format:%at});
181                         if ($? == 0 && length($line) > 1) {
182                                 $last_change = $line;
183                         }
184
185                         # Commits since last annotated tag.
186                         chomp($line = qx{$git_executable --git-dir="$srcdir"/.git describe --long --always --match "v*"});
187                         if ($? == 0 && length($line) > 1) {
188                                 my @parts = split(/-/, $line);
189                                 $git_description = $line;
190                                 $num_commits = $parts[-2];
191                                 $commit_id = $parts[-1];
192                         }
193
194                         # This will break in some cases. Hopefully not during
195                         # official package builds.
196                         chomp($line = qx{$git_executable --git-dir="$srcdir"/.git rev-parse --abbrev-ref --symbolic-full-name \@\{upstream\} 2> $devnull});
197                         if ($? == 0 && length($line) > 1) {
198                                 $repo_branch = basename($line);
199                         }
200
201                         1;
202                 };
203
204                 if ($last_change && $num_commits && $repo_branch) {
205                         $do_hack = 0;
206                 }
207         } elsif ($version_pref{"svn_client"}) {
208                 my $repo_root = undef;
209                 my $repo_url = undef;
210                 eval {
211                         use warnings "all";
212                         no warnings "all";
213                         $line = qx{$info_cmd};
214                         if (defined($line)) {
215                                 if ($line =~ /Last Changed Date: (\d{4})-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d)/) {
216                                         $last_change = timegm($6, $5, $4, $3, $2 - 1, $1);
217                                 }
218                                 if ($line =~ /Last Changed Rev: (\d+)/) {
219                                         $num_commits = $1;
220                                 }
221                                 if ($line =~ /URL: (\S+)/) {
222                                         $repo_url = $1;
223                                 }
224                                 if ($line =~ /Repository Root: (\S+)/) {
225                                         $repo_root = $1;
226                                 }
227                                 $vcs_name = "SVN";
228                         }
229                         1;
230                 };
231
232                 if ($repo_url && $repo_root && index($repo_url, $repo_root) == 0) {
233                         $repo_branch = substr($repo_url, length($repo_root));
234                 }
235
236                 if ($last_change && $num_commits && $repo_url && $repo_root) {
237                         $do_hack = 0;
238                 }
239         } elsif ($version_pref{"tortoise_svn"}) {
240                 # Dynamically generic template file needed by TortoiseSVN
241                 open(TORTOISE, ">$tortoise_file");
242                 print TORTOISE "#define VCSVERSION \"\$WCREV\$\"\r\n";
243                 print TORTOISE "#define VCSBRANCH \"\$WCURL\$\"\r\n";
244                 close(TORTOISE);
245
246                 $info_source = "Command line (SubWCRev)";
247                 $info_cmd = "SubWCRev $srcdir $tortoise_file $version_file";
248                 my $tortoise = system($info_cmd);
249                 if ($tortoise == 0) {
250                         $do_hack = 0;
251                 }
252                 $vcs_name = "SVN";
253
254                 #clean up the template file
255                 unlink($tortoise_file);
256         }
257
258         if ($num_commits == 0) {
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
409         return if (!$set_version && $package_string eq "");
410
411         open(CFGIN, "< $filepath") || die "Can't read $filepath!";
412         while ($line = <CFGIN>) {
413                 if ($line =~ /^set *\( *GIT_REVISION .*([\r\n]+)$/) {
414                         $line = sprintf("set(GIT_REVISION %d)$1", $num_commits);
415                 } elsif ($line =~ /^set *\( *PROJECT_MAJOR_VERSION .*([\r\n]+)$/) {
416                         $line = sprintf("set(PROJECT_MAJOR_VERSION %d)$1", $version_pref{"version_major"});
417                 } elsif ($line =~ /^set *\( *PROJECT_MINOR_VERSION .*([\r\n]+)$/) {
418                         $line = sprintf("set(PROJECT_MINOR_VERSION %d)$1", $version_pref{"version_minor"});
419                 } elsif ($line =~ /^set *\( *PROJECT_PATCH_VERSION .*([\r\n]+)$/) {
420                         $line = sprintf("set(PROJECT_PATCH_VERSION %d)$1", $version_pref{"version_micro"});
421                 } elsif ($line =~ /^set *\( *PROJECT_VERSION_EXTENSION .*([\r\n]+)$/) {
422                         $line = sprintf("set(PROJECT_VERSION_EXTENSION \"%s\")$1", $package_string);
423                 }
424                 $contents .= $line
425         }
426
427         open(CFGIN, "> $filepath") || die "Can't write $filepath!";
428         print(CFGIN $contents);
429         close(CFGIN);
430         print "$filepath has been updated.\n";
431 }
432
433 # Read configure.ac, then write it back out with an updated
434 # "AC_INIT" line.
435 sub update_configure_ac
436 {
437         my $line;
438         my $contents = "";
439         my $version = "";
440         my $filepath = "$srcdir/configure.ac";
441
442         return if (!$set_version && $package_string eq "");
443
444         open(CFGIN, "< $filepath") || die "Can't read $filepath!";
445         while ($line = <CFGIN>) {
446                 if ($line =~ /^m4_define\( *\[?version_major\]? *,.*([\r\n]+)$/) {
447                         $line = sprintf("m4_define([version_major], [%d])$1", $version_pref{"version_major"});
448                 } elsif ($line =~ /^m4_define\( *\[?version_minor\]? *,.*([\r\n]+)$/) {
449                         $line = sprintf("m4_define([version_minor], [%d])$1", $version_pref{"version_minor"});
450                 } elsif ($line =~ /^m4_define\( *\[?version_micro\]? *,.*([\r\n]+)$/) {
451                         $line = sprintf("m4_define([version_micro], [%d])$1", $version_pref{"version_micro"});
452                 } elsif ($line =~ /^m4_define\( *\[?version_extra\]? *,.*([\r\n]+)$/) {
453                         $line = sprintf("m4_define([version_extra], [%s])$1", $package_string);
454                 }
455                 $contents .= $line
456         }
457
458         open(CFGIN, "> $filepath") || die "Can't write $filepath!";
459         print(CFGIN $contents);
460         close(CFGIN);
461         print "$filepath has been updated.\n";
462 }
463
464 # Read config.nmake, then write it back out with an updated
465 # "VERSION" line.
466 sub update_config_nmake
467 {
468         my $line;
469         my $contents = "";
470         my $version = "";
471         my $filepath = "$srcdir/config.nmake";
472         my $win_package_string = "\$(WIRESHARK_VERSION_EXTRA)";
473
474         if ($package_string ne "") { $win_package_string = $package_string; }
475
476
477         open(CFGNMAKE, "< $filepath") || die "Can't read $filepath!";
478         while ($line = <CFGNMAKE>) {
479                 if ($line =~ /^VCS_REVISION=.*([\r\n]+)$/) {
480                         $line = sprintf("VCS_REVISION=%d$1", $num_commits);
481                 } elsif ($set_version && $line =~ /^VERSION_MAJOR=.*([\r\n]+)$/) {
482                         $line = sprintf("VERSION_MAJOR=%d$1", $version_pref{"version_major"});
483                 } elsif ($set_version && $line =~ /^VERSION_MINOR=.*([\r\n]+)$/) {
484                         $line = sprintf("VERSION_MINOR=%d$1", $version_pref{"version_minor"});
485                 } elsif ($set_version && $line =~ /^VERSION_MICRO=.*([\r\n]+)$/) {
486                         $line = sprintf("VERSION_MICRO=%d$1", $version_pref{"version_micro"});
487                 } elsif ($line =~ /^VERSION_EXTRA=.*([\r\n]+)$/) {
488                         $line = "VERSION_EXTRA=$win_package_string$1";
489                 }
490                 $contents .= $line
491         }
492
493         open(CFGNMAKE, "> $filepath") || die "Can't write $filepath!";
494         print(CFGNMAKE $contents);
495         close(CFGNMAKE);
496         print "$filepath has been updated.\n";
497 }
498
499 # Read docbook/asciidoc.conf, then write it back out with an updated
500 # wireshark-version replacement line.
501 sub update_release_notes
502 {
503         my $line;
504         my $contents = "";
505         my $version = "";
506         my $filepath = "$srcdir/docbook/asciidoc.conf";
507
508         open(ADOC_CONF, "< $filepath") || die "Can't read $filepath!";
509         while ($line = <ADOC_CONF>) {
510                 # wireshark-version:\[\]=1.9.1
511
512                 if ($line =~ /^wireshark-version=.*([\r\n]+)$/) {
513                         $line = sprintf("wireshark-version=%d.%d.%d$1",
514                                         $version_pref{"version_major"},
515                                         $version_pref{"version_minor"},
516                                         $version_pref{"version_micro"},
517                                        );
518                 }
519                 $contents .= $line
520         }
521
522         open(ADOC_CONF, "> $filepath") || die "Can't write $filepath!";
523         print(ADOC_CONF $contents);
524         close(ADOC_CONF);
525         print "$filepath has been updated.\n";
526 }
527
528 # Read debian/changelog, then write back out an updated version.
529 sub update_debian_changelog
530 {
531         my $line;
532         my $contents = "";
533         my $version = "";
534         my $filepath = "$srcdir/debian/changelog";
535
536         open(CHANGELOG, "< $filepath") || die "Can't read $filepath!";
537         while ($line = <CHANGELOG>) {
538                 if ($set_version && CHANGELOG->input_line_number() == 1) {
539                         $line = sprintf("wireshark (%d.%d.%d) unstable; urgency=low\n",
540                                         $version_pref{"version_major"},
541                                         $version_pref{"version_minor"},
542                                         $version_pref{"version_micro"},
543                                        );
544                 }
545                 $contents .= $line
546         }
547
548         open(CHANGELOG, "> $filepath") || die "Can't write $filepath!";
549         print(CHANGELOG $contents);
550         close(CHANGELOG);
551         print "$filepath has been updated.\n";
552 }
553
554 # Read Makefile.am for each library, then write back out an updated version.
555 sub update_automake_lib_releases
556 {
557         my $line;
558         my $contents = "";
559         my $version = "";
560         my $filedir;
561         my $filepath;
562
563         # The Libtool manual says
564         #   "If the library source code has changed at all since the last
565         #    update, then increment revision (‘c:r:a’ becomes ‘c:r+1:a’)."
566         # epan changes with each minor release, almost by definition. wiretap
567         # changes with *most* releases.
568         #
569         # http://www.gnu.org/software/libtool/manual/libtool.html#Updating-version-info
570         for $filedir ("epan", "wiretap") {      # "wsutil"
571                 $contents = "";
572                 $filepath = $filedir . "/Makefile.am";
573                 open(MAKEFILE_AM, "< $filepath") || die "Can't read $filepath!";
574                 while ($line = <MAKEFILE_AM>) {
575                         # libwireshark_la_LDFLAGS = -version-info 2:1:1 -export-symbols
576
577                         if ($line =~ /^(lib\w+_la_LDFLAGS.*version-info\s+\d+:)\d+(:\d+.*)/) {
578                                 $line = sprintf("$1%d$2\n", $version_pref{"version_micro"});
579                         }
580                         $contents .= $line
581                 }
582
583                 open(MAKEFILE_AM, "> $filepath") || die "Can't write $filepath!";
584                 print(MAKEFILE_AM $contents);
585                 close(MAKEFILE_AM);
586                 print "$filepath has been updated.\n";
587         }
588 }
589
590 # Read CMakeLists.txt for each library, then write back out an updated version.
591 sub update_cmake_lib_releases
592 {
593         my $line;
594         my $contents = "";
595         my $version = "";
596         my $filedir;
597         my $filepath;
598
599         for $filedir ("epan", "wiretap") {      # "wsutil"
600                 $contents = "";
601                 $filepath = $filedir . "/CMakeLists.txt";
602                 open(CMAKELISTS_TXT, "< $filepath") || die "Can't read $filepath!";
603                 while ($line = <CMAKELISTS_TXT>) {
604                         # set(FULL_SO_VERSION "0.0.0")
605
606                         if ($line =~ /^(set\s*\(\s*FULL_SO_VERSION\s+"\d+\.\d+\.)\d+(".*)/) {
607                                 $line = sprintf("$1%d$2\n", $version_pref{"version_micro"});
608                         }
609                         $contents .= $line
610                 }
611
612                 open(CMAKELISTS_TXT, "> $filepath") || die "Can't write $filepath!";
613                 print(CMAKELISTS_TXT $contents);
614                 close(CMAKELISTS_TXT);
615                 print "$filepath has been updated.\n";
616         }
617 }
618
619 # Update distributed files that contain any version information
620 sub update_versioned_files
621 {
622         # Matches CMakeLists.txt
623         printf "GR: %d, MaV: %d, MiV: %d, PL: %d, EV: %s\n",
624                 $num_commits, $version_pref{"version_major"},
625                 $version_pref{"version_minor"}, $version_pref{"version_micro"},
626                 $package_string;
627         &update_cmakelists_txt;
628         &update_configure_ac;
629         &update_config_nmake;
630         if ($set_version) {
631                 &update_release_notes;
632                 &update_debian_changelog;
633                 &update_automake_lib_releases;
634                 &update_cmake_lib_releases;
635         }
636 }
637
638 sub new_version_h
639 {
640         my $VCS_REVISION;
641         my $set_header = shift @_;
642
643         if (!$set_header) {
644                 $VCS_REVISION = "/* #undef VCSVERSION */\n";
645                 $VCS_REVISION .= "/* #undef VCSBRANCH */\n";
646                 return $VCS_REVISION;
647         }
648
649         if ($git_description) {
650                 $VCS_REVISION = "#define VCSVERSION \"" .
651                         $git_description . "\"\n" .
652                         "#define VCSBRANCH \"" . $repo_branch . "\"\n";
653         } elsif ($last_change && $num_commits) {
654                 $VCS_REVISION = "#define VCSVERSION \"" . $vcs_name . " Rev " .
655                         $num_commits . "\"\n" .
656                         "#define VCSBRANCH \"" . $repo_branch . "\"\n";
657         } else {
658                 $VCS_REVISION = "#define VCSVERSION \"" . $vcs_name .
659                         " Rev Unknown\"\n" .
660                         "#define VCSBRANCH \"unknown\"\n";
661         }
662
663         return $VCS_REVISION;
664 }
665
666 # Print the version control system's version to $version_file.
667 # Don't change the file if it is not needed.
668 sub print_VCS_REVISION
669 {
670         my $VCS_REVISION;
671         my $needs_update = 1;
672         my $set_header = 1;
673         my $git_cdir;
674
675         chomp($git_cdir = qx{git --git-dir="$srcdir/.git" rev-parse --git-common-dir 2> $devnull});
676         if ($git_cdir && -f "$git_cdir/wireshark-disable-versioning") {
677                 print_diag "Header versioning disabled using git override.\n";
678                 $set_header = 0;
679         }
680
681         $VCS_REVISION = new_version_h($set_header);
682         if (open(OLDREV, "<$version_file")) {
683                 my $old_VCS_REVISION = <OLDREV> . <OLDREV>;
684                 if ($old_VCS_REVISION eq $VCS_REVISION) {
685                         $needs_update = 0;
686                 }
687                 close OLDREV;
688         }
689
690         if (! $set_vcs) { return; }
691
692         if ($needs_update) {
693                 # print "Updating $version_file so it contains:\n$VCS_REVISION";
694                 open(VER, ">$version_file") || die ("Cannot write to $version_file ($!)\n");
695                 print VER "$VCS_REVISION";
696                 close VER;
697                 print "$version_file has been updated.\n";
698         } elsif (!$set_header) {
699                 print "$version_file disabled.\n";
700         } else {
701                 print "$version_file unchanged.\n";
702         }
703 }
704
705 # Read values from the configuration file, if it exists.
706 sub get_config {
707         my $arg;
708         my $show_help = 0;
709
710         # Get our command-line args
711         # XXX - Do we need an option to undo --set-release?
712         GetOptions(
713                    "help|h", \$show_help,
714                    "get-vcs|get-svn|g", \$get_vcs,
715                    "set-vcs|set-svn|s", \$set_vcs,
716                    "git-bin", \$git_executable,
717                    "print-vcs", \$print_vcs,
718                    "set-version|v", \$set_version,
719                    "set-release|r|package-version|p", \$set_release,
720                    "verbose", \$verbose
721                    ) || pod2usage(2);
722
723         if ($show_help) { pod2usage(1); }
724
725         if ( !( $show_help || $get_vcs || $set_vcs || $print_vcs || $set_version || $set_release ) ) {
726                 $set_vcs = 1;
727         }
728
729         if ($#ARGV >= 0) {
730                 $srcdir = $ARGV[0]
731         }
732
733         if (! open(FILE, "<$vconf_file")) {
734                 print_diag "Version configuration file $vconf_file not "
735                 . "found. Using defaults.\n";
736                 return 1;
737         }
738
739         while (<FILE>) {
740                 s/^\s+|\s+$//g; # chomp() may not handle CR
741                 next if (/^#/);
742                 next unless (/^(\w+)(:|=)\s*(\S.*)/);
743                 $version_pref{$1} = $3;
744         }
745         close FILE;
746         return 1;
747 }
748
749 ##
750 ## Start of code
751 ##
752
753 &get_config();
754
755 &read_repo_info();
756
757 &print_VCS_REVISION;
758
759 if ($set_version || $set_release) {
760         if ($set_version) {
761                 print "Generating version information.\n";
762         }
763
764         if ($version_pref{"enable"} == 0) {
765                 print "Release information disabled in $vconf_file.\n";
766                 $set_release = 0;
767         }
768
769         if ($set_release) {
770                 print "Generating release information.\n";
771         } else {
772                 print "Resetting release information\n";
773                 $num_commits = 0;
774                 $package_string = "";
775         }
776
777         &update_versioned_files;
778 }
779
780 __END__
781
782 =head1 NAM
783
784 make-version.pl - Get and set build-time version information for Wireshark
785
786 =head1 SYNOPSIS
787
788 make-version.pl [options] [source directory]
789
790   Options:
791
792     --help, -h                 This help message
793     --get-vcs, -g              Print the VCS revision and source.
794     --set-vcs, -s              Set the information in version.h
795     --print-vcs                Print the vcs version to standard output
796     --set-version, -v          Set the major, minor, and micro versions in
797                                the top-level CMakeLists.txt, configure.ac,
798                                config.nmake, docbook/asciidoc.conf,
799                                debian/changelog, the Makefile.am for all
800                                libraries, and the CMakeLists.txt for all
801                                libraries.
802                                Resets the release information when used by
803                                itself.
804     --set-release, -r          Set the release information in the top-level
805                                CMakeLists.txt, configure.ac, and
806                                config.nmake.
807     --package-version, -p      Deprecated. Same as --set-release.
808     --verbose                  Print diagnostic messages to STDERR.
809
810 Options can be used in any combination. If none are specified B<--set-vcs>
811 is assumed.
812
813 =cut
814
815 #
816 # Editor modelines  -  http://www.wireshark.org/tools/modelines.html
817 #
818 # Local variables:
819 # c-basic-offset: 8
820 # tab-width: 8
821 # indent-tabs-mode: t
822 # End:
823 #
824 # vi: set shiftwidth=8 tabstop=8 noexpandtab:
825 # :indentSize=8:tabSize=8:noTabs=false:
826 #
827 #