JAVA: Remove library source compile from JAVA packages

Previously the JAVA packages re-compile UPM library source files.  This
was a work-around for compiling JAVA-specific functionality from the UPM
source into the SWIG'ed JAVA pacakges.

This commit removes the source from the JAVA SWIG compile and provides
an example on how to add the JAVA-specific code with a swig extend call.

    * Added _upm.i file for %import (not %include)
    * Added macros to _upm.i; 1 which performs the loadLibrary, and one
      which adds the java installISR runnable.
    * Updated the src/CMakeLists.txt file to NOT build library src into
      pacakges.
    * Updated the a110x library with examples on how to use the macros.

Signed-off-by: Noel Eck <noel.eck@intel.com>
This commit is contained in:
Noel Eck 2018-01-08 11:36:05 -08:00
parent 680649ba6f
commit 6725559669
10 changed files with 52 additions and 50 deletions

View File

@ -437,16 +437,10 @@ function(upm_swig_java)
set_source_files_properties (javaupm_${libname}.i PROPERTIES CPLUSPLUS ON)
set_source_files_properties (javaupm_${libname}.i PROPERTIES SWIG_FLAGS ";-package;upm_${libname};-I${CMAKE_BINARY_DIR}/src")
# NOTE, ${module_src} is ONLY needed when swig'ing to JAVA. This is
# because *some* upm libraries have ifdef's for JAVA and SWIG in their
# source (cxx) which builds in different functionality. Because of this,
# the source must be added when building the JAVA wrapper.
# See issue #518
# TODO: Fix this somehow
if (CMAKE_VERSION VERSION_LESS "3.8")
swig_add_module (javaupm_${libname} java javaupm_${libname}.i ${module_src})
swig_add_module (javaupm_${libname} java javaupm_${libname}.i)
else ()
swig_add_library (javaupm_${libname} LANGUAGE java SOURCES javaupm_${libname}.i ${module_src})
swig_add_library (javaupm_${libname} LANGUAGE java SOURCES javaupm_${libname}.i)
endif ()
# If the C++ target depends on C++ interfaces, make the JAVA target

29
src/_upm.i Normal file
View File

@ -0,0 +1,29 @@
/* Macro for adding JAVA runnables.
example usage: JAVA_ADD_INSTALLISR(upm::A110x)
UPMClassName - UPM class name w/namespace.
*/
%define JAVA_ADD_INSTALLISR(UPMClassName)
%extend UPMClassName {
void installISR(jobject runnable)
{
$self->installISR(mraa_java_isr_callback, runnable);
}
}
%enddef
/* Macro for adding JNI loadLibrary dependency.
example usage: JAVA_JNI_LOADLIBRARY(javaupm_rhusb)
PackageName: Target UPM JAVA package.
*/
%define JAVA_JNI_LOADLIBRARY(PackageName)
%pragma(java) jniclasscode=%{
static {
try {
System.loadLibrary("PackageName");
} catch (UnsatisfiedLinkError e) {
System.err.println("Native code library (PackageName) failed to load. \n" + e);
System.exit(1);
}
}
%}
%enddef

View File

@ -57,13 +57,6 @@ bool A110X::magnetDetected()
return (!mraa_gpio_read(m_gpio) ? true : false);
}
#ifdef JAVACALLBACK
void A110X::installISR(jobject runnable)
{
installISR(mraa_java_isr_callback, runnable);
}
#endif
void A110X::installISR(void (*isr)(void *), void *arg)
{
if (m_isrInstalled)

View File

@ -85,11 +85,8 @@ namespace upm {
* @param arg Pointer to an object to be supplied as an
* argument to the ISR.
*/
#if defined(SWIGJAVA) || defined(JAVACALLBACK)
void installISR(jobject runnable);
#else
void installISR(void (*isr)(void *), void *arg);
#endif
/**
* Uninstalls the previously installed ISR
*
@ -97,9 +94,6 @@ namespace upm {
void uninstallISR();
private:
#if defined(SWIGJAVA) || defined(JAVACALLBACK)
void installISR(void (*isr)(void *), void *arg);
#endif
bool m_isrInstalled;
mraa_gpio_context m_gpio;

6
src/a110x/common.i Normal file
View File

@ -0,0 +1,6 @@
%include "../upm.i"
%{
#include "a110x.hpp"
%}
%include "a110x.hpp"

View File

@ -1,20 +1,11 @@
%module javaupm_a110x
%include "../upm.i"
%include "stdint.i"
%include "typemaps.i"
%{
#include "a110x.hpp"
%}
%include "a110x.hpp"
%include "common.i"
%pragma(java) jniclasscode=%{
static {
try {
System.loadLibrary("javaupm_a110x");
} catch (UnsatisfiedLinkError e) {
System.err.println("Native code library failed to load. \n" + e);
System.exit(1);
}
}
%}
/* Macro for loading javaupm_a110x */
JAVA_JNI_LOADLIBRARY(javaupm_a110x)
/* Macro adding an installisr call w/JAVA runnable to A110X */
JAVA_ADD_INSTALLISR(upm::A110X)

View File

@ -1,8 +1,3 @@
%module jsupm_a110x
%include "../upm.i"
%{
#include "a110x.hpp"
%}
%include "a110x.hpp"
%include "common.i"

View File

@ -1,9 +1,4 @@
// Include doxygen-generated documentation
%include "pyupm_doxy2swig.i"
%module pyupm_a110x
%include "../upm.i"
%include "a110x.hpp"
%{
#include "a110x.hpp"
%}
%include "common.i"

View File

@ -2,6 +2,9 @@
%include "stdint.i"
%include "upm_exception.i"
/* Additional swig file w/helper macros to import (but not expose) */
%import "_upm.i"
%{
#include "version.hpp"
%}

View File

@ -36,7 +36,9 @@ class AutoLoadLibrary(u.TestCase):
if not '%module' in data:
print "%%module not found in %s, skipping" % fileName
continue
if not snippet in data:
# Check for the autoload code or the autoload macro
if (not 'JAVA_JNI_LOADLIBRARY(javaupm_' in data) and \
(not snippet in data):
broken_modules.append('%s: %s' % \
(moduleName, os.path.join(subdir, fileName)))