Browse Source

Use included Lua plugin for GCC to compile FFI bindings

This resolves an ABI incompatibility with HDF5 version 1.8.14.
Peter Colberg 4 years ago
parent
commit
01480ec8ae
9 changed files with 124 additions and 1563 deletions
  1. 1
    0
      .gitignore
  2. 12
    11
      Makefile
  3. 17
    25
      doc/INSTALL.html
  4. 37
    69
      doc/INSTALL.mdwn
  5. 0
    1447
      hdf5/C.lua
  6. 2
    4
      hdf5/C.lua.in
  7. 17
    7
      hdf5/Makefile
  8. 36
    0
      hdf5/hdf5.c
  9. 2
    0
      hdf5/init.lua

+ 1
- 0
.gitignore View File

@@ -1,4 +1,5 @@
1 1
 /examples/*.h5
2
+/hdf5/C.lua
2 3
 /test/*.h5
3 4
 /test/*.out
4 5
 *.rock

+ 12
- 11
Makefile View File

@@ -17,16 +17,10 @@ FILES_DOC = index.mdwn INSTALL.mdwn README.mdwn reference.mdwn CHANGES.mdwn
17 17
 FILES_DOC_HTML = index.html INSTALL.html README.html reference.html CHANGES.html pandoc.css lua-hdf5.png hyperslab.svg
18 18
 FILES_EXAMPLES = attribute.lua dataset.lua dataspace.lua
19 19
 
20
-all: hdf5
21
-
22
-hdf5:
23
-	@$(MAKE) -C hdf5
24
-
25
-test:
26
-	@$(MAKE) -C test
27
-
28
-doc:
29
-	@$(MAKE) -C doc
20
+all: hdf5 test
21
+gcc-lua-cdecl: gcc-lua
22
+hdf5: gcc-lua-cdecl
23
+test: hdf5
30 24
 
31 25
 install:
32 26
 	$(INSTALL_D) $(DESTDIR)$(LUADIR)/hdf5
@@ -39,5 +33,12 @@ install:
39 33
 clean:
40 34
 	@$(MAKE) -C hdf5 clean
41 35
 	@$(MAKE) -C test clean
36
+	@$(MAKE) -C gcc-lua clean
37
+	@$(MAKE) -C gcc-lua-cdecl clean
38
+
39
+SUBDIRS = hdf5 test doc gcc-lua gcc-lua-cdecl
40
+
41
+.PHONY: $(SUBDIRS)
42 42
 
43
-.PHONY: hdf5 test doc install clean
43
+$(SUBDIRS):
44
+	@$(MAKE) -C $@

+ 17
- 25
doc/INSTALL.html View File

@@ -26,10 +26,11 @@
26 26
 <ul>
27 27
 <li><a href="#git">Git</a></li>
28 28
 <li><a href="#luarocks">LuaRocks</a></li>
29
-<li><a href="#ffi-c-bindings">FFI C bindings</a></li>
30 29
 </ul>
31 30
 </div>
32
-<p>Before installing HDF5 for Lua, <a href="http://www.hdfgroup.org/HDF5/release/obtain5.html">obtain the HDF5 1.8 library</a> if not shipped with your distribution.</p>
31
+<p>Before installing HDF5 for Lua, obtain the <a href="http://www.hdfgroup.org/HDF5/release/obtain5.html">HDF5 1.8</a> library if not shipped with your distribution.</p>
32
+<p>GCC 4.5 or later is required to compile <a href="http://luajit.org/ext_ffi.html">FFI</a> bindings for the HDF5 library.</p>
33
+<p>For parallel HDF5, you need an MPI C compiler and <a href="http://colberg.org/lua-mpi/">MPI for Lua</a>.</p>
33 34
 <h2 id="git">Git</h2>
34 35
 <p>HDF5 for Lua is available from a <a href="http://git.colberg.org/lua-hdf5">git repository</a>:</p>
35 36
 <pre><code>git clone http://git.colberg.org/lua-hdf5</code></pre>
@@ -39,10 +40,20 @@
39 40
 <pre><code>git tag -v 1.1.0</code></pre>
40 41
 <pre><code>Primary key fingerprint: 2997 6EC2 2BD1 EBBC CC77  F3A6 183B D5ED 3527 8611
41 42
      Subkey fingerprint: 983B F9D0 F02A 6014 0CEC  529F 767E 12A8 C92A 31CB</code></pre>
42
-<p>The module is accompanied by a test suite:</p>
43
-<pre><code>make test</code></pre>
44
-<p>If HDF5 is compiled with MPI-IO support, set <code>MPIEXEC</code>:</p>
45
-<pre><code>make test MPIEXEC=&quot;mpirun -np 8&quot;</code></pre>
43
+<p>If the HDF5 header files are in a nonstandard include path, set <code>CPATH</code>:</p>
44
+<pre><code>export CPATH=/usr/include/hdf5/mpich</code></pre>
45
+<p>If the HDF5 library is in a nonstandard library path, set <code>LIBRARY_PATH</code>:</p>
46
+<pre><code>export LIBRARY_PATH=/usr/lib/x86_64-linux-gnu/hdf5/mpich</code></pre>
47
+<p>If LuaJIT is not installed system-wide, set <code>PKG_CONFIG_PATH</code>:</p>
48
+<pre><code>export PKG_CONFIG_PATH=/opt/luajit-2.0.3/lib/pkgconfig</code></pre>
49
+<p>Compile the <a href="http://luajit.org/ext_ffi.html">FFI</a> bindings for the HDF5 library and run the test suite:</p>
50
+<pre><code>make</code></pre>
51
+<p>To override the default GCC C compiler <code>gcc</code>, set <code>CC</code>:</p>
52
+<pre><code>make CC=gcc-4.7</code></pre>
53
+<p>For parallel HDF5, to override the default MPI C compiler <code>mpicc</code>, set <code>CPP</code>:</p>
54
+<pre><code>make CPP=mpicc.mpich</code></pre>
55
+<p>To test parallel HDF5 using the MPI-IO driver:</p>
56
+<pre><code>make MPIEXEC=&quot;mpirun -np 8&quot;</code></pre>
46 57
 <h2 id="luarocks">LuaRocks</h2>
47 58
 <p>HDF5 for Lua is available from <a href="http://luarocks.org/">LuaRocks</a>:</p>
48 59
 <pre><code>luarocks install --local hdf5</code></pre>
@@ -55,25 +66,6 @@
55 66
 <p>To install the module directly from the git source, use:</p>
56 67
 <pre><code>luarocks make --local rockspec/hdf5-1.1.0-1.rockspec</code></pre>
57 68
 <p>In this case you must manually git checkout the corresponding version beforehand.</p>
58
-<h2 id="ffi-c-bindings">FFI C bindings</h2>
59
-<p>HDF5 for Lua includes precompiled FFI C bindings for the HDF5 library with support for parallel HDF5 using <a href="http://www.open-mpi.org/">Open MPI</a>. If you wish to compile the FFI C bindings yourself, for example when using a different MPI library or when packaging for a distribution, you need to install the <a href="http://colberg.org/gcc-lua/">Lua plugin for GCC</a> and <a href="http://colberg.org/gcc-lua-cdecl/">cdecl for GCC</a>.</p>
60
-<p>To generate FFI C bindings that match the HDF5 library, run:</p>
61
-<pre><code>make -B hdf5</code></pre>
62
-<p>This assumes that the Lua plugin for GCC is installed in the <a href="http://gcc.gnu.org/onlinedocs/gccint/Plugins-loading.html">GCC plugin directory</a>.</p>
63
-<p>If the plugin is located in a different directory, override the make variable <code>GCCLUA</code>:</p>
64
-<pre><code>make -B hdf5 GCCLUA=${HOME}/projects/gcc-lua/gcc/gcclua.so</code></pre>
65
-<p>You may also specify the plugin directory in the environment variable <code>LD_LIBRARY_PATH</code>:</p>
66
-<pre><code>export LD_LIBRARY_PATH=&quot;${HOME}/projects/gcc-lua/gcc${LD_LIBRARY_PATH+:$LD_LIBRARY_PATH}&quot;</code></pre>
67
-<p>The plugin may then be loaded by passing its filename:</p>
68
-<pre><code>make -B hdf5 GCCLUA=gcclua.so</code></pre>
69
-<p>To override the default MPI compiler <code>mpicc</code> for parallel HDF5, set <code>CPP</code>:</p>
70
-<pre><code>make -B hdf5 CPP=mpicc.mpich</code></pre>
71
-<p>To override the default GCC C compiler <code>gcc</code>, set <code>CC</code>:</p>
72
-<pre><code>make -B hdf5 CC=gcc-4.7</code></pre>
73
-<p>If the HDF5 header files are not installed in a system path, set <code>CPATH</code>:</p>
74
-<pre><code>export CPATH=/opt/hdf5-1.8.12/include:${HOME}/.luarocks/lib/luarocks/rocks/gcc-lua-cdecl/2.0.1-1/include</code></pre>
75
-<p>To override the name of the HDF5 library, set <code>LIBNAME</code>:</p>
76
-<pre><code>make -B hdf5 LIBNAME=libhdf5.so.7</code></pre>
77 69
 </div>
78 70
 <div class="footer">
79 71
 &copy; Copyright 2013–2014, Peter Colberg.

+ 37
- 69
doc/INSTALL.mdwn View File

@@ -2,10 +2,16 @@
2 2
 title: Installing
3 3
 ---
4 4
 
5
-Before installing HDF5 for Lua, [obtain the HDF5 1.8 library] if not shipped
5
+Before installing HDF5 for Lua, obtain the [HDF5 1.8] library if not shipped
6 6
 with your distribution.
7 7
 
8
-[obtain the HDF5 1.8 library]: http://www.hdfgroup.org/HDF5/release/obtain5.html
8
+GCC 4.5 or later is required to compile [FFI] bindings for the HDF5 library.
9
+
10
+For parallel HDF5, you need an MPI C compiler and [MPI for Lua].
11
+
12
+[HDF5 1.8]: http://www.hdfgroup.org/HDF5/release/obtain5.html
13
+[FFI]: http://luajit.org/ext_ffi.html
14
+[MPI for Lua]: http://colberg.org/lua-mpi/
9 15
 
10 16
 
11 17
 Git
@@ -34,122 +40,84 @@ Primary key fingerprint: 2997 6EC2 2BD1 EBBC CC77  F3A6 183B D5ED 3527 8611
34 40
      Subkey fingerprint: 983B F9D0 F02A 6014 0CEC  529F 767E 12A8 C92A 31CB
35 41
 ~~~
36 42
 
37
-The module is accompanied by a test suite:
43
+If the HDF5 header files are in a nonstandard include path, set `CPATH`:
38 44
 
39 45
 ~~~
40
-make test
46
+export CPATH=/usr/include/hdf5/mpich
41 47
 ~~~
42 48
 
43
-If HDF5 is compiled with MPI-IO support, set `MPIEXEC`:
49
+If the HDF5 library is in a nonstandard library path, set `LIBRARY_PATH`:
44 50
 
45 51
 ~~~
46
-make test MPIEXEC="mpirun -np 8"
52
+export LIBRARY_PATH=/usr/lib/x86_64-linux-gnu/hdf5/mpich
47 53
 ~~~
48 54
 
49
-[git repository]: http://git.colberg.org/lua-hdf5
50
-
51
-
52
-LuaRocks
53
---------
54
-
55
-HDF5 for Lua is available from [LuaRocks]:
55
+If LuaJIT is not installed system-wide, set `PKG_CONFIG_PATH`:
56 56
 
57 57
 ~~~
58
-luarocks install --local hdf5
58
+export PKG_CONFIG_PATH=/opt/luajit-2.0.3/lib/pkgconfig
59 59
 ~~~
60 60
 
61
-You may wish to remove older versions before installing:
61
+Compile the [FFI] bindings for the HDF5 library and run the test suite:
62 62
 
63 63
 ~~~
64
-luarocks remove --local hdf5
64
+make
65 65
 ~~~
66 66
 
67
-To use the module, set the Lua module path in your shell's startup file:
67
+To override the default GCC C compiler `gcc`, set `CC`:
68 68
 
69 69
 ~~~
70
-eval `luarocks path`
70
+make CC=gcc-4.7
71 71
 ~~~
72 72
 
73
-If the LuaRocks mirror is not available, install from the git repository above:
73
+For parallel HDF5, to override the default MPI C compiler `mpicc`, set `CPP`:
74 74
 
75 75
 ~~~
76
-luarocks install --local rockspec/hdf5-1.1.0-1.rockspec
76
+make CPP=mpicc.mpich
77 77
 ~~~
78 78
 
79
-To install the module directly from the git source, use:
79
+To test parallel HDF5 using the MPI-IO driver:
80 80
 
81 81
 ~~~
82
-luarocks make --local rockspec/hdf5-1.1.0-1.rockspec
82
+make MPIEXEC="mpirun -np 8"
83 83
 ~~~
84 84
 
85
-In this case you must manually git checkout the corresponding version beforehand.
86
-
87
-[LuaRocks]: http://luarocks.org/
88
-
89
-
90
-FFI C bindings
91
---------------
92
-
93
-HDF5 for Lua includes precompiled FFI C bindings for the HDF5 library with
94
-support for parallel HDF5 using [Open MPI]. If you wish to compile the FFI
95
-C bindings yourself, for example when using a different MPI library or when
96
-packaging for a distribution, you need to install the [Lua plugin for GCC]
97
-and [cdecl for GCC].
98
-
99
-To generate FFI C bindings that match the HDF5 library, run:
100
-
101
-~~~
102
-make -B hdf5
103
-~~~
104
-
105
-This assumes that the Lua plugin for GCC is installed in the [GCC plugin
106
-directory].
85
+[git repository]: http://git.colberg.org/lua-hdf5
107 86
 
108
-If the plugin is located in a different directory, override the make variable
109
-`GCCLUA`:
110 87
 
111
-~~~
112
-make -B hdf5 GCCLUA=${HOME}/projects/gcc-lua/gcc/gcclua.so
113
-~~~
88
+LuaRocks
89
+--------
114 90
 
115
-You may also specify the plugin directory in the environment variable
116
-`LD_LIBRARY_PATH`:
91
+HDF5 for Lua is available from [LuaRocks]:
117 92
 
118 93
 ~~~
119
-export LD_LIBRARY_PATH="${HOME}/projects/gcc-lua/gcc${LD_LIBRARY_PATH+:$LD_LIBRARY_PATH}"
94
+luarocks install --local hdf5
120 95
 ~~~
121 96
 
122
-The plugin may then be loaded by passing its filename:
97
+You may wish to remove older versions before installing:
123 98
 
124 99
 ~~~
125
-make -B hdf5 GCCLUA=gcclua.so
100
+luarocks remove --local hdf5
126 101
 ~~~
127 102
 
128
-To override the default MPI compiler `mpicc` for parallel HDF5, set `CPP`:
103
+To use the module, set the Lua module path in your shell's startup file:
129 104
 
130 105
 ~~~
131
-make -B hdf5 CPP=mpicc.mpich
106
+eval `luarocks path`
132 107
 ~~~
133 108
 
134
-To override the default GCC C compiler `gcc`, set `CC`:
109
+If the LuaRocks mirror is not available, install from the git repository above:
135 110
 
136 111
 ~~~
137
-make -B hdf5 CC=gcc-4.7
112
+luarocks install --local rockspec/hdf5-1.1.0-1.rockspec
138 113
 ~~~
139 114
 
140
-If the HDF5 header files are not installed in a system path, set `CPATH`:
115
+To install the module directly from the git source, use:
141 116
 
142 117
 ~~~
143
-export CPATH=/opt/hdf5-1.8.12/include:${HOME}/.luarocks/lib/luarocks/rocks/gcc-lua-cdecl/2.0.1-1/include
118
+luarocks make --local rockspec/hdf5-1.1.0-1.rockspec
144 119
 ~~~
145 120
 
146
-To override the name of the HDF5 library, set `LIBNAME`:
147
-
148
-~~~
149
-make -B hdf5 LIBNAME=libhdf5.so.7
150
-~~~
121
+In this case you must manually git checkout the corresponding version beforehand.
151 122
 
152
-[Lua plugin for GCC]: http://colberg.org/gcc-lua/
153
-[cdecl for GCC]: http://colberg.org/gcc-lua-cdecl/
154
-[GCC plugin directory]: http://gcc.gnu.org/onlinedocs/gccint/Plugins-loading.html
155
-[Open MPI]: http://www.open-mpi.org/
123
+[LuaRocks]: http://luarocks.org/

+ 0
- 1447
hdf5/C.lua
File diff suppressed because it is too large
View File


+ 2
- 4
hdf5/C.lua.in View File

@@ -41,10 +41,8 @@ ffi.cdef[[
41 41
 -- If the HDF5 library has been linked to the application, use HDF5
42 42
 -- symbols from default, global namespace. Otherwise, dynamically load
43 43
 -- the HDF5 library into its own, non-global C library namespace.
44
-local C
45
-if pcall(function() return ffi.C.H5open end) then
46
-  C = ffi.C
47
-else
44
+local C = ffi.C
45
+if not pcall(function() return ffi.C.H5open end) then
48 46
   C = ffi.load("]=], arg.libname, [=[")
49 47
 end
50 48
 return C

+ 17
- 7
hdf5/Makefile View File

@@ -5,24 +5,34 @@
5 5
 #
6 6
 
7 7
 CPP      := $(CC)
8
-CPPFLAGS  =
8
+CPPFLAGS  = -I$(CURDIR)/../gcc-lua-cdecl/ffi-cdecl
9 9
 ifneq (,$(findstring H5_HAVE_PARALLEL,$(shell $(CC) $(CPPFLAGS) -E -dM -include H5pubconf.h -x c /dev/null)))
10 10
   CPP     = mpicc
11 11
 endif
12 12
 CC        = gcc
13
-CFLAGS    = -Wall -pedantic -Wno-long-long
14
-GCCLUA    = gcclua
15
-LIBNAME   = hdf5
13
+CFLAGS    = -Wall
14
+LDFLAGS   = -ldl -lhdf5
15
+GCCLUA    = $(CURDIR)/../gcc-lua/gcc/gcclua.so
16
+LIBNAME   = $(firstword $(shell $(CURDIR)/hdf5) hdf5)
17
+
18
+ifdef LUA_PATH
19
+LUA_PATH := $(CURDIR)/../gcc-lua-cdecl/ffi-cdecl/?.lua;$(CURDIR)/../gcc-lua-cdecl/?.lua;$(LUA_PATH)
20
+else
21
+LUA_PATH := $(CURDIR)/../gcc-lua-cdecl/ffi-cdecl/?.lua;$(CURDIR)/../gcc-lua-cdecl/?.lua;;
22
+endif
16 23
 
17 24
 all: C.lua
18 25
 
19
-%.i: %.c
26
+C.i: C.c
20 27
 	$(CPP) -E -o $@ $(CPPFLAGS) $<
21 28
 
22
-%.lua: %.i %.lua.in
29
+C.lua: C.i C.lua.in hdf5
23 30
 	$(CC) -S -fplugin=$(GCCLUA) -fplugin-arg-gcclua-script=$@.in -fplugin-arg-gcclua-output=$@ -fplugin-arg-gcclua-libname=$(LIBNAME) $(CFLAGS) $<
24 31
 
32
+hdf5: hdf5.c
33
+	$(CPP) -fPIE -o $@ $(CFLAGS) $< $(LDFLAGS)
34
+
25 35
 clean:
26 36
 	$(RM) C.lua
27 37
 
28
-.PHONY: clean
38
+.INTERMEDIATE: C.i hdf5

+ 36
- 0
hdf5/hdf5.c View File

@@ -0,0 +1,36 @@
1
+/*
2
+ * Query soname of HDF5 library.
3
+ * Copyright © 2014 Peter Colberg.
4
+ * Distributed under the MIT license. (See accompanying file LICENSE.)
5
+ */
6
+
7
+#define _GNU_SOURCE
8
+#include <dlfcn.h>
9
+#include <stdio.h>
10
+#include <libgen.h>
11
+#include <stdlib.h>
12
+#include <string.h>
13
+
14
+#include <hdf5.h>
15
+#define sym H5open
16
+
17
+/* This program must be compiled as a position-independent executable. */
18
+
19
+#define error(fmt, ...) fprintf(stderr, "%s: "fmt"\n", argv[0], ##__VA_ARGS__)
20
+
21
+int main(int argc, const char *const *argv)
22
+{
23
+  Dl_info info;
24
+  if (dladdr(sym, &info) == 0) {
25
+    error("%s", dlerror());
26
+    return 1;
27
+  }
28
+  if (info.dli_saddr != sym) {
29
+    error("mismatching symbol '%s'", info.dli_sname);
30
+    return 1;
31
+  }
32
+  char *buf = strdup(info.dli_fname);
33
+  printf("%s\n", basename(buf));
34
+  free(buf);
35
+  return 0;
36
+}

+ 2
- 0
hdf5/init.lua View File

@@ -51,6 +51,8 @@ local location  = {}
51 51
 local object    = {}
52 52
 local plist     = {}
53 53
 
54
+-- Compare header and library version.
55
+assert(C.H5check_version(C.H5_VERS_MAJOR, C.H5_VERS_MINOR, C.H5_VERS_RELEASE) == 0)
54 56
 -- Initialise HDF5 constants.
55 57
 assert(C.H5open() == 0)
56 58
 -- Retain objects that reference a file when the file is closed.

Loading…
Cancel
Save