Add dissectors/packet-lcsap.c to CMake. Remove dissect_lcsap_Correlation_ID_PDU...
[obnox/wireshark/wip.git] / make-version.pl
1 #!/usr/bin/perl -w
2 #
3 # Copyright 2004 Jörg Mayer (see AUTHORS file)
4 #
5 # $Id$
6 #
7 # Wireshark - Network traffic analyzer
8 # By Gerald Combs <gerald@wireshark.org>
9 # Copyright 1998 Gerald Combs
10 #
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.
15 #
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.
20 #
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.
24
25 # usage:  ./make-version.pl [-p|--package-version]
26 #
27 # If "version.conf" is present, it is parsed for configuration values.  
28 # Possible values are:
29 #
30 #   enable     - Enable or disable versioning.  Zero (0) disables, nonzero
31 #                enables.
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.
38 #   is_release - Specifies that we're building from a release tarball;
39 #                svnversion.h is not updated.  This should be added only
40 #                to the *released* version.conf, not the one used to build
41 #                the release (IOW it should be added by automake's dist-hook).
42 #
43 # If run with the "-p" or "--package-version" argument, the
44 # AC_INIT macro in configure.in and the VERSION macro in
45 # config.nmake will have the pkg_format template appended to the 
46 # version number.  svnversion.h will _not_ be generated if either
47 # argument is present (it will also not be generated if 'is_release' is set
48 # in version.conf).
49 #
50 # Default configuration:
51 #
52 # enable: 1
53 # svn_client: 1
54 # format: SVN %Y%m%d%H%M%S
55 # pkg_enable: 1
56 # pkg_format: -SVN-%#
57
58 # XXX - We're pretty dumb about the "%#" substitution, and about having
59 # spaces in the package format.
60
61 use strict;
62
63 use Time::Local;
64 use POSIX qw(strftime);
65 use Getopt::Long;
66
67 my $version_file = 'svnversion.h';
68 my $package_string = "";
69 my $vconf_file = 'version.conf';
70 my $last_change = 0;
71 my $revision = 0;
72 my $repo_path = "unknown";
73 my $pkg_version = 0;
74 my %version_pref = (
75         "enable"     => 1,
76         "svn_client" => 1,
77         "format"     => "SVN %Y%m%d%H%M%S",
78         "is_release" => 0,
79
80         # Normal development builds
81         "pkg_enable" => 1,
82         "pkg_format" => "-SVN-%#",
83
84         # Development releases
85         #"pkg_enable" => 0,
86         #"pkg_format" => "",
87         );
88 my $srcdir = ".";
89 my $svn_info_cmd = "";
90
91 $ENV{LANG} = "C";  # Ensure we run with correct locale
92
93 # Run "svn info".  Parse out the most recent modification time and the
94 # revision number.
95 sub read_svn_info {
96         my $line;
97         my $version_format = $version_pref{"format"};
98         my $package_format = "";
99         my $in_entries = 0;
100         my $svn_name;
101         my $repo_version;
102         my $repo_root = undef;
103         my $repo_url = undef;
104         my $do_hack = 1;
105
106         if ($version_pref{"pkg_enable"}) {
107                 $package_format = $version_pref{"pkg_format"};
108         }
109
110         if ($version_pref{"svn_client"}) {
111                 eval {
112                         use warnings "all";
113                         no warnings "all";
114                         $line = qx{$svn_info_cmd};
115                         if (defined($line)) {
116                                 if ($line =~ /Last Changed Date: (\d{4})-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d)/) {
117                                         $last_change = timegm($6, $5, $4, $3, $2 - 1, $1);
118                                 }
119                                 if ($line =~ /Last Changed Rev: (\d+)/) {
120                                         $revision = $1;
121                                 }
122                                 if ($line =~ /URL: (\S+)/) {
123                                         $repo_url = $1;
124                                 }
125                                 if ($line =~ /Repository Root: (\S+)/) {
126                                         $repo_root = $1;
127                                 }
128                         }
129                         1;
130                 };
131
132                 if ($last_change && $revision && $repo_url && $repo_root) {
133                         $do_hack = 0;
134                 }
135         }
136
137         # 'svn info' failed or the user really wants us to dig around in .svn/entries
138         if ($do_hack) {
139                 # Start of ugly internal SVN file hack
140                 if (! open (ENTRIES, "< $srcdir/.svn/entries")) {
141                         print ("Unable to open $srcdir/.svn/entries\n");
142                 } else {
143                         # We need to find out whether our parser can handle the entries file
144                         $line = <ENTRIES>;
145                         chomp $line;
146                         if ($line eq '<?xml version="1.0" encoding="utf-8"?>') {
147                                 $repo_version = "pre1.4";
148                         } elsif ($line =~ /^8$/) {
149                                 $repo_version = "1.4";
150                         } else {
151                                 $repo_version = "unknown";
152                         }
153
154                         if ($repo_version eq "pre1.4") {
155                                 # The entries schema is flat, so we can use regexes to parse its contents.
156                                 while ($line = <ENTRIES>) {
157                                         if ($line =~ /<entry$/ || $line =~ /<entry\s/) {
158                                                 $in_entries = 1;
159                                                 $svn_name = "";
160                                         }
161                                         if ($in_entries) {
162                                                 if ($line =~ /name="(.*)"/) { $svn_name = $1; }
163                                                 if ($line =~ /committed-date="(\d{4})-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)/) {
164                                                         $last_change = timegm($6, $5, $4, $3, $2 - 1, $1);
165                                                 }
166                                                 if ($line =~ /revision="(\d+)"/) { $revision = $1; }
167                                         }
168                                         if ($line =~ /\/>/) {
169                                                 if (($svn_name eq "" || $svn_name eq "svn:this_dir") &&
170                                                                 $last_change && $revision) {
171                                                         $in_entries = 0;
172                                                         last;
173                                                 }
174                                         }
175                                         # XXX - Fetch the repository root & URL
176                                 }
177                         }
178                         close ENTRIES;
179                 }
180         }
181
182         # If we picked up the revision and modification time, 
183         # generate our strings.
184         if ($revision && $last_change) {
185                 $version_format =~ s/%#/$revision/;
186                 $package_format =~ s/%#/$revision/;
187                 $package_string = strftime($package_format, gmtime($last_change));
188         }
189         
190         if ($repo_url && $repo_root && index($repo_url, $repo_root) == 0) {
191                 $repo_path = substr($repo_url, length($repo_root));
192         }
193 }
194
195
196 # Read configure.in, then write it back out with an updated 
197 # "AC_INIT" line.
198 sub update_configure_in
199 {
200         my $line;
201         my $contents = "";
202         my $version = "";
203         
204         return if ($package_string eq "");
205         
206         open(CFGIN, "< configure.in") || die "Can't read configure.in!";
207         while ($line = <CFGIN>) {
208                 if ($line =~ /^AC_INIT\(wireshark, (\d+)\.(\d+).(\d+)/) {
209                         $line = "AC_INIT\(wireshark, $1.$2.$3$package_string)\n";
210                 }
211                 $contents .= $line
212         }
213         
214         open(CFGIN, "> configure.in") || die "Can't write configure.in!";
215         print(CFGIN $contents);
216         close(CFGIN);
217         print "configure.in has been updated.\n";
218 }
219
220 # Read config.nmake, then write it back out with an updated 
221 # "VERSION" line.
222 sub update_config_nmake
223 {
224         my $line;
225         my $contents = "";
226         my $version = "";
227         my $update_ve = 0;
228         
229         if ($package_string ne "") { $update_ve = 1; };
230         
231         open(CFGIN, "< config.nmake") || die "Can't read config.nmake!";
232         while ($line = <CFGIN>) {
233                 if ($update_ve && $line =~ /^VERSION_EXTRA=/) {
234                         $line = "VERSION_EXTRA=$package_string\n";
235                 }
236                 if ($line =~ /^VERSION_BUILD=/ && int($revision) > 0) {
237                         $line = "VERSION_BUILD=$revision\n";
238                 }
239                 $contents .= $line
240         }
241         
242         open(CFGIN, "> config.nmake") || die "Can't write config.nmake!";
243         print(CFGIN $contents);
244         close(CFGIN);
245         print "config.nmake has been updated.\n";
246 }
247
248
249
250 # Print the SVN version to $version_file.
251 # Don't change the file if it is not needed.
252 sub print_svn_version
253 {
254         my $svn_version;
255         my $needs_update = 1;
256
257         if ($pkg_version || $version_pref{"is_release"} == 1) { return; }
258
259         if ($last_change && $revision) {
260                 $svn_version = "#define SVNVERSION \"SVN Rev " . 
261                         $revision . "\"\n" .
262                         "#define SVNPATH \"" . $repo_path . "\"\n";
263         } else {
264                 $svn_version = "#define SVNVERSION \"SVN Rev Unknown\"\n" .
265                         "#define SVNPATH \"unknown\"\n";
266         }
267         if (open(OLDVER, "<$version_file")) {
268                 my $old_svn_version = <OLDVER> . <OLDVER>;
269                 if ($old_svn_version eq $svn_version) {
270                         $needs_update = 0;
271                 }
272                 close OLDVER;
273         }
274
275         if ($needs_update == 1) {
276                 # print "Updating $version_file so it contains:\n$svn_version";
277                 open(VER, ">$version_file") || die ("Cannot write to $version_file ($!)\n");
278                 print VER "$svn_version";
279                 close VER;
280                 print "$version_file has been updated.\n";
281         } else {
282                 print "$version_file is up-to-date.\n";
283         }
284 }
285
286 # Read values from the configuration file, if it exists.
287 sub get_config {
288         my $arg;
289
290         # Get our command-line args
291         GetOptions("package-version", \$pkg_version);
292
293         if ($#ARGV >= 0) {
294                 $srcdir = $ARGV[0]
295         }
296
297         if (! open(FILE, "<$vconf_file")) {
298                 print STDERR "Version configuration file $vconf_file not "
299                 . "found.  Using defaults.\n";
300                 return 1;
301         }
302
303         while (<FILE>) {
304                 chomp;
305                 next if (/^#/);
306                 next unless (/^(\w+)(:|=)\s*(\S.*)/);
307                 $version_pref{$1} = $3;
308         }
309         close FILE;
310         return 1;
311 }
312
313 ##
314 ## Start of code
315 ##
316
317 &get_config();
318
319 if (-d "$srcdir/.svn") {
320         $svn_info_cmd = "svn info $srcdir";
321 } elsif (-d "$srcdir/.git/svn") {
322         $svn_info_cmd = "(cd $srcdir; git svn info)";
323 }
324
325 if ($svn_info_cmd) {
326         print "This is a build from SVN (or a SVN snapshot).\n";
327         &read_svn_info();
328         if ($pkg_version) {
329                 print "Generating package version.  Ignoring $version_file\n";
330                 &update_configure_in;
331                 &update_config_nmake;
332         } elsif ($version_pref{"enable"} == 0) {
333                 print "Version tag disabled in $vconf_file.\n";
334                 $last_change = 0;
335                 $revision = 0;
336         } else {
337                 print "SVN version tag will be computed.\n";
338         }
339 } else {
340         print "This is not a SVN build.\n";
341 }
342
343 &print_svn_version;
344
345 __END__