Previous: Test Functions, Up: Writing Test Programs


6.2.3 Generating Sources

Autoconf provides a set of macros that can be used to generate test source files. They are written to be language generic, i.e., they actually depend on the current language (see Language Choice) to “format” the output properly.

— Macro: AC_LANG_CONFTEST (source)

Save the source text in the current test source file: conftest.extension where the extension depends on the current language.

Note that the source is evaluated exactly once, like regular Autoconf macro arguments, and therefore (i) you may pass a macro invocation, (ii) if not, be sure to double quote if needed.

— Macro: AC_LANG_SOURCE (source)

Expands into the source, with the definition of all the AC_DEFINE performed so far.

For instance executing (observe the double quotation!):

     AC_INIT([Hello], [1.0], [bug-hello@example.org])
     AC_DEFINE([HELLO_WORLD], ["Hello, World\n"],
       [Greetings string.])
     AC_LANG(C)
     AC_LANG_CONFTEST(
        [AC_LANG_SOURCE([[const char hw[] = "Hello, World\n";]])])
     gcc -E -dD -o - conftest.c

results in:

     ...
     # 1 "conftest.c"
     
     #define PACKAGE_NAME "Hello"
     #define PACKAGE_TARNAME "hello"
     #define PACKAGE_VERSION "1.0"
     #define PACKAGE_STRING "Hello 1.0"
     #define PACKAGE_BUGREPORT "bug-hello@example.org"
     #define HELLO_WORLD "Hello, World\n"
     
     const char hw[] = "Hello, World\n";

When the test language is Fortran or Erlang, the AC_DEFINE definitions are not automatically translated into constants in the source code by this macro.

— Macro: AC_LANG_PROGRAM (prologue, body)

Expands into a source file which consists of the prologue, and then body as body of the main function (e.g., main in C). Since it uses AC_LANG_SOURCE, the features of the latter are available.

For instance:

     AC_INIT([Hello], [1.0], [bug-hello@example.org])
     AC_DEFINE([HELLO_WORLD], ["Hello, World\n"],
       [Greetings string.])
     AC_LANG_CONFTEST(
     [AC_LANG_PROGRAM([[const char hw[] = "Hello, World\n";]],
                      [[fputs (hw, stdout);]])])
     gcc -E -dD -o - conftest.c

results in:

     ...
     # 1 "conftest.c"
     
     #define PACKAGE_NAME "Hello"
     #define PACKAGE_TARNAME "hello"
     #define PACKAGE_VERSION "1.0"
     #define PACKAGE_STRING "Hello 1.0"
     #define PACKAGE_BUGREPORT "bug-hello@example.org"
     #define HELLO_WORLD "Hello, World\n"
     
     const char hw[] = "Hello, World\n";
     int
     main ()
     {
     fputs (hw, stdout);
       ;
       return 0;
     }

In Erlang tests, the created source file is that of an Erlang module called conftest (conftest.erl). This module defines and exports at least one start/0 function, which is called to perform the test. The prologue is optional code that is inserted between the module header and the start/0 function definition. body is the body of the start/0 function without the final period (see Runtime, about constraints on this function's behavior).

For instance:

     AC_INIT([Hello], [1.0], [bug-hello@example.org])
     AC_LANG(Erlang)
     AC_LANG_CONFTEST(
     [AC_LANG_PROGRAM([[-define(HELLO_WORLD, "Hello, world!").]],
                      [[io:format("~s~n", [?HELLO_WORLD])]])])
     cat conftest.erl

results in:

     -module(conftest).
     -export([start/0]).
     -define(HELLO_WORLD, "Hello, world!").
     start() ->
     io:format("~s~n", [?HELLO_WORLD])
     .
— Macro: AC_LANG_CALL (prologue, function)

Expands into a source file which consists of the prologue, and then a call to the function as body of the main function (e.g., main in C). Since it uses AC_LANG_PROGRAM, the feature of the latter are available.

This function will probably be replaced in the future by a version which would enable specifying the arguments. The use of this macro is not encouraged, as it violates strongly the typing system.

This macro cannot be used for Erlang tests.

— Macro: AC_LANG_FUNC_LINK_TRY (function)

Expands into a source file which uses the function in the body of the main function (e.g., main in C). Since it uses AC_LANG_PROGRAM, the features of the latter are available.

As AC_LANG_CALL, this macro is documented only for completeness. It is considered to be severely broken, and in the future will be removed in favor of actual function calls (with properly typed arguments).

This macro cannot be used for Erlang tests.