Class: Fortran::Dependencies

Inherits:
Object
  • Object
show all
Defined in:
lib/fortran.rb

Overview

Find Fortran dependencies

Constant Summary collapse

OPTIONS =
{ :search_paths => [ '.', '../lib' ],
:ignore_files => %r{ },
:ignore_modules => %r{ },
:ignore_symlinks => true }
USE_MODULE_REGEX =
/^\s*use\s+(\w+)/i
MODULE_DEF_REGEX =
/^\s*module\s+(\w+)/i
FILE_EXTENSION =
/\.f90$/i

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = OPTIONS) ⇒ Dependencies

Returns a new instance of Dependencies.



25
26
27
28
29
30
31
# File 'lib/fortran.rb', line 25

def initialize( config=OPTIONS )
  @config = config
  OPTIONS.each{ |opt,default| @config[opt] = default unless @config.has_key? opt }
  @parsed, @file_dependencies, @source_files = [], {}, []
  @hash = build_hash_of_modules_in_files
  self
end

Instance Attribute Details

#file_dependenciesObject (readonly)

Returns the value of attribute file_dependencies.



23
24
25
# File 'lib/fortran.rb', line 23

def file_dependencies
  @file_dependencies
end

#source_filesObject (readonly)

Returns the value of attribute source_files.



23
24
25
# File 'lib/fortran.rb', line 23

def source_files
  @source_files
end

Instance Method Details

#build_dictionary_of_modules(files) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/fortran.rb', line 45

def build_dictionary_of_modules( files )
  file_containing_module = {}
  files.each do |file|
    modules_defined_in( file ).each{ |mod| file_containing_module[mod]=file }
  end
  file_containing_module
end

#build_hash_of_modules_in_filesObject



61
62
63
# File 'lib/fortran.rb', line 61

def build_hash_of_modules_in_files
  build_dictionary_of_modules find_fortran_files
end

#dependencies(start) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
# File 'lib/fortran.rb', line 90

def dependencies( start )
  modules = modules_used_in( start )
  @parsed = @parsed || [start]
  new_source_files = modules.collect{ |mod| @hash[mod] }.compact
  makefile_dependency_line(start) +
  new_source_files.collect do |file|
    next if @parsed.include?(file)
    @parsed.push file
    dependencies file
  end.to_s
end

#find_fortran_filesObject



53
54
55
56
57
58
59
# File 'lib/fortran.rb', line 53

def find_fortran_files
  source = @config[:search_paths].map{ |path| Dir[path+"/*.[fF]90"] }
  source.flatten!.uniq!
  source.delete_if{ |file| File.lstat(file).symlink? } if @config[:ignore_symlinks]
  source.delete_if{ |file| file.match @config[:ignore_files] }
  source.map!{ |file| file.sub(/^\.\//,'') }# strip leading ./
end

#makefile_dependency_line(source) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/fortran.rb', line 65

def makefile_dependency_line( source )
  real_source = source.sub(/PHYSICS_DUMMY/,'PHYSICS_MODULES')# FIXME: What's this?!
  source_no_path = File.basename source
  @source_files.push source_no_path.gsub(%r|^.*/|,'')
  output = ''
  if (File.expand_path(source) != File.expand_path(source_no_path))
    output += source_no_path+ ": " + real_source + "\n"
    output += "\tln -sf "+real_source+" .\n"
  end
  output += source.gsub(FILE_EXTENSION, ".o").gsub(%r|^.*/|,'' ) +
            ": " + source.gsub(%r|^.*/|,"" ) 
  modules_used_in( source ).each do |use|
    unless @hash[use]
      unless use.match @config[:ignore_modules]
        $stderr.puts 'Warning: unable to locate module #{use} used in #{source}.'
        $stderr.puts '         set :search_paths or :ignore_module_regex options.'
      end
      next
    end
    output = output + " \\\n " +
             @hash[use].gsub(FILE_EXTENSION, ".o").gsub(%r|^.*/|,'' )
  end
  output+"\n"
end

#modules_defined_in(file) ⇒ Object



39
40
41
42
43
# File 'lib/fortran.rb', line 39

def modules_defined_in( file )
  modules = IO.readlines( file ).map do |line| 
    $1.downcase if line.match MODULE_DEF_REGEX
  end.uniq.compact
end

#modules_used_in(file) ⇒ Object



33
34
35
36
37
# File 'lib/fortran.rb', line 33

def modules_used_in( file )
  modules = IO.readlines( file ).map do |line|
    $1.downcase if line.match USE_MODULE_REGEX
  end.uniq.compact
end

#required_source_files(head_f90) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/fortran.rb', line 114

def required_source_files( head_f90 )
  @parsed.clear
  source_file_dependencies( head_f90 )
  sources = []
  until @file_dependencies.empty? do
    no_dependents_pair = @file_dependencies.detect{ |h,d| d == [] }
    no_dependents = no_dependents_pair.first
    sources.push no_dependents
    @file_dependencies.delete(no_dependents){ |el| "#{el} not found" }
    @file_dependencies.each_value{ |deps| deps.delete(no_dependents) }
  end
  sources
end

#source_file_dependencies(head_f90) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
# File 'lib/fortran.rb', line 102

def source_file_dependencies( head_f90 )
  modules_head_uses = modules_used_in( head_f90 )
  required_f90s = modules_head_uses.map{ |mod| @hash[mod] }.compact
  required_f90s.delete_if{ |f| f == head_f90 }
  @file_dependencies[head_f90] = required_f90s
  required_f90s.each do |required_f90|
    next if @parsed.include?(required_f90)
    source_file_dependencies( required_f90 )
  end
  @parsed.push head_f90
end