Debian Codegraph @

src/doc/old-manual/asdf.xmlf

raw
1 <?xml version="1.0" encoding="utf-8"?>
2 <!DOCTYPE book [
3 <!ENTITY % eclent SYSTEM "ecl.ent">
4 %eclent;
5 ]>
6 <book xmlns="http://docbook.org/ns/docbook" version="5.0" xml:lang="en">
7 <chapter xml:id="ext.asdf">
8 <title>System building</title>
9
10 <section xml:id="ext.asdf.intro">
11 <title>Introduction</title>
12
13 <para>A typical application will consist of multiple lisp files that have to
14 be compiled and which will probably be linked to additional, third party
15 libraries, either written themselves in &CommonLisp; or shipped as foreign C
16 or C++ dynamically or statically linked lirbaries. Not only loading these
17 files into a running &ECL; can be a slow process in some platforms, but
18 shipping code in the form of multiple binaries and a script to load them is
19 far from optimal.</para>
20
21 <para>Traditionally, &CommonLisp; implemenations have provided a function to
22 save the dump all data from a running Lisp process into a file. The result
23 was called the Lisp image and could be shipped to other version compatible
24 implementations.Nowadays, having less control of the systems it runs in, a Lisp
25 implementation must work very hard to dump memory images and be able to load
26 and execute them afterwards.</para>
27
28 <para>&ECL; has chosen to avoid this process entirely. Instead, we conceive
29 five different portable models for building and shippin your programs. The
30 models, described in <xref linkend="table.make-build"/>, enumerate the
31 different kinds of files that &ECL; can portably produce. To get one or more
32 of the products mentioned in the table, you may resort to a low level
33 <acronym>API</acronym> described in <xref
34 linkend="part.internals"/>. However, we recommend a simpler way based on
35 using System Definition Files to describe the structure of your project and
36 let &ECL; build the desired target for you. This approach is described in the
37 following sections.</para>
38
39 <table xml:id="table.make-build">
40 <title>Code distribution models</title>
41 <tgroup cols="4">
42 <thead>
43 <row>
44 <entry>Model</entry>
45 <entry>Description</entry>
46 <entry><symbol>:TYPE</symbol></entry>
47 <entry><symbol>:MONOLITHIC</symbol></entry>
48 </row>
49 </thead>
50 <tbody>
51 <row>
52 <entry>Source code</entry>
53 <entry><para>You distribute your programs in source code form. This is
54 the easiest and most portable way, but not the fastest
55 one.</para></entry>
56 <entry>NA</entry>
57 <entry>NA</entry>
58 </row>
59 <row>
60 <entry>&FASL; or loadable file</entry>
61 <entry><para>Best suited for development. You translate all lisp code to
62 C and link it against possibly other C/C++ libraries to obtain a single
63 binary file with extension <filename>.fas</filename>, like the compiled
64 files you obtain from using <function>compile-file</function>. This
65 "unified" &FASL; can be loaded a startup time to add new functionality to
66 the &ECL; environment.</para></entry>
67 <entry><symbol>:FASL</symbol></entry>
68 <entry><symbol>T</symbol>/<symbol>NIL</symbol></entry>
69 </row>
70 <row>
71 <entry>Standalone program</entry>
72 <entry><para>Product shipping for final user. You translate all your lisp
73 code to C using the &ECL; compiler. The final object files can be linked
74 against other C/C++ libraries to obtain a standalone executable.</para></entry>
75 <entry><symbol>:PROGRAM</symbol></entry>
76 <entry>T</entry>
77 </row>
78 <row>
79 <entry>Statically linked library</entry>
80 <entry><para>For embedding purposes. You translate all your lisp code to
81 C and combine the resulting object files into a single library with
82 <filename>.a</filename> or <filename>.lib</filename> extension. You can
83 distribute this library to other people and the final users can utilize
84 these libraries to build standalone programs.</para></entry>
85 <entry><symbol>:LIB</symbol></entry>
86 <entry><symbol>T</symbol>/<symbol>NIL</symbol></entry>
87 </row>
88 <row>
89 <entry>Dynamically linked library</entry>
90 <entry><para>For embedding purposes. Similar to a statically linked
91 library, but it is loaded at run time by the operating system and can be
92 shared by more than one instance of a program.</para></entry>
93 <entry><symbol>:LIB</symbol></entry>
94 <entry><symbol>T</symbol>/<symbol>NIL</symbol></entry>
95 </row>
96 </tbody>
97 </tgroup>
98 </table>
99
100 </section>
101
102 <section xml:id="ext.asdf.sdf">
103 <title>System definition files</title>
104
105 <para>A System Definition File, or just <emphasis>system</emphasis>, is the
106 lisp equivalent of a makefile in the Unix world: it contains a list of source
107 files which are to be loaded or compiled, and dependencies among them ("load
108 source <filename>file1.lsp</filename> before compiling
109 <filename>file2.lsp</filename>", etc).</para>
110
111 <para>It is difficult to tell about the Lisp Machines history, but probably
112 the first most popular system definition format was called
113 <application>mk-defsystem</application> or simply
114 <application>defsystem</application>. Written by Mark Kantrowitz [<xref
115 linkend="bib.mk-defsystem"/>], this library now lives in the <ulink
116 url="https://sourceforge.net/projects/clocc/">CLOCC</ulink> repository and is
117 actively maintained. &ECL; ships with a copy of the version 3.x which
118 fortunately has no customizations. You can load this copy by issuing
119 <code>(require 'defsystem)</code> from the lisp toplevel.</para>
120
121 <para>However, in the last years, Another System Definition Facility known as
122 &ASDF; has become even more popular in the &CommonLisp; world. This new
123 format simplifies writing extensions to handle new kind of source files and
124 integrates very well with the package management utility known as
125 <application>ASDF-install</application>. &ASDF; has a slightly different
126 syntax from <application>mk-defsystem 3.0</application>, but because of
127 reasons of popularity and better integration with &ECL;, in this manual we
128 have focused on this particular library.</para>
129
130 <para>A simple &ASDF; definition looks as follows:</para>
131 <programlisting>
132 (defsystem test
133 :source-pathname "~/src/test/"
134 :source-extension "lisp"
135 :components ((:module file1
136 :source-pathname "")
137 (:module file2
138 :source-pathname ""
139 :depends-on (file1))))</programlisting>
140 <para>This example consists of two files, <filename>file1.lisp</filename> and
141 <filename>file2.lisp</filename>, located in
142 <filename>~/src/test/</filename>. When compiling these files,
143 <filename>file1.lisp</filename> will be processed before
144 <filename>file2.lisp</filename>, because the second depends on the former
145 one. There are more complex rules that allow a system to depend on others,
146 and to contain other kind of files, such as C or Java binaries. For further
147 information we recommend reading <ulink
148 url="https://common-lisp.net/project/asdf/asdf.html">the online
149 manual</ulink>.</para>
150
151 <para>You can load &ASDF; on a running &ECL; using a single lisp statement
152 <code>(require 'asdf)</code>. Once loaded, &ASDF; will extend the function
153 <function>require</function> to recognize and load libraries that are placed
154 in standard locations or which have been registered with &ASDF; itself. The
155 following sections describe other features of &ASDF; which are specific to
156 &ECL; and related to the code building and shipping mechanisms introduced
157 before.</para>
158 </section>
159
160 <section xml:id="ext.asdf.make-build">
161 <title>Practical examples</title>
162
163 <para>The version of &ASDF; which is shipped with &ECL; has been further
164 customized to allow building all the binary files mentioned in <xref
165 linkend="table.make-build"/>. The procedure to do this is documented in a
166 detailed and formal manual page for <xref
167 linkend="ref.make-build"/>. However, since practice is the best teacher, we
168 will show a couple of examples of how to use this function before moving into
169 the formal specification.</para>
170
171 <para>In <filename>/ecl/examples/asdf</filename> you will find a very simple
172 example that can be built in different forms. The example is built around a
173 system definition file that depends on two sources,
174 <filename>file1.lisp</filename> and <filename>file2.lisp</filename>:</para>
175 <programlisting>
176 (defsystem #:example
177 :serial t
178 :components ((:file "file1")
179 (:file "file2")))</programlisting>
180
181 <para>We can built these files into a single &FASL; file, as shown
182 below. Notice how there is a single file with the name
183 <filename>*.fas</filename>, but there are two object files generated from
184 their respective sources, <filename>file1.o</filename>,
185 <filename>file2.o</filename>.</para>
186 <screen>
187 &gt; (require 'asdf)
188 ;;; Loading #P"/home/jlr/lib/ecl/asdf.fas"
189 ("ASDF")
190 &gt; (asdf:make-build :example :type :fasl)
191 ...
192 NIL
193 &gt; (directory "*.o")
194 (#P"/home/jlr/src/ecls-new/examples/asdf/file2.o"
195 #P"/home/jlr/src/ecls-new/examples/asdf/file1.o")
196 &gt; (directory "*.fas")
197 (#P"/home/jlr/src/ecls-new/examples/asdf/example.fas")
198 &gt; (load "example.fas")
199 ;;; Loading "/home/jlr/src/ecls-new/examples/asdf/example.fas"
200 ======================================================================
201 We are now executing FILE1.LSP
202 TEST-FUNCTION has been created
203 We are now executing FILE2.LSP
204 Calling TEST-FUNCTION in FILE2.LSP
205 1 + 1 is equal to 2
206 Finished
207 ======================================================================
208 "/home/jlr/src/ecls-new/examples/asdf/example.fas"
209 </screen>
210
211 <para>The previous sources may be combined into a single program, as shown
212 below. Notice that we choose to execute <function>ext:quit</function> right
213 after all compiled files have run. If you do not supply this parameter,
214 <filename>example</filename> will jump to the lisp toplevel right after
215 that.</para>
216 <screen>
217 &gt; (asdf:make-build :example :type :program
218 :epilogue-code '(ext:quit 0))
219 NIL
220 &gt; (ext:system "./example")
221 ======================================================================
222 We are now executing FILE1.LSP
223 TEST-FUNCTION has been created
224 We are now executing FILE2.LSP
225 Calling TEST-FUNCTION in FILE2.LSP
226 1 + 1 is equal to 2
227 Finished
228 ======================================================================</screen>
229
230 </section>
231
232 <section xml:id="ext.asdf.dict">
233 <title>ASDF Reference</title>
234
235 <!-- ====================================================================== -->
236 <!-- ASDF:MAKE-BUILD -->
237 <!-- ====================================================================== -->
238
239 <refentry xml:id="ref.make-build">
240 <refnamediv>
241 <refname><function>asdf:make-build</function></refname>
242 <refpurpose>Block-build an &ASDF; system definition</refpurpose>
243 </refnamediv>
244
245 <refsynopsisdiv>
246 <title>Function</title>
247 <funcsynopsis>
248 <funcprototype>
249 <funcdef>asdf:make-build</funcdef>
250 <paramdef><parameter>system-name</parameter></paramdef>
251 <paramdef>&key;</paramdef>
252 <paramdef><parameter>type</parameter></paramdef>
253 <paramdef><parameter>monolithic</parameter></paramdef>
254 <paramdef><parameter>ld-flags</parameter></paramdef>
255 <paramdef><parameter>prologue-code</parameter></paramdef>
256 <paramdef><parameter>epilogue-code</parameter></paramdef>
257 <paramdef>&allow-other-keys;</paramdef>
258 </funcprototype>
259 </funcsynopsis>
260 <variablelist>
261 <varlistentry>
262 <term><replaceable>system-name</replaceable></term>
263 <listitem><para>A symbol naming the system to be built. Only the symbol
264 name is considered.</para></listitem>
265 </varlistentry>
266 <varlistentry>
267 <term><replaceable>type</replaceable></term>
268 <listitem><para>One of <symbol>:FASL</symbol>, <symbol>:DLL</symbol>,
269 <symbol>:LIB</symbol> or
270 <symbol>:PROGRAM</symbol></para></listitem>
271 </varlistentry>
272 <varlistentry>
273 <term><replaceable>monolithic</replaceable></term>
274 <listitem><para>A boolean value.</para></listitem>
275 </varlistentry>
276 <varlistentry>
277 <term><replaceable>ld-flags</replaceable></term>
278 <listitem><para>A list of strings.</para></listitem>
279 </varlistentry>
280 <varlistentry>
281 <term><replaceable>prologue-code</replaceable></term>
282 <listitem><para>A string.</para></listitem>
283 </varlistentry>
284 <varlistentry>
285 <term><replaceable>epilogue-code</replaceable></term>
286 <listitem><para>A string or a lisp form.</para></listitem>
287 </varlistentry>
288 </variablelist>
289 </refsynopsisdiv>
290
291 <refsect1>
292 <title>Description</title>
293
294 <para>This function takes a system definition which is known to &ASDF; and
295 builds one or more binary files, depending on the arguments. The possible
296 output files depend on the value of <varname>type</varname> and are
297 summarized in <xref linkend="table.make-build"/>.</para>
298
299 <para>Internally the function works similary to the &ASDF; function
300 <function>asdf:operate</function> with the
301 <function>asdf:load-op</function> operator. It finds out the requested
302 system definition, either by searching in a set of predefined locations or
303 because the system has been already loaded into memory, computes all
304 libraries and components this system depends on, builds them and then
305 produces the desired output.</para>
306
307 <para>If the value of <varname>:monolithic</varname> is
308 <varname>NIL</varname> the output binary will contain just the desired
309 system, while in other cases the output will be linked together with all
310 libraries your system depends on. Standalone executables, as specified by
311 <code>type = :program</code>, are always monolithic, even without specifing
312 this flag. All other systems might be non-monolithic, but in that case you
313 will have to manually satisfy the required dependencies when using those
314 files (unless you use <symbol>asdf:load-bundle-op</symbol> in which case
315 asdf will satisfy required dependencies for you automatically).</para>
316
317 <para>This function takes additional values which are related to the low
318 level details of the produced binaries. First of all we find
319 <varname>ld-flags</varname>, a list of strings with arguments for the
320 object linker. You will only need this argument if you have to link your
321 programs with foreign libraries.</para>
322
323 <para>The next two arguments represent two pieces of code which are
324 executed before (<varname>prologue-code</varname>) and after
325 (<varname>epilogue-code</varname>) running your lisp code. The prologue
326 code is a string with C code which you will typically use to initialize
327 foreign libraries. It can only be C code because this code may be executed
328 even before &ECL; itself is initialized.</para>
329
330 <para>The epilogue code, on the other hand, can be either a string with C
331 statements or a lisp form represented as a list. In the case of executables
332 it conveniently defaults to a call to the toplevel
333 <code>(SI::TOP-LEVEL)</code>, while in the case of libraries and &FASL;
334 files it is left empty.</para>
335
336 </refsect1>
337
338 <refsect1>
339 <title>Examples</title>
340
341 <para>See <xref linkend="ext.asdf.make-build"/>.</para></refsect1>
342 </refentry>
343
344 <!-- ====================================================================== -->
345 <!-- ASDF:MAKE-BUILD -->
346 <!-- ====================================================================== -->
347
348 <refentry xml:id="ref.load-bundle-op">
349 <refnamediv>
350 <refname><function>asdf:load-bundle-op</function></refname>
351 <refpurpose>Compile and load one ore more libraries using unified &FASL;</refpurpose>
352 </refnamediv>
353
354 <refsynopsisdiv>
355 <title>&ASDF; operator</title>
356 <funcsynopsis>
357 <funcprototype>
358 <funcdef>asdf:make-build</funcdef>
359 <paramdef>'asdf:load-bundle-op</paramdef>
360 <paramdef><parameter>system-name</parameter></paramdef>
361 <paramdef>&key;</paramdef>
362 <paramdef>&allow-other-keys;</paramdef>
363 </funcprototype>
364 </funcsynopsis>
365 <variablelist>
366 <varlistentry>
367 <term><replaceable>system-name</replaceable></term>
368 <listitem><para>A symbol naming the system to be built. Only the symbol
369 name is considered.</para></listitem>
370 </varlistentry>
371 </variablelist>
372 </refsynopsisdiv>
373
374 <refsect1>
375 <title>Description</title>
376
377 <para>This function is a replacement for the &ASDF; operator
378 <symbol>ASDF:LOAD-OP</symbol>. Given a system name, it will build it and
379 all its dependencies, to load them in the required order. The only
380 difference with respect to <symbol>ASDF:LOAD-OP</symbol> is that it builds
381 a single &FASL; file per module, thus being potentially faster and more
382 resource efficient.</para>
383 </refsect1>
384
385 <refsect1>
386 <title>Examples</title>
387
388 <para>Assume you want to load the &CFFI; library, which has been registered
389 with &ASDF;. You will simply type</para>
390 <screen>
391 &gt; (require 'asdf)
392 ;;; Loading #P"/home/jlr/lib/ecl/asdf.fas"
393 ("ASDF")
394 &gt; (asdf:operate 'asdf:load-bundle-op :cffi)
395 ...
396 T
397 </screen>
398 </refsect1>
399 </refentry>
400
401 </section>
402 </chapter>
403 </book>
404 <!-- Keep this comment at the end of the file
405 Local variables:
406 mode: nxml
407 sgml-parent-document: "ecl.xml"
408 sgml-indent-step: 1
409 nxml-child-indent: 1
410 nxml-outline-child-indent: 1
411 fill-column: 79
412 End:
413 -->