-
Notifications
You must be signed in to change notification settings - Fork 2
/
gendeps
executable file
·99 lines (80 loc) · 3.11 KB
/
gendeps
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
#!/usr/bin/env python
# Generates module dependancies for a Fortran file
# ================================================
#
# Author: Scott Wales
#
# Copyright 2014 ARC Centre of Excellence for Climate Systems Science
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import argparse
import re
from sys import stdout
parser = argparse.ArgumentParser(description=
'Generate module dependancies for a Fortran file')
parser.add_argument('-o','--output', help='Output filename',
type=argparse.FileType('w'),default=stdout)
parser.add_argument('INPUT', help='Input Fortran file',
type=argparse.FileType('r'))
args = parser.parse_args()
infile = args.INPUT
outfile = args.output
# Regexs to find module references in the code
moduleDef = re.compile('module\s*(\S+)',re.IGNORECASE)
useDef = re.compile('use\s*(\S+)',re.IGNORECASE)
programDef = re.compile('program\s*(\S+)',re.IGNORECASE)
# Start with empty lists
modules = set()
uses = set()
isProgram = False
# Read the file
for line in infile:
# Strip comments
stripped = line.split('!')[0].strip()
# Check if the line matches a module or use, if so add to lists
match = moduleDef.match(stripped)
if match:
modules.add(match.group(1).lower()+'.mod')
match = useDef.match(stripped)
if match:
uses.add(match.group(1).lower()+'.mod')
# Check if the file contains a program
match = programDef.match(stripped)
if match:
isProgram = True
dependencies = uses - modules
# What object file will this create?
basename = re.sub(r'[^/]+/(\S+).[fF]90',r'\1',infile.name)
obj = 'obj/' + basename + '.o'
# Create a list of dependencies for the object file
outfile.write(obj + ':' + ' '.join(dependencies) + '\n')
# Create a list of dependencies for the .mod file
if modules:
outfile.write(' '.join(modules) + ':' + obj + '\n')
# Create a variable listing all the modules this file depends on - when
# expanded this will turn into all of the objects required to make use of this
# file
outfile.write('objectrequirements_' + obj + '=' +
' '.join('$(moduleprovider_%s)'%mod for mod in dependencies)+'\n')
# List of modules provided by the file
outfile.write('modulesprovided_' + obj + '=' +
' '.join(modules)+'\n')
# Create a variable for each module in the file listing all the objects the
# module depends on
for mod in modules:
outfile.write('moduleprovider_'+mod+'='+obj+' '+'$(objectrequirements_'+obj+')\n')
# Add to the list of programs
if isProgram:
outfile.write('PROGRAMS += bin/' + basename + '\n')
infile.close()
outfile.close()