File: automoc

package info (click to toggle)
xxdiff 1%3A4.0.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 4,716 kB
  • ctags: 2,245
  • sloc: cpp: 18,495; python: 6,134; sh: 1,543; ansic: 1,535; perl: 308; lex: 284; yacc: 279; lisp: 250; tcl: 213; makefile: 82
file content (419 lines) | stat: -rw-r--r-- 10,531 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
#!/usr/local/bin/perl

# Changes Makefile.in to work correctly with moc files. When called
# without parameters, automoc works recursively on all Makefile.in in
# and below the current subdirectory. When called with parameters,
# only these Makefile.in are changed.
# The Makefile.in should have an entry with the following syntax:
#
# {program}_METASOURCES = USE_AUTOMOC [{suffix}]
#
# Moc files will have a 'cpp' suffix unless overriden by the optional
# {suffix} parameter.
# Be careful not to use a suffix that isn't used by at least one
# of the main source files in {program}_SOURCES.
#
# 1998-07-23  Harri Porten  <porten@tu-harburg.de>
#       * small bugfixes for MOCSOURCES
# 1998-11-08  Alex Zepeda  <garbanzo@hooked.net>
#	* tweak it so it will run from a "common" directory.
# 1998-11-11  Harri Porten <porten@tu-harburg.de> & David Faure <faure@kde.org>
#       * bug fix for invocation with arguments
# 1998-11-16  Harri Porten <porten@kde.org>
#       * take care of "ar" object files, too.
#         (as proposed by Martin Vogt <mvogt@rhrk.uni-kl.de>)
# 1999-01-13  Harri Porten <porten@kde.org>
#       * implemented {suffix} parameter
# 1999-01-16  Stephan Kulow <coolo@kde.org>
#       * implemented support for included MOC files and command line 
#         parameters

use Cwd;
use File::Find;
use File::Basename;

# TRUE if in verbose mode.
$verbose = 0;

$direct_arguments = 0;

@arglist = @ARGV;

@input_files = ();

$errorflag = 0;

$printname = '';

$lastdirname = '';

@mocnames = ();

$callpath = "$0";

@header_suffixes = ("h", "H", "hh", "hxx");

$cxx_suffix = "";

while (@arglist) {
  if ($arglist[0] eq "--version")
    {
      print "\n\n";
      print "*********************************************\n";
      print "automoc - Software that makes even more sense\n";
      print "*********************************************\n\n";
      print "Welcome to the wonderful world of automoc!\n";
      print "This is really free software, unencumbered by the GPL.\n";
      print "You can do anything you like with it except sueing me.\n";
      print "Copyright 1998 Kalle Dalheimer <kalle\@kde.org>\n";
      print "Concept, design and unnecessary questions about Perl by Matthias Ettrich <ettrich\@kde.org>\n\n";
      print "Making it useful by Stephan Kulow <coolo\@kde.org> and\n";
      print "Harri Porten <porten\@kde.org>";

      exit 0;
    } 
  elsif ($arglist[0] eq '--verbose' || $arglist[0] eq '-v')
    {
      $verbose = 1;
    }
  elsif ($arglist[0] =~ /^-p(.+)$/ || $arglist[0] =~ /^--path=(.+)$/)
    {
      $callpath = "$1/" . basename($0);
    }
  elsif ($arglist[0] =~ /^--help$/ || $arglist[0] =~ /^-h$/)
    {
      print "Usage automoc [OPTION] ... [Makefile.in]...\n";
      print "\n";
      print "Patches Makefile.in generated from automake\n";
      print "\n";
      print "  -v, --verbose      verbosely list files processed\n";
      print "  -h, --help         print this help, then exit\n";
      print "  --version          print version number, then exit\n";
      print "  -p, --path=        use the path to automoc if the path\n";
      print "                         called from is not the one to be used\n";
      
      exit 0;
    }
  else 
    {
      push (@input_files, $arglist[0]);
      $direct_arguments = 1;
    }

  shift @arglist;
}

if ($direct_arguments == 0)
  {
    find( \&add_makefile, cwd() );
  }

$curdir = cwd();
while( @input_files )
  {
    $filename = $input_files[0];
    $errorflag = 0;
    undef @headersourcedirs;
    @headersourcedirs = ('$(srcdir)');
    process_makefile( $filename );
    chdir ( $curdir);
    shift @input_files;
  }

sub add_makefile()
  {
	if( $_ ne "Makefile.in" )
	  {
		return;
	  }
	else
	  {
	    push (@input_files, $File::Find::name );
	  }
  }


sub process_automoc_makefile
  {
    	# find the name of the program
	$search_use_automoc[0] =~ /^([^=]*)_METASOURCES/; 
	$programname = $1;

        $cxx_suffix = "cpp";

        # check whether a suffix is given
	if( $search_use_automoc[0] =~ /USE_AUTOMOC\s+([^\s]+)/ )
          {
              $cxx_suffix = $1;
          }

	$header_list = "";
	foreach $suffix (@header_suffixes) {
	  $header_list .= " *.$suffix";
	}
       	@mocable_files = `grep -l Q_OBJECT $header_list 2> /dev/null`;


	foreach $mocable_file (@mocable_files) {
	  chomp $mocable_file;
	  print STDERR "looking for header file $mocable_file\n" if $verbose;
	  $mocable_file =~ s/\.[^\.]*$//;      # remove the suffix (.h etc)
	  push @mocnames, $mocable_file;
	  push @mocsources, "$mocable_file\.moc\.$cxx_suffix";
	}

	$objectline = $programname . "_OBJECTS";

	LINE: while( <FILEIN> )
	  {
                if( /^#/ ) {
		     print FILEOUT $_;
                     next LINE;
                }
			
		if( /(.*_METASOURCES\s*=\s*)(USE_AUTOMOC)/ )
		  {
		    print FILEOUT "$1 ";
		    foreach $mocsource (@mocsources) {
		      print FILEOUT $mocsource . " ";
		    }
		    print FILEOUT "\n";
		  }
		elsif( /^$objectline/ ) # look for programs and libraries
		  {
			chomp $_;
                        $line = $_;
			$morelines = /\\$/ ? 1 : 0;
                        $line =~ s/\\$//; 

			print FILEOUT "$line ";
			foreach $mocname (@mocnames) {
			  if ( $objectline !~ /_la_OBJECTS$/ ) {
			    print FILEOUT $mocname . ".moc.o ";
			  }
			  else  {
			    print FILEOUT $mocname . ".moc.lo ";
			  }
			}
                        if($morelines) {
                          print FILEOUT "\\";
                        }
			print FILEOUT "\n";
		  }
		else {
		  process_generell_rules($_);
		}
	      }
	
	print FILEOUT "\n\n";
	foreach $file (@mocnames) {
	  foreach $header_suffix (@header_suffixes) {
	    if (-f $file . ".$header_suffix") {
	        print FILEOUT "$file.moc.$cxx_suffix: \$(srcdir)/$file.$header_suffix\n";
	        print FILEOUT "\t\$(MOC) \$(srcdir)/$file.$header_suffix -o $file.moc.$cxx_suffix\n\n";
	        last;
        }
	  }
	}

  }

sub process_generell_rules
  {
    if ( m+cd \$\(top_srcdir\) \&\& \$\(AUTOMAKE\)+ )
      {
	print FILEOUT $_;
	print FILEOUT "\tcd \$(top_srcdir) && perl $callpath $printname\n";
        return;
      }

    if (/^distclean:/) 
      {
	s/^distclean: //;
	print FILEOUT "distclean: distclean-metasources ";
      }

    if (/^[^=]*META_INCLUDES.*=(.*)/)
      {
	chomp $1;
	@list = split(' ', $1);
	for $dir (@list) {
	  $realdir = $dir;
	  $realdir =~ s#\$\(srcdir\)#.#;
	    if (! -d $realdir) {
	      print STDERR "$printname: warning $dir can't be found. Must be a relative path to \$(srcdir)\n";
	    } else {
	      if ($dir !~ /\$\(srcdir\)/) 
		{
		  $dir = "\$(srcdir)/" . $dir; 
		  print STDERR "addded $dir\n";
		}
	      push @headersourcedirs, $dir;
	    }
	}
      }
    
    print FILEOUT $_;
    
  }

sub process_moc_makefile() 
  {
    local ($saw_bk) = 0;

    LINE: while( <FILEIN> )
      {
        if ( /^#/ ) {
		print FILEOUT $_;
		next LINE;
	}
 
	$textline = $_;
	
	if ($textline =~ /^[^=]*METASOURCES.*/ || $saw_bk)
	  {
	    $saw_bk = /\\$/;
	    $line=$textline;
	    $line =~ s/\\$//;
	    $line =~ s/.*METASOURCES.*=[ ]*//;
	    @list = split(' ', $line);
	    foreach $mocname (@list) {
	      if ($mocname !~ /\.moc$/) {
		print STDERR "error: filename $lastdirname/$mocname doesn't end with .moc\n";
                $errorflag = 1;
	      } else {
		$mocname =~ s/\.moc$//;
		push(@mocnames, $mocname);
	      }
	    }

	  } else {
            $saw_bk = 0;
          }
	
	process_generell_rules($textline);
      }

    if (@mocnames) {
      print FILEOUT "\n\n";
    }

    foreach $mocfile (@mocnames) {
      
      print STDERR "Looking for moc file $mocfile.moc\n" if $verbose;
      
      $found = 0;
      
      $header_suffix = "";

      foreach $dir (@headersourcedirs) {
	
	$realdir = $dir;
	$realdir =~ s/\$\(srcdir\)/./;
	
	foreach $suffix (@header_suffixes) {

	  if (-f $realdir . '/' . $mocfile . ".$suffix") 
	    {
	      $header_suffix = $suffix;
	      $sourcedir = $dir;
	      $found = 1;
	      last;
	    }
	}
      }
      
      if ($found == 0) {
	print STDERR "error: no header file found for mocfile $lastdirname/$mocfile.moc\n";
        $errorflag = 1;
      }

      @sourcenames=`grep -l "^#include[ ]*.$mocfile\.moc." *.cpp *.cc *.cxx *.C 2> /dev/null`;

      if (@sourcenames == 1) {
	$sourcename = $sourcenames[0];
	$sourcename =~ s/\n$//;
      } else {
        if (@sourcenames == 0)
        {
            print STDERR "error: no source file found for mocfile $lastdirname/$mocfile.moc\n";
        } else {
            print STDERR "error: Multiple source files found for mocfile $lastdirname/$mocfile.moc\n";
            print STDERR "\t",join ("\t", @sourcenames),"\n";
        }
	$errorflag = 1;
    
      }
      
      print FILEOUT "\$(srcdir)/$sourcename: $mocfile.moc\n";
      print FILEOUT "$mocfile.moc: $sourcedir/$mocfile.$header_suffix\n";
      print FILEOUT "\t\$(MOC) $sourcedir/$mocfile.$header_suffix -o $mocfile.moc\n";
      print FILEOUT "\n";

    }
   
  }

sub process_makefile
  {
    
    $printname = $filename;
    $printname =~ s#^$curdir/##;
    $lastdirname = dirname($filename);
    $lastdirname =~ s#^$curdir/##;

    print STDERR "Processing " .$printname . "\n" if $verbose;
    
    chdir( dirname( $filename ) );
    
    $filename = basename($filename);
    
    $search = `grep '[^=#]*METASOURCES.*=' $filename`;
    return if ($search eq '');

    $search = `grep "DO_NOT_USE_AUTOMOC" $filename`;
    return if ($search);
    
    $newfilename = $filename . ".tmp";

    open( FILEIN, "$filename" ) or die "Could not open $filename: $!\n";
    open( FILEOUT, ">$newfilename" ) or die "Could not open $filename: $!\n"; 
    
    # search for USE_AUTOMOC
    @search_use_automoc = `grep "[^#]*USE_AUTOMOC" $filename`;
    
    if( @search_use_automoc == 0 ) {
      process_moc_makefile();
      $cxx_suffix="";
    } else {
      process_automoc_makefile();
      $cxx_suffix="." . $cxx_suffix;
    }
    
    print FILEOUT "distclean-metasources:\n";
    if (@mocnames) 
      {
	print FILEOUT "\t-rm -f ";
	foreach $mocfile (@mocnames) 
	  {
	    print FILEOUT $mocfile . ".moc$cxx_suffix ";
	  }
      }
    print FILEOUT "\n";

    if ($errorflag == 0) {
      print FILEOUT "\n# DO_NOT_USE_AUTOMOC\n";
      rename $newfilename, $filename;
    } else {
      system("rm $newfilename");
      exit 1;   # cause the make to stop. (added 28/01/99 jbb)
                # (I've a bad feeling about this Stephan!)
    }

    undef @mocsources;
    undef @mocnames;
    
    
  }