let default_pre add modules to the qmake path
[profile/ivi/qtbase.git] / bin / syncqt
1 #!/usr/bin/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 build configuration 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 # Synchronizes Qt header files - internal development tool.
45 #
46
47 # use packages -------------------------------------------------------
48 use File::Basename;
49 use File::Path;
50 use File::Spec;
51 use Cwd;
52 use Cwd 'abs_path';
53 use Config;
54 use strict;
55 use warnings;
56 use English qw(-no_match_vars );
57
58 my $normalizePath_fixDrive = ($^O eq "msys" ? 1 : 0);
59
60 ######################################################################
61 # Syntax:  normalizePath(\$path)
62 # Params:  Reference to a path that's going to be normalized.
63 #
64 # Purpose: Converts the path into a form that can be used as include
65 #          path from C++ sources and qmake's .pro files.
66 #          Only relevant on Windows.
67 # Returns: -none-
68 ######################################################################
69 sub normalizePath {
70     my $s = shift;
71     $$s =~ s=\\=/=g;
72     if ($normalizePath_fixDrive && $$s =~ m,^/([a-zA-Z])/(.*),) {
73         $$s = lc($1) . ":/$2";
74     }
75 }
76
77 # set output basedir to be where ever syncqt is run from
78 our $out_basedir = getcwd();
79 normalizePath(\$out_basedir);
80 our $basedir;
81 our $quoted_basedir;
82
83 # Make sure we use Windows line endings for chomp and friends on Windows.
84 $INPUT_RECORD_SEPARATOR = "\r\n" if ($^O eq "msys");
85
86 my $qtbasedir = dirname(dirname($0));
87 normalizePath(\$qtbasedir) if (defined $qtbasedir);
88
89 # will be defined based on the modules sync.profile
90 our (%modules, %moduleheaders, @allmoduleheadersprivate, %classnames, %modulepris, %explicitheaders, %deprecatedheaders);
91
92 # global variables (modified by options)
93 my $isunix = 0;
94 my $module = 0;
95 my $showonly = 0;
96 my $verbose_level = 1;
97 my $remove_stale = 1;
98 my $force_win = 0;
99 my $force_relative = 0;
100 my $check_includes = 0;
101 my $copy_headers = 0;
102 my $create_uic_class_map = 0;
103 my $create_private_headers = 1;
104 my $module_fwd = "";
105 my $developer_build = 0;
106 my @modules_to_sync ;
107 $force_relative = 1 if ( -d "/System/Library/Frameworks" );
108
109
110 # functions ----------------------------------------------------------
111
112 ######################################################################
113 # Syntax:  showUsage()
114 # Params:  -none-
115 #
116 # Purpose: Show the usage of the script.
117 # Returns: -none-
118 ######################################################################
119 sub showUsage
120 {
121     print "$0 usage:\n";
122     print "  <module directory>    Specifies which module to sync header files for (required for shadow builds!)\n\n";
123
124     print "  -copy                 Copy headers instead of include-fwd(default: " . ($copy_headers ? "yes" : "no") . ")\n";
125     print "  -remove-stale         Removes stale headers              (default: " . ($remove_stale ? "yes" : "no") . ")\n";
126     print "  -relative             Force relative symlinks            (default: " . ($force_relative ? "yes" : "no") . ")\n";
127     print "  -windows              Force platform to Windows          (default: " . ($force_win ? "yes" : "no") . ")\n";
128     print "  -showonly             Show action but not perform        (default: " . ($showonly ? "yes" : "no") . ")\n";
129     print "  -outdir <PATH>        Specify output directory for sync  (default: $out_basedir)\n";
130     print "  -qtdir <PATH>         Set the path to QtBase             (detected: " . (defined $qtbasedir ? $qtbasedir : "-none-") . ")\n";
131     print "  -quiet                Only report problems, not activity (same as -verbose 0)\n";
132     print "  -v, -verbose <level>  Sets the verbosity level (max. 4)  (default: $verbose_level)\n";
133     print "                        The short form increases the level by +1\n";
134     print "  -separate-module <NAME>:<PROFILEDIR>:<HEADERDIR>\n";
135     print "                        Create headers for <NAME> with original headers in\n";
136     print "                        <HEADERDIR> relative to <PROFILEDIR> \n";
137     print "  -private              Force copy private headers         (default: " . ($create_private_headers ? "yes" : "no") . ")\n";
138     print "  -module-fwd <PATH>    Create fwd includes for module pri files in the given\n";
139     print "                        path (default: none)\n";
140     print "  -developer-build      Point libraries and binaries to a common directory for\n";
141     print "                        easy development\n";
142     print "  -help                 This help\n";
143     exit 0;
144 }
145
146 ######################################################################
147 # Syntax:  checkUnix()
148 # Params:  -none-
149 #
150 # Purpose: Check if script runs on a Unix system or not. Cygwin
151 #          systems are _not_ detected as Unix systems.
152 # Returns: 1 if a unix system, else 0.
153 ######################################################################
154 sub checkUnix {
155     my ($r) = 0;
156     if ( $force_win != 0) {
157         return 0;
158     } elsif ( -f "/bin/uname" ) {
159         $r = 1;
160         (-f "\\bin\\uname") && ($r = 0);
161     } elsif ( -f "/usr/bin/uname" ) {
162         $r = 1;
163         (-f "\\usr\\bin\\uname") && ($r = 0);
164     }
165     if($r) {
166         $_ = $Config{'osname'};
167         $r = 0 if( /(ms)|(cyg)win/i );
168     }
169     return $r;
170 }
171
172 sub checkRelative {
173     my ($dir) = @_;
174     return 0 if($dir =~ /^\//);
175     return 0 if(!checkUnix() && $dir =~ /[a-zA-Z]:[\/\\]/);
176     return 1;
177 }
178
179 ######################################################################
180 # Syntax:  classNames(iheader)
181 # Params:  iheader, string, filename to parse for classname "symlinks"
182 #
183 # Purpose: Scans through iheader to find all classnames that should be
184 #          synced into library's include structure.
185 # Returns: List of all class names in a file.
186 ######################################################################
187 sub classNames {
188     my @ret;
189     my ($iheader) = @_;
190
191     my $classname = $classnames{basename($iheader)};
192     push @ret, $classname if ($classname);
193
194     my $parsable = "";
195     if(open(F, "<$iheader")) {
196         while(<F>) {
197             my $line = $_;
198             chomp $line;
199                         chop $line if ($line =~ /\r$/);
200             if($line =~ /^\#/) {
201                 if($line =~ /\\$/) {
202                     while($line = <F>) {
203                         chomp $line;
204                         last unless($line =~ /\\$/);
205                     }
206                 }
207                 return @ret if($line =~ m/^#pragma qt_sync_stop_processing/);
208                 push(@ret, $1) if($line =~ m/^#pragma qt_class\(([^)]*)\)[\r\n]*$/);
209                 $line = 0;
210             }
211             if($line) {
212                 $line =~ s,//.*$,,; #remove c++ comments
213                 $line .= ";" if($line =~ m/^Q_[A-Z_]*\(.*\)[\r\n]*$/); #qt macro
214                 $line .= ";" if($line =~ m/^QT_(BEGIN|END)_HEADER[\r\n]*$/); #qt macro
215                 $line .= ";" if($line =~ m/^QT_(BEGIN|END)_NAMESPACE(_[A-Z]+)*[\r\n]*$/); #qt macro
216                 $line .= ";" if($line =~ m/^QT_MODULE\(.*\)[\r\n]*$/); # QT_MODULE macro
217                 $parsable .= " " . $line;
218             }
219         }
220         close(F);
221     }
222
223     my $last_definition = 0;
224     my @namespaces;
225     for(my $i = 0; $i < length($parsable); $i++) {
226         my $definition = 0;
227         my $character = substr($parsable, $i, 1);
228         if($character eq "/" && substr($parsable, $i+1, 1) eq "*") { #I parse like this for greedy reasons
229             for($i+=2; $i < length($parsable); $i++) {
230                 my $end = substr($parsable, $i, 2);
231                 if($end eq "*/") {
232                     $last_definition = $i+2;
233                     $i++;
234                     last;
235                 }
236             }
237         } elsif($character eq "{") {
238             my $brace_depth = 1;
239             my $block_start = $i + 1;
240           BLOCK: for($i+=1; $i < length($parsable); $i++) {
241               my $ignore = substr($parsable, $i, 1);
242               if($ignore eq "{") {
243                   $brace_depth++;
244               } elsif($ignore eq "}") {
245                   $brace_depth--;
246                   unless($brace_depth) {
247                       for(my $i2 = $i+1; $i2 < length($parsable); $i2++) {
248                           my $end = substr($parsable, $i2, 1);
249                           if($end eq ";" || $end ne " ") {
250                               $definition = substr($parsable, $last_definition, $block_start - $last_definition) . "}";
251                               $i = $i2 if($end eq ";");
252                               $last_definition = $i + 1;
253                               last BLOCK;
254                           }
255                       }
256                   }
257               }
258           }
259         } elsif($character eq ";") {
260             $definition = substr($parsable, $last_definition, $i - $last_definition + 1);
261             $last_definition = $i + 1;
262         } elsif($character eq "}") {
263             # a naked } must be a namespace ending
264             # if it's not a namespace, it's eaten by the loop above
265             pop @namespaces;
266             $last_definition = $i + 1;
267         }
268
269         if (substr($parsable, $last_definition, $i - $last_definition + 1) =~ m/ namespace ([^ ]*) /
270             && substr($parsable, $i+1, 1) eq "{") {
271             push @namespaces, $1;
272
273             # Eat the opening { so that the condensing loop above doesn't see it
274             $i++;
275             $last_definition = $i + 1;
276         }
277
278         if($definition) {
279             $definition =~ s=[\n\r]==g;
280             my @symbols;
281             if($definition =~ m/^ *typedef *.*\(\*([^\)]*)\)\(.*\);$/) {
282                 push @symbols, $1;
283             } elsif($definition =~ m/^ *typedef +(.*) +([^ ]*);$/) {
284                 push @symbols, $2;
285             } elsif($definition =~ m/^ *(template *<.*> *)?(class|struct) +([^ ]* +)?([^<\s]+) ?(<[^>]*> ?)?\s*((,|:)\s*(public|protected|private) *.*)? *\{\}$/) {
286                 push @symbols, $4;
287             } elsif($definition =~ m/^ *Q_DECLARE_.*ITERATOR\((.*)\);$/) {
288                 push @symbols, "Q" . $1 . "Iterator";
289                 push @symbols, "QMutable" . $1 . "Iterator";
290             }
291
292             our $publicclassregexp;
293             foreach my $symbol (@symbols) {
294                 $symbol = (join("::", @namespaces) . "::" . $symbol) if (scalar @namespaces);
295
296                 if ($symbol =~ /^Q[^:]*$/           # no-namespace, starting with Q
297                     || $symbol =~ /^Phonon::/) {    # or in the Phonon namespace
298                     push @ret, $symbol;
299                 } elsif (defined($publicclassregexp)) {
300                     push @ret, $symbol if ($symbol =~ $publicclassregexp);
301                 }
302             }
303         }
304     }
305     return @ret;
306 }
307
308 sub make_path {
309     my ($dir, $lib, $be_verbose) = @_;
310     unless(-e $dir) {
311         mkpath $dir;
312         $dir = "<outbase>" . substr($dir, length($out_basedir)) if ($be_verbose < 3);
313         print "$lib: mkpath $dir\n" if ($be_verbose > 1);
314     }
315 }
316
317 ######################################################################
318 # Syntax:  syncHeader(header, iheader, copy, timestamp)
319 # Params:  header, string, filename to create "symlink" for
320 #          iheader, string, destination name of symlink
321 #          copy, forces header to be a copy of iheader
322 #          timestamp, the requested modification time if copying
323 #
324 # Purpose: Syncronizes header to iheader
325 # Returns: 1 if successful, else 0.
326 ######################################################################
327 sub syncHeader {
328     my ($lib, $header, $iheader, $copy, $ts) = @_;
329     normalizePath(\$iheader);
330     normalizePath(\$header);
331     return copyFile($lib, $iheader, $header) if($copy);
332
333     unless(-e $header) {
334         my $header_dir = dirname($header);
335         make_path($header_dir, $lib, $verbose_level);
336
337         #write it
338         my $iheader_out = fixPaths($iheader, $header_dir);
339         open(HEADER, ">$header") || die "Could not open $header for writing: $!\n";
340         print HEADER "#include \"$iheader_out\"\n";
341         close HEADER;
342         if(defined($ts)) {
343             utime(time, $ts, $header) or die "$iheader, $header";
344         }
345         return 1;
346     }
347     return 0;
348 }
349
350 ######################################################################
351 # Syntax:  fixPaths(file, dir)
352 # Params:  file, string, filepath to be made relative to dir
353 #          dir, string, dirpath for point of origin
354 #
355 # Purpose: file is made relative (if possible) of dir.
356 # Returns: String with the above applied conversion.
357 ######################################################################
358
359 sub cleanupPath {
360     my ($file) = @_;
361     normalizePath(\$file);
362     while ($file =~ s,/[^/]+/\.\./,/,) {}
363     return $file;
364 }
365
366 sub fixPaths {
367     my ($file, $dir) = @_;
368
369     my $out = File::Spec->abs2rel(cleanupPath($file), cleanupPath($dir));
370     $out =~ s,\\,/,g;
371     return $out;
372 }
373
374 ######################################################################
375 # Syntax:  fileContents(filename)
376 # Params:  filename, string, filename of file to return contents
377 #
378 # Purpose: Get the contents of a file.
379 # Returns: String with contents of the file, or empty string if file
380 #          doens't exist.
381 # Warning: Dies if it does exist but script cannot get read access.
382 ######################################################################
383 sub fileContents {
384     my ($filename) = @_;
385     my $filecontents = "";
386     if (-e $filename) {
387         open(I, "< $filename") || die "Could not open $filename for reading, read block?";
388         local $/;
389         binmode I;
390         $filecontents = <I>;
391         close I;
392     }
393     return $filecontents;
394 }
395
396 ######################################################################
397 # Syntax:  fileCompare(file1, file2)
398 # Params:  file1, string, filename of first file
399 #          file2, string, filename of second file
400 #
401 # Purpose: Determines if files are equal, and which one is newer.
402 # Returns: 0 if files are equal no matter the timestamp, -1 if file1
403 #          is newer, 1 if file2 is newer.
404 ######################################################################
405 sub fileCompare {
406     my ($file1, $file2) = @_;
407     my $file1contents = fileContents($file1);
408     my $file2contents = fileContents($file2);
409     if (! -e $file1) { return 1; }
410     if (! -e $file2) { return -1; }
411     return $file1contents ne $file2contents ? (stat($file2))[9] <=> (stat($file1))[9] : 0;
412 }
413
414 ######################################################################
415 # Syntax:  copyFile(file, ifile)
416 # Params:  file, string, filename to create duplicate for
417 #          ifile, string, destination name of duplicate
418 #
419 # Purpose: Keeps files in sync so changes in the newer file will be
420 #          written to the other.
421 # Returns: 1 if files were synced, else 0.
422 # Warning: Dies if script cannot get write access.
423 ######################################################################
424 sub copyFile
425 {
426     my ($lib, $file,$ifile, $copy,$knowdiff,$filecontents,$ifilecontents) = @_;
427     # Bi-directional synchronization
428     open( I, "< " . $file ) || die "Could not open $file for reading";
429     local $/;
430     binmode I;
431     $filecontents = <I>;
432     close I;
433     if ( open(I, "< " . $ifile) ) {
434         local $/;
435         binmode I;
436         $ifilecontents = <I>;
437         close I;
438         $copy = fileCompare($file, $ifile);
439         $knowdiff = 0,
440     } else {
441         $copy = -1;
442         $knowdiff = 1;
443     }
444
445     if ( $knowdiff || ($filecontents ne $ifilecontents) ) {
446         if ( $copy > 0 ) {
447             my $file_dir = dirname($file);
448             make_path($file_dir, $lib, $verbose_level);
449             open(O, "> " . $file) || die "Could not open $file for writing (no write permission?)";
450             local $/;
451             binmode O;
452             print O $ifilecontents;
453             close O;
454             utime time, (stat($ifile))[9], $file;
455             return 1;
456         } elsif ( $copy < 0 ) {
457             my $ifile_dir = dirname($ifile);
458             make_path($ifile_dir, $lib, $verbose_level);
459             open(O, "> " . $ifile) || die "Could not open $ifile for writing (no write permission?)";
460             local $/;
461             binmode O;
462             print O $filecontents;
463             close O;
464             utime time, (stat($file))[9], $ifile;
465             return 1;
466         }
467     }
468     return 0;
469 }
470
471 ######################################################################
472 # Syntax:  symlinkFile(file, ifile)
473 # Params:  file, string, filename to create "symlink" for
474 #          ifile, string, destination name of symlink
475 #
476 # Purpose: File is symlinked to ifile (or copied if filesystem doesn't
477 #          support symlink).
478 # Returns: 1 on success, else 0.
479 ######################################################################
480 sub symlinkFile
481 {
482     my ($lib, $file, $ifile) = @_;
483
484     if ($isunix) {
485         print "$lib: symlink created for $file " if ($verbose_level);
486         if ( $force_relative && ($ifile =~ /^$quoted_basedir/)) {
487             my $t = getcwd();
488             my $c = -1;
489             my $p = "../";
490             $t =~ s-^$quoted_basedir/--;
491             $p .= "../" while( ($c = index( $t, "/", $c + 1)) != -1 );
492             $file =~ s-^$quoted_basedir/-$p-;
493             print " ($file)\n" if($verbose_level);
494         }
495         print "\n" if($verbose_level);
496         return symlink($file, $ifile);
497     }
498     return copyFile($lib, $file, $ifile);
499 }
500
501 ######################################################################
502 # Syntax:  findFiles(dir, match, descend)
503 # Params:  dir, string, directory to search for name
504 #          match, string, regular expression to match in dir
505 #          descend, integer, 0 = non-recursive search
506 #                            1 = recurse search into subdirectories
507 #
508 # Purpose: Finds files matching a regular expression.
509 # Returns: List of matching files.
510 #
511 # Examples:
512 #   findFiles("/usr","\.cpp$",1)  - finds .cpp files in /usr and below
513 #   findFiles("/tmp","^#",0)      - finds #* files in /tmp
514 ######################################################################
515 sub findFiles {
516     my ($dir,$match,$descend) = @_;
517     my ($file,$p,@files);
518     local(*D);
519     normalizePath(\$dir);
520     ($dir eq "") && ($dir = ".");
521     if ( opendir(D,$dir) ) {
522         if ( $dir eq "." ) {
523             $dir = "";
524         } else {
525             ($dir =~ /\/$/) || ($dir .= "/");
526         }
527         foreach $file ( sort readdir(D) ) {
528             next if ( $file  =~ /^\.\.?$/ );
529             $p = $file;
530             ($file =~ /$match/) && (push @files, $p);
531             if ( $descend && -d $p && ! -l $p ) {
532                 push @files, &findFiles($p,$match,$descend);
533             }
534         }
535         closedir(D);
536     }
537     return @files;
538 }
539
540 ######################################################################
541 # Syntax:  loadSyncProfile()
542 #
543 # Purpose: Locates the sync.profile.
544 # Returns: Hashmap of module name -> directory.
545 ######################################################################
546 sub loadSyncProfile {
547     my ($srcbase, $outbase) = @_;
548     if ($verbose_level) {
549         print("<srcbase> = $$srcbase \n");
550         print("<outbase> = $$outbase \n");
551     }
552
553     my $syncprofile = "$$srcbase/sync.profile";
554     my $result;
555     unless ($result = do "$syncprofile") {
556         die "syncqt couldn't parse $syncprofile: $@" if $@;
557         die "syncqt couldn't execute $syncprofile: $!" unless defined $result;
558     }
559     return $result;
560 }
561
562 sub basePrettify {
563     my ($arg) = @_;
564     $$arg =~ s,^\Q$basedir\E,<srcbase>,;
565     $$arg =~ s,^\Q$out_basedir\E,<outbase>,;
566 }
567
568 sub cleanPath {
569     my ($arg) = @_;
570     while ($arg =~ s,[^/]+/\.\.(/|$),,) {}
571     return $arg;
572 }
573
574 sub locateSyncProfile
575 {
576     my ($directory) = @_;
577     $directory = abs_path($directory);
578     while (1) {
579         my $file = $directory."/sync.profile";
580         return $file if (-e $file);
581         my $odir = $directory;
582         $directory = dirname($directory);
583         return undef if ($directory eq $odir);
584     }
585 }
586
587 # check if this is an in-source build, and if so use that as the basedir too
588 $basedir = locateSyncProfile($out_basedir);
589 if ($basedir) {
590     $basedir = dirname($basedir) ;
591     normalizePath(\$basedir);
592     $quoted_basedir = "\Q$basedir";
593 }
594
595 # --------------------------------------------------------------------
596 # "main" function
597 # --------------------------------------------------------------------
598
599 while ( @ARGV ) {
600     my $var = 0;
601     my $val = 0;
602
603     #parse
604     my $arg = shift @ARGV;
605     if ($arg eq "-h" || $arg eq "-help" || $arg eq "-?" || $arg eq "?") {
606         $var = "show_help";
607         $val = "yes";
608     } elsif($arg eq "-copy") {
609         $var = "copy";
610         $val = "yes";
611     } elsif($arg eq "-o" || $arg eq "-outdir") {
612         $var = "output";
613         $val = shift @ARGV;
614     } elsif($arg eq "-showonly" || $arg eq "-remove-stale" || $arg eq "-windows" ||
615             $arg eq "-relative" || $arg eq "-check-includes") {
616         $var = substr($arg, 1);
617         $val = "yes";
618     } elsif($arg eq "-module-fwd") {
619         $var = "module_fwd";
620         $val = shift @ARGV;
621     } elsif($arg eq "-developer-build") {
622         $var = "developer_build";
623         $val = "yes";
624     } elsif($arg =~ /^-no-(.*)$/) {
625         $var = $1;
626         $val = "no";
627         #these are for commandline compat
628     } elsif($arg eq "-inc") {
629         $var = "output";
630         $val = shift @ARGV;
631     } elsif($arg eq "-module") {
632         $var = "module";
633         $val = shift @ARGV;
634     } elsif($arg eq "-separate-module") {
635         $var = "separate-module";
636         $val = shift @ARGV;
637     } elsif($arg eq "-show") {
638         $var = "showonly";
639         $val = "yes";
640     } elsif($arg eq "-quiet") {
641         $var = "verbose";
642         $val = "0";
643     } elsif($arg eq "-v") {
644         $var = "verbose";
645         $val = "yes";
646     } elsif($arg eq "-verbose") {
647         $var = "verbose";
648         $val = shift @ARGV;
649     } elsif($arg eq "-private") {
650         $var = "create_private_headers";
651         $val = "yes";
652     } elsif($arg eq "-qtdir") {
653         $var = "qtdir";
654         $val = shift @ARGV;
655     } elsif($arg =~/^-/) {
656         print "Unknown option: $arg\n\n" if(!$var);
657         showUsage();
658     } else {
659         $basedir = locateSyncProfile($arg);
660         die "Could not find a sync.profile for '$arg'\n" if (!$basedir);
661         $basedir = dirname($basedir);
662         normalizePath(\$basedir);
663         $quoted_basedir = "\Q$basedir";
664         $var = "ignore";
665     }
666
667     #do something
668     if(!$var || $var eq "show_help") {
669         print "Unknown option: $arg\n\n" if(!$var);
670         showUsage();
671     } elsif ($var eq "copy") {
672         if($val eq "yes") {
673             $copy_headers++;
674         } elsif($showonly) {
675             $copy_headers--;
676         }
677     } elsif ($var eq "showonly") {
678         if($val eq "yes") {
679             $showonly++;
680         } elsif($showonly) {
681             $showonly--;
682         }
683     } elsif ($var eq "verbose") {
684         if($val eq "yes") {
685             $verbose_level++;
686         } elsif($val eq "no" && $verbose_level) {
687             $verbose_level--;
688         } else {
689             $verbose_level = int($val);
690         }
691     } elsif ($var eq "check-includes") {
692         if($val eq "yes") {
693             $check_includes++;
694         } elsif($check_includes) {
695             $check_includes--;
696         }
697     } elsif ($var eq "remove-stale") {
698         if($val eq "yes") {
699             $remove_stale++;
700         } elsif($remove_stale) {
701             $remove_stale--;
702         }
703     } elsif ($var eq "windows") {
704         if($val eq "yes") {
705             $force_win++;
706         } elsif($force_win) {
707             $force_win--;
708         }
709     } elsif ($var eq "relative") {
710         if($val eq "yes") {
711             $force_relative++;
712         } elsif($force_relative) {
713             $force_relative--;
714         }
715     } elsif ($var eq "module") {
716         print "module :$val:\n" if($verbose_level);
717         die "No such module: $val" unless(defined $modules{$val});
718         push @modules_to_sync, $val;
719     } elsif ($var eq "separate-module") {
720         my ($module, $prodir, $headerdir) = split(/:/, $val);
721         $modules{$module} = $prodir;
722         push @modules_to_sync, $module;
723         $moduleheaders{$module} = $headerdir;
724         $create_uic_class_map = 0;
725     } elsif ($var eq "qtdir") {
726         if($val) {
727             $qtbasedir = $val;
728             normalizePath(\$qtbasedir);
729         } else {
730             die "The -qtdir option requires an argument";
731         }
732     } elsif ($var eq "module_fwd") {
733         $module_fwd = $val;
734     } elsif ($var eq "developer_build") {
735         $developer_build = 1;
736     } elsif ($var eq "output") {
737         my $outdir = $val;
738         if(checkRelative($outdir)) {
739             $out_basedir = getcwd();
740             chomp $out_basedir;
741             $out_basedir .= "/" . $outdir;
742         } else {
743             $out_basedir = $outdir;
744         }
745         normalizePath(\$out_basedir);
746     }
747 }
748
749 die "Cannot automatically detect/use provided path to QtBase's build directory!\n" .
750     "QTDIR detected/provided: " . (defined $qtbasedir ? $qtbasedir : "-none-") . "\n" .
751     "Please use the -qtdir option to provide the correct path.\nsyncqt failed"
752         if (!$qtbasedir || !-d "$qtbasedir/mkspecs");
753
754 # if we have no $basedir we cannot be sure which sources you want, so die
755 die "Could not find any sync.profile for your module!\nPass <module directory> to syncqt to sync your header files.\nsyncqt failed" if (!$basedir);
756
757 my $class_lib_map_contents = "";
758 our @ignore_headers = ();
759 our @ignore_for_master_contents = ();
760 our @ignore_for_include_check = ();
761 our @ignore_for_qt_begin_header_check = ();
762 our @ignore_for_qt_begin_namespace_check = ();
763 our @ignore_for_qt_module_check = ();
764 our %inject_headers = ();
765
766 # load the module's sync.profile here, before we can
767 loadSyncProfile(\$basedir, \$out_basedir);
768
769 @modules_to_sync = keys(%modules) if($#modules_to_sync == -1);
770
771 my %allmoduleheadersprivate = map { $_ => 1 } @allmoduleheadersprivate;
772
773 $isunix = checkUnix; #cache checkUnix
774
775 my $qt_version = fileContents($qtbasedir."/mkspecs/qconfig.pri");
776 if (length($qt_version)) {
777     $qt_version =~ s,.*^QT_VERSION\h*=\h*(\S+).*,$1,sm;
778 } else {
779     $qt_version = fileContents($basedir."/src/corelib/global/qglobal.h");
780     $qt_version =~ s,.*^#\h*define\h+QT_VERSION_STR\h+"([^"]+)".*,$1,sm;
781 }
782 foreach my $lib (@modules_to_sync) {
783     #iteration info
784     my $dir = $modules{$lib};
785
786     my $project = $dir;
787     $project =~ s,/([^/]+)$,/$1/$1.pro,;
788     my $module_version = fileContents($project);
789     $module_version = $qt_version unless ($module_version =~ s,.*^VERSION\h*=\h*(\S+).*,$1,sm);
790
791     # Backwards compatibility for modules with checked in .pri files.
792     if (defined $modulepris{$lib} and -e "$modulepris{$lib}") {
793         my $content = fileContents($modulepris{$lib});
794         my @version_rows = grep(/QT\..*\.VERSION/, split('\n', $content));
795         if(@version_rows) {
796             # We only pick the first one, since each module need a separate .pri file
797             $module_version = $version_rows[0];
798             chomp $module_version;
799             $module_version =~ s/^\s*QT\..*\.VERSION\s*=\s*([^#]+).*$/$1/;
800             $module_version =~ s/\s+$//;
801         }
802     }
803
804     my $pathtoheaders = "";
805     $pathtoheaders = $moduleheaders{$lib} if ($moduleheaders{$lib});
806
807     my $allheadersprivate = 0;
808     $allheadersprivate = 1 if $allmoduleheadersprivate{$lib};
809
810     #information used after the syncing
811     my $pri_install_classes = "";
812     my $pri_install_files = "";
813     my $pri_install_pfiles = "";
814     my $pri_install_qpafiles = "";
815
816     push @{$inject_headers{$dir}}, lc($lib)."version.h";
817     $classnames{lc($lib)."version.h"} = $lib."Version";
818
819     #remove the old files
820     if($remove_stale) {
821         my %injections = ();
822         for my $p (keys %inject_headers) {
823             next unless ($p =~ /^\Q$dir\E(\/|$)/);
824             my $sp = $p;
825             $sp =~ s,^\Q$basedir\E/,$out_basedir/,;
826             for my $n (@{$inject_headers{$p}}) {
827                 $injections{$sp."/".$n} = 1;
828             }
829         }
830         my @subdirs = ("$out_basedir/include/$lib");
831         foreach my $subdir (@subdirs) {
832             if (opendir DIR, $subdir) {
833                 foreach my $t (sort readdir(DIR)) {
834                     my $file = "$subdir/$t";
835                     if(-d $file) {
836                         push @subdirs, $file unless($t eq "." || $t eq "..");
837                     } else {
838                         my @files = ($file);
839                         #push @files, "$out_basedir/include/Qt/$t" if(-e "$out_basedir/include/Qt/$t");
840                         foreach my $file (@files) {
841                            my $remove_file = 0;
842                            if(open(F, "<$file")) {
843                                 while(my $line = <F>) {
844                                     chomp $line;
845                                     if($line =~ /^\#include \"([^\"]*)\"$/) {
846                                         my $include = $1;
847                                         $include = $subdir . "/" . $include unless(substr($include, 0, 1) eq "/");
848                                         $remove_file = 1 unless(-e $include or defined $injections{cleanPath($include)});
849                                     } else {
850                                         $remove_file = 0;
851                                         last;
852                                     }
853                                 }
854                                 close(F);
855                                 unlink $file if($remove_file);
856                             }
857                         }
858                     }
859                 }
860                 closedir DIR;
861             }
862
863         }
864     }
865
866     #create the new ones
867     foreach my $current_dir (split(/;/, $dir)) {
868         my @headers_paths = split(/;/, $pathtoheaders);
869         if (@headers_paths) {
870             @headers_paths = map { "$current_dir/$_" } @headers_paths;
871         } else {
872             push @headers_paths, $current_dir;
873         }
874
875         foreach my $headers_dir (@headers_paths) {
876             #calc subdirs
877             my @subdirs = ($headers_dir);
878             foreach my $subdir (@subdirs) {
879                 if ($subdir =~ /\/doc$/) {
880                     next;
881                 }
882                 opendir DIR, $subdir or next;
883                 foreach my $t (sort readdir(DIR)) {
884                     push @subdirs, "$subdir/$t" if(-d "$subdir/$t" && !($t eq ".") &&
885                                                    !($t eq "..") && !($t eq ".obj") &&
886                                                    !($t eq ".moc") && !($t eq ".rcc") &&
887                                                    !($t eq ".uic") && !($t eq "build"));
888                 }
889                 closedir DIR;
890             }
891
892             #calc files and "copy" them
893             foreach my $subdir (@subdirs) {
894                 my @headers = findFiles($subdir, "^[-a-z0-9_]*\\.h\$" , 0);
895                 if (defined $inject_headers{$subdir}) {
896                     foreach my $if (@{$inject_headers{$subdir}}) {
897                         @headers = grep(!/^\Q$if\E$/, @headers); #in case we configure'd previously
898                         push @headers, "*".$if;
899                     }
900                 }
901                 my $header_dirname = "";
902                 foreach my $header (@headers) {
903                     my $shadow = ($header =~ s/^\*//);
904                     $header = 0 if($header =~ /^ui_.*.h/);
905                     foreach (@ignore_headers) {
906                         $header = 0 if($header eq $_);
907                     }
908                     if($header) {
909                         my $header_copies = 0;
910                         #figure out if it is a public header
911                         my $public_header = $header;
912                         my $qpa_header = 0;
913                         if($public_header =~ /^qplatform/) {
914                             $public_header = 0;
915                             $qpa_header = 1;
916                         } elsif($allheadersprivate || $public_header =~ /_p.h$/ || $public_header =~ /_pch.h$/) {
917                             $public_header = 0;
918                         } else {
919                             foreach (@ignore_for_master_contents) {
920                                 $public_header = 0 if($header eq $_);
921                             }
922                         }
923
924                         my $iheader = $subdir . "/" . $header;
925                         $iheader =~ s/^\Q$basedir\E/$out_basedir/ if ($shadow);
926                         my @classes = $public_header ? classNames($iheader) : ();
927                         if($showonly) {
928                             print "$header [$lib]\n";
929                             foreach(@classes) {
930                                 print "SYMBOL: $_\n";
931                             }
932                         } else {
933                             my $ts = (stat($iheader))[9];
934                             #find out all the places it goes..
935                             my @headers;
936                             if ($public_header) {
937                                 @headers = ( "$out_basedir/include/$lib/$header" );
938                                 foreach my $full_class (@classes) {
939                                     my $header_base = basename($header);
940                                     # Strip namespaces:
941                                     my $class = $full_class;
942                                     $class =~ s/^.*:://;
943     #                               if ($class =~ m/::/) {
944     #                                  class =~ s,::,/,g;
945     #                               }
946
947                                     if (defined $explicitheaders{$lib}{$class}) {
948                                         $header_copies++ if(syncHeader($lib, "$out_basedir/include/$lib/$class", "$out_basedir/include/$lib/$explicitheaders{$lib}{$class}", 0, $ts));
949                                     } else {
950                                         $class_lib_map_contents .= "QT_CLASS_LIB($full_class, $lib, $header_base)\n";
951                                         $header_copies++ if(syncHeader($lib, "$out_basedir/include/$lib/$class", "$out_basedir/include/$lib/$header", 0, $ts));
952                                     }
953
954                                     # KDE-Compat headers for Phonon
955                                     if ($lib eq "phonon") {
956                                         $header_copies++ if (syncHeader($lib, "$out_basedir/include/phonon_compat/Phonon/$class", "$out_basedir/include/$lib/$header", 0, $ts));
957                                     }
958                                 }
959
960                                 if ($explicitheaders{$lib}{basename($header)}) {
961                                     $header_copies++ if(syncHeader($lib, "$out_basedir/include/$lib/$explicitheaders{$lib}{basename($header)}", "$out_basedir/include/$lib/$header", 0, $ts));
962                                 }
963
964                             } elsif ($create_private_headers && !$qpa_header) {
965                                 @headers = ( "$out_basedir/include/$lib/$module_version/$lib/private/$header" );
966                             } elsif ($create_private_headers) {
967                                 @headers = ( "$out_basedir/include/$lib/$module_version/$lib/qpa/$header" );
968                             }
969
970                             foreach(@headers) { #sync them
971                                 $header_copies++ if(syncHeader($lib, $_, $iheader, $copy_headers && !$shadow, $ts));
972                             }
973
974                             if($public_header) {
975                                 #deal with the install directives
976                                 if($public_header) {
977                                     my $pri_install_iheader = fixPaths($iheader, $current_dir);
978                                     foreach my $class (@classes) {
979                                         # Strip namespaces:
980                                         $class =~ s/^.*:://;
981     #                                   if ($class =~ m/::/) {
982     #                                       $class =~ s,::,/,g;
983     #                                   }
984                                         my $class_header = fixPaths("$out_basedir/include/$lib/$class",
985                                                                     $current_dir) . " ";
986                                         $pri_install_classes .= $class_header
987                                                                     unless($pri_install_classes =~ $class_header);
988                                     }
989                                     if ($explicitheaders{$lib}{basename($iheader)}) {
990                                         my $compat_header = fixPaths("$out_basedir/include/$lib/$explicitheaders{$lib}{basename($iheader)}", $current_dir) . " ";
991                                         $pri_install_files .= $compat_header unless($pri_install_files =~ $compat_header);
992                                     }
993                                     $pri_install_files.= "$pri_install_iheader ";;
994                                 }
995                             }
996                             elsif ($qpa_header) {
997                                 my $pri_install_iheader = fixPaths($iheader, $current_dir);
998                                 $pri_install_qpafiles.= "$pri_install_iheader ";;
999                             }
1000                             else {
1001                                 my $pri_install_iheader = fixPaths($iheader, $current_dir);
1002                                 $pri_install_pfiles.= "$pri_install_iheader ";;
1003                             }
1004                         }
1005
1006                         if ($verbose_level && $header_copies) {
1007                             my $new_header_dirname = dirname($iheader);
1008                             basePrettify(\$new_header_dirname) if ($new_header_dirname && $verbose_level < 2);
1009                             my $header_base = basename($iheader);
1010                             if ($verbose_level < 3) {
1011                                 my $line_prefix = ",";
1012                                 if ($new_header_dirname ne $header_dirname) {
1013                                     $line_prefix = "$lib: created fwd-include header(s) for $new_header_dirname/ {";
1014                                     $line_prefix = " }\n".$line_prefix if ($header_dirname);
1015                                     $header_dirname = $new_header_dirname;
1016                                 } else {
1017                                     $line_prefix = ",";
1018                                 }
1019                                 print "$line_prefix $header_base ($header_copies)";
1020                             } else { # $verbose_level >= 3
1021                                 basePrettify(\$iheader) if ($verbose_level == 3);
1022                                 print "$lib: created $header_copies fwd-include headers for $iheader\n";
1023                             }
1024                         }
1025                     }
1026                 }
1027                 print " }\n" if ($header_dirname && $verbose_level > 0 && $verbose_level < 3);
1028             }
1029         }
1030     }
1031
1032     unless($showonly) {
1033         # create deprecated headers
1034         my $first = 1;
1035         while (my ($header, $include) = each %{$deprecatedheaders{$lib}}) {
1036             my $public_header = 0;
1037             $public_header = 1 unless ($allheadersprivate || ($header =~ /_p\.h$/));
1038             next unless ($public_header || $create_private_headers);
1039
1040             my $header_path = "$out_basedir/include/$lib/";
1041             unless ($public_header) {
1042                 $header_path .= "$module_version/$lib/private/";
1043             }
1044             $header_path .= "$header";
1045
1046             unless (-e $header_path) {
1047                 my $guard = "DEPRECATED_HEADER_" . $lib . "_" . $header;
1048                 $guard =~ s/([^a-zA-Z0-9_])/_/g;
1049
1050                 my $header_dir = dirname($header_path);
1051                 make_path($header_dir, $lib, $verbose_level);
1052
1053                 open(HEADER, ">$header_path") || die "Could not open $header_path for writing: $!\n";
1054                 print HEADER "#ifndef $guard\n";
1055                 print HEADER "#define $guard\n";
1056                 my $warning = "Header <$lib/";
1057                 $warning .= "private/" unless ($public_header);
1058                 $warning .= "$header> is deprecated. Please include <$include> instead.";
1059                 print HEADER "#if defined(__GNUC__)\n";
1060                 print HEADER "#  warning $warning\n";
1061                 print HEADER "#elif defined(_MSC_VER)\n";
1062                 print HEADER "#  pragma message (\"$warning\")\n";
1063                 print HEADER "#endif\n";
1064                 print HEADER "#include <$include>\n";
1065                 if ($public_header) {
1066                     print HEADER "#if 0\n";
1067                     print HEADER "#pragma qt_no_master_include\n";
1068                     print HEADER "#endif\n";
1069                 }
1070                 print HEADER "#endif\n";
1071                 close HEADER;
1072
1073                 if ($verbose_level < 3) {
1074                     my $line_prefix = ",";
1075                     $line_prefix = "$lib: created deprecated header(s) {" if ($first);
1076                     print "$line_prefix $header";
1077                 } else {
1078                     print "$lib: created deprecated header $header => $include\n";
1079                 }
1080                 $first = 0;
1081             }
1082
1083             my $addendum = fixPaths($header_path, $dir) . " ";
1084             if ($public_header) {
1085                 $pri_install_files .=  $addendum;
1086             } else {
1087                 $pri_install_pfiles .=  $addendum;
1088             }
1089         }
1090         if ($verbose_level < 3) {
1091             print " }\n" unless ($first);
1092         }
1093
1094         # KDE-Compat master header for Phonon
1095         if ($lib eq "phonon") {
1096             syncHeader($lib, "$out_basedir/include/phonon_compat/Phonon/Phonon", "$out_basedir/include/phonon/phonon", 0);
1097         }
1098
1099         #handle the headers.pri for each module
1100         my $headers_pri_contents = "";
1101         $headers_pri_contents .= "SYNCQT.HEADER_FILES = $pri_install_files\n";
1102         $headers_pri_contents .= "SYNCQT.HEADER_CLASSES = $pri_install_classes\n";
1103         $headers_pri_contents .= "SYNCQT.PRIVATE_HEADER_FILES = $pri_install_pfiles\n";
1104         $headers_pri_contents .= "SYNCQT.QPA_HEADER_FILES = $pri_install_qpafiles\n";
1105         my $headers_pri_file = "$out_basedir/include/$lib/headers.pri";
1106         if(-e $headers_pri_file) {
1107             open HEADERS_PRI_FILE, "<$headers_pri_file";
1108             local $/;
1109             binmode HEADERS_PRI_FILE;
1110             my $old_headers_pri_contents = <HEADERS_PRI_FILE>;
1111             close HEADERS_PRI_FILE;
1112             $old_headers_pri_contents =~ s/\r//g; # remove \r's , so comparison is ok on all platforms
1113             $headers_pri_file = 0 if($old_headers_pri_contents eq $headers_pri_contents);
1114         }
1115         if($headers_pri_file) {
1116             my $headers_pri_dir = dirname($headers_pri_file);
1117             make_path($headers_pri_dir, $lib, $verbose_level);
1118             open HEADERS_PRI_FILE, ">$headers_pri_file";
1119             print HEADERS_PRI_FILE $headers_pri_contents;
1120             close HEADERS_PRI_FILE;
1121             print "$lib: created headers.pri file\n" if($verbose_level);
1122         }
1123
1124         # create forwarding module pri in qtbase/mkspecs/modules
1125         if ($module_fwd) {
1126             my $modulepri = $modulepris{$lib};
1127             if (defined $modulepri and -e $modulepri) {
1128                 my $modulepriname = basename($modulepri);
1129                 make_path($module_fwd, $lib, $verbose_level);
1130                 my $moduleprifwd = "$module_fwd/$modulepriname";
1131                 my $mod_base = $basedir;
1132                 my $mod_component_base = $developer_build ? $qtbasedir : $out_basedir;
1133                 open MODULE_PRI_FILE, ">$moduleprifwd" or die("Could not open $moduleprifwd for writing");
1134                 print MODULE_PRI_FILE "QT_MODULE_BASE = $mod_base\n";
1135                 print MODULE_PRI_FILE "QT_MODULE_BIN_BASE = $mod_component_base/bin\n";
1136                 print MODULE_PRI_FILE "QT_MODULE_INCLUDE_BASE = $out_basedir/include\n";
1137                 print MODULE_PRI_FILE "QT_MODULE_IMPORT_BASE = $mod_component_base/imports\n";
1138                 print MODULE_PRI_FILE "QT_MODULE_LIB_BASE = $mod_component_base/lib\n";
1139                 print MODULE_PRI_FILE "QT_MODULE_PLUGIN_BASE = $mod_component_base/plugins\n";
1140                 print MODULE_PRI_FILE "include($modulepri)\n";
1141                 close MODULE_PRI_FILE;
1142                 utime(time, (stat($modulepri))[9], $moduleprifwd);
1143             } elsif ($modulepri) {
1144                 print "$lib: WARNING: Module\'s pri file '$modulepri' not found.\n$lib: Skipped creating forwarding pri.\n";
1145             }
1146         }
1147     }
1148 }
1149 unless($showonly || !$create_uic_class_map) {
1150     my $class_lib_map = "$out_basedir/src/tools/uic/qclass_lib_map.h";
1151     if(-e $class_lib_map) {
1152         open CLASS_LIB_MAP, "<$class_lib_map";
1153         local $/;
1154         binmode CLASS_LIB_MAP;
1155         my $old_class_lib_map_contents = <CLASS_LIB_MAP>;
1156         close CLASS_LIB_MAP;
1157         $old_class_lib_map_contents =~ s/\r//g; # remove \r's , so comparison is ok on all platforms
1158         $class_lib_map = 0 if($old_class_lib_map_contents eq $class_lib_map_contents);
1159     }
1160     if($class_lib_map) {
1161         my $class_lib_map_dir = dirname($class_lib_map);
1162         make_path($class_lib_map_dir, "<outdir>", $verbose_level);
1163         open CLASS_LIB_MAP, ">$class_lib_map";
1164         print CLASS_LIB_MAP $class_lib_map_contents;
1165         close CLASS_LIB_MAP;
1166     }
1167 }
1168
1169 if($check_includes) {
1170     for my $lib (keys(%modules)) {
1171             #calc subdirs
1172             my @subdirs = ($modules{$lib});
1173             foreach my $subdir (@subdirs) {
1174                 opendir DIR, $subdir or die "Huh, directory ".$subdir." cannot be opened.";
1175                 foreach my $t (sort readdir(DIR)) {
1176                     push @subdirs, "$subdir/$t" if(-d "$subdir/$t" && !($t eq ".") &&
1177                                                    !($t eq "..") && !($t eq ".obj") &&
1178                                                    !($t eq ".moc") && !($t eq ".rcc") &&
1179                                                    !($t eq ".uic") && !($t eq "build"));
1180                 }
1181                 closedir DIR;
1182             }
1183
1184             foreach my $subdir (@subdirs) {
1185                 my $header_skip_qt_module_test = 0;
1186                 foreach(@ignore_for_qt_module_check) {
1187                     foreach (split(/;/, $_)) {
1188                         $header_skip_qt_module_test = 1 if ($subdir =~ /^$_/);
1189                     }
1190                 }
1191                 my @headers = findFiles($subdir, "^[-a-z0-9_]*\\.h\$" , 0);
1192                 foreach my $header (@headers) {
1193                     my $header_skip_qt_begin_header_test = 0;
1194                     my $header_skip_qt_begin_namespace_test = 0;
1195                     $header = 0 if($header =~ /^ui_.*.h/);
1196                     foreach (@ignore_headers) {
1197                         $header = 0 if($header eq $_);
1198                     }
1199                     if($header) {
1200                         my $public_header = $header;
1201                         if($public_header =~ /_p.h$/ || $public_header =~ /_pch.h$/) {
1202                             $public_header = 0;
1203                         } elsif ($public_header =~ /^qplatform/) {
1204                             $public_header = 0;
1205                         } else {
1206                             foreach (@ignore_for_master_contents) {
1207                                 $public_header = 0 if($header eq $_);
1208                             }
1209                             if($public_header) {
1210                                 foreach (@ignore_for_include_check) {
1211                                     $public_header = 0 if($header eq $_);
1212                                 }
1213                                 foreach(@ignore_for_qt_begin_header_check) {
1214                                     $header_skip_qt_begin_header_test = 1 if ($header eq $_);
1215                                 }
1216                                 foreach(@ignore_for_qt_begin_namespace_check) {
1217                                     $header_skip_qt_begin_namespace_test = 1 if ($header eq $_);
1218                                 }
1219                             }
1220                         }
1221
1222                         my $iheader = $subdir . "/" . $header;
1223                         if($public_header) {
1224                             if(open(F, "<$iheader")) {
1225                                 my $qt_begin_header_found = 0;
1226                                 my $qt_end_header_found = 0;
1227                                 my $qt_begin_namespace_found = 0;
1228                                 my $qt_end_namespace_found = 0;
1229                                 my $line;
1230                                 while($line = <F>) {
1231                                     chomp $line;
1232                                     my $output_line = 1;
1233                                     if($line =~ /^ *\# *pragma (qt_no_included_check|qt_sync_stop_processing)/) {
1234                                         last;
1235                                     } elsif($line =~ /^ *\# *include/) {
1236                                         my $include = $line;
1237                                         if($line =~ /<.*>/) {
1238                                             $include =~ s,.*<(.*)>.*,$1,;
1239                                         } elsif($line =~ /".*"/) {
1240                                             $include =~ s,.*"(.*)".*,$1,;
1241                                         } else {
1242                                             $include = 0;
1243                                         }
1244                                         if($include) {
1245                                             for my $trylib (keys(%modules)) {
1246                                                 if(-e "$out_basedir/include/$trylib/$include") {
1247                                                     print "$lib: WARNING: $iheader includes $include when it should include $trylib/$include\n";
1248                                                 }
1249                                             }
1250                                         }
1251                                     } elsif ($header_skip_qt_begin_header_test == 0 and $line =~ /^QT_BEGIN_HEADER\s*$/) {
1252                                         $qt_begin_header_found = 1;
1253                                     } elsif ($header_skip_qt_begin_header_test == 0 and $line =~ /^QT_END_HEADER\s*$/) {
1254                                         $qt_end_header_found = 1;
1255                                     } elsif ($header_skip_qt_begin_namespace_test == 0 and $line =~ /^QT_BEGIN_NAMESPACE\s*$/) {
1256                                         $qt_begin_namespace_found = 1;
1257                                     } elsif ($header_skip_qt_begin_namespace_test == 0 and $line =~ /^QT_END_NAMESPACE\s*$/) {
1258                                         $qt_end_namespace_found = 1;
1259                                     }
1260                                 }
1261                                 if ($header_skip_qt_begin_header_test == 0) {
1262                                     if ($qt_begin_header_found == 0) {
1263                                         print "$lib: WARNING: $iheader does not include QT_BEGIN_HEADER\n";
1264                                     }
1265
1266                                     if ($qt_begin_header_found && $qt_end_header_found == 0) {
1267                                         print "$lib: WARNING: $iheader has QT_BEGIN_HEADER but no QT_END_HEADER\n";
1268                                     }
1269                                 }
1270
1271                                 if ($header_skip_qt_begin_namespace_test == 0) {
1272                                     if ($qt_begin_namespace_found == 0) {
1273                                         print "$lib: WARNING: $iheader does not include QT_BEGIN_NAMESPACE\n";
1274                                     }
1275
1276                                     if ($qt_begin_namespace_found && $qt_end_namespace_found == 0) {
1277                                         print "$lib: WARNING: $iheader has QT_BEGIN_NAMESPACE but no QT_END_NAMESPACE\n";
1278                                     }
1279                                 }
1280
1281                                 close(F);
1282                             }
1283                         }
1284                     }
1285                 }
1286             }
1287     }
1288 }
1289
1290 exit 0;