From: Marcus Comstedt Date: Fri, 18 Jul 2014 14:15:43 +0000 (+0200) Subject: Build a library with the C++ part of environment setup X-Git-Url: http://sigrok.org/gitweb/?p=sigrok-androidutils.git;a=commitdiff_plain;h=2b68989afe2dcb949c8ff7864d0d7594bd875284;ds=sidebyside Build a library with the C++ part of environment setup --- diff --git a/.gitignore b/.gitignore index 0f6cc25..80a7b48 100644 --- a/.gitignore +++ b/.gitignore @@ -1,11 +1,22 @@ -/Makefile -/Makefile.in +Makefile +Makefile.in /aclocal.m4 /autom4te.cache/ +/config.guess /config.log /config.status +/config.sub /configure +/depcomp /install-sh +/libtool +/ltmain.sh /missing *.jar *.class +*.o +*.lo +*.la +.deps/ +.libs/ +/lib/libsigrokandroidutils.pc diff --git a/Makefile.am b/Makefile.am index 0524640..db58d73 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1,4 +1,6 @@ +SUBDIRS = lib + pkgdata_DATA = device_filter.xml jardir = $(prefix)/jar diff --git a/autogen.sh b/autogen.sh index 971c456..5d169f2 100755 --- a/autogen.sh +++ b/autogen.sh @@ -1,5 +1,6 @@ #!/bin/sh +libtoolize --install --copy --quiet || exit 1 aclocal || exit 1 automake --add-missing --copy --foreign || exit 1 autoconf || exit 1 diff --git a/configure.ac b/configure.ac index 4dc2b09..0e793e5 100644 --- a/configure.ac +++ b/configure.ac @@ -15,7 +15,10 @@ AC_ARG_WITH([android-platform], [ANDROID_PLATFORM="$withval"], [ANDROID_PLATFORM=android-14]) +LT_INIT +AC_PROG_CXX + AC_SUBST([ANDROID_SDK]) AC_SUBST([ANDROID_PLATFORM]) -AC_OUTPUT([Makefile]) +AC_OUTPUT([Makefile lib/Makefile lib/libsigrokandroidutils.pc]) diff --git a/lib/Makefile.am b/lib/Makefile.am new file mode 100644 index 0000000..56afea5 --- /dev/null +++ b/lib/Makefile.am @@ -0,0 +1,14 @@ +lib_LTLIBRARIES = libsigrokandroidutils.la + +libsigrokandroidutils_la_CXXFLAGS = -fno-exceptions + +libsigrokandroidutils_la_SOURCES = \ + jvm_glue.cpp \ + envsetup.cpp + +pkgconfigdir = $(libdir)/pkgconfig +pkgconfig_DATA = libsigrokandroidutils.pc + +library_includedir = $(includedir)/libsigrokandroidutils +library_include_HEADERS = libsigrokandroidutils.h +noinst_HEADERS = libsigrokandroidutils-internal.h diff --git a/lib/envsetup.cpp b/lib/envsetup.cpp new file mode 100644 index 0000000..373d8e3 --- /dev/null +++ b/lib/envsetup.cpp @@ -0,0 +1,56 @@ +/* + * This file is part of the sigrok project. + * + * Copyright (C) 2014 Marcus Comstedt + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "libsigrokandroidutils.h" +#include "libsigrokandroidutils-internal.h" + +SRAU_API void srau_init_environment(void) +{ + JNIEnv* env; + + int attach_mode = srau_get_java_env(&env); + + if (attach_mode < 0) + return; + + jclass envc = srau_get_environment_class(env); + if (!envc) + return; + + jmethodID getEnv = env->GetStaticMethodID(envc, "getEnvironment", + "()[Ljava/lang/String;"); + jobjectArray envs = + (jobjectArray)env->CallStaticObjectMethod(envc, getEnv); + jsize i, envn = env->GetArrayLength(envs); + for (i=0; iGetObjectArrayElement(envs, i); + jstring value = (jstring)env->GetObjectArrayElement(envs, i+1); + const char *utfkey = env->GetStringUTFChars(key, 0); + const char *utfvalue = env->GetStringUTFChars(value, 0); + setenv(utfkey, utfvalue, 1); + env->ReleaseStringUTFChars(value, utfvalue); + env->ReleaseStringUTFChars(key, utfkey); + env->DeleteLocalRef(value); + env->DeleteLocalRef(key); + } + env->DeleteLocalRef(envs); + env->DeleteLocalRef(envc); + + srau_unget_java_env(attach_mode); +} diff --git a/lib/jvm_glue.cpp b/lib/jvm_glue.cpp new file mode 100644 index 0000000..7dbd03b --- /dev/null +++ b/lib/jvm_glue.cpp @@ -0,0 +1,98 @@ +/* + * This file is part of the sigrok project. + * + * Copyright (C) 2014 Marcus Comstedt + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "libsigrokandroidutils.h" +#include "libsigrokandroidutils-internal.h" + +static JavaVM *g_jvm = NULL; +static jclass g_environment_class = 0; + +SRAU_PRIV int srau_get_java_env(JNIEnv **env) +{ + jint st; + + if (g_jvm == NULL) { + return -1; + } + + st = g_jvm->GetEnv((void **)env, JNI_VERSION_1_6); + + if (st == JNI_EDETACHED) { + st = g_jvm->AttachCurrentThread(env, NULL); + if (st == JNI_OK) + return 1; + } + + return (st == JNI_OK? 0 : -1); +} + +SRAU_PRIV void srau_unget_java_env(int mode) +{ + if (mode == 1) { + g_jvm->DetachCurrentThread(); + } +} + +SRAU_PRIV jclass srau_get_environment_class(JNIEnv *env) +{ + if (env && g_environment_class) { + return (jclass)env->NewLocalRef(g_environment_class); + } else { + return 0; + } +} + +jint JNI_OnLoad(JavaVM *vm, void *reserved) +{ + JNIEnv* env; + + (void)reserved; + + if (vm->GetEnv((void **)&env, JNI_VERSION_1_6) != JNI_OK) { + return -1; + } + + jclass envc = env->FindClass("org/sigrok/androidutils/Environment"); + if (envc) { + g_environment_class = (jclass)env->NewGlobalRef(envc); + env->DeleteLocalRef(envc); + } + + g_jvm = vm; + + return JNI_VERSION_1_6; +} + +void JNI_OnUnload(JavaVM *vm, void *reserved) +{ + JNIEnv* env; + + (void)reserved; + + g_jvm = NULL; + + if (vm->GetEnv((void **)&env, JNI_VERSION_1_6) != JNI_OK) { + return; + } + + if (g_environment_class) { + env->DeleteGlobalRef(g_environment_class); + g_environment_class = 0; + } +} diff --git a/lib/libsigrokandroidutils-internal.h b/lib/libsigrokandroidutils-internal.h new file mode 100644 index 0000000..f0b3870 --- /dev/null +++ b/lib/libsigrokandroidutils-internal.h @@ -0,0 +1,39 @@ +/* + * This file is part of the sigrok project. + * + * Copyright (C) 2014 Marcus Comstedt + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef LIBSIGROKANDROIDUTILS_LIBSIGROKANDROIDUTILS_INTERNAL_H +#define LIBSIGROKANDROIDUTILS_LIBSIGROKANDROIDUTILS_INTERNAL_H + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +SRAU_PRIV int srau_get_java_env(JNIEnv **env); +SRAU_PRIV void srau_unget_java_env(int mode); +SRAU_PRIV jclass srau_get_environment_class(JNIEnv *env); + +#ifdef __cplusplus +} +#endif + +#endif + diff --git a/lib/libsigrokandroidutils.h b/lib/libsigrokandroidutils.h new file mode 100644 index 0000000..a69cf38 --- /dev/null +++ b/lib/libsigrokandroidutils.h @@ -0,0 +1,56 @@ +/* + * This file is part of the sigrok project. + * + * Copyright (C) 2014 Marcus Comstedt + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef LIBSIGROKANDROIDUTILS_LIBSIGROKANDROIDUTILS_H +#define LIBSIGROKANDROIDUTILS_LIBSIGROKANDROIDUTILS_H + +#ifdef __cplusplus +extern "C" { +#endif + +/* + * Use SRAU_API to mark public API symbols, and SRAU_PRIV for private symbols. + * + * Variables and functions marked 'static' are private already and don't + * need SR_PRIV. However, functions which are not static (because they need + * to be used in other libsigrokandroidutils-internal files) but are also + * not meant to be part of the public libsigrokandroidutils API, must use + * SRAU_PRIV. + * + * This uses the 'visibility' feature of gcc (requires gcc >= 4.0). + * + * Details: http://gcc.gnu.org/wiki/Visibility + */ + +/* Marks public libsigrokandroidutils API symbols. */ +#define SRAU_API __attribute__((visibility("default"))) + +/* Marks private, non-public libsigrokandroidutils symbols (not part of the API). */ +#define SRAU_PRIV __attribute__((visibility("hidden"))) + + + +SRAU_API void srau_init_environment(void); + +#ifdef __cplusplus +} +#endif + +#endif + diff --git a/lib/libsigrokandroidutils.pc.in b/lib/libsigrokandroidutils.pc.in new file mode 100644 index 0000000..c6ab1c5 --- /dev/null +++ b/lib/libsigrokandroidutils.pc.in @@ -0,0 +1,12 @@ +prefix=@prefix@ +exec_prefix=@exec_prefix@ +libdir=@libdir@ +includedir=@includedir@ + +Name: libsigrokandroidutils +Description: Android support code for sigrok +URL: http://www.sigrok.org +Version: @VERSION@ +Libs: -L${libdir} -lsigrokandroidutils +Cflags: -I${includedir} +