autoconf: Runtime

1 
1 6.6 Checking Runtime Behavior
1 =============================
1 
1 Sometimes you need to find out how a system performs at runtime, such
1 as whether a given function has a certain capability or bug.  If you
1 can, make such checks when your program runs instead of when it is
1 configured.  You can check for things like the machine's endianness when
1 your program initializes itself.
1 
1    If you really need to test for a runtime behavior while configuring,
1 you can write a test program to determine the result, and compile and
1 run it using `AC_RUN_IFELSE'.  Avoid running test programs if possible,
1 because this prevents people from configuring your package for
1 cross-compiling.
1 
1  -- Macro: AC_RUN_IFELSE (INPUT, [ACTION-IF-TRUE], [ACTION-IF-FALSE],
1           [ACTION-IF-CROSS-COMPILING = `AC_MSG_FAILURE'])
1      Run the compiler (and compilation flags) and the linker of the
1      current language (⇒Language Choice) on the INPUT, then
1      execute the resulting program.  If the program returns an exit
1      status of 0 when executed, run shell commands ACTION-IF-TRUE.
1      Otherwise, run shell commands ACTION-IF-FALSE.
1 
1      The INPUT can be made by `AC_LANG_PROGRAM' and friends.  `LDFLAGS'
1      and `LIBS' are used for linking, in addition to the compilation
1      flags of the current language (⇒Language Choice).
1      Additionally, ACTION-IF-TRUE can run `./conftest$EXEEXT' for
1      further testing.
1 
1      In the ACTION-IF-FALSE section, the failing exit status is
1      available in the shell variable `$?'.  This exit status might be
1      that of a failed compilation, or it might be that of a failed
1      program execution.
1 
1      If cross-compilation mode is enabled (this is the case if either
1      the compiler being used does not produce executables that run on
1      the system where `configure' is being run, or if the options
1      `--build' and `--host' were both specified and their values are
1      different), then the test program is not run.  If the optional
1      shell commands ACTION-IF-CROSS-COMPILING are given, those commands
1      are run instead; typically these commands provide pessimistic
1      defaults that allow cross-compilation to work even if the guess
1      was wrong.  If the fourth argument is empty or omitted, but
1      cross-compilation is detected, then `configure' prints an error
1      message and exits.  If you want your package to be useful in a
1      cross-compilation scenario, you _should_ provide a non-empty
1      ACTION-IF-CROSS-COMPILING clause, as well as wrap the
1      Caching Results::) which allows the user to override the
1      pessimistic default if needed.
1 
1      It is customary to report unexpected failures with
1      `AC_MSG_FAILURE'.
1 
1    `autoconf' prints a warning message when creating `configure' each
1 time it encounters a call to `AC_RUN_IFELSE' with no
1 ACTION-IF-CROSS-COMPILING argument given.  If you are not concerned
1 about users configuring your package for cross-compilation, you may
1 ignore the warning.  A few of the macros distributed with Autoconf
1 produce this warning message; but if this is a problem for you, please
1 report it as a bug, along with an appropriate pessimistic guess to use
1 instead.
1 
1    To configure for cross-compiling you can also choose a value for
11 those parameters based on the canonical system name (⇒Manual
 Configuration).  Alternatively, set up a test results cache file with
1 the correct values for the host system (⇒Caching Results).
1 
1    To provide a default for calls of `AC_RUN_IFELSE' that are embedded
1 in other macros, including a few of the ones that come with Autoconf,
1 you can test whether the shell variable `cross_compiling' is set to
1 `yes', and then use an alternate method to get the results instead of
1 calling the macros.
1 
1    It is also permissible to temporarily assign to `cross_compiling' in
1 order to force tests to behave as though they are in a
1 cross-compilation environment, particularly since this provides a way to
1 test your ACTION-IF-CROSS-COMPILING even when you are not using a
1 cross-compiler.
1 
1      # We temporarily set cross-compile mode to force AC_COMPUTE_INT
1      # to use the slow link-only method
1      save_cross_compiling=$cross_compiling
1      cross_compiling=yes
1      AC_COMPUTE_INT([...])
1      cross_compiling=$save_cross_compiling
1 
11    A C or C++ runtime test should be portable.  ⇒Portable C and
 C++.
1 
1    Erlang tests must exit themselves the Erlang VM by calling the
1 `halt/1' function: the given status code is used to determine the
1 success of the test (status is `0') or its failure (status is different
1 than `0'), as explained above.  It must be noted that data output
1 through the standard output (e.g., using `io:format/2') may be
1 truncated when halting the VM.  Therefore, if a test must output
1 configuration information, it is recommended to create and to output
1 data into the temporary file named `conftest.out', using the functions
1 of module `file'.  The `conftest.out' file is automatically deleted by
1 the `AC_RUN_IFELSE' macro.  For instance, a simplified implementation
1 of Autoconf's `AC_ERLANG_SUBST_LIB_DIR' macro is:
1 
1      AC_INIT([LibdirTest], [1.0], [bug-libdirtest@example.org])
1      AC_ERLANG_NEED_ERL
1      AC_LANG(Erlang)
1      AC_RUN_IFELSE(
1        [AC_LANG_PROGRAM([], [dnl
1          file:write_file("conftest.out", code:lib_dir()),
1          halt(0)])],
1        [echo "code:lib_dir() returned: `cat conftest.out`"],
1        [AC_MSG_FAILURE([test Erlang program execution failed])])
1