let default_pre add modules to the qmake path
[profile/ivi/qtbase.git] / bin / fixqt4headers.pl
1 #!/usr/bin/env perl
2 #############################################################################
3 ##
4 ## Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
5 ## Contact: http://www.qt-project.org/
6 ##
7 ## This file is part of the porting tools of the Qt Toolkit.
8 ##
9 ## $QT_BEGIN_LICENSE:LGPL$
10 ## GNU Lesser General Public License Usage
11 ## This file may be used under the terms of the GNU Lesser General Public
12 ## License version 2.1 as published by the Free Software Foundation and
13 ## appearing in the file LICENSE.LGPL included in the packaging of this
14 ## file. Please review the following information to ensure the GNU Lesser
15 ## General Public License version 2.1 requirements will be met:
16 ## http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ##
18 ## In addition, as a special exception, Nokia gives you certain additional
19 ## rights. These rights are described in the Nokia Qt LGPL Exception
20 ## version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ##
22 ## GNU General Public License Usage
23 ## Alternatively, this file may be used under the terms of the GNU General
24 ## Public License version 3.0 as published by the Free Software Foundation
25 ## and appearing in the file LICENSE.GPL included in the packaging of this
26 ## file. Please review the following information to ensure the GNU General
27 ## Public License version 3.0 requirements will be met:
28 ## http://www.gnu.org/copyleft/gpl.html.
29 ##
30 ## Other Usage
31 ## Alternatively, this file may be used in accordance with the terms and
32 ## conditions contained in a signed written agreement between you and Nokia.
33 ##
34 ##
35 ##
36 ##
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";