1 |
#! /usr/bin/env bash |
2 |
|
3 |
mkfile='Makefile' |
4 |
if [ $# -ge 1 ] ; then |
5 |
if test $1 = '-h' ; then |
6 |
echo "Usage: `basename $0` MAKEFILE (default: 'Makefile') ; [-h] print this" |
7 |
exit 9 |
8 |
fi |
9 |
mkfile=$1 |
10 |
fi |
11 |
|
12 |
if test ! -f $mkfile ; then |
13 |
echo "`basename $0` error: missing file '$mkfile'" |
14 |
exit 8 |
15 |
fi |
16 |
|
17 |
cmdline=`grep '^# .*genmake2 ' $mkfile | sed 's/^# *//'` |
18 |
echo " get Command-Line from file '$mkfile' :" |
19 |
echo $cmdline |
20 |
|
21 |
printf "=== Parse Options ===\n" |
22 |
CMDLINE= |
23 |
MODS= |
24 |
flg=0 |
25 |
MPI=0 |
26 |
prev= |
27 |
dbq=0 ; sgq=0 ; keep= |
28 |
# put newline between word in cmdline: |
29 |
# CMDLINE=`echo $cmdline | awk '{ for(i=1;i<=NF;i++) print $i}'` |
30 |
#echo "CMDLINE='$CMDLINE'" |
31 |
for xx in $cmdline |
32 |
do |
33 |
arg=`echo $xx` |
34 |
# glue multiple words argument (unbalanced starting & ending double quote) |
35 |
n1=`echo $xx | grep -c '^"'` |
36 |
n2=`echo $xx | grep -c '"$'` |
37 |
if [ $n1 -eq 1 -a $n2 -eq 0 ] ; then |
38 |
dbq=1; keep=$xx |
39 |
continue |
40 |
fi |
41 |
if [ $n1 -eq 0 -a $dbq -eq 1 ] ; then |
42 |
if [ $n2 -eq 1 ] ; then |
43 |
n1=$dbq ; dbq=0 |
44 |
xx="$keep $xx" |
45 |
else |
46 |
keep="$keep $xx" |
47 |
continue |
48 |
fi |
49 |
fi |
50 |
# remove starting & ending double quote |
51 |
if [ $n1 -eq 1 -a $n2 -eq 1 ] ; then |
52 |
arg=`echo $xx | sed 's/^"//' | sed 's/"$//'` |
53 |
fi |
54 |
# glue multiple words argument (unbalanced starting & ending single quote) |
55 |
n1=`echo $xx | grep -c "^'"` |
56 |
n2=`echo $xx | grep -c "'$"` |
57 |
if [ $n1 -eq 1 -a $n2 -eq 0 ] ; then |
58 |
sgq=1; keep=$xx |
59 |
continue |
60 |
fi |
61 |
if [ $n1 -eq 0 -a $sgq -eq 1 ] ; then |
62 |
if [ $n2 -eq 1 ] ; then |
63 |
n1=$sgq ; sgq=0 |
64 |
xx="$keep $xx" |
65 |
else |
66 |
keep="$keep $xx" |
67 |
continue |
68 |
fi |
69 |
fi |
70 |
# remove starting & ending single quote |
71 |
if [ $n1 -eq 1 -a $n2 -eq 1 ] ; then |
72 |
arg=`echo $xx | sed "s/^'//" | sed "s/'$//"` |
73 |
fi |
74 |
# process "arg" |
75 |
opt=`expr "x$arg" : 'x[^=]*=\(.*\)'` |
76 |
# echo "xx='$xx' ; arg='$arg' ; opt='$opt'" |
77 |
# sav "arg" to CMDLINE <- not actually used |
78 |
#eqs=`echo $arg | grep -c '='` |
79 |
#if [ $eqs -gt 0 ] ; then |
80 |
# pfx=`echo $arg | sed 's/=.*$/=/'` |
81 |
# nw=`echo $opt | wc -w` |
82 |
# if test $nw = '1' ; then |
83 |
# CMDLINE="$CMDLINE $pfx$opt" |
84 |
# else |
85 |
# CMDLINE="$CMDLINE $pfx'$opt'" |
86 |
# fi |
87 |
#else |
88 |
nw=`echo $arg | wc -w` |
89 |
if test $nw = '1' ; then |
90 |
CMDLINE="$CMDLINE $arg" |
91 |
else |
92 |
CMDLINE="$CMDLINE '$arg'" |
93 |
fi |
94 |
#fi |
95 |
if test -n "$prev" -a $flg = '1' ; then |
96 |
eval "$prev=\$arg" |
97 |
prev= |
98 |
continue |
99 |
fi |
100 |
case $arg in |
101 |
-mpi | --mpi | -mpi=* | --mpi=* ) |
102 |
MPI=`expr $MPI + 1` ;; |
103 |
-mods | --mods | -mo | --mo ) |
104 |
prev=MODS |
105 |
flg=`expr $flg + 1` ;; |
106 |
-mods=* | --mods=* | -mo=* | --mo=* ) |
107 |
MODS=$opt |
108 |
flg=`expr $flg + 1` ;; |
109 |
*) ;; |
110 |
esac |
111 |
done |
112 |
|
113 |
printf "=== \n" |
114 |
echo " Set MPI='$MPI' ; MODS='$MODS'" |
115 |
#echo "flg='$flg'" |
116 |
|
117 |
if [ $flg -lt 1 ] ; then |
118 |
echo "`basename $0` error: no '-mods' found" |
119 |
exit 4 |
120 |
fi |
121 |
if [ $flg -gt 1 ] ; then |
122 |
echo "`basename $0` error: found multiple '-mods' ($flg)" |
123 |
exit 3 |
124 |
fi |
125 |
if [ $MPI -gt 1 ] ; then |
126 |
echo "`basename $0` error: found multiple '-mpi' ($MPI)" |
127 |
exit 2 |
128 |
fi |
129 |
|
130 |
#---+----1----+----2----+----3----+----4----+----5----+----6----+----7-|--+----| |
131 |
|
132 |
printf "=== Link / Unlink *_mpi files from 'MODS' dirs ===\n" |
133 |
printf " MODS dir: " |
134 |
MPI_LNKF= |
135 |
for d in $MODS ; do |
136 |
if test ! -d $d ; then |
137 |
echo |
138 |
echo "Error: MODS directory \"$d\" not found!" |
139 |
exit 1 |
140 |
else |
141 |
printf "$d " |
142 |
#SOURCEDIRS="$SOURCEDIRS $d" |
143 |
#INCLUDEDIRS="$INCLUDEDIRS $d" |
144 |
#------------------------------------------------------- |
145 |
# Put special links so that MPI specific files are used |
146 |
MPI_FILES=`(cd $d ; find . -name "*_mpi" -print)` |
147 |
for i in $MPI_FILES ; do |
148 |
ii=`echo $i | sed 's:^\./::'` |
149 |
name=`echo $ii | sed 's:_mpi::' ` |
150 |
if test $MPI = 0 ; then |
151 |
# NO: We undo an _mpi symbolically linked file |
152 |
if test -L $name ; then |
153 |
cmp $name "$d/$ii" > /dev/null 2>&1 |
154 |
RETVAL=$? |
155 |
if test "x$RETVAL" = x0 ; then |
156 |
printf "Un-linking $name ; " |
157 |
rm -f $name |
158 |
fi |
159 |
fi |
160 |
else |
161 |
# YES: We symbolically link this file (with conditions if already there) |
162 |
if test -L $name ; then |
163 |
cmp $d/$ii $name > /dev/null 2>&1 |
164 |
RETVAL=$? |
165 |
yy=0 ; |
166 |
for xx in $MPI_LNKF ; do if test $xx = $name ; then yy=1 ; fi ; done |
167 |
if test "x$RETVAL" != x0 -a $yy = 0 ; then |
168 |
# remove sym-link if different and has not just been linked |
169 |
printf "Un-link + " |
170 |
rm -f $name |
171 |
fi |
172 |
if test "x$RETVAL" = x0 ; then |
173 |
# if identical, keep sym-link and keep record of it |
174 |
MPI_LNKF="$MPI_LNKF $name" |
175 |
fi |
176 |
fi |
177 |
if ! test -f $name ; then |
178 |
# make sym-link and keep record of it |
179 |
printf "Linking $ii to $name ; " |
180 |
ln -sf $d/$ii $name |
181 |
MPI_LNKF="$MPI_LNKF $name" |
182 |
fi |
183 |
fi |
184 |
done |
185 |
#------------------------------------------------------- |
186 |
fi |
187 |
done |
188 |
echo |
189 |
echo "MPI_LNKF='$MPI_LNKF'" |
190 |
|
191 |
#---+----1----+----2----+----3----+----4----+----5----+----6----+----7-|--+----| |
192 |
printf "=== Generate new Makefile ===\n" |
193 |
#echo $CMDLINE |
194 |
#eval $CMDLINE |
195 |
echo $cmdline |
196 |
eval $cmdline |