-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmakeDepFromUseInclude.py
executable file
·97 lines (95 loc) · 3.81 KB
/
makeDepFromUseInclude.py
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
#!/usr/bin/env python
#--------------------------------------------------------------------------
# Copyright 2013 Wolfgang Friederich
#
# This file is part of Gemini II.
#
# Gemini II is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# any later version.
#
# Gemini II is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Gemini II. If not, see <http://www.gnu.org/licenses/>.
#----------------------------------------------------------------------------
#-----------------------------------------------------------
# Python module to create dependencies from use and include
# statements in Fortran programs
#
# Usage: makeDepFromUseInclude [[dir] [dir]....]
#
# Search all f90 files in . and given directories and
# create a list of
# basename.o: deps.o lines
# to be included into a Makefile
#-----------------------------------------------------------
import sys
import glob
import re
import os.path
#-----------------------------------------------------------
def search_text_file(datei, pattern):
""" Search each line of a text file for a pattern.
Start searching at the beginning of the line.
Returns the group(1) match.
"""
matchlist = []
f = open(datei,'r')
for line in f:
m = re.match(pattern,line)
if m is not None:
matchlist.append(m.group(1))
f.close()
return matchlist
#-----------------------------------------------------------
def build_makefile_entry(datei, matchlist, depext):
"""Build a makefile entry.
Form: datei_without_path_and_extension.o: dependencies.
matchlist: list of dependencies
depext: extension to be appended to the dependencies
"""
target = str.split(os.path.basename(datei),'.')[0]+'.o:'
entry = target
for el in set(matchlist):
entry = entry+' '+el+depext
return entry
#-----------------------------------------------------------
dirs = ['.']+sys.argv[1:] # list of directories including current one and those given on command line
#
# search for use statements in f90 files
#
for folder in dirs: # loop through folders
for datei in glob.glob(folder+'/*.f90'): # loop through Fortran 90 files
matchlist = search_text_file(datei,'\tuse (\w+)')
if len(matchlist) != 0:
entry = build_makefile_entry(datei, matchlist,'.o')
print entry
matchlist = search_text_file(datei,'^ {2,4}use (\w+)')
if len(matchlist) != 0:
entry = build_makefile_entry(datei, matchlist,'.o')
print entry
#
# search for include statements in f-files, either tab or four blanks
#
for datei in glob.glob(folder+'/*.f'): # loop through Fortran 77 files
matchlist = search_text_file(datei,'\tinclude \'(\w+)')
if len(matchlist) != 0:
entry = build_makefile_entry(datei, matchlist,'.h')
print entry
matchlist = search_text_file(datei,' include \'(\w+)')
if len(matchlist) != 0:
entry = build_makefile_entry(datei, matchlist,'.h')
print entry
#
# search for include statements in .f.m4-files
#
for datei in glob.glob(folder+'/*.f.m4'): # loop through m4-Fortran 77 files
matchlist = search_text_file(datei,'\tinclude \'(\w+)')
if len(matchlist) != 0:
entry = build_makefile_entry(datei, matchlist,'.h')
print entry