mkdependC.prl 5.01 KB
Newer Older
1
2
3
4
#
# This stub of perl assumes that the following two
# variables are prepended:
#
5
# DEFAULT_TMPDIR CPP
6
7
8
9
10
11
12
13
14
15
16
17
#
# ToDo: strip out all the .h junk
#
($Pgm = $0) =~ s/.*\/([^\/]+)$/\1/;
$Usage  = "usage: $Pgm: not done yet\n";

$Status  = 0; # just used for exit() status
$Verbose = 0;
$Dashdashes_seen = 0;

$Begin_magic_str = "# DO NOT DELETE: Beginning of C dependencies\n";
$End_magic_str = "# DO NOT DELETE: End of C dependencies\n";
18
$Obj_suffix = 'o';
19
20
21
22
@Defines = ();
$Include_dirs = '';
$Makefile = '';
@Src_files = ();
23
@File_suffix = ();
sof's avatar
sof committed
24
$baseName='';
25

26
27
28
if ( $ENV{'TMPDIR'} ) { # where to make tmp file names
    $Tmp_prefix = $ENV{'TMPDIR'} . "/mkdependC$$";
} else {
29
30
    $Tmp_prefix ="${DEFAULT_TMPDIR}/mkdependC$$";
    $ENV{'TMPDIR'} = "${DEFAULT_TMPDIR}"; # set the env var as well
sof's avatar
sof committed
31
}
32

sof's avatar
sof committed
33
$tempfile = '';
sof's avatar
sof committed
34

35
sub quit_upon_signal { 
sof's avatar
sof committed
36
37
38
  if (-f $tempfile) {
	print STDERR "Deleting $tempfile .. \n"; 
	unlink $tempfile;
39
   }
40
}
41
42
$SIG{'INT'}  = 'quit_upon_signal';
$SIG{'QUIT'} = 'quit_upon_signal';
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63

&mangle_command_line_args();

if ( ! $Makefile && -f 'makefile' ) {
    $Makefile = 'makefile';
} elsif ( ! $Makefile && -f 'Makefile') {
    $Makefile = 'Makefile';
} elsif ( ! $Makefile) {
    die "$Pgm: no makefile or Makefile found\n";
}

@Depend_lines = ();

print STDERR "Include_dirs=$Include_dirs\n" if $Verbose;

foreach $sf (@Src_files) {
    # just like lit-inputter
    # except it puts each file through CPP and
    # a de-commenter (not implemented);
    # builds up @Depend_lines
    print STDERR "Here we go for source file: $sf\n" if $Verbose;
sof's avatar
sof committed
64
    ($baseName = $sf) =~ s/\.(c|hc)$//;
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102

    &slurp_file($sf, 'fh00');
}

# OK, mangle the Makefile
unlink("$Makefile.bak");
rename($Makefile,"$Makefile.bak");
# now copy Makefile.bak into Makefile, rm'ing old dependencies
# and adding the new
open(OMKF,"< $Makefile.bak") || die "$Pgm: can't open $Makefile.bak: $!\n";
open(NMKF,"> $Makefile") || die "$Pgm: can't open $Makefile: $!\n";
select(NMKF);
$_ = <OMKF>;
while ($_ && $_ ne $Begin_magic_str) { # copy through, 'til Begin_magic_str
    print $_;
    $_ = <OMKF>;
}
while ($_ && $_ ne $End_magic_str) { # delete 'til End_magic_str
    $_ = <OMKF>;
}
# insert dependencies
print $Begin_magic_str;
print @Depend_lines;
print $End_magic_str;
while (<OMKF>) { # copy the rest through
    print $_;
}
close(NMKF);
close(OMKF);
exit 0;

sub mangle_command_line_args {
    while($_ = $ARGV[0]) {
	shift(@ARGV);

	if ( /^--$/ ) {
	    $Dashdashes_seen++;

ken's avatar
ken committed
103
104
105
106
	} elsif ( /^(-optc)?(-D.*)/ ) { # recognized wherever they occur
	    push(@Defines, $2);
	} elsif ( /^(-optc)?(-I.*)/ ) {
	    $Include_dirs .= " $2";
107
108
109
110
111
112
113
114

	} elsif ($Dashdashes_seen != 1) { # not between -- ... --
	    if ( /^-v$/ ) {
		$Verbose++;
	    } elsif ( /^-f/ ) {
		$Makefile	= &grab_arg_arg($_);
	    } elsif ( /^-o/ ) {
		$Obj_suffix	= &grab_arg_arg($_);
115
116
117
	    } elsif ( /^-s/ ) {
		local($suff)	=  &grab_arg_arg($_);
		push(@File_suffix, $suff);
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
	    } elsif ( /^-bs/ ) {
		$Begin_magic_str = &grab_arg_arg($_) . "\n";
	    } elsif ( /^-es/ ) {
		$End_magic_str = &grab_arg_arg($_) . "\n";
	    } elsif ( /^-w/ ) {
		$Width	= &grab_arg_arg($_);
	    } elsif ( /^-/ ) {
		print STDERR "$Pgm: unknown option ignored: $_\n";
	    } else {
		push(@Src_files, $_);
	    }

	} elsif ($Dashdashes_seen == 1) { # where we ignore unknown options
	    push(@Src_files,$_) if ! /^-/;
	}
    }
}

sub grab_arg_arg {
    local($option) = @_;
    local($rest_of_arg);
    
    ($rest_of_arg = $option) =~ s/^-.//;

    if ($rest_of_arg) {
	return($rest_of_arg);
    } elsif ($#ARGV >= 0) {
	local($temp) = $ARGV[0]; shift(@ARGV); 
	return($temp);
    } else {
	die "$Pgm: no argument following $option option\n";
    }
}

sub slurp_file { # follows an example in the `open' item in perl man page
    local($fname,$fhandle) = @_;
sof's avatar
sof committed
154
155
156
    local($depend,$dep); # tmp
    local(@Deps);

157
158
159
160
    $fhandle++; # a string increment

    $fname = &tidy_dir_names($fname);

sof's avatar
sof committed
161
162
    ($tempfile = $fname) =~ s/\.[^\.]*$/\.d/;
    $tempfile =~ s|.*/([^/]+)$|$1|g;
163
164

    # ${CPP} better be 'gcc -E', or the -x option will fail...
sof's avatar
sof committed
165
    # ..and the -MM & -MMD.
166
    $result = system("${CPP} -MM -MMD $Include_dirs @Defines -x c $fname >/dev/null");
167
168
169
170
171
    if ($result != 0) {
	unlink($tempfile);
	exit($result);
    };

sof's avatar
sof committed
172
173
    local($dep_contents)='';
    local($deps)='';
sof's avatar
sof committed
174
    open($fhandle, $tempfile) || die "$Pgm: Can't open $tempfile: $!\n";
175

sof's avatar
sof committed
176
177
178
179
180
    while (<$fhandle>) {
       chop;
       $dep_contents .= $_;
    }
    ($deps = $dep_contents) =~ s|^[^:]+:(.*)$|$1|g;
sof's avatar
sof committed
181
    $deps =~ s| \\| |g;
sof's avatar
sof committed
182
183
184
185
186
187
    
    @Deps = split(/ +/, $deps);
    
    $depend = "$baseName.$Obj_suffix";
    foreach $suff (@File_suffix) {
	 $depend .= " $baseName.${suff}_$Obj_suffix";
188
    }
sof's avatar
sof committed
189
190
191
192
193
    
    foreach $dep (@Deps) {
    	push(@Depend_lines, "$depend: $dep\n") if $dep ne '';
    }

194
    close($fhandle);
sof's avatar
sof committed
195
196
    unlink($tempfile);
    $tempfile = '';  # for quit_upon_signal
197
198
199
200
201
202
203
204
205
206
207
}

sub tidy_dir_names { # rm various pernicious dir-name combinations...
    local($str) = @_;

    $str =~ s|/[^/.][^/]*/\.\.||g;	# nuke: /<dir>/..
    $str =~ s|/\.[^.][^/]*/\.\.||g;	# nuke: /./.. (and others)
    $str =~ s|"||g;
    $str =~ s| \./| |;
    $str;
}