Added python bindings
authorTarek Heiland <tarek@illimitable.com>
Tue, 7 Apr 2009 17:07:56 +0000 (19:07 +0200)
committerThomas Jarosch <thomas.jarosch@intra2net.com>
Tue, 7 Apr 2009 17:07:56 +0000 (19:07 +0200)
ChangeLog
Makefile.am
bindings/Makefile.am [new file with mode: 0644]
bindings/ftdi.i [new file with mode: 0644]
bindings/python/Makefile.am [new file with mode: 0644]
bindings/python/setup.py.in [new file with mode: 0644]
configure.in

index 00dfa0e..86dda1f 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -3,7 +3,7 @@ New in 0.16 - 2009-XX-XX
 * Relicensed C++ wrapper under GPLv2 + linking exception (Marek VavruĊĦa and Intra2net)
 * Support for FT2232H and FT4232H (David Challis and Intra2net)
 * Support for mingw cross compile (Uwe Bonnes)
-* Minor autoconf cleanup (Tarek Heiland)
+* Python bindings and minor autoconf cleanup (Tarek Heiland)
 * Code cleanup in various places (Intra2net)
 * Fixed ftdi_read_chipid in some cases (Matthias Richter)
 * eeprom decode function and small cleanups (Marius Kintel)
index 5d5e4c1..f764b35 100644 (file)
@@ -2,7 +2,7 @@
 # have all needed files, that a GNU package needs
 AUTOMAKE_OPTIONS = foreign 1.4
 
-SUBDIRS = src $(LIBFTDI_MODULES) examples doc
+SUBDIRS = src bindings $(LIBFTDI_MODULES) examples doc
 
 EXTRA_DIST = libftdi.spec libftdi.spec.in COPYING.LIB COPYING.GPL LICENSE README AUTHORS ChangeLog libftdi-config.in CMakeLists.txt packages
 
diff --git a/bindings/Makefile.am b/bindings/Makefile.am
new file mode 100644 (file)
index 0000000..8405a12
--- /dev/null
@@ -0,0 +1,4 @@
+SUBDIRS =      python
+EXTRA_DIST =   ftdi.i
+
diff --git a/bindings/ftdi.i b/bindings/ftdi.i
new file mode 100644 (file)
index 0000000..ce24b58
--- /dev/null
@@ -0,0 +1,47 @@
+/* File: example.i */
+%module ftdi
+%include "typemaps.i"
+%include "cpointer.i"
+%typemap(in) unsigned char* = char*;
+%ignore ftdi_write_data_async;
+%ignore ftdi_async_complete;
+
+%include ftdi.h
+%{
+#include <ftdi.h>
+%}
+
+extern "C" {
+
+%apply char *OUTPUT { unsigned char *buf };
+    int ftdi_read_data(struct ftdi_context *ftdi, unsigned char *buf, int size);
+%clear unsigned char *buf;
+%apply int *OUTPUT { unsigned int *chunksize };
+    int ftdi_read_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize);
+    int ftdi_write_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize);
+%clear unsigned int *chunksize;
+    //int ftdi_write_data_async(struct ftdi_context *ftdi, unsigned char *buf, int size);
+    //void ftdi_async_complete(struct ftdi_context *ftdi, int wait_for_more);
+%apply char *OUTPUT { unsigned char *pins };
+    int ftdi_read_pins(struct ftdi_context *ftdi, unsigned char *pins);
+%clear unsigned char *pins;
+%apply char *OUTPUT { unsigned char *latency };
+    int ftdi_get_latency_timer(struct ftdi_context *ftdi, unsigned char *latency);
+%clear unsigned char *status;
+%apply char *OUTPUT { unsigned char *latency };
+    int ftdi_poll_modem_status(struct ftdi_context *ftdi, unsigned short *status);
+%clear unsigned char *status;
+%apply char *OUTPUT { unsigned char *output };
+    int  ftdi_eeprom_build(struct ftdi_eeprom *eeprom, unsigned char *output);
+%clear unsigned char *output;
+%apply char *OUTPUT { unsigned char *eeprom };
+    int ftdi_read_eeprom(struct ftdi_context *ftdi, unsigned char *eeprom);
+    int ftdi_write_eeprom(struct ftdi_context *ftdi, unsigned char *eeprom);
+%clear unsigned char *eeprom;
+%apply int *OUTPUT { unsigned int *chipid };
+    int ftdi_read_chipid(struct ftdi_context *ftdi, unsigned int *chipid);
+%clear unsigned int *chipid;
+
+}
+
+
diff --git a/bindings/python/Makefile.am b/bindings/python/Makefile.am
new file mode 100644 (file)
index 0000000..2b47b6a
--- /dev/null
@@ -0,0 +1,61 @@
+
+## Process this file with automake to produce Makefile.in
+
+if HAVE_SWIG
+if HAVE_PYTHON
+if ENABLE_PYTHON_BINDING
+
+LIBFTDI_INTERFACE =    $(top_srcdir)/src/ftdi.h 
+SWIG_INTERFACE =       ../ftdi.i
+
+BUILT_SOURCES =                ftdi_wrap.c ftdi.py
+CLEANFILES =           ftdi_wrap.c ftdi.py
+
+all-local:     ftdi_wrap.c ftdi.py
+       @case "`uname`" in \
+         MINGW*) \
+               $(PYTHON) setup.py build --compiler=mingw32 \
+         ;; \
+         *) \
+               $(PYTHON) setup.py build \
+         ;; \
+       esac;
+
+# python on MinGW/MSYS requires pure Windows style paths
+# Using following (cd $dir && pwd -W) hack to get a nearly
+# native Windows path, then translating the forward slash
+# to a backward slash to make python distutils jolly.
+# P.S. the backslash has to be escaped thrice, hence a single
+# backslash turns into a sequence of eight (neat, huh?)
+#
+install-exec-local:
+       @case "`uname`" in \
+         MINGW*) \
+               NATIVE_WINDOWS_PREFIX="`cd $(DESTDIR)$(prefix) && pwd -W`"; \
+               NATIVE_WINDOWS_PREFIX="`echo $$NATIVE_WINDOWS_PREFIX | sed -e 's|/|\\\\\\\\|g'`"; \
+               $(PYTHON) setup.py install --prefix="$$NATIVE_WINDOWS_PREFIX" \
+         ;; \
+         *) \
+               $(PYTHON) setup.py install --prefix=$(DESTDIR)$(prefix) \
+         ;; \
+       esac;
+
+# This rule cleans up stuff installed by Python's setup.py
+# Unfortunately, Python's distutils do not provide an uninstall
+# command, so we have to make up for it here in uninstall-local
+# hook. This might break if distutils' behaviour changes as automake
+# has no control over what distutils install command does.
+#
+uninstall-local:
+       rm -rf $(DESTDIR)$(libdir)/python*/site-packages/*ftdi*
+
+clean-local:
+       $(PYTHON) setup.py clean --all
+
+ftdi_wrap.c ftdi.py:   $(SWIG_INTERFACE) $(LIBFTDI_INTERFACE)
+       $(SWIG) -python -I$(top_srcdir)/src -o ftdi_wrap.c -outdir . $(srcdir)/$(SWIG_INTERFACE)
+
+
+endif # ENABLE_PYTHON_BINDING
+endif # HAVE_PYTHON
+endif # HAVE_SWIG
diff --git a/bindings/python/setup.py.in b/bindings/python/setup.py.in
new file mode 100644 (file)
index 0000000..6140858
--- /dev/null
@@ -0,0 +1,17 @@
+# Process this file with configure to produce setup.py
+
+from distutils.core import setup, Extension
+setup(name='@PACKAGE_NAME@',
+      version='@PACKAGE_VERSION@',
+      description='libftdi Python binding',
+      author='Tarek Heiland',
+      author_email='@PACKAGE_BUGREPORT@',
+      maintainer='',
+      maintainer_email='@PACKAGE_BUGREPORT@',
+      url='',
+      py_modules=['ftdi'],
+      ext_modules=[Extension('_ftdi', ['ftdi_wrap.c'],
+                             include_dirs=['@top_srcdir@/src'],
+                             library_dirs=['@top_srcdir@/src/.libs','lib','/usr/lib'],
+                             libraries=['ftdi'])],
+      )
index d44f38d..4840777 100644 (file)
@@ -84,11 +84,64 @@ dnl check for doxygen
 AC_PATH_PROG(DOXYGEN, doxygen)
 AM_CONDITIONAL(HAVE_DOXYGEN, test -n $DOXYGEN)
 
+dnl ============================
+dnl Bindings for other languages
+dnl ==
+
+dnl ===============
+dnl Checks for SWIG
+dnl ===============
+AC_PATH_PROG([SWIG], [swig])
+AM_CONDITIONAL(HAVE_SWIG, test "$SWIG")
+
+dnl =================
+dnl Checks for Python
+dnl =================
+AM_PATH_PYTHON([2.0],
+               [],
+               [AC_MSG_WARN([Python not found. Python is required to build presage python binding. Python can be obtained from http://www.pyth
+on.org])])
+if test "$PYTHON"
+then
+     python_include_path=`$PYTHON -c "import distutils.sysconfig; print distutils.sysconfig.get_python_inc();"`
+     AC_CHECK_HEADERS([${python_include_path}/Python.h],
+                      [have_python_header=true],
+                      [AC_MSG_WARN([Python.h header file not found. Python development files are required to build presage python binding. Pyt
+hon can be obtained from http://www.python.org])],
+                      [])
+fi
+AM_CONDITIONAL(HAVE_PYTHON, test "$PYTHON" -a "x$have_python_header" = "xtrue")
+
+AC_ARG_ENABLE([python-binding],
+               AS_HELP_STRING([--enable-python-binding],
+                              [build python binding (default=no)]),
+               [ac_enable_python_binding=$enableval],
+               [ac_enable_python_binding=no])
+if test "x$ac_enable_python_binding" = "xyes"
+then
+    if test ! "$SWIG" -o ! "$PYTHON" -o ! "x$have_python_header" = "xtrue"
+    then
+        AC_MSG_WARN([Python binding for libftdi cannot be built. Ensure that SWIG and Python packages are available.])
+    fi
+else
+    AC_MSG_NOTICE([Python binding for libftdi will not be built.])
+    AC_MSG_NOTICE([Enable Python binding module building with --enable-python-binding])
+fi
+AM_CONDITIONAL(ENABLE_PYTHON_BINDING, test "x$ac_enable_python_binding" = "xyes")
+
+if test "$SWIG" -a "$PYTHON" -a "x$have_python_header" = "xtrue" -a "x$ac_enable_python_binding" = "xyes"
+then
+    AC_MSG_NOTICE([Python binding for libftdi will be built.])
+    build_python_binding="yes"
+else
+    build_python_binding="no"
+fi
+
 AC_SUBST(LIBFTDI_MODULES)
 AC_SUBST(LIBFTDI_MODULES_PKGCONFIG)
 
 AC_OUTPUT([libftdi-config],[chmod a+x libftdi-config])
-AC_OUTPUT(Makefile src/Makefile examples/Makefile doc/Doxyfile doc/Makefile libftdi.pc libftdi.spec)
+AC_OUTPUT(Makefile src/Makefile bindings/Makefile bindings/python/Makefile bindings/python/setup.py examples/Makefile doc/Doxyfile doc/Makefile libftdi.pc libftdi.spec)
 
 if test "x$ENABLE_LIBFTDIPP" = "x1"; then
     AC_OUTPUT(ftdipp/Makefile libftdipp.pc)