Class: Inline::Fortran

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

Constant Summary collapse

VERSION =
'1.0.0'

Instance Method Summary collapse

Constructor Details

#initialize(mod) ⇒ Fortran

Returns a new instance of Fortran.



11
12
13
14
# File 'lib/inline/fortran.rb', line 11

def initialize(mod)
  @mod = mod
  @functions = {}
end

Instance Method Details

#buildObject



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
# File 'lib/inline/fortran.rb', line 24

def build
  c_builder = Inline::C.new(@mod)

  src_name = "#{Inline.directory}/#{@mod}.f90"
  File.open(src_name, "w") do |f|
    @functions.each do |name, (signature, src)|

      src.sub!(/#{name}/, "f_#{name}")
      
      args = []
      call_args = []
      return_type = signature.shift

      signature.each_with_index do |arg_type, i|
        args << "#{arg_type} arg#{i}"
        call_args << "&arg#{i}"
      end
      
      c_func = "#{return_type} #{name}(#{args.join(', ')})"
      c_builder.c <<-EOF
        #{c_func} {
          f_#{name}(#{call_args.join(', ')});
        }
      EOF

      f.puts src
      f.puts
    end
  end

  Dir.chdir File.dirname(src_name) do
    cmd = "g95 -c -fno-underscoring #{File.basename(src_name)}"
    $stderr.puts cmd if $DEBUG
    system cmd
    c_builder.add_link_flags "#{@mod}.o"
    c_builder.add_link_flags "-lf95"
    c_builder.build
    c_builder.load
  end
end

#loadObject



64
65
# File 'lib/inline/fortran.rb', line 64

def load
end

#load_cacheObject



20
21
22
# File 'lib/inline/fortran.rb', line 20

def load_cache
  return false
end

#subroutine(subroutine_name, c_signature, src) ⇒ Object



16
17
18
# File 'lib/inline/fortran.rb', line 16

def subroutine(subroutine_name, c_signature, src)
  @functions[subroutine_name] = [c_signature, src]
end