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
10 use File::Temp qw/ tempfile tempdir /;;
12 ($Pgm = $0) =~ s/.*\/([^\/]+)$/\1/;
13 $Usage = "usage: $Pgm: not done yet\n";
15 $Status = 0; # just used for exit() status
19 $Begin_magic_str = "# DO NOT DELETE: Beginning of C dependencies";
20 $End_magic_str = "# DO NOT DELETE: End of C dependencies";
28 $ignore_output='> /dev/null';
30 if ( ${BUILDPLATFORM} eq "i386-unknown-mingw32" ) {
31 # Assuming the underlying perl uses cmd to exec system() calls.
32 $ignore_output = ">nul";
35 if ( $ENV{'TMPDIR'} ) { # where to make tmp file names
36 $Tmp_prefix = $ENV{'TMPDIR'} . "/mkdependC$$";
38 $Tmp_prefix ="${DEFAULT_TMPDIR}/mkdependC$$";
39 $ENV{'TMPDIR'} = "${DEFAULT_TMPDIR}"; # set the env var as well
44 sub quit_upon_signal {
46 print STDERR "Deleting $tempfile .. \n";
50 $SIG{'INT'} = 'quit_upon_signal';
51 $SIG{'QUIT'} = 'quit_upon_signal';
53 &mangle_command_line_args();
55 if ( ! $Makefile && -f 'makefile' ) {
56 $Makefile = 'makefile';
57 } elsif ( ! $Makefile && -f 'Makefile') {
58 $Makefile = 'Makefile';
59 } elsif ( ! $Makefile) {
60 die "$Pgm: no makefile or Makefile found\n";
65 print STDERR "Include_dirs=$Include_dirs\n" if $Verbose;
67 foreach $sf (@Src_files) {
68 # just like lit-inputter
69 # except it puts each file through CPP and
70 # a de-commenter (not implemented);
71 # builds up @Depend_lines
72 print STDERR "Here we go for source file: $sf\n" if $Verbose;
73 ($baseName = $sf) =~ s/\.(c|hc)$//;
75 &slurp_file($sf, 'fh00');
78 # Tiresome EOL termination issues
79 if ( ${BUILDPLATFORM} eq "i386-unknown-mingw32" ) {
80 $Begin_magic_str = $Begin_magic_str . "\r\n";
81 $End_magic_str = $End_magic_str . "\r\n";
83 $Begin_magic_str = $Begin_magic_str . "\n";
84 $End_magic_str = $End_magic_str . "\n";
87 # OK, mangle the Makefile
88 unlink("$Makefile.bak");
89 rename($Makefile,"$Makefile.bak");
90 # now copy Makefile.bak into Makefile, rm'ing old dependencies
92 open(OMKF,"< $Makefile.bak") || die "$Pgm: can't open $Makefile.bak: $!\n";
93 open(NMKF,"> $Makefile") || die "$Pgm: can't open $Makefile: $!\n";
94 binmode(OMKF); # Do not add stupid ^M's to the output on Win32
95 binmode(NMKF); # Do not add stupid ^M's to the output on Win32
99 while ($_ && $_ ne $Begin_magic_str) { # copy through, 'til Begin_magic_str
103 while ($_ && $_ ne $End_magic_str) { # delete 'til End_magic_str
106 # insert dependencies
107 print $Begin_magic_str;
109 print $End_magic_str;
110 while (<OMKF>) { # copy the rest through
117 sub mangle_command_line_args {
118 while($_ = $ARGV[0]) {
124 } elsif ( /^(-optc)?(-D.*)/ ) { # recognized wherever they occur
126 } elsif ( /^(-optc)?(-I.*)/ ) {
127 $Include_dirs .= " $2";
128 } elsif ( /^(-optc)?(-isystem.*)/ ) {
129 $Include_dirs .= " $2";
131 } elsif ($Dashdashes_seen != 1) { # not between -- ... --
135 $Makefile = &grab_arg_arg($_);
137 $Obj_suffix = &grab_arg_arg($_);
139 local($suff) = &grab_arg_arg($_);
140 push(@File_suffix, $suff);
142 $Begin_magic_str = &grab_arg_arg($_);
144 $End_magic_str = &grab_arg_arg($_);
146 $Width = &grab_arg_arg($_);
148 print STDERR "$Pgm: unknown option ignored: $_\n";
150 push(@Src_files, $_);
153 } elsif ($Dashdashes_seen == 1) { # where we ignore unknown options
154 push(@Src_files,$_) if ! /^-/;
163 ($rest_of_arg = $option) =~ s/^-.//;
166 return($rest_of_arg);
167 } elsif ($#ARGV >= 0) {
168 local($temp) = $ARGV[0]; shift(@ARGV);
171 die "$Pgm: no argument following $option option\n";
175 sub slurp_file { # follows an example in the `open' item in perl man page
176 local($fname,$fhandle) = @_;
177 local($depend,$dep); # tmp
180 $fhandle++; # a string increment
182 $fname = &tidy_dir_names($fname);
184 ($fh, $tempfile) = tempfile(DIR => '.', SUFFIX => '.d');
187 # ${CPP} better be 'gcc -E', or the -x option will fail...
188 # ..and the -MM & -MMD.
189 $result = system("${CPP} -MM -MMD $Include_dirs @Defines -x c $fname -o $tempfile $ignore_output");
192 # On the cheesy side..we do want to know what went wrong, so
193 # re-run the command.
194 $result = system("${CPP} -MM -MMD $Include_dirs @Defines -x c $fname -o $tempfile");
201 local($dep_contents)='';
203 open($fhandle, $tempfile) || die "$Pgm: Can't open $tempfile: $!\n";
209 ($deps = $dep_contents) =~ s|^[^:]+:(.*)$|$1|g;
212 @Deps = split(/ +/, $deps);
214 $depend = "$baseName.$Obj_suffix";
215 foreach $suff (@File_suffix) {
216 $depend .= " $baseName.${suff}_$Obj_suffix";
219 foreach $dep (@Deps) {
220 push(@Depend_lines, "$depend: $dep\n") if $dep ne '';
225 $tempfile = ''; # for quit_upon_signal
228 sub tidy_dir_names { # rm various pernicious dir-name combinations...
231 $str =~ s|/[^/.][^/]*/\.\.||g; # nuke: /<dir>/..
232 $str =~ s|/\.[^.][^/]*/\.\.||g; # nuke: /./.. (and others)