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