3 # Copyright 2004 Jörg Mayer (see AUTHORS file)
5 # Wireshark - Network traffic analyzer
6 # By Gerald Combs <gerald@wireshark.org>
7 # Copyright 1998 Gerald Combs
9 # SPDX-License-Identifier: GPL-2.0-or-later
13 # If "version.conf" is present, it is parsed for configuration values.
14 # Possible values are:
16 # enable - Enable or disable versioning. Zero (0) disables, nonzero
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
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
25 # pkg_enable - Enable or disable local package versioning.
26 # pkg_format - Like "format", but used for the local package version.
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.
33 # Default configuration:
40 # format: git %Y%m%d%H%M%S
44 # XXX - We're pretty dumb about the "%#" substitution, and about having
45 # spaces in the package format.
52 use POSIX qw(strftime);
58 my $version_file = 'version.h';
59 my $package_string = "";
60 my $vconf_file = 'version.conf';
62 my $tortoise_file = "tortoise_template";
66 my $repo_branch = "unknown";
67 my $git_description = undef;
80 "git_client" => 0, # set if .git found and .git/svn not found
81 "svn_client" => 0, # set if .svn found
82 "git_svn" => 0, # set if both .git and .git/svn are found
84 "format" => "git %Y%m%d%H%M%S",
86 # Normal development builds
88 "pkg_format" => "-%#",
90 # Development releases
97 my $devnull = File::Spec->devnull();
98 my $enable_vcsversion = 1;
100 # Ensure we run with correct locale
103 $ENV{GIT_PAGER} = "";
106 print STDERR @_ if $verbose;
109 # Attempt to get revision information from the repository.
112 my $version_format = $version_pref{"format"};
113 my $package_format = "";
118 my $info_source = "Unknown";
123 if ($version_pref{"pkg_enable"} > 0) {
124 $package_format = $version_pref{"pkg_format"};
127 # For tarball releases, do not invoke git at all and instead rely on
128 # versioning information that was provided at tarball creation time.
129 if ($version_pref{"git_description"}) {
130 $info_source = "version.conf file";
131 } elsif (-e "$srcdir/.git" && ! -d "$srcdir/.git/svn") {
132 $info_source = "Command line (git)";
133 $version_pref{"git_client"} = 1;
135 } elsif (-d "$srcdir/.svn" or -d "$srcdir/../.svn") {
136 $info_source = "Command line (svn info)";
137 $info_cmd = "cd $srcdir; svn info";
138 $version_pref{"svn_client"} = 1;
139 } elsif (-d "$srcdir/.git/svn") {
140 $info_source = "Command line (git-svn)";
141 $info_cmd = "(cd $srcdir; git svn info)";
143 $version_pref{"git_svn"} = 1;
146 # Make sure git is available.
147 if ($is_git_repo && !`git --version`) {
148 print STDERR "Git unavailable. Git revision will be missing from version string.\n";
152 # Check whether to include VCS version information in version.h
154 chomp($git_cdir = qx{git --git-dir="$srcdir/.git" rev-parse --git-common-dir 2> $devnull});
155 if ($git_cdir && -f "$git_cdir/wireshark-disable-versioning") {
156 print_diag "Header versioning disabled using git override.\n";
157 $enable_vcsversion = 0;
163 # A big ugly hash: git rev-parse HEAD
164 # 1ddc83849075addb0cac69a6fe3782f4325337b9
166 # A small ugly hash: git rev-parse --short HEAD
169 # The upstream branch path: git rev-parse --abbrev-ref --symbolic-full-name @{upstream}
172 # A version description: git describe --tags --dirty
173 # wireshark-1.8.12-15-g1ddc838
175 # Number of commits in this branch: git rev-list --count HEAD
178 # Number of commits since 1.8.0: git rev-list --count 5e212d72ce098a7fec4332cbe6c22fcda796a018..HEAD
181 # Refs: git ls-remote code.wireshark.org:wireshark
182 # ea19c7f952ce9fc53fe4c223f1d9d6797346258b (r48972, changed version to 1.11.0)
184 if ($version_pref{"git_description"}) {
185 $git_description = $version_pref{"git_description"};
187 # Assume format like v2.3.0rc0-1342-g7bdcf75
188 $commit_id = ($git_description =~ /([0-9a-f]+)$/)[0];
189 } elsif ($version_pref{"git_client"}) {
194 chomp($line = qx{git --git-dir="$srcdir"/.git log -1 --pretty=format:%at});
195 if ($? == 0 && length($line) > 1) {
196 $last_change = $line;
199 # Commits since last annotated tag.
200 chomp($line = qx{git --git-dir="$srcdir"/.git describe --abbrev=8 --long --always --match "v[1-9]*"});
201 if ($? == 0 && length($line) > 1) {
202 my @parts = split(/-/, $line);
203 $git_description = $line;
204 $num_commits = $parts[-2];
205 $commit_id = $parts[-1];
208 # This will break in some cases. Hopefully not during
209 # official package builds.
210 chomp($line = qx{git --git-dir="$srcdir"/.git rev-parse --abbrev-ref --symbolic-full-name \@\{upstream\} 2> $devnull});
211 if ($? == 0 && length($line) > 1) {
212 $repo_branch = basename($line);
218 if ($last_change && $num_commits && $repo_branch) {
221 } elsif ($version_pref{"svn_client"} || $version_pref{"git_svn"}) {
222 my $repo_root = undef;
223 my $repo_url = undef;
227 $line = qx{$info_cmd};
228 if (defined($line)) {
229 if ($line =~ /Last Changed Date: (\d{4})-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d)/) {
230 $last_change = timegm($6, $5, $4, $3, $2 - 1, $1);
232 if ($line =~ /Last Changed Rev: (\d+)/) {
235 if ($line =~ /URL: (\S+)/) {
238 if ($line =~ /Repository Root: (\S+)/) {
246 if ($repo_url && $repo_root && index($repo_url, $repo_root) == 0) {
247 $repo_branch = substr($repo_url, length($repo_root));
250 if ($last_change && $num_commits && $repo_url && $repo_root) {
253 } elsif ($version_pref{"tortoise_svn"}) {
254 # Dynamically generic template file needed by TortoiseSVN
255 open(TORTOISE, ">$tortoise_file");
256 print TORTOISE "#define VCSVERSION \"\$WCREV\$\"\r\n";
257 print TORTOISE "#define VCSBRANCH \"\$WCURL\$\"\r\n";
260 $info_source = "Command line (SubWCRev)";
261 $info_cmd = "SubWCRev $srcdir $tortoise_file $version_file";
262 my $tortoise = system($info_cmd);
263 if ($tortoise == 0) {
268 #clean up the template file
269 unlink($tortoise_file);
272 if ($num_commits == 0 and -e "$srcdir/.git") {
278 # If someone had properly tagged 1.9.0 we could also use
279 # "git describe --abbrev=1 --tags HEAD"
281 $info_cmd = "(cd $srcdir; git log --format='%b' -n 1)";
282 $line = qx{$info_cmd};
283 if (defined($line)) {
284 if ($line =~ /svn path=.*; revision=(\d+)/) {
288 $info_cmd = "(cd $srcdir; git 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);
295 $info_cmd = "(cd $srcdir; git branch)";
296 $line = qx{$info_cmd};
297 if (defined($line)) {
298 if ($line =~ /\* (\S+)/) {
305 if ($num_commits == 0 and -d "$srcdir/.bzr") {
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);
317 if ($line =~ /svn revno: (\d+) \(on (\S+)\)/) {
328 # 'svn info' failed or the user really wants us to dig around in .svn/entries
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";
334 $info_source = "Prodding .svn";
335 # We need to find out whether our parser can handle the entries file
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";
343 $repo_version = "unknown";
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/) {
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);
358 if ($line =~ /revision="(\d+)"/) { $num_commits = $1; }
360 if ($line =~ /\/>/) {
361 if (($svn_name eq "" || $svn_name eq "svn:this_dir") &&
362 $last_change && $num_commits) {
367 # XXX - Fetch the repository root & URL
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/;
379 $package_format =~ s/%#/$num_commits-$commit_id/;
381 $package_format =~ s/%#/$num_commits/;
383 $package_string = strftime($package_format, gmtime($last_change));
388 Commit distance : $num_commits
389 Commit ID : $commit_id
390 Revision source : $info_source
391 Release stamp : $package_string
393 } elsif ($print_vcs) {
394 print new_version_h();
399 # Read CMakeLists.txt, then write it back out with updated "set(PROJECT_..._VERSION ...)
401 # set(GIT_REVISION 999)
402 # set(PROJECT_MAJOR_VERSION 1)
403 # set(PROJECT_MINOR_VERSION 99)
404 # set(PROJECT_PATCH_VERSION 0)
405 # set(PROJECT_VERSION_EXTENSION "-rc5")
406 sub update_cmakelists_txt
411 my $filepath = "$srcdir/CMakeLists.txt";
413 return if (!$set_version && $package_string eq "");
415 open(CFGIN, "< $filepath") || die "Can't read $filepath!";
416 while ($line = <CFGIN>) {
417 if ($line =~ /^set *\( *GIT_REVISION .*?([\r\n]+)$/) {
418 $line = sprintf("set(GIT_REVISION %d)$1", $num_commits);
419 } elsif ($line =~ /^set *\( *PROJECT_MAJOR_VERSION .*?([\r\n]+)$/) {
420 $line = sprintf("set(PROJECT_MAJOR_VERSION %d)$1", $version_pref{"version_major"});
421 } elsif ($line =~ /^set *\( *PROJECT_MINOR_VERSION .*?([\r\n]+)$/) {
422 $line = sprintf("set(PROJECT_MINOR_VERSION %d)$1", $version_pref{"version_minor"});
423 } elsif ($line =~ /^set *\( *PROJECT_PATCH_VERSION .*?([\r\n]+)$/) {
424 $line = sprintf("set(PROJECT_PATCH_VERSION %d)$1", $version_pref{"version_micro"});
425 } elsif ($line =~ /^set *\( *PROJECT_VERSION_EXTENSION .*?([\r\n]+)$/) {
426 $line = sprintf("set(PROJECT_VERSION_EXTENSION \"%s\")$1", $package_string);
431 open(CFGIN, "> $filepath") || die "Can't write $filepath!";
432 print(CFGIN $contents);
434 print "$filepath has been updated.\n";
437 # Read docbook/attributes.asciidoc, then write it back out with an updated
438 # wireshark-version replacement line.
439 sub update_attributes_asciidoc
444 my $filepath = "$srcdir/docbook/attributes.asciidoc";
446 open(ADOC_CONF, "< $filepath") || die "Can't read $filepath!";
447 while ($line = <ADOC_CONF>) {
448 # :wireshark-version: 2.3.1
450 if ($line =~ /^:wireshark-version:.*?([\r\n]+)$/) {
451 $line = sprintf(":wireshark-version: %d.%d.%d$1",
452 $version_pref{"version_major"},
453 $version_pref{"version_minor"},
454 $version_pref{"version_micro"},
460 open(ADOC_CONF, "> $filepath") || die "Can't write $filepath!";
461 print(ADOC_CONF $contents);
463 print "$filepath has been updated.\n";
466 sub update_docinfo_asciidoc
469 my @paths = ("$srcdir/docbook/developer-guide-docinfo.xml",
470 "$srcdir/docbook/user-guide-docinfo.xml");
472 foreach my $filepath (@paths) {
474 open(DOCINFO_XML, "< $filepath") || die "Can't read $filepath!";
475 while ($line = <DOCINFO_XML>) {
476 if ($line =~ /^<subtitle>For Wireshark \d.\d+<\/subtitle>([\r\n]+)$/) {
477 $line = sprintf("<subtitle>For Wireshark %d.%d</subtitle>$1",
478 $version_pref{"version_major"},
479 $version_pref{"version_minor"},
485 open(DOCINFO_XML, "> $filepath") || die "Can't write $filepath!";
486 print(DOCINFO_XML $contents);
488 print "$filepath has been updated.\n";
492 # Read debian/changelog, then write back out an updated version.
493 sub update_debian_changelog
498 my $filepath = "$srcdir/debian/changelog";
500 open(CHANGELOG, "< $filepath") || die "Can't read $filepath!";
501 while ($line = <CHANGELOG>) {
502 if ($set_version && CHANGELOG->input_line_number() == 1) {
503 $line =~ /^.*?([\r\n]+)$/;
504 $line = sprintf("wireshark (%d.%d.%d) unstable; urgency=low$1",
505 $version_pref{"version_major"},
506 $version_pref{"version_minor"},
507 $version_pref{"version_micro"},
513 open(CHANGELOG, "> $filepath") || die "Can't write $filepath!";
514 print(CHANGELOG $contents);
516 print "$filepath has been updated.\n";
519 # Read CMakeLists.txt for each library, then write back out an updated version.
520 sub update_cmake_lib_releases
528 for $filedir ("$srcdir/epan", "$srcdir/wiretap") { # "$srcdir/wsutil"
530 $filepath = $filedir . "/CMakeLists.txt";
531 open(CMAKELISTS_TXT, "< $filepath") || die "Can't read $filepath!";
532 while ($line = <CMAKELISTS_TXT>) {
533 # VERSION "0.0.0" SOVERSION 0
535 if ($line =~ /^(\s*VERSION\s+"\d+\.\d+\.)\d+(".*[\r\n]+)$/) {
536 $line = sprintf("$1%d$2", $version_pref{"version_micro"});
541 open(CMAKELISTS_TXT, "> $filepath") || die "Can't write $filepath!";
542 print(CMAKELISTS_TXT $contents);
543 close(CMAKELISTS_TXT);
544 print "$filepath has been updated.\n";
548 # Update distributed files that contain any version information
549 sub update_versioned_files
551 # Matches CMakeLists.txt
552 printf "GR: %d, MaV: %d, MiV: %d, PL: %d, EV: %s\n",
553 $num_commits, $version_pref{"version_major"},
554 $version_pref{"version_minor"}, $version_pref{"version_micro"},
556 &update_cmakelists_txt;
558 &update_attributes_asciidoc;
559 &update_docinfo_asciidoc;
560 &update_debian_changelog;
561 &update_cmake_lib_releases;
568 if (!$enable_vcsversion) {
569 return "/* #undef VCSVERSION */\n";
572 if ($git_description) {
573 # Do not bother adding the git branch, the git describe output
574 # normally contains the base tag and commit ID which is more
575 # than sufficient to determine the actual source tree.
576 return "#define VCSVERSION \"$git_description\"\n";
579 if ($last_change && $num_commits) {
580 $line = sprintf("v%d.%d.%d",
581 $version_pref{"version_major"},
582 $version_pref{"version_minor"},
583 $version_pref{"version_micro"},
585 return "#define VCSVERSION \"$line-$vcs_name-$num_commits\"\n";
588 return "#define VCSVERSION \"$vcs_name Rev Unknown from unknown\"\n";
591 # Print the version control system's version to $version_file.
592 # Don't change the file if it is not needed.
593 sub print_VCS_REVISION
596 my $needs_update = 1;
598 $VCS_REVISION = new_version_h();
599 if (open(OLDREV, "<$version_file")) {
600 my $old_VCS_REVISION = <OLDREV>;
601 if ($old_VCS_REVISION eq $VCS_REVISION) {
607 if (! $set_vcs) { return; }
610 # print "Updating $version_file so it contains:\n$VCS_REVISION";
611 open(VER, ">$version_file") || die ("Cannot write to $version_file ($!)\n");
612 print VER "$VCS_REVISION";
614 print "$version_file has been updated.\n";
615 } elsif (!$enable_vcsversion) {
616 print "$version_file disabled.\n";
618 print "$version_file unchanged.\n";
622 # Read values from the configuration file, if it exists.
627 # Get our command-line args
628 # XXX - Do we need an option to undo --set-release?
630 "help|h", \$show_help,
631 "get-vcs|get-svn|g", \$get_vcs,
632 "set-vcs|set-svn|s", \$set_vcs,
633 "print-vcs", \$print_vcs,
634 "set-version|v", \$set_version,
635 "set-release|r|package-version|p", \$set_release,
639 if ($show_help) { pod2usage(1); }
641 if ( !( $show_help || $get_vcs || $set_vcs || $print_vcs || $set_version || $set_release ) ) {
649 if (! open(FILE, "<$vconf_file")) {
650 if (! open(FILE, "<$srcdir/$vconf_file")) {
651 print_diag "Version configuration file $vconf_file not "
652 . "found. Using defaults.\n";
658 s/^\s+|\s+$//g; # chomp() may not handle CR
660 next unless (/^(\w+)(:|=)\s*(\S.*)/);
661 $version_pref{$1} = $3;
677 if ($set_version || $set_release) {
679 print "Generating version information.\n";
682 if ($version_pref{"enable"} == 0) {
683 print "Release information disabled in $vconf_file.\n";
688 print "Generating release information.\n";
690 print "Resetting release information\n";
692 $package_string = "";
695 &update_versioned_files;
702 make-version.pl - Get and set build-time version information for Wireshark
706 make-version.pl [options] [source directory]
710 --help, -h This help message
711 --get-vcs, -g Print the VCS revision and source.
712 --set-vcs, -s Set the information in version.h
713 --print-vcs Print the vcs version to standard output
714 --set-version, -v Set the major, minor, and micro versions in
715 the top-level CMakeLists.txt, configure.ac,
716 docbook/attributes.asciidoc, debian/changelog,
717 and the CMakeLists.txt for all libraries.
718 Resets the release information when used by
720 --set-release, -r Set the release information in the top-level
721 CMakeLists.txt, configure.ac
722 --package-version, -p Deprecated. Same as --set-release.
723 --verbose Print diagnostic messages to STDERR.
725 Options can be used in any combination. If none are specified B<--set-vcs>
731 # Editor modelines - http://www.wireshark.org/tools/modelines.html
736 # indent-tabs-mode: t
739 # vi: set shiftwidth=8 tabstop=8 noexpandtab:
740 # :indentSize=8:tabSize=8:noTabs=false: