blob: ec017e23b147ae9275aa363a299da609faa05e1b [file] [log] [blame]
Eric Fiselierb17bb062015-08-22 19:40:49 +00001==============
2Testing libc++
3==============
4
5.. contents::
6 :local:
7
8Getting Started
9===============
10
Louis Dionnee9271a42020-03-11 17:03:00 -040011libc++ uses LIT to configure and run its tests.
Marshall Clowb7ebdbd2019-09-05 00:38:36 +000012
Louis Dionne82bec932020-04-07 15:02:37 -040013The primary way to run the libc++ tests is by using ``make check-cxx``.
Marshall Clowb7ebdbd2019-09-05 00:38:36 +000014
15However since libc++ can be used in any number of possible
16configurations it is important to customize the way LIT builds and runs
17the tests. This guide provides information on how to use LIT directly to
18test libc++.
Eric Fiselierb17bb062015-08-22 19:40:49 +000019
20Please see the `Lit Command Guide`_ for more information about LIT.
21
Sylvestre Ledru72fd1032020-03-22 22:42:03 +010022.. _LIT Command Guide: https://llvm.org/docs/CommandGuide/lit.html
Eric Fiselierb17bb062015-08-22 19:40:49 +000023
Louis Dionne82bec932020-04-07 15:02:37 -040024Usage
25-----
Eric Fiselierb17bb062015-08-22 19:40:49 +000026
Louis Dionne82bec932020-04-07 15:02:37 -040027After building libc++, you can run parts of the libc++ test suite by simply
Louis Dionne3fbc9c72020-04-15 15:05:14 -040028running ``llvm-lit`` on a specified test or directory. If you're unsure
Louis Dionne5d796642020-06-26 12:08:59 -040029whether the required targets have been built, you can use the `check-cxx-deps`
30target to build them. For example:
Eric Fiselier147bb892015-10-14 20:44:44 +000031
32.. code-block:: bash
33
Louis Dionne82bec932020-04-07 15:02:37 -040034 $ cd <monorepo-root>
Louis Dionne3fbc9c72020-04-15 15:05:14 -040035 $ make -C <build> check-cxx-deps # If you want to make sure the targets get rebuilt
Louis Dionne82bec932020-04-07 15:02:37 -040036 $ <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 Fiselier147bb892015-10-14 20:44:44 +000039
Louis Dionne5d796642020-06-26 12:08:59 -040040In the default configuration, the tests are built against headers that form a
41fake installation root of libc++. This installation root has to be updated when
42changes are made to the headers, so you should re-run the `check-cxx-deps` target
43before running the tests manually with `lit` when you make any sort of change,
44including to the headers.
45
Eric Fiselier147bb892015-10-14 20:44:44 +000046Sometimes you'll want to change the way LIT is running the tests. Custom options
47can be specified using the `--param=<name>=<val>` flag. The most common option
48you'll want to change is the standard dialect (ie -std=c++XX). By default the
49test suite will select the newest C++ dialect supported by the compiler and use
50that. However if you want to manually specify the option like so:
51
52.. code-block:: bash
53
Louis Dionne82bec932020-04-07 15:02:37 -040054 $ <build>/bin/llvm-lit -sv libcxx/test/std/containers # Run the tests with the newest -std
55 $ <build>/bin/llvm-lit -sv libcxx/test/std/containers --param=std=c++03 # Run the tests in C++03
Eric Fiselier147bb892015-10-14 20:44:44 +000056
57Occasionally you'll want to add extra compile or link flags when testing.
58You can do this as follows:
59
60.. code-block:: bash
61
Louis Dionne82bec932020-04-07 15:02:37 -040062 $ <build>/bin/llvm-lit -sv libcxx/test --param=compile_flags='-Wcustom-warning'
63 $ <build>/bin/llvm-lit -sv libcxx/test --param=link_flags='-L/custom/library/path'
Eric Fiselier147bb892015-10-14 20:44:44 +000064
65Some other common examples include:
66
67.. code-block:: bash
68
69 # Specify a custom compiler.
Louis Dionne82bec932020-04-07 15:02:37 -040070 $ <build>/bin/llvm-lit -sv libcxx/test/std --param=cxx_under_test=/opt/bin/g++
Eric Fiselier147bb892015-10-14 20:44:44 +000071
72 # Enable warnings in the test suite
Louis Dionne82bec932020-04-07 15:02:37 -040073 $ <build>/bin/llvm-lit -sv libcxx/test --param=enable_warnings=true
Eric Fiselier147bb892015-10-14 20:44:44 +000074
75 # Use UBSAN when running the tests.
Louis Dionne82bec932020-04-07 15:02:37 -040076 $ <build>/bin/llvm-lit -sv libcxx/test --param=use_sanitizer=Undefined
Eric Fiselier147bb892015-10-14 20:44:44 +000077
Louis Dionne82bec932020-04-07 15:02:37 -040078Using a custom site configuration
79---------------------------------
80
81By default, the libc++ test suite will use a site configuration that matches
82the current CMake configuration. It does so by generating a ``lit.site.cfg``
Louis Dionnef9ca2052020-08-29 17:13:02 -040083file in the build directory from one of the configuration file templates in
84``libcxx/test/configs/``, and pointing ``llvm-lit`` (which is a wrapper around
85``llvm/utils/lit/lit.py``) to that file. So when you're running
86``<build>/bin/llvm-lit``, the generated ``lit.site.cfg`` file is always loaded
87instead of ``libcxx/test/lit.cfg.py``. If you want to use a custom site
88configuration, simply point the CMake build to it using
89``-DLIBCXX_TEST_CONFIG=<path-to-site-config>``, and that site configuration
90will be used instead. That file can use CMake variables inside it to make
91configuration easier.
Louis Dionne82bec932020-04-07 15:02:37 -040092
93 .. code-block:: bash
94
Louis Dionne0c66af92020-06-12 15:19:55 -040095 $ cmake <options> -DLIBCXX_TEST_CONFIG=<path-to-site-config>
96 $ make -C <build> check-cxx-deps
97 $ <build>/bin/llvm-lit -sv libcxx/test # will use your custom config file
Louis Dionne82bec932020-04-07 15:02:37 -040098
Eric Fiselierb17bb062015-08-22 19:40:49 +000099
100LIT Options
101===========
102
103:program:`lit` [*options*...] [*filenames*...]
104
105Command Line Options
106--------------------
107
Zola Bridges33ad38a2020-04-17 14:51:40 -0700108To use these options you pass them on the LIT command line as ``--param NAME``
109or ``--param NAME=VALUE``. Some options have default values specified during
110CMake's configuration. Passing the option on the command line will override the
111default.
Eric Fiselierb17bb062015-08-22 19:40:49 +0000112
113.. program:: lit
114
Eric Fiselierd2c29f92016-10-14 06:15:27 +0000115.. option:: cxx_under_test=<path/to/compiler>
Eric Fiselier147bb892015-10-14 20:44:44 +0000116
117 Specify the compiler used to build the tests.
118
Louis Dionneb7853962020-07-09 13:35:01 -0400119.. option:: stdlib=<stdlib name>
Eric Fiselierd2c29f92016-10-14 06:15:27 +0000120
Louis Dionneb7853962020-07-09 13:35:01 -0400121 **Values**: libc++, libstdc++, msvc
Eric Fiselier612c00d2016-10-12 00:00:37 +0000122
Louis Dionneb7853962020-07-09 13:35:01 -0400123 Specify the C++ standard library being tested. The default is libc++ if this
124 option is not provided. This option is intended to allow running the libc++
125 test suite against other standard library implementations.
Eric Fiselier612c00d2016-10-12 00:00:37 +0000126
Eric Fiselier147bb892015-10-14 20:44:44 +0000127.. option:: std=<standard version>
128
Louis Dionne31cbe0f2020-06-01 10:38:23 -0400129 **Values**: c++03, c++11, c++14, c++17, c++2a
Eric Fiselier147bb892015-10-14 20:44:44 +0000130
131 Change the standard version used when building the tests.
132
Eric Fiselierd2c29f92016-10-14 06:15:27 +0000133.. option:: cxx_headers=<path/to/headers>
Eric Fiselierb17bb062015-08-22 19:40:49 +0000134
Eric Fiselier612c00d2016-10-12 00:00:37 +0000135 Specify the c++ standard library headers that are tested. By default the
136 headers in the source tree are used.
Eric Fiselierb17bb062015-08-22 19:40:49 +0000137
Eric Fiselierd2c29f92016-10-14 06:15:27 +0000138.. option:: cxx_library_root=<path/to/lib/>
Eric Fiselierb17bb062015-08-22 19:40:49 +0000139
Eric Fiselierf40dd322016-04-20 04:17:39 +0000140 Specify the directory of the libc++ library to be tested. By default the
Louis Dionne21b0ec22020-05-15 11:33:59 -0400141 library folder of the build directory is used.
Eric Fiselierf40dd322016-04-20 04:17:39 +0000142
143
Eric Fiselierd2c29f92016-10-14 06:15:27 +0000144.. option:: cxx_runtime_root=<path/to/lib/>
Eric Fiselierf40dd322016-04-20 04:17:39 +0000145
146 Specify the directory of the libc++ library to use at runtime. This directory
147 is not added to the linkers search path. This can be used to compile tests
148 against one version of libc++ and run them using another. The default value
Louis Dionnebc297b12018-12-14 18:19:14 +0000149 for this option is `cxx_library_root`.
Eric Fiselierb17bb062015-08-22 19:40:49 +0000150
Eric Fiselieraaec00a2016-12-23 19:09:14 +0000151.. option:: use_system_cxx_lib=<bool>
Eric Fiselierb17bb062015-08-22 19:40:49 +0000152
153 **Default**: False
154
155 Enable or disable testing against the installed version of libc++ library.
Louis Dionne21b0ec22020-05-15 11:33:59 -0400156 This impacts whether the ``with_system_cxx_lib`` Lit feature is defined or
157 not. The ``cxx_library_root`` and ``cxx_runtime_root`` parameters should
158 still be used to specify the path of the library to link to and run against,
159 respectively.
Eric Fiselierb17bb062015-08-22 19:40:49 +0000160
Eric Fiselierd2c29f92016-10-14 06:15:27 +0000161.. option:: debug_level=<level>
Eric Fiselierb17bb062015-08-22 19:40:49 +0000162
163 **Values**: 0, 1
164
165 Enable the use of debug mode. Level 0 enables assertions and level 1 enables
166 assertions and debugging of iterator misuse.
167
168.. option:: use_sanitizer=<sanitizer name>
169
170 **Values**: Memory, MemoryWithOrigins, Address, Undefined
171
172 Run the tests using the given sanitizer. If LLVM_USE_SANITIZER was given when
173 building libc++ then that sanitizer will be used by default.
174
Petr Hosek97bc08a2019-02-05 19:50:47 +0000175.. option:: llvm_unwinder
176
177 Enable the use of LLVM unwinder instead of libgcc.
178
179.. option:: builtins_library
180
181 Path to the builtins library to use instead of libgcc.
182
Eric Fiselierb17bb062015-08-22 19:40:49 +0000183
Louis Dionne90455db2020-04-02 17:14:45 -0400184Writing Tests
185-------------
186
187When writing tests for the libc++ test suite, you should follow a few guidelines.
188This will ensure that your tests can run on a wide variety of hardware and under
189a wide variety of configurations. We have several unusual configurations such as
190building the tests on one host but running them on a different host, which add a
191few requirements to the test suite. Here's some stuff you should know:
192
193- All tests are run in a temporary directory that is unique to that test and
194 cleaned up after the test is done.
195- When a test needs data files as inputs, these data files can be saved in the
196 repository (when reasonable) and referrenced by the test as
197 ``// FILE_DEPENDENCIES: <path-to-dependencies>``. Copies of these files or
198 directories will be made available to the test in the temporary directory
199 where it is run.
200- You should never hardcode a path from the build-host in a test, because that
201 path will not necessarily be available on the host where the tests are run.
202- You should try to reduce the runtime dependencies of each test to the minimum.
203 For example, requiring Python to run a test is bad, since Python is not
204 necessarily available on all devices we may want to run the tests on (even
205 though supporting Python is probably trivial for the build-host).
206
Eric Fiselierb08d8b12016-07-19 23:07:03 +0000207Benchmarks
208==========
209
210Libc++ contains benchmark tests separately from the test of the test suite.
211The benchmarks are written using the `Google Benchmark`_ library, a copy of which
212is stored in the libc++ repository.
213
214For more information about using the Google Benchmark library see the
215`official documentation <https://github.com/google/benchmark>`_.
216
217.. _`Google Benchmark`: https://github.com/google/benchmark
218
219Building Benchmarks
220-------------------
221
Eric Fiselier8b4a3052016-08-29 19:50:49 +0000222The benchmark tests are not built by default. The benchmarks can be built using
223the ``cxx-benchmarks`` target.
Eric Fiselierb08d8b12016-07-19 23:07:03 +0000224
225An example build would look like:
226
227.. code-block:: bash
228
229 $ cd build
Eric Fiselier8b4a3052016-08-29 19:50:49 +0000230 $ cmake [options] <path to libcxx sources>
231 $ make cxx-benchmarks
Eric Fiselierb08d8b12016-07-19 23:07:03 +0000232
233This will build all of the benchmarks under ``<libcxx-src>/benchmarks`` to be
234built against the just-built libc++. The compiled tests are output into
235``build/benchmarks``.
236
237The benchmarks can also be built against the platforms native standard library
238using the ``-DLIBCXX_BUILD_BENCHMARKS_NATIVE_STDLIB=ON`` CMake option. This
239is useful for comparing the performance of libc++ to other standard libraries.
240The compiled benchmarks are named ``<test>.libcxx.out`` if they test libc++ and
241``<test>.native.out`` otherwise.
242
243Also See:
244
245 * :ref:`Building Libc++ <build instructions>`
246 * :ref:`CMake Options`
247
248Running Benchmarks
249------------------
250
251The benchmarks must be run manually by the user. Currently there is no way
252to run them as part of the build.
253
254For example:
255
256.. code-block:: bash
257
258 $ cd build/benchmarks
Eric Fiselier8b4a3052016-08-29 19:50:49 +0000259 $ make cxx-benchmarks
Eric Fiselierb08d8b12016-07-19 23:07:03 +0000260 $ ./algorithms.libcxx.out # Runs all the benchmarks
261 $ ./algorithms.libcxx.out --benchmark_filter=BM_Sort.* # Only runs the sort benchmarks
262
263For more information about running benchmarks see `Google Benchmark`_.