[project @ 2000-09-05 10:16:40 by simonmar]
[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 ( /^-D(.*)/ ) { # recognized wherever they occur
104             push(@Defines, $_);
105         } elsif ( /^-I/ ) {
106             $Include_dirs .= " $_";
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) { # where we ignore unknown options
131             push(@Src_files,$_) if ! /^-/;
132         }
133     }
134 }
135
136 sub grab_arg_arg {
137     local($option) = @_;
138     local($rest_of_arg);
139     
140     ($rest_of_arg = $option) =~ s/^-.//;
141
142     if ($rest_of_arg) {
143         return($rest_of_arg);
144     } elsif ($#ARGV >= 0) {
145         local($temp) = $ARGV[0]; shift(@ARGV); 
146         return($temp);
147     } else {
148         die "$Pgm: no argument following $option option\n";
149     }
150 }
151
152 sub slurp_file { # follows an example in the `open' item in perl man page
153     local($fname,$fhandle) = @_;
154     local($depend,$dep); # tmp
155     local(@Deps);
156
157     $fhandle++; # a string increment
158
159     $fname = &tidy_dir_names($fname);
160
161     ($tempfile = $fname) =~ s/\.[^\.]*$/\.d/;
162     $tempfile =~ s|.*/([^/]+)$|$1|g;
163
164     # ${CPP} better be 'gcc -E', or the -x option will fail...
165     # ..and the -MM & -MMD.
166     $result = system("${CPP} -MM -MMD $Include_dirs @Defines -x c $fname");
167     if ($result != 0) {
168         unlink($tempfile);
169         exit($result);
170     };
171
172     local($dep_contents)='';
173     local($deps)='';
174     open($fhandle, $tempfile) || die "$Pgm: Can't open $tempfile: $!\n";
175
176     while (<$fhandle>) {
177        chop;
178        $dep_contents .= $_;
179     }
180     ($deps = $dep_contents) =~ s|^[^:]+:(.*)$|$1|g;
181     $deps =~ s| \\| |g;
182     
183     @Deps = split(/ +/, $deps);
184     
185     $depend = "$baseName.$Obj_suffix";
186     foreach $suff (@File_suffix) {
187          $depend .= " $baseName.${suff}_$Obj_suffix";
188     }
189     
190     foreach $dep (@Deps) {
191         push(@Depend_lines, "$depend: $dep\n") if $dep ne '';
192     }
193
194     close($fhandle);
195     unlink($tempfile);
196     $tempfile = '';  # for quit_upon_signal
197 }
198
199 sub tidy_dir_names { # rm various pernicious dir-name combinations...
200     local($str) = @_;
201
202     $str =~ s|/[^/.][^/]*/\.\.||g;      # nuke: /<dir>/..
203     $str =~ s|/\.[^.][^/]*/\.\.||g;     # nuke: /./.. (and others)
204     $str =~ s|"||g;
205     $str =~ s| \./| |;
206     $str;
207 }