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