Eric Fiselier | d720d1f | 2015-08-22 19:40:49 +0000 | [diff] [blame] | 1 | ============== |
| 2 | Testing libc++ |
| 3 | ============== |
| 4 | |
| 5 | .. contents:: |
| 6 | :local: |
| 7 | |
| 8 | Getting Started |
| 9 | =============== |
| 10 | |
Louis Dionne | f58d829 | 2020-03-11 17:03:00 -0400 | [diff] [blame] | 11 | libc++ uses LIT to configure and run its tests. |
Marshall Clow | ca32197 | 2019-09-05 00:38:36 +0000 | [diff] [blame] | 12 | |
Louis Dionne | 089ced0 | 2020-04-07 15:02:37 -0400 | [diff] [blame] | 13 | The primary way to run the libc++ tests is by using ``make check-cxx``. |
Marshall Clow | ca32197 | 2019-09-05 00:38:36 +0000 | [diff] [blame] | 14 | |
| 15 | However since libc++ can be used in any number of possible |
| 16 | configurations it is important to customize the way LIT builds and runs |
| 17 | the tests. This guide provides information on how to use LIT directly to |
| 18 | test libc++. |
Eric Fiselier | d720d1f | 2015-08-22 19:40:49 +0000 | [diff] [blame] | 19 | |
| 20 | Please see the `Lit Command Guide`_ for more information about LIT. |
| 21 | |
Sylvestre Ledru | b09c938 | 2020-03-22 22:42:03 +0100 | [diff] [blame] | 22 | .. _LIT Command Guide: https://llvm.org/docs/CommandGuide/lit.html |
Eric Fiselier | d720d1f | 2015-08-22 19:40:49 +0000 | [diff] [blame] | 23 | |
Louis Dionne | 089ced0 | 2020-04-07 15:02:37 -0400 | [diff] [blame] | 24 | Usage |
| 25 | ----- |
Eric Fiselier | d720d1f | 2015-08-22 19:40:49 +0000 | [diff] [blame] | 26 | |
Louis Dionne | 089ced0 | 2020-04-07 15:02:37 -0400 | [diff] [blame] | 27 | After building libc++, you can run parts of the libc++ test suite by simply |
Louis Dionne | e35df79 | 2020-04-15 15:05:14 -0400 | [diff] [blame] | 28 | running ``llvm-lit`` on a specified test or directory. If you're unsure |
Louis Dionne | 3efccd7 | 2020-10-23 09:32:50 -0400 | [diff] [blame] | 29 | whether the required libraries have been built, you can use the |
Louis Dionne | 4eaeee4 | 2022-08-02 16:15:55 -0400 | [diff] [blame] | 30 | ``cxx-test-depends`` target. For example: |
Eric Fiselier | 4ac8809 | 2015-10-14 20:44:44 +0000 | [diff] [blame] | 31 | |
| 32 | .. code-block:: bash |
| 33 | |
Louis Dionne | 089ced0 | 2020-04-07 15:02:37 -0400 | [diff] [blame] | 34 | $ cd <monorepo-root> |
Louis Dionne | 1073531 | 2021-01-12 10:56:57 -0500 | [diff] [blame] | 35 | $ make -C <build> cxx-test-depends # If you want to make sure the targets get rebuilt |
Louis Dionne | 089ced0 | 2020-04-07 15:02:37 -0400 | [diff] [blame] | 36 | $ <build>/bin/llvm-lit -sv libcxx/test/std/re # Run all of the std::regex tests |
| 37 | $ <build>/bin/llvm-lit -sv libcxx/test/std/depr/depr.c.headers/stdlib_h.pass.cpp # Run a single test |
| 38 | $ <build>/bin/llvm-lit -sv libcxx/test/std/atomics libcxx/test/std/threads # Test std::thread and std::atomic |
Eric Fiselier | 4ac8809 | 2015-10-14 20:44:44 +0000 | [diff] [blame] | 39 | |
Louis Dionne | 4e6ca9b | 2022-08-03 09:24:54 -0400 | [diff] [blame] | 40 | .. note:: |
| 41 | If you used the Bootstrapping build instead of the default runtimes build, the |
| 42 | ``cxx-test-depends`` target is instead named ``runtimes-test-depends``, and |
| 43 | you will need to prefix ``<build>/runtimes/runtimes-<target>-bins/`` to the |
| 44 | paths of all tests. |
| 45 | |
Louis Dionne | 94870d8 | 2020-06-26 12:08:59 -0400 | [diff] [blame] | 46 | In the default configuration, the tests are built against headers that form a |
| 47 | fake installation root of libc++. This installation root has to be updated when |
Louis Dionne | 4eaeee4 | 2022-08-02 16:15:55 -0400 | [diff] [blame] | 48 | changes are made to the headers, so you should re-run the ``cxx-test-depends`` |
| 49 | target before running the tests manually with ``lit`` when you make any sort of |
Louis Dionne | 94870d8 | 2020-06-26 12:08:59 -0400 | [diff] [blame] | 50 | change, including to the headers. |
| 51 | |
Eric Fiselier | 4ac8809 | 2015-10-14 20:44:44 +0000 | [diff] [blame] | 52 | Sometimes you'll want to change the way LIT is running the tests. Custom options |
Louis Dionne | 4eaeee4 | 2022-08-02 16:15:55 -0400 | [diff] [blame] | 53 | can be specified using the ``--param <name>=<val>`` flag. The most common option |
| 54 | you'll want to change is the standard dialect (ie ``-std=c++XX``). By default the |
Eric Fiselier | 4ac8809 | 2015-10-14 20:44:44 +0000 | [diff] [blame] | 55 | test suite will select the newest C++ dialect supported by the compiler and use |
Louis Dionne | 4eaeee4 | 2022-08-02 16:15:55 -0400 | [diff] [blame] | 56 | that. However, you can manually specify the option like so if you want: |
Eric Fiselier | 4ac8809 | 2015-10-14 20:44:44 +0000 | [diff] [blame] | 57 | |
| 58 | .. code-block:: bash |
| 59 | |
Louis Dionne | 089ced0 | 2020-04-07 15:02:37 -0400 | [diff] [blame] | 60 | $ <build>/bin/llvm-lit -sv libcxx/test/std/containers # Run the tests with the newest -std |
Louis Dionne | 4eaeee4 | 2022-08-02 16:15:55 -0400 | [diff] [blame] | 61 | $ <build>/bin/llvm-lit -sv libcxx/test/std/containers --param std=c++03 # Run the tests in C++03 |
Eric Fiselier | 4ac8809 | 2015-10-14 20:44:44 +0000 | [diff] [blame] | 62 | |
Louis Dionne | 4eaeee4 | 2022-08-02 16:15:55 -0400 | [diff] [blame] | 63 | Other parameters are supported by the test suite. Those are defined in ``libcxx/utils/libcxx/test/params.py``. |
| 64 | If you want to customize how to run the libc++ test suite beyond what is available |
| 65 | in ``params.py``, you most likely want to use a custom site configuration instead. |
Eric Fiselier | 4ac8809 | 2015-10-14 20:44:44 +0000 | [diff] [blame] | 66 | |
Louis Dionne | 4eaeee4 | 2022-08-02 16:15:55 -0400 | [diff] [blame] | 67 | The libc++ test suite works by loading a site configuration that defines various |
| 68 | "base" parameters (via Lit substitutions). These base parameters represent things |
| 69 | like the compiler to use for running the tests, which default compiler and linker |
| 70 | flags to use, and how to run an executable. This system is meant to be easily |
| 71 | extended for custom needs, in particular when porting the libc++ test suite to |
| 72 | new platforms. |
Eric Fiselier | 4ac8809 | 2015-10-14 20:44:44 +0000 | [diff] [blame] | 73 | |
Louis Dionne | 4eaeee4 | 2022-08-02 16:15:55 -0400 | [diff] [blame] | 74 | Using a Custom Site Configuration |
Louis Dionne | 089ced0 | 2020-04-07 15:02:37 -0400 | [diff] [blame] | 75 | --------------------------------- |
| 76 | |
| 77 | By default, the libc++ test suite will use a site configuration that matches |
| 78 | the current CMake configuration. It does so by generating a ``lit.site.cfg`` |
Louis Dionne | 6da743b | 2020-08-29 17:13:02 -0400 | [diff] [blame] | 79 | file in the build directory from one of the configuration file templates in |
| 80 | ``libcxx/test/configs/``, and pointing ``llvm-lit`` (which is a wrapper around |
| 81 | ``llvm/utils/lit/lit.py``) to that file. So when you're running |
| 82 | ``<build>/bin/llvm-lit``, the generated ``lit.site.cfg`` file is always loaded |
| 83 | instead of ``libcxx/test/lit.cfg.py``. If you want to use a custom site |
| 84 | configuration, simply point the CMake build to it using |
| 85 | ``-DLIBCXX_TEST_CONFIG=<path-to-site-config>``, and that site configuration |
| 86 | will be used instead. That file can use CMake variables inside it to make |
| 87 | configuration easier. |
Louis Dionne | 089ced0 | 2020-04-07 15:02:37 -0400 | [diff] [blame] | 88 | |
| 89 | .. code-block:: bash |
| 90 | |
Louis Dionne | 19608c6 | 2020-06-12 15:19:55 -0400 | [diff] [blame] | 91 | $ cmake <options> -DLIBCXX_TEST_CONFIG=<path-to-site-config> |
Louis Dionne | 1073531 | 2021-01-12 10:56:57 -0500 | [diff] [blame] | 92 | $ make -C <build> cxx-test-depends |
Louis Dionne | 19608c6 | 2020-06-12 15:19:55 -0400 | [diff] [blame] | 93 | $ <build>/bin/llvm-lit -sv libcxx/test # will use your custom config file |
Louis Dionne | 089ced0 | 2020-04-07 15:02:37 -0400 | [diff] [blame] | 94 | |
Nikolas Klauser | a9ad670 | 2022-08-13 13:23:16 +0200 | [diff] [blame] | 95 | Additional tools |
| 96 | ---------------- |
| 97 | |
| 98 | The libc++ test suite uses a few optional tools to improve the code quality. |
| 99 | |
| 100 | These tools are: |
Nikolas Klauser | bec3991 | 2023-01-09 03:01:26 +0100 | [diff] [blame] | 101 | - clang-tidy (you might need additional dev packages to compile libc++-specific clang-tidy checks) |
Nikolas Klauser | a9ad670 | 2022-08-13 13:23:16 +0200 | [diff] [blame] | 102 | |
Louis Dionne | bfaf65c | 2023-01-25 13:09:39 -0500 | [diff] [blame] | 103 | Reproducing CI issues locally |
| 104 | ----------------------------- |
| 105 | |
| 106 | Libc++ has extensive CI that tests various configurations of the library. The testing for |
| 107 | all these configurations is located in ``libcxx/utils/ci/run-buildbot``. Most of our |
| 108 | CI jobs are being run on a Docker image for reproducibility. The definition of this Docker |
| 109 | image is located in ``libcxx/utils/ci/Dockerfile``. If you are looking to reproduce the |
| 110 | failure of a specific CI job locally, you should first drop into a Docker container that |
| 111 | matches our CI images by running ``libcxx/utils/ci/run-buildbot-container``, and then run |
| 112 | the specific CI job that you're interested in (from within the container) using the ``run-buildbot`` |
| 113 | script above. If you want to control which compiler is used, you can set the ``CC`` and the |
| 114 | ``CXX`` environment variables before calling ``run-buildbot`` to select the right compiler. |
| 115 | Take note that some CI jobs are testing the library on specific platforms and are *not* run |
| 116 | in our Docker image. In the general case, it is not possible to reproduce these failures |
| 117 | locally, unless they aren't specific to the platform. |
| 118 | |
| 119 | Also note that the Docker container shares the same filesystem as your local machine, so |
| 120 | modifying files on your local machine will also modify what the Docker container sees. |
| 121 | This is useful for editing source files as you're testing your code in the Docker container. |
| 122 | |
Louis Dionne | 9e7cf26 | 2020-04-02 17:14:45 -0400 | [diff] [blame] | 123 | Writing Tests |
| 124 | ------------- |
| 125 | |
| 126 | When writing tests for the libc++ test suite, you should follow a few guidelines. |
| 127 | This will ensure that your tests can run on a wide variety of hardware and under |
| 128 | a wide variety of configurations. We have several unusual configurations such as |
| 129 | building the tests on one host but running them on a different host, which add a |
| 130 | few requirements to the test suite. Here's some stuff you should know: |
| 131 | |
| 132 | - All tests are run in a temporary directory that is unique to that test and |
| 133 | cleaned up after the test is done. |
| 134 | - When a test needs data files as inputs, these data files can be saved in the |
Marek Kurdej | 0dd96e5 | 2020-10-29 14:39:09 +0100 | [diff] [blame] | 135 | repository (when reasonable) and referenced by the test as |
Louis Dionne | 9e7cf26 | 2020-04-02 17:14:45 -0400 | [diff] [blame] | 136 | ``// FILE_DEPENDENCIES: <path-to-dependencies>``. Copies of these files or |
| 137 | directories will be made available to the test in the temporary directory |
| 138 | where it is run. |
| 139 | - You should never hardcode a path from the build-host in a test, because that |
| 140 | path will not necessarily be available on the host where the tests are run. |
| 141 | - You should try to reduce the runtime dependencies of each test to the minimum. |
| 142 | For example, requiring Python to run a test is bad, since Python is not |
| 143 | necessarily available on all devices we may want to run the tests on (even |
| 144 | though supporting Python is probably trivial for the build-host). |
| 145 | |
Eric Fiselier | 9e3e137 | 2016-07-19 23:07:03 +0000 | [diff] [blame] | 146 | Benchmarks |
| 147 | ========== |
| 148 | |
| 149 | Libc++ contains benchmark tests separately from the test of the test suite. |
| 150 | The benchmarks are written using the `Google Benchmark`_ library, a copy of which |
| 151 | is stored in the libc++ repository. |
| 152 | |
| 153 | For more information about using the Google Benchmark library see the |
| 154 | `official documentation <https://github.com/google/benchmark>`_. |
| 155 | |
| 156 | .. _`Google Benchmark`: https://github.com/google/benchmark |
| 157 | |
| 158 | Building Benchmarks |
| 159 | ------------------- |
| 160 | |
Eric Fiselier | 93f212c | 2016-08-29 19:50:49 +0000 | [diff] [blame] | 161 | The benchmark tests are not built by default. The benchmarks can be built using |
| 162 | the ``cxx-benchmarks`` target. |
Eric Fiselier | 9e3e137 | 2016-07-19 23:07:03 +0000 | [diff] [blame] | 163 | |
| 164 | An example build would look like: |
| 165 | |
| 166 | .. code-block:: bash |
| 167 | |
| 168 | $ cd build |
Shivam Gupta | e3cb570 | 2021-10-02 07:35:15 +0530 | [diff] [blame] | 169 | $ ninja cxx-benchmarks |
Eric Fiselier | 9e3e137 | 2016-07-19 23:07:03 +0000 | [diff] [blame] | 170 | |
| 171 | This will build all of the benchmarks under ``<libcxx-src>/benchmarks`` to be |
| 172 | built against the just-built libc++. The compiled tests are output into |
Shivam Gupta | e3cb570 | 2021-10-02 07:35:15 +0530 | [diff] [blame] | 173 | ``build/projects/libcxx/benchmarks``. |
Eric Fiselier | 9e3e137 | 2016-07-19 23:07:03 +0000 | [diff] [blame] | 174 | |
| 175 | The benchmarks can also be built against the platforms native standard library |
| 176 | using the ``-DLIBCXX_BUILD_BENCHMARKS_NATIVE_STDLIB=ON`` CMake option. This |
| 177 | is useful for comparing the performance of libc++ to other standard libraries. |
| 178 | The compiled benchmarks are named ``<test>.libcxx.out`` if they test libc++ and |
| 179 | ``<test>.native.out`` otherwise. |
| 180 | |
| 181 | Also See: |
| 182 | |
| 183 | * :ref:`Building Libc++ <build instructions>` |
| 184 | * :ref:`CMake Options` |
| 185 | |
| 186 | Running Benchmarks |
| 187 | ------------------ |
| 188 | |
| 189 | The benchmarks must be run manually by the user. Currently there is no way |
| 190 | to run them as part of the build. |
| 191 | |
| 192 | For example: |
| 193 | |
| 194 | .. code-block:: bash |
| 195 | |
Shivam Gupta | e3cb570 | 2021-10-02 07:35:15 +0530 | [diff] [blame] | 196 | $ cd build/projects/libcxx/benchmarks |
Eric Fiselier | 9e3e137 | 2016-07-19 23:07:03 +0000 | [diff] [blame] | 197 | $ ./algorithms.libcxx.out # Runs all the benchmarks |
| 198 | $ ./algorithms.libcxx.out --benchmark_filter=BM_Sort.* # Only runs the sort benchmarks |
| 199 | |
| 200 | For more information about running benchmarks see `Google Benchmark`_. |