From bb4dbafbb1f4ad13eb1c4f6a798051310c4d2208 Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Tue, 22 Nov 2011 12:20:04 +0100 Subject: 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 --- bin/fixqt4headers.pl | 159 ++++++++++++++++++++------------------------------- 1 file changed, 62 insertions(+), 97 deletions(-) (limited to 'bin/fixqt4headers.pl') diff --git a/bin/fixqt4headers.pl b/bin/fixqt4headers.pl index 60fdfa479b..1905a6b21f 100755 --- a/bin/fixqt4headers.pl +++ b/bin/fixqt4headers.pl @@ -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; - -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"; -} - -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; - } +my $fileCount = 0; +my $verbose = 0; +my $qtdir = $ENV{'QTDIR'}; + +my $USAGE=< : 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; - } - - open IN, "<", $fileName || die "Unable to open \"$fileName\": $!\n"; + return unless -f $fileName && $fileName =~ /(\.h|\.cpp|\/C|\.cc|\.CC)$/; - my $found = 0; + my $inFile = new IO::File('<' . $fileName) or die ('Unable to open ' . $fileName . ': ' . $!); + $fileCount++; + my @affectedClasses; + my @outLines; - # First, we check whether we have a match - while () { - 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*/) { - if ($dry_run) { - print "$relFileName: => \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 () { - 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*)(.*)/) { - $line = "#$1include$2$3\n"; + $line = '#' . $1 . 'include' . $2 . '' . $3 . "\n"; + push(@affectedClasses, 'QtGui'); } - - print OUT $line; + push(@outLines, $line); + } + $inFile->close(); + + 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); } - - close OUT; - - close IN; - - rename "$fileName.new", $fileName || die "Unable to move $fileName.new to $fileName: $!\n"; - - $fixedFileCount += 1; } 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"; -- cgit v1.2.3