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