Fixing stanzas to appropriately break out flags and be sanitized correctly
[WPS.git] / configure
blob946cba98055c4bc13b7d7a078336698a77470fbd
1 #!/bin/sh
3 print_help()
5 echo 'Usage: configure [options]'
6 echo ''
7 echo 'where options may be any of the following:'
8 echo ''
9 echo ' --nowrf Disables checks to find the path to the compiled WRF model.'
10 echo ' This enables some WPS programs to be compiled without'
11 echo ' requiring WRF to have been compiled, but it will preclude'
12 echo ' the compilation of WPS programs that depend on the WRF I/O'
13 echo ' API (geogrid, metgrid, and int2nc).'
14 echo ''
15 echo ' --build-grib2-libs Compiles zlib, libpng, and JasPer libraries from source in'
16 echo ' the external/ directory, and installs the libraries in'
17 echo ' grib2/.'
18 echo ''
19 echo ' -h/--help Print this help message and quit.'
23 # Check for command-line arguments
24 # At present, the only supported arguments are:
25 # --nowrf : disables checks to find the path to the compiled WRF model
26 # --build-grib2-libs : compiles zlib, libpng, and JasPer libraries from
27 # source in the external/ directory and installs
28 # the libraries in grib2/
30 nowrf=0
31 build_grib2=0
32 for arg in $@; do
33 if [ "${arg}" = "--nowrf" ]; then
34 nowrf=1
35 elif [ "${arg}" = "--build-grib2-libs" ]; then
36 build_grib2=1
37 elif [ "${arg}" = "--help" ] || [ "${arg}" = "-h" ]; then
38 print_help
39 exit
40 else
41 printf "Unrecognized option %s\n" ${arg}
43 done
46 # Look for netcdf
47 if test -z "$NETCDF" ; then
48 for p in /usr/local/netcdf
50 if test -d $p ; then
51 NETCDF=$p
52 break
54 done
57 if test -z "$NETCDF" ; then
59 if [ `hostname | cut -c 1-2` = "bs" -o \
60 `hostname | cut -c 1-2` = "bv" -o \
61 `hostname` = "tempest" -o `hostname` = "lightning" ] ; then
62 echo 'Compiling on an NCAR system with weird paths to NetCDF'
63 echo 'Setting up a local NetCDF directory with symlinks'
64 if ( test -d ./netcdf_links ) ; then
65 echo 'A directory ./netcdf_links already exists. Continuing.'
66 else
67 mkdir ./netcdf_links
68 if [ -z "$OBJECT_MODE" ] ; then
69 OBJECT_MODE=32
70 export OBJECT_MODE
72 if [ $OBJECT_MODE -ne 64 -a \( `hostname | cut -c 1-2` = "bs" \) ] ; then
73 ( cd ./netcdf_links ; ln -s /usr/local/lib32/r4i4 ./lib ; \
74 ln -s /usr/local/include ./include )
75 else
76 ( cd ./netcdf_links ; ln -s /usr/local/lib64/r4i4 ./lib ; \
77 ln -s /usr/local/include ./include )
80 NETCDF=`pwd`/netcdf_links
81 export NETCDF
84 else
85 bedone=""
86 if [ -d ./netcdf_links ] ; then
87 echo '** There is an existing ./netcdf_links file. Should I use? [y]'
88 read resp
89 if [ "$resp" = "y" ] ; then
90 NETCDF=`pwd`/netcdf_links
91 bedone="yes"
92 else
93 echo 'Removing existing ./netcdf_links directory'
94 /bin/rm -fr ./netcdf_links
96 else
97 echo '** WARNING: No path to NETCDF and environment variable NETCDF not set.'
98 echo '** would you like me to try to fix? [y]'
100 netcdfipath=""
101 netcdflpath=""
102 while [ -z "$bedone" ] ; do
103 read resp
104 if [ "$resp" = "y" -o -z "$resp" ] ; then
105 if [ -d ./netcdf_links ] ; then
106 echo 'There is already a ./netcdf_links directory. Okay to use links'
107 echo 'in this directory for NetCDF include and lib dirs? [y]'
108 read resp
109 if [ "$resp" = "y" ] ; then
110 NETCDF=`pwd`/netcdf_links
111 export NETCDF
112 bedone="yes"
113 continue
116 if [ -z "$netcdfipath" ] ; then
117 echo 'Enter full path to NetCDF include directory on your system'
118 read resp
119 if [ ! -d "$resp" ] ; then
120 echo "invalid path: $resp. Try again? [y]" ; continue
122 netcdfipath=$resp
124 if [ -z "$netcdflpath" ] ; then
125 echo 'Enter full path to NetCDF library directory on your system'
126 read resp
127 if [ ! -d "$resp" ] ; then
128 echo "invalid path: $resp. Try again? [y]" ; continue
130 netcdflpath=$resp
133 if [ -n "$netcdflpath" -a -n "$netcdfipath" ] ; then
134 if [ -d ./netcdf_links ] ; then
135 echo 'Existing ./netcdf_links directory. Okay to remove. [y]'
136 read resp
137 if [ "$resp" = "y" ] ; then
138 /bin/rm -fr ./netcdf_links
141 mkdir ./netcdf_links
142 cd ./netcdf_links
143 ln -s "$netcdfipath" include
144 ln -s "$netcdflpath" lib
145 cd ..
146 echo created new ./netcdf_links directory
147 /bin/ls -lF ./netcdf_links
148 NETCDF=`pwd`/netcdf_links
149 export NETCDF
150 bedone="yes"
152 else
153 bedone="yes"
155 done
159 if [ -n "$NETCDF" ] ; then
160 echo "Will use NETCDF in dir: $NETCDF"
161 # for 3.6.2 and greater there might be a second library, libnetcdff.a . Check for this and use
162 # if available
163 NETCDFF=" "
164 if [ -f "$NETCDF/lib/libnetcdff.a" ] ; then
165 NETCDFF="-lnetcdff"
167 else
168 echo "Will configure for use without NetCDF"
171 # if the uname command exists, give it a shot and see if
172 # we can narrow the choices; otherwise, spam 'em
173 os="ARCH"
174 mach="ARCH"
175 type uname > /dev/null
176 if [ $? -eq 0 ] ; then
177 os=`uname`
178 if [ "$os" = "AIX" -o "$os" = "IRIX" -o "$os" = "IRIX64" -o "$os" = "SunOS" -o "$os" = "HP-UX" -o "$os" = "Darwin" ] ; then
179 mach="ARCH"
180 else
181 xxx=`expr "$os" : '\(.........\).*'`
182 if [ "$xxx" = "CYGWIN_NT" ] ; then
183 os=$xxx
185 if [ "$os" = "OSF1" -o "$os" = "Linux" -o "$os" = "UNICOS/mp" -o "$os" = "UNIX_System_V" -o "$os" = "CYGWIN_NT" ] ; then
186 mach=`uname -m`
187 else
188 os="ARCH"
189 mach="ARCH"
194 wrf_dir="none"
195 standard_wrf_dirs="WRF WRF-4.0.3 WRF-4.0.2 WRF-4.0.1 WRF-4.0 WRFV3"
197 if [ ${nowrf} -eq 0 ]; then
199 # If no WRF_DIR environment variable is set, try to locate a WRF build in one
200 # of the expected directory names one directory level up; otherwise, try to use
201 # the WRF I/O library from the code in $WRF_DIR
203 if [ -z "$WRF_DIR" ]; then
204 # for d in WRF WRF-4.0.3 WRF-4.0.2 WRF-4.0.1 WRF-4.0 WRFV3; do
205 for d in ${standard_wrf_dirs}; do
206 if [ -e ../${d}/external/io_netcdf/libwrfio_nf.a ]; then
207 echo "Found what looks like a valid WRF I/O library in ../${d}"
208 wrf_dir="../${d}"
209 break
211 done
212 else
213 if [ ! -e ${WRF_DIR}/external/io_netcdf/libwrfio_nf.a ]; then
214 echo ""
215 echo "Error: The \$WRF_DIR environment variable was set, but the WRF code at"
216 echo " ${WRF_DIR} doesn't appear to have been successfully compiled"
217 echo ""
218 exit
221 echo "Using WRF I/O library in WRF build identified by \$WRF_DIR: ${WRF_DIR}"
222 wrf_dir=$WRF_DIR
225 if [ $wrf_dir = "none" ]; then
226 echo ""
227 echo "Error: No compiled WRF code found. Please check that the WRF model has been compiled"
228 echo " one directory level up (i.e., ../) in a directory named 'WRF', 'WRF-4.0.3', 'WRF-4.0.2', etc.,"
229 echo " or specify the full path to the compiled WRF model with the environment variable \$WRF_DIR ."
230 echo ""
231 echo "Alternatively, if only WPS components that do no depend on WRF I/O libraries are needed, re-run"
232 echo "the configure script with the --nowrf option."
233 echo ""
234 exit
236 else
237 cat << EOF
238 ********************************************************************************
239 Configuring the WPS without a compiled WRF model.
240 It will not be possible to build the following WPS components, which depend on
241 the WRF I/O libraries:
242 - geogrid
243 - metgrid
244 - int2nc
245 ********************************************************************************
249 grib2dir=""
250 if [ ${build_grib2} -eq 1 ]; then
251 grib2dir="${PWD}/grib2"
253 cat << EOF
254 ********************************************************************************
255 The zlib, libpng, and JasPer libraries will be compiled from source and
256 installed in ${grib2dir} .
258 The environment variables JASPERLIB and JASPERINC will be ignored.
259 ********************************************************************************
263 # Found perl, so proceed with configuration
264 perl arch/Config.pl -perl=$PERL -netcdf=$NETCDF -netcdff=$NETCDFF -os=$os -mach=$mach -wrfdir=$wrf_dir -grib2dir=$grib2dir
267 #Checking cross-compiling capability for some particular environment
268 #on Linux and Mac box
270 if [ $os = "Linux" -o $os = "Darwin" ]; then
272 SFC=`grep '^SFC' configure.wps | awk '{print $3}'`
273 SCC=`grep '^SCC' configure.wps | awk '{print $3}'`
275 SFC="`type $SFC 2>/dev/null | awk '{print $NF}' | sed -e 's/(//g;s/)//g'`"
276 SCC="`type $SCC 2>/dev/null | awk '{print $NF}' | sed -e 's/(//g;s/)//g'`"
278 if [ -e $NETCDF/lib/libnetcdf.a -a "$SFC" != "" -a "$SCC" != "" ]; then
280 SFC_MULTI_ABI=0
281 SCC_MULTI_ABI=0
282 CROSS_COMPILING=0
284 foo=foo_$$
286 echo
287 echo Testing for NetCDF, C and Fortran compiler
288 echo
290 cat > ${foo}.c <<EOF
291 int main(int argc, char ** argv)
293 return (0);
297 cat > ${foo}.f <<EOF
298 program test_exit
299 integer :: STATUS = 0
300 call EXIT(STATUS)
301 end program test_exit
304 nct=`nm $NETCDF/lib/libnetcdf.a | grep '^0' | cut -d' ' -f 1 | head -1 | wc -c | awk '{print $1+0}'`
305 if [ "$nct" -eq "9" ]; then
306 netcdf_arch="32-bit"
307 else
308 netcdf_arch="64-bit"
311 $SFC -o ${foo} ${foo}.f > /dev/null 2>&1
312 SFC_arch="`file ${foo} | grep -o -E '[0-9]{2}-bit|i386'`"
313 rm ${foo} ${foo}.o 2> /dev/null
315 $SCC -o ${foo} ${foo}.c > /dev/null 2>&1
316 SCC_arch="`file ${foo} | grep -o -E '[0-9]{2}-bit|i386'`"
317 rm ${foo} ${foo}.o 2> /dev/null
319 if [ "$SFC_arch" = "" -o "$SCC_arch" = "" ]; then
320 echo " One of compilers testing failed!"
321 echo " Please check your compiler"
322 echo
323 rm -f ${foo} ${foo}.[cfo] 2> /dev/null
324 exit
325 else
326 cp configure.wps configure.wps.edit
329 case $netcdf_arch in
331 32-bit|i386 )
333 if [ "$SFC_arch" = "64-bit" ] ; then
334 CROSS_COMPILING=1
335 $SFC -m32 -o ${foo} ${foo}.f > /dev/null 2>&1
336 if [ $? = 0 ]; then
337 SFC_MULTI_ABI=1
338 sed '/^SFC.*=/s/$/ -m32/' configure.wps.edit > configure.wps.tmp
339 mv configure.wps.tmp configure.wps.edit
342 if [ "$SCC_arch" = "64-bit" ] ; then
343 CROSS_COMPILING=1
344 $SCC -m32 -o ${foo} ${foo}.c > /dev/null 2>&1
345 if [ $? = 0 ]; then
346 SCC_MULTI_ABI=1
347 sed '/^SCC.*=/s/$/ -m32/' configure.wps.edit > configure.wps.tmp
348 mv configure.wps.tmp configure.wps.edit
352 if [ $CROSS_COMPILING -eq 1 ] ; then
353 echo NOTE:
354 echo This installation NetCDF is 32-bit
355 if [ \( $SFC_MULTI_ABI -ne 1 -a "$SFC_arch" = "64-bit" \) \
356 -o \( $SCC_MULTI_ABI -ne 1 -a "$SCC_arch" = "64-bit" \) ] ; then
357 rm configure.wps.edit
358 echo One of compilers is 64-bit and doesn\'t support cross-compiling.
359 echo Please check your NETCDF lib and compiler
360 else
361 echo -m32 is appended to configure.wps
362 echo It will be forced to build in 32-bit.
363 echo If you don\'t want 32-bit binaries, please use 64-bit NetCDF, and re-run the configure script.
368 64-bit )
370 if [ "$SFC_arch" = "32-bit" -o "$SFC_arch" = "i386" ] ; then
371 CROSS_COMPILING=1
372 $SFC -m64 -o ${foo} ${foo}.f > /dev/null 2>&1
373 if [ $? = 0 ]; then
374 SFC_MULTI_ABI=1
375 sed '/^SFC.*=/s/$/ -m64/' configure.wps.edit > configure.wps.tmp
376 mv configure.wps.tmp configure.wps.edit
379 if [ "$SCC_arch" = "32-bit" -o "$SCC_arch" = "i386" ] ; then
380 CROSS_COMPILING=1
381 $SCC -m64 -o ${foo} ${foo}.c > /dev/null 2>&1
382 if [ $? = 0 ]; then
383 SCC_MULTI_ABI=1
384 sed '/^SCC.*=/s/$/ -m64/' configure.wps.edit > configure.wps.tmp
385 mv configure.wps.tmp configure.wps.edit
389 if [ $CROSS_COMPILING -eq 1 ] ; then
390 echo NOTE:
391 echo This installation NetCDF is 64-bit
392 if [ \( $SFC_MULTI_ABI -ne 1 -a "$SFC_arch" != "64-bit" \) \
393 -o \( $SCC_MULTI_ABI -ne 1 -a "$SCC_arch" != "64-bit" \) ]; then
394 rm configure.wps.edit
395 echo One of Compilers is 32-bit and doesn\'t support cross-compiling.
396 echo Please check your NetCDF lib and compiler
397 else
398 echo -m64 is appended to configure.wps
399 echo It will be forced to build in 64-bit.
400 echo If you don\'t want 64-bit binaries, please use 32-bit NetCDF, and re-run the configure script.
404 esac
405 rm -f ${foo} ${foo}.[cfo] 2> /dev/null
407 if [ -e configure.wps.edit ]; then
408 mv configure.wps.edit configure.wps
411 if [ $CROSS_COMPILING -eq 0 ] ; then
412 echo This installation NetCDF is $netcdf_arch
413 echo C compiler is $SCC_arch
414 echo Fortran compiler is $SFC_arch
416 echo
418 # Does our Fortran work with the given NETCDF library.
420 /bin/rm -f fort_netcdf.f fort_netcdf
421 cat > fort_netcdf.f <<EOF
422 program foo
423 include 'netcdf.inc'
424 integer ncid , status
425 status = nf_open ( 'foo.nc' , 0 , ncid )
426 print *,'status = ',status
427 end program
429 FFLAGS=`grep ^FFLAGS configure.wps | cut -d"=" -f2-`
430 cp $NETCDF/include/netcdf.inc .
431 FC=`grep ^SFC configure.wps | cut -d"=" -f2-`
432 $FC ${FFLAGS} fort_netcdf.f -o fort_netcdf -L${NETCDF}/lib $NETCDFF -lnetcdf > /dev/null 2>&1
433 if [ -f "fort_netcdf" ] ; then
434 ./fort_netcdf > /dev/null 2>&1
435 if [ $? = 0 ]; then
436 /bin/rm -f fort_netcdf.f fort_netcdf.o fort_netcdf netcdf.inc
437 else
438 echo
439 echo "Your Fortran + NETCDF did not run successfully."
440 echo
442 else
443 echo
444 echo "Your versions of Fortran and NETCDF are not consistent."
445 echo
448 cat > c_test.c <<EOF
449 #ifndef CRAY
450 # ifdef NOUNDERSCORE
451 # define C_TEST c_test
452 # else
453 # if defined ( F2CSTYLE ) || defined ( _DOUBLEUNDERSCORE )
454 # define C_TEST c_test__
455 # else
456 # define C_TEST c_test_
457 # endif
458 # endif
459 #endif
460 #include <stdio.h>
462 int C_TEST ( float *xx, int *ii )
465 printf("OK print in C function. \n" ) ;
466 printf("Values are xx = %5.2f and ii = %d \n", *xx, *ii ) ;
467 return(0) ;
470 CFLAGS=`grep ^CFLAGS configure.wps | cut -d"=" -f2-`
471 CPPFLAGS=`grep ^CPPFLAGS configure.wps | cut -d"=" -f2-`
472 CC=`grep ^SCC configure.wps | cut -d"=" -f2-`
473 ${CC} -c ${CFLAGS} ${CPPFLAGS} c_test.c
475 cat > f_test.f90 <<EOF
476 PROGRAM f_test
477 INTEGER :: ii
478 REAL :: xx
480 ii = 1
481 xx = 2
483 CALL c_test ( xx , ii )
485 END PROGRAM f_test
487 $FC ${FFLAGS} f_test.f90 -o cf_test c_test.o > /dev/null 2>&1
488 if [ -f "cf_test" ] ; then
489 ./cf_test > /dev/null 2>&1
490 if [ $? = 0 ]; then
491 /bin/rm -f cf_test c_test.c c_test.o f_test.f90 f_test.o
492 else
493 echo
494 echo "Your Fortran + C did not run successfully."
495 echo
497 else
498 echo
499 echo "Your versions of Fortran and C are not consistent."
500 echo
508 # Check for newer GNU Fortran compilers that require the use of the following:
509 # -fallow-argument-mismatch
511 cat > gnu_flag_test.F90 << EOF
512 program gnu_flag_test
513 #ifdef __GNUC__
514 #if __GNUC__ > 9
515 call exit(1) ! A GNU extension, but at this point we know this is a GNU compiler
516 #endif
517 #endif
518 end program gnu_flag_test
521 # The above test program gives exit status 1 iff we are using the GNU Fortran
522 # compiler with a major version greater than 9
524 FC=`grep ^SFC configure.wps | cut -d"=" -f2-`
525 FFLAGS=`grep ^FFLAGS configure.wps | cut -d"=" -f2-`
526 $FC ${FFLAGS} gnu_flag_test.F90 -o gnu_flag_test > /dev/null 2>&1
527 if [ -f ./gnu_flag_test ]; then
528 ./gnu_flag_test > /dev/null 2>&1
529 if [ $? -eq 1 ]; then
530 compat="-fallow-argument-mismatch"
532 rm gnu_flag_test
533 else
534 printf "*** Failed to compile the gnu_flag_test program!\n"
535 printf " This may be because the selected build option does not work correctly.\n"
537 rm gnu_flag_test.F90
539 sed "s/CONFIGURE_COMPAT_FLAGS/${compat}/" configure.wps > configure.wps.tmp
540 mv configure.wps.tmp configure.wps