blob: 774003698beae863249e4a5324302c5e1c260d25 [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 Dionnef58d8292020-03-11 17:03:00 -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
Eric Fiselierd720d1f2015-08-22 19:40:49 +000024Setting up the Environment
25--------------------------
26
27After building libc++ you must setup your environment to test libc++ using
28LIT.
29
30#. Create a shortcut to the actual lit executable so that you can invoke it
31 easily from the command line.
32
33 .. code-block:: bash
34
35 $ alias lit='python path/to/llvm/utils/lit/lit.py'
36
37#. Tell LIT where to find your build configuration.
38
39 .. code-block:: bash
40
41 $ export LIBCXX_SITE_CONFIG=path/to/build-libcxx/test/lit.site.cfg
42
Eric Fiselier4ac88092015-10-14 20:44:44 +000043Example Usage
44-------------
45
46Once you have your environment set up and you have built libc++ you can run
47parts of the libc++ test suite by simply running `lit` on a specified test or
48directory. For example:
49
50.. code-block:: bash
51
52 $ cd path/to/src/libcxx
53 $ lit -sv test/std/re # Run all of the std::regex tests
54 $ lit -sv test/std/depr/depr.c.headers/stdlib_h.pass.cpp # Run a single test
55 $ lit -sv test/std/atomics test/std/threads # Test std::thread and std::atomic
56
57Sometimes you'll want to change the way LIT is running the tests. Custom options
58can be specified using the `--param=<name>=<val>` flag. The most common option
59you'll want to change is the standard dialect (ie -std=c++XX). By default the
60test suite will select the newest C++ dialect supported by the compiler and use
61that. However if you want to manually specify the option like so:
62
63.. code-block:: bash
64
65 $ lit -sv test/std/containers # Run the tests with the newest -std
66 $ lit -sv --param=std=c++03 test/std/containers # Run the tests in C++03
67
68Occasionally you'll want to add extra compile or link flags when testing.
69You can do this as follows:
70
71.. code-block:: bash
72
73 $ lit -sv --param=compile_flags='-Wcustom-warning'
74 $ lit -sv --param=link_flags='-L/custom/library/path'
75
76Some other common examples include:
77
78.. code-block:: bash
79
80 # Specify a custom compiler.
81 $ lit -sv --param=cxx_under_test=/opt/bin/g++ test/std
82
83 # Enable warnings in the test suite
84 $ lit -sv --param=enable_warnings=true test/std
85
86 # Use UBSAN when running the tests.
87 $ lit -sv --param=use_sanitizer=Undefined
88
Eric Fiselierd720d1f2015-08-22 19:40:49 +000089
90LIT Options
91===========
92
93:program:`lit` [*options*...] [*filenames*...]
94
95Command Line Options
96--------------------
97
98To use these options you pass them on the LIT command line as --param NAME or
99--param NAME=VALUE. Some options have default values specified during CMake's
100configuration. Passing the option on the command line will override the default.
101
102.. program:: lit
103
Eric Fiselier4fe629c2016-10-14 06:15:27 +0000104.. option:: cxx_under_test=<path/to/compiler>
Eric Fiselier4ac88092015-10-14 20:44:44 +0000105
106 Specify the compiler used to build the tests.
107
Eric Fiselier4fe629c2016-10-14 06:15:27 +0000108.. option:: cxx_stdlib_under_test=<stdlib name>
109
110 **Values**: libc++, libstdc++
Eric Fiselierbbbbd112016-10-12 00:00:37 +0000111
112 Specify the C++ standard library being tested. Unless otherwise specified
113 libc++ is used. This option is intended to allow running the libc++ test
114 suite against other standard library implementations.
115
Eric Fiselier4ac88092015-10-14 20:44:44 +0000116.. option:: std=<standard version>
117
Eric Fiselier32f3f702017-11-07 20:26:23 +0000118 **Values**: c++98, c++03, c++11, c++14, c++17, c++2a
Eric Fiselier4ac88092015-10-14 20:44:44 +0000119
120 Change the standard version used when building the tests.
121
Eric Fiselier4fe629c2016-10-14 06:15:27 +0000122.. option:: libcxx_site_config=<path/to/lit.site.cfg>
Eric Fiselierd720d1f2015-08-22 19:40:49 +0000123
124 Specify the site configuration to use when running the tests. This option
Eric Fiselier17ec5b12017-05-09 23:47:20 +0000125 overrides the environment variable LIBCXX_SITE_CONFIG.
Eric Fiselierd720d1f2015-08-22 19:40:49 +0000126
Eric Fiselier4fe629c2016-10-14 06:15:27 +0000127.. option:: cxx_headers=<path/to/headers>
Eric Fiselierd720d1f2015-08-22 19:40:49 +0000128
Eric Fiselierbbbbd112016-10-12 00:00:37 +0000129 Specify the c++ standard library headers that are tested. By default the
130 headers in the source tree are used.
Eric Fiselierd720d1f2015-08-22 19:40:49 +0000131
Eric Fiselier4fe629c2016-10-14 06:15:27 +0000132.. option:: cxx_library_root=<path/to/lib/>
Eric Fiselierd720d1f2015-08-22 19:40:49 +0000133
Eric Fiselier661b0c12016-04-20 04:17:39 +0000134 Specify the directory of the libc++ library to be tested. By default the
135 library folder of the build directory is used. This option cannot be used
Eric Fiselier60be2462016-12-23 19:09:14 +0000136 when use_system_cxx_lib is provided.
Eric Fiselier661b0c12016-04-20 04:17:39 +0000137
138
Eric Fiselier4fe629c2016-10-14 06:15:27 +0000139.. option:: cxx_runtime_root=<path/to/lib/>
Eric Fiselier661b0c12016-04-20 04:17:39 +0000140
141 Specify the directory of the libc++ library to use at runtime. This directory
142 is not added to the linkers search path. This can be used to compile tests
143 against one version of libc++ and run them using another. The default value
Louis Dionnee70b1dd2018-12-14 18:19:14 +0000144 for this option is `cxx_library_root`.
Eric Fiselierd720d1f2015-08-22 19:40:49 +0000145
Eric Fiselier60be2462016-12-23 19:09:14 +0000146.. option:: use_system_cxx_lib=<bool>
Eric Fiselierd720d1f2015-08-22 19:40:49 +0000147
148 **Default**: False
149
150 Enable or disable testing against the installed version of libc++ library.
151 Note: This does not use the installed headers.
152
Eric Fiselier4fe629c2016-10-14 06:15:27 +0000153.. option:: use_lit_shell=<bool>
Eric Fiselierd720d1f2015-08-22 19:40:49 +0000154
155 Enable or disable the use of LIT's internal shell in ShTests. If the
156 environment variable LIT_USE_INTERNAL_SHELL is present then that is used as
157 the default value. Otherwise the default value is True on Windows and False
158 on every other platform.
159
Eric Fiselier4fe629c2016-10-14 06:15:27 +0000160.. option:: compile_flags="<list-of-args>"
Eric Fiselierd720d1f2015-08-22 19:40:49 +0000161
162 Specify additional compile flags as a space delimited string.
163 Note: This options should not be used to change the standard version used.
164
Eric Fiselier4fe629c2016-10-14 06:15:27 +0000165.. option:: link_flags="<list-of-args>"
Eric Fiselierd720d1f2015-08-22 19:40:49 +0000166
167 Specify additional link flags as a space delimited string.
168
Eric Fiselier4fe629c2016-10-14 06:15:27 +0000169.. option:: debug_level=<level>
Eric Fiselierd720d1f2015-08-22 19:40:49 +0000170
171 **Values**: 0, 1
172
173 Enable the use of debug mode. Level 0 enables assertions and level 1 enables
174 assertions and debugging of iterator misuse.
175
176.. option:: use_sanitizer=<sanitizer name>
177
178 **Values**: Memory, MemoryWithOrigins, Address, Undefined
179
180 Run the tests using the given sanitizer. If LLVM_USE_SANITIZER was given when
181 building libc++ then that sanitizer will be used by default.
182
183.. option:: color_diagnostics
184
185 Enable the use of colorized compile diagnostics. If the color_diagnostics
186 option is specified or the environment variable LIBCXX_COLOR_DIAGNOSTICS is
187 present then color diagnostics will be enabled.
188
Petr Hosek7b9f43a2019-02-05 19:50:47 +0000189.. option:: llvm_unwinder
190
191 Enable the use of LLVM unwinder instead of libgcc.
192
193.. option:: builtins_library
194
195 Path to the builtins library to use instead of libgcc.
196
Eric Fiselierd720d1f2015-08-22 19:40:49 +0000197
198Environment Variables
199---------------------
200
Eric Fiselier71646ec2016-05-05 08:12:25 +0000201.. envvar:: LIBCXX_SITE_CONFIG=<path/to/lit.site.cfg>
Eric Fiselierd720d1f2015-08-22 19:40:49 +0000202
203 Specify the site configuration to use when running the tests.
Eric Fiselier887b86b2016-09-16 03:47:53 +0000204 Also see `libcxx_site_config`.
Eric Fiselierd720d1f2015-08-22 19:40:49 +0000205
206.. envvar:: LIBCXX_COLOR_DIAGNOSTICS
207
208 If ``LIBCXX_COLOR_DIAGNOSTICS`` is defined then the test suite will attempt
209 to use color diagnostic outputs from the compiler.
Eric Fiselier887b86b2016-09-16 03:47:53 +0000210 Also see `color_diagnostics`.
Eric Fiselier9e3e1372016-07-19 23:07:03 +0000211
Louis Dionne9e7cf262020-04-02 17:14:45 -0400212Writing Tests
213-------------
214
215When writing tests for the libc++ test suite, you should follow a few guidelines.
216This will ensure that your tests can run on a wide variety of hardware and under
217a wide variety of configurations. We have several unusual configurations such as
218building the tests on one host but running them on a different host, which add a
219few requirements to the test suite. Here's some stuff you should know:
220
221- All tests are run in a temporary directory that is unique to that test and
222 cleaned up after the test is done.
223- When a test needs data files as inputs, these data files can be saved in the
224 repository (when reasonable) and referrenced by the test as
225 ``// FILE_DEPENDENCIES: <path-to-dependencies>``. Copies of these files or
226 directories will be made available to the test in the temporary directory
227 where it is run.
228- You should never hardcode a path from the build-host in a test, because that
229 path will not necessarily be available on the host where the tests are run.
230- You should try to reduce the runtime dependencies of each test to the minimum.
231 For example, requiring Python to run a test is bad, since Python is not
232 necessarily available on all devices we may want to run the tests on (even
233 though supporting Python is probably trivial for the build-host).
234
Eric Fiselier9e3e1372016-07-19 23:07:03 +0000235Benchmarks
236==========
237
238Libc++ contains benchmark tests separately from the test of the test suite.
239The benchmarks are written using the `Google Benchmark`_ library, a copy of which
240is stored in the libc++ repository.
241
242For more information about using the Google Benchmark library see the
243`official documentation <https://github.com/google/benchmark>`_.
244
245.. _`Google Benchmark`: https://github.com/google/benchmark
246
247Building Benchmarks
248-------------------
249
Eric Fiselier93f212c2016-08-29 19:50:49 +0000250The benchmark tests are not built by default. The benchmarks can be built using
251the ``cxx-benchmarks`` target.
Eric Fiselier9e3e1372016-07-19 23:07:03 +0000252
253An example build would look like:
254
255.. code-block:: bash
256
257 $ cd build
Eric Fiselier93f212c2016-08-29 19:50:49 +0000258 $ cmake [options] <path to libcxx sources>
259 $ make cxx-benchmarks
Eric Fiselier9e3e1372016-07-19 23:07:03 +0000260
261This will build all of the benchmarks under ``<libcxx-src>/benchmarks`` to be
262built against the just-built libc++. The compiled tests are output into
263``build/benchmarks``.
264
265The benchmarks can also be built against the platforms native standard library
266using the ``-DLIBCXX_BUILD_BENCHMARKS_NATIVE_STDLIB=ON`` CMake option. This
267is useful for comparing the performance of libc++ to other standard libraries.
268The compiled benchmarks are named ``<test>.libcxx.out`` if they test libc++ and
269``<test>.native.out`` otherwise.
270
271Also See:
272
273 * :ref:`Building Libc++ <build instructions>`
274 * :ref:`CMake Options`
275
276Running Benchmarks
277------------------
278
279The benchmarks must be run manually by the user. Currently there is no way
280to run them as part of the build.
281
282For example:
283
284.. code-block:: bash
285
286 $ cd build/benchmarks
Eric Fiselier93f212c2016-08-29 19:50:49 +0000287 $ make cxx-benchmarks
Eric Fiselier9e3e1372016-07-19 23:07:03 +0000288 $ ./algorithms.libcxx.out # Runs all the benchmarks
289 $ ./algorithms.libcxx.out --benchmark_filter=BM_Sort.* # Only runs the sort benchmarks
290
291For more information about running benchmarks see `Google Benchmark`_.