@@ -10,27 +10,32 @@ Table of contents:
1010 - [ Using libsci from Python] ( #using-libsci-from-python )
1111 - [ References] ( #references )
1212
13- To use sci as a shared library from e.g. C++, follow along with this
14- tutorial. We illustrate what is happening when you run the script
15- ` libsci/compile-libsci ` and ` libsci/compile-cpp ` .
13+ To use sci as a shared library from e.g. C++, follow along with this tutorial. We illustrate
14+ what is happening when you run the tasks ` libsci:compile ` to build the shared ` libsci ` library
15+ and headers; and e.g. ` libsci:compile-cpp ` to build a trivial executable in a specific language, that
16+ uses the shared ` libsci ` library to evaluate arbitrary input strings as Clojure code.
1617
17- There are also instructions at the end for using the shared library from
18- Python using ctypes .
18+ There are instructions for using the shared library from C++, or Python using ` ctypes ` , or from
19+ Rust using ` bindgen ` .
1920
2021## Prerequisites
2122
22- If you want to run this script yourself, prepare as follows:
23+ If you want to compile ` libsci ` yourself, prepare as follows:
2324
2425- Download [ GraalVM] ( https://github.com/graalvm/graalvm-ce-builds/releases ) and
2526 set ` GRAALVM_HOME ` . Currently we use Oracle GraalVM 21 (double-check ` .github/workflows/ci.yml ` for what CI is currently using).
2627- Install [ lein] ( https://github.com/technomancy/leiningen ) . This is used for
2728 compiling Clojure code.
28- - You should have ` g++ ` available to compile C++ code.
29+
30+ Then, to use ` libsci ` from a specific language, you should have its tools, listed in each
31+ section below (e.g. ` g++ ` to compile C++).
2932
3033## Babashka tasks
3134
32- Convenient ` babashka ` tasks are provided to compile ` libsci ` and most
33- of the examples mentioned here, see ` bb tasks ` for the full list.
35+ Convenient ` babashka ` tasks are provided to compile ` libsci ` and most of the examples mentioned
36+ here, which can be run from the ` sci ` project root directory. e.g. ` bb libsci:compile ` .
37+
38+ See ` bb tasks ` for the full list of tasks.
3439
3540## Walkthrough
3641
@@ -99,14 +104,23 @@ $ $GRAALVM_HOME/bin/native-image \
99104```
100105
101106This begets the files ` graal_isolate_dynamic.h ` , ` graal_isolate.h ` , ` libsci.h ` ,
102- ` libsci.dylib ` (on linux ` libsci.so ` , on MS-Windows ` libsci.dll ` ) and ` libsci_dynamic.h ` .
107+ ` libsci.dylib ` (on Linux ` libsci.so ` , on MS-Windows ` libsci.dll ` ) and ` libsci_dynamic.h ` .
103108We move all these files to ` libsci/target ` .
104109
105110In addtion, on MS-Windows, there is one more library file,
106111` libsci.lib ` , which should be copied over as ` sci.lib ` .
107112
113+ Now, these headers and shared objects can be used natively from C++, or used to generate
114+ foreign bindings from other languages.
115+
108116### Using libsci from C++
109117
118+ #### Prerequisites
119+
120+ * ` g++ ` to compile C++ code.
121+
122+ #### Usage
123+
110124Let's use the library from a C++ program now. Here's the code:
111125
112126``` c++
@@ -141,7 +155,7 @@ To run, we first have to set an environment variable to locate the shared libary
141155$ export DYLD_LIBRARY_PATH=libsci/target
142156```
143157
144- On linux this environment variable is called ` LD_LIBRARY_PATH ` .
158+ On Linux this environment variable is called ` LD_LIBRARY_PATH ` .
145159
146160Now, let's run it.
147161
@@ -162,9 +176,16 @@ we printed the JSON string from the C++ program.
162176
163177### Using libsci from Rust
164178
165- To use ` libsci ` from a Rust program, we use the same shared lib generated in the
166- previous section (produced by running ` libsci/compile-libsci ` ). Here we
167- describe what happens when you run ` libsci/compile-rust ` .
179+ #### Prerequisites
180+
181+ * Rust and ` cargo `
182+ * Clang and ` libclang `
183+
184+ #### Usage
185+
186+ To use ` libsci ` from a Rust program, we use the same shared lib generated in the previous
187+ section (produced by running the ` libsci:compile ` task). Here we describe what happens when you
188+ run the ` libsci:compile-rust ` task.
168189
169190To build Rust language bindings to ` libsci ` , we use
170191[ bindgen] ( https://rust-lang.github.io/rust-bindgen/ ) which need a ` build.rs `
@@ -240,9 +261,12 @@ fn main() {
240261}
241262```
242263
243- After running ` libsci/compile-rust ` and exporting ` DYLD_LIBRARY_PATH `
244- (` LD_LIBRARY_PATH ` on linux) to ` libsci/target ` , you should be able to run as
245- follows:
264+ To compile the main program, run the ` libsci:compile-rust ` task. It should create a new
265+ ` libsci/target/from-rust ` executable.
266+
267+ Next, export ` DYLD_LIBRARY_PATH ` (` LD_LIBRARY_PATH ` on Linux) to ` libsci/target ` .
268+
269+ Now, you should be able to run ` from-rust ` :
246270
247271``` shell
248272$ libsci/target/from-rust " (require '[cheshire.core :as json]) (json/generate-string (range 10))"
@@ -251,12 +275,18 @@ $ libsci/target/from-rust "(require '[cheshire.core :as json]) (json/generate-st
251275
252276### Using libsci from Python
253277
254- To use the shared library from Python via ctypes, do the following from the directory
278+ #### Prerequisites
279+
280+ * Python 3 with the ` ctypes ` module.
281+
282+ #### Usage
283+
284+ To use the shared library from Python via ` ctypes ` , do the following from the directory
255285containing the shared object:
256286``` python
257287$ python
258288Python 3.8 .5 (default, Sep 5 2020 , 10 :50 :12 )
259- [GCC 10.2 .0] on linux
289+ [GCC 10.2 .0] on Linux
260290Type " help" , " copyright" , " credits" or " license" for more information.
261291>> > from ctypes import *
262292>> > dll = CDLL(" ./libsci.so" )
272302
273303The above instructions are for a Linux system.
274304
275- For macos , the file extension of the shared library should be different, probably `.dylib` .
305+ For Mac OS , the file extension of the shared library should be different, probably `.dylib` .
276306
277307For Windows, the file extension of the shared library should be different, probably `.dll` .
278308Also it may be necessary to use `WinDLL` instead of `CDLL ` .
0 commit comments