-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.sh
executable file
·539 lines (479 loc) · 18.3 KB
/
build.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
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
#!/bin/bash
# Copyright 2019 Khang Hoang Nguyen
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files
# (the "Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge,
# publish, distribute, sublicense, and/or sell copies of the Software,
# and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# Any help with the build file(s) are welcome.
# faiNumber build file for gfortran.
#
# @note This file supports building the library with individual support
# to 32/64/128-bit. If 128-bit support is not available. Build a 64
# and a 32-bit libary file then combine the "mod" directory
# together.
#
# Take note that currently this script will always delete the
# output directory if it existed.
compiler="gfortran"
compiler_flag=""
static_or_dynamic=0
build_type=0 # <- 0 - all-in-one
# 1 - individual library file
# 32 - 32-bit library only
# 64 - 64-bit library only
# 128 - 128-bit library only
read_temp=""
##################### Custom gfortran command #########################
echo "Warning: Any command can be set, but only gfortran is supported(7,8)"
echo "Earlier version may or may not work."
echo "Set the compiler command or empty for 'gfortran':"
read -r read_temp
if [ ! -z "$read_temp" ]; then
compiler=$read_temp
fi
echo ""
################### End custom gfortran command #######################
################### Verify if gfortran is installed ####################
if ! compiler_string=$("$compiler" --version); then
echo "Error: ${compiler} is not installed."
exit 1
fi
################# End verify if gfortran is installed ##################
################################# DIRS #################################
# Everything in dir_output and dir_temp will be delete if existed
dir_src="./src"
dir_output="./lib"
dir_mod="${dir_output}/mod"
dir_temp="./buildtemp"
dir_test="./test"
############################### End DIRS ###############################
############################### Functions ##############################
function exit_with_code() {
local code=$1
local clean=$2
if [ "$clean" -eq 1 ]; then
rm -rf "$dir_temp"
rm -rf "$dir_output"
elif [ "$clean" -eq 2 ]; then
rm -rf "$dir_temp"
fi
exit "$code"
}
# @note The compile_single() function should only be use after the
# compile_o_files and compile_mod_files variable have been
# declared.
function compile_single(){
local file=$1
local compiler=$2
local compiler_flag=$3
local file_name="${file%.*}"
local file_o="${dir_temp}/${file_name}.o"
local file_mod="${file_name,,}.mod"
if ! eval "${compiler} ${compiler_flag} -J ${dir_temp}/ -c ${dir_src}/${file} -o ${file_o}"; then
exit_with_code 1 1
fi
compiled_o_files+=("$file_o")
compiled_mod_files+=("$file_mod")
}
############################# End functions ############################
############## Verify if all the file in the repo existed ##############
# This will be moved to file.list when faiNumber-Fortran expand to be
# more than 30 files.
#
# @note Don't change file_consts order. Its order is used for the build
# procedure.
file_consts=("fnConsts.f90" "fnConsts64.f90" "fnConsts128.f90")
file_32=("fnBinaryUtil.f90" "fnDecimalUtil.f90" "fnHexUtil.f90" \
"fnOctalUtil.f90" "fnInt32Util.f90" "fnNumberStringUtil.f90")
file_64=("fnBinaryUtil64.f90" "fnDecimalUtil64.f90" "fnHexUtil64.f90" \
"fnOctalUtil64.f90" "fnInt64Util.f90")
file_128=("fnBinaryUtil128.f90" "fnDecimalUtil128.f90" "fnHexUtil128.f90" \
"fnOctalUtil128.f90" "fnInt128Util.f90")
file_count=19
file_missing=()
file_test_32=("fnBinaryUtilTest.f90" "fnDecimalUtilTest.f90" \
"fnHexUtilTest.f90" "fnInt32UtilTest.f90" "fnOctalUtilTest.f90" \
"fnNumberStringUtilTest.f90")
file_test_64=("fnBinaryUtil64Test.f90" "fnDecimalUtil64Test.f90" \
"fnHexUtil64Test.f90" "fnInt64UtilTest.f90" "fnOctalUtil64Test.f90")
file_test_128=("fnBinaryUtil128Test.f90" "fnDecimalUtil128Test.f90" \
"fnHexUtil128Test.f90" "fnInt128UtilTest.f90" "fnOctalUtil128Test.f90")
file_test_count=16
file_test_missing=()
compiled_o_files=()
compiled_mod_files=()
if [ $((${#file_consts[@]} + ${#file_32[@]} + ${#file_64[@]} + ${#file_128[@]})) \
-ne $file_count ]; then
echo "Error: Incorrect build script setup."
echo $((${#file_consts[@]} + ${#file_32[@]} + ${#file_64[@]} + ${#file_128[@]}))
exit 1
fi
if [ $((${#file_test_32[@]} + ${#file_test_64[@]} + ${#file_test_128[@]})) \
-ne $file_test_count ]; then
echo "Warning: Incorrect build script setup. Test file count mismatch."
echo "It may not be possible to executed all test."
echo $((${#file_test_32[@]} + ${#file_test_64[@]} + ${#file_test_128[@]}))
echo ""
fi
for file in "${file_consts[@]}" "${file_32[@]}" "${file_64[@]}" "${file_128[@]}"
do
if [ ! -f "${dir_src}/${file}" ]; then
file_missing+=("${dir_src}/${file}")
fi
done
if [ "${#file_missing[@]}" -gt 0 ]; then
echo "${#file_missing[@]}"
echo "Error: Missing file in the repo. The following file(s) is/are missing: "
for file in "${file_missing[@]}"
do
echo "$file"
done
exit 1
fi
for file in "${file_test_32[@]}" "${file_test_64[@]}" "${file_test_128[@]}"
do
if [ ! -f "${dir_test}/${file}" ]; then
file_test_missing+=("${dir_test}/${file}")
fi
done
if [ "${#file_test_missing[@]}" -gt 0 ]; then
echo "${#file_test_missing[@]}"
echo "Warning: Missing test file in the repo."
echo "It may not be possible to executed all test."
echo "The following test file(s) is/are missing:"
for file in "${file_test_missing[@]}"
do
echo "${file}"
done
echo ""
fi
############ End verify if all the file in the repo existed ############
#################### Getting user input for options ####################
fortran_version=""
echo "Fortran version(i.e. legacy, f95, f2003, f2008, f2018) or empty?"
read -r fortran_version
if [ ! -z "$fortran_version" ]; then
fortran_version="-std=$fortran_version"
fi
echo ""
echo "Static(0) or Dynamic(1) libary, or empty for defaul(0)?"
read -r read_temp
if [ ! -z "$read_temp" ]; then
if [ "$read_temp" == "0" ]; then
static_or_dynamic=0
elif [ "$read_temp" == "1" ]; then
static_or_dynamic=1
else
echo "Error: Unknown option. 0 for static, 1 for dynamic."
exit 1
fi
fi
echo ""
echo "Build type(0, 1, 32, 64, 128) or empty for default(0):"
echo "0 - All-in-one file."
echo "1 - Individual libary file for 32/64/128 bits."
echo "32 - Build 32-bit library only."
echo "64 - Build 64-bit library only."
echo "128 - Build 128-bit library only."
read -r read_temp
if [ ! -z "$read_temp" ]; then
if [ "$read_temp" == "0" ]; then
build_type=0
elif [ "$read_temp" == "1" ]; then
build_type=1
elif [ "$read_temp" == "32" ]; then
build_type=32
elif [ "$read_temp" == "64" ]; then
build_type=64
elif [ "$read_temp" == "128" ]; then
build_type=128
else
echo "Error: Unknown option. Only support(0, 1, 32, 64, or 128)."
exit 1
fi
fi
echo ""
################## End getting user input for options ##################
########################## Compiling section ###########################
echo "Compiling with: "
echo "$compiler_string"
echo ""
if [ $static_or_dynamic -eq 1 ]; then
compiler_flag="-fPIC"
fi
if [ ! -z "$fortran_version" ]; then
compiler_flag="$compiler_flag $fortran_version"
fi
if [ -d "$dir_output" ]; then
if ! rm -rf "$dir_output"; then
echo "Error: output directory '$dir_output' existed, but couldn't be removed"
exit 1
fi
fi
if [ -d "$dir_temp" ]; then
if ! rm -rf "$dir_temp"; then
echo "Error: temp directory '$dir_temp' existed but couldn't be removed"
exit 1
fi
fi
if ! mkdir "$dir_temp"; then
echo "Error: couldn't make $dir_temp"
exit 1
fi
if ! mkdir "$dir_output"; then
echo "Error: couldn't make $dir_output"
exit 1
fi
if ! mkdir "$dir_mod"; then
echo "Error: couldn't make $dir_mod"
exit 1
fi
### Compile the consts file first depended on mode.
compile_single "${file_consts[0]}" "${compiler}" "${compiler_flag}"
if [ $build_type -eq 0 ] || [ $build_type -eq 1 ] || [ $build_type -eq 64 ]; then
compile_single "${file_consts[1]}" "${compiler}" "${compiler_flag}"
fi
if [ $build_type -eq 0 ] || [ $build_type -eq 1 ] || [ $build_type -eq 128 ]; then
compile_single "${file_consts[2]}" "${compiler}" "${compiler_flag}"
fi
### Compiling 32 if $build_type is 0, 1, or 32
if [ $build_type -eq 0 ] || [ $build_type -eq 1 ] || [ $build_type -eq 32 ]; then
for file in "${file_32[@]}"
do
file_name="${file%.*}"
file_o="${dir_temp}/${file_name}.o"
file_mod="${file_name,,}.mod"
if ! eval "${compiler} ${compiler_flag} -J ${dir_temp}/ -c ${dir_src}/${file} -o ${file_o}"; then
exit_with_code 1 1
fi
compiled_o_files+=("$file_o")
compiled_mod_files+=("$file_mod")
done
fi
### Compiling 64 if $build_type is 0, 1, or 64
if [ $build_type -eq 0 ] || [ $build_type -eq 1 ] || [ $build_type -eq 64 ]; then
for file in "${file_64[@]}"
do
file_name="${file%.*}"
file_o="${dir_temp}/${file_name}.o"
file_mod="${file_name,,}.mod"
if ! eval "${compiler} ${compiler_flag} -J ${dir_temp}/ -c ${dir_src}/${file} -o ${file_o}"; then
exit_with_code 1 1
fi
compiled_o_files+=("$file_o")
compiled_mod_files+=("$file_mod")
done
fi
### Compiling 128 if $build_type is 0, 1, or 128
if [ $build_type -eq 0 ] || [ $build_type -eq 1 ] || [ $build_type -eq 128 ]; then
for file in "${file_128[@]}"
do
file_name="${file%.*}"
file_o="${dir_temp}/${file_name}.o"
file_mod="${file_name,,}.mod"
if ! eval "${compiler} ${compiler_flag} -J ${dir_temp}/ -c ${dir_src}/${file} -o ${file_o}"; then
exit_with_code 1 1
fi
compiled_o_files+=("$file_o")
compiled_mod_files+=("$file_mod")
done
fi
if [ $build_type -eq 0 ]; then
if [ $static_or_dynamic -eq 0 ]; then
eval "ar rcs ${dir_output}/faiNumber.a ${compiled_o_files[*]}"
elif [ $static_or_dynamic -eq 1 ]; then
eval "${compiler} -shared ${compiled_o_files[*]} -o ${dir_output}/faiNumber.so"
fi
fi
if [ $build_type -ne 0 ]; then
if [ $build_type -eq 1 ] || [ $build_type -eq 32 ]; then
compiled_o_files_str="${compiled_o_files[0]}"
for file in "${file_32[@]}"
do
compiled_o_files_str="${compiled_o_files_str} ${dir_temp}/${file%.*}.o"
done
if [ $static_or_dynamic -eq 0 ]; then
eval "ar rcs ${dir_output}/faiNumber32.a " \
"${compiled_o_files_str}" "${compiled_o_files[0]}"
elif [ $static_or_dynamic -eq 1 ]; then
eval "${compiler} -shared ${compiled_o_files_str} " \
"-o ${dir_output}/faiNumber32.so"
fi
fi
if [ $build_type -eq 1 ] || [ $build_type -eq 64 ]; then
compiled_o_files_str="${compiled_o_files[0]} ${compiled_o_files[1]}"
for file in "${file_64[@]}"
do
compiled_o_files_str="${compiled_o_files_str} ${dir_temp}/${file%.*}.o"
done
if [ $static_or_dynamic -eq 0 ]; then
eval "ar rcs ${dir_output}/faiNumber64.a " \
"${compiled_o_files_str}"
elif [ $static_or_dynamic -eq 1 ]; then
eval "${compiler} -shared ${compiled_o_files_str} " \
"-o ${dir_output}/faiNumber64.so"
fi
fi
if [ $build_type -eq 1 ] || [ $build_type -eq 128 ]; then
if [ $build_type -eq 1 ]; then
compiled_o_files_str="${compiled_o_files[0]} ${compiled_o_files[2]}"
elif [ $build_type -eq 128 ]; then
compiled_o_files_str="${compiled_o_files[0]} ${compiled_o_files[1]}"
fi
for file in "${file_128[@]}"
do
compiled_o_files_str="${compiled_o_files_str} ${dir_temp}/${file%.*}.o"
done
if [ $static_or_dynamic -eq 0 ]; then
eval "ar rcs ${dir_output}/faiNumber128.a " \
"${compiled_o_files_str}"
elif [ $static_or_dynamic -eq 1 ]; then
eval "${compiler} -shared ${compiled_o_files_str} " \
"-o ${dir_output}/faiNumber128.so"
fi
fi
fi
for file in "${compiled_mod_files[@]}"
do
if ! mv "${dir_temp}/${file}" "${dir_mod}/${file}"; then
echo "Error: Couldn't move the mod files."
exit_with_code 1 1
fi
done
if ! rm -rf "${dir_temp}"; then
echo "Error: Can't remove ${dir_temp} * files."
exit_with_code 1 0
fi
echo "Run test(y) or exit?: "
read -r going_to_run_test
echo ""
if [ "$going_to_run_test" == "y" ]; then
echo "#################### Starting All Tests ####################"
echo "Note: this will take sometime."
echo ""
echo ""
include_extension=""
include_files=""
if [ $static_or_dynamic -eq 0 ]; then
include_extension=".a"
elif [ $static_or_dynamic -eq 1 ]; then
include_extension=".so"
fi
include_files=""
if [ $build_type -eq 0 ]; then
include_files="${dir_output}/faiNumber${include_extension}"
elif [ $build_type -eq 1 ]; then
include_files="${dir_output}/faiNumber32${include_extension} "
include_files+="${dir_output}/faiNumber64${include_extension} "
include_files+="${dir_output}/faiNumber128${include_extension}"
elif [ $build_type -eq 32 ]; then
include_files="${dir_output}/faiNumber32${include_extension} "
elif [ $build_type -eq 64 ]; then
include_files="${dir_output}/faiNumber64${include_extension} "
elif [ $build_type -eq 128 ]; then
include_files="${dir_output}/faiNumber128${include_extension} "
fi
if ! mkdir "$dir_temp"; then
echo "Error: Can't make dir temp for test"
exit 1
fi
if [ $build_type -eq 0 ] || [ $build_type -eq 1 ] || [ $build_type -eq 32 ]; then
for file in "${file_test_32[@]}"
do
file_test="${dir_test}/${file}"
if [ ! -f "$file_test" ]; then
echo "Skipping test file '$file_test'. The file is missing."
continue
fi
file_o="${dir_temp}/${file%.*}.o"
file_exe="${dir_temp}/${file%.*}$"
if ! eval "${compiler} -I $dir_mod -c $file_test -J $dir_temp -o ${file_o}"; then
echo "Can't compile test code"
exit_with_code 1 2
fi
if ! eval "${compiler} -I $dir_mod -o $file_exe $file_o ${include_files}"; then
echo "Can't make test code"
exit_with_code 1 2
fi
echo "################# Executing file ${file_exe} ###########"
if ! "$file_exe"; then
echo "Couldn't run test build file or the test failed."
exit_with_code 1 2
fi
done
fi
if [ $build_type -eq 0 ] || [ $build_type -eq 1 ] || [ $build_type -eq 64 ]; then
for file in "${file_test_64[@]}"
do
file_test="${dir_test}/${file}"
if [ ! -f "$file_test" ]; then
echo "Skipping test file '$file_test'. The file is missing."
continue
fi
file_o="${dir_temp}/${file%.*}.o"
file_exe="${dir_temp}/${file%.*}$"
if ! eval "${compiler} -I $dir_mod -c $file_test -J $dir_temp -o ${file_o}"; then
echo "Can't compile test code"
exit_with_code 1 2
fi
if ! eval "${compiler} -I $dir_mod -o $file_exe $file_o ${include_files}"; then
echo "Can't make test code"
exit_with_code 1 2
fi
echo "################# Executing file ${file_exe} ###########"
if ! "$file_exe"; then
echo "Couldn't run test build file or the test failed."
exit_with_code 1 2
fi
done
fi
if [ $build_type -eq 0 ] || [ $build_type -eq 1 ] || [ $build_type -eq 128 ]; then
for file in "${file_test_128[@]}"
do
file_test="${dir_test}/${file}"
if [ ! -f "$file_test" ]; then
echo "Skipping test file '$file_test'. The file is missing."
continue
fi
file_o="${dir_temp}/${file%.*}.o"
file_exe="${dir_temp}/${file%.*}$"
if ! eval "${compiler} -I $dir_mod -c $file_test -J $dir_temp -o ${file_o}"; then
echo "Can't compile test code"
exit_with_code 1 2
fi
if ! eval "${compiler} -I $dir_mod -o $file_exe $file_o ${include_files}"; then
echo "Can't make test code"
exit_with_code 1 2
fi
echo "################# Executing file ${file_exe} ###########"
if ! "$file_exe"; then
echo "Couldn't run test build file or the test failed."
exit_with_code 1 2
fi
done
fi
echo "##############################################################"
echo "Tests passed for the all the test file that were present."
echo "##############################################################"
fi
if ! rm -rf "${dir_temp}"; then
echo "Error: Can't remove ${dir_temp} for some reason"
exit 1
fi
exit 0