1#!/usr/bin/perl
2
3# Expands the specialised KDE tags in Makefile.in to (hopefully) valid
4# make syntax.
5# When called without file parameters, we work recursively on all Makefile.in
6# in and below the current subdirectory. When called with file parameters,
7# only those Makefile.in are changed.
8# The currently supported tags are
9#
10# {program}_METASOURCES
11# where you have a choice of two styles
12#   {program}_METASOURCES = name1.moc name2.moc ... [\]
13#   {program}_METASOURCES = AUTO
14#       The second style requires other tags as well.
15#
16# To install icons :
17#    KDE_ICON = iconname iconname2 ...
18#    KDE_ICON = AUTO
19#
20# For documentation :
21#    ...
22#
23# and more new tags TBD!
24#
25# The concept (and base code) for this program came from automoc,
26# supplied by the following
27#
28# Matthias Ettrich <ettrich@kde.org>      (The originator)
29# Kalle Dalheimer <kalle@kde.org>      (The original implementator)
30# Harri Porten  <porten@tu-harburg.de>
31# Alex Zepeda  <jazepeda@pacbell.net>
32# David Faure <faure@kde.org>
33# Stephan Kulow <coolo@kde.org>
34#
35# I've puddled around with automoc and produced something different
36# 1999-02-01 John Birch <jb.nz@writeme.com>
37#       * Rewritten automoc to cater for more than just moc file expansion
38#         Version 0.01 does the same as automoc at this stage.
39# 1999-02-18 jb
40#       * We must always write a Makefile.in file out even if we fail
41#         because we need the "perl autokmake" in the AUTOMAKE so that a
42#         "make" will regenerate the Makefile.in correctly.
43#         Reworked moc file checking so that missing includes in cpp
44#         will work and includes in cpp when using use_automoc will also
45#         work.
46# 1999-02-23 jb
47#       * Added POFILE processing and changed the USE_AUTOMOC tag to
48#         AUTO instead.
49# ... See ChangeLog for more logs
50
51use Cwd;
52use File::Find;
53use File::Basename;
54
55# Prototype the functions
56sub initialise ();
57sub processMakefile ($);
58sub updateMakefile ();
59sub restoreMakefile ();
60
61sub removeLine ($$);
62sub appendLines ($);
63sub substituteLine ($$);
64
65sub findMocCandidates ();
66sub pruneMocCandidates ($);
67sub checkMocCandidates ();
68sub addMocRules ();
69
70sub tag_AUTOMAKE ();
71sub tag_META_INCLUDES ();
72sub tag_METASOURCES ();
73sub tag_POFILES ();
74sub tag_DOCFILES ();
75sub tag_LOCALINSTALL();
76sub tag_IDLFILES();
77sub tag_UIFILES();
78sub tag_SUBDIRS();
79sub tag_ICON();
80sub tag_CLOSURE();
81sub tag_DIST();
82
83# Some global globals...
84$verbose    = 0;        # a debug flag
85$thisProg   = "$0";     # This programs name
86$topdir     = cwd();    # The current directory
87@makefiles  = ();       # Contains all the files we'll process
88@foreignfiles = ();
89$start      = (times)[0]; # some stats for testing - comment out for release
90$version    = "v0.2";
91$errorflag  = 0;
92$cppExt     = "(cpp|cc|cxx|C|c\\+\\+)";
93$hExt       = "(h|H|hh|hxx|hpp|h\\+\\+)";
94$progId     = "KDE tags expanded automatically by " . basename($thisProg);
95$automkCall = "\n";
96$printname  = "";  # used to display the directory the Makefile is in
97$use_final  = 1;        # create code for --enable-final
98$cleantarget = "clean";
99$dryrun     = 0;
100$pathoption = 0;
101
102while (defined ($ARGV[0]))
103{
104    $_ = shift;
105    if (/^--version$/)
106    {
107        print STDOUT "\n";
108        print STDOUT basename($thisProg), " $version\n",
109                "This is really free software, unencumbered by the GPL.\n",
110                "You can do anything you like with it except sueing me.\n",
111                "Copyright 1998 Kalle Dalheimer <kalle\@kde.org>\n",
112                "Concept, design and unnecessary questions about perl\n",
113                "       by Matthias Ettrich <ettrich\@kde.org>\n\n",
114                "Making it useful by Stephan Kulow <coolo\@kde.org> and\n",
115                "Harri Porten <porten\@kde.org>\n",
116                "Updated (Feb-1999), John Birch <jb.nz\@writeme.com>\n",
117	        "Current Maintainer Stephan Kulow\n\n";
118        exit 0;
119    }
120    elsif (/^--verbose$|^-v$/)
121    {
122        $verbose = 1;       # Oh is there a problem...?
123    }
124    elsif (/^-p(.+)$|^--path=(.+)$/)
125    {
126        $thisProg = "$1/".basename($thisProg) if($1);
127        $thisProg = "$2/".basename($thisProg) if($2);
128        warn ("$thisProg doesn't exist\n")      if (!(-f $thisProg));
129        $pathoption=1;
130    }
131    elsif (/^--help$|^-h$/)
132    {
133        print STDOUT "Usage $thisProg [OPTION] ... [dir/Makefile.in]...\n",
134                "\n",
135                "Patches dir/Makefile.in generated from automake\n",
136                "(where dir can be a full or relative directory name)",
137                "\n",
138                "  -v, --verbose      verbosely list files processed\n",
139                "  -h, --help         print this help, then exit\n",
140                "  --version          print version number, then exit\n",
141                "  -p, --path=        use the path to am_edit if the path\n",
142	        "  --no-final         don't patch for --enable-final\n",
143                "                     called from is not the one to be used\n";
144
145        exit 0;
146    }
147    elsif (/^--no-final$/)
148    {
149	$use_final = 0;
150        $thisProg .= " --no-final";
151    }
152    elsif (/^-n$/)
153    {
154    	$dryrun = 1;
155    }
156    else
157    {
158        # user selects what input files to check
159        # add full path if relative path is given
160        $_ = cwd()."/".$_   if (! /^\//);
161        print "User wants $_\n" if ($verbose);
162        push (@makefiles, $_);
163    }
164}
165
166if ($thisProg =~ /^\// && !$pathoption )
167{
168  print STDERR "Illegal full pathname call performed...\n",
169      "The call to \"$thisProg\"\nwould be inserted in some Makefile.in.\n",
170      "Please use option --path.\n";
171  exit 1;
172}
173
174# Only scan for files when the user hasn't entered data
175if (!@makefiles)
176{
177    print STDOUT "Scanning for Makefile.in\n"       if ($verbose);
178    find (\&add_makefile, cwd());
179    #chdir('$topdir');
180} else {
181    print STDOUT "Using user enter input files\n"   if ($verbose);
182}
183
184foreach $makefile (sort(@makefiles))
185{
186    processMakefile ($makefile);
187    last            if ($errorflag);
188}
189
190# Just some debug statistics - comment out for release as it uses printf.
191printf STDOUT "Time %.2f CPU sec\n", (times)[0] - $start     if ($verbose);
192
193exit $errorflag;        # causes make to fail if erroflag is set
194
195#-----------------------------------------------------------------------------
196
197# In conjunction with the "find" call, this builds the list of input files
198sub add_makefile ()
199{
200  push (@makefiles, $File::Find::name) if (/Makefile.in$/);
201}
202
203#-----------------------------------------------------------------------------
204
205# Processes a single make file
206# The parameter contains the full path name of the Makefile.in to use
207sub processMakefile ($)
208{
209    # some useful globals for the subroutines called here
210    local ($makefile)       = @_;
211    local @headerdirs       = ('.');
212    local $haveAutomocTag   = 0;
213    local $MakefileData     = "";
214
215    local $cxxsuffix  = "KKK";
216
217    local @programs = ();  # lists the names of programs and libraries
218    local $program = "";
219
220    local %realObjs = ();  # lists the objects compiled into $program
221    local %sources = ();   # lists the sources used for $program
222    local %finalObjs = (); # lists the objects compiled when final
223    local %realname = ();  # the binary name of program variable
224    local %idlfiles = ();  # lists the idl files used for $program
225    local %globalmocs = ();# list of all mocfiles (in %mocFiles format)
226    local %important = (); # list of files to be generated asap
227    local %uiFiles = ();
228
229    local $allidls = "";
230    local $idl_output = "";# lists all idl generated files for cleantarget
231    local $ui_output = "";# lists all uic generated files for cleantarget
232
233    local %depedmocs = ();
234
235    local $metasourceTags = 0;
236    local $dep_files      = "";
237    local $dep_finals     = "";
238    local %target_adds    = (); # the targets to add
239    local $kdelang        = "";
240    local @cleanfiles     = ();
241    local $cleanMoc       = "";
242    local $closure_output = "";
243
244    $makefileDir = dirname($makefile);
245    chdir ($makefileDir);
246    $printname = $makefile;
247    $printname =~ s/^\Q$topdir\E\///;
248    $makefile = basename($makefile);
249
250    print STDOUT "Processing makefile $printname\n"   if ($verbose);
251
252    # Setup and see if we need to do this.
253    return      if (!initialise());
254
255    tag_AUTOMAKE ();            # Allows a "make" to redo the Makefile.in
256    tag_META_INCLUDES ();       # Supplies directories for src locations
257
258    foreach $program (@programs) {
259        $sources_changed{$program} = 0;
260        $depedmocs{$program} = "";
261        $important{$program} = "";
262	tag_IDLFILES();             # Sorts out idl rules
263	tag_CLOSURE();
264	tag_UIFILES();             # Sorts out ui rules
265        tag_METASOURCES ();         # Sorts out the moc rules
266        if ($sources_changed{$program}) {
267            my $lookup = "$program" . '_SOURCES\s*=\s*(.*)';
268            substituteLine($lookup, "$program\_SOURCES=" . $sources{$program});
269        }
270        if ($important{$program}) {
271            local %source_dict = ();
272            for $source (split(/[\034\s]+/, $sources{$program})) {
273                $source_dict{$source} = 1;
274            }
275            for $source (@cleanfiles) {
276                $source_dict{$source} = 0;
277            }
278            for $source (keys %source_dict) {
279                next if (!$source);
280                if ($source_dict{$source}) {
281                    # sanity check
282                    if (! -f $source) {
283                        print STDERR "Error: $source is listed in a _SOURCE line in $printname, but doesn't exist yet. Put it in DISTCLEANFILES!\n";
284                    } else {
285                        $target_adds{"\$(srcdir)/$source"} .= $important{$program};
286                    }
287                }
288            }
289        }
290    }
291    if ($cleanMoc) {
292        # Always add dist clean tag
293        # Add extra *.moc.cpp files created for USE_AUTOMOC because they
294        # aren't included in the normal *.moc clean rules.
295        appendLines ("$cleantarget-metasources:\n\t-rm -f $cleanMoc\n");
296        $target_adds{"$cleantarget-am"} .= "$cleantarget-metasources ";
297    }
298    tag_DIST();
299
300    if ($idl_output) {
301        appendLines ("$cleantarget-idl:\n\t-rm -f $idl_output\n");
302        $target_adds{"$cleantarget-am"} .= "$cleantarget-idl ";
303    }
304
305    if ($ui_output) {
306        appendLines ("$cleantarget-ui:\n\t-rm -f $ui_output\n");
307        $target_adds{"$cleantarget-am"} .= "$cleantarget-ui ";
308    }
309
310    if ($closure_output) {
311        appendLines ("$cleantarget-closures:\n\t-rm -f $closure_output\n");
312        $target_adds{"$cleantarget-am"} .= "$cleantarget-closures ";
313    }
314
315    if ($MakefileData =~ /\nKDE_LANG\s*=\s*(\S*)\s*\n/) {
316        $kdelang = '$(KDE_LANG)'
317    } else {
318        $kdelang = '';
319    }
320
321    tag_POFILES ();             # language rules for po directory
322    tag_DOCFILES ();            # language rules for doc directories
323    tag_LOCALINSTALL();         # add $(DESTDIR) before all kde_ dirs
324    tag_ICON();
325    tag_SUBDIRS();
326
327    my $tmp = "force-reedit:\n";
328    $tmp   .= "\t$automkCall\n\tcd \$(top_srcdir) && perl $thisProg $printname\n\n";
329    appendLines($tmp);
330
331    make_meta_classes();
332    tag_FINAL() if (!$kdeopts{"nofinal"});
333
334    my $final_lines = "final:\n\t\$(MAKE) ";
335    my $final_install_lines = "final-install:\n\t\$(MAKE) ";
336    my $nofinal_lines = "no-final:\n\t\$(MAKE) ";
337    my $nofinal_install_lines = "no-final-install:\n\t\$(MAKE) ";
338
339    foreach $program (@programs) {
340
341        my $lookup = "$program\_OBJECTS.*=[^\n]*";
342
343        my $new = "";
344
345        my @list = split(/[\034\s]+/, $realObjs{$program});
346
347        if (!$kdeopts{"nofinal"} && @list > 1 && $finalObjs{$program}) {
348
349            $new .= "$program\_final\_OBJECTS = " . $finalObjs{$program};
350            $new .= "\n$program\_nofinal\_OBJECTS = " . $realObjs{$program};
351            $new .= "\n\@KDE_USE_FINAL_FALSE\@$program\_OBJECTS = \$($program\_nofinal\_OBJECTS)";
352            $new .= "\n\@KDE_USE_FINAL_TRUE\@$program\_OBJECTS = \$($program\_final\_OBJECTS)";
353
354            $final_lines .= "$program\_OBJECTS=\"\$($program\_final_OBJECTS)\" ";
355            $final_install_lines .= "$program\_OBJECTS=\"\$($program\_final_OBJECTS)\" ";
356            $nofinal_lines .= "$program\_OBJECTS=\"\$($program\_nofinal\_OBJECTS)\" ";
357            $nofinal_install_lines .= "$program\_OBJECTS=\"\$($program\_nofinal_OBJECTS)\" ";
358        } else {
359            $new = "$program\_OBJECTS = " . $realObjs{$program};
360        }
361        substituteLine ($lookup, $new);
362    }
363    appendLines($final_lines . "all-am");
364    appendLines($final_install_lines . "install-am");
365    appendLines($nofinal_lines . "all-am");
366    appendLines($nofinal_install_lines . "install-am");
367
368    my $lookup = 'DEP_FILES\s*=([^\n]*)';
369    if ($MakefileData =~ /\n$lookup\n/o) {
370        $depfiles = $1;
371
372        if ($dep_finals) {
373            $lines  = "\@KDE_USE_FINAL_TRUE\@DEP_FILES = $dep_files $dep_finals \034\t$depfiles\n";
374            $lines .= "\@KDE_USE_FINAL_FALSE\@DEP_FILES = $dep_files $depfiles\n";
375        } else {
376            $lines = "DEP_FILES = $dep_files $depfiles\n";
377        }
378
379        substituteLine($lookup, $lines);
380    }
381
382    my $cvs_lines = "cvs-clean:\n";
383    $cvs_lines .= "\t\$(MAKE) -f \$(top_srcdir)/admin/Makefile.common cvs-clean\n";
384    appendLines($cvs_lines);
385
386    $cvs_lines  = "kde-rpo-clean:\n";
387    $cvs_lines .= "\t-rm -f *.rpo\n";
388    appendLines($cvs_lines);
389    $target_adds{"clean"} .= "kde-rpo-clean ";
390
391    # some strange people like to do a install-exec, and expect that also
392    # all modules are installed.  automake doesn't know this, so we need to move
393    # this here from install-data to install-exec.
394    if ($MakefileData =~ m/\nkde_module_LTLIBRARIES\s*=/) {
395      $target_adds{"install-exec-am"} .= "install-kde_moduleLTLIBRARIES";
396      my $lookup = "install-data-am:\s*(.*)";
397      if ($MakefileData =~ /\n$lookup\n/) {
398        my $newdeps = $1;
399	$newdeps =~ s/\s*install-kde_moduleLTLIBRARIES\s*/ /g;
400	substituteLine($lookup, "install-data-am: " . $newdeps);
401      }
402    }
403
404    my $lines = "";
405
406    foreach $add (keys %target_adds) {
407        my $lookup = quotemeta($add) . ":\s*(.*)";
408        if ($MakefileData =~ /\n$lookup\n/) {
409            substituteLine($lookup, "$add: " . $target_adds{$add} . $1);
410        } else {
411            $lines .= "$add: " . $target_adds{$add} . "\n";
412        }
413    }
414    if ($lines) {
415        appendLines($lines);
416    }
417
418    my $found = 1;
419
420    while ($found) {
421        if ($MakefileData =~ m/\n(.*)\$\(CXXFLAGS\)(.*)\n/) {
422            my $vor = $1;   # "vor" means before in German
423            my $nach = $2; # "nach" means after in German
424            my $lookup = quotemeta("$1\$(CXXFLAGS)$2");
425            my $replacement = "$1\$(KCXXFLAGS)$2";
426            $MakefileData =~ s/$lookup/$replacement/;
427            $lookup =~ s/\\\$\\\(CXXFLAGS\\\)/\\\$\\\(KCXXFLAGS\\\)/;
428            $replacement = "$vor\$(KCXXFLAGS) \$(KDE_CXXFLAGS)$nach";
429            substituteLine($lookup, $replacement);
430        } else {
431            $found = 0;
432        }
433    }
434
435    $lookup = '(\n[^#].*\$\(LIBTOOL\) --mode=link) (\$\(CXXLD\).*\$\(KCXXFLAGS\))';
436
437    if ($MakefileData =~ m/$lookup/ ) {
438        $MakefileData =~ s/$lookup/$1 --tag=CXX $2/;
439    }
440
441    $lookup = '(\n[^#].*\$\(LIBTOOL\) --mode=compile) (\$\(CXX\).*\$\(KCXXFLAGS\))';
442    if ($MakefileData =~ m/$lookup/ ) {
443        $MakefileData =~ s/$lookup/$1 --tag=CXX $2/;
444    }
445
446    $MakefileData =~ s/\$\(KCXXFLAGS\)/\$\(CXXFLAGS\)/g;
447
448    $lookup = '(.*)cp -pr \$\$/\$\$file \$\(distdir\)/\$\$file(.*)';
449    if ($MakefileData =~ m/\n$lookup\n/) {
450        substituteLine($lookup, "$1cp -pr \$\$d/\$\$file \$(distdir)/\$\$file$2");
451    }
452
453    # Always update the Makefile.in
454    updateMakefile ();
455    return;
456}
457
458#-----------------------------------------------------------------------------
459
460# Check to see whether we should process this make file.
461# This is where we look for tags that we need to process.
462# A small amount of initialising on the tags is also done here.
463# And of course we open and/or create the needed make files.
464sub initialise ()
465{
466    if (! -r "Makefile.am") {
467	print STDOUT "found Makefile.in without Makefile.am\n" if ($verbose);
468	return 0;
469    }
470
471    # Checking for files to process...
472    open (FILEIN, $makefile)
473      || die "Could not open $makefileDir/$makefile: $!\n";
474    # Read the file
475    # stat(FILEIN)[7] might look more elegant, but is slower as it
476    # requires stat'ing the file
477    seek(FILEIN, 0, 2);
478    my $fsize = tell(FILEIN);
479    seek(FILEIN, 0, 0);
480    read FILEIN, $MakefileData, $fsize;
481    close FILEIN;
482    print "DOS CRLF within $makefileDir/$makefile!\n" if($MakefileData =~ y/\r//d);
483
484    # Remove the line continuations, but keep them marked
485    # Note: we lose the trailing spaces but that's ok.
486    $MakefileData =~ s/\\\s*\n/\034/g;
487
488    # If we've processed the file before...
489    restoreMakefile ()      if ($MakefileData =~ /$progId/);
490
491    foreach $dir (@foreignfiles) {
492      if (substr($makefileDir,0,length($dir)) eq $dir) {
493	return 0;
494      }
495    }
496
497    %kdeopts = ();
498    $kdeopts{"foreign"} = 0;
499    $kdeopts{"qtonly"} = 0;
500    $kdeopts{"nofinal"} = !$use_final; # default
501
502    if ($MakefileData =~ /\nKDE_OPTIONS\s*=\s*([^\n]*)\n/) {
503        local @kde_options = split(/[\s\034]/, $1);
504        if (grep(/^foreign$/, @kde_options)) {
505            push(@foreignfiles, $makefileDir . "/");
506            return 0; # don't touch me
507        }
508        for $opt (@kde_options) {
509            if (!defined $kdeopts{$opt}) {
510                print STDERR "Warning: unknown option $opt in $printname\n";
511            } else {
512                $kdeopts{$opt} = 1;
513            }
514        }
515    }
516
517    # Look for the tags that mean we should process this file.
518    $metasourceTags = 0;
519    $metasourceTags++    while ($MakefileData =~ /\n[^=\#]*METASOURCES\s*=/g);
520
521    my $pofileTag = 0;
522    $pofileTag++    while ($MakefileData =~ /\nPOFILES\s*=/g);
523    if ($pofileTag > 1)
524      {
525          print STDERR "Error: Only one POFILES tag allowed\n";
526          $errorflag = 1;
527      }
528
529    while ($MakefileData =~ /\n\.SUFFIXES:([^\n]+)\n/g) {
530	my @list=split(' ', $1);
531	foreach $ext (@list) {
532	    if ($ext =~ /^\.$cppExt$/) {
533		$cxxsuffix = $ext;
534		$cxxsuffix =~ s/\.//g;
535		print STDOUT "will use suffix $cxxsuffix\n" if ($verbose);
536		last;
537	    }
538	}
539    }
540
541    while ($MakefileData =~ /\n(\S*)_OBJECTS\s*=[ \t\034]*([^\n]*)\n/g) {
542
543        my $program = $1;
544        my $objs = $2; # safe them
545
546        my $ocv = 0;
547
548        my @objlist = split(/[\s\034]+/, $objs);
549        foreach $obj (@objlist) {
550            if ($obj =~ /\$\((\S+)\)/ ) {
551                my $variable = $1;
552                if ($variable !~ 'OBJEXT') {
553                    $ocv = 1;
554                }
555            }
556        }
557
558        next if ($ocv);
559
560        $program =~ s/^am_// if ($program =~ /^am_/);
561
562        my $sourceprogram = $program;
563        $sourceprogram =~ s/\@am_/\@/ if($sourceprogram =~ /^.*\@am_.+/);
564
565        print STDOUT "found program $program\n" if ($verbose);
566        push(@programs, $program);
567
568        $realObjs{$program} = $objs;
569
570        if ($MakefileData =~ /\n$sourceprogram\_SOURCES\s*=\s*(.*)\n/) {
571            $sources{$program} = $1;
572        }
573        else {
574            $sources{$program} = "";
575            print STDERR "found program with no _SOURCES: $program\n";
576        }
577
578        my $realprogram = $program;
579        $realprogram =~ s/_/./g; # unmask to regexp
580        if ($MakefileData =~ /\n($realprogram)(\$\(EXEEXT\)?)?:.*\$\($program\_OBJECTS\)/) {
581            $realname{$program} = $1;
582        } else {
583            # not standard Makefile - nothing to worry about
584            $realname{$program} = "";
585        }
586    }
587
588    my $lookup = '\nDEPDIR\s*=.*';
589    if ($MakefileData !~ /($lookup)\n/o) {
590        $lookup = '\nbindir\s*=.*';
591        if ($MakefileData =~ /($lookup)\n/) {
592            substituteLine ($lookup, "DEPDIR = .deps\n$1");
593        }
594    }
595
596    my @marks = ('MAINTAINERCLEANFILES', 'CLEANFILES', 'DISTCLEANFILES');
597    foreach $mark (@marks) {
598        while ($MakefileData =~ /\n($mark)\s*=\s*([^\n]*)/g) {
599            foreach $file (split('[\034\s]', $2)) {
600                $file =~ s/\.\///;
601                push(@cleanfiles, $file);
602            }
603        }
604    }
605
606    my $localTag = 0;
607    $localTag++ if ($MakefileData =~ /\ninstall-\S+-local:/);
608
609    return (!$errorflag);
610}
611
612#-----------------------------------------------------------------------------
613
614# Gets the list of user defined directories - relative to $srcdir - where
615# header files could be located.
616sub tag_META_INCLUDES ()
617{
618    my $lookup = '[^=\n]*META_INCLUDES\s*=\s*(.*)';
619    return 1    if ($MakefileData !~ /($lookup)\n/o);
620    print STDOUT "META_INCLUDE processing <$1>\n"       if ($verbose);
621
622    my $headerStr = $2;
623    removeLine ($lookup, $1);
624
625    $headerStr =~ tr/\034/ /;
626    my @headerlist = split(' ', $headerStr);
627
628    foreach $dir (@headerlist)
629    {
630        $dir =~ s#\$\(srcdir\)#.#;
631        if (! -d $dir)
632        {
633            print STDERR "Warning: $dir can't be found. ",
634                            "Must be a relative path to \$(srcdir)\n";
635        }
636        else
637        {
638            push (@headerdirs, $dir);
639        }
640    }
641
642    return 0;
643}
644
645#-----------------------------------------------------------------------------
646
647sub tag_FINAL()
648{
649    my @final_names = ();
650
651    foreach $program (@programs) {
652
653        if ($sources{$program} =~ /\(/) {
654            print STDOUT "found ( in $program\_SOURCES. skipping\n" if ($verbose);
655            next;
656        }
657
658        my $mocsources = "";
659
660        my @progsources = split(/[\s\034]+/, $sources{$program});
661        my %sourcelist = ();
662
663        foreach $source (@progsources) {
664            my $suffix = $source;
665            $suffix =~ s/^.*\.([^\.]+)$/$1/;
666
667            if (defined($sourcelist{$suffix})) {
668                $sourcelist{$suffix} .= " " . $source;
669            } else {
670                $sourcelist{$suffix} .= $source;
671            }
672        }
673
674        foreach $suffix (keys %sourcelist) {
675
676            # See if this file contains c++ code. (ie Just check the files suffix against
677            my $suffix_is_cxx = 0;
678            if($suffix =~ /($cppExt)$/) {
679              $cxx_suffix = $1;
680              $suffix_is_cxx = 1;
681            }
682
683            my $mocfiles_in = ($suffix eq $cxxsuffix) &&
684              defined($depedmocs{$program});
685
686            my @sourcelist = split(/[\s\034]+/, $sourcelist{$suffix});
687
688            if ((@sourcelist == 1 && !$mocfiles_in) || $suffix_is_cxx != 1 ) {
689
690                # we support IDL on our own
691                if ($suffix =~ /^skel$/ || $suffix =~ /^stub/ || $suffix =~ /^h$/
692                    || $suffix =~ /^ui$/ ) {
693                    next;
694                }
695
696                foreach $file (@sourcelist) {
697
698                    $file =~ s/\Q$suffix\E$//;
699
700                    $finalObjs{$program} .= $file;
701                    if ($program =~ /_la$/) {
702                        $finalObjs{$program} .= "lo ";
703                    } else {
704                        $finalObjs{$program} .= "o ";
705                    }
706                }
707                next; # suffix
708            }
709
710            my $source_deps = "";
711            foreach $source (@sourcelist) {
712                if (-f $source) {
713                    $source_deps .= "\$(srcdir)/$source ";
714                } else {
715                    $source_deps .= "$source ";
716                }
717            }
718
719            $handling = "$program.all_$suffix.$suffix: \$(srcdir)/Makefile.in " . $source_deps . " ";
720
721            if ($mocfiles_in) {
722                $handling .= $depedmocs{$program};
723                foreach $mocfile (split(' ', $depedmocs{$program})) {
724                    if ($mocfile =~ m/\.$suffix$/) {
725                        $mocsources .= " " . $mocfile;
726                    }
727                }
728            }
729
730            $handling .= "\n";
731            $handling .= "\t\@echo 'creating $program.all_$suffix.$suffix ...'; \\\n";
732            $handling .= "\trm -f $program.all_$suffix.files $program.all_$suffix.final; \\\n";
733            $handling .= "\techo \"#define KDE_USE_FINAL 1\" >> $program.all_$suffix.final; \\\n";
734            $handling .= "\tfor file in " . $sourcelist{$suffix} . " $mocsources; do \\\n";
735            $handling .= "\t  echo \"#include \\\"\$\$file\\\"\" >> $program.all_$suffix.files; \\\n";
736            $handling .= "\t  test ! -f \$\(srcdir\)/\$\$file || egrep '^#pragma +implementation' \$\(srcdir\)/\$\$file >> $program.all_$suffix.final; \\\n";
737            $handling .= "\tdone; \\\n";
738            $handling .= "\tcat $program.all_$suffix.final $program.all_$suffix.files  > $program.all_$suffix.$suffix; \\\n";
739            $handling .= "\trm -f $program.all_$suffix.final $program.all_$suffix.files\n";
740
741            appendLines($handling);
742
743            push(@final_names, "$program.all_$suffix.$suffix");
744            $finalObjs{$program} .= "$program.all_$suffix.";
745            if ($program =~ /_la$/) {
746                $finalObjs{$program} .= "lo ";
747            } else {
748                $finalObjs{$program} .= "o ";
749            }
750        }
751    }
752
753    if (!$kdeopts{"nofinal"} && @final_names >= 1) {
754        # add clean-final target
755        my $lines = "$cleantarget-final:\n";
756        $lines .= "\t-rm -f " . join(' ', @final_names) . "\n" if (@final_names);
757        appendLines($lines);
758        $target_adds{"$cleantarget-am"} .= "$cleantarget-final ";
759
760        foreach $finalfile (@final_names) {
761            $finalfile =~ s/\.[^.]*$/.P/;
762            $dep_finals .= " \$(DEPDIR)/$finalfile";
763        }
764    }
765}
766
767# Organises the list of headers that we'll use to produce moc files
768# from.
769sub tag_METASOURCES ()
770{
771    local @newObs           = ();  # here we add to create object files
772    local @deped            = ();  # here we add to create moc files
773    local $mocExt           = ".moc";
774    local %mocFiles         = ();
775
776    my $line = "";
777    my $postEqual = "";
778
779    my $lookup;
780    my $found = "";
781
782    if ($metasourceTags > 1) {
783	$lookup = $program . '_METASOURCES\s*=\s*(.*)';
784	return 1    if ($MakefileData !~ /\n($lookup)\n/);
785	$found = $1;
786    } else {
787	$lookup = $program . '_METASOURCES\s*=\s*(.*)';
788	if ($MakefileData !~ /\n($lookup)\n/) {
789	    $lookup = 'METASOURCES\s*=\s*(.*)';
790	    return 1    if ($MakefileData !~ /\n($lookup)\n/o);
791	    $found = $1;
792	    $metasourceTags = 0; # we can use the general target only once
793	} else {
794            $found = $1;
795        }
796    }
797    print STDOUT "METASOURCE processing <$found>)\n"      if ($verbose);
798
799    $postEqual = $found;
800    $postEqual =~ s/[^=]*=//;
801
802    removeLine ($lookup, $found);
803
804    # Always find the header files that could be used to "moc"
805    return 1    if (findMocCandidates ());
806
807    if ($postEqual =~ /AUTO\s*(\S*)|USE_AUTOMOC\s*(\S*)/)
808    {
809	print STDERR "$printname: the argument for AUTO|USE_AUTOMOC is obsolete" if ($+);
810	$mocExt = ".moc.$cxxsuffix";
811	$haveAutomocTag = 1;
812    }
813    else
814    {
815        # Not automoc so read the list of files supplied which
816        # should be .moc files.
817
818        $postEqual =~ tr/\034/ /;
819
820        # prune out extra headers - This also checks to make sure that
821        # the list is valid.
822        pruneMocCandidates ($postEqual);
823    }
824
825    checkMocCandidates ();
826
827    if (@newObs) {
828        my $ext =  ($program =~ /_la$/) ? ".moc.lo " : ".moc.o ";
829        $realObjs{$program} .= "\034" . join ($ext, @newObs) . $ext;
830        $depedmocs{$program} = join (".moc.$cxxsuffix " , @newObs) . ".moc.$cxxsuffix";
831        foreach $file (@newObs) {
832            $dep_files .= " \$(DEPDIR)/$file.moc.P" if($dep_files !~/$file.moc.P/);
833        }
834    }
835    if (@deped) {
836        $depedmocs{$program} .= " ";
837        $depedmocs{$program} .= join('.moc ', @deped) . ".moc";
838        $depedmocs{$program} .= " ";
839    }
840    addMocRules ();
841    @globalmocs{keys %mocFiles}=values %mocFiles;
842}
843
844#-----------------------------------------------------------------------------
845
846# Returns 0 if the line was processed - 1 otherwise.
847# Errors are logged in the global $errorflags
848sub tag_AUTOMAKE ()
849{
850    my $lookup = '.*cd \$\(top_srcdir\)\s+&&[\s\034]+\$\(AUTOMAKE\)(.*)';
851    return 1    if ($MakefileData !~ /\n($lookup)\n/);
852    print STDOUT "AUTOMAKE processing <$1>\n"        if ($verbose);
853
854    my $newLine = $1."\n\tcd \$(top_srcdir) && perl $thisProg $printname";
855    substituteLine ($lookup, $newLine);
856    $automkCall = $1;
857    return 0;
858}
859
860#-----------------------------------------------------------------------------
861
862sub handle_TOPLEVEL()
863{
864    my $pofiles = "";
865    my @restfiles = ();
866    opendir (THISDIR, ".");
867    foreach $entry (readdir(THISDIR)) {
868        next if (-d $entry);
869
870        next if ($entry eq "CVS" || $entry =~ /^\./  || $entry =~ /^Makefile/ || $entry =~ /~$/ || $entry =~ /^\#.*\#$/ || $entry =~ /.gmo$/);
871
872        if ($entry =~ /\.po$/) {
873             next;
874        }
875        push(@restfiles, $entry);
876    }
877    closedir (THISDIR);
878
879    if (@restfiles) {
880        $target_adds{"install-data-am"} .= "install-nls-files ";
881        $lines = "install-nls-files:\n";
882        $lines .= "\t\$(mkinstalldirs) \$(DESTDIR)\$(kde_locale)/$kdelang\n";
883        for $file (@restfiles) {
884            $lines .= "\t\$(INSTALL_DATA) \$\(srcdir\)/$file \$(DESTDIR)\$(kde_locale)/$kdelang/$file\n";
885        }
886	$target_adds{"uninstall"} .= "uninstall-nls-files ";
887        $lines .= "uninstall-nls-files:\n";
888        for $file (@restfiles) {
889            $lines .= "\t-rm -f \$(DESTDIR)\$(kde_locale)/$kdelang/$file\n";
890        }
891        appendLines($lines);
892    }
893
894    return 0;
895}
896
897#-----------------------------------------------------------------------------
898
899sub tag_SUBDIRS ()
900{
901  if ($MakefileData !~ /\nSUBDIRS\s*=\s*\$\(AUTODIRS\)\s*\n/) {
902    return 1;
903  }
904
905  my $subdirs;
906
907  opendir (THISDIR, ".");
908  foreach $entry (readdir(THISDIR)) {
909    next if ($entry eq "CVS" || $entry =~ /^\./);
910    if (-d $entry && -f $entry . "/Makefile.am") {
911      $subdirs .= " $entry";
912      next;
913    }
914  }
915  closedir (THISDIR);
916
917  my $lines = "SUBDIRS =$subdirs\n";
918  substituteLine('SUBDIRS\s*=.*', $lines);
919  return 0;
920}
921
922sub tag_IDLFILES ()
923{
924    my @psources = split(/[\034\s]+/, $sources{$program});
925    my $dep_lines = "";
926    my @cppFiles = ();
927
928    foreach $source (@psources) {
929
930        my $skel = ($source =~ m/\.skel$/);
931
932        if ($source =~ m/\.stub$/ || $skel) {
933
934            my $qs = quotemeta($source);
935            $sources{$program} =~ s/$qs//;
936            $sources_changed{$program} = 1;
937
938            print STDOUT "adding IDL file $source\n" if ($verbose);
939
940            $source =~ s/\.(stub|skel)$//;
941
942            my $sourcename;
943
944            if ($skel) {
945                $sourcename = "$source\_skel";
946            } else {
947                $sourcename = "$source\_stub";
948            }
949
950            my $sourcedir = '';
951            if (-f "$makefileDir/$source.h") {
952                $sourcedir = '$(srcdir)/';
953            } else {
954                if ($MakefileData =~ /\n$source\_DIR\s*=\s*(\S+)\n/) {
955                    $sourcedir = $1;
956                    $sourcedir .= "/" if ($sourcedir !~ /\/$/);
957                }
958            }
959
960            if ($allidls !~ /$source\_kidl/) {
961
962                $dep_lines .= "$source.kidl: $sourcedir$source.h \$(DCOPIDL_DEPENDENCIES)\n";
963                $dep_lines .= "\t\$(DCOPIDL) $sourcedir$source.h > $source.kidl || ( rm -f $source.kidl ; /bin/false )\n";
964
965                $allidls .= $source . "_kidl ";
966            }
967
968            if ($allidls !~ /$sourcename/) {
969
970                if ($skel) {
971                    $dep_lines .= "$sourcename.$cxxsuffix: $source.kidl\n";
972                    $dep_lines .= "\t\$(DCOPIDL2CPP) --c++-suffix $cxxsuffix --no-stub $source.kidl\n";
973                } else {
974                    $target_adds{"$sourcename.$cxxsuffix"} .= "$sourcename.h ";
975                    $dep_lines .= "$sourcename.h: $source.kidl\n";
976                    $dep_lines .= "\t\$(DCOPIDL2CPP) --c++-suffix $cxxsuffix --no-skel $source.kidl\n";
977                }
978
979                $allidls .= $sourcename . " ";
980            }
981
982            $idlfiles{$program} .= $sourcename . " ";
983
984            if ($program =~ /_la$/) {
985                $realObjs{$program} .= " $sourcename.lo";
986            } else {
987                $realObjs{$program} .= " $sourcename.\$(OBJEXT)";
988            }
989            $sources{$program} .= " $sourcename.$cxxsuffix";
990            $sources_changed{$program} = 1;
991            $important{$program} .= "$sourcename.h " if (!$skel);
992            $idl_output .= "\\\n\t$sourcename.$cxxsuffix $sourcename.h $source.kidl ";
993            push(@cleanfiles, "$sourcename.$cxxsuffix");
994            push(@cleanfiles, "$sourcename.h");
995            push(@cleanfiles, "$sourcename.kidl");
996            $dep_files .= " \$(DEPDIR)/$sourcename.P" if ($dep_files !~/$sourcename.P/);
997        }
998    }
999    if ($dep_lines) {
1000        appendLines($dep_lines);
1001    }
1002
1003    if (0) {
1004        my $lookup = "($program)";
1005        $lookup .= '(|\$\(EXEEXT\))';
1006        $lookup =~ s/\_/./g;
1007        $lookup .= ":(.*..$program\_OBJECTS..*)";
1008        #    $lookup = quotemeta($lookup);
1009        if ($MakefileData =~ /\n$lookup\n/) {
1010
1011            my $line = "$1$2: ";
1012            foreach $file (split(' ', $idlfiles{$program})) {
1013                $line .= "$file.$cxxsuffix ";
1014            }
1015            $line .= $3;
1016            substituteLine($lookup, $line);
1017        } else {
1018            print STDERR "no built dependency found $lookup\n";
1019        }
1020    }
1021}
1022
1023sub tag_UIFILES ()
1024{
1025    my @psources = split(/[\034\s]+/, $sources{$program});
1026    my $dep_lines = "";
1027    my @depFiles = ();
1028
1029    foreach $source (@psources) {
1030
1031        if ($source =~ m/\.ui$/) {
1032
1033            print STDERR "adding UI file $source\n" if ($verbose);
1034
1035            my $qs = quotemeta($source);
1036            $sources{$program} =~ s/$qs//;
1037            $sources_changed{$program} = 1;
1038
1039            $source =~ s/\.ui$//;
1040
1041            my $sourcedir = '';
1042            if (-f "$makefileDir/$source.ui") {
1043                $sourcedir = '$(srcdir)/';
1044            }
1045
1046            if (!$uiFiles{$source}) {
1047
1048                $dep_lines .= "$source.$cxxsuffix: $sourcedir$source.ui $source.h $source.moc\n";
1049                $dep_lines .= "\trm -f $source.$cxxsuffix\n";
1050                if (!$kdeopts{"qtonly"}) {
1051                    $dep_lines .= "\techo '#include <klocale.h>' > $source.$cxxsuffix\n";
1052                    $dep_lines .= "\t\$(UIC) -tr \${UIC_TR} -i $source.h $sourcedir$source.ui | sed -e \"s,i18n( \\\"\\\" ),QString::null,g\" | sed -e \"s,QT_KDE_I18N( \\\"\\\"\\, \\\"\\\" ),QString::null,g\" >> $source.$cxxsuffix || rm -f $source.$cxxsuffix\n";
1053                } else {
1054                    $dep_lines .= "\t\$(UIC) -i $source.h $sourcedir$source.ui > $source.$cxxsuffix || rm -f $source.$cxxsuffix\n";
1055                }
1056                $dep_lines .= "\techo '#include \"$source.moc\"' >> $source.$cxxsuffix\n\n";
1057                $dep_lines .= "$source.h: $sourcedir$source.ui\n";
1058                $dep_lines .= "\t\$(UIC) -o $source.h $sourcedir$source.ui\n\n";
1059                $dep_lines .= "$source.moc: $source.h\n";
1060                $dep_lines .= "\t\$(MOC) $source.h -o $source.moc\n";
1061
1062		$uiFiles{$source} = 1;
1063                $depedmocs{$program} .= " $source.moc";
1064                $globalmocs{$source} = "\035$source.h\035$source.cpp";
1065            }
1066
1067            if ($program =~ /_la$/) {
1068                $realObjs{$program} .= " $source.lo";
1069            } else {
1070                $realObjs{$program} .= " $source.\$(OBJEXT)";
1071            }
1072            $sources{$program} .= " $source.$cxxsuffix";
1073            $sources_changed{$program} = 1;
1074            $important{$program} .= "$source.h ";
1075            $ui_output .= "\\\n\t$source.$cxxsuffix $source.h $source.moc ";
1076            push(@cleanfiles, "$source.$cxxsuffix");
1077            push(@cleanfiles, "source.h");
1078            push(@cleanfiles, "$source.moc");
1079            $dep_files .= " \$(DEPDIR)/$source.P" if($dep_files !~/$source.P/ );
1080        }
1081    }
1082    if ($dep_lines) {
1083        appendLines($dep_lines);
1084    }
1085}
1086
1087sub tag_ICON()
1088{
1089    my $lookup = '([^\s]*)_ICON\s*=\s*([^\n]*)';
1090    my $install = "";
1091    my $uninstall = "";
1092
1093    while ($MakefileData =~ /\n$lookup/og) {
1094        my $destdir;
1095        if ($1 eq "KDE") {
1096            $destdir = "kde_icondir";
1097        } else {
1098            $destdir = $1 . "dir";
1099        }
1100        my $iconauto = ($2 =~ /AUTO\s*$/);
1101        my @appnames = ();
1102        if ( ! $iconauto ) {
1103            my @_appnames = split(" ", $2);
1104            print STDOUT "KDE_ICON processing <@_appnames>\n"   if ($verbose);
1105            foreach $appname (@_appnames) {
1106                push(@appnames, quotemeta($appname));
1107            }
1108        } else {
1109            print STDOUT "KDE_ICON processing <AUTO>\n"   if ($verbose);
1110        }
1111
1112        my @files = ();
1113        opendir (THISDIR, ".");
1114        foreach $entry (readdir(THISDIR)) {
1115            next if ($entry eq "CVS" || $entry =~ /^\./  || $entry =~ /^Makefile/ || $entry =~ /~$/ || $entry =~ /^\#.*\#$/);
1116            next if (! -f $entry);
1117            if ( $iconauto )
1118              {
1119                  push(@files, $entry)
1120                    if ($entry =~ /\.xpm/ || $entry =~ /\.png/);
1121              } else {
1122                  foreach $appname (@appnames) {
1123                      push(@files, $entry)
1124                        if ($entry =~ /-$appname\.xpm/ || $entry =~ /-$appname\.png/);
1125                  }
1126              }
1127        }
1128        closedir (THISDIR);
1129
1130        my %directories = ();
1131
1132        foreach $file (@files) {
1133            my $newfile = $file;
1134            my $prefix = $file;
1135            $prefix =~ s/\.(png|xpm)$//;
1136            my $appname = $prefix;
1137            $appname =~ s/^[^-]+-// if ($appname =~ /-/) ;
1138            $appname =~ s/^[^-]+-// if ($appname =~ /-/) ;
1139            $appname = quotemeta($appname);
1140            $prefix =~ s/$appname$//;
1141            $prefix =~ s/-$//;
1142
1143            $prefix = 'lo16-app' if ($prefix eq 'mini');
1144            $prefix = 'lo32-app' if ($prefix eq 'lo');
1145            $prefix = 'hi48-app' if ($prefix eq 'large');
1146            $prefix .= '-app' if ($prefix =~ m/^...$/);
1147
1148            my $type = $prefix;
1149            $type =~ s/^.*-([^-]+)$/$1/;
1150            $prefix =~ s/^(.*)-[^-]+$/$1/;
1151
1152            my %type_hash =
1153              (
1154               'action' => 'actions',
1155               'app' => 'apps',
1156               'device' => 'devices',
1157               'filesys' => 'filesystems',
1158               'mime' => 'mimetypes'
1159              );
1160
1161            if (! defined $type_hash{$type} ) {
1162                print STDERR "unknown icon type $type in $printname ($file)\n";
1163                next;
1164            }
1165
1166            my %dir_hash =
1167              (
1168               'los' => 'locolor/16x16',
1169               'lom' => 'locolor/32x32',
1170               'him' => 'hicolor/32x32',
1171               'hil' => 'hicolor/48x48',
1172               'lo16' => 'locolor/16x16',
1173               'lo22' => 'locolor/22x22',
1174               'lo32' => 'locolor/32x32',
1175               'hi16' => 'hicolor/16x16',
1176               'hi22' => 'hicolor/22x22',
1177               'hi32' => 'hicolor/32x32',
1178               'hi48' => 'hicolor/48x48',
1179               'hi64' => 'hicolor/64x64',
1180               'hisc' => 'hicolor/scalable'
1181              );
1182
1183            $newfile =~ s@.*-($appname\.(png|xpm?))@$1@;
1184
1185            if (! defined $dir_hash{$prefix}) {
1186                print STDERR "unknown icon prefix $prefix in $printname\n";
1187                next;
1188            }
1189
1190            my $dir = $dir_hash{$prefix} . "/" . $type_hash{$type};
1191            if ($newfile =~ /-[^\.]/) {
1192                my $tmp = $newfile;
1193                $tmp =~ s/^([^-]+)-.*$/$1/;
1194                $dir = $dir . "/" . $tmp;
1195                $newfile =~ s/^[^-]+-//;
1196            }
1197
1198            if (!defined $directories{$dir}) {
1199                $install .= "\t\$(mkinstalldirs) \$(DESTDIR)\$($destdir)/$dir\n";
1200                $directories{$dir} = 1;
1201            }
1202
1203            $install .= "\t\$(INSTALL_DATA) \$(srcdir)/$file \$(DESTDIR)\$($destdir)/$dir/$newfile\n";
1204            $uninstall .= "\t-rm -f \$(DESTDIR)\$($destdir)/$dir/$newfile\n";
1205
1206        }
1207    }
1208
1209    if (length($install)) {
1210        $target_adds{"install-data-am"} .= "install-kde-icons ";
1211        $target_adds{"uninstall-am"} .= "uninstall-kde-icons ";
1212        appendLines("install-kde-icons:\n" . $install . "\nuninstall-kde-icons:\n" . $uninstall);
1213    }
1214}
1215
1216sub handle_POFILES($$)
1217{
1218  my @pofiles = split(" ", $_[0]);
1219  my $lang = $_[1];
1220
1221  # Build rules for creating the gmo files
1222  my $tmp = "";
1223  my $allgmofiles     = "";
1224  my $pofileLine   = "POFILES =";
1225  foreach $pofile (@pofiles)
1226    {
1227        $pofile =~ /(.*)\.[^\.]*$/;          # Find name minus extension
1228        $tmp .= "$1.gmo: $pofile\n";
1229        $tmp .= "\trm -f $1.gmo; \$(GMSGFMT) -o $1.gmo \$(srcdir)/$pofile\n";
1230        $tmp .= "\ttest ! -f $1.gmo || touch $1.gmo\n";
1231        $allgmofiles .= " $1.gmo";
1232        $pofileLine  .= " $1.po";
1233    }
1234  appendLines ($tmp);
1235  my $lookup = 'POFILES\s*=([^\n]*)';
1236  if ($MakefileData !~ /\n$lookup/o) {
1237    appendLines("$pofileLine\nGMOFILES =$allgmofiles");
1238  } else {
1239    substituteLine ($lookup, "$pofileLine\nGMOFILES =$allgmofiles");
1240  }
1241
1242    if ($allgmofiles) {
1243
1244        # Add the "clean" rule so that the maintainer-clean does something
1245        appendLines ("clean-nls:\n\t-rm -f $allgmofiles\n");
1246
1247	$target_adds{"maintainer-clean"} .= "clean-nls ";
1248
1249	$lookup = 'DISTFILES\s*=\s*(.*)';
1250	if ($MakefileData =~ /\n$lookup\n/o) {
1251	  $tmp = "DISTFILES = \$(GMOFILES) \$(POFILES) $1";
1252	  substituteLine ($lookup, $tmp);
1253	}
1254    }
1255
1256  $target_adds{"install-data-am"} .= "install-nls ";
1257
1258  $tmp = "install-nls:\n";
1259  if ($lang) {
1260    $tmp  .= "\t\$(mkinstalldirs) \$(DESTDIR)\$(kde_locale)/$lang/LC_MESSAGES\n";
1261  }
1262  $tmp .= "\t\@for base in ";
1263  foreach $pofile (@pofiles)
1264    {
1265      $pofile =~ /(.*)\.[^\.]*$/;          # Find name minus extension
1266      $tmp .= "$1 ";
1267    }
1268
1269  $tmp .= "; do \\\n";
1270  if ($lang) {
1271    $tmp .= "\t  echo \$(INSTALL_DATA) \$\$base.gmo \$(DESTDIR)\$(kde_locale)/$lang/LC_MESSAGES/\$\$base.mo ;\\\n";
1272    $tmp .= "\t  test ! -f \$\$base.gmo || \$(INSTALL_DATA) \$\$base.gmo \$(DESTDIR)\$(kde_locale)/$lang/LC_MESSAGES/\$\$base.mo ;\\\n"
1273  } else {
1274    $tmp .= "\t  echo \$(INSTALL_DATA) \$\$base.gmo \$(DESTDIR)\$(kde_locale)/\$\$base/LC_MESSAGES/\$(PACKAGE).mo ;\\\n";
1275    $tmp .= "\t  \$(mkinstalldirs) \$(DESTDIR)\$(kde_locale)/\$\$base/LC_MESSAGES ; \\\n";
1276    $tmp .= "\t  test ! -f \$\$base.gmo || \$(INSTALL_DATA) \$\$base.gmo \$(DESTDIR)\$(kde_locale)/\$\$base/LC_MESSAGES/\$(PACKAGE).mo ;\\\n";
1277  }
1278  $tmp .= "\tdone\n\n";
1279  appendLines ($tmp);
1280
1281  $target_adds{"uninstall"} .= "uninstall-nls ";
1282
1283  $tmp = "uninstall-nls:\n";
1284  foreach $pofile (@pofiles)
1285    {
1286      $pofile =~ /(.*)\.[^\.]*$/;          # Find name minus extension
1287      if ($lang) {
1288	$tmp .= "\trm -f \$(DESTDIR)\$(kde_locale)/$lang/LC_MESSAGES/$1.mo\n";
1289      } else {
1290	$tmp .= "\trm -f \$(DESTDIR)\$(kde_locale)/$1/LC_MESSAGES/\$(PACKAGE).mo\n";
1291      }
1292    }
1293  appendLines($tmp);
1294
1295  $target_adds{"all"} .= "all-nls ";
1296
1297  $tmp = "all-nls: \$(GMOFILES)\n";
1298
1299  appendLines($tmp);
1300
1301  $target_adds{"distdir"} .= "distdir-nls ";
1302
1303  $tmp = "distdir-nls:\$(GMOFILES)\n";
1304  $tmp .= "\tfor file in \$(POFILES); do \\\n";
1305  $tmp .= "\t  cp \$(srcdir)/\$\$file \$(distdir); \\\n";
1306  $tmp .= "\tdone\n";
1307  $tmp .= "\tfor file in \$(GMOFILES); do \\\n";
1308  $tmp .= "\t  cp \$(srcdir)/\$\$file \$(distdir); \\\n";
1309  $tmp .= "\tdone\n";
1310
1311  appendLines ($tmp);
1312
1313  if (!$lang) {
1314    appendLines("merge:\n\t\$(MAKE) -f \$(top_srcdir)/admin/Makefile.common package-merge POFILES=\"\${POFILES}\" PACKAGE=\${PACKAGE}\n\n");
1315  }
1316
1317}
1318
1319#-----------------------------------------------------------------------------
1320
1321# Returns 0 if the line was processed - 1 otherwise.
1322# Errors are logged in the global $errorflags
1323sub tag_POFILES ()
1324{
1325    my $lookup = 'POFILES\s*=([^\n]*)';
1326    return 1    if ($MakefileData !~ /\n$lookup/o);
1327    print STDOUT "POFILES processing <$1>\n"   if ($verbose);
1328
1329    my $tmp = $1;
1330
1331    # make sure these are all gone.
1332    if ($MakefileData =~ /\n\.po\.gmo:\n/)
1333    {
1334        print STDERR "Warning: Found old .po.gmo rules in $printname. New po rules not added\n";
1335        return 1;
1336    }
1337
1338    # Either find the pofiles in the directory (AUTO) or use
1339    # only the specified po files.
1340    my $pofiles = "";
1341    if ($tmp =~ /^\s*AUTO\s*$/)
1342    {
1343        opendir (THISDIR, ".");
1344	next if ($entry eq "CVS" || $entry =~ /^\./  || $entry =~ /^Makefile/ || $entry =~ /~$/ || $entry =~ /^#.*#$/);
1345	$pofiles =  join(" ", grep(/\.po$/, readdir(THISDIR)));
1346        closedir (THISDIR);
1347        print STDOUT "pofiles found = $pofiles\n"   if ($verbose);
1348	if (-f "charset" && -f "kdelibs.po") {
1349	    handle_TOPLEVEL();
1350	}
1351    }
1352    else
1353    {
1354        $tmp =~ s/\034/ /g;
1355        $pofiles = $tmp;
1356    }
1357    return 1    if (!$pofiles);        # Nothing to do
1358
1359    handle_POFILES($pofiles, $kdelang);
1360
1361    return 0;
1362}
1363
1364sub helper_LOCALINSTALL($)
1365{
1366  my $lookup = "\n" . $_[0] . ":";
1367  if ($MakefileData =~ /($lookup)/) {
1368
1369    my $install = $MakefileData;
1370    $install =~ s/\n/\035/g;
1371    $install =~ s/.*\035$_[0]:[^\035]*\035//;
1372    my $emptyline = 0;
1373    while (! $emptyline) {
1374      if ($install =~ /([^\035]*)\035(.*)/) {
1375	local $line = $1;
1376	$install = $2;
1377	if ($line !~ /^\s*$/ && $line !~ /^(\@.*\@)*\t/) {
1378	  $emptyline = 1;
1379	} else {
1380	  replaceDestDir($line);
1381	}
1382      } else {
1383	$emptyline = 1;
1384      }
1385    }
1386  }
1387
1388}
1389
1390sub tag_LOCALINSTALL ()
1391{
1392  helper_LOCALINSTALL('install-exec-local');
1393  helper_LOCALINSTALL('install-data-local');
1394  helper_LOCALINSTALL('uninstall-local');
1395
1396  return 0;
1397}
1398
1399sub replaceDestDir($) {
1400  local $line = $_[0];
1401
1402  if (   $line =~ /^\s*(\@.*\@)*\s*\$\(mkinstalldirs\)/
1403      || $line =~ /^\s*(\@.*\@)*\s*\$\(INSTALL\S*\)/
1404      || $line =~ /^\s*(\@.*\@)*\s*(-?rm.*) \S*$/)
1405  {
1406    $line =~ s/^(.*) ([^\s]*)\s*$/$1 \$(DESTDIR)$2/ if ($line !~ /\$\(DESTDIR\)/);
1407  }
1408
1409  if ($line ne $_[0]) {
1410    $_[0] = quotemeta $_[0];
1411    substituteLine($_[0], $line);
1412  }
1413}
1414
1415#---------------------------------------------------------------------------
1416sub tag_CLOSURE () {
1417    return if ($program !~ /_la$/);
1418
1419    my $lookup = quotemeta($realname{$program}) . ":.*?\n\t.*?\\((.*?)\\) .*\n";
1420    $MakefileData =~ m/$lookup/;
1421    return if ($1 !~ /CXXLINK/);
1422
1423    if ($MakefileData !~ /\n$program\_LDFLAGS\s*=.*-no-undefined/ &&
1424        $MakefileData !~ /\n$program\_LDFLAGS\s*=.*KDE_PLUGIN/ ) {
1425        print STDERR "Report: $program contains undefined in $printname\n" if ($program =~ /^lib/ && $dryrun);
1426        return;
1427    }
1428    my $closure = $realname{$program} . ".closure";
1429    my $lines = "$closure: \$($program\_OBJECTS) \$($program\_DEPENDENCIES)\n";
1430    $lines .= "\t\@echo \"int main() {return 0;}\" > $program\_closure.$cxxsuffix\n";
1431    $lines .= "\t\@\$\(LTCXXCOMPILE\) -c $program\_closure.$cxxsuffix\n";
1432    $lines .= "\t\@\$\(CXXLINK\) $program\_closure.lo \$($program\_LDFLAGS) \$($program\_OBJECTS) \$($program\_LIBADD) \$(LIBS)\n";
1433    $lines .= "\t\@rm -f $program\_closure.* $closure\n";
1434    $lines .= "\t\@echo \"timestamp\" > $closure\n";
1435    $lines .= "\n";
1436    appendLines($lines);
1437    $lookup = $realname{$program} . ": (.*)";
1438    if ($MakefileData =~ /\n$lookup\n/) {
1439        $lines  = "\@KDE_USE_CLOSURE_TRUE@". $realname{$program} . ": $closure $1";
1440        $lines .= "\n\@KDE_USE_CLOSURE_FALSE@" . $realname{$program} . ": $1";
1441        substituteLine($lookup, $lines);
1442    }
1443    $closure_output .= " $closure";
1444}
1445
1446sub tag_DIST () {
1447    my %foundfiles = ();
1448    opendir (THISDIR, ".");
1449    foreach $entry (readdir(THISDIR)) {
1450        next if ($entry eq "CVS" || $entry =~ /^\./  || $entry =~ /^Makefile$$/ || $entry =~ /~$/ || $entry =~ /^\#.*\#$/);
1451        next if (! -f $entry);
1452        next if ($entry =~ /\.moc/ || $entry =~ /\.lo$/ || $entry =~ /\.la$/ || $entry =~ /\.o/);
1453        $foundfiles{$entry} = 1;
1454    }
1455    closedir (THISDIR);
1456
1457    my @marks = ("EXTRA_DIST", "DIST_COMMON", '\S*_SOURCES', '\S*_HEADERS', 'MAINTAINERCLEANFILES', 'CLEANFILES', 'DISTCLEANFILES', '\S*_OBJECTS');
1458    foreach $mark (@marks) {
1459        while ($MakefileData =~ /\n($mark)\s*=\s*([^\n]*)/g) {
1460            foreach $file (split('[\034\s]', $2)) {
1461                $file =~ s/\.\///;
1462                $foundfiles{$file} = 0 if (defined $foundfiles{$file});
1463            }
1464        }
1465    }
1466    my @files = ("Makefile", "config.cache", "config.log", "stamp-h",
1467                 "stamp-h1", "stamp-h1", "config.h", "Makefile", "config.status", "config.h", "libtool");
1468    foreach $file (@files) {
1469        $foundfiles{$file} = 0 if (defined $foundfiles{$file});
1470    }
1471
1472    my $KDE_DIST = "";
1473    foreach $file (keys %foundfiles) {
1474        if ($foundfiles{$file} == 1) {
1475            $KDE_DIST .= "$file ";
1476        }
1477    }
1478    if ($KDE_DIST) {
1479        print "KDE_DIST $printname $KDE_DIST\n" if ($verbose);
1480
1481        my $lookup = "DISTFILES *=(.*)";
1482        if ($MakefileData =~ /\n$lookup\n/o) {
1483            substituteLine($lookup, "KDE_DIST=$KDE_DIST\n\nDISTFILES=$1 \$(KDE_DIST)\n");
1484        }
1485    }
1486}
1487
1488#-----------------------------------------------------------------------------
1489# Returns 0 if the line was processed - 1 otherwise.
1490# Errors are logged in the global $errorflags
1491sub tag_DOCFILES ()
1492{
1493#    if ($MakefileData =~ /\nSUBDIRS\s*=/) { # subdirs
1494#      $MakefileData =~ /\n(.*-recursive:\s*)\n/;
1495#      my $orig_rules = $1;
1496#      my $rules = $orig_rules;
1497#      $rules =~ s/:\s*$//;
1498#      substituteLine($orig_rules, "$rules docs-recursive:");
1499#      appendLines("docs: docs-recursive docs-am\n");
1500#    } else {
1501#      appendLines("docs: docs-am\n");
1502#    }
1503    $target_adds{"all"} .= "docs-am ";
1504
1505    my $lookup = 'KDE_DOCS\s*=\s*([^\n]*)';
1506    goto nodocs    if ($MakefileData !~ /\n$lookup/o);
1507    print STDOUT "KDE_DOCS processing <$1>\n"   if ($verbose);
1508
1509    my $tmp = $1;
1510
1511    # Either find the files in the directory (AUTO) or use
1512    # only the specified po files.
1513    my $files = "";
1514    my $appname = $tmp;
1515    $appname =~ s/^(\S*)\s*.*$/$1/;
1516    if ($appname =~ /AUTO/) {
1517      $appname = basename($makefileDir);
1518      if ("$appname" eq "en") {
1519      	  print STDERR "Error: KDE_DOCS = AUTO relies on the directory name. Yours is 'en' - you most likely want something else, e.g. KDE_DOCS = myapp\n";
1520          exit(1);
1521      }
1522    }
1523
1524    if ($tmp !~ / - /)
1525    {
1526        opendir (THISDIR, ".");
1527	foreach $entry (readdir(THISDIR)) {
1528	  next if ($entry eq "CVS" || $entry =~ /^\./  || $entry =~ /^Makefile/ || $entry =~ /~$/ || $entry =~ /^\#.*\#$/);
1529	  next if (! -f $entry);
1530	  $files .= "$entry ";
1531	}
1532        closedir (THISDIR);
1533        print STDOUT "docfiles found = $files\n"   if ($verbose);
1534    }
1535    else
1536    {
1537        $tmp =~ s/\034/ /g;
1538	$tmp =~ s/^\S*\s*-\s*//;
1539        $files = $tmp;
1540    }
1541    goto nodocs if (!$files);        # Nothing to do
1542
1543    if ($files =~ /(^| )index\.docbook($| )/) {
1544
1545      my $lines = "";
1546      my $lookup = 'MEINPROC\s*=';
1547      if ($MakefileData !~ /\n($lookup)/) {
1548	$lines = "MEINPROC=/\$(kde_bindir)/meinproc\n";
1549      }
1550      $lookup = 'KDE_XSL_STYLESHEET\s*=';
1551      if ($MakefileData !~ /\n($lookup)/) {
1552        $lines .= "KDE_XSL_STYLESHEET=/\$(kde_datadir)/ksgmltools2/customization/kde-chunk.xsl\n";
1553      }
1554      $lookup = '\nindex.cache.bz2:';
1555      if ($MakefileData !~ /\n($lookup)/) {
1556         $lines .= "index.cache.bz2: \$(srcdir)/index.docbook \$(KDE_XSL_STYLESHEET) $files\n";
1557         $lines .= "\t-\@if test -n \"\$(MEINPROC)\"; then echo \$(MEINPROC) --check --cache index.cache.bz2 \$(srcdir)/index.docbook; \$(MEINPROC) --check --cache index.cache.bz2 \$(srcdir)/index.docbook; fi\n";
1558         $lines .= "\n";
1559      }
1560
1561      $lines .= "docs-am: index.cache.bz2\n";
1562      $lines .= "\n";
1563      $lines .= "install-docs: docs-am install-nls\n";
1564      $lines .= "\t\$(mkinstalldirs) \$(DESTDIR)\$(kde_htmldir)/$kdelang/$appname\n";
1565      $lines .= "\t\@if test -f index.cache.bz2; then \\\n";
1566      $lines .= "\techo \$(INSTALL_DATA) index.cache.bz2 \$(DESTDIR)\$(kde_htmldir)/$kdelang/$appname/; \\\n";
1567      $lines .= "\t\$(INSTALL_DATA) index.cache.bz2 \$(DESTDIR)\$(kde_htmldir)/$kdelang/$appname/; \\\n";
1568      $lines .= "\tfi\n";
1569      $lines .= "\t-rm -f \$(DESTDIR)\$(kde_htmldir)/$kdelang/$appname/common\n";
1570      $lines .= "\t\$(LN_S) \$(kde_libs_htmldir)/$kdelang/common \$(DESTDIR)\$(kde_htmldir)/$kdelang/$appname/common\n";
1571
1572      $lines .= "\n";
1573      $lines .= "uninstall-docs:\n";
1574      $lines .= "\t-rm -rf \$(kde_htmldir)/$kdelang/$appname\n";
1575      $lines .= "\n";
1576      $lines .= "clean-docs:\n";
1577      $lines .= "\t-rm -f index.cache.bz2\n";
1578      $lines .= "\n";
1579      $target_adds{"install-data-am"} .= "install-docs ";
1580      $target_adds{"uninstall"} .= "uninstall-docs ";
1581      $target_adds{"clean-am"} .= "clean-docs ";
1582      appendLines ($lines);
1583    } else {
1584      appendLines("docs-am: $files\n");
1585    }
1586
1587    $target_adds{"install-data-am"} .= "install-nls";
1588    $target_adds{"uninstall"} .= "uninstall-nls ";
1589
1590    $tmp = "install-nls:\n";
1591    $tmp .= "\t\$(mkinstalldirs) \$(DESTDIR)\$(kde_htmldir)/$kdelang/$appname\n";
1592    $tmp .= "\t\@for base in $files; do \\\n";
1593    $tmp .= "\t  echo \$(INSTALL_DATA) \$\$base \$(DESTDIR)\$(kde_htmldir)/$kdelang/$appname/\$\$base ;\\\n";
1594    $tmp .= "\t  \$(INSTALL_DATA) \$(srcdir)/\$\$base \$(DESTDIR)\$(kde_htmldir)/$kdelang/$appname/\$\$base ;\\\n";
1595    $tmp .= "\tdone\n";
1596    if ($appname eq 'common') {
1597      $tmp .= "\t\@echo \"merging common and language specific dir\" ;\\\n";
1598      $tmp .= "\tif test ! -e \$(kde_htmldir)/en/common/kde-common.css; then echo 'no english docs found in \$(kde_htmldir)/en/common/'; exit 1; fi \n";
1599      $tmp .= "\t\@com_files=`cd \$(kde_htmldir)/en/common && echo *` ;\\\n";
1600      $tmp .= "\tcd \$(DESTDIR)\$(kde_htmldir)/$kdelang/common ;\\\n";
1601      $tmp .= "\tif test -n \"\$\$com_files\"; then for p in \$\$com_files ; do \\\n";
1602      $tmp .= "\t  case \" $files \" in \\\n";
1603      $tmp .= "\t    *\" \$\$p \"*) ;; \\\n";
1604      $tmp .= "\t    *) test ! -e \$\$p && echo \$(LN_S) ../../en/common/\$\$p \$(DESTDIR)\$(kde_htmldir)/$kdelang/common/\$\$p && \$(LN_S) ../../en/common/\$\$p \$\$p ;; \\\n";
1605      $tmp .= "\t  esac ; \\\n";
1606      $tmp .= "\tdone ; fi ; true\n";
1607    }
1608    $tmp .= "\n";
1609    $tmp .= "uninstall-nls:\n";
1610    $tmp .= "\tfor base in $files; do \\\n";
1611    $tmp .= "\t  rm -f \$(DESTDIR)\$(kde_htmldir)/$kdelang/$appname/\$\$base ;\\\n";
1612    $tmp .= "\tdone\n\n";
1613    appendLines ($tmp);
1614
1615    $target_adds{"distdir"} .= "distdir-nls ";
1616
1617    $tmp = "distdir-nls:\n";
1618    $tmp .= "\tfor file in $files; do \\\n";
1619    $tmp .= "\t  cp \$(srcdir)/\$\$file \$(distdir); \\\n";
1620    $tmp .= "\tdone\n";
1621
1622    appendLines ($tmp);
1623
1624    return 0;
1625
1626  nodocs:
1627    appendLines("docs-am:\n");
1628    return 1;
1629}
1630
1631#-----------------------------------------------------------------------------
1632# Find headers in any of the source directories specified previously, that
1633# are candidates for "moc-ing".
1634sub findMocCandidates ()
1635{
1636    foreach $dir (@headerdirs)
1637    {
1638        my @list = ();
1639        opendir (SRCDIR, "$dir");
1640        @hFiles = grep { /.+\.$hExt$/o } readdir(SRCDIR);
1641        closedir SRCDIR;
1642        foreach $hf (@hFiles)
1643        {
1644	    $hf =~ /(.*)\.[^\.]*$/;          # Find name minus extension
1645	    next if ($uiFiles{$1});
1646            open (HFIN, "$dir/$hf") || die "Could not open $dir/$hf: $!\n";
1647            my $hfsize = 0;
1648            seek(HFIN, 0, 2);
1649            $hfsize = tell(HFIN);
1650            seek(HFIN, 0, 0);
1651            read HFIN, $hfData, $hfsize;
1652            close HFIN;
1653            # push (@list, $hf) if(index($hfData, "Q_OBJECT") >= 0); ### fast but doesn't handle //Q_OBJECT
1654            if ( $hfData =~ /{([^}]*)Q_OBJECT/s ) {              ## handle " { friend class blah; Q_OBJECT "
1655                push (@list, $hf) unless $1 =~ m://[^\n]*Q_OBJECT[^\n]*$:s;  ## handle "// Q_OBJECT"
1656            }
1657        }
1658        # The assoc array of root of headerfile and header filename
1659        foreach $hFile (@list)
1660        {
1661            $hFile =~ /(.*)\.[^\.]*$/;          # Find name minus extension
1662            if ($mocFiles{$1})
1663            {
1664              print STDERR "Warning: Multiple header files found for $1\n";
1665              next;                           # Use the first one
1666            }
1667            $mocFiles{$1} = "$dir\035$hFile";   # Add relative dir
1668        }
1669    }
1670
1671    return 0;
1672}
1673
1674#-----------------------------------------------------------------------------
1675
1676# The programmer has specified a moc list. Prune out the moc candidates
1677# list that we found based on looking at the header files. This generates
1678# a warning if the programmer gets the list wrong, but this doesn't have
1679# to be fatal here.
1680sub pruneMocCandidates ($)
1681{
1682    my %prunedMoc = ();
1683    local @mocList = split(' ', @_[0]);
1684
1685    foreach $mocname (@mocList)
1686    {
1687        $mocname =~ s/\.moc$//;
1688        if ($mocFiles{$mocname})
1689        {
1690            $prunedMoc{$mocname} = $mocFiles{$mocname};
1691        }
1692        else
1693        {
1694            my $print = $makefileDir;
1695            $print =~ s/^\Q$topdir\E\\//;
1696            # They specified a moc file but we can't find a header that
1697            # will generate this moc file. That's possible fatal!
1698            print STDERR "Warning: No moc-able header file for $print/$mocname\n";
1699        }
1700    }
1701
1702    undef %mocFiles;
1703    %mocFiles = %prunedMoc;
1704}
1705
1706#-----------------------------------------------------------------------------
1707
1708# Finds the cpp files (If they exist).
1709# The cpp files get appended to the header file separated by \035
1710sub checkMocCandidates ()
1711{
1712    my @cppFiles;
1713    my $cpp2moc;  # which c++ file includes which .moc files
1714    my $moc2cpp;  # which moc file is included by which c++ files
1715
1716    return unless (keys %mocFiles);
1717    opendir(THISDIR, ".") || return;
1718    @cppFiles = grep { /.+\.$cppExt$/o  && !/.+\.moc\.$cppExt$/o
1719                         && !/.+\.all_$cppExt\.$cppExt$/o } readdir(THISDIR);
1720    closedir THISDIR;
1721    return unless (@cppFiles);
1722    my $files = join (" ", @cppFiles);
1723    $cpp2moc = {};
1724    $moc2cpp = {};
1725    foreach $cxxf (@cppFiles)
1726    {
1727      open (CXXFIN, $cxxf) || die "Could not open $cxxf: $!\n";
1728      seek(CXXFIN, 0, 2);
1729      my $cxxfsize = tell(CXXFIN);
1730      seek(CXXFIN, 0, 0);
1731      read CXXFIN, $cxxfData, $cxxfsize;
1732      close CXXFIN;
1733      while(($cxxfData =~ m/^[ \t]*\#include\s*[<\"](.*\.moc)[>\"]/gm)) {
1734	$cpp2moc->{$cxxf}->{$1} = 1;
1735	$moc2cpp->{$1}->{$cxxf} = 1;
1736      }
1737    }
1738    foreach my $mocFile (keys (%mocFiles))
1739    {
1740	@cppFiles = keys %{$moc2cpp->{"$mocFile.moc"}};
1741        if (@cppFiles == 1) {
1742            $mocFiles{$mocFile} .= "\035" . $cppFiles[0];
1743	    push(@deped, $mocFile);
1744        } elsif (@cppFiles == 0) {
1745            push (@newObs, $mocFile);           # Produce new object file
1746            next    if ($haveAutomocTag);       # This is expected...
1747            # But this is an error we can deal with - let them know
1748            print STDERR
1749                "Warning: No c++ file that includes $mocFile.moc\n";
1750        } else {
1751            # We can't decide which file to use, so it's fatal. Although as a
1752            # guess we could use the mocFile.cpp file if it's in the list???
1753            print STDERR
1754                "Error: Multiple c++ files that include $mocFile.moc\n";
1755            print STDERR "\t",join ("\t", @cppFiles),"\n";
1756            $errorflag = 1;
1757            delete $mocFiles{$mocFile};
1758            # Let's continue and see what happens - They have been told!
1759        }
1760    }
1761}
1762
1763#-----------------------------------------------------------------------------
1764
1765# Add the rules for generating moc source from header files
1766# For Automoc output *.moc.cpp but normally we'll output *.moc
1767# (We must compile *.moc.cpp separately. *.moc files are included
1768# in the appropriate *.cpp file by the programmer)
1769sub addMocRules ()
1770{
1771    my $cppFile;
1772    my $hFile;
1773
1774    foreach $mocFile (keys (%mocFiles))
1775    {
1776        undef $cppFile;
1777        ($dir, $hFile, $cppFile) =  split ("\035", $mocFiles{$mocFile}, 3);
1778        $dir =~ s#^\.#\$(srcdir)#;
1779        if (defined ($cppFile))
1780        {
1781            $target_adds{"\$(srcdir)/$cppFile"} .= "$mocFile.moc ";
1782            appendLines ("$mocFile.moc: $dir/$hFile\n\t\$(MOC) $dir/$hFile -o $mocFile.moc\n");
1783            $cleanMoc .= " $mocFile.moc";
1784        }
1785        else
1786        {
1787            appendLines ("$mocFile$mocExt: $dir/$hFile\n\t\$(MOC) $dir/$hFile -o $mocFile$mocExt\n");
1788            $cleanMoc .= " $mocFile$mocExt";
1789        }
1790    }
1791}
1792
1793sub make_meta_classes ()
1794{
1795    return if ($kdeopts{"qtonly"});
1796
1797    my $cppFile;
1798    my $hFile;
1799    my $moc_class_headers = "";
1800    foreach $program (@programs) {
1801	my $mocs = "";
1802	my @progsources = split(/[\s\034]+/, $sources{$program});
1803	my @depmocs = split(' ', $depedmocs{$program});
1804	my %shash = (), %mhash = ();
1805	@shash{@progsources} = 1;  # we are only interested in the existence
1806	@mhash{@depmocs} = 1;
1807
1808	print STDOUT "program=$program\n" if ($verbose);
1809	print STDOUT "psources=[".join(' ', keys %shash)."]\n" if ($verbose);
1810	print STDOUT "depmocs=[".join(' ', keys %mhash)."]\n" if ($verbose);
1811	print STDOUT "globalmocs=[".join(' ', keys(%globalmocs))."]\n" if ($verbose);
1812	foreach my $mocFile (keys (%globalmocs))
1813	{
1814	    undef $cppFile;
1815	    ($dir, $hFile, $cppFile) = split ("\035", $globalmocs{$mocFile}, 3);
1816	    $dir =~ s#^\.#\$(srcdir)#;
1817	    if (defined ($cppFile))
1818	    {
1819		$mocs .= " $mocFile.moc" if exists $shash{$cppFile};
1820	    }
1821	    else
1822	    {
1823		# Bah. This is the case, if no C++ file includes the .moc
1824		# file. We make a .moc.cpp file for that. Unfortunately this
1825		# is not included in the %sources hash, but rather is mentioned
1826		# in %depedmocs. If the user wants to use AUTO he can't just
1827		# use an unspecific METAINCLUDES. Instead he must use
1828		# program_METAINCLUDES. Anyway, it's not working real nicely.
1829		# E.g. Its not clear what happens if user specifies two
1830		# METAINCLUDES=AUTO in the same Makefile.am.
1831		$mocs .= " $mocFile.moc.$cxxsuffix"
1832		    if exists $mhash{$mocFile.".moc.$cxxsuffix"};
1833	    }
1834	}
1835	if ($mocs) {
1836	    print STDOUT "==> mocs=[".$mocs."]\n" if ($verbose);
1837	    my $sourcename = $program."_meta_unload";
1838	    my $ext = ($program =~ /_la$/) ? ".lo" : ".o";
1839	    my $srcfile = $sourcename.".$cxxsuffix";
1840	    my $objfile = $sourcename.$ext;
1841	    $moc_class_headers .= " $srcfile";
1842	    my $appl;
1843	    $appl  = "$srcfile: $mocs\n";
1844	    $appl .= "\t\@echo 'creating $srcfile'\n";
1845	    $appl .= "\t\@-rm -f $srcfile\n";
1846	    $appl .= "\t\@if test \${kde_qtver} = 2; then \\\n";
1847	    $appl .= "\t\techo 'static const char * _metalist_$program\[\] = {' > $srcfile ;\\\n";
1848	    $appl .= "\t\tcat $mocs | grep 'char.*className' | ";
1849	    $appl .=  "sed -e 's/.*[^A-Za-z0-9_:]\\([A-Za-z0-9_:]*\\)::className.*\$\$/\\\"\\1\\\",/' | sort | uniq >> $srcfile ;\\\n";
1850	    $appl .= "\t\techo '0};' >> $srcfile ;\\\n";
1851  	    $appl .= "\t\techo '#include <kunload.h>' >> $srcfile ;\\\n";
1852	    $appl .= "\t\techo '_UNLOAD($program)' >> $srcfile ;\\\n";
1853	    $appl .= "\telse echo > $srcfile; fi\n";
1854  	    $appl .= "\n";
1855
1856	    $realObjs{$program} .= " \034" . $objfile . " ";
1857	    $sources{$program} .= " $srcfile";
1858            $sources_changed{$program} = 1;
1859            $dep_files .= " \$(DEPDIR)/$sourcename.P" if($dep_files !~/$sourcename.P/);
1860	    appendLines ($appl);
1861	}
1862	print STDOUT "\n" if $verbose;
1863    }
1864    if ($moc_class_headers) {
1865        appendLines ("$cleantarget-moc-classes:\n\t-rm -f $moc_class_headers\n");
1866        $target_adds{"$cleantarget-am"} .= "$cleantarget-moc-classes ";
1867    }
1868}
1869
1870#-----------------------------------------------------------------------------
1871
1872sub updateMakefile ()
1873{
1874    return if ($dryrun);
1875
1876    open (FILEOUT, "> $makefile")
1877                        || die "Could not create $makefile: $!\n";
1878
1879    print FILEOUT "\# $progId - " . '$Revision: 1.280 $ '  . "\n";
1880    $MakefileData =~ s/\034/\\\n/g;    # Restore continuation lines
1881    print FILEOUT $MakefileData;
1882    close FILEOUT;
1883}
1884
1885#-----------------------------------------------------------------------------
1886
1887# The given line needs to be removed from the makefile
1888# Do this by adding the special "removed line" comment at the line start.
1889sub removeLine ($$)
1890{
1891    my ($lookup, $old) = @_;
1892
1893    $old =~ s/\034/\\\n#>- /g;          # Fix continuation lines
1894    $MakefileData =~ s/\n$lookup/\n#>\- $old/;
1895}
1896
1897#-----------------------------------------------------------------------------
1898
1899# Replaces the old line with the new line
1900# old line(s) are retained but tagged as removed. The new line(s) have the
1901# "added" tag placed before it.
1902sub substituteLine ($$)
1903{
1904    my ($lookup, $new) = @_;
1905
1906    if ($MakefileData =~ /\n($lookup)/) {
1907      $old = $1;
1908      $old =~ s/\034/\\\n#>\- /g;         # Fix continuation lines
1909      $new =~ s/\034/\\\n/g;
1910      my $newCount = ($new =~ tr/\n//) + 1;
1911      $MakefileData =~ s/\n$lookup/\n#>- $old\n#>\+ $newCount\n$new/;
1912    } else {
1913      print STDERR "Warning: substitution of \"$lookup\" in $printname failed\n";
1914    }
1915}
1916
1917#-----------------------------------------------------------------------------
1918
1919# Slap new lines on the back of the file.
1920sub appendLines ($)
1921{
1922  my ($new) = @_;
1923  $new =~ s/\034/\\\n/g;        # Fix continuation lines
1924  my $newCount = ($new =~ tr/\n//) + 1;
1925  $MakefileData .= "\n#>\+ $newCount\n$new";
1926}
1927
1928#-----------------------------------------------------------------------------
1929
1930# Restore the Makefile.in to the state it was before we fiddled with it
1931sub restoreMakefile ()
1932{
1933    $MakefileData =~ s/# $progId[^\n\034]*[\n\034]*//g;
1934    # Restore removed lines
1935    $MakefileData =~ s/([\n\034])#>\- /$1/g;
1936    # Remove added lines
1937    while ($MakefileData =~ /[\n\034]#>\+ ([^\n\034]*)/)
1938    {
1939        my $newCount = $1;
1940        my $removeLines = "";
1941        while ($newCount--) {
1942            $removeLines .= "[^\n\034]*([\n\034]|)";
1943        }
1944        $MakefileData =~ s/[\n\034]#>\+.*[\n\034]$removeLines/\n/;
1945    }
1946}
1947
1948#-----------------------------------------------------------------------------
1949