Eric Fiselier | d720d1f | 2015-08-22 19:40:49 +0000 | [diff] [blame] | 1 | ============== |
| 2 | Testing libc++ |
| 3 | ============== |
| 4 | |
| 5 | .. contents:: |
| 6 | :local: |
| 7 | |
| 8 | Getting Started |
| 9 | =============== |
| 10 | |
Louis Dionne | f58d829 | 2020-03-11 17:03:00 -0400 | [diff] [blame] | 11 | libc++ uses LIT to configure and run its tests. |
Marshall Clow | ca32197 | 2019-09-05 00:38:36 +0000 | [diff] [blame] | 12 | |
Louis Dionne | 089ced0 | 2020-04-07 15:02:37 -0400 | [diff] [blame] | 13 | The primary way to run the libc++ tests is by using ``make check-cxx``. |
Marshall Clow | ca32197 | 2019-09-05 00:38:36 +0000 | [diff] [blame] | 14 | |
| 15 | However since libc++ can be used in any number of possible |
| 16 | configurations it is important to customize the way LIT builds and runs |
| 17 | the tests. This guide provides information on how to use LIT directly to |
| 18 | test libc++. |
Eric Fiselier | d720d1f | 2015-08-22 19:40:49 +0000 | [diff] [blame] | 19 | |
| 20 | Please see the `Lit Command Guide`_ for more information about LIT. |
| 21 | |
Sylvestre Ledru | b09c938 | 2020-03-22 22:42:03 +0100 | [diff] [blame] | 22 | .. _LIT Command Guide: https://llvm.org/docs/CommandGuide/lit.html |
Eric Fiselier | d720d1f | 2015-08-22 19:40:49 +0000 | [diff] [blame] | 23 | |
Louis Dionne | 089ced0 | 2020-04-07 15:02:37 -0400 | [diff] [blame] | 24 | Usage |
| 25 | ----- |
Eric Fiselier | d720d1f | 2015-08-22 19:40:49 +0000 | [diff] [blame] | 26 | |
Louis Dionne | 089ced0 | 2020-04-07 15:02:37 -0400 | [diff] [blame] | 27 | After building libc++, you can run parts of the libc++ test suite by simply |
Louis Dionne | e35df79 | 2020-04-15 15:05:14 -0400 | [diff] [blame^] | 28 | running ``llvm-lit`` on a specified test or directory. If you're unsure |
| 29 | whether the required libraries have been built, you can use the |
| 30 | `check-cxx-deps` target. For example: |
Eric Fiselier | 4ac8809 | 2015-10-14 20:44:44 +0000 | [diff] [blame] | 31 | |
| 32 | .. code-block:: bash |
| 33 | |
Louis Dionne | 089ced0 | 2020-04-07 15:02:37 -0400 | [diff] [blame] | 34 | $ cd <monorepo-root> |
Louis Dionne | e35df79 | 2020-04-15 15:05:14 -0400 | [diff] [blame^] | 35 | $ make -C <build> check-cxx-deps # If you want to make sure the targets get rebuilt |
Louis Dionne | 089ced0 | 2020-04-07 15:02:37 -0400 | [diff] [blame] | 36 | $ <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 Fiselier | 4ac8809 | 2015-10-14 20:44:44 +0000 | [diff] [blame] | 39 | |
| 40 | Sometimes you'll want to change the way LIT is running the tests. Custom options |
| 41 | can be specified using the `--param=<name>=<val>` flag. The most common option |
| 42 | you'll want to change is the standard dialect (ie -std=c++XX). By default the |
| 43 | test suite will select the newest C++ dialect supported by the compiler and use |
| 44 | that. However if you want to manually specify the option like so: |
| 45 | |
| 46 | .. code-block:: bash |
| 47 | |
Louis Dionne | 089ced0 | 2020-04-07 15:02:37 -0400 | [diff] [blame] | 48 | $ <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 Fiselier | 4ac8809 | 2015-10-14 20:44:44 +0000 | [diff] [blame] | 50 | |
| 51 | Occasionally you'll want to add extra compile or link flags when testing. |
| 52 | You can do this as follows: |
| 53 | |
| 54 | .. code-block:: bash |
| 55 | |
Louis Dionne | 089ced0 | 2020-04-07 15:02:37 -0400 | [diff] [blame] | 56 | $ <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 Fiselier | 4ac8809 | 2015-10-14 20:44:44 +0000 | [diff] [blame] | 58 | |
| 59 | Some other common examples include: |
| 60 | |
| 61 | .. code-block:: bash |
| 62 | |
| 63 | # Specify a custom compiler. |
Louis Dionne | 089ced0 | 2020-04-07 15:02:37 -0400 | [diff] [blame] | 64 | $ <build>/bin/llvm-lit -sv libcxx/test/std --param=cxx_under_test=/opt/bin/g++ |
Eric Fiselier | 4ac8809 | 2015-10-14 20:44:44 +0000 | [diff] [blame] | 65 | |
| 66 | # Enable warnings in the test suite |
Louis Dionne | 089ced0 | 2020-04-07 15:02:37 -0400 | [diff] [blame] | 67 | $ <build>/bin/llvm-lit -sv libcxx/test --param=enable_warnings=true |
Eric Fiselier | 4ac8809 | 2015-10-14 20:44:44 +0000 | [diff] [blame] | 68 | |
| 69 | # Use UBSAN when running the tests. |
Louis Dionne | 089ced0 | 2020-04-07 15:02:37 -0400 | [diff] [blame] | 70 | $ <build>/bin/llvm-lit -sv libcxx/test --param=use_sanitizer=Undefined |
Eric Fiselier | 4ac8809 | 2015-10-14 20:44:44 +0000 | [diff] [blame] | 71 | |
Louis Dionne | 089ced0 | 2020-04-07 15:02:37 -0400 | [diff] [blame] | 72 | Using a custom site configuration |
| 73 | --------------------------------- |
| 74 | |
| 75 | By default, the libc++ test suite will use a site configuration that matches |
| 76 | the current CMake configuration. It does so by generating a ``lit.site.cfg`` |
| 77 | file in the build directory from the ``libcxx/test/lit.site.cfg.in`` template, |
| 78 | and pointing ``llvm-lit`` (which is a wrapper around ``llvm/utils/lit/lit.py``) |
| 79 | to 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 |
| 82 | site configuration. To do that, you can use ``--param=libcxx_site_config`` or |
| 83 | the ``LIBCXX_SITE_CONFIG`` environment variable to point to the right site |
| 84 | configuration file. However, you must stop using ``llvm-lit``, or else the |
| 85 | generated ``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 | |
| 93 | In 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`` |
| 95 | module expects. |
Eric Fiselier | d720d1f | 2015-08-22 19:40:49 +0000 | [diff] [blame] | 96 | |
| 97 | LIT Options |
| 98 | =========== |
| 99 | |
| 100 | :program:`lit` [*options*...] [*filenames*...] |
| 101 | |
| 102 | Command Line Options |
| 103 | -------------------- |
| 104 | |
| 105 | To use these options you pass them on the LIT command line as --param NAME or |
| 106 | --param NAME=VALUE. Some options have default values specified during CMake's |
| 107 | configuration. Passing the option on the command line will override the default. |
| 108 | |
| 109 | .. program:: lit |
| 110 | |
Eric Fiselier | 4fe629c | 2016-10-14 06:15:27 +0000 | [diff] [blame] | 111 | .. option:: cxx_under_test=<path/to/compiler> |
Eric Fiselier | 4ac8809 | 2015-10-14 20:44:44 +0000 | [diff] [blame] | 112 | |
| 113 | Specify the compiler used to build the tests. |
| 114 | |
Eric Fiselier | 4fe629c | 2016-10-14 06:15:27 +0000 | [diff] [blame] | 115 | .. option:: cxx_stdlib_under_test=<stdlib name> |
| 116 | |
| 117 | **Values**: libc++, libstdc++ |
Eric Fiselier | bbbbd11 | 2016-10-12 00:00:37 +0000 | [diff] [blame] | 118 | |
| 119 | Specify the C++ standard library being tested. Unless otherwise specified |
| 120 | libc++ is used. This option is intended to allow running the libc++ test |
| 121 | suite against other standard library implementations. |
| 122 | |
Eric Fiselier | 4ac8809 | 2015-10-14 20:44:44 +0000 | [diff] [blame] | 123 | .. option:: std=<standard version> |
| 124 | |
Eric Fiselier | 32f3f70 | 2017-11-07 20:26:23 +0000 | [diff] [blame] | 125 | **Values**: c++98, c++03, c++11, c++14, c++17, c++2a |
Eric Fiselier | 4ac8809 | 2015-10-14 20:44:44 +0000 | [diff] [blame] | 126 | |
| 127 | Change the standard version used when building the tests. |
| 128 | |
Eric Fiselier | 4fe629c | 2016-10-14 06:15:27 +0000 | [diff] [blame] | 129 | .. option:: libcxx_site_config=<path/to/lit.site.cfg> |
Eric Fiselier | d720d1f | 2015-08-22 19:40:49 +0000 | [diff] [blame] | 130 | |
| 131 | Specify the site configuration to use when running the tests. This option |
Eric Fiselier | 17ec5b1 | 2017-05-09 23:47:20 +0000 | [diff] [blame] | 132 | overrides the environment variable LIBCXX_SITE_CONFIG. |
Eric Fiselier | d720d1f | 2015-08-22 19:40:49 +0000 | [diff] [blame] | 133 | |
Eric Fiselier | 4fe629c | 2016-10-14 06:15:27 +0000 | [diff] [blame] | 134 | .. option:: cxx_headers=<path/to/headers> |
Eric Fiselier | d720d1f | 2015-08-22 19:40:49 +0000 | [diff] [blame] | 135 | |
Eric Fiselier | bbbbd11 | 2016-10-12 00:00:37 +0000 | [diff] [blame] | 136 | Specify the c++ standard library headers that are tested. By default the |
| 137 | headers in the source tree are used. |
Eric Fiselier | d720d1f | 2015-08-22 19:40:49 +0000 | [diff] [blame] | 138 | |
Eric Fiselier | 4fe629c | 2016-10-14 06:15:27 +0000 | [diff] [blame] | 139 | .. option:: cxx_library_root=<path/to/lib/> |
Eric Fiselier | d720d1f | 2015-08-22 19:40:49 +0000 | [diff] [blame] | 140 | |
Eric Fiselier | 661b0c1 | 2016-04-20 04:17:39 +0000 | [diff] [blame] | 141 | Specify the directory of the libc++ library to be tested. By default the |
| 142 | library folder of the build directory is used. This option cannot be used |
Eric Fiselier | 60be246 | 2016-12-23 19:09:14 +0000 | [diff] [blame] | 143 | when use_system_cxx_lib is provided. |
Eric Fiselier | 661b0c1 | 2016-04-20 04:17:39 +0000 | [diff] [blame] | 144 | |
| 145 | |
Eric Fiselier | 4fe629c | 2016-10-14 06:15:27 +0000 | [diff] [blame] | 146 | .. option:: cxx_runtime_root=<path/to/lib/> |
Eric Fiselier | 661b0c1 | 2016-04-20 04:17:39 +0000 | [diff] [blame] | 147 | |
| 148 | Specify the directory of the libc++ library to use at runtime. This directory |
| 149 | is not added to the linkers search path. This can be used to compile tests |
| 150 | against one version of libc++ and run them using another. The default value |
Louis Dionne | e70b1dd | 2018-12-14 18:19:14 +0000 | [diff] [blame] | 151 | for this option is `cxx_library_root`. |
Eric Fiselier | d720d1f | 2015-08-22 19:40:49 +0000 | [diff] [blame] | 152 | |
Eric Fiselier | 60be246 | 2016-12-23 19:09:14 +0000 | [diff] [blame] | 153 | .. option:: use_system_cxx_lib=<bool> |
Eric Fiselier | d720d1f | 2015-08-22 19:40:49 +0000 | [diff] [blame] | 154 | |
| 155 | **Default**: False |
| 156 | |
| 157 | Enable or disable testing against the installed version of libc++ library. |
| 158 | Note: This does not use the installed headers. |
| 159 | |
Eric Fiselier | 4fe629c | 2016-10-14 06:15:27 +0000 | [diff] [blame] | 160 | .. option:: use_lit_shell=<bool> |
Eric Fiselier | d720d1f | 2015-08-22 19:40:49 +0000 | [diff] [blame] | 161 | |
| 162 | Enable or disable the use of LIT's internal shell in ShTests. If the |
| 163 | environment variable LIT_USE_INTERNAL_SHELL is present then that is used as |
| 164 | the default value. Otherwise the default value is True on Windows and False |
| 165 | on every other platform. |
| 166 | |
Eric Fiselier | 4fe629c | 2016-10-14 06:15:27 +0000 | [diff] [blame] | 167 | .. option:: compile_flags="<list-of-args>" |
Eric Fiselier | d720d1f | 2015-08-22 19:40:49 +0000 | [diff] [blame] | 168 | |
| 169 | Specify additional compile flags as a space delimited string. |
| 170 | Note: This options should not be used to change the standard version used. |
| 171 | |
Eric Fiselier | 4fe629c | 2016-10-14 06:15:27 +0000 | [diff] [blame] | 172 | .. option:: link_flags="<list-of-args>" |
Eric Fiselier | d720d1f | 2015-08-22 19:40:49 +0000 | [diff] [blame] | 173 | |
| 174 | Specify additional link flags as a space delimited string. |
| 175 | |
Eric Fiselier | 4fe629c | 2016-10-14 06:15:27 +0000 | [diff] [blame] | 176 | .. option:: debug_level=<level> |
Eric Fiselier | d720d1f | 2015-08-22 19:40:49 +0000 | [diff] [blame] | 177 | |
| 178 | **Values**: 0, 1 |
| 179 | |
| 180 | Enable the use of debug mode. Level 0 enables assertions and level 1 enables |
| 181 | assertions and debugging of iterator misuse. |
| 182 | |
| 183 | .. option:: use_sanitizer=<sanitizer name> |
| 184 | |
| 185 | **Values**: Memory, MemoryWithOrigins, Address, Undefined |
| 186 | |
| 187 | Run the tests using the given sanitizer. If LLVM_USE_SANITIZER was given when |
| 188 | building libc++ then that sanitizer will be used by default. |
| 189 | |
| 190 | .. option:: color_diagnostics |
| 191 | |
| 192 | Enable the use of colorized compile diagnostics. If the color_diagnostics |
| 193 | option is specified or the environment variable LIBCXX_COLOR_DIAGNOSTICS is |
| 194 | present then color diagnostics will be enabled. |
| 195 | |
Petr Hosek | 7b9f43a | 2019-02-05 19:50:47 +0000 | [diff] [blame] | 196 | .. option:: llvm_unwinder |
| 197 | |
| 198 | Enable the use of LLVM unwinder instead of libgcc. |
| 199 | |
| 200 | .. option:: builtins_library |
| 201 | |
| 202 | Path to the builtins library to use instead of libgcc. |
| 203 | |
Eric Fiselier | d720d1f | 2015-08-22 19:40:49 +0000 | [diff] [blame] | 204 | |
| 205 | Environment Variables |
| 206 | --------------------- |
| 207 | |
Eric Fiselier | 71646ec | 2016-05-05 08:12:25 +0000 | [diff] [blame] | 208 | .. envvar:: LIBCXX_SITE_CONFIG=<path/to/lit.site.cfg> |
Eric Fiselier | d720d1f | 2015-08-22 19:40:49 +0000 | [diff] [blame] | 209 | |
| 210 | Specify the site configuration to use when running the tests. |
Eric Fiselier | 887b86b | 2016-09-16 03:47:53 +0000 | [diff] [blame] | 211 | Also see `libcxx_site_config`. |
Eric Fiselier | d720d1f | 2015-08-22 19:40:49 +0000 | [diff] [blame] | 212 | |
| 213 | .. envvar:: LIBCXX_COLOR_DIAGNOSTICS |
| 214 | |
| 215 | If ``LIBCXX_COLOR_DIAGNOSTICS`` is defined then the test suite will attempt |
| 216 | to use color diagnostic outputs from the compiler. |
Eric Fiselier | 887b86b | 2016-09-16 03:47:53 +0000 | [diff] [blame] | 217 | Also see `color_diagnostics`. |
Eric Fiselier | 9e3e137 | 2016-07-19 23:07:03 +0000 | [diff] [blame] | 218 | |
Louis Dionne | 9e7cf26 | 2020-04-02 17:14:45 -0400 | [diff] [blame] | 219 | Writing Tests |
| 220 | ------------- |
| 221 | |
| 222 | When writing tests for the libc++ test suite, you should follow a few guidelines. |
| 223 | This will ensure that your tests can run on a wide variety of hardware and under |
| 224 | a wide variety of configurations. We have several unusual configurations such as |
| 225 | building the tests on one host but running them on a different host, which add a |
| 226 | few requirements to the test suite. Here's some stuff you should know: |
| 227 | |
| 228 | - All tests are run in a temporary directory that is unique to that test and |
| 229 | cleaned up after the test is done. |
| 230 | - When a test needs data files as inputs, these data files can be saved in the |
| 231 | repository (when reasonable) and referrenced by the test as |
| 232 | ``// FILE_DEPENDENCIES: <path-to-dependencies>``. Copies of these files or |
| 233 | directories will be made available to the test in the temporary directory |
| 234 | where it is run. |
| 235 | - You should never hardcode a path from the build-host in a test, because that |
| 236 | path will not necessarily be available on the host where the tests are run. |
| 237 | - You should try to reduce the runtime dependencies of each test to the minimum. |
| 238 | For example, requiring Python to run a test is bad, since Python is not |
| 239 | necessarily available on all devices we may want to run the tests on (even |
| 240 | though supporting Python is probably trivial for the build-host). |
| 241 | |
Eric Fiselier | 9e3e137 | 2016-07-19 23:07:03 +0000 | [diff] [blame] | 242 | Benchmarks |
| 243 | ========== |
| 244 | |
| 245 | Libc++ contains benchmark tests separately from the test of the test suite. |
| 246 | The benchmarks are written using the `Google Benchmark`_ library, a copy of which |
| 247 | is stored in the libc++ repository. |
| 248 | |
| 249 | For more information about using the Google Benchmark library see the |
| 250 | `official documentation <https://github.com/google/benchmark>`_. |
| 251 | |
| 252 | .. _`Google Benchmark`: https://github.com/google/benchmark |
| 253 | |
| 254 | Building Benchmarks |
| 255 | ------------------- |
| 256 | |
Eric Fiselier | 93f212c | 2016-08-29 19:50:49 +0000 | [diff] [blame] | 257 | The benchmark tests are not built by default. The benchmarks can be built using |
| 258 | the ``cxx-benchmarks`` target. |
Eric Fiselier | 9e3e137 | 2016-07-19 23:07:03 +0000 | [diff] [blame] | 259 | |
| 260 | An example build would look like: |
| 261 | |
| 262 | .. code-block:: bash |
| 263 | |
| 264 | $ cd build |
Eric Fiselier | 93f212c | 2016-08-29 19:50:49 +0000 | [diff] [blame] | 265 | $ cmake [options] <path to libcxx sources> |
| 266 | $ make cxx-benchmarks |
Eric Fiselier | 9e3e137 | 2016-07-19 23:07:03 +0000 | [diff] [blame] | 267 | |
| 268 | This will build all of the benchmarks under ``<libcxx-src>/benchmarks`` to be |
| 269 | built against the just-built libc++. The compiled tests are output into |
| 270 | ``build/benchmarks``. |
| 271 | |
| 272 | The benchmarks can also be built against the platforms native standard library |
| 273 | using the ``-DLIBCXX_BUILD_BENCHMARKS_NATIVE_STDLIB=ON`` CMake option. This |
| 274 | is useful for comparing the performance of libc++ to other standard libraries. |
| 275 | The compiled benchmarks are named ``<test>.libcxx.out`` if they test libc++ and |
| 276 | ``<test>.native.out`` otherwise. |
| 277 | |
| 278 | Also See: |
| 279 | |
| 280 | * :ref:`Building Libc++ <build instructions>` |
| 281 | * :ref:`CMake Options` |
| 282 | |
| 283 | Running Benchmarks |
| 284 | ------------------ |
| 285 | |
| 286 | The benchmarks must be run manually by the user. Currently there is no way |
| 287 | to run them as part of the build. |
| 288 | |
| 289 | For example: |
| 290 | |
| 291 | .. code-block:: bash |
| 292 | |
| 293 | $ cd build/benchmarks |
Eric Fiselier | 93f212c | 2016-08-29 19:50:49 +0000 | [diff] [blame] | 294 | $ make cxx-benchmarks |
Eric Fiselier | 9e3e137 | 2016-07-19 23:07:03 +0000 | [diff] [blame] | 295 | $ ./algorithms.libcxx.out # Runs all the benchmarks |
| 296 | $ ./algorithms.libcxx.out --benchmark_filter=BM_Sort.* # Only runs the sort benchmarks |
| 297 | |
| 298 | For more information about running benchmarks see `Google Benchmark`_. |