From 7c155f74567a9e3d949369260e732daf13310269 Mon Sep 17 00:00:00 2001
From: Erik Schnetter <schnetter@gmail.com>
Date: Wed, 20 Apr 2016 16:33:59 -0400
Subject: [PATCH] Check the installed HDF5 library for consistency

---
 .../repos/builtin/packages/hdf5/package.py    | 63 ++++++++++++++++++-
 1 file changed, 60 insertions(+), 3 deletions(-)

diff --git a/var/spack/repos/builtin/packages/hdf5/package.py b/var/spack/repos/builtin/packages/hdf5/package.py
index cce609eb29..eb58eeb637 100644
--- a/var/spack/repos/builtin/packages/hdf5/package.py
+++ b/var/spack/repos/builtin/packages/hdf5/package.py
@@ -24,6 +24,8 @@
 ##############################################################################
 
 from spack import *
+import shutil
+import subprocess
 
 
 class Hdf5(Package):
@@ -114,14 +116,16 @@ def install(self, spec, prefix):
             # this is not actually a problem.
             extra_args.extend([
                 "--enable-parallel",
-                "CC=%s" % spec['mpi'].prefix.bin + "/mpicc",
+                "CC=%s" % join_path(spec['mpi'].prefix.bin, "mpicc"),
             ])
 
             if '+cxx' in spec:
-                extra_args.append("CXX=%s" % spec['mpi'].prefix.bin + "/mpic++")
+                extra_args.append("CXX=%s" % join_path(spec['mpi'].prefix.bin,
+                                                       "mpic++"))
 
             if '+fortran' in spec:
-                extra_args.append("FC=%s" % spec['mpi'].prefix.bin + "/mpifort")
+                extra_args.append("FC=%s" % join_path(spec['mpi'].prefix.bin,
+                                                      "mpifort"))
 
         if '+szip' in spec:
             extra_args.append("--with-szlib=%s" % spec['szip'].prefix)
@@ -138,6 +142,59 @@ def install(self, spec, prefix):
             *extra_args)
         make()
         make("install")
+        self.check_install(spec)
+
+    def check_install(self, spec):
+        "Build and run a small program to test the installed HDF5 library"
+        print "Checking HDF5 installation..."
+        checkdir = "spack-check"
+        with working_dir(checkdir, create=True):
+            source = r"""
+#include <hdf5.h>
+#include <assert.h>
+#include <stdio.h>
+int main(int argc, char **argv) {
+  unsigned majnum, minnum, relnum;
+  herr_t herr = H5get_libversion(&majnum, &minnum, &relnum);
+  assert(!herr);
+  printf("HDF5 version %d.%d.%d %u.%u.%u\n", H5_VERS_MAJOR, H5_VERS_MINOR,
+         H5_VERS_RELEASE, majnum, minnum, relnum);
+  return 0;
+}
+"""
+            expected = """\
+HDF5 version {version} {version}
+""".format(version=str(spec.version))
+            with open("check.c", 'w') as f:
+                f.write(source)
+            if '+mpi' in spec:
+                cc = which(join_path(spec['mpi'].prefix.bin, "mpicc"))
+            else:
+                cc = which('cc')
+            # TODO: Automate these path settings
+            cc('-c', "-I%s" % join_path(spec.prefix, "include"), "check.c")
+            cc('-o', "check", "check.o",
+               # I couldn't make libraries work on Darwin
+               "-L%s" % join_path(spec.prefix, "lib"), "-lhdf5",
+               # join_path(spec.prefix, "lib", "libhdf5.a"),
+               "-lz")
+            try:
+                output = subprocess.check_output("./check")
+            except:
+                output = ""
+            success = output == expected
+            if not success:
+                print "Produced output does not match expected output."
+                print "Expected output:"
+                print '-'*80
+                print expected
+                print '-'*80
+                print "Produced output:"
+                print '-'*80
+                print output
+                print '-'*80
+                raise RuntimeError("HDF5 install check failed")
+        shutil.rmtree(checkdir)
 
     def url_for_version(self, version):
         v = str(version)
-- 
GitLab