blob: 683d5e0d9564abf11ad6d57eaf8cc460136f18ea [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
28running ``llvm-lit`` on a specified test or directory. For example:
Eric Fiselier4ac88092015-10-14 20:44:44 +000029
30.. code-block:: bash
31
Louis Dionne089ced02020-04-07 15:02:37 -040032 $ cd <monorepo-root>
33 $ <build>/bin/llvm-lit -sv libcxx/test/std/re # Run all of the std::regex tests
34 $ <build>/bin/llvm-lit -sv libcxx/test/std/depr/depr.c.headers/stdlib_h.pass.cpp # Run a single test
35 $ <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 +000036
37Sometimes you'll want to change the way LIT is running the tests. Custom options
38can be specified using the `--param=<name>=<val>` flag. The most common option
39you'll want to change is the standard dialect (ie -std=c++XX). By default the
40test suite will select the newest C++ dialect supported by the compiler and use
41that. However if you want to manually specify the option like so:
42
43.. code-block:: bash
44
Louis Dionne089ced02020-04-07 15:02:37 -040045 $ <build>/bin/llvm-lit -sv libcxx/test/std/containers # Run the tests with the newest -std
46 $ <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 +000047
48Occasionally you'll want to add extra compile or link flags when testing.
49You can do this as follows:
50
51.. code-block:: bash
52
Louis Dionne089ced02020-04-07 15:02:37 -040053 $ <build>/bin/llvm-lit -sv libcxx/test --param=compile_flags='-Wcustom-warning'
54 $ <build>/bin/llvm-lit -sv libcxx/test --param=link_flags='-L/custom/library/path'
Eric Fiselier4ac88092015-10-14 20:44:44 +000055
56Some other common examples include:
57
58.. code-block:: bash
59
60 # Specify a custom compiler.
Louis Dionne089ced02020-04-07 15:02:37 -040061 $ <build>/bin/llvm-lit -sv libcxx/test/std --param=cxx_under_test=/opt/bin/g++
Eric Fiselier4ac88092015-10-14 20:44:44 +000062
63 # Enable warnings in the test suite
Louis Dionne089ced02020-04-07 15:02:37 -040064 $ <build>/bin/llvm-lit -sv libcxx/test --param=enable_warnings=true
Eric Fiselier4ac88092015-10-14 20:44:44 +000065
66 # Use UBSAN when running the tests.
Louis Dionne089ced02020-04-07 15:02:37 -040067 $ <build>/bin/llvm-lit -sv libcxx/test --param=use_sanitizer=Undefined
Eric Fiselier4ac88092015-10-14 20:44:44 +000068
Louis Dionne089ced02020-04-07 15:02:37 -040069Using a custom site configuration
70---------------------------------
71
72By default, the libc++ test suite will use a site configuration that matches
73the current CMake configuration. It does so by generating a ``lit.site.cfg``
74file in the build directory from the ``libcxx/test/lit.site.cfg.in`` template,
75and pointing ``llvm-lit`` (which is a wrapper around ``llvm/utils/lit/lit.py``)
76to that file. So when you're running ``<build>/bin/llvm-lit``, the generated
77``lit.site.cfg`` file is always loaded first, followed by the actual config in
78``libcxx/test/lit.cfg``. However, it is sometimes desirable to use a custom
79site configuration. To do that, you can use ``--param=libcxx_site_config`` or
80the ``LIBCXX_SITE_CONFIG`` environment variable to point to the right site
81configuration file. However, you must stop using ``llvm-lit``, or else the
82generated ``lit.site.cfg`` will still be preferred:
83
84 .. code-block:: bash
85
86 $ LIBCXX_SITE_CONFIG=path/to/your/site/configuration llvm/utils/lit/lit.py -sv ...
87
88 $ llvm/utils/lit/lit.py -sv ... --param=libcxx_site_config=path/to/your/site/configuration
89
90In both of these cases, your custom site configuration should set up the
91``config`` object in a way that is compatible with what libc++'s ``config.py``
92module expects.
Eric Fiselierd720d1f2015-08-22 19:40:49 +000093
94LIT Options
95===========
96
97:program:`lit` [*options*...] [*filenames*...]
98
99Command Line Options
100--------------------
101
102To use these options you pass them on the LIT command line as --param NAME or
103--param NAME=VALUE. Some options have default values specified during CMake's
104configuration. Passing the option on the command line will override the default.
105
106.. program:: lit
107
Eric Fiselier4fe629c2016-10-14 06:15:27 +0000108.. option:: cxx_under_test=<path/to/compiler>
Eric Fiselier4ac88092015-10-14 20:44:44 +0000109
110 Specify the compiler used to build the tests.
111
Eric Fiselier4fe629c2016-10-14 06:15:27 +0000112.. option:: cxx_stdlib_under_test=<stdlib name>
113
114 **Values**: libc++, libstdc++
Eric Fiselierbbbbd112016-10-12 00:00:37 +0000115
116 Specify the C++ standard library being tested. Unless otherwise specified
117 libc++ is used. This option is intended to allow running the libc++ test
118 suite against other standard library implementations.
119
Eric Fiselier4ac88092015-10-14 20:44:44 +0000120.. option:: std=<standard version>
121
Eric Fiselier32f3f702017-11-07 20:26:23 +0000122 **Values**: c++98, c++03, c++11, c++14, c++17, c++2a
Eric Fiselier4ac88092015-10-14 20:44:44 +0000123
124 Change the standard version used when building the tests.
125
Eric Fiselier4fe629c2016-10-14 06:15:27 +0000126.. option:: libcxx_site_config=<path/to/lit.site.cfg>
Eric Fiselierd720d1f2015-08-22 19:40:49 +0000127
128 Specify the site configuration to use when running the tests. This option
Eric Fiselier17ec5b12017-05-09 23:47:20 +0000129 overrides the environment variable LIBCXX_SITE_CONFIG.
Eric Fiselierd720d1f2015-08-22 19:40:49 +0000130
Eric Fiselier4fe629c2016-10-14 06:15:27 +0000131.. option:: cxx_headers=<path/to/headers>
Eric Fiselierd720d1f2015-08-22 19:40:49 +0000132
Eric Fiselierbbbbd112016-10-12 00:00:37 +0000133 Specify the c++ standard library headers that are tested. By default the
134 headers in the source tree are used.
Eric Fiselierd720d1f2015-08-22 19:40:49 +0000135
Eric Fiselier4fe629c2016-10-14 06:15:27 +0000136.. option:: cxx_library_root=<path/to/lib/>
Eric Fiselierd720d1f2015-08-22 19:40:49 +0000137
Eric Fiselier661b0c12016-04-20 04:17:39 +0000138 Specify the directory of the libc++ library to be tested. By default the
139 library folder of the build directory is used. This option cannot be used
Eric Fiselier60be2462016-12-23 19:09:14 +0000140 when use_system_cxx_lib is provided.
Eric Fiselier661b0c12016-04-20 04:17:39 +0000141
142
Eric Fiselier4fe629c2016-10-14 06:15:27 +0000143.. option:: cxx_runtime_root=<path/to/lib/>
Eric Fiselier661b0c12016-04-20 04:17:39 +0000144
145 Specify the directory of the libc++ library to use at runtime. This directory
146 is not added to the linkers search path. This can be used to compile tests
147 against one version of libc++ and run them using another. The default value
Louis Dionnee70b1dd2018-12-14 18:19:14 +0000148 for this option is `cxx_library_root`.
Eric Fiselierd720d1f2015-08-22 19:40:49 +0000149
Eric Fiselier60be2462016-12-23 19:09:14 +0000150.. option:: use_system_cxx_lib=<bool>
Eric Fiselierd720d1f2015-08-22 19:40:49 +0000151
152 **Default**: False
153
154 Enable or disable testing against the installed version of libc++ library.
155 Note: This does not use the installed headers.
156
Eric Fiselier4fe629c2016-10-14 06:15:27 +0000157.. option:: use_lit_shell=<bool>
Eric Fiselierd720d1f2015-08-22 19:40:49 +0000158
159 Enable or disable the use of LIT's internal shell in ShTests. If the
160 environment variable LIT_USE_INTERNAL_SHELL is present then that is used as
161 the default value. Otherwise the default value is True on Windows and False
162 on every other platform.
163
Eric Fiselier4fe629c2016-10-14 06:15:27 +0000164.. option:: compile_flags="<list-of-args>"
Eric Fiselierd720d1f2015-08-22 19:40:49 +0000165
166 Specify additional compile flags as a space delimited string.
167 Note: This options should not be used to change the standard version used.
168
Eric Fiselier4fe629c2016-10-14 06:15:27 +0000169.. option:: link_flags="<list-of-args>"
Eric Fiselierd720d1f2015-08-22 19:40:49 +0000170
171 Specify additional link flags as a space delimited string.
172
Eric Fiselier4fe629c2016-10-14 06:15:27 +0000173.. option:: debug_level=<level>
Eric Fiselierd720d1f2015-08-22 19:40:49 +0000174
175 **Values**: 0, 1
176
177 Enable the use of debug mode. Level 0 enables assertions and level 1 enables
178 assertions and debugging of iterator misuse.
179
180.. option:: use_sanitizer=<sanitizer name>
181
182 **Values**: Memory, MemoryWithOrigins, Address, Undefined
183
184 Run the tests using the given sanitizer. If LLVM_USE_SANITIZER was given when
185 building libc++ then that sanitizer will be used by default.
186
187.. option:: color_diagnostics
188
189 Enable the use of colorized compile diagnostics. If the color_diagnostics
190 option is specified or the environment variable LIBCXX_COLOR_DIAGNOSTICS is
191 present then color diagnostics will be enabled.
192
Petr Hosek7b9f43a2019-02-05 19:50:47 +0000193.. option:: llvm_unwinder
194
195 Enable the use of LLVM unwinder instead of libgcc.
196
197.. option:: builtins_library
198
199 Path to the builtins library to use instead of libgcc.
200
Eric Fiselierd720d1f2015-08-22 19:40:49 +0000201
202Environment Variables
203---------------------
204
Eric Fiselier71646ec2016-05-05 08:12:25 +0000205.. envvar:: LIBCXX_SITE_CONFIG=<path/to/lit.site.cfg>
Eric Fiselierd720d1f2015-08-22 19:40:49 +0000206
207 Specify the site configuration to use when running the tests.
Eric Fiselier887b86b2016-09-16 03:47:53 +0000208 Also see `libcxx_site_config`.
Eric Fiselierd720d1f2015-08-22 19:40:49 +0000209
210.. envvar:: LIBCXX_COLOR_DIAGNOSTICS
211
212 If ``LIBCXX_COLOR_DIAGNOSTICS`` is defined then the test suite will attempt
213 to use color diagnostic outputs from the compiler.
Eric Fiselier887b86b2016-09-16 03:47:53 +0000214 Also see `color_diagnostics`.
Eric Fiselier9e3e1372016-07-19 23:07:03 +0000215
Louis Dionne9e7cf262020-04-02 17:14:45 -0400216Writing Tests
217-------------
218
219When writing tests for the libc++ test suite, you should follow a few guidelines.
220This will ensure that your tests can run on a wide variety of hardware and under
221a wide variety of configurations. We have several unusual configurations such as
222building the tests on one host but running them on a different host, which add a
223few requirements to the test suite. Here's some stuff you should know:
224
225- All tests are run in a temporary directory that is unique to that test and
226 cleaned up after the test is done.
227- When a test needs data files as inputs, these data files can be saved in the
228 repository (when reasonable) and referrenced by the test as
229 ``// FILE_DEPENDENCIES: <path-to-dependencies>``. Copies of these files or
230 directories will be made available to the test in the temporary directory
231 where it is run.
232- You should never hardcode a path from the build-host in a test, because that
233 path will not necessarily be available on the host where the tests are run.
234- You should try to reduce the runtime dependencies of each test to the minimum.
235 For example, requiring Python to run a test is bad, since Python is not
236 necessarily available on all devices we may want to run the tests on (even
237 though supporting Python is probably trivial for the build-host).
238
Eric Fiselier9e3e1372016-07-19 23:07:03 +0000239Benchmarks
240==========
241
242Libc++ contains benchmark tests separately from the test of the test suite.
243The benchmarks are written using the `Google Benchmark`_ library, a copy of which
244is stored in the libc++ repository.
245
246For more information about using the Google Benchmark library see the
247`official documentation <https://github.com/google/benchmark>`_.
248
249.. _`Google Benchmark`: https://github.com/google/benchmark
250
251Building Benchmarks
252-------------------
253
Eric Fiselier93f212c2016-08-29 19:50:49 +0000254The benchmark tests are not built by default. The benchmarks can be built using
255the ``cxx-benchmarks`` target.
Eric Fiselier9e3e1372016-07-19 23:07:03 +0000256
257An example build would look like:
258
259.. code-block:: bash
260
261 $ cd build
Eric Fiselier93f212c2016-08-29 19:50:49 +0000262 $ cmake [options] <path to libcxx sources>
263 $ make cxx-benchmarks
Eric Fiselier9e3e1372016-07-19 23:07:03 +0000264
265This will build all of the benchmarks under ``<libcxx-src>/benchmarks`` to be
266built against the just-built libc++. The compiled tests are output into
267``build/benchmarks``.
268
269The benchmarks can also be built against the platforms native standard library
270using the ``-DLIBCXX_BUILD_BENCHMARKS_NATIVE_STDLIB=ON`` CMake option. This
271is useful for comparing the performance of libc++ to other standard libraries.
272The compiled benchmarks are named ``<test>.libcxx.out`` if they test libc++ and
273``<test>.native.out`` otherwise.
274
275Also See:
276
277 * :ref:`Building Libc++ <build instructions>`
278 * :ref:`CMake Options`
279
280Running Benchmarks
281------------------
282
283The benchmarks must be run manually by the user. Currently there is no way
284to run them as part of the build.
285
286For example:
287
288.. code-block:: bash
289
290 $ cd build/benchmarks
Eric Fiselier93f212c2016-08-29 19:50:49 +0000291 $ make cxx-benchmarks
Eric Fiselier9e3e1372016-07-19 23:07:03 +0000292 $ ./algorithms.libcxx.out # Runs all the benchmarks
293 $ ./algorithms.libcxx.out --benchmark_filter=BM_Sort.* # Only runs the sort benchmarks
294
295For more information about running benchmarks see `Google Benchmark`_.