blob: 78134bca8f84ab89ae26cbfc9b778c81c3cdf3f0 [file] [log] [blame]
Eric Fiselierd720d1f2015-08-22 19:40:49 +00001==============
2Testing libc++
3==============
4
5.. contents::
6 :local:
7
8Getting Started
9===============
10
Louis Dionnef58d8292020-03-11 17:03:00 -040011libc++ uses LIT to configure and run its tests.
Marshall Clowca321972019-09-05 00:38:36 +000012
Louis Dionne089ced02020-04-07 15:02:37 -040013The primary way to run the libc++ tests is by using ``make check-cxx``.
Marshall Clowca321972019-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 Fiselierd720d1f2015-08-22 19:40:49 +000019
20Please see the `Lit Command Guide`_ for more information about LIT.
21
Sylvestre Ledrub09c9382020-03-22 22:42:03 +010022.. _LIT Command Guide: https://llvm.org/docs/CommandGuide/lit.html
Eric Fiselierd720d1f2015-08-22 19:40:49 +000023
Louis Dionne089ced02020-04-07 15:02:37 -040024Usage
25-----
Eric Fiselierd720d1f2015-08-22 19:40:49 +000026
Louis Dionne089ced02020-04-07 15:02:37 -040027After building libc++, you can run parts of the libc++ test suite by simply
Louis Dionnee35df792020-04-15 15:05:14 -040028running ``llvm-lit`` on a specified test or directory. If you're unsure
29whether the required libraries have been built, you can use the
30`check-cxx-deps` target. For example:
Eric Fiselier4ac88092015-10-14 20:44:44 +000031
32.. code-block:: bash
33
Louis Dionne089ced02020-04-07 15:02:37 -040034 $ cd <monorepo-root>
Louis Dionnee35df792020-04-15 15:05:14 -040035 $ make -C <build> check-cxx-deps # If you want to make sure the targets get rebuilt
Louis Dionne089ced02020-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 Fiselier4ac88092015-10-14 20:44:44 +000039
40Sometimes you'll want to change the way LIT is running the tests. Custom options
41can be specified using the `--param=<name>=<val>` flag. The most common option
42you'll want to change is the standard dialect (ie -std=c++XX). By default the
43test suite will select the newest C++ dialect supported by the compiler and use
44that. However if you want to manually specify the option like so:
45
46.. code-block:: bash
47
Louis Dionne089ced02020-04-07 15:02:37 -040048 $ <build>/bin/llvm-lit -sv libcxx/test/std/containers # Run the tests with the newest -std
49 $ <build>/bin/llvm-lit -sv libcxx/test/std/containers --param=std=c++03 # Run the tests in C++03
Eric Fiselier4ac88092015-10-14 20:44:44 +000050
51Occasionally you'll want to add extra compile or link flags when testing.
52You can do this as follows:
53
54.. code-block:: bash
55
Louis Dionne089ced02020-04-07 15:02:37 -040056 $ <build>/bin/llvm-lit -sv libcxx/test --param=compile_flags='-Wcustom-warning'
57 $ <build>/bin/llvm-lit -sv libcxx/test --param=link_flags='-L/custom/library/path'
Eric Fiselier4ac88092015-10-14 20:44:44 +000058
59Some other common examples include:
60
61.. code-block:: bash
62
63 # Specify a custom compiler.
Louis Dionne089ced02020-04-07 15:02:37 -040064 $ <build>/bin/llvm-lit -sv libcxx/test/std --param=cxx_under_test=/opt/bin/g++
Eric Fiselier4ac88092015-10-14 20:44:44 +000065
66 # Enable warnings in the test suite
Louis Dionne089ced02020-04-07 15:02:37 -040067 $ <build>/bin/llvm-lit -sv libcxx/test --param=enable_warnings=true
Eric Fiselier4ac88092015-10-14 20:44:44 +000068
69 # Use UBSAN when running the tests.
Louis Dionne089ced02020-04-07 15:02:37 -040070 $ <build>/bin/llvm-lit -sv libcxx/test --param=use_sanitizer=Undefined
Eric Fiselier4ac88092015-10-14 20:44:44 +000071
Louis Dionne089ced02020-04-07 15:02:37 -040072Using a custom site configuration
73---------------------------------
74
75By default, the libc++ test suite will use a site configuration that matches
76the current CMake configuration. It does so by generating a ``lit.site.cfg``
77file in the build directory from the ``libcxx/test/lit.site.cfg.in`` template,
78and pointing ``llvm-lit`` (which is a wrapper around ``llvm/utils/lit/lit.py``)
79to that file. So when you're running ``<build>/bin/llvm-lit``, the generated
80``lit.site.cfg`` file is always loaded first, followed by the actual config in
81``libcxx/test/lit.cfg``. However, it is sometimes desirable to use a custom
82site configuration. To do that, you can use ``--param=libcxx_site_config`` or
83the ``LIBCXX_SITE_CONFIG`` environment variable to point to the right site
84configuration file. However, you must stop using ``llvm-lit``, or else the
85generated ``lit.site.cfg`` will still be preferred:
86
87 .. code-block:: bash
88
89 $ LIBCXX_SITE_CONFIG=path/to/your/site/configuration llvm/utils/lit/lit.py -sv ...
90
91 $ llvm/utils/lit/lit.py -sv ... --param=libcxx_site_config=path/to/your/site/configuration
92
93In both of these cases, your custom site configuration should set up the
94``config`` object in a way that is compatible with what libc++'s ``config.py``
95module expects.
Eric Fiselierd720d1f2015-08-22 19:40:49 +000096
97LIT Options
98===========
99
100:program:`lit` [*options*...] [*filenames*...]
101
102Command Line Options
103--------------------
104
Zola Bridges3c66e222020-04-17 14:51:40 -0700105To use these options you pass them on the LIT command line as ``--param NAME``
106or ``--param NAME=VALUE``. Some options have default values specified during
107CMake's configuration. Passing the option on the command line will override the
108default.
Eric Fiselierd720d1f2015-08-22 19:40:49 +0000109
110.. program:: lit
111
Eric Fiselier4fe629c2016-10-14 06:15:27 +0000112.. option:: cxx_under_test=<path/to/compiler>
Eric Fiselier4ac88092015-10-14 20:44:44 +0000113
114 Specify the compiler used to build the tests.
115
Eric Fiselier4fe629c2016-10-14 06:15:27 +0000116.. option:: cxx_stdlib_under_test=<stdlib name>
117
118 **Values**: libc++, libstdc++
Eric Fiselierbbbbd112016-10-12 00:00:37 +0000119
120 Specify the C++ standard library being tested. Unless otherwise specified
121 libc++ is used. This option is intended to allow running the libc++ test
122 suite against other standard library implementations.
123
Eric Fiselier4ac88092015-10-14 20:44:44 +0000124.. option:: std=<standard version>
125
Eric Fiselier32f3f702017-11-07 20:26:23 +0000126 **Values**: c++98, c++03, c++11, c++14, c++17, c++2a
Eric Fiselier4ac88092015-10-14 20:44:44 +0000127
128 Change the standard version used when building the tests.
129
Eric Fiselier4fe629c2016-10-14 06:15:27 +0000130.. option:: libcxx_site_config=<path/to/lit.site.cfg>
Eric Fiselierd720d1f2015-08-22 19:40:49 +0000131
132 Specify the site configuration to use when running the tests. This option
Eric Fiselier17ec5b12017-05-09 23:47:20 +0000133 overrides the environment variable LIBCXX_SITE_CONFIG.
Eric Fiselierd720d1f2015-08-22 19:40:49 +0000134
Eric Fiselier4fe629c2016-10-14 06:15:27 +0000135.. option:: cxx_headers=<path/to/headers>
Eric Fiselierd720d1f2015-08-22 19:40:49 +0000136
Eric Fiselierbbbbd112016-10-12 00:00:37 +0000137 Specify the c++ standard library headers that are tested. By default the
138 headers in the source tree are used.
Eric Fiselierd720d1f2015-08-22 19:40:49 +0000139
Eric Fiselier4fe629c2016-10-14 06:15:27 +0000140.. option:: cxx_library_root=<path/to/lib/>
Eric Fiselierd720d1f2015-08-22 19:40:49 +0000141
Eric Fiselier661b0c12016-04-20 04:17:39 +0000142 Specify the directory of the libc++ library to be tested. By default the
143 library folder of the build directory is used. This option cannot be used
Eric Fiselier60be2462016-12-23 19:09:14 +0000144 when use_system_cxx_lib is provided.
Eric Fiselier661b0c12016-04-20 04:17:39 +0000145
146
Eric Fiselier4fe629c2016-10-14 06:15:27 +0000147.. option:: cxx_runtime_root=<path/to/lib/>
Eric Fiselier661b0c12016-04-20 04:17:39 +0000148
149 Specify the directory of the libc++ library to use at runtime. This directory
150 is not added to the linkers search path. This can be used to compile tests
151 against one version of libc++ and run them using another. The default value
Louis Dionnee70b1dd2018-12-14 18:19:14 +0000152 for this option is `cxx_library_root`.
Eric Fiselierd720d1f2015-08-22 19:40:49 +0000153
Eric Fiselier60be2462016-12-23 19:09:14 +0000154.. option:: use_system_cxx_lib=<bool>
Eric Fiselierd720d1f2015-08-22 19:40:49 +0000155
156 **Default**: False
157
158 Enable or disable testing against the installed version of libc++ library.
159 Note: This does not use the installed headers.
160
Eric Fiselier4fe629c2016-10-14 06:15:27 +0000161.. option:: use_lit_shell=<bool>
Eric Fiselierd720d1f2015-08-22 19:40:49 +0000162
163 Enable or disable the use of LIT's internal shell in ShTests. If the
164 environment variable LIT_USE_INTERNAL_SHELL is present then that is used as
165 the default value. Otherwise the default value is True on Windows and False
166 on every other platform.
167
Eric Fiselier4fe629c2016-10-14 06:15:27 +0000168.. option:: compile_flags="<list-of-args>"
Eric Fiselierd720d1f2015-08-22 19:40:49 +0000169
170 Specify additional compile flags as a space delimited string.
171 Note: This options should not be used to change the standard version used.
172
Eric Fiselier4fe629c2016-10-14 06:15:27 +0000173.. option:: link_flags="<list-of-args>"
Eric Fiselierd720d1f2015-08-22 19:40:49 +0000174
175 Specify additional link flags as a space delimited string.
176
Eric Fiselier4fe629c2016-10-14 06:15:27 +0000177.. option:: debug_level=<level>
Eric Fiselierd720d1f2015-08-22 19:40:49 +0000178
179 **Values**: 0, 1
180
181 Enable the use of debug mode. Level 0 enables assertions and level 1 enables
182 assertions and debugging of iterator misuse.
183
184.. option:: use_sanitizer=<sanitizer name>
185
186 **Values**: Memory, MemoryWithOrigins, Address, Undefined
187
188 Run the tests using the given sanitizer. If LLVM_USE_SANITIZER was given when
189 building libc++ then that sanitizer will be used by default.
190
Petr Hosek7b9f43a2019-02-05 19:50:47 +0000191.. option:: llvm_unwinder
192
193 Enable the use of LLVM unwinder instead of libgcc.
194
195.. option:: builtins_library
196
197 Path to the builtins library to use instead of libgcc.
198
Eric Fiselierd720d1f2015-08-22 19:40:49 +0000199
200Environment Variables
201---------------------
202
Eric Fiselier71646ec2016-05-05 08:12:25 +0000203.. envvar:: LIBCXX_SITE_CONFIG=<path/to/lit.site.cfg>
Eric Fiselierd720d1f2015-08-22 19:40:49 +0000204
205 Specify the site configuration to use when running the tests.
Eric Fiselier887b86b2016-09-16 03:47:53 +0000206 Also see `libcxx_site_config`.
Eric Fiselierd720d1f2015-08-22 19:40:49 +0000207
Louis Dionne9e7cf262020-04-02 17:14:45 -0400208Writing Tests
209-------------
210
211When writing tests for the libc++ test suite, you should follow a few guidelines.
212This will ensure that your tests can run on a wide variety of hardware and under
213a wide variety of configurations. We have several unusual configurations such as
214building the tests on one host but running them on a different host, which add a
215few requirements to the test suite. Here's some stuff you should know:
216
217- All tests are run in a temporary directory that is unique to that test and
218 cleaned up after the test is done.
219- When a test needs data files as inputs, these data files can be saved in the
220 repository (when reasonable) and referrenced by the test as
221 ``// FILE_DEPENDENCIES: <path-to-dependencies>``. Copies of these files or
222 directories will be made available to the test in the temporary directory
223 where it is run.
224- You should never hardcode a path from the build-host in a test, because that
225 path will not necessarily be available on the host where the tests are run.
226- You should try to reduce the runtime dependencies of each test to the minimum.
227 For example, requiring Python to run a test is bad, since Python is not
228 necessarily available on all devices we may want to run the tests on (even
229 though supporting Python is probably trivial for the build-host).
230
Eric Fiselier9e3e1372016-07-19 23:07:03 +0000231Benchmarks
232==========
233
234Libc++ contains benchmark tests separately from the test of the test suite.
235The benchmarks are written using the `Google Benchmark`_ library, a copy of which
236is stored in the libc++ repository.
237
238For more information about using the Google Benchmark library see the
239`official documentation <https://github.com/google/benchmark>`_.
240
241.. _`Google Benchmark`: https://github.com/google/benchmark
242
243Building Benchmarks
244-------------------
245
Eric Fiselier93f212c2016-08-29 19:50:49 +0000246The benchmark tests are not built by default. The benchmarks can be built using
247the ``cxx-benchmarks`` target.
Eric Fiselier9e3e1372016-07-19 23:07:03 +0000248
249An example build would look like:
250
251.. code-block:: bash
252
253 $ cd build
Eric Fiselier93f212c2016-08-29 19:50:49 +0000254 $ cmake [options] <path to libcxx sources>
255 $ make cxx-benchmarks
Eric Fiselier9e3e1372016-07-19 23:07:03 +0000256
257This will build all of the benchmarks under ``<libcxx-src>/benchmarks`` to be
258built against the just-built libc++. The compiled tests are output into
259``build/benchmarks``.
260
261The benchmarks can also be built against the platforms native standard library
262using the ``-DLIBCXX_BUILD_BENCHMARKS_NATIVE_STDLIB=ON`` CMake option. This
263is useful for comparing the performance of libc++ to other standard libraries.
264The compiled benchmarks are named ``<test>.libcxx.out`` if they test libc++ and
265``<test>.native.out`` otherwise.
266
267Also See:
268
269 * :ref:`Building Libc++ <build instructions>`
270 * :ref:`CMake Options`
271
272Running Benchmarks
273------------------
274
275The benchmarks must be run manually by the user. Currently there is no way
276to run them as part of the build.
277
278For example:
279
280.. code-block:: bash
281
282 $ cd build/benchmarks
Eric Fiselier93f212c2016-08-29 19:50:49 +0000283 $ make cxx-benchmarks
Eric Fiselier9e3e1372016-07-19 23:07:03 +0000284 $ ./algorithms.libcxx.out # Runs all the benchmarks
285 $ ./algorithms.libcxx.out --benchmark_filter=BM_Sort.* # Only runs the sort benchmarks
286
287For more information about running benchmarks see `Google Benchmark`_.