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