-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_explicit_dependencies_for_makefile.sh
executable file
·246 lines (211 loc) · 7.89 KB
/
create_explicit_dependencies_for_makefile.sh
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#!/bin/bash
# it is an useful script to create "makefile_explicit_dependencies"
# reason why you may wnat to use this:
# when you edit some file in Sources/, all dependent functions/subroutines must
# be updated to link program later.
# program make by default does not track dependencies.
# Therefore, you need to type "make clean; make ..." everytime you make changes
# It is allowed to specify such dependencies yourself.
# This script does it for you.
# folder structure
TEST_DIR=$PWD # Sources/Utilities
GENE_DIR=$PWD/../Generate # Generate src folder
CONV_DIR=$PWD/../Convert # Convert src folder
DIVI_DIR=$PWD/../Divide # Divide src folder
PROC_DIR=$PWD/../Process # Process src folder
# tmp file name and location
tmp_file=$PWD/tmp
#-------------------------------------------------#
#--------- READ ABOVE UP TO THIS ROW ---------#
#-------------------------------------------------#
set -e # exit when any command fails
#set -v #Prints shell input lines as they are read.
#set -x #Print command traces before executing command.
#set +x #disable previous line
# keep track of the last executed command
trap 'last_command=$current_command; current_command=$BASH_COMMAND' DEBUG
# echo an error message before exiting
trap 'echo "\"${last_command}\" command filed with exit code $?."' EXIT
#------------------------------------------------------------------------------#
# produces correct module structure
#------------------------------------------------------------------------------#
function module_list() {
# $1 - dir
cd $1
# find file
# with _Mod
# remove ^./
# not ^.
# turn _Mod.f90$ to _Mod.o
# turn _Mod/ to #
# turn #abc/ to ?
# turn ?abc.f90$ to ?*.f90
# remove ^abc/
# turn #abc.f90$ to #*.f90$
# turn # to _Mod
# turn ? to _Mod/*/
# sort reverse unique
# delete empty line
local result=$(find . -type f -print \
| grep -i '_Mod' \
| sed -e 's%^\.\/%%' \
| grep -v '^\.' \
| sed -e 's%_Mod.f90$%_Mod.o%g' \
| sed -e 's%_Mod/%#%' \
| sed -e 's%#.*/%?%g' \
| sed -e 's%\?.*f90$%?*.f90%g' \
| sed -e 's%^.*/%%g' \
| sed -e 's%\#.*.f90$%#*.f90%g' \
| sed -e 's%\#%_Mod/%g' \
| sed -e 's%\?%_Mod/*/%g' \
| sort -ur \
| sed '/^\s*$/d')
echo "$result"
}
#------------------------------------------------------------------------------#
# produces correct module structure
#------------------------------------------------------------------------------#
function search_string_in_list() {
# $1 - string
# $2 - list
# $3 - f90_file name without extension
# $4 - text to append in front
# to deal with "Comm_Mod_Par" and "Cgns_Mod_Par"
str=$(echo "$1" | sed 's%Mod_.*$%Mod%g')
while read -r line_of_list; do # read $2 line by line
if [[ $line_of_list == *"$str"* ]]; then # if string 1 contains string 2
if [[ $line_of_list == *"/*"* ]]; then
echo "$4""$line_of_list" \\
elif [ "${line_of_list:(-5)}" == "Mod.o" ]; then
if [ "$str" != "$3" ]; then
echo "\$(DIR_OBJECT)/""$line_of_list" \\
fi
else
echo "$4""$line_of_list" \\
fi
fi
done <<< "$2"
}
#------------------------------------------------------------------------------#
# make file explicit dependencies constructor
#------------------------------------------------------------------------------#
function make_file_explicit_dependencies() {
# $1 - folder to create makefile list (Generate, Divide, Process, ..)
# create empty file or remove all content
cd $1; cp /dev/null $tmp_file
#--------------------
# search for Modules
#--------------------
proc_mods=$(module_list $1)
#echo -e "$proc_mods"
for folder in "$1" ; do
cd "$folder"
for f90_file in $(find . -print | grep -i .f90); do
base_name=$(basename -- "${f90_file%.*}") #f90_file name without extension
#---------------------------
# determine deps of f90 file
#---------------------------
dependencies='' # all deps for $f90_file are store in this var
#---------------------------------------------------------------------
# dependencies of modules with "module_name/subroutines.f90" structure
#---------------------------------------------------------------------
mod_included_this=$(grep -ie "include " $f90_file \
| cut -d"!" -f1 \
| sed "s%'%%g" \
| sed 's%"%%g' \
| grep -viI '.h"$\|.h$' \
| tr -s ' ' \
| cut -d' ' -f3- \
| sort -ur \
| sed '/^\s*$/d')
# with include
# ignore commented
# remove '
# remove "
# not .h" or .h'
# unite delimiter
# print from 3rd column
# unique
# delete empty line
if [ ! -z "$mod_included_this" ]; then
while read -r mod_included_this_line; do # line of mod_included_this
dependencies=$(echo -e "$dependencies\n$(\
grep -ie "use .*Mod" $mod_included_this_line \
| cut -d"!" -f1 \
| sed 's/\,.*$//' \
| tr -s ' ' \
| cut -d' ' -f3 \
| sort -ur \
| sed '/^\s*$/d')")
# with use and Mod
# ignore commented
# remove anything after ,
# unite delimiter
# print 3rd column
# unique
# delete empty line
done <<< "$mod_included_this"
#echo "$f90_file"
#echo "$f90_file" | sed 's%.f90%%g' | sed 's%./%%g'
dependencies=$(echo -e "$dependencies\n$(echo "$f90_file" | sed 's%.f90%%g' | sed 's%./%%g')")
fi
#------------------------------------------
# dependencies of functions and subroutines
#------------------------------------------
file_included_this=$(grep -ie "use .*Mod" $f90_file \
| cut -d"!" -f1 \
| sed 's/\,.*$//' \
| sed 's%\/.*$%%' \
| sed "s%'%%g" \
| tr -s ' ' \
| cut -d' ' -f3 \
| sort -ur \
| sed '/^\s*$/d')
# with use and Mod
# ignore commented
# remove anything after ,
# remove anything after /
# unite delimiter
# print 3rd column
# unique
# delete empty line
if [ ! -z "$file_included_this" ]; then #if non-empty
if [ ! -z "$dependencies" ]; then #if non-empty
dependencies=$(echo -e "$dependencies\n$file_included_this")
else
dependencies="$file_included_this"
fi
fi
if [ ! -z "$dependencies" ]; then
dependencies=$(echo "$dependencies" | sort -ur \
| sed '/^\s*$/d')
#echo -e "$(basename -- "${f90_file%.*}")"" depends on\n""$dependencies"
fi
#-------------------------------------------------
# search for deps in list proc_mods and shar_mods
# if found : add them to $tmp_file
#-------------------------------------------------
echo '#-- '$f90_file >> $tmp_file
if [ ! -z "$dependencies" ]; then #if non-empty
echo "\$(DIR_OBJECT)/""$base_name".o:\\ >> $tmp_file
while read -r line_of_deps_list; do # line of current.f90
# $1 deps
dep_list=$(search_string_in_list \
"$line_of_deps_list" "$proc_mods" "$base_name" "")
if [ ! -z "$dep_list" ]; then
echo "$dep_list" >> $tmp_file
fi
done <<< "$dependencies"
sed -i '$ s/.$//' $tmp_file
echo '' >> $tmp_file
fi
done #for f90_file
done #for folder
cd $1; mv $tmp_file makefile_explicit_dependencies
}
#------------------------------------------------------------------------------#
# actual script
#------------------------------------------------------------------------------#
echo creating makefile_explicit_dependencies ...
make_file_explicit_dependencies .
echo done