gauche-c-wrapper's cwcompile stopped working: function is not found

Jens Thiele <[email protected]> Sun, 28 Apr 2024 19:07:11 +0200
Newsgroups gmane.lisp.scheme.gauche
Message-ID <[email protected]>
Hi,

likely nobody can help (I think I am the only remaining user of gauche
c-wrapper?) but anyway here we go:

gauche-c-wrapper's cwcompile stopped working with newer gauche versions
and so far I have no idea what's the problem. Maybe the stub file?

A simple test script to create a simple shared library with an add
function that should be used via gauche c-wrapper:

#!/bin/bash -xe
cat <<EOF > libhello.h
int add(int i, int j);
EOF
cat <<EOF > libhello.c
#include "libhello.h"
int add(int i, int j) {
  return i+j;
}
EOF
gcc -shared -o libhello.so -fPIC libhello.c
cat <<'EOF' > test-libhello.scm
#!/bin/sh
#| -*- mode: scheme; coding: utf-8; -*-
exec gosh -I. -- $0 "$@"
|#
(use c-wrapper)
(c-load "libhello.h"
	:libs "-L. -lhello"
	:import '(add)
	:compiled-lib "libgauche-hello.so"
	)

(define (main args)
  #?=(add 10 32)
  0)
EOF
chmod +x ./test-libhello.scm
rm -vf libgauche-hello.so
gosh -V
dpkg -l gauche
dpkg -l gauche-c-wrapper
./test-libhello.scm
cwcompile -v test-libhello.scm
cat libgauche-hello.stub
LD_LIBRARY_PATH=. ./test-libhello.scm

output on a old system with gauche 0.9.6 where it still works:

$ ./test.sh
+ cat
+ cat
+ gcc -shared -o libhello.so -fPIC libhello.c
+ cat
+ chmod +x ./test-libhello.scm
+ rm -vf libgauche-hello.so
+ gosh -V
Gauche scheme shell, version 0.9.6 [utf-8,pthreads], x86_64-pc-linux-gnu
+ dpkg -l gauche
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name           Version      Architecture Description
+++-==============-============-============-=================================
ii  gauche         0.9.6-10     amd64        Scheme script engine
+ dpkg -l gauche-c-wrapper
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name             Version      Architecture Description
+++-================-============-============-====================================================
ii  gauche-c-wrapper 0.6.1-11     amd64        Foreign function interface for Gauche to C libraries
+ ./test-libhello.scm
#?="././test-libhello.scm":13:(add 10 32)
#?-    42
+ cwcompile -v test-libhello.scm
gauche-package compile --verbose --cflags=  --cppflags=   --ldflags=   --libs=  -L. -lhello  libgauche-hello libgauche-hello.stub
gcc -c    '-I/usr/lib/gauche-0.9/0.9.6/include' '-I/usr/lib/gauche-0.9/site/include'   -fPIC -o 'libgauche-hello.o' 'libgauche-hello.c'
gcc    '-L/usr/lib/gauche-0.9/0.9.6/x86_64-pc-linux-gnu' '-L/usr/lib/gauche-0.9/site/x86_64-pc-linux-gnu'  -shared -o libgauche-hello.so 'libgauche-hello.o' -lgauche-0.9 -ldl -lcrypt -lrt -lm  -lpthread   -L. -lhello 
+ cat libgauche-hello.stub
;;;
;;; libgauche-hello.stub
;;;
"#include \"stdlib.h\""
"#include \"gauche.h\""
"#include \"gauche/extend.h\""
"#include \"gauche/uvector.h\""
"#include \"libhello.h\""
"/* Hack for initialization stub */"
"static void internal_init(ScmModule*);"
"static ScmObj __c_wrapper_mod;"
"static ScmObj __sandbox_mod;"
"void Scm_Init_libgauche_hello(void)"
"{"
"    SCM_INIT_EXTENSION(libgauche_hello);"
"    __c_wrapper_mod = Scm_ApplyRec0(Scm_GlobalVariableRef(SCM_FIND_MODULE(\"c-wrapper.c-ffi\", FALSE), SCM_SYMBOL(SCM_INTERN(\"ffi-sandbox-module\")), FALSE));"
"    __sandbox_mod = Scm_ApplyRec0(Scm_GlobalVariableRef(SCM_FIND_MODULE(\"c-wrapper\", FALSE), SCM_SYMBOL(SCM_INTERN(\"sandbox-module\")), FALSE));"
"    internal_init(SCM_MODULE(__c_wrapper_mod));"
"}"
"#define Scm_Init_libgauche_hello internal_init"
"#define INIT_SYMVAL(var, sym) if (!var) { var = Scm_GlobalVariableRef(SCM_MODULE(__c_wrapper_mod), SCM_SYMBOL(SCM_INTERN(sym)), FALSE); }"
"static ScmObj cw_boxvar(ScmObj klass, void *varptr)"
"{"
"    static ScmObj make_proc = NULL;"
"    static ScmObj csizeof_proc = NULL;"
"    ScmObj obj;"
"    ScmObj buf;"
"    size_t size;"
"    INIT_SYMVAL(make_proc, \"make\");"
"    INIT_SYMVAL(csizeof_proc, \"c-sizeof\");"
"    size = Scm_GetIntegerU(Scm_ApplyRec1(csizeof_proc, klass));"
"    buf = Scm_MakeU8VectorFromArrayShared(size, varptr);"
"    obj = Scm_ApplyRec3(make_proc, klass, SCM_MAKE_KEYWORD(\"buffer\"), buf);"
"    SCM_RETURN(obj);"
"}"
"static void cw_unbox(void *dest, ScmObj obj, size_t size)"
"{"
"    static ScmObj bufferof_proc = NULL;"
"    ScmObj buf;"
"    if (!bufferof_proc) {"
"        bufferof_proc = Scm_GlobalVariableRef(SCM_FIND_MODULE(\"c-wrapper.c-ffi\", FALSE), SCM_SYMBOL(SCM_INTERN(\"buffer-of\")), FALSE);"
"    }"
"    buf = Scm_ApplyRec1(bufferof_proc, obj);"
"    memcpy(dest, SCM_UVECTOR_ELEMENTS(buf), size);"
"}"
"static ScmObj __c_wrapper_979;    /* add */"
(define-symbol make-c-func "__c_wrapper_974")
(define-symbol quote "__c_wrapper_975")
(define-symbol add "__c_wrapper_976")
(define-symbol <c-int> "__c_wrapper_977")
(define-symbol list "__c_wrapper_978")
(initcode "__c_wrapper_979 = Scm_EvalRec(SCM_LIST4(__c_wrapper_974, SCM_LIST2(__c_wrapper_975, __c_wrapper_976), __c_wrapper_977, SCM_LIST3(__c_wrapper_978, __c_wrapper_977, __c_wrapper_977)), SCM_OBJ(__c_wrapper_mod));")
(initcode "Scm_Define(SCM_MODULE(__c_wrapper_mod), SCM_SYMBOL(__c_wrapper_976), __c_wrapper_979);")
(initcode "Scm_Define(SCM_MODULE(__sandbox_mod), SCM_SYMBOL(__c_wrapper_976), Scm_GlobalVariableRef(SCM_MODULE(__c_wrapper_mod), SCM_SYMBOL(__c_wrapper_976), 0));")
;; Local variables:
;; mode: scheme
;; end:
+ LD_LIBRARY_PATH=.
+ ./test-libhello.scm
#?="././test-libhello.scm":13:(add 10 32)
#?-    42

output on a newer system (gauche 0.9.14) where it doesn't work:

$ ./test-libhello.sh
+ cat
+ cat
+ gcc -shared -o libhello.so -fPIC libhello.c
+ cat
+ chmod +x ./test-libhello.scm
+ rm -vf libgauche-hello.so
'libgauche-hello.so' wurde entfernt
+ gosh -V
Gauche scheme shell, version 0.9.14 [utf-8,pthreads], x86_64-pc-linux-gnu
(version "0.9.14")
(command "gosh")
(scheme.id gauche)
(languages scheme r5rs r7rs)
(encodings utf-8)
(website "https://practical-scheme.net/gauche")
(build.platform "x86_64-pc-linux-gnu")
(build.configure)
(scheme.path "/usr/share/gauche-0.98/site/lib" "/usr/share/gauche-0.98/0.9.14/lib")
(threads pthreads)
(gauche.net.tls mbedtls)
+ dpkg -l gauche

Gewünscht=Unbekannt/Installieren/R=Entfernen/P=Vollständig Löschen/Halten
| Status=Nicht/Installiert/Config/U=Entpackt/halb konFiguriert/
         Halb installiert/Trigger erWartet/Trigger anhängig
|/ Fehler?=(kein)/R=Neuinstallation notwendig (Status, Fehler: GROSS=schlecht)
||/ Name           Version            Architektur  Beschreibung
+++-==============-==================-============-=================================
ii  gauche         0.9.14-5~bookworm3 amd64        Scheme script engine
+ dpkg -l gauche-c-wrapper

Gewünscht=Unbekannt/Installieren/R=Entfernen/P=Vollständig Löschen/Halten
| Status=Nicht/Installiert/Config/U=Entpackt/halb konFiguriert/
         Halb installiert/Trigger erWartet/Trigger anhängig
|/ Fehler?=(kein)/R=Neuinstallation notwendig (Status, Fehler: GROSS=schlecht)
||/ Name             Version            Architektur  Beschreibung
+++-================-==================-============-====================================================
ii  gauche-c-wrapper 0.6.1-16~bookworm1 amd64        Foreign function interface for Gauche to C libraries
+ ./test-libhello.scm
#?="././test-libhello.scm":13:(add 10 32)
#?-    42
+ cwcompile -v test-libhello.scm
gauche-package compile --verbose --cflags=  --cppflags=   --ldflags=   --libs=  -L. -lhello  libgauche-hello libgauche-hello.stub
gcc -c    '-I/usr/lib/gauche-0.98/0.9.14/include' '-I/usr/lib/gauche-0.98/site/include'   -fPIC -o 'libgauche-hello.o' 'libgauche-hello.c'
gcc    '-L/usr/lib/gauche-0.98/0.9.14/x86_64-pc-linux-gnu' '-L/usr/lib/gauche-0.98/site/x86_64-pc-linux-gnu'  -shared -o libgauche-hello.so 'libgauche-hello.o' -lgauche-0.98 -lmbedtls -lcrypt -lrt -lm  -lpthread   -L. -lhello 
+ cat libgauche-hello.stub
;;;
;;; libgauche-hello.stub
;;;
"#include \"stdlib.h\""
"#include \"gauche.h\""
"#include \"gauche/extend.h\""
"#include \"gauche/uvector.h\""
"#include \"libhello.h\""
"/* Hack for initialization stub */"
"static void internal_init(ScmModule*);"
"static ScmObj __c_wrapper_mod;"
"static ScmObj __sandbox_mod;"
"void Scm_Init_libgauche_hello(void)"
"{"
"    SCM_INIT_EXTENSION(libgauche_hello);"
"    __c_wrapper_mod = Scm_ApplyRec0(Scm_GlobalVariableRef(SCM_FIND_MODULE(\"c-wrapper.c-ffi\", FALSE), SCM_SYMBOL(SCM_INTERN(\"ffi-sandbox-module\")), FALSE));"
"    __sandbox_mod = Scm_ApplyRec0(Scm_GlobalVariableRef(SCM_FIND_MODULE(\"c-wrapper\", FALSE), SCM_SYMBOL(SCM_INTERN(\"sandbox-module\")), FALSE));"
"    internal_init(SCM_MODULE(__c_wrapper_mod));"
"}"
"#define Scm_Init_libgauche_hello internal_init"
"#define INIT_SYMVAL(var, sym) if (!var) { var = Scm_GlobalVariableRef(SCM_MODULE(__c_wrapper_mod), SCM_SYMBOL(SCM_INTERN(sym)), FALSE); }"
"static ScmObj cw_boxvar(ScmObj klass, void *varptr)"
"{"
"    static ScmObj make_proc = NULL;"
"    static ScmObj csizeof_proc = NULL;"
"    ScmObj obj;"
"    ScmObj buf;"
"    size_t size;"
"    INIT_SYMVAL(make_proc, \"make\");"
"    INIT_SYMVAL(csizeof_proc, \"c-sizeof\");"
"    size = Scm_GetIntegerU(Scm_ApplyRec1(csizeof_proc, klass));"
"    buf = Scm_MakeU8VectorFromArrayShared(size, varptr);"
"    obj = Scm_ApplyRec3(make_proc, klass, SCM_MAKE_KEYWORD(\"buffer\"), buf);"
"    SCM_RETURN(obj);"
"}"
"static void cw_unbox(void *dest, ScmObj obj, size_t size)"
"{"
"    static ScmObj bufferof_proc = NULL;"
"    ScmObj buf;"
"    if (!bufferof_proc) {"
"        bufferof_proc = Scm_GlobalVariableRef(SCM_FIND_MODULE(\"c-wrapper.c-ffi\", FALSE), SCM_SYMBOL(SCM_INTERN(\"buffer-of\")), FALSE);"
"    }"
"    buf = Scm_ApplyRec1(bufferof_proc, obj);"
"    memcpy(dest, SCM_UVECTOR_ELEMENTS(buf), size);"
"}"
"static ScmObj __c_wrapper_894;    /* add */"
(define-symbol make-c-func "__c_wrapper_889")
(define-symbol quote "__c_wrapper_890")
(define-symbol add "__c_wrapper_891")
(define-symbol <c-int> "__c_wrapper_892")
(define-symbol list "__c_wrapper_893")
(initcode "__c_wrapper_894 = Scm_EvalRec(SCM_LIST4(__c_wrapper_889, SCM_LIST2(__c_wrapper_890, __c_wrapper_891), __c_wrapper_892, SCM_LIST3(__c_wrapper_893, __c_wrapper_892, __c_wrapper_892)), SCM_OBJ(__c_wrapper_mod));")
(initcode "Scm_Define(SCM_MODULE(__c_wrapper_mod), SCM_SYMBOL(__c_wrapper_891), __c_wrapper_894);")
(initcode "Scm_Define(SCM_MODULE(__sandbox_mod), SCM_SYMBOL(__c_wrapper_891), Scm_GlobalVariableRef(SCM_MODULE(__c_wrapper_mod), SCM_SYMBOL(__c_wrapper_891), 0));")
;; Local variables:
;; mode: scheme
;; end:
+ LD_LIBRARY_PATH=.
+ ./test-libhello.scm
#?="././test-libhello.scm":13:(add 10 32)
*** ERROR: function add is not found.
Stack Trace:
_______________________________________
  0  (report-error e)
  1  (add 10 32)
        at "././test-libhello.scm":13
  2  (main args)

greetings
Jens


_______________________________________________
Gauche-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/gauche-devel