embedding chibi-scheme in an android app

70 views
Skip to first unread message

Nawal Husnoo

unread,
Dec 5, 2022, 8:33:58 PM12/5/22
to chibi-scheme
Hi,

I've managed to add chibi-scheme to an android app, at least at the vm level. Now I'm trying to get the libraries working, but having some issues related to dynamic linking possibly. Starting with (srfi 69), I've got the hash.c compiled into libhash.so - android insists on this naming convention, and it can't load .so files from /sdcard (where the lib/...scm) currently lives.

So I modified lib/srfi/69.sld to remove (include-shared "69/hash") and replaced it with a (load ...) to the specific file:
  (display "69.sld: attempt to load libhash...")
  (newline)
  (display "69.sld: CHIBI_ANDROID_LIB=")
  (display CHIBI_ANDROID_LIB)
  (newline)
  (define X (string-append CHIBI_ANDROID_LIB "/libhash.so"))
  (display X)
  (load X)

I have set up CHIBI_ANDROID_LIB=/data/app/com.husnoo.loki-2/lib/arm64/ through the C++ code loading chibi-scheme (but the -2 changes to -1 sometimes so I don't want to hard code that).

The code is then called using:
      printf("load scheme small:\n");
      obj1 = sexp_eval_string(ctx, "(import (srfi 69))", -1, NULL);
      sexp_debug(ctx, "obj1: ", obj1);
      check_exception(ctx, obj1);

The output looks like:
    load scheme small:
    69.sld: attempt to load libhash...
    69.sld: CHIBI_ANDROID_LIB=/data/app/com.husnoo.loki-2/lib/arm64
    /data/app/com.husnoo.loki-2/lib/arm64/libhash.so
2022-12-05 19:19:56.725 23643-23658/com.husnoo.loki V/Loki: stderr: test stderr worked!
    obj1: #<undef>
    suspicious use of define in library declarations - did you forget to wrap it in begin?: (define X (string-append CHIBI_ANDROID_LIB "/libhash.so"))
    WARNING: out of order define: (define X (string-append CHIBI_ANDROID_LIB "/libhash.so"))
    WARNING: reference to undefined variable: hash-table-cell
    WARNING: reference to undefined variable: hash
    WARNING: reference to undefined variable: hash-by-identity
    WARNING: importing already defined binding: hash-table-cell
    WARNING: importing undefined variable: hash-table-delete!
    WARNING: importing already defined binding: hash
    WARNING: importing undefined variable: string-hash
    WARNING: importing undefined variable: string-ci-hash
    WARNING: importing already defined binding: hash-by-identity
    obj1: #<undef>
    obj1: #<Exception 498980297472>
    ERROR on line 11 of file /sdcard/Loki/chibi-lib/srfi/69/interface.scm: undefined variable: hash
      called from <anonymous>


I'm aware it's not happy about the define and such, but even replacing this with (load (string-append CHIBI_ANDROID_LIB "/libhash.so")) doesn't help with the importing of already defined bindings that also are undefined....

Thanks for any hints on where to look next! (CMake fragment below for the compilation).

Nawal

CMake fragment:
add_library(hash SHARED ${CMAKE_CURRENT_SOURCE_DIR}/../deps/chibi-scheme/lib/srfi/69/hash.c)
target_include_directories(hash PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../deps/chibi-scheme/include )
target_compile_definitions(hash PRIVATE SEXP_USE_NTPGETTIME SEXP_USE_INTTYPES)
target_link_libraries(hash PRIVATE  chibi_scheme)

This is meant to match the version run by make on linux:
cc -fPIC -shared  -Iinclude  -DSEXP_USE_NTPGETTIME -DSEXP_USE_INTTYPES -Wall -g -g3 -O3   -o lib/srfi/69/hash.so lib/srfi/69/hash.c -L.   -lchibi-scheme


*...@speechcode.com

unread,
Mar 1, 2023, 5:37:45 PM3/1/23
to chibi-scheme
Did you ever manage to make progress on this project?  I'm interested in having Chibi Scheme running on Android, too.

Nawal Husnoo

unread,
Mar 2, 2023, 1:00:02 AM3/2/23
to chibi-...@googlegroups.com
I did, I'll try to put it on GitHub in the weekend if I have time. I used ImGui, which may or may not fit your use case. It's pretty rubbish on Android if you care about native experience, but it's fine for my use.

On Wed, Mar 1, 2023, 22:37 *...@speechcode.com <*@speechcode.com> wrote:
Did you ever manage to make progress on this project?  I'm interested in having Chibi Scheme running on Android, too.

--
You received this message because you are subscribed to a topic in the Google Groups "chibi-scheme" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/chibi-scheme/hlqJ5hRSTnM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to chibi-scheme...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/chibi-scheme/4870d892-26f4-4ba1-88fb-fd4d90dc1b69n%40googlegroups.com.

Nawal Husnoo

unread,
Mar 2, 2023, 11:04:30 AM3/2/23
to chibi-...@googlegroups.com
Hi,

Have a look at this: https://github.com/husnoo/loki-core

I'm really hoping you're on Linux, as the script to set up the
dependencies assumes a Linux-ish environment to download/patch stuff
and build the -dynamic chibi scheme.

The linux build is necessary to ensure the c-stubs
(https://github.com/husnoo/loki-core/blob/master/src/lib/imgui/imgui.stub)
turn into .c files and clibs.c is generated. You might not need
anything in the src/lib but it's a nice set of examples for binding to
c-code.

The readme tells you to adb push the chibi lib/ to the device - the
path for that might be different for you, so you might need to adjust
https://github.com/husnoo/loki-core/blob/master/src/loki_main.cpp#L153
- L159ish.

You'll probably want to add a font if you're using the imgui example
(https://github.com/husnoo/loki-core/blob/master/src/loki_main.cpp#L40)
- the built-in imgui font on Android looks like it's for people with
very good eyesight.

I hope I've included enough of the android junk for it to compile -
let me know if anything's missing.

If you already have your own code in Android/C/JNI stuff, you might be
able to just pick out the essential bits from mine and run with that.
My code is meant to have an entry point from Linux
(src/linux_main.cpp) and Android (android/main.cpp) to set up the
window and GL stuff etc, and then call src/loki_main.cpp.

Hope that helps!

Arthur A. Gleckler

unread,
Mar 3, 2023, 12:31:39 PM3/3/23
to chibi-...@googlegroups.com
On Thu, Mar 2, 2023 at 8:04 AM Nawal Husnoo <na...@husnoo.com> wrote:
  
I'm really hoping you're on Linux, as the script to set up the
dependencies assumes a Linux-ish environment to download/patch stuff
and build the -dynamic chibi scheme.

I'm on Linux.

I ran ./setup_deps.sh, then make -j8, and got the results below. Do I need to do something with Nix first? I've never used it, but I'm happy to try.

echo "rebuilding imgui..."
echo "building chibi static"
rebuilding imgui...
PKG_CONFIG_PATH=$(dirname $(ls /nix/store/*xorgproto*/share/pkgconfig/xproto.pc)):$PKG_CONFIG_PATH \
make -C deps/imgui/examples/example_glfw_opengl3;
g++ -c src/linux_main.cpp \
-I./deps/imgui \
-I./deps/imgui/backends/ \
-o build/linux_main.o
building chibi static
echo '#define sexp_so_extension "'.so'"' > deps/chibi-scheme-static/include/chibi/install.h
g++ -c src/loki_main.cpp \
-I./deps/imgui \
-I./deps/imgui/backends/ \
-I./deps/chibi-scheme-static/include \
-o build/loki_main.o
echo '#define sexp_default_module_path "'/usr/local/share/chibi:/usr/local/lib/chibi:/usr/local/share/snow:/usr/local/lib/snow'"' >> deps/chibi-scheme-static/include/chibi/install.h
echo "generating chibi c code from stub for chibi libs"
gcc -c src/lib/imgui/imgui_src.c \
-I./deps/imgui \
-I./deps/cimgui \
-I./deps/imgui/backends/ \
-o build/imgui_src.o
echo '#define sexp_platform "'linux'"' >> deps/chibi-scheme-static/include/chibi/install.h
generating chibi c code from stub for chibi libs
echo '#define sexp_version "'0.10.0'"' >> deps/chibi-scheme-static/include/chibi/install.h
echo '#define sexp_release_name "'`cat deps/chibi-scheme-static/RELEASE`'"' >> deps/chibi-scheme-static/include/chibi/install.h
ls: cannot access '/nix/store/*xorgproto*/share/pkgconfig/xproto.pc': No such file or directory
dirname: missing operand
Try 'dirname --help' for more information.
make[1]: Entering directory '/home/arthur/repo-clones/loki-core/deps/imgui/examples/example_glfw_opengl3'
g++ -std=c++11 -I../.. -I../../backends -g -Wall -Wformat `pkg-config --cflags glfw3` -c -o main.o main.cpp
g++ -std=c++11 -I../.. -I../../backends -g -Wall -Wformat `pkg-config --cflags glfw3` -c -o imgui.o ../../imgui.cpp
g++ -std=c++11 -I../.. -I../../backends -g -Wall -Wformat `pkg-config --cflags glfw3` -c -o imgui_demo.o ../../imgui_demo.cpp
In file included from src/lib/imgui/imgui_src.c:2:
src/lib/imgui/imgui_src.h:8:10: fatal error: cimgui.h: No such file or directory
    8 | #include "cimgui.h"
      |          ^~~~~~~~~~
compilation terminated.
make: *** [Makefile:45: build/imgui_src.o] Error 1
make: *** Waiting for unfinished jobs....
g++ -std=c++11 -I../.. -I../../backends -g -Wall -Wformat `pkg-config --cflags glfw3` -c -o imgui_draw.o ../../imgui_draw.cpp
g++ -std=c++11 -I../.. -I../../backends -g -Wall -Wformat `pkg-config --cflags glfw3` -c -o imgui_tables.o ../../imgui_tables.cpp
g++ -std=c++11 -I../.. -I../../backends -g -Wall -Wformat `pkg-config --cflags glfw3` -c -o imgui_widgets.o ../../imgui_widgets.cpp
g++ -std=c++11 -I../.. -I../../backends -g -Wall -Wformat `pkg-config --cflags glfw3` -c -o imgui_impl_glfw.o ../../backends/imgui_impl_glfw.cpp
g++ -std=c++11 -I../.. -I../../backends -g -Wall -Wformat `pkg-config --cflags glfw3` -c -o imgui_impl_opengl3.o ../../backends/imgui_impl_opengl3.cpp
g++ -o example_glfw_opengl3 main.o imgui.o imgui_demo.o imgui_draw.o imgui_tables.o imgui_widgets.o imgui_impl_glfw.o imgui_impl_opengl3.o -std=c++11 -I../.. -I../../backends -g -Wall -Wformat `pkg-config --cflags glfw3` -lGL `pkg-config --libs glfw3` -ldl
touch deps/chibi-scheme-static/lib/.stubbed
Build complete for Linux
make[1]: Leaving directory '/home/arthur/repo-clones/loki-core/deps/imgui/examples/example_glfw_opengl3'
ar -crs build/imgui.a \
deps/imgui/examples/example_glfw_opengl3/imgui_demo.o \
deps/imgui/examples/example_glfw_opengl3/imgui_draw.o \
deps/imgui/examples/example_glfw_opengl3/imgui_impl_glfw.o \
deps/imgui/examples/example_glfw_opengl3/imgui_impl_opengl3.o \
deps/imgui/examples/example_glfw_opengl3/imgui.o \
deps/imgui/examples/example_glfw_opengl3/imgui_tables.o \
deps/imgui/examples/example_glfw_opengl3/imgui_widgets.o
cd deps/chibi-scheme-static/; ar -crs libchibi-scheme.a gc.c.o sexp.c.o bignum.c.o gc_heap.c.o opcodes.c.o vm.c.o simplify.c.o

Nawal Husnoo

unread,
Mar 3, 2023, 1:12:53 PM3/3/23
to chibi-...@googlegroups.com
No you shouldn't need nix, it looks like the step for generating the .c file for imgui didn't work, I'll have a look at it tomorrow.

--
You received this message because you are subscribed to a topic in the Google Groups "chibi-scheme" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/chibi-scheme/hlqJ5hRSTnM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to chibi-scheme...@googlegroups.com.

Nawal Husnoo

unread,
Mar 5, 2023, 4:04:33 AM3/5/23
to chibi-...@googlegroups.com
Issue seems to be around here:

$ grep nix -R ./Makefile
PKG_CONFIG_PATH=$$(dirname $$(ls
/nix/store/*xorgproto*/share/pkgconfig/xproto.pc)):$$PKG_CONFIG_PATH \
Try removing line 32 (including the \), but leave the rest
$(MAKE)..... on the remaining lines.

You might still get issues with missing dependencies - have a look in
the default.nix, it'll give you an idea of what's being used - and if
the new errors (after removing the line above) complain about a
missing dependency, try to find the name in default.nix, and then look
for something similar on your preferred package manager (are you on
Debian/redhat/etc?). A good way to know if all the imgui dependencies
have been met is to go into deps/imgui/examples/example_glfw_opengl3
and typing make.

As I mentioned, I used imgui, but you could try simply picking apart
what I have done and only use the chibi parts. It would imply starting
from a working Android App with JNI and then going from there.

Nawal Husnoo

unread,
Mar 5, 2023, 8:17:54 AM3/5/23
to chibi-...@googlegroups.com
If you still have problems with imgui, I've ripped out the dependency
on imgui - so now Android shows a blank screen, and the only way to
see the output is adb logcat (or Android-studio logcat). I've attached
a screenshot of this. You might need to modify
https://github.com/husnoo/chibi-droid/blob/master/src/loki_main.cpp#L66-L73
and https://github.com/husnoo/chibi-droid/blob/master/src/loki_main.cpp#L132.

This should be easier to get running, but you'll have to figure out
the JNI/UI stuff to make it do stuff on the screen.

https://github.com/husnoo/chibi-droid

If you find this helpful, and figure out the UI side, may I please ask
that you contact the chibi maintainers on github and contribute back a
suitably sanitised example for android?
image.png

Arthur A. Gleckler

unread,
Mar 5, 2023, 2:02:01 PM3/5/23
to chibi-...@googlegroups.com
On Sun, Mar 5, 2023 at 5:17 AM Nawal Husnoo <na...@husnoo.com> wrote:
  
This should be easier to get running, but you'll have to figure out
the JNI/UI stuff to make it do stuff on the screen.

Thanks!  It worked, except that I had to remove -lglut first.
  
If you find this helpful, and figure out the UI side, may I please ask
that you contact the chibi maintainers on github and contribute back a
suitably sanitised example for android?

We'll see how far I get, but I'd be happy to do that if I get far enough.

Thanks so much for setting me on the right path. 
Reply all
Reply to author
Forward
0 new messages