File: user-language.sgml

package info (click to toggle)
aap 1.072-1.1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, lenny
  • size: 4,976 kB
  • ctags: 2,160
  • sloc: python: 15,113; makefile: 62; sh: 13
file content (473 lines) | stat: -rw-r--r-- 16,837 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
<!--  vim: set sw=2 sts=2 et ft=docbk:

  Part of the A-A-P recipe executive: Adding a Language

  Copyright (C) 2002-2003 Stichting NLnet Labs
  Permission to copy and use this file is specified in the file COPYING.
  If this file is missing you can find it here: http://www.a-a-p.org/COPYING

-->

<para>
  This chapter explains how to add support for a language to &Aap;.
  Both for your own setup and for a module that is to be included in an &Aap;
  release.
</para>
<para>
  Although the text explains modules for a language, this can also be used for
  adding a module for other purposes.  It is a generic method to have &Aap;
  load a module that defines actions, sets variables, etc.
</para>
<para>
  In a recipe a module can be used with the
  <link linkend="cmd-import">:import</link> command.
  Thus when your recipe uses Java, you would add this line:
</para>
<programlisting>
      :import java
</programlisting>

<para>
  For the standard modules see <xref linkend="ref-modules"/>.
</para>


<bridgehead>Module Name</bridgehead>

<para>
  It is important to chose a good name for the module, because it must be
  unique on your system.  For a programming language simply use the name of
  the language.  For Java you would use "java", for the D language "d".  In
  case there are variants of a language that are different enough to justify
  using a different module, add something to the name to make this clear.
  You can use letters, digits and the underscore.  Examples: "foo", "foo2004",
  "foo_latest".
</para>
<para>
  If you have a specific module with different functionality, you could
  prepend "my".  For example, "myjava".
</para>
<para>
  After the module has been imported, the variables in it can be accessed
  through the "m_name" scope, where "name" is the name of the module.  For
  example the variable "$m_d.DMD" holds the command used for the "d" language
  to execute the compiler.
</para>
<para>
  Note: It is not possible to have a variable and a scope with the same name.
  Thus if you have a module called "foo", which uses the scope "m_foo", you
  cannot have a variable "m_foo" anywhere in a recipe.
</para>
<para>
  Since the modules are &Aap; recipes the file names always end in ".aap".
</para>


<bridgehead>Module directories</bridgehead>

<para>
  Modules that are included with &Aap; are located in the &Aap; sub-directory
  "modules".  If you are working on a module that is to be submitted for a
  next &Aap; release you may also want to put the module there for testing.
  But be aware that this directory may be overwritten or deleted when updating
  &Aap;!
</para>

<para>
  Modules that you want to share with all users on your system should be
  placed in a system-wide directory.  For Unix this usually is
  <filename>/usr/local/share/aap/modules</filename>.  This can be changed with
  <link linkend="var-aapsysdir">$AAPSYSDIR</link>.
</para>

<para>
  Modules that you only use for yourself are placed in
  <filename>$HOME/.aap/modules</filename> (for Unix and Mac OS X),
  <filename>$HOME/aap/modules/</filename>,
  <filename>$HOMEDRIVE/$HOMEPATH/aap/modules/</filename> or
  <filename>c:/aap/modules/</filename> (MS-Windows).
</para>

<para>
  If you only want to change a few settings after the module is loaded, you
  can place these in the <filename>modules2</filename> directory. See
  <link linkend="cmd-import">:import</link> for details.
</para>


<bridgehead>Parts Of A Module</bridgehead>

<para>
  In a module you will usually find these parts:
<variablelist>
  <varlistentry>
    <term>(1) filetype recognition</term>
    <listitem><para>How to recognize files used in the module.  For a module
        included with &Aap; this is normally missing, since it can be added to
        the generic Python filetype module.
        See <xref linkend="user-filetype"/>.
    </para></listitem>
  </varlistentry>

  <varlistentry>
    <term>(2) default values</term>
    <listitem>
      <para>
        Give default values to variables used in the commands further on.  The
        user can change these values if he needs to.  It is a good habit to
        put all specific command names, directories and arguments in a
        variable.
      </para>
    </listitem>
  </varlistentry>

  <varlistentry>
    <term>(3) object file suffixes</term>
    <listitem>
      <para>
        If the language uses different suffixes for object files, for static
        or dynamic libraries, this can be specified.
        See <link linkend="lang-suffix">below</link>.
      </para>
    </listitem>
  </varlistentry>

  <varlistentry>
    <term>(4) search for tools</term>
    <listitem>
      <para>
        Usually there are different ways to invoke the compiler on different
        platforms.  And on one platforms various tools may be installed.  The
        <link linkend="cmd-toolsearch">:toolsearch</link> command is used to
        locate the tools.
        See <link linkend="lang-toolsearch">below</link>.
      </para>
    </listitem>
  </varlistentry>

  <varlistentry>
    <term>(5) actions, rules and routes</term>
    <listitem>
      <para>
        The generic methods for compiling and building.
        See <link linkend="lang-actions">below</link>.
      </para>
    </listitem>
  </varlistentry>

</variablelist>
</para>


<bridgehead>Example</bridgehead>

<para>
  This example is for the "D" language.  This module is included with &Aap;.
</para>

<programlisting>
      # (1) Filetype recognition
      :filetype
          suffix d d

      # (2) Default values
      DMD = dmd

      # (3) Object file suffixes
      # dll and lib objects are equal to normal objects.
      _top.D_LIBOBJSUF = $OBJSUF
      _top.D_DLLOBJSUF = $OBJSUF

      # (4) Search for tools
      # Actions are installed for every toolchain that exists.
      # The first one found sets $D_COMPILE_ACTION and friends.
      :toolsearch dmd

      # (5) Actions, Rules and Routes

      # :do compile

      :action compile object,libobject,dllobject,default d
          @if not _no.get("target"):
              target = `src2obj(fname)`
          # Use the d_build action for building and also for :dll and :lib.
          :attr {buildaction = d_build} $target
          @if _no.get("D_COMPILE_ACTION"):
              :do $D_COMPILE_ACTION {target = $target} $source
          @else:
              :sys $DMD $?DFLAGS -of$target -c $source

      :rule {global} {default} %$OBJSUF : {buildcheck = $DMD $?DFLAGS } %.d
          :do compile {target = $target} $source

      # :do build for object files resulting from "d" source files.

      :action d_build default
          @if _no.get("D_BUILD_ACTION"):
              :do $D_BUILD_ACTION {target = $target} $source
          @else:
              :sys $DMD $?DLINKFLAGS -of$target $source

      # default route

      :route {default} d object,libobject,dllobject
           compile
</programlisting>


<bridgehead id="lang-suffix">Object File Suffixes</bridgehead>

<para>
  Many languages use the same object file suffixes as used for C: ".o" on Unix
  and ".obj" on MS-Windows.  But for some languages a different suffix is
  used and the suffixes for building a static and shared library are
  different.
</para>

<para>
  The simplest example is when a language always uses the same suffix for all
  kinds of object files.  For example, the "d" language does not make a
  difference between normal object files, static library objects, etc.  This
  can be specified like this:
</para>

<programlisting>
     _top.D_DLLOBJSUF = $OBJSUF
     _top.D_LIBOBJSUF = $OBJSUF
</programlisting>

<para>
  Note that the "_top" scope is used.  If this would be left out the variables
  would only be set in the scope of the module and would not be available in
  the user recipe.
</para>

<para>
  The name of the variable is made from the filetype in upper case, and
  underscore and the name of the generic variable.
  The <link linkend="python-src2obj">src2obj()</link> function will look for
  these variables.
  Note that the <emphasis>filetype</emphasis> is used, not the module name!
</para>

<para>
  An extreme example is when all suffixes are different:
</para>

<programlisting>
     _top.FOO_OBJSUF = .fo
     _top.FOO_DLLOBJSUF = .dfo
     _top.FOO_LIBOBJSUF = .lfo
     _top.FOO_LTOBJSUF = .tfo
</programlisting>

<para>
  An advantage of using a different suffix for every type of object file is
  that all types can be generated in the same directory.
</para>

<para>
  If the compiler or linker does not support using different suffixes, but the
  various object files are different, the user must make sure that the object
  files are put in different build directories.  This can be done by
  specifying an attribute on the source files to use a different build
  directory:
</para>

<programlisting>
    :program foo : foo.c                    # uses $BDIR
    :dll foo : foo.c {var_BDIR = $BDIR-dll}
    :lib foo : foo.c {var_BDIR = $BDIR-lib}
</programlisting>


<bridgehead id="lang-toolsearch">Searching For Tools</bridgehead>

<para>
  When there is only one compiler that always uses the same (type of)
  arguments you can simply invoke it directly from the actions in the module.
  Otherwise, &Aap; can search for tools that are currently installed.
  The working of tools is explained in <xref linkend="user-tools"/>
</para>

<para>
  As an example, let's look at how &Aap; searches for C compilers:
</para>

<programlisting>
      @if osname() == 'mswin':
          :toolsearch msvc mingw gcc icc bcc
      @elif osname() == 'os2':
          :toolsearch icc gcc msvc
</programlisting>

<para>
  The <link linkend="cmd-toolsearch">:toolsearch</link> command is used with a
  list of tools that need to be checked for.  The first tool for which the
  exists() function returns True is then used.  This works by setting
  variables to the name of the action to be used.  For example, the "msvc"
  tool sets $C_COMPILE_ACTION to "compile_msvc".
</para>
<para>
  The list of tools to be searched depends on the platform.  Note that the
  "msvc" tool is the first choice for MS-Windows, but the last choice for
  OS/2.  This ordering is based on what works best for most people.
</para>
<para>
  Note that for Unix systems there is no search for tools.  That is because
  the default command for the C compiler "always" works there.
</para>
<para>
  There is one trick you should be aware of, which is using the "buildaction"
  attribute on object files.  When a specific compiler is used to turn a
  source file into an object file, this often means a specific action needs to
  be used to turn this object file into a program or library.  The
  compile action must then add a "buildaction" attribute to the object file,
  so that the generic build action can inspect the object files and invoke the
  proper build action.  You can see this in the example above: The compile
  action for the "d" language contains:
</para>
<programlisting>
          # Use the d_build action for building and also for :dll and :lib.
          :attr {buildaction = d_build} $target
</programlisting>
<para>
  The action is not executed when the target is already up-to-date.  Therefore
  the "buildaction" attribute should also be set by the dependency or rule,
  using a ">always" section.  See <xref linkend="build-sections"/>.
  Example:
</para>
<programlisting>
          :rule %$OBJSUF : $.cpp
              >always
                  :attr {buildaction = cxx_build} $target
              >build
                  :do compile {target = $target} $source
</programlisting>
<para>
  Note that the generic build action cannot handle the situation that object
  files with different "buildaction" attributes are linked together.  And
  object files that do not have a "buildaction" attribute are taken along,
  &Aap; assumes they can be linked together with the object files that specify
  the "buildaction" attribute.  If this does not do what you intend, you can
  specify another build action to use with $BUILD_ACTION.  Similarly,
  $BUILDDLL_ACTION and $BUILDLIB_ACTION are used for building libraries.  Look
  in the distributed <filename>default.aap</filename> for the details.
</para>


<bridgehead id="lang-actions">Actions, Rules and Routes</bridgehead>

<para>
  Actions are used to specify how files are to be compiled and build into a
  program or library.  Rules are used to specify which actions to use for
  files matching a pattern.  Routes are used to specify which actions to use
  to turn one filetype into another.  Thus these three items are closely
  related.
</para>

<para>
  The most obvious thing for a language module is to define an action for
  compiling the language.  Here is an excerpt from the "d" module:
</para>

<programlisting>
      :action compile object,libobject,dllobject,default d
          @if not _no.get("target"):
              target = `src2obj(fname)`
          # Use the d_build action for building and also for :dll and :lib.
          :attr {buildaction = d_build} $target
          @if _no.get("D_COMPILE_ACTION"):
              :do $D_COMPILE_ACTION {target = $target} $source
          @else:
              :sys $DMD $?DFLAGS -of$target -c $source
</programlisting>

<para>
  This action is specified for all kind of object files, including dllobject
  and libobject.  If compiling for a library has to be done differently you
  should specify a separate action for libobject and/or dllobject.
</para>

<para>
  The action starts with checking if $target is set.  In rare cases it isn't
  set and needs to be derived from $source.  Since the object file suffixes
  have been defined (see <link linkend="lang-suffix">above</link>) calling
  <link linkend="python-src2obj">src2obj()</link> takes care of this.
  $fname is the first item in $source.
</para>

<para>
  When a D file has been compiled into an object file it needs to be build in
  a special way.  To accomplish this the "buildaction" attribute of the target
  is set to "d_build".  This action is also defined in the D module.
</para>

<para>
  The check for $D_COMPILE_ACTION takes care of invoking the action that was
  defined by a tool.  The first tool found with
  <link linkend="cmd-toolsearch">:toolsearch</link> will set this variable.
  It can later be changed with a
  <link linkend="cmd-usetool">:usetool</link> command.
</para>

<para>
  When $D_COMPILE_ACTION is not set, the default command to compile a D file
  is executed with <link linkend="cmd-sys">:sys</link>.  This happens when no
  appropriate tool could be found.  Often this is used for Unix, where the
  compile command is the same for various compilers.  The user may set
  $m_d.DMD to the name of the compiler to be used and set $DFLAGS
  to additional arguments.  What could be added here is using $DINCLUDE and
  $DDEFINE, like what <link linkend="var-include">$INCLUDE</link> and
  <link linkend="var-define">$DEFINE</link> do for C.
  A compiler may also use <link linkend="var-debug">$DEBUG</link> and
  <link linkend="var-optimize">$OPTIMIZE</link>.  Note that these can't be
  directly used as an argument but must be translated.
</para>

<para>
  The other actions defined in the module are similar.  You can see in the
  example for the D module how the build action is defined:
</para>

<programlisting>
      :action d_build default
          @if _no.get("D_BUILD_ACTION"):
              :do $D_BUILD_ACTION {target = $target} $source
          @else:
              :sys $DMD $?DLINKFLAGS -of$target $source
</programlisting>

<para>
  Defining a rule is optional.  The
  <link linkend="cmd-rule">:rule</link> command selects the source and target
  based on a match of the file name with a pattern.  This is useful when
  a list of source files is defined without telling how they are to be turned
  into object files.  This is the way the rule is defined in the D module:
</para>

<programlisting>
      :rule {global} {default} %$OBJSUF : {buildcheck = $DMD $?DFLAGS } %.d
          :do compile {target = $target} $source
</programlisting>

<para>
  The {global} option is needed to make the rule available everywhere
  (otherwise it would only work in the module recipe).  The {default} option
  allows overruling this rule without getting a warning.
</para>

<para>
  The <link linkend="cmd-route">:route</link> command is used to tell &Aap;
  how to turn a source file into an object file.  This is used for the
  <link linkend="cmd-program">:program</link>,
  <link linkend="cmd-dll">:dll</link> and
  <link linkend="cmd-lib">:lib</link> commands.  For the D language it's
  simply a matter of invoking the compile action.  for other languages it may
  consist of several steps with intermediate results.
</para>

<programlisting>
      :route {default} d object,libobject,dllobject
           compile
</programlisting>