This commit is contained in:
Vitor Santos Costa 2017-11-18 08:16:09 +00:00
parent 484213efb6
commit 9dab200c61
4 changed files with 170 additions and 63 deletions

View File

@ -1,72 +1,40 @@
include (UseSWIG)
INCLUDE_DIRECTORIES(
../../../H
../../../H/generated
../../../OPTYap
../../../include
../../../CXX
.
..
${CMAKE_CURRENT_BINARY_DIR}
${CMAKE_CURRENT_SOURCE_DIR}
)
set_property(GLOBAL
APPEND PROPERTY
COMPILE_DEFINITIONS
-Dmmap=mmap64)
# This is a CMake file for SWIG and Android
FILE( MAKE_DIRECTORY ${YAP_APP_DIR}/src/generated/java/pt/up/yap/lib )
FILE( MAKE_DIRECTORY ${YAP_APP_DIR}/src/generated/assets)
set(CMAKE_SWIG_OUTDIR ${YAP_APP_DIR}/src/generated/java/pt/up/yap/lib )
FILE( MAKE_DIRECTORY ${YAP_APP_DIR}/src/main/java/pt/up/yap/lib )
FILE( MAKE_DIRECTORY ${YAP_APP_DIR}/src/main/assets)
set(CMAKE_SWIG_OUTDIR ${YAP_APP_DIR}/src/main/java )
set(SWIG_OUTFILE_OUTDIR ${CMAKE_CURRENT_SOURCE_DIR} )
set_property(SOURCE ../yap.i PROPERTY CPLUSPLUS ON)
set_property(SOURCE streamer.i PROPERTY CPLUSPLUS ON)
set( SWIG_MODULE_NAME pt.up.yap.lib )
set ( pllib ${YAP_APP_DIR}/src/generated/assets/Yap )
set ( pllib ${YAP_APP_DIR}/src/main/assets/Yap )
set ( SWIG_SOURCES ${CMAKE_SOURCE_DIR}/packages/swig/yap.i )
SET_SOURCE_FILES_PROPERTIES(${SWIG_SOURCES} PROPERTIES CPLUSPLUS ON)
set (SWIG_SOURCES ../yap.i streamer.i streamer.cpp)
include_directories (
${CMAKE_SOURCE_DIR}/CXX
)
set( GMP_ROOT ${CMAKE_SOURCE_DIR}/../gmp/${ANDROID_ABI} )
set (GMP_INCLUDE_DIRS ${GMP_ROOT})
set (GMP_LIBRARIES ${GMP_ROOT}/libgmp.so)
add_library(YAPJava SHARED ${YAP_APP_DIR}/src/generated/jni/yap_swig.cpp)
add_library(YAPStreamer SHARED ${YAP_APP_DIR}/src/generated/jni/streamer_swig.cpp streamer.cpp)
add_custom_command (OUTPUT yap_swig.cpp
COMMAND ${CMAKE_COMMAND} -E make_directory ${pllib}
COMMAND ${CMAKE_COMMAND} -E make_directory ${pllib}/pl
COMMAND ${CMAKE_COMMAND} -E make_directory ${pllib}/os
target_link_libraries(YAPJava YAP++ android log)
COMMAND ${CMAKE_COMMAND} -E copy ${pl_library} ${pllib}
COMMAND ${CMAKE_COMMAND} -E copy ${pl_boot_library} ${pllib}/pl
COMMAND ${CMAKE_COMMAND} -E copy ${pl_os_library} ${pllib}/os
COMMAND ${SWIG_EXECUTABLE} -c++ -java -package ${SWIG_MODULE_NAME} -outdir ${CMAKE_SWIG_OUTDIR} -outcurrentdir -addextern -I${CMAKE_SOURCE_DIR}/CXX -I${CMAKE_SOURCE_DIR}/include -I${CMAKE_SOURCE_DIR}/H -I${CMAKE_SOURCE_DIR}/os -I${CMAKE_SOURCE_DIR}/OPTYap -I${CMAKE_BINARY_DIR} -I${GMP_INCLUDE_DIRS} -DX_API="" -o yap_swig.cpp ${SWIG_SOURCES}
DEPENDS ${SWIG_SOURCES} YAP++
)
add_custom_command (OUTPUT swig_streamer.cpp
COMMAND ${SWIG_EXECUTABLE} -c++ -java -package ${SWIG_MODULE_NAME} -outdir ${CMAKE_SWIG_OUTDIR} -outcurrentdir -addextern -I${CMAKE_CURRENT_SOURCE_DIR} -o swig_streamer.cpp streamer.i
DEPENDS streamer.i
)
# GMP_FOUND - true if GMP/MPIR was found
# GMP_INCLUDE_DIRS - include search path
# GMP_LIBRARIES - libraries to link with
#config.h needs this (TODO: change in code latter)
include_directories( .;${GMP_INCLUDE_DIRS};${CMAKE_SOURCE_DIR}/include;${CMAKE_SOURCE_DIR}/H;${CMAKE_SOURCE_DIR}/H/generated;${CMAKE_SOURCE_DIR}/os;${CMAKE_SOURCE_DIR}/OPTYap;${CMAKE_BINARY_DIR};${CMAKE_CURRENT_SOURCE_DIR} )
add_lib(YAPJava
yap_swig.cpp swig_streamer.cpp streamer.cpp streamer.h
)
target_link_libraries(YAPJava ${GMP_LIBRARIES} )
target_link_libraries( YAPJava YAP++ libYap android log)
if (FALSE)
set (SWIG_ADD_MODULE YAPJava SHARED CPLUPLUS ${SWIG_SOURCES} )
# Define swig module with given name and specified language
set (SWIG_LINK_LIBRARIES YAPJava YAP++ libYAP )
#- Link libraries to swig module
add_library (YAPJavaTop SHARED
main.cpp main.h
)
target_link_libraries( YAPJavaTop ${SWIG_MODULE_${YAPJava}_REAL_NAME} YAP++ libYap android)
endif()
target_link_libraries(YAPStreamer YAP++ android log)

View File

@ -0,0 +1,96 @@
//
// Created by vsc on 7/6/17.
//
/* File : example.cxx */
#include "streamer.h"
static AndroidStreamer * streamerInstance = 0;
void setStreamer(AndroidStreamer* streamer) {
streamerInstance = streamer;
}
AndroidStreamer& getStreamer() {
return *streamerInstance;
}
#include <gmpxx.h>
extern "C" {
#include <Yap.h>
#include <YapStreams.h>
#include <VFS.h>
VFS_t andstream;
static std::string buff0;
int n;
static void *
and_open(const char *name, const char *io_mode) {
if (!strcmp(name,"//android/user_output") ||
!strcmp(name,"//android/user_error"))
// we assume object is already open, so there is no need to open it.
return streamerInstance;
else
return NULL;
}
static bool
and_close(int sno) {
return true;
}
static int
and_put(int sno, int ch) {
buff0[n++] = ch;
if (ch=='\n' || n == 128) { buff0[n] = '\0'; streamerInstance->display(buff0); n = 0; }
return ch;
}
static int
and_get(int sno) {
return EOF;
}
static int64_t and_seek(int sno, int64_t where, int how) {
return EOF;
}
static void
and_flush(int sno) {
buff0[n] = '\0';
streamerInstance->display(buff0); n = 0;
}
//
// Created by vsc on 11-07-2017.
//
}
void
AndroidStreamer::bind()
{
buff0 = * new std::string[256];
n = 0;
andstream.name = "//android/user_error";
andstream.vflags = VFS_CAN_WRITE|VFS_HAS_PREFIX;
andstream.prefix = "//android/";
andstream.suffix = NULL;
andstream.open = and_open;
andstream.close = and_close;
andstream.get_char = and_get;
andstream.put_char = and_put;
andstream.flush = and_flush;
andstream.seek = and_seek;
andstream.next = GLOBAL_VFS;
GLOBAL_VFS = &andstream;
// NULL;
Yap_InitStdStream(StdOutStream, Output_Stream_f, NULL, &andstream);
Yap_InitStdStream(StdErrStream, Output_Stream_f, NULL, &andstream);
}

View File

@ -0,0 +1,27 @@
//
// Created by vsc on 7/6/17.
//
#ifndef YAPDROID_MAIN_H
#define YAPDROID_MAIN_H
#include <cstdio>
#include <iostream>
#include <string>
#include <sstream>
struct AndroidStreamer {
virtual void display(std::string text) const = 0;
virtual ~AndroidStreamer() {}
void bind();
};
void setStreamer(AndroidStreamer* streamer);
AndroidStreamer& getStreamer();
template<typename T> AndroidStreamer& operator<<(AndroidStreamer& stream, T const& val) {
std::ostringstream s;
s << val;
stream.display(s.str());
return stream;
};
#endif //YAPDROID_MAIN_H

View File

@ -0,0 +1,16 @@
/* File : example.i */
%module(directors="1") streamer
%{
#include "streamer.h"
%}
%include "std_string.i"
/* A base class for callbacks from C++ to output text on the Java side */
%feature("director") AndroidStreamer;
%include "streamer.h"
%init %{
%}