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

Polish the example a little bit #22

Closed
wants to merge 3 commits into from
Closed
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
36 changes: 27 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,39 @@
Opinionated interface for creating shared libraries. Requires SBCL
version >2.1.10.

After loading this system, you can build the example library like this:

`sbcl --eval "(progn (asdf:load-system :sbcl-librarian) (load \"libcalc.lisp\"))"`
## Building the example

which produces a header file, a source file, and a core file, and then you can compile the artifacts like so with:
In the `example/` directory there is a full example shown. It is
assumed sbcl-librarian is cloned in your local-projects directory so
it is visible to asdf. For example if you are using roswell:

`gcc -c -fpic libcalc.c`
```
$ cd ~/.roswell/local-projects/
$ git clone https://github.com/quil-lang/sbcl-librarian
```

`gcc -shared libcalc.o -o libcalc.so -lsbcl`
the user needs to have the SBCL shared library built. To do so one
needs to run the `make-shared-libarary.sh` script. For roswell users
(at tha time of writing the lates SBCL version is 2.2.2):

`gcc example.c -o example -lsbcl -lcalc -L.`

which creates a shared library and executable using the functions
defined in the example system, assuming you have `libsbcl.so` and
`libcalc.so` in a shared library path somewhere.
```
$ cd ~/.roswell/src/sbcl-2.2.2/
$ ./make-shared-library.sh
```

Once that is done you are ready to build the example

```
$ make
$ ./example
> (+ 1 2)
3
```

(in case there is a conflict related to build IDs see
[issue](https://github.com/quil-lang/sbcl-librarian/issues/21#issuecomment-1068250667))

NOTE: On Mac OS X you *MUST* specify `-pagezero_size 0x100000` when
linking the final executable, otherwise SBCL will fail to mmap its
Expand Down
23 changes: 18 additions & 5 deletions example/Makefile
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
.PHONY: all clean

all: libcalc.dylib
SBCL_SRC=$(HOME)/.roswell/src/sbcl-2.2.2

all: example

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

# When missing you need to run ./make-shared-library.sh in sbcl
# source.
#
# see: https://github.com/quil-lang/sbcl-librarian/issues/21#issuecomment-1068060794
libsbcl.so: $(SBCL_SRC)/src/runtime/libsbcl.so
ln -s $< $@

libcalc.core libcalc.c libcalc.h libcalc.py: libcalc.lisp
$(SBCL_SRC)/run-sbcl.sh --script "script.lisp"
ros run -l "script.lisp" -q
# $(SBCL) --script "script.lisp"

libcalc.dylib: libcalc.core libcalc.c
$(CC) -dynamiclib -o $@ libcalc.c -L$(SBCL_SRC)/src/runtime -lsbcl
libcalc.dylib: libcalc.core libcalc.c libsbcl.so
$(CC) -dynamiclib -o $@ libcalc.c -L. -lsbcl
clean:
rm -f libcalc.c libcalc.h libcalc.core libcalc.py libcalc.dylib
rm -f libcalc.c libcalc.h libcalc.core libcalc.py libcalc.dylib example
1 change: 0 additions & 1 deletion example/example.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,3 @@ def die(msg):

libcalc.lisp_release_handle(expr)
libcalc.lisp_release_handle(simplified)

28 changes: 14 additions & 14 deletions example/libcalc.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,21 @@
(defun sum-expression-p (obj)
(typep obj 'sum-expression))

(defun parse-expr (lisp-expr)
(cond ((integerp lisp-expr)
(make-instance 'int-literal :value lisp-expr))
((and (listp lisp-expr) (eq '+ (first lisp-expr)))
(let* ((left (parse-expr (second lisp-expr)))
(right (parse-expr (third lisp-expr))))
(make-instance 'sum-expression
:left left
:right right)))
(t
(error "Unable to parse expression: ~A" lisp-expr))))

(defun parse (source)
(labels ((parse-expr (lisp-expr)
(cond ((integerp lisp-expr)
(make-instance 'int-literal :value lisp-expr))
((and (listp lisp-expr) (eq '+ (first lisp-expr)))
(let* ((left (parse-expr (second lisp-expr)))
(right (parse-expr (third lisp-expr))))
(make-instance 'sum-expression
:left left
:right right)))
(t
(error "Unable to parse expression: ~A" lisp-expr)))))
(let ((*package* (find-package "SBCL-LIBRARIAN-EXAMPLE")))
(with-input-from-string (stream source)
(parse-expr (read stream))))))
(with-input-from-string (stream source)
(parse-expr (read stream))))

(defparameter *print-expression-indent* 0)

Expand Down
7 changes: 6 additions & 1 deletion example/script.lisp
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
(require '#:asdf)

;; Requirement for loading the .asd directly can be avoided by adding
;;
;; sbcl-librarian/example/libcalc.asd
;;
;; to the system-index.txt file.
(asdf:load-asd (truename "./libcalc.asd"))
(asdf:load-system '#:libcalc)


(in-package #:sbcl-librarian/example/libcalc)

(build-bindings libcalc ".")
Expand Down