Skip to content

Commit f100e9f

Browse files
committed
Merge pull request #82 from zbeekman/segfault
Command line parsing for build.sh
2 parents 457b8fd + 2635ae3 commit f100e9f

File tree

1 file changed

+163
-25
lines changed

1 file changed

+163
-25
lines changed

build.sh

Lines changed: 163 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -30,28 +30,162 @@ LIBDIR='./lib/' # build directory for library
3030
MODCODE='json_module.f90' # json module file name
3131
LIBOUT='libjsonfortran.a' # name of json library
3232

33-
if [ "$1" == "-ifort" ]; then
34-
# Intel compiler
3533

36-
FCOMPILER='Intel'
37-
# The following warning might be triggered by ifort unless explicitly silenced:
38-
# warning #7601: F2008 standard does not allow an internal procedure to be an actual argument procedure name. (R1214.4).
39-
# In the context of F2008 this is an erroneous warning.
40-
# See https://prd1idz.cps.intel.com/en-us/forums/topic/486629
41-
FCOMPILERFLAGS='-c -O2 -warn -stand f08 -diag-disable 7601 -traceback'
42-
#FCOMPILERFLAGS='-c -O2 -warn -traceback -stand f08 -assume protect_parens -assume buffered_io -check all'
34+
# The following warning might be triggered by ifort unless explicitly silenced:
35+
# warning #7601: F2008 standard does not allow an internal procedure to be an actual argument procedure name. (R1214.4).
36+
# In the context of F2008 this is an erroneous warning.
37+
# See https://prd1idz.cps.intel.com/en-us/forums/topic/486629
38+
INTELCOMPILERFLAGS='-c -O2 -warn -stand f08 -diag-disable 7601 -traceback'
39+
#INTELCOMPILERFLAGS='-c -O2 -warn -traceback -stand f08 -assume protect_parens -assume buffered_io -check all'
40+
41+
GNUCOMPILERFLAGS='-c -O2 -fbacktrace -Wall -Wextra -Wno-maybe-uninitialized -pedantic -std=f2008'
42+
43+
FCOMPILER='gnu' #Set default compiler to gfortran
4344

44-
else
45-
# GFortran (must be >= 4.9)
4645

47-
FCOMPILER='gnu'
48-
FCOMPILERFLAGS='-c -O2 -fbacktrace -Wall -Wextra -Wno-maybe-uninitialized -pedantic -std=f2008'
49-
if [[ $CODE_COVERAGE == [yY]* ]]; then # Add coverage info with gcov
50-
echo "Compiling with gcov code coverage instrumentation."
51-
COVERAGE="-coverage"
52-
fi
53-
#FCOMPILERFLAGS='-c -O2 -fbacktrace -fall-intrinsics -Wall -Wextra -Wno-maybe-uninitialized -pedantic -std=f2008'
46+
# command line argument parsing
47+
# N.B.: Arguments appearing later in the list take precidence over those appearing earlier.
48+
# e.g., "./build.sh --compiler intel --coverage no --compiler gnu --coverage" will
49+
# perform the build with the GFORTRAN compiler, and coverage analysis
50+
51+
script_name="$(basename $0)"
52+
53+
# usage message
54+
print_usage () {
55+
echo -e "\n\nUsage:\n"
56+
echo -e "${script_name} [--compiler {intel|gnu|<other>}] [--cflags '<custom compiler flags here>']\n\
57+
[--coverage [{yes|no}]] [--profile [{yes|no}]] [--skip-tests [{yes|no}]]\n\
58+
[--skip-documentation [{yes|no}]] [--help]"
59+
echo ""
60+
echo -e "Any flags that take an optional yes or no argument will default to 'yes' when no\n\
61+
argument is passed. Additionally, A custom compiler may be passed to the 'compiler'\n\
62+
flag, but appropriate 'cflags' should also be passed to the script.\n\n"
63+
}
64+
65+
66+
while [ "$#" -ge "1" ]; do # Get command line arguments while there are more left to process
67+
68+
key="$1" # Command line args are key-value pairs or value-less keys
69+
70+
case $key in #find known keys
71+
--compiler) #pick the compiler. Defaults to gfortran, but intel or custom compilers can be used
72+
case "$2" in
73+
intel|Intel|INTEL|ifort)
74+
FCOMPILER='Intel'
75+
FCOMPILERFLAGS="$INTELCOMPILERFLAGS"
76+
shift
77+
;;
78+
gnu|Gnu|GNU|gfortran|Gfortran|GFortran|GFORTRAN)
79+
FCOMPILER='gnu'
80+
FCOMPILERFLAGS="$GNUCOMPILERFLAGS"
81+
shift
82+
;;
83+
*)
84+
FCOMPILER="custom"
85+
echo "Warning: Trying to build with unsupported compiler, $2." 1>&2
86+
echo "Please ensure you set appropriate --cflags and (single) quote them" 1>&2
87+
FC="$2"
88+
shift
89+
;;
90+
esac
91+
;;
92+
--cflags)
93+
FCOMPILERFLAGS="$2"
94+
# no good way to check that the user didn't do something questionable
95+
shift
96+
;;
97+
--coverage) # enable coverage
98+
case $2 in
99+
yes|Yes|YES)
100+
CODE_COVERAGE="yes"
101+
shift
102+
;;
103+
no|No|NO)
104+
CODE_COVERAGE="no"
105+
shift
106+
;;
107+
*)
108+
CODE_COVERAGE="yes"
109+
# don't shift because $2 is some other flag
110+
;;
111+
esac
112+
;;
113+
--profile) #nable profiling
114+
case $2 in
115+
yes|Yes|YES)
116+
CODE_PROFILE="yes"
117+
shift
118+
;;
119+
no|No|NO)
120+
CODE_PROFILE="no"
121+
shift
122+
;;
123+
*)
124+
CODE_PROFILE="yes"
125+
# don't shift because $2 is some other flag
126+
;;
127+
esac
128+
;;
129+
--skip-tests) # skip tests
130+
case $2 in
131+
yes|Yes|YES)
132+
JF_SKIP_TESTS="yes"
133+
shift
134+
;;
135+
no|No|NO)
136+
JF_SKIP_TESTS="no"
137+
shift
138+
;;
139+
*)
140+
JF_SKIP_TESTS="yes"
141+
;;
142+
esac
143+
;;
144+
--skip-documentation)
145+
case $2 in
146+
yes|Yes|YES)
147+
JF_SKIP_DOCS="yes"
148+
shift
149+
;;
150+
no|No|NO)
151+
JF_SKIP_DOCSS="no"
152+
shift
153+
;;
154+
*)
155+
JF_SKIP_DOCS="yes"
156+
;;
157+
esac
158+
;;
159+
--help)
160+
print_usage
161+
exit 0
162+
;;
163+
*)
164+
echo "Unknown flag, \"$1\", passed to ${script_name}!" 2>&1
165+
print_usage
166+
exit 1
167+
;;
168+
esac
169+
shift # look at next argument
170+
done # with argument parsing loop
171+
172+
# if no compiler selected, then we're defaulting to gnu, and need to check that the cflags are set
173+
if [ "$FCOMPILER" = 'gnu' ] && [ -z "$FCOMPILERFLAGS" ]; then
174+
FCOMPILERFLAGS="$GNUCOMPILERFLAGS"
175+
fi
176+
177+
if [[ $CODE_COVERAGE == [yY]* ]]; then
178+
echo "Trying to compile with code coverage instrumentation."
179+
COVERAGE="-coverage"
180+
fi
181+
182+
if [[ $CODE_PROFILE == [yY]* ]]; then
183+
echo "Trying to compile with code profiling instrumentation."
184+
PROFILING="-profile"
185+
fi
54186

187+
if [[ $FCOMPILER == custom ]]; then
188+
CUSTOM="-fc $FC"
55189
fi
56190

57191
#build the stand-alone library:
@@ -61,7 +195,7 @@ echo "Building library..."
61195
# work around for FoBiS.py PR #45
62196
[ -d "$LIBDIR" ] || mkdir "$LIBDIR"
63197

64-
FoBiS.py build -ch -compiler ${FCOMPILER} -cflags "${FCOMPILERFLAGS}" ${COVERAGE} -dbld ${LIBDIR} -s ${SRCDIR} -dmod ./ -dobj ./ -t ${MODCODE} -o ${LIBOUT} -mklib static -colors
198+
FoBiS.py build -ch -compiler ${FCOMPILER} ${CUSTOM} -cflags "${FCOMPILERFLAGS}" ${COVERAGE} ${PROFILING} -dbld ${LIBDIR} -s ${SRCDIR} -dmod ./ -dobj ./ -t ${MODCODE} -o ${LIBOUT} -mklib static -colors
65199

66200
#build the unit tests (uses the above library):
67201
if [[ $JF_SKIP_TESTS != [yY]* ]]; then
@@ -74,19 +208,23 @@ if [[ $JF_SKIP_TESTS != [yY]* ]]; then
74208
for TEST in "${TESTDIR%/}"/jf_test_*.f90; do
75209
THIS_TEST=${TEST##*/}
76210
echo "Build ${THIS_TEST%.f90}"
77-
FoBiS.py build -ch -compiler ${FCOMPILER} -cflags "${FCOMPILERFLAGS}" ${COVERAGE} -dbld ${BINDIR} -s ${TESTDIR} -i ${LIBDIR} -libs ${LIBDIR}/${LIBOUT} -dmod ./ -dobj ./ -t ${THIS_TEST} -o ${THIS_TEST%.f90} -colors
211+
FoBiS.py build -ch -compiler ${FCOMPILER} ${CUSTOM} -cflags "${FCOMPILERFLAGS}" ${COVERAGE} ${PROFILING} -dbld ${BINDIR} -s ${TESTDIR} -i ${LIBDIR} -libs ${LIBDIR}/${LIBOUT} -dmod ./ -dobj ./ -t ${THIS_TEST} -o ${THIS_TEST%.f90} -colors
78212
done
79213
else
80214
echo "Skip building the unit tests since \$JF_SKIP_TESTS has been set to 'true'."
81215
fi
82216

83217
#build the documentation with RoboDoc (if present):
84218
echo ""
85-
if hash robodoc 2>/dev/null; then
86-
echo "Building documentation..."
87-
robodoc --rc ./robodoc.rc --src ${SRCDIR} --doc ${DOCDIR} --documenttitle ${PROJECTNAME}
219+
if [[ $JF_SKIP_DOCS != [yY]* ]]; then
220+
if hash robodoc 2>/dev/null; then
221+
echo "Building documentation..."
222+
robodoc --rc ./robodoc.rc --src ${SRCDIR} --doc ${DOCDIR} --documenttitle ${PROJECTNAME}
223+
else
224+
echo "ROBODoc not found! Cannot build documentation. ROBODoc can be installed from: http://www.xs4all.nl/~rfsber/Robo/"
225+
fi
88226
else
89-
echo "ROBODoc not found! Cannot build documentation. ROBODoc can be installed from: http://www.xs4all.nl/~rfsber/Robo/"
227+
echo "Skip building documentation since \$JF_SKIP_DOCS has been set to ${JF_SKIP_DOCS}."
90228
fi
91229

92230
# Run all the tests unless $JF_SKIP_TESTS
@@ -104,5 +242,5 @@ if [[ $JF_SKIP_TESTS != [yY]* ]] ; then
104242
done
105243
GLOBIGNORE="$OLD_IGNORES"
106244
else
107-
echo "Skip running the unit tests since \$JF_SKIP_TESTS has been set to 'true'."
245+
echo "Skip running the unit tests since \$JF_SKIP_TESTS has been set to ${JF_SKIP_TESTS}."
108246
fi

0 commit comments

Comments
 (0)