Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename the "init" function to avoid clashes #45

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions examples/libcalc/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,19 @@ ROOT_DIR := $(shell dirname $(shell dirname $(EXAMPLES_DIR)))

.PHONY: all clean

OS:=$(shell uname -s)
ifeq ($(OS), Darwin)
CALC_SO_NAME = libcalc.dylib
CCFLAGS = -dynamiclib
else
CALC_SO_NAME = libcalc.so
CCFLAGS = -shared
endif

all: example

example: libsbcl.so libcalc.dylib
$(CC) example.c -o example -lsbcl -lcalc -L.
example: libsbcl.so $(CALC_SO_NAME)
LD_LIBRARY_PATH=. $(CC) example.c -o example -lsbcl -lcalc -L.

# For some reason, even on OSX SBCL builds its library with *.so
# extension, but this works!
Expand All @@ -17,7 +26,8 @@ libsbcl.so: $(SBCL_SRC)
libcalc.core libcalc.c libcalc.h libcalc.py: libcalc.lisp
CL_SOURCE_REGISTRY="$(ROOT_DIR)//" $(SBCL_SRC)/run-sbcl.sh --script "script.lisp"

libcalc.dylib: libcalc.core libcalc.c
$(CC) -dynamiclib -o $@ libcalc.c -L$(SBCL_SRC)/src/runtime -lsbcl
$(CALC_SO_NAME): libcalc.core libcalc.c
$(CC) $(CCFLAGS) -o $@ libcalc.c -L$(SBCL_SRC)/src/runtime -lsbcl

clean:
rm -f example libsbcl.so libcalc.c libcalc.h libcalc.core libcalc.py libcalc.dylib
2 changes: 1 addition & 1 deletion examples/libcalc/example.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ void die(char *msg) {
}

int main(int argc, char **argv) {
init("libcalc.core");
sbcl_librarian_init("libcalc.core");

char source[256];
printf("> ");
Expand Down
11 changes: 8 additions & 3 deletions src/bindings.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
(defparameter *elf-export-linkage*
"__attribute__ ((visibility (\"default\")))")

(defvar *default-init-name*
'sbcl-librarian-init)

(defun write-linkage-macro (linkage build-name stream)
(let ((windows "_WIN64")
(elf "__ELF__"))
Expand Down Expand Up @@ -82,7 +85,8 @@
(format stream " initialized = 1;~%")
(format stream " return 0; }"))

(defun build-bindings (library directory &key (omit-init-function nil)
(defun build-bindings (library directory &key (init-function-name *default-init-name*)
(omit-init-function nil)
(initialize-lisp-args nil))
(let* ((c-name (library-c-name library))
(header-name (concatenate 'string c-name ".h"))
Expand All @@ -103,7 +107,8 @@
(write-api-to-header api linkage stream))
(unless omit-init-function
(format stream "~A;~%~%"
(c-function-declaration 'init ':int '((core :string))
(c-function-declaration init-function-name
':int '((core :string))
:datap nil
:linkage linkage)))
(format stream "#endif~%"))
Expand All @@ -116,4 +121,4 @@
(dolist (api (library-apis library))
(write-api-to-source api stream))
(unless omit-init-function
(write-init-function 'init linkage stream initialize-lisp-args)))))
(write-init-function init-function-name linkage stream initialize-lisp-args)))))
7 changes: 4 additions & 3 deletions src/python-bindings.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
library-name
(coerce-to-c-name callable-name))))

(defun write-default-python-header (library stream &optional (omit-init-call nil))
(defun write-default-python-header (library stream &optional (init-function-name *default-init-name*)
(omit-init-call nil))
(let ((name (library-c-name library)))
(format stream "import os~%")
(format stream "from ctypes import *~%")
Expand All @@ -35,8 +36,8 @@

(format stream "~a = CDLL(str(libpath), mode=RTLD_GLOBAL)~%~%" name)
(unless omit-init-call
(format stream "~a.init(str(libpath.parent / '~a.core').encode('utf-8'))~%~%"
name name))))
(format stream "~a.~a(str(libpath.parent / '~a.core').encode('utf-8'))~%~%"
name (coerce-to-c-name init-function-name) name))))

(defun write-api-to-python (api library-name stream)
(loop :for (kind . things) :in (api-specs api)
Expand Down