fixqt4headers.pl: Add --strip-module option.
- Add --strip-module option to strip the module name for portable code - Use GetOpt::Long - Streamline, remove need for rewinding the file - Streamline output, just report affected classes and introduce --verbose for normal operation. Change-Id: I8643a5f53d4ed8ce2ae44721164cbdba1aad8b46 Reviewed-by: Harald Fernengel <harald.fernengel@nokia.com>
This commit is contained in:
parent
52ef2ee57f
commit
bb4dbafbb1
@ -44,39 +44,39 @@
|
|||||||
use Cwd;
|
use Cwd;
|
||||||
use File::Find;
|
use File::Find;
|
||||||
use File::Spec;
|
use File::Spec;
|
||||||
|
use IO::File;
|
||||||
|
use Getopt::Long;
|
||||||
use strict;
|
use strict;
|
||||||
use warnings;
|
use warnings;
|
||||||
|
|
||||||
my $dry_run = 0;
|
my $dry_run = 0;
|
||||||
|
my $help = 0;
|
||||||
|
my $stripModule = 0;
|
||||||
my $fixedFileCount = 0;
|
my $fixedFileCount = 0;
|
||||||
|
my $fileCount = 0;
|
||||||
|
my $verbose = 0;
|
||||||
|
my $qtdir = $ENV{'QTDIR'};
|
||||||
|
|
||||||
sub show_help
|
my $USAGE=<<EOF;
|
||||||
{
|
This script replaces all Qt 4 style includes with Qt 5 includes.
|
||||||
print "This script replaces all Qt 4 style includes with Qt 5 includes\n";
|
|
||||||
print "\n";
|
|
||||||
print "Usage: $0 [--dry-run]\n";
|
|
||||||
print "\n";
|
|
||||||
print " --dry-run : Do not replace anything, just print what would be replaced\n";
|
|
||||||
print "\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($#ARGV >= 0) {
|
Usage: $0 [options]
|
||||||
if ($#ARGV >= 1) {
|
|
||||||
die "$0: Takes only one or zero arguments\n";
|
Options:
|
||||||
} elsif ($ARGV[0] eq "--dry-run") {
|
--dry-run : Do not replace anything, just print what would be replaced
|
||||||
$dry_run = 1;
|
--strip-modules : Strip the module headers for writing portable code
|
||||||
} elsif ($ARGV[0] eq "--help") {
|
--verbose : Verbose
|
||||||
show_help();
|
--qtdir <directory> : Point to Qt 5's qtbase directory
|
||||||
exit 0
|
EOF
|
||||||
} else {
|
|
||||||
show_help();
|
if (!GetOptions('dry-run' => \$dry_run, 'help' => \$help,
|
||||||
print "Unknown argument: $ARGV[0]\n";
|
'strip-modules' => \$stripModule, 'verbose' => \$verbose, 'qtdir:s' => \$qtdir)
|
||||||
exit 1;
|
|| $help) {
|
||||||
}
|
print $USAGE;
|
||||||
|
exit (1);
|
||||||
}
|
}
|
||||||
|
|
||||||
my %headerSubst = ();
|
my %headerSubst = ();
|
||||||
my $qtdir;
|
|
||||||
my $cwd = getcwd();
|
my $cwd = getcwd();
|
||||||
|
|
||||||
sub fixHeaders
|
sub fixHeaders
|
||||||
@ -85,68 +85,39 @@ sub fixHeaders
|
|||||||
my $relFileName = File::Spec->abs2rel($fileName, $cwd);
|
my $relFileName = File::Spec->abs2rel($fileName, $cwd);
|
||||||
|
|
||||||
# only check sources, also ignore symbolic links and directories
|
# only check sources, also ignore symbolic links and directories
|
||||||
if ($fileName !~ /(\.h|\.cpp|\/C|\.cc|\.CC)$/ || ! -f $fileName) {
|
return unless -f $fileName && $fileName =~ /(\.h|\.cpp|\/C|\.cc|\.CC)$/;
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
open IN, "<", $fileName || die "Unable to open \"$fileName\": $!\n";
|
my $inFile = new IO::File('<' . $fileName) or die ('Unable to open ' . $fileName . ': ' . $!);
|
||||||
|
$fileCount++;
|
||||||
|
my @affectedClasses;
|
||||||
|
my @outLines;
|
||||||
|
|
||||||
my $found = 0;
|
while (my $line = <$inFile>) {
|
||||||
|
|
||||||
# First, we check whether we have a match
|
|
||||||
while (<IN>) {
|
|
||||||
my $line = $_;
|
|
||||||
|
|
||||||
if ($line =~ /^#\s*include\s*<(.*?\/(.*?))>/) {
|
|
||||||
my $newHeader = $headerSubst{$2};
|
|
||||||
if ($newHeader && $1 ne $newHeader) {
|
|
||||||
if ($dry_run) {
|
|
||||||
print "$relFileName: <$1> => <$newHeader>\n";
|
|
||||||
} else {
|
|
||||||
$found = 1;
|
|
||||||
last;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} elsif ($line =~ /^#\s*include\s*<QtGui>/) {
|
|
||||||
if ($dry_run) {
|
|
||||||
print "$relFileName: <QtGui> => <QtWidgets>\n";
|
|
||||||
} else {
|
|
||||||
$found = 1;
|
|
||||||
last;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($dry_run || !$found) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
# rewind to top
|
|
||||||
seek(IN, 0, 0) || die "Unable to seek in $fileName: $!\n";
|
|
||||||
|
|
||||||
open OUT, ">", "$fileName.new" || die "Unable to open \"$fileName.new\": $!\n";
|
|
||||||
|
|
||||||
while (<IN>) {
|
|
||||||
my $line = $_;
|
|
||||||
if ($line =~ /^#(\s*)include(\s*)<.*?\/(.*?)>(.*)/) {
|
if ($line =~ /^#(\s*)include(\s*)<.*?\/(.*?)>(.*)/) {
|
||||||
my $newHeader = $headerSubst{$3};
|
my $newHeader = $headerSubst{$3};
|
||||||
if ($newHeader) {
|
if ($newHeader) {
|
||||||
$line = "#$1include$2<$newHeader>$4\n";
|
$line = '#' . $1 . 'include' . $2 . '<' . $newHeader . '>' . $4 . "\n";
|
||||||
|
push(@affectedClasses, $3);
|
||||||
}
|
}
|
||||||
} elsif ($line =~ /^#(\s*)include(\s*)<QtGui>(.*)/) {
|
} elsif ($line =~ /^#(\s*)include(\s*)<QtGui>(.*)/) {
|
||||||
$line = "#$1include$2<QtWidgets>$3\n";
|
$line = '#' . $1 . 'include' . $2 . '<QtWidgets>' . $3 . "\n";
|
||||||
|
push(@affectedClasses, 'QtGui');
|
||||||
}
|
}
|
||||||
|
push(@outLines, $line);
|
||||||
print OUT $line;
|
|
||||||
}
|
}
|
||||||
|
$inFile->close();
|
||||||
|
|
||||||
close OUT;
|
if (scalar(@affectedClasses)) {
|
||||||
|
$fixedFileCount++;
|
||||||
close IN;
|
print $relFileName, ': ', join(', ', @affectedClasses), "\n" if ($verbose || $dry_run);
|
||||||
|
if (!$dry_run) {
|
||||||
rename "$fileName.new", $fileName || die "Unable to move $fileName.new to $fileName: $!\n";
|
my $outFile = new IO::File('>' . $fileName) or die ('Unable to open ' . $fileName . ': ' . $!);
|
||||||
|
map { print $outFile $_; } @outLines;
|
||||||
$fixedFileCount += 1;
|
$outFile->close();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
print $relFileName, ": no modification.\n" if ($verbose || $dry_run);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
sub findQtHeaders
|
sub findQtHeaders
|
||||||
@ -155,41 +126,35 @@ sub findQtHeaders
|
|||||||
|
|
||||||
local (*DIR);
|
local (*DIR);
|
||||||
|
|
||||||
opendir(DIR, "$baseDir/include/$dirName") || die "Unable to open \"$baseDir/include/$dirName\": $!\n";
|
opendir(DIR, $baseDir . '/include/' . $dirName) || die ('Unable to open ' .$baseDir . '/include/' . $dirName . ': ' . $!);
|
||||||
my @headers = readdir(DIR);
|
my @headers = readdir(DIR);
|
||||||
closedir(DIR);
|
closedir(DIR);
|
||||||
|
|
||||||
foreach my $header (@headers) {
|
foreach my $header (@headers) {
|
||||||
if (-d "$baseDir/include/$dirName/$header" || $header =~ /\.pri$/) {
|
next if (-d ($baseDir . '/include/' . $dirName . '/' . $header) || $header =~ /\.pri$/);
|
||||||
next;
|
$headerSubst{$header} = $stripModule ? $header : ($dirName . '/' . $header);
|
||||||
}
|
|
||||||
$headerSubst{$header} = "$dirName/$header";
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$qtdir = $ENV{'QTDIR'};
|
# -------- MAIN
|
||||||
|
|
||||||
if (!$qtdir) {
|
die "This script requires the QTDIR environment variable pointing to Qt 5\n" unless $qtdir;
|
||||||
die "This script requires the QTDIR environment variable pointing to Qt 5\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
findQtHeaders("QtWidgets", $qtdir);
|
findQtHeaders('QtWidgets', $qtdir);
|
||||||
findQtHeaders("QtPrintSupport", $qtdir);
|
findQtHeaders('QtPrintSupport', $qtdir);
|
||||||
|
|
||||||
if (-d "$qtdir/include/QtQuick1") {
|
if (-d $qtdir . '/include/QtQuick1') {
|
||||||
findQtHeaders("QtQuick1", $qtdir);
|
findQtHeaders('QtQuick1', $qtdir);
|
||||||
} elsif (-d "$qtdir/../qtdeclarative" ) {
|
} elsif (-d $qtdir . '/../qtdeclarative' ) {
|
||||||
# This is the case if QTDIR points to a source tree instead of an installed Qt
|
# This is the case if QTDIR points to a source tree instead of an installed Qt
|
||||||
findQtHeaders("QtQuick1", "$qtdir/../qtdeclarative");
|
findQtHeaders('QtQuick1', $qtdir . '/../qtdeclarative');
|
||||||
} else {
|
} else {
|
||||||
print "Warning - cannot find QtQuick1 headers\n";
|
print "Warning - cannot find QtQuick1 headers\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
# special case
|
# special case
|
||||||
$headerSubst{"QtGui"} = "QtWidgets/QtWidgets";
|
$headerSubst{'QtGui'} = 'QtWidgets/QtWidgets';
|
||||||
|
|
||||||
find({ wanted => \&fixHeaders, no_chdir => 1}, $cwd);
|
find({ wanted => \&fixHeaders, no_chdir => 1}, $cwd);
|
||||||
|
|
||||||
if ($dry_run == 0) {
|
print 'Done. ', ($dry_run ? 'Checked' : 'Modified'), ' ', $fixedFileCount, ' of ', $fileCount, " file(s).\n";
|
||||||
print "Done. Modified $fixedFileCount file(s).\n";
|
|
||||||
}
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user