xref: /netbsd-src/external/apache2/llvm/dist/libcxx/docs/TestingLibcxx.rst (revision 4d6fc14bc9b0c5bf3e30be318c143ee82cadd108)
1*4d6fc14bSjoerg==============
2*4d6fc14bSjoergTesting libc++
3*4d6fc14bSjoerg==============
4*4d6fc14bSjoerg
5*4d6fc14bSjoerg.. contents::
6*4d6fc14bSjoerg  :local:
7*4d6fc14bSjoerg
8*4d6fc14bSjoergGetting Started
9*4d6fc14bSjoerg===============
10*4d6fc14bSjoerg
11*4d6fc14bSjoerglibc++ uses LIT to configure and run its tests.
12*4d6fc14bSjoerg
13*4d6fc14bSjoergThe primary way to run the libc++ tests is by using ``make check-cxx``.
14*4d6fc14bSjoerg
15*4d6fc14bSjoergHowever since libc++ can be used in any number of possible
16*4d6fc14bSjoergconfigurations it is important to customize the way LIT builds and runs
17*4d6fc14bSjoergthe tests. This guide provides information on how to use LIT directly to
18*4d6fc14bSjoergtest libc++.
19*4d6fc14bSjoerg
20*4d6fc14bSjoergPlease see the `Lit Command Guide`_ for more information about LIT.
21*4d6fc14bSjoerg
22*4d6fc14bSjoerg.. _LIT Command Guide: https://llvm.org/docs/CommandGuide/lit.html
23*4d6fc14bSjoerg
24*4d6fc14bSjoergUsage
25*4d6fc14bSjoerg-----
26*4d6fc14bSjoerg
27*4d6fc14bSjoergAfter building libc++, you can run parts of the libc++ test suite by simply
28*4d6fc14bSjoergrunning ``llvm-lit`` on a specified test or directory. If you're unsure
29*4d6fc14bSjoergwhether the required libraries have been built, you can use the
30*4d6fc14bSjoerg`cxx-test-depends` target. For example:
31*4d6fc14bSjoerg
32*4d6fc14bSjoerg.. code-block:: bash
33*4d6fc14bSjoerg
34*4d6fc14bSjoerg  $ cd <monorepo-root>
35*4d6fc14bSjoerg  $ make -C <build> cxx-test-depends # If you want to make sure the targets get rebuilt
36*4d6fc14bSjoerg  $ <build>/bin/llvm-lit -sv libcxx/test/std/re # Run all of the std::regex tests
37*4d6fc14bSjoerg  $ <build>/bin/llvm-lit -sv libcxx/test/std/depr/depr.c.headers/stdlib_h.pass.cpp # Run a single test
38*4d6fc14bSjoerg  $ <build>/bin/llvm-lit -sv libcxx/test/std/atomics libcxx/test/std/threads # Test std::thread and std::atomic
39*4d6fc14bSjoerg
40*4d6fc14bSjoergIn the default configuration, the tests are built against headers that form a
41*4d6fc14bSjoergfake installation root of libc++. This installation root has to be updated when
42*4d6fc14bSjoergchanges are made to the headers, so you should re-run the `cxx-test-depends`
43*4d6fc14bSjoergtarget before running the tests manually with `lit` when you make any sort of
44*4d6fc14bSjoergchange, including to the headers.
45*4d6fc14bSjoerg
46*4d6fc14bSjoergSometimes you'll want to change the way LIT is running the tests. Custom options
47*4d6fc14bSjoergcan be specified using the `--param=<name>=<val>` flag. The most common option
48*4d6fc14bSjoergyou'll want to change is the standard dialect (ie -std=c++XX). By default the
49*4d6fc14bSjoergtest suite will select the newest C++ dialect supported by the compiler and use
50*4d6fc14bSjoergthat. However if you want to manually specify the option like so:
51*4d6fc14bSjoerg
52*4d6fc14bSjoerg.. code-block:: bash
53*4d6fc14bSjoerg
54*4d6fc14bSjoerg  $ <build>/bin/llvm-lit -sv libcxx/test/std/containers # Run the tests with the newest -std
55*4d6fc14bSjoerg  $ <build>/bin/llvm-lit -sv libcxx/test/std/containers --param=std=c++03 # Run the tests in C++03
56*4d6fc14bSjoerg
57*4d6fc14bSjoergOccasionally you'll want to add extra compile or link flags when testing.
58*4d6fc14bSjoergYou can do this as follows:
59*4d6fc14bSjoerg
60*4d6fc14bSjoerg.. code-block:: bash
61*4d6fc14bSjoerg
62*4d6fc14bSjoerg  $ <build>/bin/llvm-lit -sv libcxx/test --param=compile_flags='-Wcustom-warning'
63*4d6fc14bSjoerg  $ <build>/bin/llvm-lit -sv libcxx/test --param=link_flags='-L/custom/library/path'
64*4d6fc14bSjoerg
65*4d6fc14bSjoergSome other common examples include:
66*4d6fc14bSjoerg
67*4d6fc14bSjoerg.. code-block:: bash
68*4d6fc14bSjoerg
69*4d6fc14bSjoerg  # Specify a custom compiler.
70*4d6fc14bSjoerg  $ <build>/bin/llvm-lit -sv libcxx/test/std --param=cxx_under_test=/opt/bin/g++
71*4d6fc14bSjoerg
72*4d6fc14bSjoerg  # Disable warnings in the test suite
73*4d6fc14bSjoerg  $ <build>/bin/llvm-lit -sv libcxx/test --param=enable_warnings=False
74*4d6fc14bSjoerg
75*4d6fc14bSjoerg  # Use UBSAN when running the tests.
76*4d6fc14bSjoerg  $ <build>/bin/llvm-lit -sv libcxx/test --param=use_sanitizer=Undefined
77*4d6fc14bSjoerg
78*4d6fc14bSjoergUsing a custom site configuration
79*4d6fc14bSjoerg---------------------------------
80*4d6fc14bSjoerg
81*4d6fc14bSjoergBy default, the libc++ test suite will use a site configuration that matches
82*4d6fc14bSjoergthe current CMake configuration. It does so by generating a ``lit.site.cfg``
83*4d6fc14bSjoergfile in the build directory from one of the configuration file templates in
84*4d6fc14bSjoerg``libcxx/test/configs/``, and pointing ``llvm-lit`` (which is a wrapper around
85*4d6fc14bSjoerg``llvm/utils/lit/lit.py``) to that file. So when you're running
86*4d6fc14bSjoerg``<build>/bin/llvm-lit``, the generated ``lit.site.cfg`` file is always loaded
87*4d6fc14bSjoerginstead of ``libcxx/test/lit.cfg.py``. If you want to use a custom site
88*4d6fc14bSjoergconfiguration, simply point the CMake build to it using
89*4d6fc14bSjoerg``-DLIBCXX_TEST_CONFIG=<path-to-site-config>``, and that site configuration
90*4d6fc14bSjoergwill be used instead. That file can use CMake variables inside it to make
91*4d6fc14bSjoergconfiguration easier.
92*4d6fc14bSjoerg
93*4d6fc14bSjoerg   .. code-block:: bash
94*4d6fc14bSjoerg
95*4d6fc14bSjoerg     $ cmake <options> -DLIBCXX_TEST_CONFIG=<path-to-site-config>
96*4d6fc14bSjoerg     $ make -C <build> cxx-test-depends
97*4d6fc14bSjoerg     $ <build>/bin/llvm-lit -sv libcxx/test # will use your custom config file
98*4d6fc14bSjoerg
99*4d6fc14bSjoerg
100*4d6fc14bSjoergLIT Options
101*4d6fc14bSjoerg===========
102*4d6fc14bSjoerg
103*4d6fc14bSjoerg:program:`lit` [*options*...] [*filenames*...]
104*4d6fc14bSjoerg
105*4d6fc14bSjoergCommand Line Options
106*4d6fc14bSjoerg--------------------
107*4d6fc14bSjoerg
108*4d6fc14bSjoergTo use these options you pass them on the LIT command line as ``--param NAME``
109*4d6fc14bSjoergor ``--param NAME=VALUE``. Some options have default values specified during
110*4d6fc14bSjoergCMake's configuration. Passing the option on the command line will override the
111*4d6fc14bSjoergdefault.
112*4d6fc14bSjoerg
113*4d6fc14bSjoerg.. program:: lit
114*4d6fc14bSjoerg
115*4d6fc14bSjoerg.. option:: cxx_under_test=<path/to/compiler>
116*4d6fc14bSjoerg
117*4d6fc14bSjoerg  Specify the compiler used to build the tests.
118*4d6fc14bSjoerg
119*4d6fc14bSjoerg.. option:: stdlib=<stdlib name>
120*4d6fc14bSjoerg
121*4d6fc14bSjoerg  **Values**: libc++, libstdc++, msvc
122*4d6fc14bSjoerg
123*4d6fc14bSjoerg  Specify the C++ standard library being tested. The default is libc++ if this
124*4d6fc14bSjoerg  option is not provided. This option is intended to allow running the libc++
125*4d6fc14bSjoerg  test suite against other standard library implementations.
126*4d6fc14bSjoerg
127*4d6fc14bSjoerg.. option:: std=<standard version>
128*4d6fc14bSjoerg
129*4d6fc14bSjoerg  **Values**: c++03, c++11, c++14, c++17, c++20, c++2b
130*4d6fc14bSjoerg
131*4d6fc14bSjoerg  Change the standard version used when building the tests.
132*4d6fc14bSjoerg
133*4d6fc14bSjoerg.. option:: cxx_headers=<path/to/headers>
134*4d6fc14bSjoerg
135*4d6fc14bSjoerg  Specify the c++ standard library headers that are tested. By default the
136*4d6fc14bSjoerg  headers in the source tree are used.
137*4d6fc14bSjoerg
138*4d6fc14bSjoerg.. option:: cxx_library_root=<path/to/lib/>
139*4d6fc14bSjoerg
140*4d6fc14bSjoerg  Specify the directory of the libc++ library to be tested. By default the
141*4d6fc14bSjoerg  library folder of the build directory is used.
142*4d6fc14bSjoerg
143*4d6fc14bSjoerg
144*4d6fc14bSjoerg.. option:: cxx_runtime_root=<path/to/lib/>
145*4d6fc14bSjoerg
146*4d6fc14bSjoerg  Specify the directory of the libc++ library to use at runtime. This directory
147*4d6fc14bSjoerg  is not added to the linkers search path. This can be used to compile tests
148*4d6fc14bSjoerg  against one version of libc++ and run them using another. The default value
149*4d6fc14bSjoerg  for this option is `cxx_library_root`.
150*4d6fc14bSjoerg
151*4d6fc14bSjoerg.. option:: use_system_cxx_lib=<bool>
152*4d6fc14bSjoerg
153*4d6fc14bSjoerg  **Default**: False
154*4d6fc14bSjoerg
155*4d6fc14bSjoerg  Enable or disable testing against the installed version of libc++ library.
156*4d6fc14bSjoerg  This impacts whether the ``use_system_cxx_lib`` Lit feature is defined or
157*4d6fc14bSjoerg  not. The ``cxx_library_root`` and ``cxx_runtime_root`` parameters should
158*4d6fc14bSjoerg  still be used to specify the path of the library to link to and run against,
159*4d6fc14bSjoerg  respectively.
160*4d6fc14bSjoerg
161*4d6fc14bSjoerg.. option:: debug_level=<level>
162*4d6fc14bSjoerg
163*4d6fc14bSjoerg  **Values**: 0, 1
164*4d6fc14bSjoerg
165*4d6fc14bSjoerg  Enable the use of debug mode. Level 0 enables assertions and level 1 enables
166*4d6fc14bSjoerg  assertions and debugging of iterator misuse.
167*4d6fc14bSjoerg
168*4d6fc14bSjoerg.. option:: use_sanitizer=<sanitizer name>
169*4d6fc14bSjoerg
170*4d6fc14bSjoerg  **Values**: Memory, MemoryWithOrigins, Address, Undefined
171*4d6fc14bSjoerg
172*4d6fc14bSjoerg  Run the tests using the given sanitizer. If LLVM_USE_SANITIZER was given when
173*4d6fc14bSjoerg  building libc++ then that sanitizer will be used by default.
174*4d6fc14bSjoerg
175*4d6fc14bSjoerg.. option:: llvm_unwinder
176*4d6fc14bSjoerg
177*4d6fc14bSjoerg  Enable the use of LLVM unwinder instead of libgcc.
178*4d6fc14bSjoerg
179*4d6fc14bSjoerg.. option:: builtins_library
180*4d6fc14bSjoerg
181*4d6fc14bSjoerg  Path to the builtins library to use instead of libgcc.
182*4d6fc14bSjoerg
183*4d6fc14bSjoerg
184*4d6fc14bSjoergWriting Tests
185*4d6fc14bSjoerg-------------
186*4d6fc14bSjoerg
187*4d6fc14bSjoergWhen writing tests for the libc++ test suite, you should follow a few guidelines.
188*4d6fc14bSjoergThis will ensure that your tests can run on a wide variety of hardware and under
189*4d6fc14bSjoerga wide variety of configurations. We have several unusual configurations such as
190*4d6fc14bSjoergbuilding the tests on one host but running them on a different host, which add a
191*4d6fc14bSjoergfew requirements to the test suite. Here's some stuff you should know:
192*4d6fc14bSjoerg
193*4d6fc14bSjoerg- All tests are run in a temporary directory that is unique to that test and
194*4d6fc14bSjoerg  cleaned up after the test is done.
195*4d6fc14bSjoerg- When a test needs data files as inputs, these data files can be saved in the
196*4d6fc14bSjoerg  repository (when reasonable) and referenced by the test as
197*4d6fc14bSjoerg  ``// FILE_DEPENDENCIES: <path-to-dependencies>``. Copies of these files or
198*4d6fc14bSjoerg  directories will be made available to the test in the temporary directory
199*4d6fc14bSjoerg  where it is run.
200*4d6fc14bSjoerg- You should never hardcode a path from the build-host in a test, because that
201*4d6fc14bSjoerg  path will not necessarily be available on the host where the tests are run.
202*4d6fc14bSjoerg- You should try to reduce the runtime dependencies of each test to the minimum.
203*4d6fc14bSjoerg  For example, requiring Python to run a test is bad, since Python is not
204*4d6fc14bSjoerg  necessarily available on all devices we may want to run the tests on (even
205*4d6fc14bSjoerg  though supporting Python is probably trivial for the build-host).
206*4d6fc14bSjoerg
207*4d6fc14bSjoergBenchmarks
208*4d6fc14bSjoerg==========
209*4d6fc14bSjoerg
210*4d6fc14bSjoergLibc++ contains benchmark tests separately from the test of the test suite.
211*4d6fc14bSjoergThe benchmarks are written using the `Google Benchmark`_ library, a copy of which
212*4d6fc14bSjoergis stored in the libc++ repository.
213*4d6fc14bSjoerg
214*4d6fc14bSjoergFor more information about using the Google Benchmark library see the
215*4d6fc14bSjoerg`official documentation <https://github.com/google/benchmark>`_.
216*4d6fc14bSjoerg
217*4d6fc14bSjoerg.. _`Google Benchmark`: https://github.com/google/benchmark
218*4d6fc14bSjoerg
219*4d6fc14bSjoergBuilding Benchmarks
220*4d6fc14bSjoerg-------------------
221*4d6fc14bSjoerg
222*4d6fc14bSjoergThe benchmark tests are not built by default. The benchmarks can be built using
223*4d6fc14bSjoergthe ``cxx-benchmarks`` target.
224*4d6fc14bSjoerg
225*4d6fc14bSjoergAn example build would look like:
226*4d6fc14bSjoerg
227*4d6fc14bSjoerg.. code-block:: bash
228*4d6fc14bSjoerg
229*4d6fc14bSjoerg  $ cd build
230*4d6fc14bSjoerg  $ cmake [options] <path to libcxx sources>
231*4d6fc14bSjoerg  $ make cxx-benchmarks
232*4d6fc14bSjoerg
233*4d6fc14bSjoergThis will build all of the benchmarks under ``<libcxx-src>/benchmarks`` to be
234*4d6fc14bSjoergbuilt against the just-built libc++. The compiled tests are output into
235*4d6fc14bSjoerg``build/benchmarks``.
236*4d6fc14bSjoerg
237*4d6fc14bSjoergThe benchmarks can also be built against the platforms native standard library
238*4d6fc14bSjoergusing the ``-DLIBCXX_BUILD_BENCHMARKS_NATIVE_STDLIB=ON`` CMake option. This
239*4d6fc14bSjoergis useful for comparing the performance of libc++ to other standard libraries.
240*4d6fc14bSjoergThe compiled benchmarks are named ``<test>.libcxx.out`` if they test libc++ and
241*4d6fc14bSjoerg``<test>.native.out`` otherwise.
242*4d6fc14bSjoerg
243*4d6fc14bSjoergAlso See:
244*4d6fc14bSjoerg
245*4d6fc14bSjoerg  * :ref:`Building Libc++ <build instructions>`
246*4d6fc14bSjoerg  * :ref:`CMake Options`
247*4d6fc14bSjoerg
248*4d6fc14bSjoergRunning Benchmarks
249*4d6fc14bSjoerg------------------
250*4d6fc14bSjoerg
251*4d6fc14bSjoergThe benchmarks must be run manually by the user. Currently there is no way
252*4d6fc14bSjoergto run them as part of the build.
253*4d6fc14bSjoerg
254*4d6fc14bSjoergFor example:
255*4d6fc14bSjoerg
256*4d6fc14bSjoerg.. code-block:: bash
257*4d6fc14bSjoerg
258*4d6fc14bSjoerg  $ cd build/benchmarks
259*4d6fc14bSjoerg  $ make cxx-benchmarks
260*4d6fc14bSjoerg  $ ./algorithms.libcxx.out # Runs all the benchmarks
261*4d6fc14bSjoerg  $ ./algorithms.libcxx.out --benchmark_filter=BM_Sort.* # Only runs the sort benchmarks
262*4d6fc14bSjoerg
263*4d6fc14bSjoergFor more information about running benchmarks see `Google Benchmark`_.
264