improve Android support

This commit is contained in:
Vítor Santos Costa 2014-06-11 19:31:24 +01:00
parent ab06b04a09
commit 2d2eb70842
7 changed files with 261 additions and 90 deletions

View File

@ -1,13 +1,31 @@
# File: Android.mk
#use current directory
LOCAL_PATH := $(call my-dir)
NDK_TOOLCHAIN_VERSION := 4.8
include $(CLEAR_VARS)
LOCAL_LDLIBS := -llog -landroid
LOCAL_LDLIBS += @abs_top_builddir@/libYap.a @abs_top_builddir@/yapi.o
LOCAL_LDLIBS := -landroid -llog
LOCAL_MODULE := yap0
LOCAL_SRC_FILES += @abs_top_builddir@/../../gmp/lib/libgmp.so
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := yap1
LOCAL_SRC_FILES += @abs_top_builddir@/libYap.a
include $(PREBUILT_STATIC_LIBRARY)
include $(CLEAR_VARS)
NDK_TOOLCHAIN_VERSION := @GCC_VERSION@
LOCAL_LDLIBS := -landroid -llog
LOCAL_MODULE := example
LOCAL_SRC_FILES := yap_wrap.cpp
LOCAL_C_INCLUDES := @abs_top_builddir@ @srcdir@/../../H @srcdir@/../../include @srcdir@/../../os @srcdir@/../../OPTYap @srcdir@/../../BEAM @srcdir@/../../CXX
LOCAL_CFLAGS := @DEFS@ -D_YAP_NOT_INSTALLED_=1
LOCAL_SHARED_LIBRARIES := yap0 yap1
LOCAL_STATIC_LIBRARIES := @abs_top_builddir@/libYap.ar
LOCAL_SRC_FILES += @abs_top_builddir@/packages/swig/android/jni/yap_wrap.cpp
LOCAL_C_INCLUDES := @abs_top_builddir@ @abs_top_builddir@/../../gmp/include @srcdir@/../../H @srcdir@/../../include @srcdir@/../../os @srcdir@/../../OPTYap @srcdir@/../../BEAM @srcdir@/../../CXX
LOCAL_CFLAGS := @YAP_EXTRAS@ @DEFS@ -D_YAP_NOT_INSTALLED_=1 -DDESTDIR=\"\"
LOCAL_CPP_FEATURES := rtti
# LOCAL_ALLOW_UNDEFINED_SYMBOLS := true
include $(BUILD_SHARED_LIBRARY)

View File

@ -8,8 +8,8 @@ include ../Makefile.defs
DEFS=@DEFS@ -D_YAP_NOT_INSTALLED_=1 -DDESTDIR=\"$(DESTDIR)\"
YAP_EXTRAS=@YAP_EXTRAS@
srcdir=@srcdir@
CPPFLAGS=@CPPFLAGS@ -I../.. -I$(srcdir)/../../H -I$(srcdir)/../../include -I$(srcdir)/../../os -I$(srcdir)/../../OPTYap -I$(srcdir)/../../BEAM -I$(srcdir)/../../CXX
CXXFLAGS= @SHLIB_CXXFLAGS@ $(YAP_EXTRAS) $(DEFS) $(CPPFLAGS)
CPPFLAGS= -I../.. -I$(srcdir)/../../H -I$(srcdir)/../../include -I$(srcdir)/../../os -I$(srcdir)/../../OPTYap -I$(srcdir)/../../BEAM -I$(srcdir)/../../CXX
CXXFLAGS= $(YAP_EXTRAS) $(DEFS) $(CPPFLAGS)
PROGRAMS= $(srcdir)/python/demo.py \
@ -26,7 +26,7 @@ python/_yap.so: python/yap_wrap.o
python/yap_wrap.c: $(srcdir)/yap.i
mkdir -p python
$(SWIG) -c++ -python -outdir python -o $@ $(DEFS) $(CPPFLAGS) -Wall $<
$(SWIG) -c++ -python -outdir python -o $@ $(CXXFLAGS) -Wall $<
python/yap_wrap.o: python/yap_wrap.c
$(CXX) -c $(CXXFLAGS) @PYTHON_INCLUDES@ $< -o $@
@ -35,10 +35,10 @@ java: jni/libyap.@SO@ java/yap.java
cd java ; $(JAVAC) *.java; $(JAR) cvf yap.jar *.class
jni/libyap.@SO@: jni/yap_wrap.o
$(CXX) -shared $(LDSOFLAGS) -L ../.. -lYap -o $@ ../../yapi.o $< $(LIBS) @JPLLDFLAGS@ -L ../.. -lYap -lpthread
$(CXX) -shared $(LDSOFLAGS) -o $@ ../../yapi.o $< $(LIBS) @JPLLDFLAGS@ -L ../.. -lYap -lpthread
jni/yap_wrap.cpp: $(srcdir)/yap.i
$(SWIG) -c++ -java -package pt.up.fc.dcc.yap -outdir java -o $@ $(DEFS) $(CPPFLAGS) -Wall $<
$(SWIG) -c++ -java -package pt.up.fc.dcc.yap -outdir java -o $@ $(CXXFLAGS) -Wall $<
jni/yap_wrap.o: jni/yap_wrap.cpp
$(CXX) -c $(CXXFLAGS) @JPLCFLAGS@ $< -o $@
@ -51,16 +51,18 @@ jni/yap_wrap.o: jni/yap_wrap.cpp
# adb install bin/SwigSimple-debug.apk
#
#
android: android/jni/yap_wrap.cpp
android: android/jni/yap_wrap.cpp
android/jni/yap_wrap.cpp: $(srcdir)/yap.i
android create project --target 1 --name SwigSimple --path ./android --activity SwigSimple --package org.swig.simple
( cd android ; mkdir -p jni; mkdir -p assets/share/Yap; mkdir -p assets/lib/Yap;\
$(SWIG) -c++ -java -I$(srcdir)/../../CXX -package org.swig.simple -outdir src/org/swig/simple -o jni/yap_wrap.cpp $< ;\
android create project --target 1 --name SwigSimple --path ./android --activity SwigSimple --package org.swig.simple;\
( cd android ; mkdir -p jni jni/lib; mkdir -p assets/share/Yap; mkdir -p assets/lib/Yap;\
$(SWIG) -c++ -java -I$(srcdir)/../../CXX -package org.swig.simple -outdir src/org/swig/simple -o jni/yap_wrap.cpp $(CXXFLAGS) $< ;\
cp $(srcdir)/android/SwigSimple.java src/org/swig/simple/ ;\
cp $(srcdir)/android/main.xml res/layout ;\
cp $(srcdir)/android/Application.mk jni ;\
cp -r $(DESTDIR)$(PLLIBDIR)/* assets/share/Yap;\
cp -r $(DESTDIR)$(SOLIBDIR)/* assets/lib/Yap;\
cp -r ../../../startup.yss assets/lib/Yap;\
cp -r $(DESTDIR)$(SOLIBDIR)/*.so jni;\
cp ../Android.mk jni ;\
ndk-build; \
ant debug \
@ -75,7 +77,7 @@ install:
clean:
rm -f */*
rm -fr android/* jni/* java/* python/*
distclean: clean
rm -f *def Makefile

View File

@ -1,2 +1,4 @@
# File: Application.mk
NDK_TOOLCHAIN_VERSION = 4.8
APP_STL := gnustl_static
APP_CPPFLAGS += -fexceptions

View File

@ -11,79 +11,184 @@ import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.util.Log;
import android.content.res.AssetManager;
import android.widget.EditText;
public class SwigSimple extends Activity
{
TextView outputText = null;
ScrollView scroller = null;
TextView outputText = null;
ScrollView scroller = null;
YAPEngine eng = null;
EditText text;
String str;
String buf;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
String s = "ugh";
setContentView(R.layout.main);
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
String s = null;
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try {
PackageManager m = getPackageManager();
s = getPackageName();
PackageInfo p = m.getPackageInfo(s, 0);
s = p.applicationInfo.dataDir;
mgr = getResources().getAssets();
load(mgr);
} catch(NameNotFoundException e) {
Log.e(TAG, "Couldn't find package information in PackageManager", e);
}
outputText = (TextView)findViewById(R.id.OutputText);
outputText.setText("Application " + s + "\nPress 'Run' to start...\n");
outputText.setMovementMethod(new ScrollingMovementMethod());
YAPParams p = new YAPParams();
eng = new YAPEngine( p );
scroller = (ScrollView)findViewById(R.id.Scroller);
}
try {
PackageManager m = getPackageManager();
s = getPackageName();
PackageInfo p = m.getPackageInfo(s, 0);
//s = p.applicationInfo.dataDir;
AssetManager mgr = getResources().getAssets();
load(mgr);
} catch(NameNotFoundException e) {
Log.e(TAG, "Couldn't find package information in PackageManager", e);
}
Log.i(TAG, "mgr=" +mgr);
public void onRunButtonClick(View view)
{
outputText.append("Started...\n");
if (BuildConfig.DEBUG) {
Log.i(TAG, "onRunButtonClick called");
}
outputText.append("Finished!\n");
text = (EditText)findViewById(R.id.EditText01);
outputText = (TextView)findViewById(R.id.OutputText);
outputText.setText("Application " + s + "\nPress 'Run' to start...\n");
outputText.setMovementMethod(new ScrollingMovementMethod());
scroller = (ScrollView)findViewById(R.id.Scroller);
eng = new YAPEngine( );
Log.i(TAG, "engine done");
JavaCallback callback = new JavaCallback( outputText );
// set the Java Callback
eng.setYAPCallback(callback);
if (BuildConfig.DEBUG) {
Log.i(TAG, "callback done");
}
}
// Ensure scroll to end of text
scroller.post(new Runnable() {
public void run() {
scroller.fullScroll(ScrollView.FOCUS_DOWN);
String s = "['/assets/share/Yap/lists'].\n";
outputText.append(s);
YAPQuery q = eng.query(s);
q.next();
String s = "member(X, [1,2,3]).\n";
outputText.append(s);
YAPQuery q = eng.query(s);
q.next();
String sn =
}
});
}
public void onResetButtonClick(View view)
{
if (BuildConfig.DEBUG) {
Log.i(TAG, "onReseButtonClick called");
}
// Ensure scroll to end of text
scroller.post(new Runnable() {
public void run() {
scroller.fullScroll(ScrollView.FOCUS_DOWN);
text.setText("");
}
});
}
public void onRunButtonClick(View view)
{
if (BuildConfig.DEBUG) {
Log.i(TAG, "onRunButtonClick called");
}
// Ensure scroll to end of text
scroller.post(new Runnable() {
public void run() {
scroller.fullScroll(ScrollView.FOCUS_DOWN);
str = text.getText().toString();
outputText.append("?- " + str);
YAPQuery q = eng.query( str );
YAPListTerm vs = q.namedVars();
// text.setText("");
if (vs.nil()) {
if (q.next()) {
outputText.append( "yes\n" );
} else {
outputText.append( "no\n" );
}
} else {
while (q.next()) {
int i=1;
// outputText.append(Integer.toString(i++) + ": " + vs.text() +"\n");
while(!vs.nil()){
YAPTerm eq = vs.car();
//outputText.append(Integer.toString(i) + ": " + eq.text() );
outputText.append(Integer.toString(i++) + ":\t" + eq.getArg(1).text() + " = " + eq.getArg(2).text() +"\n" );
vs = vs.cdr();
}
}
}
q.close();
}
});
}
public void onRunSelectionButtonClick(View view)
{
if (BuildConfig.DEBUG) {
Log.i(TAG, "onRunButtonClick called");
}
// Ensure scroll to end of text
scroller.post(new Runnable() {
public void run() {
scroller.fullScroll(ScrollView.FOCUS_DOWN);
int startSelection = text.getSelectionStart();
int endSelection = text.getSelectionEnd();
str = text.getText().toString().substring( startSelection, endSelection );
outputText.append("?- " + str);
YAPQuery q = eng.query( str );
YAPListTerm vs = q.namedVars();
// text.setText("");
if (vs.nil()) {
if (q.next()) {
outputText.append( "yes\n" );
} else {
outputText.append( "no\n" );
}
} else {
while (q.next()) {
int i=1;
// outputText.append(Integer.toString(i++) + ": " + vs.text() +"\n");
while(!vs.nil()){
YAPTerm eq = vs.car();
//outputText.append(Integer.toString(i) + ": " + eq.text() );
outputText.append(Integer.toString(i++) + ":\t" + eq.getArg(1).text() + " = " + eq.getArg(2).text() +"\n" );
vs = vs.cdr();
}
}
}
q.close();
}
});
}
/** static constructor */
static {
System.loadLibrary("android");
System.loadLibrary("log");
System.loadLibrary("example");
}
private static native void load(AssetManager mgr);
/** static constructor */
static {
System.loadLibrary("android");
System.loadLibrary("log");
System.loadLibrary("gmp");
System.loadLibrary("example");
}
private AssetManager mgr;
private static native void load(AssetManager mgr);
private static final String TAG = "SwigSimple";
private AssetManager mgr;
private static final String TAG = "SwigSimple";
}
class JavaCallback extends YAPCallback
{
TextView output;
public JavaCallback( TextView outputText )
{
super();
output = outputText;
Log.i(TAG, "java callback init");
}
public void run()
{
System.out.println("JavaCallback.run() ");
Log.i(TAG, "java callback ");
}
public void displayInWindow(String s)
{
System.out.println("JavaCallback.run() ");
Log.i(TAG, "java callback ");
output.append(s);
}
private static final String TAG = "JavaCallback";
}

View File

@ -1,9 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/EditText01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:lines="6" />
<Button
android:id="@+id/RunButton"
android:layout_width="wrap_content"
@ -11,16 +21,32 @@
android:text="Run..."
android:onClick="onRunButtonClick"
/>
<Button
android:id="@+id/RunSelectionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select"
android:onClick="onRunSelectionButtonClick"
/>
<Button
android:id="@+id/ClearButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Reset"
android:onClick="onClearButtonClick"
/>
</LinearLayout>
<ScrollView
android:id="@+id/Scroller"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_height="fill_parent">
<TextView
android:id="@+id/OutputText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<!-- Set MaxLegth EditText -->
</ScrollView>
</LinearLayout>

View File

@ -14,7 +14,6 @@ def go():
vs = q.namedVars();
while vs.length() > 0:
eq = vs.car()
print eq.text()
print eq.getArg(1).text() + " = " + eq.getArg(2).text()
vs = vs.cdr()
print "no more answers"
print "no more answers"

View File

@ -1,12 +1,31 @@
/* example.i */
%module yap
%module(directors="1") yap
%{
/* Put header files here or function declarations like below */
#define YAP_CPP_INTERFACE 1
extern "C" {
#include "yapi.hh"
extern Term Yap_StringToTerm(const char *s, size_t len, term_t bindings);
#if THREADS
#define Yap_regp regcache
#endif
// we cannot consult YapInterface.h, that conflicts with what we declare, though
// it shouldn't
}
%}
/* turn on director wrapping Callback */
%feature("director") YAPCallback;
%include "yapi.hh"