Skip to content

Commit

Permalink
depgen.py: add basic support for Fortran submodules.
Browse files Browse the repository at this point in the history
  • Loading branch information
skosukhin committed Apr 22, 2024
1 parent b1272fd commit 56b608e
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 0 deletions.
1 change: 1 addition & 0 deletions mkhelper.mk.in
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ sanitize-mod-proxies:
extra_f90.d: mkhelper.mk
$(silent_DEPGEN):;{ \
echo 'src/program/main.o:| src/program/implicit_external.o'; \
echo 'src/program/main.o:| src/modules/submo_test_submodule.o'; \
} >$@

# Configure delayed bundled libraries:
Expand Down
4 changes: 4 additions & 0 deletions mkhelper/depgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,9 @@ def module_declare_callback(module):
def module_use_callback(module):
required_modules.add(module)

def submodule_declare_callback(module, submodule):
required_modules.add(module)

lc_debug_info = None
pp_debug_info = None
ftn_debug_info = None
Expand All @@ -478,6 +481,7 @@ def module_use_callback(module):

ftn.include_callback = include_callback
ftn.module_declare_callback = module_declare_callback
ftn.submodule_declare_callback = submodule_declare_callback
ftn.module_use_callback = module_use_callback

def debug_callback(line, msg):
Expand Down
16 changes: 16 additions & 0 deletions mkhelper/depgen/fortran_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ class FortranParser:
_re_module_declare = re.compile(
r"^\s*module\s+(?!(?:procedure|subroutine|function)\s)(\w+)", re.I
)
_re_submodule_declare = re.compile(
r"^\s*submodule\s+\(\s*(\w+)\s*\)\s+(\w+)", re.I)
_re_module_use = re.compile(
r"^\s*use(?:\s+|(?:\s*,\s*((?:non_)?intrinsic))?\s*::\s*)(\w+)", re.I
)
Expand Down Expand Up @@ -73,6 +75,7 @@ def __init__(
# Callbacks:
self.include_callback = None
self.module_declare_callback = None
self.submodule_declare_callback = None
self.module_use_callback = None
self.debug_callback = None

Expand Down Expand Up @@ -124,6 +127,19 @@ def parse(self, stream):
)
continue

# submodule declared
match = FortranParser._re_submodule_declare.match(line)
if match:
module_name = match.group(1).lower()
submodule_name = match.group(2).lower()
if self.submodule_declare_callback:
self.submodule_declare_callback(module_name, submodule_name)
if self.debug_callback:
self.debug_callback(
line, "declared submodule '%s' of module '%s'" % (submodule_name, module_name)
)
continue

# module used
match = FortranParser._re_module_use.match(line)
if match:
Expand Down
18 changes: 18 additions & 0 deletions src/modules/mo_test_submodule.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module mo_test_submodule
implicit none
public

interface
module subroutine print_hello_subroutine()
end subroutine print_hello_subroutine

module function print_hello_function()
integer :: print_hello_function
end function print_hello_function
end interface

contains
subroutine print_hello()
print *, "Hello from submodule test (parent module)."
end subroutine print_hello
end module mo_test_submodule
14 changes: 14 additions & 0 deletions src/modules/submo_test_submodule.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
submodule (mo_test_submodule) submo_test_submodule
implicit none

contains
module subroutine print_hello_subroutine()
print *, "Hello from submodule test (submodule subroutine)."
end subroutine print_hello_subroutine

module function print_hello_function()
integer :: print_hello_function
print_hello_function = 0
print *, "Hello from submodule test (submodule function)."
end function print_hello_function
end submodule submo_test_submodule
8 changes: 8 additions & 0 deletions src/program/main.f90
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
program main

use mo_mkhelper, only: print_mkhelper_hello => print_hello
use mo_test_submodule, only: &
& print_test_submodule_parent => print_hello, &
& print_test_submodule_subroutine => print_hello_subroutine, &
& print_test_submodule_function => print_hello_function
use mo_threaded_hello, only: print_threaded_hello => print_hello
use mo_delayed, only: print_delayed_hello => print_hello
use mo_cmake_based, only: print_cmake_based_hello => print_hello
Expand Down Expand Up @@ -82,6 +86,10 @@ program main

call print_mkhelper_hello()

call print_test_submodule_parent()
call print_test_submodule_subroutine()
retval = print_test_submodule_function()

print *, "Running threaded bundled library..."
retval = print_threaded_hello()
if (retval .ne. 0) stop 2
Expand Down

0 comments on commit 56b608e

Please sign in to comment.