summaryrefslogtreecommitdiff
path: root/lib/sbin/f_depend_modules.pl
blob: feb59e5e64513c4ce5c479e15d67320e905c6ecc (plain)
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/usr/bin/perl -w

#/*@@
#  @file    f_depend_modules.pl
#  @author  Erik Schnetter
#  @date    19 January 2004
#  @desc
#           Create dependencies for Fortran 90 "use" and "include" statements
#  @enddesc
#  @version $Header$
# @@*/



use strict;

my $srcfile = $ARGV[0];
my $dest = $ARGV[1];
my $srcdir = $ARGV[2];
my @otherdirs = @ARGV[3..$#ARGV];

my @suffixes = (".f77", ".f", ".f90", ".F77", ".F", ".F90");

print "$dest:";

my %modules;

my $line = 0;
while (<STDIN>)
{
  ++ $line;
  if (/^\s*#\s*(\d+)/)
  {
    # line number directive from C preprocessor
    $line = $1 - 1;
  }
  elsif (/^\s*include\s*['"]([^'"]+)['"]/i)
  {
    # include statement
    my $name = $1;
    print " \\\n  $srcdir/$name";
  }
  elsif (/^\s*module\s+(\w+)/i)
  {
    # begin of a module
    my $name = $1;
    $modules{$name} = 1;
  }
  elsif (/^\s*use\s+(\w+)/i)
  {
    # use statement
    my $name = $1;
    my $found = 0;
    if (! $found)
    {
      # reference to a module in this file?
      if ($modules{$name})
      {
        $found = 1;
      }
    }
    if (! $found)
    {
      # reference to a module in this thorn?
      my $dirhdl;
      if( opendir( DIRHDL, "$srcdir" ) )
      {
        while( defined( my $filename = readdir( DIRHDL ) ) )
        {
          loop: foreach my $suffix (@suffixes)
          {
            if( $filename eq "$name$suffix" )
            {
              $found = 1;
              print " \\\n  $filename.o";
              last loop;
            }
          }
        }
        closedir DIRHDL;
      }
    }
    if (! $found)
    {
      # reference to a module in another thorn?
    loop: foreach my $dir (@otherdirs)
      {
        # note: we could also use the SUBDIRS from the make.code.defn here
        foreach my $subdir (".", "include")
        {
          if( opendir( DIRHDL, "$dir/$subdir" ) )
          {
            while( defined( my $filename = readdir( DIRHDL ) ) )
            {
              foreach my $suffix (@suffixes)
              {
                if( $filename eq "$name$suffix.o" )
                {
                  $found = 1;
                  print " \\\n  $dir/$subdir/$filename";
                  last loop;
                }
              }
            }
            closedir DIRHDL;
          }
        }
      }
    }
    if (! $found)
    {
      print STDERR "$srcfile:$line: Warning: While tracing module depencencies: Source file for module \"$name\" not found\n";
      if (@otherdirs)
      {
        print STDERR "   Searched in thorn directory and in [" . join(', ', @otherdirs) . "]\n";
      }
      else
      {
        print STDERR "   Searched in thorn directory only.\n";
      }
    }
  }
}

print "\n";