fortran_dependencies.pl 1.53 KB
Newer Older
Lorenz Hüdepohl's avatar
Lorenz Hüdepohl committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#!/usr/bin/perl -w

use strict;

my %defs = ();
my %uses = ();

my $use_re = qr/^\s*use\s+(\S+)\s*$/;
my $def_re = qr/^\s*module\s+(\S+)\s*$/;

sub add_use {
	my ($file, $module) = @_;
	if (defined($defs{$module}) && $defs{$module} eq $file) {
		# do not add self-dependencies
		return;
	}
	if (!defined($uses{$file})) {
		$uses{$file} = { $module => 1 };
	} else {
		$uses{$file}{$module} = 1;
	}
}

sub add_def {
	my ($file, $module) = @_;
	if (!defined($defs{$module})) {
		$defs{$module} = $file;
		if (defined($uses{$file}) && defined($uses{$file}{$module})) {
			delete $uses{$file}{$module};
		}
	} else {
		die "Module $module both defined in $file, $defs{$module}";
	}
}

foreach my $file (@ARGV) {
	my $re;
	my $add;
	my $object;
Lorenz Hüdepohl's avatar
Lorenz Hüdepohl committed
40
	if ($file =~ /^(.*)\.def_mods(\..*)$/) {
Lorenz Hüdepohl's avatar
Lorenz Hüdepohl committed
41
42
		$re = $def_re;
		$add = \&add_def;
Lorenz Hüdepohl's avatar
Lorenz Hüdepohl committed
43
44
		$object = $1 . $2;
	} elsif ($file =~ /^(.*)\.use_mods(\..*)$/) {
Lorenz Hüdepohl's avatar
Lorenz Hüdepohl committed
45
46
		$re = $use_re;
		$add = \&add_use;
Lorenz Hüdepohl's avatar
Lorenz Hüdepohl committed
47
		$object = $1 . $2;
Lorenz Hüdepohl's avatar
Lorenz Hüdepohl committed
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
	} else {
		die "Unrecognized file extension for '$file'";
	}
	open(FILE,"<",$file) || die "\nCan't open $file: $!\n\n";
	while(<FILE>) {
		chomp;
		$_ = lc($_);
		if ($_ =~ $re) {
			&$add($object, $1);
		} else {
			die "Cannot parse module statement '$_', was expecting $re";
		}
	}
	close(FILE)
}

foreach my $object (sort keys %uses) {
65
	for my $m (keys %{$uses{$object}}) {
Lorenz Hüdepohl's avatar
Lorenz Hüdepohl committed
66
67
68
69
70
71
		if (defined $defs{$m}) {
			print "$object: ", $defs{$m}, "\n";
		} elsif (defined($ENV{V}) && $ENV{V} eq "1") {
			print STDERR "Warning: Cannot find definition of module $m in files for current program, might be external\n";
		}
	}
Lorenz Hüdepohl's avatar
Lorenz Hüdepohl committed
72
}