ocaml Documentation

The core OCaml system: compilers, runtime system, base libraries

0 stars OCaml OCaml 2 files ยท ~3,984 tokens
RAW Doc

Technical Documentation: bbatsov/ocaml

โ„น๏ธ Provenance: Multi-Tier Ingestion ยท CodeWiki Reference

Overview (`ocamltest/README.md`)

Introduction

This documents how to run the OCaml compiler test suite. To learn how to
write tests using ocamltest, read OCAMLTEST.org.

Context

The testsuite of the OCaml compiler consists of a series of programs
that are compiled and executed. The output of their compilation and
execution is compared to expected outputs.

Before the introduction of ocamltest, the tests were driven by a set of
makefiles which were responsible for compiling and running the test
programs, and verifying that the compilation and execution outputs were
matching the expected ones.

In this set-up, the precise information about how exactly one test
should be compiled was separated from the test itself. It was stored
somewhere in the makefiles, interleaved with the recipes to actually
compile and run the test. Thus, given one test, it was not easy to
determine exactly how this test was supposed to be compiled and run.

Purpose

The ocamltest tool has been introduced to replace most of the makefiles
logic. It takes a test program as its input and derives from annotations
stored as a special comment at the beginning of the program the exact
way to compile and run it. Thus the test-specific metadata are stored in
the test file itself and clearly separated from the machinery required
to perform the actual tasks, which is centralized in the ocamltest tool.

Constraints

It may look odd at first glance to write the tool used to test the
compiler in its target language. There are, however, parts of the
compiler and the standard library that are already tested in a way,
namely those used to compile the compiler itself. Therefore, these
components can be considered more trustworthy than those that have not
yet been used and that's why ocamltest relies only on the part of the
standard library that has been used to develop the compiler itself.

This excludes for instance the use of the Unix and Str libraries.

Initial set-up

ocamltest needs to know two things:

  1. Where the sources of the OCaml compiler to test are located. This is
    determined while OCaml is built. The default location can be overridden
    by defining the OCAMLSRCDIR environment variable.

  2. Which directory to use to build tests. The default value for this is
    "ocamltest" under Filename.get_temp_dir_name(). This value can be
    overridden by defining the OCAMLTESTDIR environment variable.

Running tests

(all the commands below are assumed to be run from
OCAMLSRCDIR/testsuite)

From here, one can:

Run all tests: `make all`

This runs the complete testsuite. This includes the "legacy" tests that
still use the makefile-based infrastructure and the "new" tests that
have been migrated to use ocamltest.

Run legacy tests: `make legacy`

Run new tests: `make new`

Run tests manually

It is convenient to have the following ocamltest script in a directory
appearing in PATH, like ~/bin:

sh
#!/bin/sh
TERM=dumb OCAMLRUNPARAM= /path/to/ocaml/sources/ocamltest/ocamltest $*

Once this file has been made executable, one can for instance run:

sh
ocamltest tests/basic-io/wc.ml

As can be seen, ocamltest's output looks similar to the legacy format.

This is to make the transition between the makefile-based infrastructure
and ocamltest as smooth as possible. Once all the tests will have been
migrated to ocamltest, it will become possible to change this output
format.

The details of what exactly has been tested can be found in
${OCAMLTESTDIR}/tests/basic-io/wc/wc.log

One can then examine tests/basic-io/wc.ml to see how the file had to
be annotated to produce such a result.

Many other tests have already been migrated and it may be useful to see
how the test files have been annotated. the command

sh
find tests -name '*ocamltests*' | xargs cat

gives a list of tests that have been modified and can therefore be used
as starting points to understand what ocamltest can do.

Migrating tests from makefiles to ocamltest

It may be a good idea to run make new from the testsuite directory
before starting to migrate tests. This will show how many "new" tests
there already are.

Then, when running make new after migrating n tests, the number of
new tests reported by make new should have increased by n.

OCaml's testsuite is divided into directories, each of them containing
one or several tests, which can each consist of one or several files.

Thus, the directory is the smallest unit that can be migrated.

To see which directories still need to be migrated, do:

sh
find tests -name 'Makefile'

In other words, the directories that still need to be migrated are the
subdirectories of testsuite/tests that still contain a Makefile.

Once you know which directory you want to migrate, say foo, here is
what you should do:

Read foo/Makefile to see how many tests the directory contains and how
they are compiled. If the makefile only includes other makefiles and
does not define any variable, then it means that nothing special has to
be done to compile or run the tests.

You can also run the tests of this directory with the legacy framework,
to see exactly how they are compiled and executed. To do so, use the
following command from the testsuite directory:

sh
make --trace DIR=tests/foo

(You may want to log the output of this command for future reference.)

For each test, annotate its main file with a test block, i.e. a comment
that looks like this:

ocaml
(* TEST
  Optional variable assignments and tests
*)

In particular, if the test's main file is foo.ml and the test uses
modules m1.ml and m2.ml, the test block will look like this:

ocaml
(* TEST
  modules = "m1.ml m2.ml";
*)

And if the test consists of a single file foo.ml that needs to be run
under the top-level, then its test block will look like this:

ocaml
(* TEST
  toplevel;
*)

Or, if there are two reference files for that test and the name of one
of them contains "principal", then it means the file should be tested
with the top-level, without and with the -principal option. This is
expressed as follows:

ocaml
(* TEST
  toplevel;
  include principal;
  toplevel;
*)

Lines starting with stars indicate which tests to run. If no test is
specified, then the tests that are enabled by default are used, namely
to compile and run the test program in both bytecode and native code
(roughly speaking).

Once your test has been annotated, run ocamltest on it and see whether
it passes or fails. If it fails, see the log file to understand why and
make the necessary adjustments until all the tests pass.

The adjustments will mostly consist in renaming reference files and
updating their content.

Note that there are different types of reference files, those for
compiler output and those for program output.

To make sure the migration has been done correctly, you can compare the
commands used to compile the programs in ocamltest's log file to those
obtained with make --trace. Beware that the commands used to compare
an obtained result to an expected one will not show up in ocamltest's
log file.

Once this has been done for all tests, create a file called ocamltests
(mark the final s!) with the names of all the files that have been
annotated for ocamltest, one per line.

Finally, git rm the Makefile and run make new from the testsuite
directory to make sure the number of new tests has increased as
expected.


Overview (`testsuite/in_prefix/README.md`)

"in-prefix" compiler testing

This directory performs tests on the compiler after it has been installed to the
prefix given to configure. The test requires at least the Unix library to have
been compiled, and only works if bindir and libdir share a common root directory
(e.g. /usr/bin and /usr/lib/ocaml share a common root of /usr, but /bin and
/lib/ocaml do not share a common root directory). The test needs to be able to
rename this common root directory by adding a suffix ".new". For this reason,
the test intentionally uses a non-standard Makefile filename and a specific
target giving make -f Makefile.test -C testsuite/in_prefix test-in-prefix. The
test should always restore the common root directory to its original name.

At its most basic level, the test validates the install target of the build
system. For this reason, the test driver takes the expected configuration of the
compiler as command line parameters, rather than probing it (for example
--with-ocamlopt is passed, rather than relying on the detection of the
ocamlopt binary).

Testing

The test battery consists of five individual tests:

  1. Loading libraries with Dynlink (skipped for --disable-native-compiler)
  2. Loading libraries in the two toplevels (native code skipped for
    --disable-native-toplevel)
  3. Processing of CAML_LD_LIBRARY_PATH and ld.conf by ocamlrun and ocamlc
  4. Executing any bytecode binaries found in bindir with -vnum
  5. Compilation and execution of a print_endline Config.standard_library
    application with the fourteen compilation mechanisms (tests requiring the
    shared runtime are skipped for --disable-shared or on Windows, where it is
    isn't available and native mode tests are skipped for
    --disable-native-compiler):
    • Bytecode/native standard compilation
    • Bytecode compilation with -custom running on the static/shared runtime
    • Bytecode compilation with -output-complete-exe on the static/shared
      runtime
    • Bytecode/native compilation with -output-obj linked with the
      static/shared runtime
    • Bytecode/native compilation with -output-complete-obj linked with the
      static/shared runtime

Having executed this battery on the configured prefix, the test then renames the
common root directory, appending the suffix .new. The programs compiled in the
fifth test are re-run and then the entire battery is executed a second time.

During this second execution, the test harness does whatever is physically
possible to allow these tests to proceed:

  • Environment variables CAML_LD_LIBRARY_PATH and OCAMLLIB are manipulated to
    allow the compiler to operate (unless the compiler has been configured with
    --with-relative-libdir)
  • Bytecode executables which will no longer be able to find ocamlrun are
    explicitly passed to ocamlrun. The harness always verifies that this step is
    required by first executing the binary and ensuring that it fails and then
    passing it directly to ocamlrun.

Tests

In these descriptions, the Shims section describes the adjustments necessary for
the second phase of testing after the common root directory has been renamed.

Loading archives/plugins (.cma / .cmxa / .cmxs) in `ocaml` / `ocamlnat`

Verifies that all the .cma files built from otherlibs/ (dynlink,
runtime_events, str, threads and unix) can be loaded in the two toplevels.
This test is skipped on builds which don't support shared libraries.

Exercises:

  • CAML_LD_LIBRARY_PATH and ld.conf logic (locating C stubs)
  • ocamlnat's conversion machinery converting dynlink.cmxa to dynlink.cmxs
  • CAMLextern header attribute for Windows (RELOC_REL32, etc.)

Shims:

  • On Unix, the bytecode toplevel contains the absolute location of ocamlrun,
    so must be explicitly invoked via ocamlrun, unless the compiler is
    configured with --enable-runtime-search
  • Both toplevels contain the absolute location of the Standard Library,
    requiring OCAMLLIB to be set, unless the compiler was configured with
    --with-relative-libdir

As for the toplevel test, but using the Dynlink library.

Shims:

  • For a bytecode-only build, ocamlc contains the absolute location of
    ocamlrun, so must be explicitly invoked via ocamlrun (if the native
    compiler is available, then both ocamlc and ocamlopt will be native
    executables), unless the compiler is configured with --enable-runtime-search
  • Both compilers contain the absolute location of the Standard Library,
    requiring OCAMLLIB to be set, unless the comnpiler was configured with
    --with-relative-libdir
  • The executable created by ocamlc contains the absolute location of
    ocamlrun, so must be explicitly invoked via ocamlrun, unless the
    compiler is configured with --enable-runtime-search-target

Executing installed bytecode binaries with `-vnum`

This test looks for filenames matching flexlink* and ocaml* in bindir. For
each name matched, if the file ends with the bytecode magic number, then that
program is executed with -vnum.

Additionally, for native Windows, executables are additionally called with -M
as an argument, both with and without the .exe. This exercises a known bug in
the hand-off between the executable launcher (stdlib/header.c) and ocamlrun
where, for example, ocamlc.byte when resolved in PATH just runs as though it
were ocamlrun. The test works on the basis that -M is only a valid argument
for ocamlrun (returning the magic number).

Exercises:

  • Bytecode executable header and logic in ocamlc for computing the "shebang"
    header
  • Hand-off of the bytecode image between the executable header and ocamlrun

Shims:

  • On builds with shared library support, all the executables will contain the
    absolute location of ocamlrun and will fail to execute, unless the compiler
    was configured with --enable-runtime-search
  • On builds without shared library support, executables using libraries with
    C stubs (in particular, ocamldebug and ocamldoc) are compiled with
    -custom and do succeed
  • The test ensures that executables fail exactly when expected, rather than
    explicitly invoking them via ocamlrun. The reason for this is that some of
    the binaries (in particular, ocamlmktop) invoke another executable (in
    ocamlmktop's case, ocamlc) which will itself fail if ocamlc is a
    bytecode executable

Compilation mechanisms

This battery of tests exercises each of the compilation mechanisms available in
OCaml with a simple test program linked with the ocamlcommon library which
displays the location of the Standard Library (as determined by
Config.standard_library) and checks whether that directory exists.

Each of the executables is additionally called with a variety of Sys.argv.(0)
values testing:

  • a non-existent command (so an implicit Sys.argv.(0) which won't resolve in
    `PATH)
  • a command resolvable in PATH
  • a relative invocation of the executable (i.e. ./prog)
  • an implicit invocation where . is not in PATH (i.e. prog)
  • an implicit invocation but with . in PATH (i.e. PATH=".:$PATH" prog)
  • an absolute invocation of the executable (i.e. $PWD/prog)

Exercises:

  • Default linking mode of both ocamlc ("tendered bytecode") and ocamlopt
    (static executables)
  • -custom and -output-complete-exe modes of ocamlc with both the default
    static runtime and the shared runtime (libcamlrun_shared/libasmrun_shared)
  • "Main program in C" linking modes -output-obj and -output-complete-obj
    with both the static and shared runtimes
  • Sys.argv.(0) and Sys.executable_name

Shims:

  • As with the Dynlink test, on bytecode-only builds the compiler must be
    explicitly invoked via ocamlrun, unless the compiler was configured with
    --enable-runtime-search
  • The executable produced by ocamlc by default contains the absolute location
    of ocamlrun and so has to be run explicitly via ocamlrun, unless the
    compiler was configured with --enable-runtime-search-target

--- METRICS ---