-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbuild.sh
executable file
·313 lines (253 loc) · 8.23 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
#!/usr/bin/env bash
if [ -e /.dockerenv ]; then
set -eo pipefail
set -o errtrace
trap "ERROR: There was an error, details to follow" ERR
fi
###########################################################################
### Author: Panagiotis Velissariou <panagiotis.velissariou@noaa.gov>
###
### Version - 1.0 Mon Nov 16 2020
###########################################################################
###====================
# Make sure that the current working directory is in the PATH
[[ ! :$PATH: == *:".":* ]] && export PATH="${PATH}:."
# Get the directory where the script is located
if [[ $(uname -s) == Darwin ]]; then
# readonly scrDIR="$(cd "$(dirname "$(greadlink -f -n "${BASH_SOURCE[${#BASH_SOURCE[@]} - 1]}" )" )" && pwd -P)"
readonly scrNAME="$( grealpath -s "${BASH_SOURCE[${#BASH_SOURCE[@]} - 1]}" )"
readonly scrDIR="$(cd "$(dirname "${scrNAME}" )" && pwd -P)"
else
# readonly scrDIR="$(cd "$(dirname "$(readlink -f -n "${BASH_SOURCE[${#BASH_SOURCE[@]} - 1]}" )" )" && pwd -P)"
readonly scrNAME="$( realpath -s "${BASH_SOURCE[${#BASH_SOURCE[@]} - 1]}" )"
readonly scrDIR="$(cd "$(dirname "${scrNAME}" )" && pwd -P)"
fi
lst="${scrDIR:+${scrDIR}/}functions_build ${scrDIR:+${scrDIR}/}scripts/functions_build functions_build"
funcs=
for ilst in ${lst}
do
if [ -f "${ilst:-}" ]; then
funcs="${ilst}"
break
fi
done
if [ -n "${funcs:+1}" ]; then
source "${funcs}"
[ $? -ne 0 ] && exit 1
else
echo " ### ERROR :: in ${scrNAME}"
echo " Cannot load the required file: functions_build"
echo " Exiting now ..."
echo
exit 1
fi
unset ilst funcs
###====================
############################################################
### BEG:: SYSTEM CONFIGURATION
############################################################
# Call ParseArgs to get the user input.
ParseArgs "${@}"
##########
# Set the variables for this script
ACCEPT_ALL=${MY_ACCEPT_ALL:-0}
CLEAN=${MY_CLEAN:-0}
ADD_CMAKE_FLAGS="${MY_ADD_CMAKE_FLAGS}"
[ -n "${MY_COMPILER:+1}" ] && COMPILER="$( toLOWER "$( basename "${MY_COMPILER}" )" )"
TYPE=${MY_TYPE:-R}
[ "${MY_PARMAKE:0}" -gt 1 ] && PARMAKE=${MY_PARMAKE}
PARALLEL=${MY_PARALLEL:-0}
PLATFORM=${MY_PLATFORM:-}
PROJECT_DIR=${MY_PROJECT_DIR:-}
[ -n "${MY_INSTALL_DIR:+1}" ] && INSTALL_DIR=${MY_INSTALL_DIR:-}
[ -n "${MY_VERBOSE:+1}" ] && VERBOSE=${MY_VERBOSE}
##########
##########
# Get the project directories and perform a basic check on them
readonly projDIR="${PROJECT_DIR:-${scrDIR}}"
readonly buildDIR="${projDIR}/build"
if [ ! -f "${projDIR}/CMakeLists.txt" ]; then
echo "The project directory \"${projDIR}\" does not appear to contain CMakeLists.txt."
echo "Is this the correct project source directory?"
echo "Is the CMake build system established for this project?"
echo "Exiting ..."
exit 1
fi
##########
##########
# If the user requested to clean the build folder, do the cleaning end exit
if [ ${CLEAN:-0} -ge 1 ]; then
echo "User requested to only clean the project. Cleaning ..."
if [ ! -f "${buildDIR}/CMakeCache.txt" ]; then
echo "Could not find the cache file: ${buildDIR}/CMakeCache.txt."
echo "Nothing to clean. Exiting ..."
exit 0
else
if [ "${projDIR}" != "${buildDIR}" ]; then
if [ ${ACCEPT_ALL:-0} -le 0 ]; then
echo_response=
while [ -z "${echo_response}" ] ; do
echo -n "Ready to clean ${buildDIR}? [y/n]: "
read echo_response
echo_response="$( getYesNo "${echo_response}" )"
done
if [ "${echo_response:-no}" = "no" ]; then
echo
echo "User responded: ${echo_response}"
echo "Exiting now ..."
echo
exit 0
fi
else
echo "User accepted all settings."
sleep 2
fi
if [ ${CLEAN:-0} -eq 1 ]; then
pushd ${buildDIR} >/dev/null 2>&1
make clean
popd >/dev/null 2>&1
fi
if [ ${CLEAN:-0} -eq 2 ]; then
if [ -f ${projDIR}/cmake/distclean.cmake ]; then
cmake -P ${projDIR}/cmake/distclean.cmake
else
rm -rf ${buildDIR}
fi
fi
exit 0
else
echo "The \"build\" directory cannot be the same as the \"project\" directory:"
echo " projDIR = ${projDIR}"
echo " buildDIR = ${buildDIR}"
fi
fi
fi
##########
##########
# Get the compilers to use for this project compilation
getCompilerNames "${COMPILER}"
##########
##########
# Get the CMake build type
case "${TYPE}" in
D) OPT_TYPE="Debug" ;;
R) OPT_TYPE="Release" ;;
RD) OPT_TYPE="RelWithDebInfo" ;;
RD) OPT_TYPE="MinSizeRel" ;;
*) OPT_TYPE="Release" ;;
esac
##########
##########
# Export some environment variables for CMake
export COMPILER=${COMPILER}
if [ "${PARALLEL:-0}" -lt 1 ]; then
export CC="${CC}" CXX="${CXX}" FC="${FC}" F90="${F90}"
else
export CC="${PCC}" CXX="${PCXX}" FC="${PFC}" F90="${PF90}"
fi
if [ -n "${INSTALL_DIR:+1}" ]; then
export INSTALL_DIR=${INSTALL_DIR}
else
unset INSTALL_DIR
fi
if [ -n "${VERBOSE:+1}" ]; then
export VERBOSE=${VERBOSE}
else
unset VERBOSE
fi
##########
##########
# Generate some flags to pass to CMake
CMAKE_FLAGS="-DCMAKE_BUILD_TYPE=${OPT_TYPE}"
[ -n "${PLATFORM:+1}" ] && CMAKE_FLAGS+=" -DCMAKE_Platform=${PLATFORM}"
[ -n "${VERBOSE:+1}" ] && CMAKE_FLAGS+=" -DCMAKE_VERBOSE_MAKEFILE=TRUE"
[ -n "${ADD_CMAKE_FLAGS:+1}" ] && CMAKE_FLAGS+=" ${ADD_CMAKE_FLAGS}"
##########
##########
# Get a final user response for the variables
echo
echo "The following variables are defined:"
echo " ACCEPT_ALL = ${ACCEPT_ALL}"
echo " PROJECT_SOURCE_DIR = ${projDIR}"
echo " PROJECT_BINARY_DIR = ${buildDIR}"
echo " CMAKE_SOURCE_DIR = ${projDIR}"
echo " CMAKE_BINARY_DIR = ${buildDIR}"
echo " INSTALL_DIR = ${INSTALL_DIR:-${projDIR}}"
echo " CMAKE_BUILD_TYPE = ${OPT_TYPE}"
echo " CMAKE_FLAGS = ${CMAKE_FLAGS}"
echo " PLATFORM = ${PLATFORM}"
echo " COMPILER = ${COMPILER:-Undefined, Supported values are: [${MY_COMPILING_SYTEMS}]}"
echo " CC = ${CC:-Will use the OS default compiler}"
echo " CXX = ${CXX:-Will use the OS default compiler}"
echo " FC = ${FC:-Will use the OS default compiler}"
echo " F90 = ${F90:-Will use the OS default compiler}"
echo " VERBOSE = ${VERBOSE:-0}"
echo
if [ ${ACCEPT_ALL:-0} -le 0 ]; then
echo_response=
while [ -z "${echo_response}" ] ; do
echo -n "Are these values correct? [y/n]: "
read echo_response
echo_response="$( getYesNo "${echo_response}" )"
done
if [ "${echo_response:-no}" = "no" ]; then
echo
echo "User responded: ${echo_response}"
echo "Exiting now ..."
echo
exit 1
fi
unset echo_response
else
echo "User accepted all settings."
sleep 2
fi
##########
############################################################
### START THE CALCULATIONS
############################################################
##########
# Create the build directory and check against the project source directory.
[ ! -d "${buildDIR}" ] && mkdir -p ${buildDIR}
if [ "${projDIR}" == "${buildDIR}" ]; then
echo "The \"build\" directory cannot be the same as the \"project\" directory:"
echo " projDIR = ${projDIR}"
echo " buildDIR = ${buildDIR}"
echo "Exiting ..."
exit 1
fi
# Perform the CMake configuration and compile the project
compileERR=0
pushd ${buildDIR} >/dev/null 2>&1
fls="$( find . -type f -name "*CMakeCache.txt" | xargs )"
if [ -n "${fls:+1}" ]; then
echo "Removing the cache files in the build directory"
rm -f ${fls}
fi
echo
echo "Running: cmake ${CMAKE_FLAGS} -S${projDIR} -B${buildDIR}"
eval "cmake ${CMAKE_FLAGS} -S${projDIR} -B${buildDIR}"
compileERR=$?
if [ ${compileERR} -eq 0 ]; then
if [ ${CLEAN:-0} -ne 0 -a ${CLEAN:-0} -ne -3 ]; then
echo
echo "Running: make clean"
make clean
fi
echo
echo "Running: make ${PARMAKE:+-j ${PARMAKE}}"
make ${PARMAKE:+-j ${PARMAKE}}
compileERR=$?
if [ ${compileERR} -eq 0 ]; then
echo
echo "Running: make install"
make install
else
echo
echo "Couldn't run \"make install\" due to \"make compile\" errors."
fi
else
echo "Couldn't run \"make\" due to \"cmake\" errors."
fi
popd >/dev/null 2>&1
exit 0