aboutsummaryrefslogtreecommitdiff
path: root/HDF5.sh
blob: 9eed71e4333d89f80619dfaaf7d5734b8e9c7d77 (plain)
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
#! /bin/bash

################################################################################
# Prepare
################################################################################

# Set up shell
if [ "$(echo ${VERBOSE} | tr '[:upper:]' '[:lower:]')" = 'yes' ]; then
    set -x                      # Output commands
fi
set -e                          # Abort on errors



################################################################################
# Check for old mechanism
################################################################################

if [ -n "${HDF5}" ]; then
    echo 'BEGIN ERROR'
    echo "Setting the option \"HDF5\" is incompatible with the HDF5 thorn. Please remove the option HDF5=${HDF5}."
    echo 'END ERROR'
    exit 1
fi



################################################################################
# Decide which libraries to link with
################################################################################

# Set up names of the libraries based on configuration variables. Also
# assign default values to variables.
HDF5_C_LIBS='hdf5_hl hdf5'
if [ "${HDF5_ENABLE_CXX:=yes}" = 'yes' ]; then
    HDF5_CXX_LIBS='hdf5_hl_cpp hdf5_cpp'
fi
if [ "${HDF5_ENABLE_FORTRAN:=yes}" = 'yes' ]; then
    if [ "${F90}" != "none" ]; then
        HDF5_FORTRAN_LIBS='hdf5hl_fortran hdf5_fortran'
    fi
fi



################################################################################
# Search
################################################################################

if [ -z "${HDF5_DIR}"                                                   \
     -o "$(echo "${HDF5_DIR}" | tr '[a-z]' '[A-Z]')" = 'BUILD' ]
then
    echo "BEGIN MESSAGE"
    echo "HDF5 selected, but HDF5_DIR not set. Checking some places..."
    echo "END MESSAGE"
    
    DIRS="/usr /usr/local /usr/local/hdf5 /usr/local/packages/hdf5 /usr/local/apps/hdf5 /opt/local ${HOME} ${HOME}/hdf5 c:/packages/hdf5"
    # look into each directory
    for dir in $DIRS; do
        # libraries might have different file extensions
        for libext in a so dylib; do
            # libraries can be in /lib or /lib64
            for libdir in lib64 lib; do
                FILES="include/hdf5.h $(for lib in ${HDF5_CXX_LIBS} ${HDF5_FORTRAN_LIBS} ${HDF5_C_LIBS}; do echo $libdir/lib${lib}.${libext}; done)"
                # assume this is the one and check all needed files
                HDF5_DIR="$dir"
                for file in $FILES; do
                    # discard this directory if one file was not found
                    if [ ! -r "$dir/$file" ]; then
                        unset HDF5_DIR
                        break
                    fi
                done
                # don't look further if all files have been found
                if [ -n "$HDF5_DIR" ]; then
                    break
                fi
           done
           # don't look further if all files have been found
           if [ -n "$HDF5_DIR" ]; then
               break
           fi
        done
        # don't look further if all files have been found
        if [ -n "$HDF5_DIR" ]; then
            break
        fi
    done
    
    if [ -z "$HDF5_DIR" ]; then
        echo "BEGIN MESSAGE"
        echo "HDF5 not found"
        echo "END MESSAGE"
    else
        echo "BEGIN MESSAGE"
        echo "Found HDF5 in ${HDF5_DIR}"
        echo "END MESSAGE"
    fi
fi



################################################################################
# Build
################################################################################

if [ -z "${HDF5_DIR}"                                                  \
     -o "$(echo "${HDF5_DIR}" | tr '[a-z]' '[A-Z]')" = 'BUILD' ]
then
    echo "BEGIN MESSAGE"
    echo "Using bundled HDF5..."
    echo "END MESSAGE"
    
    # Set locations
    THORN=HDF5
    NAME=hdf5-1.8.8
    SRCDIR=$(dirname $0)
    BUILD_DIR=${SCRATCH_BUILD}/build/${THORN}
    if [ -z "${HDF5_INSTALL_DIR}" ]; then
        INSTALL_DIR=${SCRATCH_BUILD}/external/${THORN}
    else
        echo "BEGIN MESSAGE"
        echo "Installing HDF5 into ${HDF5_INSTALL_DIR}"
        echo "END MESSAGE"
        INSTALL_DIR=${HDF5_INSTALL_DIR}
    fi
    DONE_FILE=${SCRATCH_BUILD}/done/${THORN}
    HDF5_DIR=${INSTALL_DIR}
    
    if [ -e ${DONE_FILE} -a ${DONE_FILE} -nt ${SRCDIR}/dist/${NAME}.tar.gz \
                         -a ${DONE_FILE} -nt ${SRCDIR}/HDF5.sh ]
    then
        echo "BEGIN MESSAGE"
        echo "HDF5 has already been built; doing nothing"
        echo "END MESSAGE"
    else
        echo "BEGIN MESSAGE"
        echo "Building HDF5"
        echo "END MESSAGE"
        
        # Build in a subshell
        (
        exec >&2                # Redirect stdout to stderr
        if [ "$(echo ${VERBOSE} | tr '[:upper:]' '[:lower:]')" = 'yes' ]; then
            set -x              # Output commands
        fi
        set -e                  # Abort on errors
        cd ${SCRATCH_BUILD}
        
        # Set up environment
        if [ "${F90}" = "none" ]; then
            echo 'BEGIN MESSAGE'
            echo 'No Fortran 90 compiler available. Building HDF5 library without Fortran support.'
            echo 'END MESSAGE'
            unset FC
            unset FCFLAGS
        else
            export FC="${F90}"
            export FCFLAGS="${F90FLAGS}"
        fi
        export LDFLAGS
        unset LIBS
        unset RPATH
        if echo '' ${ARFLAGS} | grep 64 > /dev/null 2>&1; then
            export OBJECT_MODE=64
        fi
        
        echo "HDF5: Preparing directory structure..."
        mkdir build external done 2> /dev/null || true
        rm -rf ${BUILD_DIR} ${INSTALL_DIR}
        mkdir ${BUILD_DIR} ${INSTALL_DIR}
        
        # Build core library
        echo "HDF5: Unpacking archive..."
        pushd ${BUILD_DIR}
        ${TAR} xzf ${SRCDIR}/dist/${NAME}.tar.gz
        
        echo "HDF5: Configuring..."
        cd ${NAME}
        # Do not build Fortran API if it has been disabled, or if
        # there is no Fortran 90 compiler.
        # Do not build C++ API if it has been disabled.
        # Do not build shared library executables if we want to link
        # statically.
        ./configure --prefix=${HDF5_DIR} --with-zlib=${ZLIB_DIR} --enable-cxx=${HDF5_ENABLE_CXX} $(if [ -n "${FC}" ]; then echo '' "--enable-fortran=${HDF5_ENABLE_FORTRAN}"; fi) $(if echo '' "${LDFLAGS}" | grep -q '[-]static'; then echo '' '--disable-shared --enable-static-exec'; fi)
        
        echo "HDF5: Building..."
        ${MAKE}
        
        echo "HDF5: Installing..."
        ${MAKE} install
        popd
        
        # Build checker
        echo "HDF5: Unpacking checker archive..."
        pushd ${BUILD_DIR}
        ${TAR} xzf ${SRCDIR}/dist/h5check_2_0.tar.gz
        
        echo "HDF5: Configuring checker..."
        cd h5check_2_0
        # Point the checker to the just-installed library
        export CPPFLAGS="${CPPFLAGS} -I${HDF5_DIR}/include"
        export LDFLAGS="${LDFLAGS} -L${HDF5_DIR}/lib"
        export H5CC="${CC}"
        export H5CC_PP="${CPP}"
        export H5FC="${FC}"
        export H5FC_PP="${FPP}"
        export H5CPP="${CXX}"
        ./configure --prefix=${HDF5_DIR} --with-zlib=${ZLIB_DIR}
        
        echo "HDF5: Building checker..."
        #${MAKE}
        (cd src && ${MAKE})
        (cd tool && ${MAKE})
        
        echo "HDF5: Installing checker..."
        # The build fails in the "test" subdirectory, because
        # /usr/include/hdf5.h (if it exists) is used instead of the
        # the one we just installed. We therefore skip the build in
        # the "test" subdirectory.
        #${MAKE} install
        (cd src && ${MAKE} install)
        (cd tool && ${MAKE} install)
        popd

        echo "HDF5: Cleaning up..."
        rm -rf ${BUILD_DIR}
        
        date > ${DONE_FILE}
        echo "HDF5: Done."
        
        )
        
        if (( $? )); then
            echo 'BEGIN ERROR'
            echo 'Error while building HDF5. Aborting.'
            echo 'END ERROR'
            exit 1
        fi
    fi
    
fi



################################################################################
# Check for additional libraries
################################################################################

# Set options
if [ "${HDF5_DIR}" = '/usr' -o "${HDF5_DIR}" = '/usr/local' ]; then
    # Fortran modules may be located in the lib directory
    HDF5_INC_DIRS='${HDF5_DIR}/lib'
    HDF5_LIB_DIRS=''
else
    # Fortran modules may be located in the lib directory
    HDF5_INC_DIRS="${HDF5_DIR}/include ${HDF5_DIR}/lib"
    HDF5_LIB_DIRS="${HDF5_DIR}/lib"
fi
HDF5_LIBS="${HDF5_CXX_LIBS} ${HDF5_FORTRAN_LIBS} ${HDF5_C_LIBS}"



# Check whether we are running on Windows
if perl -we 'exit (`uname` =~ /^CYGWIN/)'; then
    is_windows=0
else
    is_windows=1
fi

# Check whether we are running on MacOS
if perl -we 'exit (`uname` =~ /^Darwin/)'; then
    is_macos=0
else
    is_macos=1
fi



# Check whether we have to link with libsz.a
if grep -qe '#define H5_HAVE_LIBSZ 1' ${HDF5_DIR}/include/H5pubconf.h 2> /dev/null; then
    test_szlib=0
else
    test_szlib=1
fi
if [ $test_szlib -eq 0 ]; then
    HDF5_LIB_DIRS="$HDF5_LIB_DIRS $LIBSZ_DIR"
    if [ $is_windows -eq 0 ]; then
        HDF5_LIBS="$HDF5_LIBS sz"
    else
        HDF5_LIBS="$HDF5_LIBS szlib"
    fi
fi

# Check whether we have to link with libz.a
if grep -qe '#define H5_HAVE_LIBZ 1' ${HDF5_DIR}/include/H5pubconf.h 2> /dev/null; then
    test_zlib=0
else
    test_zlib=1
fi
if [ $test_zlib -eq 0 ]; then
    HDF5_LIB_DIRS="$HDF5_LIB_DIRS $LIBZ_DIR"
    if [ $is_windows -eq 0 ]; then
        HDF5_LIBS="$HDF5_LIBS z"
    else
        HDF5_LIBS="$HDF5_LIBS zlib"
    fi
fi

# Add the math library which might not be linked by default
if [ $is_windows -eq 0 ]; then
    HDF5_LIBS="$HDF5_LIBS m"
fi



################################################################################
# Configure Cactus
################################################################################

# Pass options to Cactus
echo "BEGIN MAKE_DEFINITION"
echo "HAVE_HDF5           = 1"
echo "HDF5_DIR            = ${HDF5_DIR}"
echo "HDF5_ENABLE_CXX     = ${HDF5_ENABLE_CXX}"
echo "HDF5_ENABLE_FORTRAN = ${HDF5_ENABLE_FORTRAN}"
echo "HDF5_INC_DIRS       = ${HDF5_INC_DIRS}"
echo "HDF5_LIB_DIRS       = ${HDF5_LIB_DIRS}"
echo "HDF5_LIBS           = ${HDF5_LIBS}"
echo "END MAKE_DEFINITION"

echo 'INCLUDE_DIRECTORY $(HDF5_INC_DIRS)'
echo 'LIBRARY_DIRECTORY $(HDF5_LIB_DIRS)'
echo 'LIBRARY           $(HDF5_LIBS)'