d22f6d9a1470545b4c64b2885d3da72ee0f6a641
[ghc-hetmet.git] / glafp-utils / mkdependC / mkdependC.prl
1 #
2 # This stub of perl assumes that the following two
3 # variables are prepended:
4 #
5 # DEFAULT_TMPDIR CPP
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\n";
17 $End_magic_str = "# DO NOT DELETE: End of C dependencies\n";
18 $Obj_suffix = 'o';
19 @Defines = ();
20 $Include_dirs = '';
21 $Makefile = '';
22 @Src_files = ();
23 @File_suffix = ();
24 $baseName='';
25
26 if ( $ENV{'TMPDIR'} ) { # where to make tmp file names
27     $Tmp_prefix = $ENV{'TMPDIR'} . "/mkdependC$$";
28 } else {
29     $Tmp_prefix ="${DEFAULT_TMPDIR}/mkdependC$$";
30     $ENV{'TMPDIR'} = "${DEFAULT_TMPDIR}"; # set the env var as well
31 }
32
33 $tempfile = '';
34
35 sub quit_upon_signal { 
36   if (-f $tempfile) {
37         print STDERR "Deleting $tempfile .. \n"; 
38         unlink $tempfile;
39    }
40 }
41 $SIG{'INT'}  = 'quit_upon_signal';
42 $SIG{'QUIT'} = 'quit_upon_signal';
43
44 &mangle_command_line_args();
45
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";
52 }
53
54 @Depend_lines = ();
55
56 print STDERR "Include_dirs=$Include_dirs\n" if $Verbose;
57
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)$//;
65
66     &slurp_file($sf, 'fh00');
67 }
68
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
73 # and adding the new
74 open(OMKF,"< $Makefile.bak") || die "$Pgm: can't open $Makefile.bak: $!\n";
75 open(NMKF,"> $Makefile") || die "$Pgm: can't open $Makefile: $!\n";
76 binmode(OMKF);  # Do not add stupid ^M's to the output on Win32
77 binmode(NMKF);  # Do not add stupid ^M's to the output on Win32
78
79 select(NMKF);
80 $_ = <OMKF>;
81 while ($_ && $_ ne $Begin_magic_str) { # copy through, 'til Begin_magic_str
82     print $_;
83     $_ = <OMKF>;
84 }
85 while ($_ && $_ ne $End_magic_str) { # delete 'til End_magic_str
86     $_ = <OMKF>;
87 }
88 # insert dependencies
89 print $Begin_magic_str;
90 print @Depend_lines;
91 print $End_magic_str;
92 while (<OMKF>) { # copy the rest through
93     print $_;
94 }
95 close(NMKF);
96 close(OMKF);
97 exit 0;
98
99 sub mangle_command_line_args {
100     while($_ = $ARGV[0]) {
101         shift(@ARGV);
102
103         if ( /^--$/ ) {
104             $Dashdashes_seen++;
105
106         } elsif ( /^(-optc)?(-D.*)/ ) { # recognized wherever they occur
107             push(@Defines, $2);
108         } elsif ( /^(-optc)?(-I.*)/ ) {
109             $Include_dirs .= " $2";
110
111         } elsif ($Dashdashes_seen != 1) { # not between -- ... --
112             if ( /^-v$/ ) {
113                 $Verbose++;
114             } elsif ( /^-f/ ) {
115                 $Makefile       = &grab_arg_arg($_);
116             } elsif ( /^-o/ ) {
117                 $Obj_suffix     = &grab_arg_arg($_);
118             } elsif ( /^-s/ ) {
119                 local($suff)    =  &grab_arg_arg($_);
120                 push(@File_suffix, $suff);
121             } elsif ( /^-bs/ ) {
122                 $Begin_magic_str = &grab_arg_arg($_) . "\n";
123             } elsif ( /^-es/ ) {
124                 $End_magic_str = &grab_arg_arg($_) . "\n";
125             } elsif ( /^-w/ ) {
126                 $Width  = &grab_arg_arg($_);
127             } elsif ( /^-/ ) {
128                 print STDERR "$Pgm: unknown option ignored: $_\n";
129             } else {
130                 push(@Src_files, $_);
131             }
132
133         } elsif ($Dashdashes_seen == 1) { # where we ignore unknown options
134             push(@Src_files,$_) if ! /^-/;
135         }
136     }
137 }
138
139 sub grab_arg_arg {
140     local($option) = @_;
141     local($rest_of_arg);
142     
143     ($rest_of_arg = $option) =~ s/^-.//;
144
145     if ($rest_of_arg) {
146         return($rest_of_arg);
147     } elsif ($#ARGV >= 0) {
148         local($temp) = $ARGV[0]; shift(@ARGV); 
149         return($temp);
150     } else {
151         die "$Pgm: no argument following $option option\n";
152     }
153 }
154
155 sub slurp_file { # follows an example in the `open' item in perl man page
156     local($fname,$fhandle) = @_;
157     local($depend,$dep); # tmp
158     local(@Deps);
159
160     $fhandle++; # a string increment
161
162     $fname = &tidy_dir_names($fname);
163
164     ($tempfile = $fname) =~ s/\.[^\.]*$/\.d/;
165     $tempfile =~ s|.*/([^/]+)$|$1|g;
166
167     # ${CPP} better be 'gcc -E', or the -x option will fail...
168     # ..and the -MM & -MMD.
169     $result = system("${CPP} -MM -MMD $Include_dirs @Defines -x c $fname > /dev/null");
170  
171     if ($result != 0) {
172         # On the cheesy side..we do want to know what went wrong, so
173         # re-run the command. (This also has the beneficial effect
174         # of fixing a redirection problem with gcc-2.95 builds used with
175         # mingw32.)
176         $result = system("${CPP} -MM -MMD $Include_dirs @Defines -x c $fname ");
177         if ($result != 0) {
178            unlink($tempfile);
179            exit($result);
180         }
181     };
182
183     local($dep_contents)='';
184     local($deps)='';
185     open($fhandle, $tempfile) || die "$Pgm: Can't open $tempfile: $!\n";
186
187     while (<$fhandle>) {
188        chop;
189        $dep_contents .= $_;
190     }
191     ($deps = $dep_contents) =~ s|^[^:]+:(.*)$|$1|g;
192     $deps =~ s| \\| |g;
193     
194     @Deps = split(/ +/, $deps);
195     
196     $depend = "$baseName.$Obj_suffix";
197     foreach $suff (@File_suffix) {
198          $depend .= " $baseName.${suff}_$Obj_suffix";
199     }
200     
201     foreach $dep (@Deps) {
202         push(@Depend_lines, "$depend: $dep\n") if $dep ne '';
203     }
204
205     close($fhandle);
206     unlink($tempfile);
207     $tempfile = '';  # for quit_upon_signal
208 }
209
210 sub tidy_dir_names { # rm various pernicious dir-name combinations...
211     local($str) = @_;
212
213     $str =~ s|/[^/.][^/]*/\.\.||g;      # nuke: /<dir>/..
214     $str =~ s|/\.[^.][^/]*/\.\.||g;     # nuke: /./.. (and others)
215     $str =~ s|"||g;
216     $str =~ s| \./| |;
217     $str;
218 }