Debian Codegraph @

src/doc/manual/extensions/building.txi

raw
1 @node System building
2 @section System building
3
4 @menu
5 * Compiling with ECL::
6 * Compiling with ASDF::
7 * C compiler configuration::
8 @end menu
9 @c * Compiling with Matroska::
10
11 @cindex System building
12 @cindex Creating executables and libraries
13
14 @node Compiling with ECL
15 @subsection Compiling with ECL
16
17 In this section we will introduce topics on compiling Lisp
18 programs. ECL is especially powerful on combining lisp programs with C
19 programs. You can embed ECL as a lisp engine in C programs, or call C
20 functions via @ref{Foreign Function Interface}. We explain file types
21 generated by some compilation approaches. For the examples,
22 a GNU/Linux system and gcc as a development environment are assumed.
23
24 You can generate the following files with ECL:
25
26 @enumerate
27 @item Portable FASL file (.fasc)
28 @item Native FASL file (.fas, .fasb)
29 @item Object file (.o)
30 @item Static library
31 @item Shared library
32 @item Executable file
33 @end enumerate
34
35 Relations among them are depicted below:
36
37 @float Figure,fig:file_types
38 @caption{Build file types}
39 @image{figures/file-types}
40 @end float
41
42 @menu
43 * Portable FASL (fasc)::
44 * Native FASL::
45 * Object file::
46 * Static library::
47 * Shared library::
48 * Executable::
49 * Summary::
50 @end menu
51
52 @node Portable FASL (fasc)
53 @subsubsection Portable FASL
54 @cindex Portable FASL
55
56 ECL provides two compilers (bytecodes compiler, and C/C++
57 compiler). Portable FASL files are built from source lisp files by the
58 bytecodes compiler. Generally FASC files are portable across
59 architectures and operating systems providing a convenient way of shipping
60 portable modules. Portable FASL files may be concatenated, what leads to
61 bundles. FASC files are faster to compile, but generally slower to run.
62
63 @exindex Building Portable FASL file
64 @lisp
65 ;; install bytecodes compiler
66 (ext:install-bytecodes-compiler)
67
68 ;; compile hello.lisp file to hello.fasc
69 (compile-file "hello1.lisp")
70 (compile-file "hello2.lisp")
71
72 ;; reinitialize C/C++ compiler back
73 (ext:install-c-compiler)
74
75 ;; FASC file may be loaded dynamically from lisp program
76 (load "hello1.fasc")
77
78 ;; ... concatenated into a bundle with other FASC
79 (with-open-file (output "hello.fasc"
80 :direction :output
81 :if-exists :supersede)
82 (ext:run-program
83 "cat" '("hello1.fasc" "hello2.fasc") :output output))
84
85 ;; ... and loaded dynamically from lisp program
86 (load "hello.fasc")
87 @end lisp
88
89 @node Native FASL
90 @subsubsection Native FASL
91
92 @cindex Native FASL
93 @ftindex DLOPEN
94 @cfindex --enable-shared [YES|no]
95
96
97 If you want to make a library which is loaded dynamically from a lisp
98 program, you should choose the fasl file format. Under the hood native fasls
99 are just shared library files.
100
101 This means you can load fasl files with @code{dlopen} and initialize it
102 by calling a init function from C programs, but this is not an intended
103 usage. The recommended usage is to load fasl files by calling the load lisp
104 function. To work with @emph{Native FASL files} ECL has to be compiled
105 with @code{--enable-shared} configure option (enabled by default).
106
107 Creating a fasl file from one lisp file is very easy.
108
109 @lisp
110 (compile-file "hello.lisp")
111 @end lisp
112
113 To create a fasl file from more lisp files, firstly you have to compile
114 each lisp file into an object file, and then combine them with
115 @code{c:build-fasl}.
116
117 @exindex Building native FASL
118 @lisp
119 ;; generates hello.o
120 (compile-file "hello.lisp" :system-p t)
121 ;; generates goodbye.o
122 (compile-file "goodbye.lisp" :system-p t)
123
124 ;; generates hello-goodbye.fas
125 (c:build-fasl "hello-goodbye"
126 :lisp-files '("hello.o" "goodbye.o"))
127
128 ;; fasls may be built from mix of objects and libraries (both shared and
129 ;; static)
130 (c:build-fasl "mixed-bundle"
131 :lisp-files '("hello1.o" "hello2.a" "hello3.so"))
132 @end lisp
133
134 @node Object file
135 @subsubsection Object file
136
137 Object files work as an intermediate file format. If you want to compile
138 more than two lisp files, you might better to compile with a
139 @code{:system-p t} option, which generates object files (instead of a
140 fasl).
141
142 On linux systems, ECL invokes @code{gcc -c} to generate object files.
143
144 An object file consists of some functions in C:
145
146 @itemize
147 @item Functions corresponding to Lisp functions
148 @item The initialization function which registers defined functions on the lisp environment
149 @end itemize
150
151 Consider the example below.
152
153 @lisp
154 (defun say-hello ()
155 (print "Hello, world"))
156 @end lisp
157
158 @cindex Object file internal layout
159 During compilation, this simple lisp program is translated into the C
160 program, and then compiled into the object file. The C program contains
161 two functions:
162
163 @itemize
164
165 @item @code{static cl_object L1say_hello}:
166 'say-hello' function
167
168 @item @code{ECL_DLLEXPORT void _eclwm2nNauJEfEnD_CLSxi0z(cl_object flag)}:
169 initialization function
170
171 @end itemize
172
173 In order to use these object files from your C program, you have to call
174 initialization functions before using lisp functions (such as
175 @code{say-hello}). However the name of an init function is seemed to be
176 randomized and not user-friendly. This is because object files are not
177 intended to be used directly.
178
179 ECL provides other user-friendly ways to generate compiled lisp programs
180 (as static/shared libraries or executables), and in each approach, object
181 files act as intermediate files.
182
183 @node Static library
184 @subsubsection Static library
185
186 ECL can compile lisp programs to static libraries, which can be linked
187 with C programs. A static library is created by
188 @code{c:build-static-library} with some compiled object files.
189
190 @exindex Building static library
191 @lisp
192 ;; generates hello.o
193 (compile-file "hello.lsp" :system-p t)
194 ;; generates goodbye.o
195 (compile-file "goodbye.lsp" :system-p t)
196
197 ;; generates libhello-goodbye.a
198 (c:build-static-library "hello-goodbye"
199 :lisp-files '("hello.o" "goodbye.o")
200 :init-name "init_hello_goodbye")
201 @end lisp
202
203 When you use a static/shared library, you have to call its init function. The
204 name of this function is specified by the @code{:init-name} option. In this
205 example, it is then @code{init_hello_goodbye}. The usage of this function is
206 shown below:
207
208 @cppdef ecl_init_module
209 @exindex Initializing static/shared library in C/C++
210 @example
211 @verbatim
212 #include <ecl/ecl.h>
213 extern void init_hello_goodbye(cl_object cblock);
214
215 int
216 main(int argc, char **argv)
217 {
218 /* setup the lisp runtime */
219 cl_boot(argc, argv);
220
221 /* call the init function via ecl_init_module */
222 ecl_init_module(NULL, init_hello_goodbye);
223
224 /* ... */
225
226 /* shutdown the lisp runtime */
227 cl_shutdown();
228
229 return 0;
230 }
231 @end verbatim
232 @end example
233
234 Because the program itself does not know the type of the init function,
235 a prototype declaration is inserted. After booting up the lisp
236 environment, it invokes @code{init_hello_goodbye} via
237 @coderef{ecl_init_module}. @code{init_hello_goodbye} takes an argument,
238 and @coderef{ecl_init_module} supplies an appropriate one. Now that the
239 initialization is finished, we can use functions and other stuff defined
240 in the library.
241
242 @strong{DEPRECATED} @code{read_VV} - equivalent to @coderef{ecl_init_module}
243
244 @node Shared library
245 @subsubsection Shared library
246
247 Almost the same as with a static library. The user has to use
248 @code{c:build-shared-library}:
249
250 @exindex Building shared library
251 @lisp
252 ;; generates hello.o
253 (compile-file "hello.lsp" :system-p t)
254 ;; generates goodbye.o
255 (compile-file "goodbye.lsp" :system-p t)
256
257 ;; generates libhello-goodbye.so
258 (c:build-shared-library "hello-goodbye"
259 :lisp-files '("hello.o" "goodbye.o")
260 :init-name "init_hello_goodbye")
261 @end lisp
262
263 @node Executable
264 @subsubsection Executable
265
266 ECL supports the generation of executable files. To create a standalone
267 executable from a lisp program, compile all lisp files to object
268 files. After that, calling @code{c:build-program} creates the
269 executable:
270
271 @exindex Building executable
272 @lisp
273 ;; generates hello.o
274 (compile-file "hello.lsp" :system-p t)
275 ;; generates goodbye.o
276 (compile-file "goodbye.lsp" :system-p t)
277
278 ;; generates hello-goodbye
279 (c:build-program "hello-goodbye"
280 :lisp-files '("hello.o" "goodbye.o"))
281 @end lisp
282
283 Like with native FASL, the program may be built also from libraries.
284
285 @node Summary
286 @subsubsection Summary
287
288 In this section, some file types that can be compiled with ECL were
289 introduced. Each file type has an adequate purpose:
290
291 @itemize
292 @item Object file: intermediate file format for others
293 @item Fasl files: loaded dynamically via the @code{load} lisp function
294 @item Static library: linked with and used from C programs
295 @item Shared library: loaded dynamically and used from C programs
296 @item Executable: standalone executable
297 @end itemize
298
299 ECL provides a high-level interface @code{c:build-*} for each native
300 format. In case of @emph{Portable FASL} the bytecodes compiler is needed.
301
302 @node Compiling with ASDF
303 @subsection Compiling with ASDF
304
305 For larger systems involving more complex file dependencies, or for systems
306 that are portable across different Common Lisp implementations, it may be
307 better to define systems using @uref{https://common-lisp.net/project/asdf/,asdf}.
308
309 ECL provides a useful extension for asdf called @code{asdf:make-build}, which
310 offers an abstraction for building libraries directly from system definitions.
311 Note that this extension is only available in the ASDF that is shipped with
312 ECL; it may not be available from an ASDF installed from the system or from
313 Quicklisp.
314
315 To download dependencies you may use
316 @uref{https://www.quicklisp.org,Quicklisp} to load your system (with
317 dependencies defined). Make sure you can successfully load and run your
318 library in the ECL REPL (or @code{*slime-repl*}). Don't worry about
319 other libraries loaded in your image – ECL will only build and pack
320 libraries your project depends on (that is, all dependencies you put in
321 your @code{.asd} file, and their dependencies - nothing more, despite
322 the fact that other libraries may be loaded).
323
324 @menu
325 * Example code to build::
326 * Build it as an single executable::
327 * Build it as shared library and use in C::
328 * Build it as static library and use in C::
329 @end menu
330
331 @node Example code to build
332 @subsubsection Example code to build
333
334 An example project is included in the ECL source distribution in the
335 @code{examples/asdf_with_dependence/} directory.
336
337 This project depends on the @code{alexandria} library and consists of a system
338 definition (@code{example-with-dep.asd}), package definition
339 (@code{package.lisp}), and the actual library code (@code{example.lisp}).
340
341 Before following the steps below, you must
342 @uref{https://asdf.common-lisp.dev/asdf/Configuring-ASDF-to-find-your-systems.html,configure ASDF to find your systems}.
343 You can either copy or symlink the example directory in one of the standard
344 ASDF locations, or push the path of the example directory to your
345 @code{asdf:*central-registry*}, for example:
346
347 @lisp
348 (push "./" asdf:*central-registry*)
349 @end lisp
350
351 @node Build it as an single executable
352 @subsubsection Build it as an single executable
353
354 Use this in the REPL to make an executable:
355
356 @lisp
357 (asdf:make-build :example-with-dep
358 :type :program
359 :move-here #P"./"
360 :epilogue-code '(progn (example:test-function 5)
361 (si:exit)))
362 @end lisp
363
364 Here the @code{:epilogue-code} is executed after loading our library;
365 we can use arbitrary Lisp forms here. You can also put this code in
366 your Lisp files and directly build them without this
367 @code{:epilogue-code} option to achieve the same result. Running the
368 program in a console will display the following and exit:
369
370 @example
371 Factorial of 5 is: 120
372 @end example
373
374 @node Build it as shared library and use in C
375 @subsubsection Build it as shared library and use in C
376
377 Use this in the REPL to make a shared library:
378
379 @lisp
380 (asdf:make-build :example-with-dep
381 :type :shared-library
382 :move-here #P"./"
383 :monolithic t
384 :init-name "init_dll_example")
385 @end lisp
386
387 Here @code{:monolithic t} means that ECL will compile the library and
388 all its dependencies into a single library named
389 @code{example-with-dep--all-systems.so}. The @code{:move-here}
390 parameter is self-explanatory. @code{:init-name} sets the name of the
391 initialization function. Each library linked from C/C++ code must be
392 initialized, and this is a mechanism to specify the initialization
393 function's name.
394
395 To use it, we write a simple C program:
396
397 @example
398 @verbatim
399 /* test.c */
400 #include <ecl/ecl.h>
401 extern void init_dll_example(cl_object);
402
403 int main (int argc, char **argv) {
404
405 cl_boot(argc, argv);
406 ecl_init_module(NULL, init_dll_example);
407
408 /* do things with the Lisp library */
409 cl_eval(c_string_to_object("(example:test-function 5)"));
410
411 cl_shutdown();
412 return 0;
413 }
414 @end verbatim
415 @end example
416
417 Compile the file using a standard C compiler (note we're linking to
418 @code{libecl.so} with @code{-lecl}, which provides the lisp
419 runtime@footnote{You may also link ECL runtime statically. That is not
420 covered in this walkthrough.}):
421
422 @example
423 gcc test.c example-with-dep--all-systems.so -o test -lecl
424 @end example
425
426 If ECL is installed in a non-standard location you may need to provide
427 flags for the compiler and the linker. You may read them with:
428
429 @example
430 ecl-config --cflags
431 ecl-config --libs
432 @end example
433
434 Since our shared object is not in the standard location, you need to
435 provide @code{LD_LIBRARY_PATH} pointing to the current directory to
436 run the application:
437
438 @example
439 LD_LIBRARY_PATH=`pwd` ./test
440 @end example
441
442 This will show:
443
444 @example
445 Factorial of 5 is: 120
446 @end example
447
448 You can also build all dependent libraries separately as a few
449 @code{.so} files and link them together. For example, if you are
450 building a library called @code{complex-example}, that depends on
451 @code{alexandria} and @code{cl-fad}, you can do the following (in the
452 REPL):
453
454 @lisp
455 (asdf:make-build :complex-example
456 :type :shared-library
457 :move-here #P"./"
458 :init-name "init_example")
459
460 (asdf:make-build :alexandria
461 :type :shared-library
462 :move-here #P"./"
463 :init-name "init_alexandria")
464
465 (asdf:make-build :cl-fad
466 :type :shared-library
467 :move-here #P"./"
468 :init-name "init_fad")
469
470 (asdf:make-build :bordeaux-threads
471 :type :shared-library
472 :move-here #P"./"
473 :init-name "init_bt")
474 @end lisp
475
476 Note that we haven't specified @code{:monolithic t}, so we need to build
477 @code{bordeaux-threads} as well because @code{cl-fad} depends on it. The
478 building sequence doesn't matter and the resultant @code{.so} files can
479 also be used in your future programs if these libraries are not
480 modified.
481
482 We need to initialize all these modules using @coderef{ecl_init_module}
483 in the correct order. (@code{bordeaux-threads} must be initialized
484 before @code{cl-fad}; @code{cl-fad} and @code{alexandria} must be
485 initialized before @code{complex-ecample}.)
486
487 Here is a code snippet (not a full program):
488
489 @example
490 extern void init_fad(cl_object);
491 extern void init_alexandria(cl_object);
492 extern void init_bt(cl_object);
493 extern void init_example(cl_object);
494
495 /* call these *after* cl_boot(argc, argv);
496 if B depends on A, you should first init A then B. */
497 ecl_init_module(NULL, init_bt);
498 ecl_init_module(NULL, init_fad);
499 ecl_init_module(NULL, init_alexandria);
500 ecl_init_module(NULL, init_example);
501 @end example
502
503 @node Build it as static library and use in C
504 @subsubsection Build it as static library and use in C
505
506 To build a static library, use:
507
508 @lisp
509 (asdf:make-build :example-with-dep
510 :type :static-library
511 :move-here #P"./"
512 :monolithic t
513 :init-name "init_example")
514 @end lisp
515
516 This will generate @code{example-with-dep--all-systems.a} in the
517 current directory which we need to initialize with the
518 @code{init_example} function. Compile it using:
519
520 @example
521 gcc test.c example-with-dep--all-systems.a -o test-static -lecl
522 @end example
523
524 Then run it:
525
526 @example
527 ./test-static
528 @end example
529
530 This will show:
531
532 @example
533 Factorial of 5 is: 120
534 @end example
535
536 Note we don't need to pass the current path in @code{LD_LIBRARY_PATH}
537 here, since our Lisp library is statically bundled with the executable.
538 The result is the same as the shared library example above. You can also
539 build all dependent libraries separately as static libraries.
540
541 @node C compiler configuration
542 @subsection C compiler configuration
543
544 ECL provides some global variables to customize which C compiler and
545 compiler options to use:
546
547 @subsubsection Compiler flags
548
549 It is not required to surround the compiler flags with quotes or use
550 slashes before special characters.
551
552 @deftypevar string {c:*user-cc-flags*}
553 Flags and options to be passed to the C compiler when building FASL,
554 shared libraries and standalone programs.
555 @end deftypevar
556
557 @deftypevar string {c:*user-linker-flags*}
558 Flags for options (e.g. @code{-Wl,foo} flags, usually in the
559 @code{$LDFLAGS} variable in autoconf) to be passed to the linker when
560 building FASL, shared libraries and standalone programs.
561 @end deftypevar
562
563 @deftypevar string {c:*user-linker-libs*}
564 Flags for libraries (e.g. @code{-lfoo} flags, usually in the
565 @code{$LIBS} variable in autoconf) to be passed to the linker when
566 building FASL, shared libraries and standalone programs.
567 @end deftypevar
568
569 @deftypevar string {c:*cc-optimize*}
570 Optimize options to be passed to the C compiler.
571 @end deftypevar
572
573 @deftypevar string {c:*user-ld-flags*}
574 @strong{DEPRECATED} Flags and options to be passed to the linker when
575 building FASL, shared libraries and standalone programs.
576 @end deftypevar
577
578 @subsubsection Compiler & Linker programs
579
580 @deftypevar string {c::*cc*}
581 This variable controls how the C compiler is invoked by ECL. One can
582 set the variable appropriately adding for instance flags which the C
583 compiler may need to exploit special hardware features (e.g. a
584 floating point coprocessor).
585 @end deftypevar
586
587 @deftypevar string {c::*ld*}
588 This variable controls the linker which is used by ECL.
589 @end deftypevar
590
591 @deftypevar string {c::*ranlib*}
592 Name of the `ranlib' program on the hosting platform.
593 @end deftypevar
594
595 @deftypevar string {c::*ar*}
596 Name of the `ar' program on the hosting platform.
597 @end deftypevar
598
599 @deftypevar string {c::*ecl-include-directory*}
600 Directory where the ECL header files for the target platform are located.
601 @end deftypevar
602
603 @deftypevar string {c::*ecl-library-directory*}
604 Directory where the ECL library files for the target platform are located.
605 @end deftypevar