[project @ 2004-01-26 09:56:29 by simonmar]
[ghc-hetmet.git] / glafp-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\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 $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 # OK, mangle the Makefile
76 unlink("$Makefile.bak");
77 rename($Makefile,"$Makefile.bak");
78 # now copy Makefile.bak into Makefile, rm'ing old dependencies
79 # and adding the new
80 open(OMKF,"< $Makefile.bak") || die "$Pgm: can't open $Makefile.bak: $!\n";
81 open(NMKF,"> $Makefile") || die "$Pgm: can't open $Makefile: $!\n";
82 binmode(OMKF);  # Do not add stupid ^M's to the output on Win32
83 binmode(NMKF);  # Do not add stupid ^M's to the output on Win32
84
85 select(NMKF);
86 $_ = <OMKF>;
87 while ($_ && $_ ne $Begin_magic_str) { # copy through, 'til Begin_magic_str
88     print $_;
89     $_ = <OMKF>;
90 }
91 while ($_ && $_ ne $End_magic_str) { # delete 'til End_magic_str
92     $_ = <OMKF>;
93 }
94 # insert dependencies
95 print $Begin_magic_str;
96 print @Depend_lines;
97 print $End_magic_str;
98 while (<OMKF>) { # copy the rest through
99     print $_;
100 }
101 close(NMKF);
102 close(OMKF);
103 exit 0;
104
105 sub mangle_command_line_args {
106     while($_ = $ARGV[0]) {
107         shift(@ARGV);
108
109         if ( /^--$/ ) {
110             $Dashdashes_seen++;
111
112         } elsif ( /^(-optc)?(-D.*)/ ) { # recognized wherever they occur
113             push(@Defines, $2);
114         } elsif ( /^(-optc)?(-I.*)/ ) {
115             $Include_dirs .= " $2";
116
117         } elsif ($Dashdashes_seen != 1) { # not between -- ... --
118             if ( /^-v$/ ) {
119                 $Verbose++;
120             } elsif ( /^-f/ ) {
121                 $Makefile       = &grab_arg_arg($_);
122             } elsif ( /^-o/ ) {
123                 $Obj_suffix     = &grab_arg_arg($_);
124             } elsif ( /^-s/ ) {
125                 local($suff)    =  &grab_arg_arg($_);
126                 push(@File_suffix, $suff);
127             } elsif ( /^-bs/ ) {
128                 $Begin_magic_str = &grab_arg_arg($_) . "\n";
129             } elsif ( /^-es/ ) {
130                 $End_magic_str = &grab_arg_arg($_) . "\n";
131             } elsif ( /^-w/ ) {
132                 $Width  = &grab_arg_arg($_);
133             } elsif ( /^-/ ) {
134                 print STDERR "$Pgm: unknown option ignored: $_\n";
135             } else {
136                 push(@Src_files, $_);
137             }
138
139         } elsif ($Dashdashes_seen == 1) { # where we ignore unknown options
140             push(@Src_files,$_) if ! /^-/;
141         }
142     }
143 }
144
145 sub grab_arg_arg {
146     local($option) = @_;
147     local($rest_of_arg);
148     
149     ($rest_of_arg = $option) =~ s/^-.//;
150
151     if ($rest_of_arg) {
152         return($rest_of_arg);
153     } elsif ($#ARGV >= 0) {
154         local($temp) = $ARGV[0]; shift(@ARGV); 
155         return($temp);
156     } else {
157         die "$Pgm: no argument following $option option\n";
158     }
159 }
160
161 sub slurp_file { # follows an example in the `open' item in perl man page
162     local($fname,$fhandle) = @_;
163     local($depend,$dep); # tmp
164     local(@Deps);
165
166     $fhandle++; # a string increment
167
168     $fname = &tidy_dir_names($fname);
169
170     ($tempfile = $fname) =~ s/\.[^\.]*$/\.d/;
171     $tempfile =~ s|.*/([^/]+)$|$1|g;
172
173     # ${CPP} better be 'gcc -E', or the -x option will fail...
174     # ..and the -MM & -MMD.
175     $result = system("${CPP} -MM -MMD $Include_dirs @Defines -x c $fname $ignore_output");
176  
177     if ($result != 0) {
178         # On the cheesy side..we do want to know what went wrong, so
179         # re-run the command.
180         $result = system("${CPP} -MM -MMD $Include_dirs @Defines -x c $fname ");
181         if ($result != 0) {
182            unlink($tempfile);
183            exit($result);
184         }
185     };
186
187     local($dep_contents)='';
188     local($deps)='';
189     open($fhandle, $tempfile) || die "$Pgm: Can't open $tempfile: $!\n";
190
191     while (<$fhandle>) {
192        chop;
193        $dep_contents .= $_;
194     }
195     ($deps = $dep_contents) =~ s|^[^:]+:(.*)$|$1|g;
196     $deps =~ s| \\| |g;
197     
198     @Deps = split(/ +/, $deps);
199     
200     $depend = "$baseName.$Obj_suffix";
201     foreach $suff (@File_suffix) {
202          $depend .= " $baseName.${suff}_$Obj_suffix";
203     }
204     
205     foreach $dep (@Deps) {
206         push(@Depend_lines, "$depend: $dep\n") if $dep ne '';
207     }
208
209     close($fhandle);
210     unlink($tempfile);
211     $tempfile = '';  # for quit_upon_signal
212 }
213
214 sub tidy_dir_names { # rm various pernicious dir-name combinations...
215     local($str) = @_;
216
217     $str =~ s|/[^/.][^/]*/\.\.||g;      # nuke: /<dir>/..
218     $str =~ s|/\.[^.][^/]*/\.\.||g;     # nuke: /./.. (and others)
219     $str =~ s|"||g;
220     $str =~ s| \./| |;
221     $str;
222 }