2 # This perl script template assumes that definitions for
3 # the following variables are prepended:
5 # DEFAULT_TMPDIR CPP BUILDPLATFORM
7 # ToDo: strip out all the .h junk
9 ($Pgm = $0) =~ s/.*\/([^\/]+)$/\1/;
10 $Usage = "usage: $Pgm: not done yet\n";
12 $Status = 0; # just used for exit() status
16 $Begin_magic_str = "# DO NOT DELETE: Beginning of C dependencies\n";
17 $End_magic_str = "# DO NOT DELETE: End of C dependencies\n";
25 $ignore_output='> /dev/null';
27 if ( ${BUILDPLATFORM} == "i386-unknown-mingw32" ) {
28 # Assuming the underlying perl uses cmd to exec system() calls.
29 $ignore_output = ">nul";
32 if ( $ENV{'TMPDIR'} ) { # where to make tmp file names
33 $Tmp_prefix = $ENV{'TMPDIR'} . "/mkdependC$$";
35 $Tmp_prefix ="${DEFAULT_TMPDIR}/mkdependC$$";
36 $ENV{'TMPDIR'} = "${DEFAULT_TMPDIR}"; # set the env var as well
41 sub quit_upon_signal {
43 print STDERR "Deleting $tempfile .. \n";
47 $SIG{'INT'} = 'quit_upon_signal';
48 $SIG{'QUIT'} = 'quit_upon_signal';
50 &mangle_command_line_args();
52 if ( ! $Makefile && -f 'makefile' ) {
53 $Makefile = 'makefile';
54 } elsif ( ! $Makefile && -f 'Makefile') {
55 $Makefile = 'Makefile';
56 } elsif ( ! $Makefile) {
57 die "$Pgm: no makefile or Makefile found\n";
62 print STDERR "Include_dirs=$Include_dirs\n" if $Verbose;
64 foreach $sf (@Src_files) {
65 # just like lit-inputter
66 # except it puts each file through CPP and
67 # a de-commenter (not implemented);
68 # builds up @Depend_lines
69 print STDERR "Here we go for source file: $sf\n" if $Verbose;
70 ($baseName = $sf) =~ s/\.(c|hc)$//;
72 &slurp_file($sf, 'fh00');
75 # OK, mangle the Makefile
76 unlink("$Makefile.bak");
77 rename($Makefile,"$Makefile.bak");
78 # now copy Makefile.bak into Makefile, rm'ing old dependencies
80 open(OMKF,"< $Makefile.bak") || die "$Pgm: can't open $Makefile.bak: $!\n";
81 open(NMKF,"> $Makefile") || die "$Pgm: can't open $Makefile: $!\n";
82 binmode(OMKF); # Do not add stupid ^M's to the output on Win32
83 binmode(NMKF); # Do not add stupid ^M's to the output on Win32
87 while ($_ && $_ ne $Begin_magic_str) { # copy through, 'til Begin_magic_str
91 while ($_ && $_ ne $End_magic_str) { # delete 'til End_magic_str
95 print $Begin_magic_str;
98 while (<OMKF>) { # copy the rest through
105 sub mangle_command_line_args {
106 while($_ = $ARGV[0]) {
112 } elsif ( /^(-optc)?(-D.*)/ ) { # recognized wherever they occur
114 } elsif ( /^(-optc)?(-I.*)/ ) {
115 $Include_dirs .= " $2";
117 } elsif ($Dashdashes_seen != 1) { # not between -- ... --
121 $Makefile = &grab_arg_arg($_);
123 $Obj_suffix = &grab_arg_arg($_);
125 local($suff) = &grab_arg_arg($_);
126 push(@File_suffix, $suff);
128 $Begin_magic_str = &grab_arg_arg($_) . "\n";
130 $End_magic_str = &grab_arg_arg($_) . "\n";
132 $Width = &grab_arg_arg($_);
134 print STDERR "$Pgm: unknown option ignored: $_\n";
136 push(@Src_files, $_);
139 } elsif ($Dashdashes_seen == 1) { # where we ignore unknown options
140 push(@Src_files,$_) if ! /^-/;
149 ($rest_of_arg = $option) =~ s/^-.//;
152 return($rest_of_arg);
153 } elsif ($#ARGV >= 0) {
154 local($temp) = $ARGV[0]; shift(@ARGV);
157 die "$Pgm: no argument following $option option\n";
161 sub slurp_file { # follows an example in the `open' item in perl man page
162 local($fname,$fhandle) = @_;
163 local($depend,$dep); # tmp
166 $fhandle++; # a string increment
168 $fname = &tidy_dir_names($fname);
170 ($tempfile = $fname) =~ s/\.[^\.]*$/\.d/;
171 $tempfile =~ s|.*/([^/]+)$|$1|g;
173 # ${CPP} better be 'gcc -E', or the -x option will fail...
174 # ..and the -MM & -MMD.
175 $result = system("${CPP} -MM -MMD $Include_dirs @Defines -x c $fname $ignore_output");
178 # On the cheesy side..we do want to know what went wrong, so
179 # re-run the command.
180 $result = system("${CPP} -MM -MMD $Include_dirs @Defines -x c $fname ");
187 local($dep_contents)='';
189 open($fhandle, $tempfile) || die "$Pgm: Can't open $tempfile: $!\n";
195 ($deps = $dep_contents) =~ s|^[^:]+:(.*)$|$1|g;
198 @Deps = split(/ +/, $deps);
200 $depend = "$baseName.$Obj_suffix";
201 foreach $suff (@File_suffix) {
202 $depend .= " $baseName.${suff}_$Obj_suffix";
205 foreach $dep (@Deps) {
206 push(@Depend_lines, "$depend: $dep\n") if $dep ne '';
211 $tempfile = ''; # for quit_upon_signal
214 sub tidy_dir_names { # rm various pernicious dir-name combinations...
217 $str =~ s|/[^/.][^/]*/\.\.||g; # nuke: /<dir>/..
218 $str =~ s|/\.[^.][^/]*/\.\.||g; # nuke: /./.. (and others)