forked from apptainer/singularity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mconfig
executable file
·315 lines (264 loc) · 8.02 KB
/
mconfig
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#!/bin/sh -
# Copyright (c) 2016-2018, Yannick Cote <yanick@divyan.org>. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be found
# in the LICENSE file.
set -e
# defaults and compiler options
prefix="/usr/local"
verbose=0
debug=0
hstcc=
hstcc_opts="cc gcc clang i686-w64-mingw32-gcc x86_64-w64-mingw32-gcc"
hstcxx=
hstcxx_opts="c++ g++ clang++ i686-w64-mingw32-g++ x86_64-w64-mingw32-g++"
tgtcc=
tgtcc_opts=$hstcc_opts
tgtcxx=
tgtcxx_opts=$hstcxx_opts
hststatic=0
tgtstatic=0
usage () {
echo "${0##*/}: could not complete configuration"
}
usage_args () {
echo "usage: ${0##*/}: [-dvsS] [-p prefix] [-b builddir] [-c hstcc]"
echo " [-C tgtcc] [-x hstcxx] [-X tgtcxx]"
echo " -v build project with verbose flags on"
echo " -d build project with debug flags on"
echo " -s build final host project binary statically"
echo " -S build final target project binary statically"
echo " -p install project in \`prefix'"
echo " -b build project in \`builddir'"
echo " -c build project with host C \`compiler'"
echo " -C build project with target C\`compiler'"
echo " -x build project with host C++ \`compiler'"
echo " -h this help"
echo "\`host C compiler': $hstcc_opts"
echo "\`host C++ compiler': $hstcxx_opts"
echo "\`target C compiler': $tgtcc_opts"
}
# save the command line
cmdline="`readlink -f $0` $*"
# check line options
args=`getopt p:b:c:C:dsSvx:h $*`
if [ $? -ne 0 ]; then
usage_args
exit 2
fi
set -- $args
while [ $# -ne 0 ]; do
case $1 in
-p) prefix="$2"; shift; shift;;
-b) builddir="$2"; shift; shift;;
-c) hstcc="$2"; shift; shift;;
-C) tgtcc="$2"; shift; shift;;
-x) hstcxx="$2"; shift; shift;;
-d) debug=1; shift;;
-s) hststatic=1; shift;;
-S) tgtstatic=1; shift;;
-v) verbose=1; shift;;
-h) usage_args; exit 2;;
--) shift; break;;
?) usage_args
exit 2;;
esac
done
#
# non-option param
if [ $# != 0 ]; then
usage_args
exit 2
fi
topdir=`dirname \`readlink mconfig\`` || (echo "\`mconfig' needs to be symlinked at the top of the project."; exit 2)
sourcedir=`readlink -f .`
mfragsdir=$sourcedir/mlocal/frags
mchecksdir=$sourcedir/mlocal/checks
#######################################################################
# System build tool configuration and debug/verbose flags
#######################################################################
if [ -f $mchecksdir/basechecks.chk ]; then
echo "=> running base system checks ..."
. $mchecksdir/basechecks.chk
else
echo "error: file $mchecksdir/basechecks.chk needs to be present"
exit 2
fi
#######################################################################
# Extra user specified system configuration checks (mlocal/checks)
#######################################################################
# write a "#define" definition entry into $builddir/config.h
config_add_def ()
{
if [ "$1" = "" -o "$2" = "" ]; then
return
fi
echo "#define $*" >> $builddir/config.h
}
rm -rf $builddir/config.h
if [ -f $mchecksdir/project.chk ]; then
echo "=> running project specific checks ..."
. $mchecksdir/project.chk
fi
#######################################################################
# Generated fragments
#######################################################################
echo "=> generating fragments ..."
if [ ! -d $mfragsdir ]; then
echo "error: $mfragsdir should be populated with Makefile fragments."
echo "NOTE: Fragments may be copied from examples found in $topdir/examples/<project>/* "
echo "NOTE: to $sourcedir/mlocal/* and tweak for your project."
exit 2
fi
########################
# verbosity
########################
if [ "$verbose" = 1 ]; then
echo "# build with verbose flag on" >> $genmk
echo "V :=" >> $genmk
else
echo "# silent build" >> $genmk
echo "V := @" >> $genmk
fi
echo >> $genmk
########################
# build tools
########################
cat >> $genmk << EOF
# source/build locations
BUILDDIR := .
SOURCEDIR := $sourcedir
CONTRIBDIR := $contribdir
PREFIX := $prefix
HOSTAR := ar
HOSTCC := $hstcc
HOSTCXX := $hstcxx
HOSTLD := $hstcc
HOSTRANLIB := ranlib
HOSTSIZE := size
HOSTOBJCOPY := objcopy
AR := `readlink -f \`which \\\`$tgtcc -print-prog-name=ar\\\`\``
CC := $tgtcc
CXX := $tgtcxx
LD := `readlink -f \`which \\\`$tgtcc -print-prog-name=ld\\\`\``
RANLIB := `readlink -f \`which \\\`$tgtcc -print-prog-name=ranlib\\\`\``
SIZE := size
OBJCOPY := `readlink -f \`which \\\`$tgtcc -print-prog-name=objcopy\\\`\``
ARCH := $tgt_arch
EOF
########################
# modules
########################
:>$genconfdir/module.lst
found_modules=`(cat modules | awk '{ printf("%s ", $0) }') \
2>/dev/null || true`
if [ "$found_modules" = "" ]; then
found_modules=`find . -name '*.mconf' -print`
if [ "$found_modules" = "" ]; then
echo "error: no build modules found !"
echo
echo "You need to at least create one \`<module_name>.mconf' "
echo "file describing either a program, a lib, or a set of"
echo "objects to build."
exit 2
fi
fi
echo " found build modules:"
echo "CPPFLAGS :=" >> $genmk
# NOTE: parsed module (*.mconf) files only substitute var tgt_arch for now
for m in $found_modules; do
mod=`eval echo ${m##*./}`
modpath=`eval echo ${mod%/*}`
if [ `readlink -f "$modpath"` = `readlink -f "."` ]; then
modpath="."
fi
if [ ! -f $mod ]; then
echo "error: module file \`$mod' not found!"
exit 2
fi
echo " +-> $mod"
echo "$modpath `eval basename $mod`" >> $genconfdir/module.lst
mkdir -p $genconfdir/$modpath
cat $mod | awk -v tgt_arch=$tgt_arch '{ gsub(/tgt_arch/, \
tgt_arch); print }' > $genconfdir/${mod}.parsed
# echo "CPPFLAGS += -iquote\$(SOURCEDIR)/$modpath" >> $genmk
done
echo >> $genmk
# init LDFLAGS if static builds are supported
if [ "$hststatic" = "1" -a "$tgtstatic" = "1" ]; then
echo "LDFLAGS := -static" >> $genmk
else
echo "LDFLAGS :=" >> $genmk
fi
echo >> $genmk
cat >> $genmk << EOF
# make sure \`all' is the first rule
all:
EOF
# call genmod.awk, generating Makefile components
$topdir/genmod.awk modfile=$genconfdir/module.lst \
topdir=$sourcedir \
host=$host \
verbose=$verbose \
debug=$debug \
genconfdir=$genconfdir \
tmpldir=$topdir/tmpl
#######################################################################
# Target Makefile
#######################################################################
drawline () {
echo >> $output
printf "# [ Embedding file : %50s ] #\n" $1 >> $output
echo >> $output
}
echo "=> building Makefile ..."
:> $output
echo "#" >> $output
echo "# Non-recursive Makefile GENERATED by \`${0##*/}' -- `date`" >> $output
echo "# configured: $cmdline" >> $output
echo "#" >> $output
drawline $genmk
cat $genmk >> $output
drawline $mfragsdir/common_opts.mk
cat $mfragsdir/common_opts.mk >> $output
if [ "$debug" != 1 ]; then
drawline $mfragsdir/release_opts.mk
cat $mfragsdir/release_opts.mk >> $output
else
drawline $mfragsdir/debug_opts.mk
cat $mfragsdir/debug_opts.mk >> $output
fi
if [ -f "$mfragsdir/arch_${tgt_arch}_opts.mk" ]; then
drawline $mfragsdir/arch_${tgt_arch}_opts.mk
cat $mfragsdir/arch_${tgt_arch}_opts.mk >> $output
fi
drawline $genconfdir/modules.mk
cat $genconfdir/modules.mk >> $output
drawline $genconfdir/all.mk
cat $genconfdir/all.mk >> $output
drawline $mfragsdir/Makefile.stub
# here, `depends' need to happen after all other rules; at the very end
final_all=`cat $genconfdir/all.mk | awk 'BEGIN { FS="all: " } { print $2 }'`
cat $mfragsdir/Makefile.stub | awk \
"{ gsub(/^collect:/, \"collect: $final_all\"); print }" >> $output
rm -rf $genconfdir
#######################################################################
# Configuration results
#######################################################################
echo "=> project setup with :"
echo " - target arch: $tgt_arch"
echo " - target compiler: $tgtcc"
echo " - host wordsize: ${wordsize}-bit"
echo " - host system: $host"
if [ "$verbose" = 1 ]; then
echo " - verbose: yes"
else
echo " - verbose: no"
fi
if [ "$debug" = 1 ]; then
echo " - debug: yes"
else
echo " - debug: no"
fi
echo "=> $builddir/Makefile ready, try:"
echo " $ cd $builddir"
echo " $ make"