xref: /aosp_15_r20/external/libcxx/docs/TestingLibcxx.rst (revision 58b9f456b02922dfdb1fad8a988d5fd8765ecb80)
1*58b9f456SAndroid Build Coastguard Worker==============
2*58b9f456SAndroid Build Coastguard WorkerTesting libc++
3*58b9f456SAndroid Build Coastguard Worker==============
4*58b9f456SAndroid Build Coastguard Worker
5*58b9f456SAndroid Build Coastguard Worker.. contents::
6*58b9f456SAndroid Build Coastguard Worker  :local:
7*58b9f456SAndroid Build Coastguard Worker
8*58b9f456SAndroid Build Coastguard WorkerGetting Started
9*58b9f456SAndroid Build Coastguard Worker===============
10*58b9f456SAndroid Build Coastguard Worker
11*58b9f456SAndroid Build Coastguard Workerlibc++ uses LIT to configure and run its tests. The primary way to run the
12*58b9f456SAndroid Build Coastguard Workerlibc++ tests is by using make check-libcxx. However since libc++ can be used
13*58b9f456SAndroid Build Coastguard Workerin any number of possible configurations it is important to customize the way
14*58b9f456SAndroid Build Coastguard WorkerLIT builds and runs the tests. This guide provides information on how to use
15*58b9f456SAndroid Build Coastguard WorkerLIT directly to test libc++.
16*58b9f456SAndroid Build Coastguard Worker
17*58b9f456SAndroid Build Coastguard WorkerPlease see the `Lit Command Guide`_ for more information about LIT.
18*58b9f456SAndroid Build Coastguard Worker
19*58b9f456SAndroid Build Coastguard Worker.. _LIT Command Guide: http://llvm.org/docs/CommandGuide/lit.html
20*58b9f456SAndroid Build Coastguard Worker
21*58b9f456SAndroid Build Coastguard WorkerSetting up the Environment
22*58b9f456SAndroid Build Coastguard Worker--------------------------
23*58b9f456SAndroid Build Coastguard Worker
24*58b9f456SAndroid Build Coastguard WorkerAfter building libc++ you must setup your environment to test libc++ using
25*58b9f456SAndroid Build Coastguard WorkerLIT.
26*58b9f456SAndroid Build Coastguard Worker
27*58b9f456SAndroid Build Coastguard Worker#. Create a shortcut to the actual lit executable so that you can invoke it
28*58b9f456SAndroid Build Coastguard Worker   easily from the command line.
29*58b9f456SAndroid Build Coastguard Worker
30*58b9f456SAndroid Build Coastguard Worker   .. code-block:: bash
31*58b9f456SAndroid Build Coastguard Worker
32*58b9f456SAndroid Build Coastguard Worker     $ alias lit='python path/to/llvm/utils/lit/lit.py'
33*58b9f456SAndroid Build Coastguard Worker
34*58b9f456SAndroid Build Coastguard Worker#. Tell LIT where to find your build configuration.
35*58b9f456SAndroid Build Coastguard Worker
36*58b9f456SAndroid Build Coastguard Worker   .. code-block:: bash
37*58b9f456SAndroid Build Coastguard Worker
38*58b9f456SAndroid Build Coastguard Worker     $ export LIBCXX_SITE_CONFIG=path/to/build-libcxx/test/lit.site.cfg
39*58b9f456SAndroid Build Coastguard Worker
40*58b9f456SAndroid Build Coastguard WorkerExample Usage
41*58b9f456SAndroid Build Coastguard Worker-------------
42*58b9f456SAndroid Build Coastguard Worker
43*58b9f456SAndroid Build Coastguard WorkerOnce you have your environment set up and you have built libc++ you can run
44*58b9f456SAndroid Build Coastguard Workerparts of the libc++ test suite by simply running `lit` on a specified test or
45*58b9f456SAndroid Build Coastguard Workerdirectory. For example:
46*58b9f456SAndroid Build Coastguard Worker
47*58b9f456SAndroid Build Coastguard Worker.. code-block:: bash
48*58b9f456SAndroid Build Coastguard Worker
49*58b9f456SAndroid Build Coastguard Worker  $ cd path/to/src/libcxx
50*58b9f456SAndroid Build Coastguard Worker  $ lit -sv test/std/re # Run all of the std::regex tests
51*58b9f456SAndroid Build Coastguard Worker  $ lit -sv test/std/depr/depr.c.headers/stdlib_h.pass.cpp # Run a single test
52*58b9f456SAndroid Build Coastguard Worker  $ lit -sv test/std/atomics test/std/threads # Test std::thread and std::atomic
53*58b9f456SAndroid Build Coastguard Worker
54*58b9f456SAndroid Build Coastguard WorkerSometimes you'll want to change the way LIT is running the tests. Custom options
55*58b9f456SAndroid Build Coastguard Workercan be specified using the `--param=<name>=<val>` flag. The most common option
56*58b9f456SAndroid Build Coastguard Workeryou'll want to change is the standard dialect (ie -std=c++XX). By default the
57*58b9f456SAndroid Build Coastguard Workertest suite will select the newest C++ dialect supported by the compiler and use
58*58b9f456SAndroid Build Coastguard Workerthat. However if you want to manually specify the option like so:
59*58b9f456SAndroid Build Coastguard Worker
60*58b9f456SAndroid Build Coastguard Worker.. code-block:: bash
61*58b9f456SAndroid Build Coastguard Worker
62*58b9f456SAndroid Build Coastguard Worker  $ lit -sv test/std/containers # Run the tests with the newest -std
63*58b9f456SAndroid Build Coastguard Worker  $ lit -sv --param=std=c++03 test/std/containers # Run the tests in C++03
64*58b9f456SAndroid Build Coastguard Worker
65*58b9f456SAndroid Build Coastguard WorkerOccasionally you'll want to add extra compile or link flags when testing.
66*58b9f456SAndroid Build Coastguard WorkerYou can do this as follows:
67*58b9f456SAndroid Build Coastguard Worker
68*58b9f456SAndroid Build Coastguard Worker.. code-block:: bash
69*58b9f456SAndroid Build Coastguard Worker
70*58b9f456SAndroid Build Coastguard Worker  $ lit -sv --param=compile_flags='-Wcustom-warning'
71*58b9f456SAndroid Build Coastguard Worker  $ lit -sv --param=link_flags='-L/custom/library/path'
72*58b9f456SAndroid Build Coastguard Worker
73*58b9f456SAndroid Build Coastguard WorkerSome other common examples include:
74*58b9f456SAndroid Build Coastguard Worker
75*58b9f456SAndroid Build Coastguard Worker.. code-block:: bash
76*58b9f456SAndroid Build Coastguard Worker
77*58b9f456SAndroid Build Coastguard Worker  # Specify a custom compiler.
78*58b9f456SAndroid Build Coastguard Worker  $ lit -sv --param=cxx_under_test=/opt/bin/g++ test/std
79*58b9f456SAndroid Build Coastguard Worker
80*58b9f456SAndroid Build Coastguard Worker  # Enable warnings in the test suite
81*58b9f456SAndroid Build Coastguard Worker  $ lit -sv --param=enable_warnings=true test/std
82*58b9f456SAndroid Build Coastguard Worker
83*58b9f456SAndroid Build Coastguard Worker  # Use UBSAN when running the tests.
84*58b9f456SAndroid Build Coastguard Worker  $ lit -sv --param=use_sanitizer=Undefined
85*58b9f456SAndroid Build Coastguard Worker
86*58b9f456SAndroid Build Coastguard Worker
87*58b9f456SAndroid Build Coastguard WorkerLIT Options
88*58b9f456SAndroid Build Coastguard Worker===========
89*58b9f456SAndroid Build Coastguard Worker
90*58b9f456SAndroid Build Coastguard Worker:program:`lit` [*options*...] [*filenames*...]
91*58b9f456SAndroid Build Coastguard Worker
92*58b9f456SAndroid Build Coastguard WorkerCommand Line Options
93*58b9f456SAndroid Build Coastguard Worker--------------------
94*58b9f456SAndroid Build Coastguard Worker
95*58b9f456SAndroid Build Coastguard WorkerTo use these options you pass them on the LIT command line as --param NAME or
96*58b9f456SAndroid Build Coastguard Worker--param NAME=VALUE. Some options have default values specified during CMake's
97*58b9f456SAndroid Build Coastguard Workerconfiguration. Passing the option on the command line will override the default.
98*58b9f456SAndroid Build Coastguard Worker
99*58b9f456SAndroid Build Coastguard Worker.. program:: lit
100*58b9f456SAndroid Build Coastguard Worker
101*58b9f456SAndroid Build Coastguard Worker.. option:: cxx_under_test=<path/to/compiler>
102*58b9f456SAndroid Build Coastguard Worker
103*58b9f456SAndroid Build Coastguard Worker  Specify the compiler used to build the tests.
104*58b9f456SAndroid Build Coastguard Worker
105*58b9f456SAndroid Build Coastguard Worker.. option:: cxx_stdlib_under_test=<stdlib name>
106*58b9f456SAndroid Build Coastguard Worker
107*58b9f456SAndroid Build Coastguard Worker  **Values**: libc++, libstdc++
108*58b9f456SAndroid Build Coastguard Worker
109*58b9f456SAndroid Build Coastguard Worker  Specify the C++ standard library being tested. Unless otherwise specified
110*58b9f456SAndroid Build Coastguard Worker  libc++ is used. This option is intended to allow running the libc++ test
111*58b9f456SAndroid Build Coastguard Worker  suite against other standard library implementations.
112*58b9f456SAndroid Build Coastguard Worker
113*58b9f456SAndroid Build Coastguard Worker.. option:: std=<standard version>
114*58b9f456SAndroid Build Coastguard Worker
115*58b9f456SAndroid Build Coastguard Worker  **Values**: c++98, c++03, c++11, c++14, c++17, c++2a
116*58b9f456SAndroid Build Coastguard Worker
117*58b9f456SAndroid Build Coastguard Worker  Change the standard version used when building the tests.
118*58b9f456SAndroid Build Coastguard Worker
119*58b9f456SAndroid Build Coastguard Worker.. option:: libcxx_site_config=<path/to/lit.site.cfg>
120*58b9f456SAndroid Build Coastguard Worker
121*58b9f456SAndroid Build Coastguard Worker  Specify the site configuration to use when running the tests.  This option
122*58b9f456SAndroid Build Coastguard Worker  overrides the environment variable LIBCXX_SITE_CONFIG.
123*58b9f456SAndroid Build Coastguard Worker
124*58b9f456SAndroid Build Coastguard Worker.. option:: cxx_headers=<path/to/headers>
125*58b9f456SAndroid Build Coastguard Worker
126*58b9f456SAndroid Build Coastguard Worker  Specify the c++ standard library headers that are tested. By default the
127*58b9f456SAndroid Build Coastguard Worker  headers in the source tree are used.
128*58b9f456SAndroid Build Coastguard Worker
129*58b9f456SAndroid Build Coastguard Worker.. option:: cxx_library_root=<path/to/lib/>
130*58b9f456SAndroid Build Coastguard Worker
131*58b9f456SAndroid Build Coastguard Worker  Specify the directory of the libc++ library to be tested. By default the
132*58b9f456SAndroid Build Coastguard Worker  library folder of the build directory is used. This option cannot be used
133*58b9f456SAndroid Build Coastguard Worker  when use_system_cxx_lib is provided.
134*58b9f456SAndroid Build Coastguard Worker
135*58b9f456SAndroid Build Coastguard Worker
136*58b9f456SAndroid Build Coastguard Worker.. option:: cxx_runtime_root=<path/to/lib/>
137*58b9f456SAndroid Build Coastguard Worker
138*58b9f456SAndroid Build Coastguard Worker  Specify the directory of the libc++ library to use at runtime. This directory
139*58b9f456SAndroid Build Coastguard Worker  is not added to the linkers search path. This can be used to compile tests
140*58b9f456SAndroid Build Coastguard Worker  against one version of libc++ and run them using another. The default value
141*58b9f456SAndroid Build Coastguard Worker  for this option is `cxx_library_root`.
142*58b9f456SAndroid Build Coastguard Worker
143*58b9f456SAndroid Build Coastguard Worker.. option:: use_system_cxx_lib=<bool>
144*58b9f456SAndroid Build Coastguard Worker
145*58b9f456SAndroid Build Coastguard Worker  **Default**: False
146*58b9f456SAndroid Build Coastguard Worker
147*58b9f456SAndroid Build Coastguard Worker  Enable or disable testing against the installed version of libc++ library.
148*58b9f456SAndroid Build Coastguard Worker  Note: This does not use the installed headers.
149*58b9f456SAndroid Build Coastguard Worker
150*58b9f456SAndroid Build Coastguard Worker.. option:: use_lit_shell=<bool>
151*58b9f456SAndroid Build Coastguard Worker
152*58b9f456SAndroid Build Coastguard Worker  Enable or disable the use of LIT's internal shell in ShTests. If the
153*58b9f456SAndroid Build Coastguard Worker  environment variable LIT_USE_INTERNAL_SHELL is present then that is used as
154*58b9f456SAndroid Build Coastguard Worker  the default value. Otherwise the default value is True on Windows and False
155*58b9f456SAndroid Build Coastguard Worker  on every other platform.
156*58b9f456SAndroid Build Coastguard Worker
157*58b9f456SAndroid Build Coastguard Worker.. option:: compile_flags="<list-of-args>"
158*58b9f456SAndroid Build Coastguard Worker
159*58b9f456SAndroid Build Coastguard Worker  Specify additional compile flags as a space delimited string.
160*58b9f456SAndroid Build Coastguard Worker  Note: This options should not be used to change the standard version used.
161*58b9f456SAndroid Build Coastguard Worker
162*58b9f456SAndroid Build Coastguard Worker.. option:: link_flags="<list-of-args>"
163*58b9f456SAndroid Build Coastguard Worker
164*58b9f456SAndroid Build Coastguard Worker  Specify additional link flags as a space delimited string.
165*58b9f456SAndroid Build Coastguard Worker
166*58b9f456SAndroid Build Coastguard Worker.. option:: debug_level=<level>
167*58b9f456SAndroid Build Coastguard Worker
168*58b9f456SAndroid Build Coastguard Worker  **Values**: 0, 1
169*58b9f456SAndroid Build Coastguard Worker
170*58b9f456SAndroid Build Coastguard Worker  Enable the use of debug mode. Level 0 enables assertions and level 1 enables
171*58b9f456SAndroid Build Coastguard Worker  assertions and debugging of iterator misuse.
172*58b9f456SAndroid Build Coastguard Worker
173*58b9f456SAndroid Build Coastguard Worker.. option:: use_sanitizer=<sanitizer name>
174*58b9f456SAndroid Build Coastguard Worker
175*58b9f456SAndroid Build Coastguard Worker  **Values**: Memory, MemoryWithOrigins, Address, Undefined
176*58b9f456SAndroid Build Coastguard Worker
177*58b9f456SAndroid Build Coastguard Worker  Run the tests using the given sanitizer. If LLVM_USE_SANITIZER was given when
178*58b9f456SAndroid Build Coastguard Worker  building libc++ then that sanitizer will be used by default.
179*58b9f456SAndroid Build Coastguard Worker
180*58b9f456SAndroid Build Coastguard Worker.. option:: color_diagnostics
181*58b9f456SAndroid Build Coastguard Worker
182*58b9f456SAndroid Build Coastguard Worker  Enable the use of colorized compile diagnostics. If the color_diagnostics
183*58b9f456SAndroid Build Coastguard Worker  option is specified or the environment variable LIBCXX_COLOR_DIAGNOSTICS is
184*58b9f456SAndroid Build Coastguard Worker  present then color diagnostics will be enabled.
185*58b9f456SAndroid Build Coastguard Worker
186*58b9f456SAndroid Build Coastguard Worker
187*58b9f456SAndroid Build Coastguard WorkerEnvironment Variables
188*58b9f456SAndroid Build Coastguard Worker---------------------
189*58b9f456SAndroid Build Coastguard Worker
190*58b9f456SAndroid Build Coastguard Worker.. envvar:: LIBCXX_SITE_CONFIG=<path/to/lit.site.cfg>
191*58b9f456SAndroid Build Coastguard Worker
192*58b9f456SAndroid Build Coastguard Worker  Specify the site configuration to use when running the tests.
193*58b9f456SAndroid Build Coastguard Worker  Also see `libcxx_site_config`.
194*58b9f456SAndroid Build Coastguard Worker
195*58b9f456SAndroid Build Coastguard Worker.. envvar:: LIBCXX_COLOR_DIAGNOSTICS
196*58b9f456SAndroid Build Coastguard Worker
197*58b9f456SAndroid Build Coastguard Worker  If ``LIBCXX_COLOR_DIAGNOSTICS`` is defined then the test suite will attempt
198*58b9f456SAndroid Build Coastguard Worker  to use color diagnostic outputs from the compiler.
199*58b9f456SAndroid Build Coastguard Worker  Also see `color_diagnostics`.
200*58b9f456SAndroid Build Coastguard Worker
201*58b9f456SAndroid Build Coastguard WorkerBenchmarks
202*58b9f456SAndroid Build Coastguard Worker==========
203*58b9f456SAndroid Build Coastguard Worker
204*58b9f456SAndroid Build Coastguard WorkerLibc++ contains benchmark tests separately from the test of the test suite.
205*58b9f456SAndroid Build Coastguard WorkerThe benchmarks are written using the `Google Benchmark`_ library, a copy of which
206*58b9f456SAndroid Build Coastguard Workeris stored in the libc++ repository.
207*58b9f456SAndroid Build Coastguard Worker
208*58b9f456SAndroid Build Coastguard WorkerFor more information about using the Google Benchmark library see the
209*58b9f456SAndroid Build Coastguard Worker`official documentation <https://github.com/google/benchmark>`_.
210*58b9f456SAndroid Build Coastguard Worker
211*58b9f456SAndroid Build Coastguard Worker.. _`Google Benchmark`: https://github.com/google/benchmark
212*58b9f456SAndroid Build Coastguard Worker
213*58b9f456SAndroid Build Coastguard WorkerBuilding Benchmarks
214*58b9f456SAndroid Build Coastguard Worker-------------------
215*58b9f456SAndroid Build Coastguard Worker
216*58b9f456SAndroid Build Coastguard WorkerThe benchmark tests are not built by default. The benchmarks can be built using
217*58b9f456SAndroid Build Coastguard Workerthe ``cxx-benchmarks`` target.
218*58b9f456SAndroid Build Coastguard Worker
219*58b9f456SAndroid Build Coastguard WorkerAn example build would look like:
220*58b9f456SAndroid Build Coastguard Worker
221*58b9f456SAndroid Build Coastguard Worker.. code-block:: bash
222*58b9f456SAndroid Build Coastguard Worker
223*58b9f456SAndroid Build Coastguard Worker  $ cd build
224*58b9f456SAndroid Build Coastguard Worker  $ cmake [options] <path to libcxx sources>
225*58b9f456SAndroid Build Coastguard Worker  $ make cxx-benchmarks
226*58b9f456SAndroid Build Coastguard Worker
227*58b9f456SAndroid Build Coastguard WorkerThis will build all of the benchmarks under ``<libcxx-src>/benchmarks`` to be
228*58b9f456SAndroid Build Coastguard Workerbuilt against the just-built libc++. The compiled tests are output into
229*58b9f456SAndroid Build Coastguard Worker``build/benchmarks``.
230*58b9f456SAndroid Build Coastguard Worker
231*58b9f456SAndroid Build Coastguard WorkerThe benchmarks can also be built against the platforms native standard library
232*58b9f456SAndroid Build Coastguard Workerusing the ``-DLIBCXX_BUILD_BENCHMARKS_NATIVE_STDLIB=ON`` CMake option. This
233*58b9f456SAndroid Build Coastguard Workeris useful for comparing the performance of libc++ to other standard libraries.
234*58b9f456SAndroid Build Coastguard WorkerThe compiled benchmarks are named ``<test>.libcxx.out`` if they test libc++ and
235*58b9f456SAndroid Build Coastguard Worker``<test>.native.out`` otherwise.
236*58b9f456SAndroid Build Coastguard Worker
237*58b9f456SAndroid Build Coastguard WorkerAlso See:
238*58b9f456SAndroid Build Coastguard Worker
239*58b9f456SAndroid Build Coastguard Worker  * :ref:`Building Libc++ <build instructions>`
240*58b9f456SAndroid Build Coastguard Worker  * :ref:`CMake Options`
241*58b9f456SAndroid Build Coastguard Worker
242*58b9f456SAndroid Build Coastguard WorkerRunning Benchmarks
243*58b9f456SAndroid Build Coastguard Worker------------------
244*58b9f456SAndroid Build Coastguard Worker
245*58b9f456SAndroid Build Coastguard WorkerThe benchmarks must be run manually by the user. Currently there is no way
246*58b9f456SAndroid Build Coastguard Workerto run them as part of the build.
247*58b9f456SAndroid Build Coastguard Worker
248*58b9f456SAndroid Build Coastguard WorkerFor example:
249*58b9f456SAndroid Build Coastguard Worker
250*58b9f456SAndroid Build Coastguard Worker.. code-block:: bash
251*58b9f456SAndroid Build Coastguard Worker
252*58b9f456SAndroid Build Coastguard Worker  $ cd build/benchmarks
253*58b9f456SAndroid Build Coastguard Worker  $ make cxx-benchmarks
254*58b9f456SAndroid Build Coastguard Worker  $ ./algorithms.libcxx.out # Runs all the benchmarks
255*58b9f456SAndroid Build Coastguard Worker  $ ./algorithms.libcxx.out --benchmark_filter=BM_Sort.* # Only runs the sort benchmarks
256*58b9f456SAndroid Build Coastguard Worker
257*58b9f456SAndroid Build Coastguard WorkerFor more information about running benchmarks see `Google Benchmark`_.
258