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