Skip to content

Commit

Permalink
SWIG stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
bmcdorman committed Feb 16, 2015
1 parent fd58a16 commit 478effd
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 64 deletions.
54 changes: 42 additions & 12 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,9 @@ file(GLOB SWIG_SOURCES ${BINDINGS}/*.i)
if(build_python)
find_package(SWIG REQUIRED)
find_package(PythonLibs REQUIRED)
list(APPEND BINDINGS_SOURCES ${CMAKE_BINARY_DIR}/kovan_wrap_py.cxx)
list(APPEND BINDINGS_LIBRARIES ${PYTHON_LIBRARY})
message("${PYTHON_LIBRARY}")
set(PYTHON_BINDING_SOURCES ${CMAKE_BINARY_DIR}/kovan_wrap_py.cxx)
set(PYTHON_BINDING_LIBRARIES ${PYTHON_LIBRARY})
include_directories(${PYTHON_INCLUDE_DIR})
add_custom_command(COMMAND ${SWIG_EXECUTABLE} -Wall -python -c++ -I${INCLUDE} -outdir ${LIBRARY_OUTPUT_PATH} -o ${CMAKE_BINARY_DIR}/kovan_wrap_py.cxx ${BINDINGS}/kovan.i
DEPENDS ${SWIG_SOURCES}
Expand All @@ -179,30 +180,59 @@ option(build_java "Build java bindings" ON)
if(build_java)
find_package(SWIG REQUIRED)
find_package(JNI REQUIRED)
list(APPEND BINDINGS_SOURCES ${CMAKE_BINARY_DIR}/kovan_wrap_java.cxx)
list(APPEND BINDINGS_LIBRARIES ${JNI_LIBRARIES})
find_package(Java REQUIRED)
set(JAVA_BINDING_SOURCES ${CMAKE_BINARY_DIR}/kovan_wrap_java.cxx)
if(APPLE)
# This is a hack!
# By default, JNI_LIBRARIES contains libjawt and libjvm. Lib jawt pulls in libawt_lwawt.dylib,
# which crashes on load into the JVM. To avoid this, we just use libjvm.
set(JAVA_BINDING_LIBRARIES /Library/Java/JavaVirtualMachines/jdk1.8.0_20.jdk/Contents/Home/jre/lib/server/libjvm.dylib)
else()
set(JAVA_BINDING_LIBRARIES ${JNI_LIBRARIES})
endif()
include_directories(${JAVA_INCLUDE_PATH} ${JAVA_INCLUDE_PATH2} ${BINDINGS})
set(JAVA_OUT_PATH ${LIBRARY_OUTPUT_PATH}/java/org/kipr/kovan)
set(JAVA_BASE_PATH ${CMAKE_BINARY_DIR}/java)
set(JAVA_EXTRAS_PATH ${BINDINGS}/java/org/kipr/kovan)
set(JAVA_OUT_PATH ${JAVA_BASE_PATH}/org/kipr/kovan)
set(JAVA_BIN_PATH ${CMAKE_BINARY_DIR}/java_bin)
file(REMOVE_RECURSE ${JAVA_OUT_PATH})
file(MAKE_DIRECTORY ${JAVA_OUT_PATH})
add_custom_command(COMMAND ${SWIG_EXECUTABLE} -Wall -java -package org.kipr.kovan -c++ -I${INCLUDE} -outdir ${JAVA_OUT_PATH} -o ${CMAKE_BINARY_DIR}/kovan_wrap_java.cxx ${BINDINGS}/kovan.i
DEPENDS ${SWIG_SOURCES}
OUTPUT ${CMAKE_BINARY_DIR}/kovan_wrap_java.cxx)
file(REMOVE_RECURSE ${JAVA_BIN_PATH})
file(MAKE_DIRECTORY ${JAVA_BIN_PATH})
add_custom_target(swig_java
COMMAND ${SWIG_EXECUTABLE} -Wall -java -package org.kipr.kovan -c++ -outdir ${JAVA_OUT_PATH} -o ${CMAKE_BINARY_DIR}/kovan_wrap_java.cxx ${BINDINGS}/kovan.i
DEPENDS ${SWIG_SOURCES})
add_custom_target(java_compile
COMMAND ${Java_JAVAC_EXECUTABLE} -d ${JAVA_BIN_PATH} -cp ${JAVA_BIN_PATH} ${JAVA_OUT_PATH}/*.java ${JAVA_EXTRAS_PATH}/*.java
DEPENDS swig_java)
add_custom_target(kovan_jar ALL
COMMAND ${Java_JAR_EXECUTABLE} cf ${LIBRARY_OUTPUT_PATH}/libkovan.jar -C ${JAVA_BIN_PATH}/ .
DEPENDS swig_java java_compile)
endif()

add_library(kovan SHARED ${VI_SOURCES} ${VI_P_INCLUDES} ${OBJC_SOURCES} ${VF_INCLUDES} ${PIXELTOASTER_SOURCES})


if(build_python)
add_library(kovan_python SHARED ${PYTHON_BINDING_SOURCES})
set_target_properties(kovan_python PROPERTIES PREFIX _ OUTPUT_NAME kovan SUFFIX .so)
target_link_libraries(kovan_python kovan ${PYTHON_BINDING_LIBRARIES})
endif()

add_library(kovan SHARED ${VI_SOURCES} ${VI_P_INCLUDES} ${OBJC_SOURCES} ${VF_INCLUDES} ${PIXELTOASTER_SOURCES} ${BINDINGS_SOURCES})
if(build_java)
add_library(kovan_java SHARED ${JAVA_BINDING_SOURCES})
target_link_libraries(kovan_java kovan ${JAVA_BINDING_LIBRARIES})
endif()

if(WIN32)
# TODO: Make sure these are current
target_link_libraries(kovan opencv_core2410 opencv_highgui2410 opencv_imgproc2410 libzbar-0 OpenNI2)
target_link_libraries(kovan d3d9)
target_link_libraries(kovan ws2_32)
else()
target_link_libraries(kovan pthread opencv_core opencv_highgui opencv_imgproc zbar avcodec avformat avutil z swscale bz2 OpenNI2)
target_link_libraries(kovan pthread opencv_core opencv_videoio opencv_imgproc zbar avcodec avformat avutil z swscale bz2 OpenNI2)
endif()

target_link_libraries(kovan ${BINDINGS_LIBRARIES})

if(APPLE)
find_library(OPENGL_LIBRARY OpenGL)
find_library(COCOA_LIBRARY Cocoa)
Expand Down
4 changes: 3 additions & 1 deletion bindings/java/org/kipr/kovan/Buttons.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
package org.kipr.kovan;

public class Buttons
{
public static IdButton A = kovan.getA();
Expand All @@ -7,4 +9,4 @@ public class Buttons
public static IdButton Y = kovan.getY();
public static IdButton Z = kovan.getZ();
public static AbstractButton SIDE = kovan.getSide();
}
}
11 changes: 11 additions & 0 deletions bindings/java/org/kipr/kovan/Loader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.kipr.kovan;

public class Loader {
static {
try {
System.loadLibrary("kovan");
} catch (final UnsatisfiedLinkError e) {
System.err.println("Failed to load kovan native library.\n" + e);
}
}
}
98 changes: 51 additions & 47 deletions bindings/kovan.i
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
%module kovan
%{
#include "kovan/kovan.hpp"
#include "kovan/kovan.h"
#include "kovan/segment.hpp"
#include "kovan/colinear_segmenter.hpp"
#include "kovan/xtion_depth_driver.hpp"
#include "kovan.hpp"
#include "kovan.h"
#include "segment.hpp"
#include "colinear_segmenter.hpp"
#include "xtion_depth_driver.hpp"

#include "motor.hpp"

Expand All @@ -23,60 +23,64 @@
#define open _open
#endif

%include "vtable.h"

%include "button_prelude.i"
%include "camera_prelude.i"

%include "kovan/sensor.hpp"
%include "sensor.hpp"
%template(IntSensor) Sensor<int>;
%template(ShortSensor) Sensor<short>;
%template(UnsignedShortSensor) Sensor<unsigned short>;
%template(BoolSensor) Sensor<bool>;
%include "kovan/motors.hpp"
%include "kovan/geom.hpp"
%include "kovan/port.hpp"
%include "kovan/segment.hpp"
%include "motors.hpp"
%include "geom.hpp"
%include "port.hpp"
%include "segment.hpp"
%template(IntSegmenter) Segmenter<int>;
%include "kovan/depth_driver.hpp"
%include "kovan/depth_image.hpp"
%include "kovan/servo.hpp"
%include "kovan/util.hpp"
%include "kovan/xtion_depth_driver.hpp"
%include "kovan/create.hpp"
%include "kovan/color.hpp"
%include "kovan/battery.hpp"
%include "kovan/analog.hpp"
%include "kovan/accel.hpp"
%include "kovan/camera.hpp"
%include "kovan/button.hpp"
%include "kovan/ardrone.hpp"
%include "kovan/config.hpp"
%include "kovan/datalog.hpp"
%include "kovan/colinear_segmenter.hpp"
%include "kovan/digital.hpp"
%include "kovan/graphics.h"
%include "depth_driver.hpp"
%include "depth_image.hpp"
%include "servo.hpp"
%include "util.hpp"
%include "xtion_depth_driver.hpp"
%include "create.hpp"
%include "color.hpp"
%include "battery.hpp"
%include "analog.hpp"
%include "accel.hpp"
%include "camera.hpp"
%include "button.hpp"
%include "ardrone.hpp"
%include "config.hpp"
%include "datalog.hpp"
%include "colinear_segmenter.hpp"
%include "digital.hpp"
%include "graphics.h"

#ifndef SWIGJAVA
%include "kovan/thread.hpp"

%include "kovan/motors.h"
%include "kovan/geom.h"
%include "kovan/servo.h"
%include "kovan/util.h"
%include "kovan/create.h"
%include "kovan/depth.h"
%include "kovan/battery.h"
%include "kovan/analog.h"
%include "kovan/accel.h"
%include "kovan/camera.h"
%include "kovan/button.h"
%include "kovan/ardrone.h"
%include "kovan/datalog.h"
%include "kovan/console.h"
%include "kovan/audio.h"
%include "kovan/thread.h"
%include "kovan/digital.h"
%include "thread.hpp"
#endif

%include "motors.h"
%include "geom.h"
%include "graphics.h"
%include "servo.h"
%include "util.h"
%include "create.h"
%include "depth.h"
%include "battery.h"
%include "analog.h"
%include "general.h"
%include "accel.h"
%include "camera.h"
%include "button.h"
%include "ardrone.h"
%include "datalog.h"
%include "console.h"
%include "audio.h"
%include "thread.h"
%include "digital.h"

%include "camera.i"
%include "create.i"
%include "motor.i"
2 changes: 2 additions & 0 deletions include/kovan/depth_driver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ namespace depth
* image available yet.
* \return DepthImage object
*/
#ifndef SWIG
virtual DepthImage *depthImage() const = 0;
#endif
};
}

Expand Down
18 changes: 14 additions & 4 deletions include/kovan/vtable.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,29 @@ extern "C" {
#define POSSIBLY_UNUSED
#endif

#ifndef SWIG
#define VTABLE_FUNC_VOID(name, signature, args) \
typedef void (*name##_func) signature; \
EXPORT_SYM extern name##_func g_##name##_func; \
EXPORT_SYM extern const name##_func g_##name##_func_default; \
EXPORT_SYM extern name##_func g_##name##_func; \
EXPORT_SYM extern const name##_func g_##name##_func_default; \
static const char *const name##_signature = "void" #signature; \
POSSIBLY_UNUSED static void name signature { (*g_##name##_func) args; }

#define VTABLE_FUNC(name, returnType, signature, args) \
typedef returnType (*name##_func) signature; \
EXPORT_SYM extern name##_func g_##name##_func; \
EXPORT_SYM extern const name##_func g_##name##_func_default; \
EXPORT_SYM extern name##_func g_##name##_func; \
EXPORT_SYM extern const name##_func g_##name##_func_default; \
static const char *const name##_signature = #returnType #signature; \
POSSIBLY_UNUSED static returnType name signature { return (*g_##name##_func) args; }
#else
#define VTABLE_FUNC_VOID(name, signature, args) \
typedef void (*name##_func) signature; \
static void name signature;

#define VTABLE_FUNC(name, returnType, signature, args) \
typedef returnType (*name##_func) signature; \
static returnType name signature;
#endif

#define VTABLE_SET_DEFAULT(name, impl) \
const name##_func g_##name##_func_default = &impl; \
Expand Down
Binary file added useful/SWIGDocumentation.pdf
Binary file not shown.

0 comments on commit 478effd

Please sign in to comment.