3 # Copyright 2004 Jörg Mayer (see AUTHORS file)
7 # Wireshark - Network traffic analyzer
8 # By Gerald Combs <gerald@wireshark.org>
9 # Copyright 1998 Gerald Combs
11 # This program is free software; you can redistribute it and/or
12 # modify it under the terms of the GNU General Public License
13 # as published by the Free Software Foundation; either version 2
14 # of the License, or (at your option) any later version.
16 # This program is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 # GNU General Public License for more details.
21 # You should have received a copy of the GNU General Public License
22 # along with this program; if not, write to the Free Software
23 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 # usage: ./make-version.pl [-p|--package-version]
27 # If "version.conf" is present, it is parsed for configuration values.
28 # Possible values are:
30 # enable - Enable or disable versioning. Zero (0) disables, nonzero
32 # svn_client - Use svn client i.s.o. ugly internal SVN file hack
33 # format - A strftime() formatted string to use as a template for
34 # the version string. The sequence "%#" will substitute
35 # the SVN revision number.
36 # pkg_enable - Enable or disable package versioning.
37 # pkg_format - Like "format", but used for the package version.
39 # If run with the "-p" or "--package-version" argument, the
40 # AC_INIT macro in configure.in and the VERSION macro in
41 # config.nmake will have the pkg_format template appended to the
42 # version number. svnversion.h will _not_ be generated if either
43 # argument is present.
45 # Default configuration:
49 # format: SVN %Y%m%d%H%M%S
53 # XXX - We're pretty dumb about the "%#" substitution, and about having
54 # spaces in the package format.
59 use POSIX qw(strftime);
62 my $version_file = 'svnversion.h';
63 my $package_string = "";
64 my $vconf_file = 'version.conf';
67 my $repo_path = "unknown";
72 "format" => "SVN %Y%m%d%H%M%S",
74 # Normal development builds
76 "pkg_format" => "-SVN-%#",
78 # Development releases
84 $ENV{LANG} = "C"; # Ensure we run with correct locale
86 # Run "svn info". Parse out the most recent modification time and the
90 my $version_format = $version_pref{"format"};
91 my $package_format = "";
95 my $repo_root = undef;
99 if ($version_pref{"pkg_enable"}) {
100 $package_format = $version_pref{"pkg_format"};
103 if ($version_pref{"svn_client"}) {
107 $line = qx{svn info $srcdir};
108 if (defined($line)) {
109 if ($line =~ /Last Changed Date: (\d{4})-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d)/) {
110 $last_change = timegm($6, $5, $4, $3, $2 - 1, $1);
112 if ($line =~ /Last Changed Rev: (\d+)/) {
115 if ($line =~ /URL: (\S+)/) {
118 if ($line =~ /Repository Root: (\S+)/) {
125 if ($last_change && $revision && $repo_url && $repo_root) {
130 # 'svn info' failed or the user really wants us to dig around in .svn/entries
132 # Start of ugly internal SVN file hack
133 if (! open (ENTRIES, "< $srcdir/.svn/entries")) {
134 print ("Unable to open $srcdir/.svn/entries\n");
136 # We need to find out whether our parser can handle the entries file
139 if ($line eq '<?xml version="1.0" encoding="utf-8"?>') {
140 $repo_version = "pre1.4";
141 } elsif ($line =~ /^8$/) {
142 $repo_version = "1.4";
144 $repo_version = "unknown";
147 if ($repo_version eq "pre1.4") {
148 # The entries schema is flat, so we can use regexes to parse its contents.
149 while ($line = <ENTRIES>) {
150 if ($line =~ /<entry$/ || $line =~ /<entry\s/) {
155 if ($line =~ /name="(.*)"/) { $svn_name = $1; }
156 if ($line =~ /committed-date="(\d{4})-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)/) {
157 $last_change = timegm($6, $5, $4, $3, $2 - 1, $1);
159 if ($line =~ /revision="(\d+)"/) { $revision = $1; }
161 if ($line =~ /\/>/) {
162 if (($svn_name eq "" || $svn_name eq "svn:this_dir") &&
163 $last_change && $revision) {
168 # XXX - Fetch the repository root & URL
175 # If we picked up the revision and modification time,
176 # generate our strings.
177 if ($revision && $last_change) {
178 $version_format =~ s/%#/$revision/;
179 $package_format =~ s/%#/$revision/;
180 $package_string = strftime($package_format, gmtime($last_change));
183 if ($repo_url && $repo_root && index($repo_url, $repo_root) == 0) {
184 $repo_path = substr($repo_url, length($repo_root));
189 # Read configure.in, then write it back out with an updated
191 sub update_configure_in
197 return if ($package_string eq "");
199 open(CFGIN, "< configure.in") || die "Can't read configure.in!";
200 while ($line = <CFGIN>) {
201 if ($line =~ /^AC_INIT\(wireshark, (\d+)\.(\d+).(\d+)/) {
202 $line = "AC_INIT\(wireshark, $1.$2.$3$package_string)\n";
207 open(CFGIN, "> configure.in") || die "Can't write configure.in!";
208 print(CFGIN $contents);
210 print "configure.in has been updated.\n";
213 # Read config.nmake, then write it back out with an updated
215 sub update_config_nmake
222 if ($package_string ne "") { $update_ve = 1; };
224 open(CFGIN, "< config.nmake") || die "Can't read config.nmake!";
225 while ($line = <CFGIN>) {
226 if ($update_ve && $line =~ /^VERSION_EXTRA=/) {
227 $line = "VERSION_EXTRA=$package_string\n";
229 if ($line =~ /^VERSION_BUILD=/ && int($revision) > 0) {
230 $line = "VERSION_BUILD=$revision\n";
235 open(CFGIN, "> config.nmake") || die "Can't write config.nmake!";
236 print(CFGIN $contents);
238 print "config.nmake has been updated.\n";
243 # Print the SVN version to $version_file.
244 # Don't change the file if it is not needed.
245 sub print_svn_version
248 my $needs_update = 1;
250 if ($pkg_version) { return; }
252 if ($last_change && $revision) {
253 $svn_version = "#define SVNVERSION \"SVN Rev " .
255 "#define SVNPATH \"" . $repo_path . "\"\n";
257 $svn_version = "/* #define SVNVERSION \"\" */\n" .
258 "/* #define SVNPATH \"\" */\n";
260 if (open(OLDVER, "<$version_file")) {
261 my $old_svn_version = <OLDVER> . <OLDVER>;
262 if ($old_svn_version eq $svn_version) {
268 if ($needs_update == 1) {
269 # print "Updating $version_file so it contains:\n$svn_version";
270 open(VER, ">$version_file") || die ("Cannot write to $version_file ($!)\n");
271 print VER "$svn_version";
273 print "$version_file has been updated.\n";
275 print "$version_file is up-to-date.\n";
279 # Read values from the configuration file, if it exists.
283 # Get our command-line args
284 GetOptions("package-version", \$pkg_version);
291 if (! open(FILE, "<$vconf_file")) {
292 print STDERR "Version configuration file $vconf_file not "
293 . "found. Using defaults.\n";
300 next unless (/^(\w+)(:|=)\s*(\S.*)/);
301 $version_pref{$1} = $3;
313 if (-d "$srcdir/.svn") {
314 print "This is a build from SVN (or a SVN snapshot).\n";
317 print "Generating package version. Ignoring $version_file\n";
318 &update_configure_in;
319 &update_config_nmake;
320 } elsif ($version_pref{"enable"} == 0) {
321 print "Version tag disabled in $vconf_file.\n";
325 print "SVN version tag will be computed.\n";
328 print "This is not a SVN build.\n";