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:
Friedemann Kleint 2011-11-22 12:20:04 +01:00 committed by Qt by Nokia
parent 52ef2ee57f
commit bb4dbafbb1

View File

@ -44,39 +44,39 @@
use Cwd;
use File::Find;
use File::Spec;
use IO::File;
use Getopt::Long;
use strict;
use warnings;
my $dry_run = 0;
my $help = 0;
my $stripModule = 0;
my $fixedFileCount = 0;
my $fileCount = 0;
my $verbose = 0;
my $qtdir = $ENV{'QTDIR'};
sub show_help
{
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";
}
my $USAGE=<<EOF;
This script replaces all Qt 4 style includes with Qt 5 includes.
if ($#ARGV >= 0) {
if ($#ARGV >= 1) {
die "$0: Takes only one or zero arguments\n";
} elsif ($ARGV[0] eq "--dry-run") {
$dry_run = 1;
} elsif ($ARGV[0] eq "--help") {
show_help();
exit 0
} else {
show_help();
print "Unknown argument: $ARGV[0]\n";
exit 1;
}
Usage: $0 [options]
Options:
--dry-run : Do not replace anything, just print what would be replaced
--strip-modules : Strip the module headers for writing portable code
--verbose : Verbose
--qtdir <directory> : Point to Qt 5's qtbase directory
EOF
if (!GetOptions('dry-run' => \$dry_run, 'help' => \$help,
'strip-modules' => \$stripModule, 'verbose' => \$verbose, 'qtdir:s' => \$qtdir)
|| $help) {
print $USAGE;
exit (1);
}
my %headerSubst = ();
my $qtdir;
my $cwd = getcwd();
sub fixHeaders
@ -85,68 +85,39 @@ sub fixHeaders
my $relFileName = File::Spec->abs2rel($fileName, $cwd);
# only check sources, also ignore symbolic links and directories
if ($fileName !~ /(\.h|\.cpp|\/C|\.cc|\.CC)$/ || ! -f $fileName) {
return;
}
return unless -f $fileName && $fileName =~ /(\.h|\.cpp|\/C|\.cc|\.CC)$/;
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;
# 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 = $_;
while (my $line = <$inFile>) {
if ($line =~ /^#(\s*)include(\s*)<.*?\/(.*?)>(.*)/) {
my $newHeader = $headerSubst{$3};
if ($newHeader) {
$line = "#$1include$2<$newHeader>$4\n";
$line = '#' . $1 . 'include' . $2 . '<' . $newHeader . '>' . $4 . "\n";
push(@affectedClasses, $3);
}
} elsif ($line =~ /^#(\s*)include(\s*)<QtGui>(.*)/) {
$line = "#$1include$2<QtWidgets>$3\n";
$line = '#' . $1 . 'include' . $2 . '<QtWidgets>' . $3 . "\n";
push(@affectedClasses, 'QtGui');
}
print OUT $line;
push(@outLines, $line);
}
$inFile->close();
close OUT;
close IN;
rename "$fileName.new", $fileName || die "Unable to move $fileName.new to $fileName: $!\n";
$fixedFileCount += 1;
if (scalar(@affectedClasses)) {
$fixedFileCount++;
print $relFileName, ': ', join(', ', @affectedClasses), "\n" if ($verbose || $dry_run);
if (!$dry_run) {
my $outFile = new IO::File('>' . $fileName) or die ('Unable to open ' . $fileName . ': ' . $!);
map { print $outFile $_; } @outLines;
$outFile->close();
}
} else {
print $relFileName, ": no modification.\n" if ($verbose || $dry_run);
}
}
sub findQtHeaders
@ -155,41 +126,35 @@ sub findQtHeaders
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);
closedir(DIR);
foreach my $header (@headers) {
if (-d "$baseDir/include/$dirName/$header" || $header =~ /\.pri$/) {
next;
}
$headerSubst{$header} = "$dirName/$header";
next if (-d ($baseDir . '/include/' . $dirName . '/' . $header) || $header =~ /\.pri$/);
$headerSubst{$header} = $stripModule ? $header : ($dirName . '/' . $header);
}
}
$qtdir = $ENV{'QTDIR'};
# -------- MAIN
if (!$qtdir) {
die "This script requires the QTDIR environment variable pointing to Qt 5\n";
}
die "This script requires the QTDIR environment variable pointing to Qt 5\n" unless $qtdir;
findQtHeaders("QtWidgets", $qtdir);
findQtHeaders("QtPrintSupport", $qtdir);
findQtHeaders('QtWidgets', $qtdir);
findQtHeaders('QtPrintSupport', $qtdir);
if (-d "$qtdir/include/QtQuick1") {
findQtHeaders("QtQuick1", $qtdir);
} elsif (-d "$qtdir/../qtdeclarative" ) {
if (-d $qtdir . '/include/QtQuick1') {
findQtHeaders('QtQuick1', $qtdir);
} elsif (-d $qtdir . '/../qtdeclarative' ) {
# 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 {
print "Warning - cannot find QtQuick1 headers\n";
}
# special case
$headerSubst{"QtGui"} = "QtWidgets/QtWidgets";
$headerSubst{'QtGui'} = 'QtWidgets/QtWidgets';
find({ wanted => \&fixHeaders, no_chdir => 1}, $cwd);
if ($dry_run == 0) {
print "Done. Modified $fixedFileCount file(s).\n";
}
print 'Done. ', ($dry_run ? 'Checked' : 'Modified'), ' ', $fixedFileCount, ' of ', $fileCount, " file(s).\n";