Skip to content

Extend SCons.Scanner.Fortran to add USE statements in functions and subroutines #2784

Open
@bdbaddog

Description

@bdbaddog

This issue was originally created at: 2011-08-25 12:57:26.
This issue was reported by: tcfisher.

tcfisher said at 2011-08-25 12:57:26

The dependencies through 'USE' statements in fortran 90+ seem to only be searched after module statements. However, the fortran standard permits 'USE' inside procedures and many programmers prefer to put 'use' statements at the lowest level possible to avoid improper scoping. It seems to me that if regex defintions for 'function', 'subroutine', and possibly 'interface' were added to SCons.Scanner.Fortran, then this could be easily resolved.

bdbaddog said at 2011-08-25 13:50:00

Can you provide a sample Fortran90 source file for use in making this change? And/or a simple SConstruct which processes it incorrectly now?
Thanks!

tcfisher said at 2011-08-31 06:05:16

The problem is more subtle than I thought, and will likely be something beyond what you were hoping for. It has to do with using "include" for different fortran source files that are in different directories, but have a "use" in them for modules in the current directory.

The directory structure for the contrived case I created (which fails) is:

-> ls src
functions   main.f90   modone.f90   modtwo.f90   SConstruct
-> ls src/functions
func1.f90  func2.f90

The contents of each file is as follows:

main.f90:

program main
  use modtwo, only: func1
  implicit none
 
  real x, y
 
  y = func1(x)
 
end program

modone.f90:

module modone
  implicit none
 
  private
  public func2
 
  contains
 
  include 'func2.f90'
 
end module modone

modtwo.f90:

module modtwo
  implicit none
 
  private
  public func1
 
  contains
 
  include 'func1.f90'
 
end module modtwo

SConstruct:

import os
env = Environment(ENV = os.environ)
env['F90'] = 'gfortran'
env['F90FLAGS'] = '-g'
env['LINK'] = 'gfortran'
env['LINKFLAGS'] = ''
env['F90PATH'] = Split("""
./functions
""")
sharedsrc = Split("""
modtwo.f90
modone.f90
""")
sharedobj = Split("""
modtwo.o
modone.o
""")
# without this explicit dependency, the compilation won't work
# Depends('modtwo.o','modone.o')
progsrc = Split("""
main.f90
""")
extlibs = Split("""
""")
extpaths = Split("""
""")
env.Object(sharedsrc)
test = env.Program(target = 'Test', source = progsrc+sharedobj, LIBS = extlibs,
LIBPATH = extpaths)

func1.f90:

function func1(xin)
  use modone
 
  real, intent(in) :: xin
 
  real func1
 
  func1 = func2(xin)
 
end function func1

func2.f90:

function func2(xin)
  real, intent(in) :: xin
 
  real func2
 
  func2 = xin
 
end function func2

Perhaps the scanner doesn't look for "use" when it looks inside an "include"?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions