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/;
378 $package_format =~ s/%#/$num_commits-$commit_id/;
379 $package_string = strftime($package_format, gmtime($last_change));
384 Commit distance : $num_commits
385 Commit ID : $commit_id
386 Revision source : $info_source
387 Release stamp : $package_string
389 } elsif ($print_vcs) {
390 print new_version_h();
395 # Read CMakeLists.txt, then write it back out with updated "set(PROJECT_..._VERSION ...)
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
407 my $filepath = "$srcdir/CMakeLists.txt";
409 return if (!$set_version && $package_string eq "");
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);
427 open(CFGIN, "> $filepath") || die "Can't write $filepath!";
428 print(CFGIN $contents);
430 print "$filepath has been updated.\n";
433 # Read docbook/attributes.asciidoc, then write it back out with an updated
434 # wireshark-version replacement line.
435 sub update_attributes_asciidoc
440 my $filepath = "$srcdir/docbook/attributes.asciidoc";
442 open(ADOC_CONF, "< $filepath") || die "Can't read $filepath!";
443 while ($line = <ADOC_CONF>) {
444 # :wireshark-version: 2.3.1
446 if ($line =~ /^:wireshark-version:.*?([\r\n]+)$/) {
447 $line = sprintf(":wireshark-version: %d.%d.%d$1",
448 $version_pref{"version_major"},
449 $version_pref{"version_minor"},
450 $version_pref{"version_micro"},
456 open(ADOC_CONF, "> $filepath") || die "Can't write $filepath!";
457 print(ADOC_CONF $contents);
459 print "$filepath has been updated.\n";
462 sub update_docinfo_asciidoc
465 my @paths = ("$srcdir/docbook/developer-guide-docinfo.xml",
466 "$srcdir/docbook/user-guide-docinfo.xml");
468 foreach my $filepath (@paths) {
470 open(DOCINFO_XML, "< $filepath") || die "Can't read $filepath!";
471 while ($line = <DOCINFO_XML>) {
472 if ($line =~ /^<subtitle>For Wireshark \d.\d+<\/subtitle>([\r\n]+)$/) {
473 $line = sprintf("<subtitle>For Wireshark %d.%d</subtitle>$1",
474 $version_pref{"version_major"},
475 $version_pref{"version_minor"},
481 open(DOCINFO_XML, "> $filepath") || die "Can't write $filepath!";
482 print(DOCINFO_XML $contents);
484 print "$filepath has been updated.\n";
488 # Read debian/changelog, then write back out an updated version.
489 sub update_debian_changelog
494 my $filepath = "$srcdir/debian/changelog";
496 open(CHANGELOG, "< $filepath") || die "Can't read $filepath!";
497 while ($line = <CHANGELOG>) {
498 if ($set_version && CHANGELOG->input_line_number() == 1) {
499 $line =~ /^.*?([\r\n]+)$/;
500 $line = sprintf("wireshark (%d.%d.%d) unstable; urgency=low$1",
501 $version_pref{"version_major"},
502 $version_pref{"version_minor"},
503 $version_pref{"version_micro"},
509 open(CHANGELOG, "> $filepath") || die "Can't write $filepath!";
510 print(CHANGELOG $contents);
512 print "$filepath has been updated.\n";
515 # Read CMakeLists.txt for each library, then write back out an updated version.
516 sub update_cmake_lib_releases
524 for $filedir ("$srcdir/epan", "$srcdir/wiretap") { # "$srcdir/wsutil"
526 $filepath = $filedir . "/CMakeLists.txt";
527 open(CMAKELISTS_TXT, "< $filepath") || die "Can't read $filepath!";
528 while ($line = <CMAKELISTS_TXT>) {
529 # VERSION "0.0.0" SOVERSION 0
531 if ($line =~ /^(\s*VERSION\s+"\d+\.\d+\.)\d+(".*[\r\n]+)$/) {
532 $line = sprintf("$1%d$2", $version_pref{"version_micro"});
537 open(CMAKELISTS_TXT, "> $filepath") || die "Can't write $filepath!";
538 print(CMAKELISTS_TXT $contents);
539 close(CMAKELISTS_TXT);
540 print "$filepath has been updated.\n";
544 # Update distributed files that contain any version information
545 sub update_versioned_files
547 # Matches CMakeLists.txt
548 printf "GR: %d, MaV: %d, MiV: %d, PL: %d, EV: %s\n",
549 $num_commits, $version_pref{"version_major"},
550 $version_pref{"version_minor"}, $version_pref{"version_micro"},
552 &update_cmakelists_txt;
554 &update_attributes_asciidoc;
555 &update_docinfo_asciidoc;
556 &update_debian_changelog;
557 &update_cmake_lib_releases;
564 if (!$enable_vcsversion) {
565 return "/* #undef VCSVERSION */\n";
568 if ($git_description) {
569 # Do not bother adding the git branch, the git describe output
570 # normally contains the base tag and commit ID which is more
571 # than sufficient to determine the actual source tree.
572 return "#define VCSVERSION \"$git_description\"\n";
575 if ($last_change && $num_commits) {
576 $line = sprintf("v%d.%d.%d",
577 $version_pref{"version_major"},
578 $version_pref{"version_minor"},
579 $version_pref{"version_micro"},
581 return "#define VCSVERSION \"$line-$vcs_name-$num_commits\"\n";
584 return "#define VCSVERSION \"$vcs_name Rev Unknown from unknown\"\n";
587 # Print the version control system's version to $version_file.
588 # Don't change the file if it is not needed.
589 sub print_VCS_REVISION
592 my $needs_update = 1;
594 $VCS_REVISION = new_version_h();
595 if (open(OLDREV, "<$version_file")) {
596 my $old_VCS_REVISION = <OLDREV>;
597 if ($old_VCS_REVISION eq $VCS_REVISION) {
603 if (! $set_vcs) { return; }
606 # print "Updating $version_file so it contains:\n$VCS_REVISION";
607 open(VER, ">$version_file") || die ("Cannot write to $version_file ($!)\n");
608 print VER "$VCS_REVISION";
610 print "$version_file has been updated.\n";
611 } elsif (!$enable_vcsversion) {
612 print "$version_file disabled.\n";
614 print "$version_file unchanged.\n";
618 # Read values from the configuration file, if it exists.
623 # Get our command-line args
624 # XXX - Do we need an option to undo --set-release?
626 "help|h", \$show_help,
627 "get-vcs|get-svn|g", \$get_vcs,
628 "set-vcs|set-svn|s", \$set_vcs,
629 "print-vcs", \$print_vcs,
630 "set-version|v", \$set_version,
631 "set-release|r|package-version|p", \$set_release,
635 if ($show_help) { pod2usage(1); }
637 if ( !( $show_help || $get_vcs || $set_vcs || $print_vcs || $set_version || $set_release ) ) {
645 if (! open(FILE, "<$vconf_file")) {
646 if (! open(FILE, "<$srcdir/$vconf_file")) {
647 print_diag "Version configuration file $vconf_file not "
648 . "found. Using defaults.\n";
654 s/^\s+|\s+$//g; # chomp() may not handle CR
656 next unless (/^(\w+)(:|=)\s*(\S.*)/);
657 $version_pref{$1} = $3;
673 if ($set_version || $set_release) {
675 print "Generating version information.\n";
678 if ($version_pref{"enable"} == 0) {
679 print "Release information disabled in $vconf_file.\n";
684 print "Generating release information.\n";
686 print "Resetting release information\n";
688 $package_string = "";
691 &update_versioned_files;
698 make-version.pl - Get and set build-time version information for Wireshark
702 make-version.pl [options] [source directory]
706 --help, -h This help message
707 --get-vcs, -g Print the VCS revision and source.
708 --set-vcs, -s Set the information in version.h
709 --print-vcs Print the vcs version to standard output
710 --set-version, -v Set the major, minor, and micro versions in
711 the top-level CMakeLists.txt, configure.ac,
712 docbook/attributes.asciidoc, debian/changelog,
713 and the CMakeLists.txt for all libraries.
714 Resets the release information when used by
716 --set-release, -r Set the release information in the top-level
717 CMakeLists.txt, configure.ac
718 --package-version, -p Deprecated. Same as --set-release.
719 --verbose Print diagnostic messages to STDERR.
721 Options can be used in any combination. If none are specified B<--set-vcs>
727 # Editor modelines - http://www.wireshark.org/tools/modelines.html
732 # indent-tabs-mode: t
735 # vi: set shiftwidth=8 tabstop=8 noexpandtab:
736 # :indentSize=8:tabSize=8:noTabs=false: