deprecate-prototypes.pl [plain text]
use strict;
my ($dir) = @ARGV;
if ($dir eq "clean") {
exit 0;
}
if (! -d $dir || ! -d "$dir/openssl") {
print STDERR "$dir is invalid; run with one argument, a directory to process\n";
exit 1;
}
chdir("$dir/openssl");
my @files = `find . -name '*.h' -type f`;
chomp @files;
my $hash = "#";
my $leftp = "(";
my $rightp = ")";
sub fixProto($) {
my ($proto) = @_;
if (($proto =~ m|typedef|si) ||
($proto =~ m|OPENSSL_DECLARE_GLOBAL|si)) {
return $proto;
}
$proto =~ s|\Q${rightp}\E\s*;|${rightp} DEPRECATED_IN_MAC_OS_X_VERSION_10_7_AND_LATER;|sg;
return $proto;
}
sub readFile($) {
my ($file) = @_;
open my $f, "< $file";
my $data = "";
my @lines;
while (my $l = <$f>) {
$data .= $l;
push @lines, $l;
}
return ($data, @lines);
}
sub countBalancedParens($) {
my ($str) = @_;
$str =~ s|/\*.*?\*/||sg;
my @left = ($str =~ m|(\Q$leftp\E)|sg);
my @right = ($str =~ m|(\Q$rightp\E)|sg);
if ($#left != $#right) {
return -1; }
return $#left + 1;
}
foreach my $file (sort(@files)) {
my ($data, @lines) = readFile($file);
my $newdata = "";
my $cur = "";
foreach my $line (@lines) {
$cur .= $line;
if (($cur =~ m|/\*|s) &&
($cur !~ m|\*/|s)) {
} elsif ($cur =~ m|\\\n$|) {
} elsif ($cur =~ m|^\s*${hash}|s) {
$newdata .= $cur;
$cur = "";
} else {
my $parenCount = countBalancedParens($cur);
if ($parenCount == -1) {
} else {
if ($parenCount > 0) {
$cur = fixProto($cur);
}
$newdata .= $cur;
$cur = "";
}
}
}
$newdata .= $cur;
if ($data ne $newdata) {
$newdata =~ s|(${hash}ifndef\s+(\w+_H)\s*\n${hash}define\s+\g2[ \t]*\n)|"$1\n#include <AvailabilityMacros.h>\n"|es;
open my $f, "> $file";
print $f $newdata;
close $f;
}
}
open my $f, "> /tmp/test.$$.c";
print $f "#include <openssl/ssl.h>\n";
foreach my $file (@files) {
$file =~ s|^\./||;
print $f "#include <openssl/$file>\n";
}
close $f;
my @cmd = ("clang", "-I$dir", "-c", "-o", "/tmp/test.$$.o", "/tmp/test.$$.c");
system(@cmd);
my $ret = 0;
if (! -e "/tmp/test.$$.o") {
print STDERR "There was an error including the deprecated headers\n";
$ret = 1;
}
unlink("/tmp/test.$$.c", "/tmp/test.$$.o");
exit $ret;