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