1995-05-04 03:15:53 -07:00
|
|
|
#!/usr/local/bin/perl
|
|
|
|
|
|
|
|
# To scan a Caml Light source file, find all references to external modules
|
|
|
|
# (open Foo or Foo.bar), and output the dependencies on standard output.
|
|
|
|
#
|
|
|
|
# Usage: camldep [-I path] <file> ...
|
|
|
|
|
|
|
|
while ($#ARGV >= 0) {
|
|
|
|
$_ = shift(@ARGV);
|
|
|
|
if (/^-I(.*)$/) {
|
|
|
|
$dir = $1 ? $1 : shift(@ARGV);
|
|
|
|
$dir =~ s|/$||;
|
|
|
|
unshift(@path, $dir);
|
|
|
|
}
|
|
|
|
elsif (/(.*)\.mli$/ || /(.*)\.cmi$/) {
|
|
|
|
do scan_source ($_, "$1.cmi");
|
|
|
|
}
|
|
|
|
elsif (/(.*)\.ml$/ || /(.*)\.cmo$/) {
|
|
|
|
do scan_source ($_, "$1.cmo");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
die "Don't know what to do with $_";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub scan_source {
|
|
|
|
local ($source_name, $target_name) = @_;
|
|
|
|
$modname = $target_name;
|
|
|
|
$modname =~ s|^.*/||;
|
1995-07-10 02:48:27 -07:00
|
|
|
$modname =~ s|\.cm[iox]$||;
|
1995-05-04 03:15:53 -07:00
|
|
|
undef(%imports);
|
|
|
|
open(SRC, $source_name) || return;
|
|
|
|
while(<SRC>) {
|
|
|
|
if (m/\bopen\s*([A-Z][a-zA-Z0-9_]*)\b/) {
|
|
|
|
$imports{$1} = 1;
|
|
|
|
}
|
|
|
|
while(m/\b([A-Z][a-zA-Z0-9_]*)\./) {
|
|
|
|
$imports{$1} = 1;
|
|
|
|
$_ = $';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close(SRC);
|
|
|
|
undef(@deps);
|
1995-07-10 02:48:27 -07:00
|
|
|
undef(@optdeps);
|
1995-05-04 03:15:53 -07:00
|
|
|
if ($target_name =~ m/(.*)\.cmo$/ && -r ($source_name . "i")) {
|
|
|
|
push(@deps, "$1.cmi");
|
1995-07-10 02:48:27 -07:00
|
|
|
push(@optdeps, "$1.cmi");
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
foreach $modl (keys(%imports)) {
|
|
|
|
$modl = do lowercase($modl);
|
|
|
|
next if ($modl eq $modname);
|
|
|
|
if ($dep = do find_path ("$modl.mli")) {
|
1995-07-12 07:29:13 -07:00
|
|
|
$dep =~ s/\.mli$//;
|
|
|
|
push(@deps, "$dep.cmi");
|
|
|
|
if (-f "$dep.ml") {
|
|
|
|
push(@optdeps, "$dep.cmx");
|
|
|
|
} else {
|
|
|
|
push(@optdeps, "$dep.cmi");
|
|
|
|
}
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
elsif ($dep = do find_path ("$modl.ml")) {
|
1995-07-12 07:29:13 -07:00
|
|
|
$dep =~ s/\.ml$//;
|
|
|
|
push(@deps, "$dep.cmo");
|
|
|
|
push(@optdeps, "$dep.cmx");
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($#deps >= 0) {
|
|
|
|
print "$target_name: ";
|
1995-07-10 02:48:27 -07:00
|
|
|
do print_deps(@deps);
|
|
|
|
}
|
|
|
|
if ($target_name =~ /^(.*)\.cmo$/ && $#optdeps >= 0) {
|
|
|
|
print "$1.cmx: ";
|
|
|
|
do print_deps(@optdeps);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub print_deps {
|
|
|
|
$col = length($target_name) + 3;
|
|
|
|
foreach $dep (@_) {
|
|
|
|
next if $dep eq $target_name;
|
|
|
|
$col += length($dep) + 1;
|
|
|
|
if ($col >= 77) {
|
|
|
|
print "\\\n ";
|
|
|
|
$col = length($dep) + 5;
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
1995-07-10 02:48:27 -07:00
|
|
|
print $dep, " ";
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
1995-07-10 02:48:27 -07:00
|
|
|
print "\n";
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
sub find_path {
|
|
|
|
local ($filename) = @_;
|
|
|
|
return $filename if (-r $filename);
|
|
|
|
foreach $dir (@path) {
|
|
|
|
return "$dir/$filename" if (-r "$dir/$filename");
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub lowercase {
|
|
|
|
local ($_) = @_;
|
|
|
|
m/^(.)(.*)$/;
|
|
|
|
$hd = $1;
|
|
|
|
$tl = $2;
|
|
|
|
$hd =~ tr/A-Z/a-z/;
|
|
|
|
return $hd . $tl;
|
|
|
|
}
|