2 # This stub of perl assumes that the following two
3 # variables are prepended:
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";
26 if ( $ENV{'TMPDIR'} ) { # where to make tmp file names
27 $Tmp_prefix = $ENV{'TMPDIR'} . "/mkdependC$$";
29 $Tmp_prefix ="${TMPDIR}/mkdependC$$";
30 $ENV{'TMPDIR'} = "${TMPDIR}"; # set the env var as well
35 sub quit_upon_signal {
37 print STDERR "Deleting $tempfile .. \n";
41 $SIG{'INT'} = 'quit_upon_signal';
42 $SIG{'QUIT'} = 'quit_upon_signal';
44 &mangle_command_line_args();
46 if ( ! $Makefile && -f 'makefile' ) {
47 $Makefile = 'makefile';
48 } elsif ( ! $Makefile && -f 'Makefile') {
49 $Makefile = 'Makefile';
50 } elsif ( ! $Makefile) {
51 die "$Pgm: no makefile or Makefile found\n";
56 print STDERR "Include_dirs=$Include_dirs\n" if $Verbose;
58 foreach $sf (@Src_files) {
59 # just like lit-inputter
60 # except it puts each file through CPP and
61 # a de-commenter (not implemented);
62 # builds up @Depend_lines
63 print STDERR "Here we go for source file: $sf\n" if $Verbose;
64 ($baseName = $sf) =~ s/\.(c|hc)$//;
66 &slurp_file($sf, 'fh00');
69 # OK, mangle the Makefile
70 unlink("$Makefile.bak");
71 rename($Makefile,"$Makefile.bak");
72 # now copy Makefile.bak into Makefile, rm'ing old dependencies
74 open(OMKF,"< $Makefile.bak") || die "$Pgm: can't open $Makefile.bak: $!\n";
75 open(NMKF,"> $Makefile") || die "$Pgm: can't open $Makefile: $!\n";
78 while ($_ && $_ ne $Begin_magic_str) { # copy through, 'til Begin_magic_str
82 while ($_ && $_ ne $End_magic_str) { # delete 'til End_magic_str
86 print $Begin_magic_str;
89 while (<OMKF>) { # copy the rest through
96 sub mangle_command_line_args {
97 while($_ = $ARGV[0]) {
103 } elsif ( /^-D(.*)/ ) { # recognized wherever they occur
106 $Include_dirs .= " $_";
108 } elsif ($Dashdashes_seen != 1) { # not between -- ... --
112 $Makefile = &grab_arg_arg($_);
114 $Obj_suffix = &grab_arg_arg($_);
116 local($suff) = &grab_arg_arg($_);
117 push(@File_suffix, $suff);
119 $Begin_magic_str = &grab_arg_arg($_) . "\n";
121 $End_magic_str = &grab_arg_arg($_) . "\n";
123 $Width = &grab_arg_arg($_);
125 print STDERR "$Pgm: unknown option ignored: $_\n";
127 push(@Src_files, $_);
130 } elsif ($Dashdashes_seen == 1) { # where we ignore unknown options
131 push(@Src_files,$_) if ! /^-/;
140 ($rest_of_arg = $option) =~ s/^-.//;
143 return($rest_of_arg);
144 } elsif ($#ARGV >= 0) {
145 local($temp) = $ARGV[0]; shift(@ARGV);
148 die "$Pgm: no argument following $option option\n";
152 sub slurp_file { # follows an example in the `open' item in perl man page
153 local($fname,$fhandle) = @_;
154 local($depend,$dep); # tmp
157 $fhandle++; # a string increment
159 $fname = &tidy_dir_names($fname);
161 ($tempfile = $fname) =~ s/\.[^\.]*$/\.d/;
162 $tempfile =~ s|.*/([^/]+)$|$1|g;
164 # ${CPP} better be 'gcc -E', or the -x option will fail...
165 # ..and the -MM & -MMD.
166 $result = system("${CPP} -MM -MMD $Include_dirs @Defines -x c $fname");
172 local($dep_contents)='';
174 open($fhandle, $tempfile) || die "$Pgm: Can't open $tempfile: $!\n";
180 ($deps = $dep_contents) =~ s|^[^:]+:(.*)$|$1|g;
183 @Deps = split(/ +/, $deps);
185 $depend = "$baseName.$Obj_suffix";
186 foreach $suff (@File_suffix) {
187 $depend .= " $baseName.${suff}_$Obj_suffix";
190 foreach $dep (@Deps) {
191 push(@Depend_lines, "$depend: $dep\n") if $dep ne '';
196 $tempfile = ''; # for quit_upon_signal
199 sub tidy_dir_names { # rm various pernicious dir-name combinations...
202 $str =~ s|/[^/.][^/]*/\.\.||g; # nuke: /<dir>/..
203 $str =~ s|/\.[^.][^/]*/\.\.||g; # nuke: /./.. (and others)