1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
|
#!/usr/bin/perl
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# -----------------------------------------------------------------------------
# Compare the native API with native methods in the Java API.
# -----------------------------------------------------------------------------
# -----------------------------------------------------------------------------
# TODO:
# - use method name plus return type plus args as key instead of
# only method name
# -----------------------------------------------------------------------------
#
use strict;
# Modules needed, should all be standard
# Commandline option parsing
use Getopt::Std;
# dirname function
use File::Basename;
# Traverse through directory structure and execute
# callback for each file
use File::Find;
# Platform agnostic concatenation of directory and file names
use File::Spec::Functions;
################### BEGIN VARIABLES WHICH MUST BE MAINTAINED #####################
# Script version, printed via getopts with "--version"
our $VERSION = '1.0';
# Sub directories containing c native API and Java classes.
# Path relative to tcnative main project directory.
my $C_NATIVE_SUBDIR = 'native';
my $JAVA_API_SUBDIR = 'java';
# Macro used for marking C native API
my $C_NATIVE_DECLARATION = 'TCN_IMPLEMENT_CALL';
# Macro used for marking C native API arguments
my $C_NATIVE_ARGUMENTS = 'TCN_STDARGS';
################### END VARIABLES WHICH MUST BE MAINTAINED #####################
# Global data variables
# Print verbose output?
my $verbose = 0;
# Signature structures
my %cSignature;
my %javaSignature;
# Usage/Help
sub HELP_MESSAGE {
my $fh = shift;
print $fh "Usage:: $0 [ -h ] [ -v ] [ -d DIRECTORY ]\n";
print $fh " -h: Print help\n";
print $fh " -v: Verbose output\n";
print $fh " -d DIRECTORY: Path to tcnative project main directory.\n";
print $fh " Default is '..\\..' relative to the script directory \n";
}
# Parse arguments:
# -h: help
# -v: verbose
# -d: tcnative project main directory
$Getopt::Std::STANDARD_HELP_VERSION = 1;
our ($opt_h, $opt_v, $opt_d);
getopts('hvd:');
if ($opt_h) {
HELP_MESSAGE(*STDOUT);
exit 1;
}
if ($opt_v) {
$verbose = 1;
}
my $directory = $opt_d;
if (!defined($directory) || $directory eq '') {
$directory = dirname($0);
$directory = catfile($directory, ('..', '..'));
}
if (! -d $directory) {
print STDERR "Invalid tcnative project directory '$directory' - Aborting!\n";
exit 1;
}
sub mapTypes {
my $types = shift;
# Replace "Array" by "[]"
$types =~ s/Array/\\[\\]/g;
# Remove leading "j" from scalar types
$types =~ s/j(byte|short|int|long|boolean)/$1/g;
# Replace string type
$types =~ s/jstring/String/g;
# Replace object type with pattern
$types =~ s/jobject/([A-Z][A-Za-z0-9]*|[a-z]+\\[\\])/g;
return $types;
}
# Extract C native API from one file
sub cNativeApi {
# Only check C source files
return if $_ !~ /\.c$/;
my $file = $_;
my ($m, $signature, $type, $args, $ret, $class, $method);
print "Checking C API file $file\n" if $verbose;
open(IN, "<$file") or do {
print "ERROR: Ignoring file '$file', could not open file for read: $!\n";
return;
};
while(<IN>) {
# Example declaration:
#TCN_IMPLEMENT_CALL(jlong, Socket, create)(TCN_STDARGS, jint family,
# jint type, jint protocol,
# jlong pool)
if ($_ =~ /^\s*$C_NATIVE_DECLARATION\s*/o) {
chomp();
$signature = $_;
# Concat next line until signature is complete
while($signature !~ /\([^\)]*\)\s*\([^\)]*\)/ && $signature !~ /;/ && ($_ = <IN>)) {
chomp();
$signature .= $_;
}
# Strip C-style comments. See: "perldoc -q comment"
$signature =~ s#/\*[^*]*\*+([^/*][^*]*\*+)*/|("(\\.|[^"\\])*"|'(\\.|[^'\\])*'|.[^/"'\\]*)#defined $2 ? $2 : ""#gse;
# Save one declaration
if ($signature =~ /^\s*$C_NATIVE_DECLARATION\s*\(([^\)]*)\)\s*\(([^\)]*)\)/) {
($type, $args) = ($1, $2);
# Normalize return type and method name
# Collapse multiple spaces
$type =~ s/\s+/ /g;
# Remove spaces around commas
$type =~ s/, /,/g;
$type =~ s/ ,/,/g;
# Trim spaces at start and end
$type =~ s/^ //;
$type =~ s/ $//;
($ret, $class, $method) = split(/,/, $type);
# Map return type
$ret = mapTypes($ret);
# Normalize argument list
# Collapse multiple spaces
$args =~ s/\s+/ /g;
# Remove spaces around commas
$args =~ s/, /,/g;
$args =~ s/ ,/,/g;
# Remove argument names, only types are needed
$args =~ s/ [^, ]+//g;
# Trim spaces at start and end
$args =~ s/^ //;
$args =~ s/ $//;
# Remove leading JNI arguments macro
$args =~ s/^$C_NATIVE_ARGUMENTS,?//o;
# Map argument list types
$args = mapTypes($args);
$m = $cSignature{$class};
if (!defined($m)) {
$m = $cSignature{$class} = {};
}
if (exists($m->{$method}) && ($m->{$method}->{return} ne $ret || $m->{$method}->{args} ne $args)) {
print "ERROR: C native file $file method $method in class $class will be overwritten!";
print "\tOld signature: '$m->{$method}->{return} $m->{$method}->{method}($m->{$method}->{args})\n";
print "\tNew signature: '$ret $method($args)\n";
}
# XXX Use method plus ret plus args as key instead
$m = $m->{$method} = {};
$m->{method} = $method;
$m->{return} = $ret;
$m->{args} = $args;
print "\tFound C API in $file class $class: '$m->{return} $m->{method}($m->{args})'\n" if $verbose;
} else {
print "ERROR: Incomplete C signature in file $file ignored in '$signature'";
}
}
}
close(IN);
}
# Extract native methods in Java API from one file
sub javaNativeApi {
# Only check Java source files
return if $_ !~ /\.java$/;
my $file = $_;
my ($m, $signature, $type, $args, $ret, $class, $method);
print "Checking Java API file $file\n" if $verbose;
open(IN, "<$_") or do {
print "ERROR: Ignoring file '$file', could not open file for read: $!\n";
return;
};
$class = $file;
$class =~ s/\.java$//;
while(<IN>) {
# Example declaration
#public static native long uid(String username, long p)
# throws Error;
if ($_ =~ /^\s*((public|protected|private)\s+)?([^\(]*)\(/ && ($type = $3) && $type =~ /\snative\s/) {
chomp();
$signature = $_;
# Concat next line until signature is complete
while($signature !~ /\([^\)]*\)/ && $signature !~ /;/ && ($_ = <IN>)) {
chomp();
$signature .= $_;
}
# Save one declaration
if ($signature =~ /^\s*([^\(]+)\(([^\)]*)\)/) {
($type, $args) = ($1, $2);
# Normalize return type and method name
# Remove unused specifiers
$type =~ s/\b(public|protected|private)\b//g;
$type =~ s/\bnative\b//g;
$type =~ s/\bstatic\b//g;
# Collapse multiple spaces
$type =~ s/\s+/ /g;
# Trim spaces at start and end
$type =~ s/^ //;
$type =~ s/ $//;
($ret, $method) = split(/\s+/, $type);
# Normalize argument list
# Collapse multiple spaces
$args =~ s/\s+/ /g;
# Remove spaces around commas
$args =~ s/, /,/g;
$args =~ s/ ,/,/g;
# Remove spaces in front of array "[]"
$args =~ s/ \[\]/[]/g;
# Remove argument names, only types are needed
$args =~ s/ [^, ]+//g;
# Trim spaces at start and end
$args =~ s/^ //;
$args =~ s/ $//;
$m = $javaSignature{$class};
if (!defined($m)) {
$m = $javaSignature{$class} = {};
}
if (exists($m->{$method})) {
print "ERROR: Java file $file method $method in class $class will be overwritten!";
print "\tOld signature: '$m->{$method}->{return} $m->{$method}->{method}($m->{$method}->{args})\n";
print "\tNew signature: '$ret $method($args)\n";
}
# XXX Use method plus ret plus args as key instead
$m = $m->{$method} = {};
$m->{method} = $method;
$m->{return} = $ret;
$m->{args} = $args;
print "\tFound native Java API in $file class $class: '$m->{return} $m->{method}($m->{args})'\n" if $verbose;
} else {
print "ERROR: Incomplete Java signature in file $file ignored in '$signature'";
}
}
}
close(IN);
}
# Search for C native API
find(\&cNativeApi, catfile($directory, $C_NATIVE_SUBDIR));
# Search for native methods in Java API
find(\&javaNativeApi, catfile($directory, $JAVA_API_SUBDIR));
print "Comparing C and Java APIs...\n";
# Check whether all native methods have correct Java counterparts
for my $class (sort keys %cSignature) {
my $c = $cSignature{$class};
my $j = $javaSignature{$class};
if (!defined($j)) {
print "ERROR: No Java API defined for class '$class'\n";
for my $method (sort keys %{$c}) {
my $m = $c->{$method};
print "\tMissing Java method: $m->{return} $m->{method}($m->{args})\n";
}
} else {
for my $method (sort keys %{$c}) {
my $m = $c->{$method};
if (!exists($j->{$method})) {
print "ERROR: Missing Java method in class '$class': $m->{return} $m->{method}($m->{args})\n";
} else {
my $n = $j->{$method};
if ($n->{return} !~ /^$m->{return}$/ || $n->{args} !~ /^$m->{args}$/) {
print "ERROR: Incompatible API in class '$class'!\n";
print "\tC signature: $m->{return} $m->{method}($m->{args})\n";
print "\tJava signature: $n->{return} $n->{method}($n->{args})\n";
}
delete($j->{$method});
}
}
}
}
# Check whether any native Java API methods remain
for my $class (sort keys %javaSignature) {
my $j = $javaSignature{$class};
my $c = $cSignature{$class};
if (!defined($c)) {
print "ERROR: No C API defined for class '$class'!\n";
for my $method (sort keys %{$j}) {
my $m = $j->{$method};
print "\tMissing C method: $m->{return} $m->{method}($m->{args})\n";
}
} else {
for my $method (sort keys %{$j}) {
my $m = $j->{$method};
if (!exists($c->{$method})) {
print "ERROR: Missing C method in class '$class': $m->{return} $m->{method}($m->{args})\n";
} else {
my $n = $c->{$method};
if ($m->{return} !~ /^$n->{return}$/ || $m->{args} !~ /^$n->{args}$/) {
print "ERROR: Incompatible API in class '$class'!\n";
print "\tC signature: $n->{return} $n->{method}($n->{args})\n";
print "\tJava signature: $m->{return} $m->{method}($m->{args})\n";
}
delete($c->{$method});
}
}
}
}
print "Done.\n";
|