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";
17 $End_magic_str = "# DO NOT DELETE: End of C dependencies";
25 $ignore_output='> /dev/null';
27 if ( ${BUILDPLATFORM} eq "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 # Tiresome EOL termination issues
76 if ( ${BUILDPLATFORM} eq "i386-unknown-mingw32" ) {
77 $Begin_magic_str = $Begin_magic_str . "\r\n";
78 $End_magic_str = $End_magic_str . "\r\n";
80 $Begin_magic_str = $Begin_magic_str . "\n";
81 $End_magic_str = $End_magic_str . "\n";
84 # OK, mangle the Makefile
85 unlink("$Makefile.bak");
86 rename($Makefile,"$Makefile.bak");
87 # now copy Makefile.bak into Makefile, rm'ing old dependencies
89 open(OMKF,"< $Makefile.bak") || die "$Pgm: can't open $Makefile.bak: $!\n";
90 open(NMKF,"> $Makefile") || die "$Pgm: can't open $Makefile: $!\n";
91 binmode(OMKF); # Do not add stupid ^M's to the output on Win32
92 binmode(NMKF); # Do not add stupid ^M's to the output on Win32
96 while ($_ && $_ ne $Begin_magic_str) { # copy through, 'til Begin_magic_str
100 while ($_ && $_ ne $End_magic_str) { # delete 'til End_magic_str
103 # insert dependencies
104 print $Begin_magic_str;
106 print $End_magic_str;
107 while (<OMKF>) { # copy the rest through
114 sub mangle_command_line_args {
115 while($_ = $ARGV[0]) {
121 } elsif ( /^(-optc)?(-D.*)/ ) { # recognized wherever they occur
123 } elsif ( /^(-optc)?(-I.*)/ ) {
124 $Include_dirs .= " $2";
126 } elsif ($Dashdashes_seen != 1) { # not between -- ... --
130 $Makefile = &grab_arg_arg($_);
132 $Obj_suffix = &grab_arg_arg($_);
134 local($suff) = &grab_arg_arg($_);
135 push(@File_suffix, $suff);
137 $Begin_magic_str = &grab_arg_arg($_);
139 $End_magic_str = &grab_arg_arg($_);
141 $Width = &grab_arg_arg($_);
143 print STDERR "$Pgm: unknown option ignored: $_\n";
145 push(@Src_files, $_);
148 } elsif ($Dashdashes_seen == 1) { # where we ignore unknown options
149 push(@Src_files,$_) if ! /^-/;
158 ($rest_of_arg = $option) =~ s/^-.//;
161 return($rest_of_arg);
162 } elsif ($#ARGV >= 0) {
163 local($temp) = $ARGV[0]; shift(@ARGV);
166 die "$Pgm: no argument following $option option\n";
170 sub slurp_file { # follows an example in the `open' item in perl man page
171 local($fname,$fhandle) = @_;
172 local($depend,$dep); # tmp
175 $fhandle++; # a string increment
177 $fname = &tidy_dir_names($fname);
179 ($tempfile = $fname) =~ s/\.[^\.]*$/\.d/;
180 $tempfile =~ s|.*/([^/]+)$|$1|g;
182 # ${CPP} better be 'gcc -E', or the -x option will fail...
183 # ..and the -MM & -MMD.
184 $result = system("${CPP} -MM -MMD $Include_dirs @Defines -x c $fname $ignore_output");
187 # On the cheesy side..we do want to know what went wrong, so
188 # re-run the command.
189 $result = system("${CPP} -MM -MMD $Include_dirs @Defines -x c $fname ");
196 local($dep_contents)='';
198 open($fhandle, $tempfile) || die "$Pgm: Can't open $tempfile: $!\n";
204 ($deps = $dep_contents) =~ s|^[^:]+:(.*)$|$1|g;
207 @Deps = split(/ +/, $deps);
209 $depend = "$baseName.$Obj_suffix";
210 foreach $suff (@File_suffix) {
211 $depend .= " $baseName.${suff}_$Obj_suffix";
214 foreach $dep (@Deps) {
215 push(@Depend_lines, "$depend: $dep\n") if $dep ne '';
220 $tempfile = ''; # for quit_upon_signal
223 sub tidy_dir_names { # rm various pernicious dir-name combinations...
226 $str =~ s|/[^/.][^/]*/\.\.||g; # nuke: /<dir>/..
227 $str =~ s|/\.[^.][^/]*/\.\.||g; # nuke: /./.. (and others)