Add module specific pris, and make syncqt create fwd includes
[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 = 1;
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     $dir =~ s=^$quoted_basedir/=$out_basedir/= if(!($basedir eq $out_basedir));
309     $file =~ s=\\=/=g;
310     $dir =~ s=\\=/=g;
311
312     #setup
313     my $ret = $file;
314     $ret =~ s,/cygdrive/([a-zA-Z])/,$1:/,g;
315     my $file_dir = dirname($file);
316     if($file_dir eq ".") {
317         $file_dir = getcwd();
318         $file_dir =~ s=\\=/=g;
319     }
320     $file_dir =~ s,/cygdrive/([a-zA-Z])/,$1:/,g;
321     if($dir eq ".") {
322         $dir = getcwd();
323         $dir =~ s=\\=/=g;
324     }
325     $dir =~ s,/cygdrive/([a-zA-Z])/,$1:/,g;
326     return basename($file) if($file_dir eq $dir);
327
328     #guts
329     my $match_dir = 0;
330     for(my $i = 1; $i < length($file_dir); $i++) {
331         my $slash = index($file_dir, "/", $i);
332         last if($slash == -1);
333         my $tmp = substr($file_dir, 0, $slash);
334         last unless($dir =~ m,^\Q$tmp\E/,);
335         $match_dir = $tmp;
336         $i = $slash;
337     }
338     if($match_dir) {
339         my $after = substr($dir, length($match_dir));
340         my $count = ($after =~ tr,/,,);
341         my $dots = "";
342         for(my $i = 0; $i < $count; $i++) {
343             $dots .= "../";
344         }
345         $ret =~ s,^\Q$match_dir\E,$dots,;
346     }
347     $ret =~ s,/+,/,g;
348     return $ret;
349 }
350
351 ######################################################################
352 # Syntax:  fileContents(filename)
353 # Params:  filename, string, filename of file to return contents
354 #
355 # Purpose: Get the contents of a file.
356 # Returns: String with contents of the file, or empty string if file
357 #          doens't exist.
358 # Warning: Dies if it does exist but script cannot get read access.
359 ######################################################################
360 sub fileContents {
361     my ($filename) = @_;
362     my $filecontents = "";
363     if (-e $filename) {
364         open(I, "< $filename") || die "Could not open $filename for reading, read block?";
365         local $/;
366         binmode I;
367         $filecontents = <I>;
368         close I;
369     }
370     return $filecontents;
371 }
372
373 ######################################################################
374 # Syntax:  fileCompare(file1, file2)
375 # Params:  file1, string, filename of first file
376 #          file2, string, filename of second file
377 #
378 # Purpose: Determines if files are equal, and which one is newer.
379 # Returns: 0 if files are equal no matter the timestamp, -1 if file1
380 #          is newer, 1 if file2 is newer.
381 ######################################################################
382 sub fileCompare {
383     my ($file1, $file2) = @_;
384     my $file1contents = fileContents($file1);
385     my $file2contents = fileContents($file2);
386     if (! -e $file1) { return 1; }
387     if (! -e $file2) { return -1; }
388     return $file1contents ne $file2contents ? (stat($file2))[9] <=> (stat($file1))[9] : 0;
389 }
390
391 ######################################################################
392 # Syntax:  copyFile(file, ifile)
393 # Params:  file, string, filename to create duplicate for
394 #          ifile, string, destination name of duplicate
395 #
396 # Purpose: Keeps files in sync so changes in the newer file will be
397 #          written to the other.
398 # Returns: 1 if files were synced, else 0.
399 # Warning: Dies if script cannot get write access.
400 ######################################################################
401 sub copyFile
402 {
403     my ($file,$ifile, $copy,$knowdiff,$filecontents,$ifilecontents) = @_;
404     # Bi-directional synchronization
405     open( I, "< " . $file ) || die "Could not open $file for reading";
406     local $/;
407     binmode I;
408     $filecontents = <I>;
409     close I;
410     if ( open(I, "< " . $ifile) ) {
411         local $/;
412         binmode I;
413         $ifilecontents = <I>;
414         close I;
415         $copy = fileCompare($file, $ifile);
416         $knowdiff = 0,
417     } else {
418         $copy = -1;
419         $knowdiff = 1;
420     }
421
422     if ( $knowdiff || ($filecontents ne $ifilecontents) ) {
423         if ( $copy > 0 ) {
424             my $file_dir = dirname($file);
425             mkpath $file_dir, !$quiet unless(-e $file_dir);
426             open(O, "> " . $file) || die "Could not open $file for writing (no write permission?)";
427             local $/;
428             binmode O;
429             print O $ifilecontents;
430             close O;
431             utime time, (stat($ifile))[9], $file;
432             return 1;
433         } elsif ( $copy < 0 ) {
434             my $ifile_dir = dirname($ifile);
435             mkpath $ifile_dir, !$quiet unless(-e $ifile_dir);
436             open(O, "> " . $ifile) || die "Could not open $ifile for writing (no write permission?)";
437             local $/;
438             binmode O;
439             print O $filecontents;
440             close O;
441             utime time, (stat($file))[9], $ifile;
442             return 1;
443         }
444     }
445     return 0;
446 }
447
448 ######################################################################
449 # Syntax:  symlinkFile(file, ifile)
450 # Params:  file, string, filename to create "symlink" for
451 #          ifile, string, destination name of symlink
452 #
453 # Purpose: File is symlinked to ifile (or copied if filesystem doesn't
454 #          support symlink).
455 # Returns: 1 on success, else 0.
456 ######################################################################
457 sub symlinkFile
458 {
459     my ($file,$ifile) = @_;
460
461     if ($isunix) {
462         print "symlink created for $file " unless $quiet;
463         if ( $force_relative && ($ifile =~ /^$quoted_basedir/)) {
464             my $t = getcwd();
465             my $c = -1;
466             my $p = "../";
467             $t =~ s-^$quoted_basedir/--;
468             $p .= "../" while( ($c = index( $t, "/", $c + 1)) != -1 );
469             $file =~ s-^$quoted_basedir/-$p-;
470             print " ($file)\n" unless $quiet;
471         }
472         print "\n" unless $quiet;
473         return symlink($file, $ifile);
474     }
475     return copyFile($file, $ifile);
476 }
477
478 ######################################################################
479 # Syntax:  findFiles(dir, match, descend)
480 # Params:  dir, string, directory to search for name
481 #          match, string, regular expression to match in dir
482 #          descend, integer, 0 = non-recursive search
483 #                            1 = recurse search into subdirectories
484 #
485 # Purpose: Finds files matching a regular expression.
486 # Returns: List of matching files.
487 #
488 # Examples:
489 #   findFiles("/usr","\.cpp$",1)  - finds .cpp files in /usr and below
490 #   findFiles("/tmp","^#",0)      - finds #* files in /tmp
491 ######################################################################
492 sub findFiles {
493     my ($dir,$match,$descend) = @_;
494     my ($file,$p,@files);
495     local(*D);
496     $dir =~ s=\\=/=g;
497     ($dir eq "") && ($dir = ".");
498     if ( opendir(D,$dir) ) {
499         if ( $dir eq "." ) {
500             $dir = "";
501         } else {
502             ($dir =~ /\/$/) || ($dir .= "/");
503         }
504         foreach $file ( sort readdir(D) ) {
505             next if ( $file  =~ /^\.\.?$/ );
506             $p = $file;
507             ($file =~ /$match/) && (push @files, $p);
508             if ( $descend && -d $p && ! -l $p ) {
509                 push @files, &findFiles($p,$match,$descend);
510             }
511         }
512         closedir(D);
513     }
514     return @files;
515 }
516
517 ######################################################################
518 # Syntax:  loadSyncProfile()
519 #
520 # Purpose: Locates the sync.profile.
521 # Returns: Hashmap of module name -> directory.
522 ######################################################################
523 sub loadSyncProfile {
524     my ($srcbase, $outbase) = @_;
525     print("srcbase = $$srcbase \n");
526     print("outbase = $$outbase \n");
527
528     my $syncprofile = "$$srcbase/sync.profile";
529     my $result;
530     unless ($result = do "$syncprofile") {
531         die "syncqt couldn't parse $syncprofile: $@" if $@;
532         die "syncqt couldn't execute $syncprofile: $!" unless defined $result;
533     }
534     return $result;
535 }
536
537 sub locateSyncProfile
538 {
539     my ($directory) = @_;
540     my $syncprofile;
541     $directory = abs_path($directory);
542     while(!defined $syncprofile) {
543         local(*D);
544         if (opendir(D, $directory)) {
545             foreach my $file (sort readdir(D)) {
546                 next if ($file =~ /^\.\.?$/);
547                 $syncprofile = "$directory/$file" if ($file =~ /^sync\.profile$/);
548                 last if (defined $syncprofile);
549             }
550             closedir(D);
551         }
552         last if (defined $syncprofile || $directory eq "/" || $directory =~ /^?:[\/\\]$/);
553         $directory = dirname($directory);
554     }
555     return $syncprofile;
556 }
557
558 # check if this is an in-source build, and if so use that as the basedir too
559 $basedir = locateSyncProfile($out_basedir);
560 $basedir = dirname($basedir) if ($basedir);
561 $quoted_basedir = "\Q$basedir";
562
563 # --------------------------------------------------------------------
564 # "main" function
565 # --------------------------------------------------------------------
566
567 while ( @ARGV ) {
568     my $var = 0;
569     my $val = 0;
570
571     #parse
572     my $arg = shift @ARGV;
573     if ($arg eq "-h" || $arg eq "-help" || $arg eq "-?" || $arg eq "?") {
574         $var = "show_help";
575         $val = "yes";
576     } elsif($arg eq "-copy") {
577         $var = "copy";
578         $val = "yes";
579     } elsif($arg eq "-o" || $arg eq "-outdir") {
580         $var = "output";
581         $val = shift @ARGV;
582     } elsif($arg eq "-showonly" || $arg eq "-remove-stale" || $arg eq "-windows" ||
583             $arg eq "-relative" || $arg eq "-check-includes") {
584         $var = substr($arg, 1);
585         $val = "yes";
586     } elsif($arg =~ /^-no-(.*)$/) {
587         $var = $1;
588         $val = "no";
589         #these are for commandline compat
590     } elsif($arg eq "-inc") {
591         $var = "output";
592         $val = shift @ARGV;
593     } elsif($arg eq "-module") {
594         $var = "module";
595         $val = shift @ARGV;
596     } elsif($arg eq "-separate-module") {
597         $var = "separate-module";
598         $val = shift @ARGV;
599     } elsif($arg eq "-show") {
600         $var = "showonly";
601         $val = "yes";
602     } elsif($arg eq "-quiet") {
603         $var = "quiet";
604         $val = "yes";
605     } elsif($arg eq "-private") {
606         $var = "create_private_headers";
607         $val = "yes";
608     } elsif($arg eq "-qtdir") {
609         $var = "qtdir";
610         $val = shift @ARGV;
611     } elsif($arg eq "-base-dir") {
612         # skip, it's been dealt with at the top of the file
613         shift @ARGV;
614         next;
615     } elsif($arg eq "-no-module-fwd") {
616         $var = "no_module_fwd";
617         $val = "yes";
618     } elsif($arg =~/^-/) {
619         print "Unknown option: $arg\n\n" if(!$var);
620         showUsage();
621     } else {
622         $basedir = locateSyncProfile($arg);
623         die "Could not find a sync.profile for '$arg'\n" if (!$basedir);
624         $basedir = dirname($basedir);
625         $basedir =~ s=\\=/=g;
626         $var = "ignore";
627     }
628
629     #do something
630     if(!$var || $var eq "show_help") {
631         print "Unknown option: $arg\n\n" if(!$var);
632         showUsage();
633     } elsif ($var eq "copy") {
634         if($val eq "yes") {
635             $copy_headers++;
636         } elsif($showonly) {
637             $copy_headers--;
638         }
639     } elsif ($var eq "showonly") {
640         if($val eq "yes") {
641             $showonly++;
642         } elsif($showonly) {
643             $showonly--;
644         }
645     } elsif ($var eq "quiet") {
646         if($val eq "yes") {
647             $quiet++;
648         } elsif($quiet) {
649             $quiet--;
650         }
651     } elsif ($var eq "check-includes") {
652         if($val eq "yes") {
653             $check_includes++;
654         } elsif($check_includes) {
655             $check_includes--;
656         }
657     } elsif ($var eq "remove-stale") {
658         if($val eq "yes") {
659             $remove_stale++;
660         } elsif($remove_stale) {
661             $remove_stale--;
662         }
663     } elsif ($var eq "windows") {
664         if($val eq "yes") {
665             $force_win++;
666         } elsif($force_win) {
667             $force_win--;
668         }
669     } elsif ($var eq "relative") {
670         if($val eq "yes") {
671             $force_relative++;
672         } elsif($force_relative) {
673             $force_relative--;
674         }
675     } elsif ($var eq "module") {
676         print "module :$val:\n" unless $quiet;
677         die "No such module: $val" unless(defined $modules{$val});
678         push @modules_to_sync, $val;
679     } elsif ($var eq "separate-module") {
680         my ($module, $prodir, $headerdir) = split(/:/, $val);
681         $modules{$module} = $prodir;
682         push @modules_to_sync, $module;
683         $moduleheaders{$module} = $headerdir;
684         $create_uic_class_map = 0;
685     } elsif ($var eq "qtdir") {
686         if($val) {
687             $qtbasedir = $val;
688             $qtbasedir =~ s=\\=/=g;
689         } else {
690             die "The -qtdir option requires an argument";
691         }
692     } elsif ($var eq "no_module_fwd") {
693         $no_module_fwd = 1;
694     } elsif ($var eq "output") {
695         my $outdir = $val;
696         if(checkRelative($outdir)) {
697             $out_basedir = getcwd();
698             chomp $out_basedir;
699             $out_basedir .= "/" . $outdir;
700         } else {
701             $out_basedir = $outdir;
702         }
703         # \ -> /
704         $out_basedir =~ s=\\=/=g;
705     }
706 }
707
708 # if the $qtbasedir neither has 'qtbase' somewhere in its path, nor a
709 # '.qmake.cache' file in its directory, we assume it's not a valid path
710 # (remember that a yet-to-be-built qtbase doesn't have this file either,
711 # thus the 'qtbase' path check!)
712 die "Cannot automatically detect/use provided path to QtBase's build directory!\n" .
713     "QTDIR detected/provided: " . (defined $qtbasedir ? $qtbasedir : "-none-") . "\n" .
714     "Please -qtdir option to provide the correct path.\nsyncqt failed"
715         if (!defined $qtbasedir || (!-e "$qtbasedir/.qmake.cache" && $qtbasedir !~ /qtbase/));
716
717 # if we have no $basedir we cannot be sure which sources you want, so die
718 die "Could not find any sync.profile for your module!\nPass <module directory> to syncqt to sync your header files.\nsyncqt failed" if (!$basedir);
719
720 # load the module's sync.profile here, before we can
721 loadSyncProfile(\$basedir, \$out_basedir);
722
723 @modules_to_sync = keys(%modules) if($#modules_to_sync == -1);
724
725 $isunix = checkUnix; #cache checkUnix
726
727 # create path
728 mkpath "$out_basedir/include", !$quiet;
729 mkpath "$out_basedir/include/Qt", !$quiet;
730
731 my @ignore_headers = ();
732 my $class_lib_map_contents = "";
733 our @ignore_for_master_contents = ();
734 our @ignore_for_include_check = ();
735 our @ignore_for_qt_begin_header_check = ();
736 our @ignore_for_qt_begin_namespace_check = ();
737 our @ignore_for_qt_module_check = ();
738 my %colliding_headers = ();
739 my %inject_headers = ( "$basedir/src/corelib/global" => ( "qconfig.h" ) ); # all from build dir
740
741 foreach my $lib (@modules_to_sync) {
742     #iteration info
743     my $dir = $modules{$lib};
744     my $pathtoheaders = "";
745     $pathtoheaders = $moduleheaders{$lib} if ($moduleheaders{$lib});
746
747     #information used after the syncing
748     my $pri_install_classes = "";
749     my $pri_install_files = "";
750     my $pri_install_pfiles = "";
751
752     my $libcapitals = $lib;
753     $libcapitals =~ y/a-z/A-Z/;
754     my $master_contents = "#ifndef QT_".$libcapitals."_MODULE_H\n#define QT_".$libcapitals."_MODULE_H\n";
755
756     #get dependencies
757     if(-e "$dir/" . basename($dir) . ".pro") {
758         if(open(F, "<$dir/" . basename($dir) . ".pro")) {
759             while(my $line = <F>) {
760                 chomp $line;
761                 if($line =~ /^ *QT *\+?= *([^\r\n]*)/) {
762                     foreach(split(/ /, $1)) {
763                         my $content = $mastercontent{$_};
764                         $master_contents .= $content if ($content);
765                     }
766                 }
767             }
768             close(F);
769         }
770     }
771
772     #remove the old files
773     if($remove_stale) {
774         my @subdirs = ("$out_basedir/include/$lib");
775         foreach my $subdir (@subdirs) {
776             if (opendir DIR, $subdir) {
777                 while(my $t = readdir(DIR)) {
778                     my $file = "$subdir/$t";
779                     if(-d $file) {
780                         push @subdirs, $file unless($t eq "." || $t eq "..");
781                     } else {
782                         my @files = ($file);
783                         #push @files, "$out_basedir/include/Qt/$t" if(-e "$out_basedir/include/Qt/$t");
784                         foreach my $file (@files) {
785                            my $remove_file = 0;
786                            if(open(F, "<$file")) {
787                                 while(my $line = <F>) {
788                                     chomp $line;
789                                     if($line =~ /^\#include \"([^\"]*)\"$/) {
790                                         my $include = $1;
791                                         $include = $subdir . "/" . $include unless(substr($include, 0, 1) eq "/");
792                                         $remove_file = 1 unless(-e $include);
793                                     } else {
794                                         $remove_file = 0;
795                                         last;
796                                     }
797                                 }
798                                 close(F);
799                                 unlink $file if($remove_file);
800                             }
801                         }
802                     }
803                 }
804                 closedir DIR;
805             }
806
807         }
808     }
809
810     #create the new ones
811     foreach my $current_dir (split(/;/, $dir)) {
812         my $headers_dir = $current_dir;
813         $headers_dir .= "/$pathtoheaders" if ($pathtoheaders);
814         #calc subdirs
815         my @subdirs = ($headers_dir);
816         foreach my $subdir (@subdirs) {
817             opendir DIR, $subdir or next;
818             while(my $t = readdir(DIR)) {
819                 push @subdirs, "$subdir/$t" if(-d "$subdir/$t" && !($t eq ".") &&
820                                                !($t eq "..") && !($t eq ".obj") &&
821                                                !($t eq ".moc") && !($t eq ".rcc") &&
822                                                !($t eq ".uic") && !($t eq "build"));
823             }
824             closedir DIR;
825         }
826
827         #calc files and "copy" them
828         foreach my $subdir (@subdirs) {
829             my @headers = findFiles($subdir, "^[-a-z0-9_]*\\.h\$" , 0);
830             if (defined $inject_headers{$subdir}) {
831                 foreach my $if ($inject_headers{$subdir}) {
832                     @headers = grep(!/^\Q$if\E$/, @headers); #in case we configure'd previously
833                     push @headers, "*".$if;
834                 }
835             }
836             foreach my $header (@headers) {
837                 my $shadow = ($header =~ s/^\*//);
838                 $header = 0 if($header =~ /^ui_.*.h/);
839                 foreach (@ignore_headers) {
840                     $header = 0 if($header eq $_);
841                 }
842                 if($header) {
843                     my $header_copies = 0;
844                     #figure out if it is a public header
845                     my $public_header = $header;
846                     if($public_header =~ /_p.h$/ || $public_header =~ /_pch.h$/) {
847                         $public_header = 0;
848                     } else {
849                         foreach (@ignore_for_master_contents) {
850                             $public_header = 0 if($header eq $_);
851                         }
852                     }
853
854                     my $iheader = $subdir . "/" . $header;
855                     $iheader =~ s/^\Q$basedir\E/$out_basedir/ if ($shadow);
856                     my @classes = $public_header ? classNames($iheader) : ();
857                     if($showonly) {
858                         print "$header [$lib]\n";
859                         foreach(@classes) {
860                             print "SYMBOL: $_\n";
861                         }
862                     } else {
863                         my $ts = (stat($iheader))[9];
864                         #find out all the places it goes..
865                         my @headers;
866                         if ($public_header) {
867                             @headers = ( "$out_basedir/include/$lib/$header" );
868
869                             # write forwarding headers to include/Qt
870                             if ($lib ne "phonon" && $subdir =~ /^$quoted_basedir\/src/) {
871                                 my $file_name = "$out_basedir/include/Qt/$header";
872                                 my $file_op = '>';
873                                 my $header_content = '';
874                                 if (exists $colliding_headers{$file_name}) {
875                                     $file_op = '>>';
876                                 } else {
877                                     $colliding_headers{$file_name} = 1;
878                                     my $warning_msg = 'Inclusion of header files from include/Qt is deprecated.';
879                                     $header_content = "#ifndef QT_NO_QT_INCLUDE_WARN\n" .
880                                                       "    #if defined(__GNUC__)\n" .
881                                                       "        #warning \"$warning_msg\"\n" .
882                                                       "    #elif defined(_MSC_VER)\n" .
883                                                       "        #pragma message(\"WARNING: $warning_msg\")\n" .
884                                                       "    #endif\n".
885                                                       "#endif\n\n";
886                                 }
887                                 $header_content .= '#include "' . "../$lib/$header" . "\"\n";
888                                 open HEADERFILE, $file_op, $file_name or die "unable to open '$file_name' : $!\n";
889                                 print HEADERFILE $header_content;
890                                 close HEADERFILE;
891                             }
892
893                             foreach my $full_class (@classes) {
894                                 my $header_base = basename($header);
895                                 # Strip namespaces:
896                                 my $class = $full_class;
897                                 $class =~ s/^.*:://;
898 #                               if ($class =~ m/::/) {
899 #                                  class =~ s,::,/,g;
900 #                               }
901                                 $class_lib_map_contents .= "QT_CLASS_LIB($full_class, $lib, $header_base)\n";
902                                 $header_copies++ if(syncHeader("$out_basedir/include/$lib/$class", "$out_basedir/include/$lib/$header", 0, $ts));
903
904                                 # KDE-Compat headers for Phonon
905                                 if ($lib eq "phonon") {
906                                     $header_copies++ if (syncHeader("$out_basedir/include/phonon_compat/Phonon/$class", "$out_basedir/include/$lib/$header", 0, $ts));
907                                 }
908                             }
909                         } elsif ($create_private_headers) {
910                             @headers = ( "$out_basedir/include/$lib/private/$header" );
911                         }
912                         foreach(@headers) { #sync them
913                             $header_copies++ if(syncHeader($_, $iheader, $copy_headers && !$shadow, $ts));
914                         }
915
916                         if($public_header) {
917                             #put it into the master file
918                             $master_contents .= "#include \"$public_header\"\n" if(shouldMasterInclude($iheader));
919
920                             #deal with the install directives
921                             if($public_header) {
922                                 my $pri_install_iheader = fixPaths($iheader, $current_dir);
923                                 foreach my $class (@classes) {
924                                     # Strip namespaces:
925                                     $class =~ s/^.*:://;
926 #                                   if ($class =~ m/::/) {
927 #                                       $class =~ s,::,/,g;
928 #                                   }
929                                     my $class_header = fixPaths("$out_basedir/include/$lib/$class",
930                                                                 $current_dir) . " ";
931                                     $pri_install_classes .= $class_header
932                                                                 unless($pri_install_classes =~ $class_header);
933                                 }
934                                 $pri_install_files.= "$pri_install_iheader ";;
935                             }
936                         }
937                         else {
938                             my $pri_install_iheader = fixPaths($iheader, $current_dir);
939                             $pri_install_pfiles.= "$pri_install_iheader ";;
940                         }
941                     }
942                     print "header created for $iheader ($header_copies)\n" if($header_copies > 0 && !$quiet);
943                 }
944             }
945         }
946     }
947
948     # close the master include:
949     $master_contents .= "#endif\n";
950
951     unless($showonly) {
952         my @master_includes;
953         push @master_includes, "$out_basedir/include/$lib/$lib";
954         push @master_includes, "$out_basedir/include/phonon_compat/Phonon/Phonon" if ($lib eq "phonon");
955         foreach my $master_include (@master_includes) {
956             #generate the "master" include file
957             my @tmp = split(/;/,$modules{$lib});
958             $pri_install_files .= fixPaths($master_include, $tmp[0]) . " "; #get the master file installed too
959             if($master_include && -e $master_include) {
960                 open MASTERINCLUDE, "<$master_include";
961                 local $/;
962                 binmode MASTERINCLUDE;
963                 my $oldmaster = <MASTERINCLUDE>;
964                 close MASTERINCLUDE;
965                 $oldmaster =~ s/\r//g; # remove \r's , so comparison is ok on all platforms
966                 $master_include = 0 if($oldmaster eq $master_contents);
967             }
968             if($master_include && $master_contents) {
969                 my $master_dir = dirname($master_include);
970                 mkpath $master_dir, !$quiet;
971                 print "header (master) created for $lib\n" unless $quiet;
972                 open MASTERINCLUDE, ">$master_include";
973                 print MASTERINCLUDE $master_contents;
974                 close MASTERINCLUDE;
975             }
976         }
977
978         #handle the headers.pri for each module
979         my $headers_pri_contents = "";
980         $headers_pri_contents .= "SYNCQT.HEADER_FILES = $pri_install_files\n";
981         $headers_pri_contents .= "SYNCQT.HEADER_CLASSES = $pri_install_classes\n";
982         $headers_pri_contents .= "SYNCQT.PRIVATE_HEADER_FILES = $pri_install_pfiles\n";
983         my $headers_pri_file = "$out_basedir/include/$lib/headers.pri";
984         if(-e $headers_pri_file) {
985             open HEADERS_PRI_FILE, "<$headers_pri_file";
986             local $/;
987             binmode HEADERS_PRI_FILE;
988             my $old_headers_pri_contents = <HEADERS_PRI_FILE>;
989             close HEADERS_PRI_FILE;
990             $old_headers_pri_contents =~ s/\r//g; # remove \r's , so comparison is ok on all platforms
991             $headers_pri_file = 0 if($old_headers_pri_contents eq $headers_pri_contents);
992         }
993         if($headers_pri_file && $master_contents) {
994             my $headers_pri_dir = dirname($headers_pri_file);
995             mkpath $headers_pri_dir, !$quiet;
996             print "headers.pri file created for $lib\n" unless $quiet;
997             open HEADERS_PRI_FILE, ">$headers_pri_file";
998             print HEADERS_PRI_FILE $headers_pri_contents;
999             close HEADERS_PRI_FILE;
1000         }
1001
1002         # create forwarding module pri in qtbase/mkspecs/modules
1003         unless ($no_module_fwd) {
1004             my $modulepri = $modulepris{$lib};
1005             if (-e $modulepri) {
1006                 my $modulepriname = basename($modulepri);
1007                 my $moduleprifwd = "$qtbasedir/mkspecs/modules/$modulepriname";
1008                 open MODULE_PRI_FILE, ">$moduleprifwd";
1009                 print MODULE_PRI_FILE "QT_MODULE_INCLUDE_BASE = $out_basedir/include\n";
1010                 print MODULE_PRI_FILE "QT_MODULE_LIB_BASE = $out_basedir/lib\n";
1011                 print MODULE_PRI_FILE "include($modulepri)\n";
1012                 close MODULE_PRI_FILE;
1013                 utime(time, (stat($modulepri))[9], $moduleprifwd);
1014             } elsif ($modulepri) {
1015                 print "WARNING: Module $lib\'s pri file '$modulepri' not found.\nSkipped creating forwarding pri for $lib.\n";
1016             }
1017         }
1018     }
1019 }
1020 unless($showonly || !$create_uic_class_map) {
1021     my $class_lib_map = "$out_basedir/src/tools/uic/qclass_lib_map.h";
1022     if(-e $class_lib_map) {
1023         open CLASS_LIB_MAP, "<$class_lib_map";
1024         local $/;
1025         binmode CLASS_LIB_MAP;
1026         my $old_class_lib_map_contents = <CLASS_LIB_MAP>;
1027         close CLASS_LIB_MAP;
1028         $old_class_lib_map_contents =~ s/\r//g; # remove \r's , so comparison is ok on all platforms
1029         $class_lib_map = 0 if($old_class_lib_map_contents eq $class_lib_map_contents);
1030     }
1031     if($class_lib_map) {
1032         my $class_lib_map_dir = dirname($class_lib_map);
1033         mkpath $class_lib_map_dir, !$quiet;
1034         open CLASS_LIB_MAP, ">$class_lib_map";
1035         print CLASS_LIB_MAP $class_lib_map_contents;
1036         close CLASS_LIB_MAP;
1037     }
1038 }
1039
1040 if($check_includes) {
1041     for my $lib (keys(%modules)) {
1042             #calc subdirs
1043             my @subdirs = ($modules{$lib});
1044             foreach my $subdir (@subdirs) {
1045                 opendir DIR, $subdir or die "Huh, directory ".$subdir." cannot be opened.";
1046                 while(my $t = readdir(DIR)) {
1047                     push @subdirs, "$subdir/$t" if(-d "$subdir/$t" && !($t eq ".") &&
1048                                                    !($t eq "..") && !($t eq ".obj") &&
1049                                                    !($t eq ".moc") && !($t eq ".rcc") &&
1050                                                    !($t eq ".uic") && !($t eq "build"));
1051                 }
1052                 closedir DIR;
1053             }
1054
1055             foreach my $subdir (@subdirs) {
1056                 my $header_skip_qt_module_test = 0;
1057                 foreach(@ignore_for_qt_module_check) {
1058                     foreach (split(/;/, $_)) {
1059                         $header_skip_qt_module_test = 1 if ($subdir =~ /^$_/);
1060                     }
1061                 }
1062                 my @headers = findFiles($subdir, "^[-a-z0-9_]*\\.h\$" , 0);
1063                 foreach my $header (@headers) {
1064                     my $header_skip_qt_begin_header_test = 0;
1065                     my $header_skip_qt_begin_namespace_test = 0;
1066                     $header = 0 if($header =~ /^ui_.*.h/);
1067                     foreach (@ignore_headers) {
1068                         $header = 0 if($header eq $_);
1069                     }
1070                     if($header) {
1071                         my $public_header = $header;
1072                         if($public_header =~ /_p.h$/ || $public_header =~ /_pch.h$/) {
1073                             $public_header = 0;
1074                         } else {
1075                             foreach (@ignore_for_master_contents) {
1076                                 $public_header = 0 if($header eq $_);
1077                             }
1078                             if($public_header) {
1079                                 foreach (@ignore_for_include_check) {
1080                                     $public_header = 0 if($header eq $_);
1081                                 }
1082                                 foreach(@ignore_for_qt_begin_header_check) {
1083                                     $header_skip_qt_begin_header_test = 1 if ($header eq $_);
1084                                 }
1085                                 foreach(@ignore_for_qt_begin_namespace_check) {
1086                                     $header_skip_qt_begin_namespace_test = 1 if ($header eq $_);
1087                                 }
1088                             }
1089                         }
1090
1091                         my $iheader = $subdir . "/" . $header;
1092                         if($public_header) {
1093                             if(open(F, "<$iheader")) {
1094                                 my $qt_module_found = 0;
1095                                 my $qt_begin_header_found = 0;
1096                                 my $qt_end_header_found = 0;
1097                                 my $qt_begin_namespace_found = 0;
1098                                 my $qt_end_namespace_found = 0;
1099                                 my $line;
1100                                 while($line = <F>) {
1101                                     chomp $line;
1102                                     my $output_line = 1;
1103                                     if($line =~ /^ *\# *pragma (qt_no_included_check|qt_sync_stop_processing)/) {
1104                                         last;
1105                                     } elsif($line =~ /^ *\# *include/) {
1106                                         my $include = $line;
1107                                         if($line =~ /<.*>/) {
1108                                             $include =~ s,.*<(.*)>.*,$1,;
1109                                         } elsif($line =~ /".*"/) {
1110                                             $include =~ s,.*"(.*)".*,$1,;
1111                                         } else {
1112                                             $include = 0;
1113                                         }
1114                                         if($include) {
1115                                             for my $trylib (keys(%modules)) {
1116                                                 if(-e "$out_basedir/include/$trylib/$include") {
1117                                                     print "WARNING: $iheader includes $include when it should include $trylib/$include\n";
1118                                                 }
1119                                             }
1120                                         }
1121                                     } elsif ($header_skip_qt_begin_header_test == 0 and $line =~ /^QT_BEGIN_HEADER\s*$/) {
1122                                         $qt_begin_header_found = 1;
1123                                     } elsif ($header_skip_qt_begin_header_test == 0 and $line =~ /^QT_END_HEADER\s*$/) {
1124                                         $qt_end_header_found = 1;
1125                                     } elsif ($header_skip_qt_begin_namespace_test == 0 and $line =~ /^QT_BEGIN_NAMESPACE\s*$/) {
1126                                         $qt_begin_namespace_found = 1;
1127                                     } elsif ($header_skip_qt_begin_namespace_test == 0 and $line =~ /^QT_END_NAMESPACE\s*$/) {
1128                                         $qt_end_namespace_found = 1;
1129                                     } elsif ($header_skip_qt_module_test == 0 and $line =~ /^QT_MODULE\(.*\)\s*$/) {
1130                                         $qt_module_found = 1;
1131                                     }
1132                                 }
1133                                 if ($header_skip_qt_begin_header_test == 0) {
1134                                     if ($qt_begin_header_found == 0) {
1135                                         print "WARNING: $iheader does not include QT_BEGIN_HEADER\n";
1136                                     }
1137
1138                                     if ($qt_begin_header_found && $qt_end_header_found == 0) {
1139                                         print "WARNING: $iheader has QT_BEGIN_HEADER but no QT_END_HEADER\n";
1140                                     }
1141                                 }
1142
1143                                 if ($header_skip_qt_begin_namespace_test == 0) {
1144                                     if ($qt_begin_namespace_found == 0) {
1145                                         print "WARNING: $iheader does not include QT_BEGIN_NAMESPACE\n";
1146                                     }
1147
1148                                     if ($qt_begin_namespace_found && $qt_end_namespace_found == 0) {
1149                                         print "WARNING: $iheader has QT_BEGIN_NAMESPACE but no QT_END_NAMESPACE\n";
1150                                     }
1151                                 }
1152
1153                                 if ($header_skip_qt_module_test == 0) {
1154                                     if ($qt_module_found == 0) {
1155                                         print "WARNING: $iheader does not include QT_MODULE\n";
1156                                     }
1157                                 }
1158                                 close(F);
1159                             }
1160                         }
1161                     }
1162                 }
1163             }
1164     }
1165 }
1166
1167 exit 0;