Filetypes and Actions

A-A-P home page | A-A-P Recipe Executive
--- | ---
Prev |  | Next

# Chapter 8. Filetypes and Actions

A-A-P can recognize what the type of a file is, either by looking at the file name or by inspecting the contents of the file. The filetype can then be used to decide how to perform an action with the file.

## A New Type of File

Suppose you are using the "foo" programming language and want to use A-A-P to compile your programs. Once this is has been setup you can compile "hello.foo" into the "hello" program with a simple recipe:

        SOURCE = hello.foo
        TARGET = hello

You need to explain Aap how to deal with "foo" files. This is done with a recipe:

        :filetype
            suffix foo foo

        :action compile foo
            :sys foocomp $source -o $target

For Unix, write this recipe as "/usr/local/share/aap/startup/foo.aap" or "~/.aap/startup/foo.aap". The recipes in these "startup" directories are always read when Aap starts up.

Now try it out, using the simple recipe at the top as "main.aap":

% `aap`
Aap: foocomp hello.foo -o build-FreeBSD4_5_RELEASE/hello.o
Aap: cc -L/usr/local/lib -g -O2 -o hello build-FreeBSD4_5_RELEASE/hello.o
%

The "foo.aap" recipe does two things. The `:filetype` command is used to tell A-A-P to recognize your "hello.foo" file as being a "foo" file. The `:action` command is used to specify how the "foocomp" compiler is used to compile a "foo" program into an object file.

## Defining a Filetype by Suffix

The `:filetype` command is followed by the line "suffix foo foo". The first word "suffix" means that recognizing is done by the suffix of the file name (the suffix is what comes after the last dot in the name). The second word is the suffix and the third word is the type. Quite often the type is equal to the suffix, but not always. Here are a few more examples of lines used with `:filetype`:

        :filetype
            suffix fooh foo
            suffix bash sh

It is also possible to recognize a file by matching the name with a pattern, checking the contents of the file or using a Python script. See the user manual.

## Defining a Compile Action

The lower half of "foo.aap" specifies the compile action for the "foo" filetype:

        :action compile foo
            :sys foocomp $source -o $target

The `:action` command has two arguments. The first one specifies the kind of action that is being defined. In this case "compile". This action is used to make an object file from a source file. The second argument specifies the type of source file this action is used for, in this case "foo".

Below the `:action` line the build commands are specified. In this case just one, there could be more. The `:sys` command invokes an exteral program, "foocomp", and passes the arguments. In an action `$source` is expanded to the source of the action and `$target` to the target. These are obtained from the `:do` command that invokes the action. Example:

        :do compile {target = $BDIR/main.$OBJSUF} main.foo

This `:do` command invokes the compile action, specified with its first argument. The target is specified as an attribute to the action, the source is the following argument "main.foo". When executing the `:do` command the filetype of "main.foo" is detected to be "foo", resulting in the compile action for "foo" to be invoked. In the build command of the action `$source` and `$target` are replaced, resulting in:

        :sys foocomp main.foo -o $BDIR/main.$OBJSUF

Note that in many cases `$target` is passed implicitly from a dependency and does not appear in the `:do` command argument.

## Another Use of Filetypes

When building a program you often want to include the date and time when it was build. A simple way of doing this is creating a source file "version.c" that contains the timestamp. This file needs to be compiled every time your program is build. Here is an example how this can be done:

    1   SOURCE =
    2            main.c
    3            work.c
    4   TARGET = prog {filetype = myprog}
    5
    6   :action build myprog object
    7        VERSION_OBJ = $BDIR/version$OBJSUF
    8        :do compile {target = $VERSION_OBJ} version.c
    9        :do build {target = $target {filetype = program}} $source $VERSION_OBJ

The target "prog" is explicitly given a different filetype in line 4. The default filetype for a program is "program", here it is set to "myprog". This allows us to specify a different build action for "prog".

Write the recipe as "main.aap" (without the line numbers) and execute it with `aap`. The first time all the files will be compiled and linked together. Executing `aap` again will do nothing. Thus the timestamp used in "version.c" will not be updated if the files were not changed. If you now make a change in "main.c" and run `aap` you will see that both "main.c" and "version.c" are compiled.

The `:action` command in line 6 has three arguments. The first one "build" is the kind of action, like before. The second argument "myprog" specifies the target filetype, the third one "object" the source filetype. Thus the template is:

`:action` kind-of-action target-filetype source-filetype

This order may seem a bit strange. Remember that putting the target left of the source also happens in a dependency and an assignment.

There are three commands for the build action, lines 7 to 9. The first one assigns the name of the object file for "version.c" to `VERSION_OBJ`. "version.c" was not included in `SOURCE` at the top, it is compiled here explicitly in line 8. This is what makes sure "version.c" is compiled each time "prog" is build. The other source files will be compiled with the default rules used when `SOURCE` and `TARGET` variables are defined.

Finally the `:do build` command in line 9 invokes the build action to link all the object files together. Note that the target for this build action is explicitly defined and its "filetype" attribute is set to "program". This is required for this `:do` command to use the default action for a program target. Otherwise the action would invoke itself, since the filetype for $target is "myprog".

Prev | Contents | Next
Version Control with CVS | Up | More Than One Recipe