Test: remove QSKIP from tst_LargeFile::mapOffsetOverflow
[profile/ivi/qtbase.git] / bin / fixqt4headers.pl
1 #!/usr/bin/env perl
2 #############################################################################
3 ##
4 ## Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
5 ## Contact: http://www.qt-project.org/legal
6 ##
7 ## This file is part of the porting tools of the Qt Toolkit.
8 ##
9 ## $QT_BEGIN_LICENSE:LGPL$
10 ## Commercial License Usage
11 ## Licensees holding valid commercial Qt licenses may use this file in
12 ## accordance with the commercial license agreement provided with the
13 ## Software or, alternatively, in accordance with the terms contained in
14 ## a written agreement between you and Digia.  For licensing terms and
15 ## conditions see http://qt.digia.com/licensing.  For further information
16 ## use the contact form at http://qt.digia.com/contact-us.
17 ##
18 ## GNU Lesser General Public License Usage
19 ## Alternatively, this file may be used under the terms of the GNU Lesser
20 ## General Public License version 2.1 as published by the Free Software
21 ## Foundation and appearing in the file LICENSE.LGPL included in the
22 ## packaging of this file.  Please review the following information to
23 ## ensure the GNU Lesser General Public License version 2.1 requirements
24 ## will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ##
26 ## In addition, as a special exception, Digia gives you certain additional
27 ## rights.  These rights are described in the Digia Qt LGPL Exception
28 ## version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ##
30 ## GNU General Public License Usage
31 ## Alternatively, this file may be used under the terms of the GNU
32 ## General Public License version 3.0 as published by the Free Software
33 ## Foundation and appearing in the file LICENSE.GPL included in the
34 ## packaging of this file.  Please review the following information to
35 ## ensure the GNU General Public License version 3.0 requirements will be
36 ## met: http://www.gnu.org/copyleft/gpl.html.
37 ##
38 ##
39 ## $QT_END_LICENSE$
40 ##
41 #############################################################################
42
43
44 use Cwd;
45 use File::Find;
46 use File::Spec;
47 use IO::File;
48 use Getopt::Long;
49 use strict;
50 use warnings;
51
52 my $dry_run = 0;
53 my $help = 0;
54 my $stripModule = 0;
55 my $fixedFileCount = 0;
56 my $fileCount = 0;
57 my $verbose = 0;
58 my $qtdir = $ENV{'QTDIR'};
59
60 my $USAGE=<<EOF;
61 This script replaces all Qt 4 style includes with Qt 5 includes.
62
63 Usage: $0 [options]
64
65 Options:
66    --dry-run           : Do not replace anything, just print what would be replaced
67    --strip-modules     : Strip the module headers for writing portable code
68    --verbose           : Verbose
69    --qtdir <directory> : Point to Qt 5's qtbase directory
70 EOF
71
72 if (!GetOptions('dry-run' => \$dry_run, 'help' => \$help,
73      'strip-modules' => \$stripModule, 'verbose' => \$verbose, 'qtdir:s' => \$qtdir)
74     || $help) {
75     print $USAGE;
76     exit (1);
77 }
78
79 my %headerSubst = ();
80 my $cwd = getcwd();
81
82 sub fixHeaders
83 {
84     my $fileName = $File::Find::name;
85     my $relFileName = File::Spec->abs2rel($fileName, $cwd);
86
87     # only check sources, also ignore symbolic links and directories
88     return unless -f $fileName && $fileName =~ /(\.h|\.cpp|\/C|\.cc|\.CC)$/;
89
90     my $inFile = new IO::File('<' . $fileName) or die ('Unable to open ' . $fileName . ': ' . $!);
91     $fileCount++;
92     my @affectedClasses;
93     my @outLines;
94
95     while (my $line = <$inFile>) {
96         if ($line =~ /^#(\s*)include(\s*)<.*?\/(.*?)>(.*)/) {
97             my $newHeader = $headerSubst{$3};
98             if ($newHeader) {
99                 $line = '#' . $1 . 'include' . $2 . '<' . $newHeader . '>' . $4 . "\n";
100                 push(@affectedClasses, $3);
101             }
102         } elsif ($line =~ /^#(\s*)include(\s*)<QtGui>(.*)/) {
103             $line = '#' . $1 . 'include' . $2 . '<QtWidgets>' . $3 . "\n";
104             push(@affectedClasses, 'QtGui');
105         }
106         push(@outLines, $line);
107     }
108     $inFile->close();
109
110     if (scalar(@affectedClasses)) {
111         $fixedFileCount++;
112         print $relFileName, ': ', join(', ', @affectedClasses), "\n" if ($verbose || $dry_run);
113         if (!$dry_run) {
114             my $outFile = new IO::File('>' . $fileName) or die ('Unable to open ' . $fileName . ': ' . $!);
115             map { print $outFile $_; } @outLines;
116             $outFile->close();
117         }
118     } else {
119         print $relFileName, ": no modification.\n" if ($verbose || $dry_run);
120     }
121 }
122
123 sub findQtHeaders
124 {
125     my ($dirName,$baseDir) = @_;
126
127     local (*DIR);
128
129     opendir(DIR, $baseDir . '/include/' . $dirName) || die ('Unable to open ' .$baseDir . '/include/' . $dirName . ': ' . $!);
130     my @headers = readdir(DIR);
131     closedir(DIR);
132
133     foreach my $header (@headers) {
134         next if (-d ($baseDir . '/include/' . $dirName . '/' . $header) || $header =~ /\.pri$/);
135         $headerSubst{$header} = $stripModule ?  $header : ($dirName . '/' . $header);
136     }
137 }
138
139 # -------- MAIN
140
141 die "This script requires the QTDIR environment variable pointing to Qt 5\n" unless $qtdir;
142
143 findQtHeaders('QtCore', $qtdir);
144 findQtHeaders('QtConcurrent', $qtdir);
145 findQtHeaders('QtWidgets', $qtdir);
146 findQtHeaders('QtPrintSupport', $qtdir);
147
148 if (-d $qtdir . '/include/QtQuick1') {
149     findQtHeaders('QtQuick1', $qtdir);
150 } elsif (-d $qtdir . '/../qtdeclarative' ) {
151     # This is the case if QTDIR points to a source tree instead of an installed Qt
152     findQtHeaders('QtQuick1', $qtdir . '/../qtdeclarative');
153 } else {
154     print "Warning - cannot find QtQuick1 headers\n";
155 }
156
157 # Support porting from "Qt 4.99" QtDeclarative to QtQuick (QQuickItem et al)
158 if (-d $qtdir . '/include/QtQuick') {
159     findQtHeaders('QtQuick', $qtdir);
160 } elsif (-d $qtdir . '/../qtdeclarative' ) {
161     # This is the case if QTDIR points to a source tree instead of an installed Qt
162     findQtHeaders('QtQuick', $qtdir . '/../qtdeclarative');
163 }
164
165 # special case
166 $headerSubst{'QtGui'} = 'QtWidgets/QtWidgets';
167
168 find({ wanted => \&fixHeaders, no_chdir => 1}, $cwd);
169
170 print 'Done. ', ($dry_run ? 'Checked' : 'Modified'), ' ', $fixedFileCount, ' of ', $fileCount, " file(s).\n";