Debian Codegraph @

examples/asdf_with_dependence/readme.org

raw
1 #+TITLE: Build an asdf system with dependencies
2 #+AUTHOR: Bo Yao <[email protected]>
3
4 First, let's disregard the simple situation in which we write Lisp
5 without depending on any other Lisp libraries. A more practical
6 example is to build a library that depends on other asdf systems. ECL
7 provides a useful extension for asdf called ~asdf:make-build~, which
8 offers an abstraction for building libraries directly from system
9 definitions.
10
11 To download dependencies you may use Quicklisp to load your system
12 (with dependencies defined). Make sure you can successfully load and
13 run your library in ECL REPL (or ~*slime-repl*~). Don't worry about
14 other libraries loaded in your image – ECL will only build and pack
15 libraries your project depends on (that is, all dependencies you put
16 in your ~.asd~ file, and their dependencies - nothing more, despite
17 the fact that other libraries may be loaded).
18
19 ** Example code to build
20
21 We use a simple project that depends on ~alexandria~ to demonstrate
22 the interface. The example consists of ~example-with-dep.asd~,
23 ~package.lisp~ and ~example.lisp~ (included in the
24 ~examples/asdf_with_dependence/~ directory in the ECL source tree).
25 Before any kind of build you need to push the full path of this
26 directory to ~asdf:*central-registry*~ (or link it in a location
27 already recognized by ASDF).
28
29 ** Build it as a single executable
30
31 Use this in REPL to make an executable:
32
33 #+BEGIN_SRC common-lisp
34 (asdf:load-asd "example-with-dep.asd")
35 (asdf:load-system :example-with-dep)
36 (asdf:make-build :example-with-dep
37 :type :program
38 :move-here #P"./"
39 :epilogue-code '(progn (example:test-function 5)
40 (si:exit)))
41 #+END_SRC
42
43 Here the ~:epilogue-code~ is executed after loading our library; we
44 can use arbitrary Lisp forms here. You can also put this code in
45 your Lisp files and directly build them without this ~:epilogue-code~
46 option to achieve the same result. Running the program in a console
47 will display the following and exit:
48
49 #+BEGIN_SRC shell
50 Factorial of 5 is: 120
51 #+END_SRC
52
53 ** Build it as shared library and use in C
54
55 Use this in REPL to make a shared library:
56 #+BEGIN_SRC common-lisp
57 (asdf:make-build :example-with-dep
58 :type :shared-library
59 :move-here #P"./"
60 :monolithic t
61 :init-name "init_example")
62 #+END_SRC
63
64 Here ~:monolithic t~ means that ECL will compile the library and all
65 its dependencies into a single library named
66 ~example-with-dep--all-systems.so~. The ~:move-here~ parameter is
67 self-explanatory. ~:init-name~ sets the name of the initialization
68 function. Each library linked from C/C++ code must be initialized,
69 and this is a mechanism to specify the initialization function's name.
70
71 To use it, we write a simple C program:
72
73 #+BEGIN_SRC c
74 /* test.c */
75 #include <ecl/ecl.h>
76
77 int main (int argc, char **argv) {
78 extern void init_dll_example(cl_object);
79
80 cl_boot(argc, argv);
81 ecl_init_module(NULL, init_dll_example);
82
83 /* do things with the Lisp library */
84 cl_eval(c_string_to_object("(example:test-function 5)"));
85
86 cl_shutdown();
87 return 0;
88 }
89
90 #+END_SRC
91
92 Compile the file using a standard C compiler (note we're linking to
93 ~libecl.so~ with ~-lecl~, which provides the lisp runtime[fn:1]):
94
95 #+BEGIN_SRC shell
96 gcc test.c example-with-dep--all-systems.so -o test -lecl
97 #+END_SRC
98
99 If ECL is installed in a non-standard location you may need to provide
100 flags for the compiler and the linker. You may read them with:
101
102 #+BEGIN_SRC shell
103 ecl-config --cflags
104 ecl-config --libs
105 #+END_SRC
106
107 Since our shared object is not in the standard location, you need to
108 provide ~LD_LIBRARY_PATH~ pointing to the current directory to run
109 the application:
110
111 #+BEGIN_SRC shell
112 LD_LIBRARY_PATH=`pwd` ./test
113 #+END_SRC
114
115 This will show:
116
117 #+BEGIN_SRC shell
118 Factorial of 5 is: 120
119 #+END_SRC
120
121 You can also build all dependent libraries separately as a few ~.so~
122 files and link them together. For example, if you are building a
123 library called ~complex-example~, that depends on ~alexandria~ and
124 ~cl-fad~, you can do the following (in the REPL):
125
126 #+BEGIN_SRC common-lisp
127 (asdf:make-build :complex-example
128 :type :shared-library
129 :move-here #P"./"
130 :init-name "init_example")
131
132 (asdf:make-build :alexandria
133 :type :shared-library
134 :move-here #P"./"
135 :init-name "init_alexandria")
136
137 (asdf:make-build :cl-fad
138 :type :shared-library
139 :move-here #P"./"
140 :init-name "init_fad")
141
142 (asdf:make-build :bordeaux-threads
143 :type :shared-library
144 :move-here #P"./"
145 :init-name "init_bt")
146 #+END_SRC
147
148 Note that we haven't specified ~:monolithic t~, so we need to build
149 ~bordeaux-threads~ as well because ~cl-fad~ depends on it. The
150 building sequence doesn't matter and the resultant ~.so~ files can
151 also be used in your future programs if these libraries are not
152 modified.
153
154 We need to initialize all these modules using ~ecl_init_module~ in the
155 correct order. (~bordeaux-threads~ must be initialized before ~cl-fad~;
156 ~cl-fad~ and ~alexandria~ must be initialized before ~complex-ecample~.)
157
158 Here is a code snippet (not a full program):
159 #+BEGIN_SRC c
160 extern void init_fad(cl_object);
161 extern void init_alexandria(cl_object);
162 extern void init_bt(cl_object);
163 extern void init_example(cl_object);
164
165 /* call these *after* cl_boot(argc, argv);
166 if B depends on A, you should first init A then B. */
167 ecl_init_module(NULL, init_bt);
168 ecl_init_module(NULL, init_fad);
169 ecl_init_module(NULL, init_alexandria);
170 ecl_init_module(NULL, init_example);
171 #+END_SRC
172
173 ** Build it as a static library and use in C
174 To build a static library, use:
175
176 #+BEGIN_SRC common-lisp
177 (asdf:make-build :example-with-dep
178 :type :static-library
179 :move-here #P"./"
180 :monolithic t
181 :init-name "init_example")
182 #+END_SRC
183
184 This will generate ~example-with-dep--all-systems.a~ in the current
185 directory which we need to initialize with the ~init_example~
186 function. Compile it using:
187
188 #+BEGIN_SRC shell
189 gcc test.c example-with-dep--all-systems.a -o test-static -lecl
190 #+END_SRC
191
192 Then run it:
193
194 #+BEGIN_SRC shell
195 ./test-static
196 #+END_SRC
197
198 This will show:
199
200 #+BEGIN_SRC shell
201 Factorial of 5 is: 120
202 #+END_SRC
203
204 Note we don't need to pass the current path in ~LD_LIBRARY_PATH~ here,
205 since our Lisp library is statically bundled with the executable. The
206 result is the same as the shared library example above. You can also
207 build all dependent libraries separately as static libraries.
208
209 * Footnotes
210
211 [fn:1] You may also link ECL runtime statically. That is not covered
212 in this walkthrough.