6f06b1cc7033ca58bc4495581e2a2ca6c24a3358
[ghc-hetmet.git] / utils / mkdependC / mkdependC.prl
1 #
2 # This perl script template assumes that definitions for
3 # the following variables are prepended:
4 #
5 # DEFAULT_TMPDIR CPP BUILDPLATFORM
6 #
7 # ToDo: strip out all the .h junk
8 #
9
10 use File::Temp qw/ tempfile tempdir /;;
11
12 ($Pgm = $0) =~ s/.*\/([^\/]+)$/\1/;
13 $Usage  = "usage: $Pgm: not done yet\n";
14
15 $Status  = 0; # just used for exit() status
16 $Verbose = 0;
17 $Dashdashes_seen = 0;
18
19 $Begin_magic_str = "# DO NOT DELETE: Beginning of C dependencies";
20 $End_magic_str = "# DO NOT DELETE: End of C dependencies";
21 $Obj_suffix = 'o';
22 @Defines = ();
23 $Include_dirs = '';
24 $Makefile = '';
25 @Src_files = ();
26 @File_suffix = ();
27 $baseName='';
28 $ignore_output='> /dev/null';
29
30 if ( ${BUILDPLATFORM} eq "i386-unknown-mingw32" ) {
31     # Assuming the underlying perl uses cmd to exec system() calls.
32     $ignore_output = ">nul";
33 }
34
35 if ( $ENV{'TMPDIR'} ) { # where to make tmp file names
36     $Tmp_prefix = $ENV{'TMPDIR'} . "/mkdependC$$";
37 } else {
38     $Tmp_prefix ="${DEFAULT_TMPDIR}/mkdependC$$";
39     $ENV{'TMPDIR'} = "${DEFAULT_TMPDIR}"; # set the env var as well
40 }
41
42 $tempfile = '';
43
44 sub quit_upon_signal { 
45   if (-f $tempfile) {
46         print STDERR "Deleting $tempfile .. \n"; 
47         unlink $tempfile;
48    }
49 }
50 $SIG{'INT'}  = 'quit_upon_signal';
51 $SIG{'QUIT'} = 'quit_upon_signal';
52
53 &mangle_command_line_args();
54
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";
61 }
62
63 @Depend_lines = ();
64
65 print STDERR "Include_dirs=$Include_dirs\n" if $Verbose;
66
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)$//;
74
75     &slurp_file($sf, 'fh00');
76 }
77
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";
82 } else {
83     $Begin_magic_str = $Begin_magic_str . "\n";
84     $End_magic_str = $End_magic_str . "\n";
85 }
86
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
91 # and adding the new
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
96
97 select(NMKF);
98 $_ = <OMKF>;
99 while ($_ && $_ ne $Begin_magic_str) { # copy through, 'til Begin_magic_str
100     print $_;
101     $_ = <OMKF>;
102 }
103 while ($_ && $_ ne $End_magic_str) { # delete 'til End_magic_str
104     $_ = <OMKF>;
105 }
106 # insert dependencies
107 print $Begin_magic_str;
108 print @Depend_lines;
109 print $End_magic_str;
110 while (<OMKF>) { # copy the rest through
111     print $_;
112 }
113 close(NMKF);
114 close(OMKF);
115 exit 0;
116
117 sub mangle_command_line_args {
118     while($_ = $ARGV[0]) {
119         shift(@ARGV);
120
121         if ( /^--$/ ) {
122             $Dashdashes_seen++;
123
124         } elsif ( /^(-optc)?(-D.*)/ ) { # recognized wherever they occur
125             push(@Defines, $2);
126         } elsif ( /^(-optc)?(-I.*)/ ) {
127             $Include_dirs .= " $2";
128         } elsif ( /^(-optc)?(-isystem.*)/ ) {
129             $Include_dirs .= " $2";
130
131         } elsif ($Dashdashes_seen != 1) { # not between -- ... --
132             if ( /^-v$/ ) {
133                 $Verbose++;
134             } elsif ( /^-f/ ) {
135                 $Makefile       = &grab_arg_arg($_);
136             } elsif ( /^-o/ ) {
137                 $Obj_suffix     = &grab_arg_arg($_);
138             } elsif ( /^-s/ ) {
139                 local($suff)    =  &grab_arg_arg($_);
140                 push(@File_suffix, $suff);
141             } elsif ( /^-bs/ ) {
142                 $Begin_magic_str = &grab_arg_arg($_);
143             } elsif ( /^-es/ ) {
144                 $End_magic_str = &grab_arg_arg($_);
145             } elsif ( /^-w/ ) {
146                 $Width  = &grab_arg_arg($_);
147             } elsif ( /^-/ ) {
148                 print STDERR "$Pgm: unknown option ignored: $_\n";
149             } else {
150                 push(@Src_files, $_);
151             }
152
153         } elsif ($Dashdashes_seen == 1) { # where we ignore unknown options
154             push(@Src_files,$_) if ! /^-/;
155         }
156     }
157 }
158
159 sub grab_arg_arg {
160     local($option) = @_;
161     local($rest_of_arg);
162     
163     ($rest_of_arg = $option) =~ s/^-.//;
164
165     if ($rest_of_arg) {
166         return($rest_of_arg);
167     } elsif ($#ARGV >= 0) {
168         local($temp) = $ARGV[0]; shift(@ARGV); 
169         return($temp);
170     } else {
171         die "$Pgm: no argument following $option option\n";
172     }
173 }
174
175 sub slurp_file { # follows an example in the `open' item in perl man page
176     local($fname,$fhandle) = @_;
177     local($depend,$dep); # tmp
178     local(@Deps);
179
180     $fhandle++; # a string increment
181
182     $fname = &tidy_dir_names($fname);
183
184     ($fh, $tempfile) = tempfile();
185     close $fh;
186
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 $ignore_output");
190  
191     if ($result != 0) {
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 ");
195         if ($result != 0) {
196            unlink($tempfile);
197            exit($result);
198         }
199     };
200
201     local($dep_contents)='';
202     local($deps)='';
203     open($fhandle, $tempfile) || die "$Pgm: Can't open $tempfile: $!\n";
204
205     while (<$fhandle>) {
206        chop;
207        $dep_contents .= $_;
208     }
209     ($deps = $dep_contents) =~ s|^[^:]+:(.*)$|$1|g;
210     $deps =~ s| \\| |g;
211     
212     @Deps = split(/ +/, $deps);
213     
214     $depend = "$baseName.$Obj_suffix";
215     foreach $suff (@File_suffix) {
216          $depend .= " $baseName.${suff}_$Obj_suffix";
217     }
218     
219     foreach $dep (@Deps) {
220         push(@Depend_lines, "$depend: $dep\n") if $dep ne '';
221     }
222
223     close($fhandle);
224     unlink($tempfile);
225     $tempfile = '';  # for quit_upon_signal
226 }
227
228 sub tidy_dir_names { # rm various pernicious dir-name combinations...
229     local($str) = @_;
230
231     $str =~ s|/[^/.][^/]*/\.\.||g;      # nuke: /<dir>/..
232     $str =~ s|/\.[^.][^/]*/\.\.||g;     # nuke: /./.. (and others)
233     $str =~ s|"||g;
234     $str =~ s| \./| |;
235     $str;
236 }