1 |
#! /usr/bin/env bash |
2 |
# |
3 |
# Cheap shell script for building makefile |
4 |
# Has various missing features |
5 |
# o can't cope with source files that have duplicate names but that |
6 |
# are in different libraries |
7 |
# o doesn't have any explicit concept of ordering |
8 |
# o needs to deal with .F, .F90 and .c suffixes |
9 |
# |
10 |
# Debug settings |
11 |
# set -uvx |
12 |
# |
13 |
if test -f makemake.header ; then |
14 |
cat makemake.header |
15 |
fi |
16 |
|
17 |
F90_SUFF=F90 |
18 |
OBJ_SUFF='o' |
19 |
MOD_SUFF='mod' |
20 |
F90C=gfortran |
21 |
F90C=ifort |
22 |
F90C_ARGS='-O0 -c -CB -g -o' |
23 |
F90C_ARGS='-O0 -c -g -o' |
24 |
LINK=gfortran |
25 |
LINK=ifort |
26 |
EXE=a.out |
27 |
# |
28 |
dlist='../src ../pkg' |
29 |
command=`which $0` |
30 |
tdir=.`echo ${command} | sed -e 's/\/.*\///g' | sed -e 's/\..*$//g'`.$$ |
31 |
echo "# Searching directories "${dlist} |
32 |
echo "# Using temporary directory "${tdir} |
33 |
|
34 |
echo ifndef F90C |
35 |
echo F90C=${F90C} |
36 |
echo endif |
37 |
|
38 |
echo F90C_ARGS=${F90C_ARGS} |
39 |
echo OBJ_SUFF=${OBJ_SUFF} |
40 |
echo MOD_SUFF=${MOD_SUFF} |
41 |
|
42 |
echo ifndef F90C |
43 |
echo LINK=${LINK} |
44 |
echo endif |
45 |
|
46 |
echo ifndef EXE |
47 |
echo EXE=${EXE} |
48 |
echo endif |
49 |
|
50 |
mkdir ${tdir} |
51 |
|
52 |
# Set the includes |
53 |
echo IDIR='\' |
54 |
for d in ${dlist} ; do |
55 |
dl=`find $d -follow -type d -not -regex '.*\.svn.*' -not -regex '.*\CVS*'` |
56 |
for di in ${dl} ; do |
57 |
echo '-I'${di}' \' |
58 |
done |
59 |
done |
60 |
echo '$(EXTRA_IDIR)' |
61 |
|
62 |
# Executable links all the .o from ${dlist} except libs which are built |
63 |
# from each ../pkg subdirectory |
64 |
ol='' |
65 |
ll='' |
66 |
lo='' |
67 |
for d in ${dlist} ; do |
68 |
if test "${d##*/}" = "pkg" ; then |
69 |
# All these are treated as separate library sources |
70 |
dl=`find $d -follow -type d -not -regex '.*\.svn.*' -not -regex '.*\CVS*'` |
71 |
for dd in ${dl} ; do |
72 |
if test "${dd##*/}" != "pkg" ; then |
73 |
echo '## Library name = 'lib${dd##*/}.a |
74 |
lfl=`find ${dd} -follow -maxdepth 1 -type f -not -regex '.*\.svn.*' -not -regex '.*\CVS*'` |
75 |
echo '## Library files = '${lfl##*/} |
76 |
ll=${ll}' 'lib${dd##*/}.a |
77 |
lo=${lo}' '-l${dd##*/} |
78 |
fi |
79 |
done |
80 |
else |
81 |
# All others are .o |
82 |
fl=`find $d -follow -name '*.'${F90_SUFF} -not -regex '.*\.svn.*' -not -regex '.*\CVS*'` |
83 |
for f in ${fl} ; do |
84 |
fn=${f##*/} |
85 |
of=${fn%.*}.${OBJ_SUFF} |
86 |
ol=${ol}' '${of} |
87 |
done |
88 |
fi |
89 |
done |
90 |
|
91 |
# Write objlist |
92 |
echo 'OBJLIST=\' |
93 |
for o in ${ol} ; do |
94 |
echo ${o}' \' |
95 |
done |
96 |
echo ' ' |
97 |
|
98 |
# Write library list |
99 |
echo 'LIBLIST=\' |
100 |
for l in ${ll} ; do |
101 |
echo ${l}' \' |
102 |
done |
103 |
echo '$(EXTRA_LIB_LIST)' |
104 |
echo ' ' |
105 |
|
106 |
# Write library option |
107 |
# write it twice because gnu linker doesn't search backwards and we don't know the |
108 |
# right order (I think this is what ar and ranlib are for!) |
109 |
echo 'LIBOPT=\' |
110 |
for l in ${lo} ; do |
111 |
echo ${l}' \' |
112 |
done |
113 |
for l in ${lo} ; do |
114 |
echo ${l}' \' |
115 |
done |
116 |
echo ' ' |
117 |
|
118 |
|
119 |
# Set rule for executable |
120 |
echo '$(EXE): $(OBJLIST) $(LIBLIST)' |
121 |
echo -e \\t'$(LINK) -L. -o $(EXE) $(OBJLIST) $(LIBOPT) $(EXTRA_LOPT)' |
122 |
|
123 |
# Set rule for libraries |
124 |
ol='' |
125 |
ll='' |
126 |
for d in ${dlist} ; do |
127 |
if test "${d##*/}" = "pkg" ; then |
128 |
# All these are treated as separate library sources |
129 |
dl=`find $d -follow -type d -not -regex '.*\.svn.*' -not -regex '.*\CVS*'` |
130 |
for dd in ${dl} ; do |
131 |
if test "${dd##*/}" != "pkg" ; then |
132 |
lfl=`find ${dd} -follow -maxdepth 1 -type f -not -regex '.*/\.[a-zA-Z].*' -not -regex '.*\CVS*'` |
133 |
# lfl=`find -L ${dd} -maxdepth 1 -type f -not -regex '.*\.svn.*'` |
134 |
fo='' |
135 |
for ff in ${lfl}; do |
136 |
f=${ff##*/} |
137 |
fo=${fo}' '${f%%.${F90_SUFF}}.${OBJ_SUFF} |
138 |
done |
139 |
echo lib${dd##*/}.a:' '${fo} |
140 |
echo -e \\t'@touch 'lib${dd##*/}.a |
141 |
echo -e \\t'@rm 'lib${dd##*/}.a |
142 |
echo -e \\t'$(AR) -rus' lib${dd##*/}.a ${fo} |
143 |
fi |
144 |
echo ' ' |
145 |
done |
146 |
fi |
147 |
done |
148 |
|
149 |
# Set rule for individual object and module files |
150 |
for d in ${dlist} ; do |
151 |
fl=`find $d -follow -name '*.'${F90_SUFF} -not -regex '.*\.svn.*' -not -regex '.*\CVS*'` |
152 |
for f in ${fl} ; do |
153 |
fonly=${f##*/} |
154 |
base=${fonly%.*} |
155 |
echo ${base}.${OBJ_SUFF}: $f |
156 |
echo -e \\t'$(F90C) $(IDIR) $(F90C_ARGS) $@ $<' |
157 |
echo ${base}.${MOD_SUFF}: $f |
158 |
echo -e \\t'$(F90C) $(IDIR) $(F90C_ARGS) '${base}.${OBJ_SUFF}' $<' |
159 |
done |
160 |
done |
161 |
|
162 |
# Add dependency rules for source that includes another module via a USE |
163 |
# statement |
164 |
for d in ${dlist} ; do |
165 |
fl=`find $d -follow -name '*.'${F90_SUFF} -not -regex '.*\.svn.*' -not -regex '.*\CVS*'` |
166 |
for f in ${fl} ; do |
167 |
fonly=${f##*/} |
168 |
base=${fonly%.*} |
169 |
mlist=`grep -i '^ *use *' ${f} | awk '{print $2}'` |
170 |
for m in ${mlist} ; do |
171 |
# echo ${base}.'$(OBJ_SUFF)': $m.'$(OBJ_SUFF)' |
172 |
if test -f makemake.externals; then |
173 |
chkext=`grep -i '^'$m' *$' makemake.externals ` |
174 |
extyn=$? |
175 |
fi |
176 |
if test "$extyn" != "0" ; then |
177 |
echo ${base}.'$(OBJ_SUFF)': $m.'$(MOD_SUFF)' |
178 |
echo ${base}.'$(MOD_SUFF)': $m.'$(MOD_SUFF)' |
179 |
fi |
180 |
done |
181 |
done |
182 |
done |
183 |
|
184 |
\rm -fr ${tdir} |