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 | f58d829 | 2020-03-11 17:03:00 -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 | |
Eric Fiselier | d720d1f | 2015-08-22 19:40:49 +0000 | [diff] [blame] | 24 | Setting up the Environment |
| 25 | -------------------------- |
| 26 | |
| 27 | After building libc++ you must setup your environment to test libc++ using |
| 28 | LIT. |
| 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 Fiselier | 4ac8809 | 2015-10-14 20:44:44 +0000 | [diff] [blame] | 43 | Example Usage |
| 44 | ------------- |
| 45 | |
| 46 | Once you have your environment set up and you have built libc++ you can run |
| 47 | parts of the libc++ test suite by simply running `lit` on a specified test or |
| 48 | directory. 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 | |
| 57 | Sometimes you'll want to change the way LIT is running the tests. Custom options |
| 58 | can be specified using the `--param=<name>=<val>` flag. The most common option |
| 59 | you'll want to change is the standard dialect (ie -std=c++XX). By default the |
| 60 | test suite will select the newest C++ dialect supported by the compiler and use |
| 61 | that. 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 | |
| 68 | Occasionally you'll want to add extra compile or link flags when testing. |
| 69 | You 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 | |
| 76 | Some 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 Fiselier | d720d1f | 2015-08-22 19:40:49 +0000 | [diff] [blame] | 89 | |
| 90 | LIT Options |
| 91 | =========== |
| 92 | |
| 93 | :program:`lit` [*options*...] [*filenames*...] |
| 94 | |
| 95 | Command Line Options |
| 96 | -------------------- |
| 97 | |
| 98 | To 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 |
| 100 | configuration. Passing the option on the command line will override the default. |
| 101 | |
| 102 | .. program:: lit |
| 103 | |
Eric Fiselier | 4fe629c | 2016-10-14 06:15:27 +0000 | [diff] [blame] | 104 | .. option:: cxx_under_test=<path/to/compiler> |
Eric Fiselier | 4ac8809 | 2015-10-14 20:44:44 +0000 | [diff] [blame] | 105 | |
| 106 | Specify the compiler used to build the tests. |
| 107 | |
Eric Fiselier | 4fe629c | 2016-10-14 06:15:27 +0000 | [diff] [blame] | 108 | .. option:: cxx_stdlib_under_test=<stdlib name> |
| 109 | |
| 110 | **Values**: libc++, libstdc++ |
Eric Fiselier | bbbbd11 | 2016-10-12 00:00:37 +0000 | [diff] [blame] | 111 | |
| 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 Fiselier | 4ac8809 | 2015-10-14 20:44:44 +0000 | [diff] [blame] | 116 | .. option:: std=<standard version> |
| 117 | |
Eric Fiselier | 32f3f70 | 2017-11-07 20:26:23 +0000 | [diff] [blame] | 118 | **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] | 119 | |
| 120 | Change the standard version used when building the tests. |
| 121 | |
Eric Fiselier | 4fe629c | 2016-10-14 06:15:27 +0000 | [diff] [blame] | 122 | .. option:: libcxx_site_config=<path/to/lit.site.cfg> |
Eric Fiselier | d720d1f | 2015-08-22 19:40:49 +0000 | [diff] [blame] | 123 | |
| 124 | Specify the site configuration to use when running the tests. This option |
Eric Fiselier | 17ec5b1 | 2017-05-09 23:47:20 +0000 | [diff] [blame] | 125 | overrides the environment variable LIBCXX_SITE_CONFIG. |
Eric Fiselier | d720d1f | 2015-08-22 19:40:49 +0000 | [diff] [blame] | 126 | |
Eric Fiselier | 4fe629c | 2016-10-14 06:15:27 +0000 | [diff] [blame] | 127 | .. option:: cxx_headers=<path/to/headers> |
Eric Fiselier | d720d1f | 2015-08-22 19:40:49 +0000 | [diff] [blame] | 128 | |
Eric Fiselier | bbbbd11 | 2016-10-12 00:00:37 +0000 | [diff] [blame] | 129 | Specify the c++ standard library headers that are tested. By default the |
| 130 | headers in the source tree are used. |
Eric Fiselier | d720d1f | 2015-08-22 19:40:49 +0000 | [diff] [blame] | 131 | |
Eric Fiselier | 4fe629c | 2016-10-14 06:15:27 +0000 | [diff] [blame] | 132 | .. option:: cxx_library_root=<path/to/lib/> |
Eric Fiselier | d720d1f | 2015-08-22 19:40:49 +0000 | [diff] [blame] | 133 | |
Eric Fiselier | 661b0c1 | 2016-04-20 04:17:39 +0000 | [diff] [blame] | 134 | 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 Fiselier | 60be246 | 2016-12-23 19:09:14 +0000 | [diff] [blame] | 136 | when use_system_cxx_lib is provided. |
Eric Fiselier | 661b0c1 | 2016-04-20 04:17:39 +0000 | [diff] [blame] | 137 | |
| 138 | |
Eric Fiselier | 4fe629c | 2016-10-14 06:15:27 +0000 | [diff] [blame] | 139 | .. option:: cxx_runtime_root=<path/to/lib/> |
Eric Fiselier | 661b0c1 | 2016-04-20 04:17:39 +0000 | [diff] [blame] | 140 | |
| 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 Dionne | e70b1dd | 2018-12-14 18:19:14 +0000 | [diff] [blame] | 144 | for this option is `cxx_library_root`. |
Eric Fiselier | d720d1f | 2015-08-22 19:40:49 +0000 | [diff] [blame] | 145 | |
Eric Fiselier | 60be246 | 2016-12-23 19:09:14 +0000 | [diff] [blame] | 146 | .. option:: use_system_cxx_lib=<bool> |
Eric Fiselier | d720d1f | 2015-08-22 19:40:49 +0000 | [diff] [blame] | 147 | |
| 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 Fiselier | 4fe629c | 2016-10-14 06:15:27 +0000 | [diff] [blame] | 153 | .. option:: use_lit_shell=<bool> |
Eric Fiselier | d720d1f | 2015-08-22 19:40:49 +0000 | [diff] [blame] | 154 | |
| 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 Fiselier | 4fe629c | 2016-10-14 06:15:27 +0000 | [diff] [blame] | 160 | .. option:: compile_flags="<list-of-args>" |
Eric Fiselier | d720d1f | 2015-08-22 19:40:49 +0000 | [diff] [blame] | 161 | |
| 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 Fiselier | 4fe629c | 2016-10-14 06:15:27 +0000 | [diff] [blame] | 165 | .. option:: link_flags="<list-of-args>" |
Eric Fiselier | d720d1f | 2015-08-22 19:40:49 +0000 | [diff] [blame] | 166 | |
| 167 | Specify additional link flags as a space delimited string. |
| 168 | |
Eric Fiselier | 4fe629c | 2016-10-14 06:15:27 +0000 | [diff] [blame] | 169 | .. option:: debug_level=<level> |
Eric Fiselier | d720d1f | 2015-08-22 19:40:49 +0000 | [diff] [blame] | 170 | |
| 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 Hosek | 7b9f43a | 2019-02-05 19:50:47 +0000 | [diff] [blame] | 189 | .. 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 Fiselier | d720d1f | 2015-08-22 19:40:49 +0000 | [diff] [blame] | 197 | |
| 198 | Environment Variables |
| 199 | --------------------- |
| 200 | |
Eric Fiselier | 71646ec | 2016-05-05 08:12:25 +0000 | [diff] [blame] | 201 | .. envvar:: LIBCXX_SITE_CONFIG=<path/to/lit.site.cfg> |
Eric Fiselier | d720d1f | 2015-08-22 19:40:49 +0000 | [diff] [blame] | 202 | |
| 203 | Specify the site configuration to use when running the tests. |
Eric Fiselier | 887b86b | 2016-09-16 03:47:53 +0000 | [diff] [blame] | 204 | Also see `libcxx_site_config`. |
Eric Fiselier | d720d1f | 2015-08-22 19:40:49 +0000 | [diff] [blame] | 205 | |
| 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 Fiselier | 887b86b | 2016-09-16 03:47:53 +0000 | [diff] [blame] | 210 | Also see `color_diagnostics`. |
Eric Fiselier | 9e3e137 | 2016-07-19 23:07:03 +0000 | [diff] [blame] | 211 | |
Louis Dionne | 9e7cf26 | 2020-04-02 17:14:45 -0400 | [diff] [blame^] | 212 | Writing Tests |
| 213 | ------------- |
| 214 | |
| 215 | When writing tests for the libc++ test suite, you should follow a few guidelines. |
| 216 | This will ensure that your tests can run on a wide variety of hardware and under |
| 217 | a wide variety of configurations. We have several unusual configurations such as |
| 218 | building the tests on one host but running them on a different host, which add a |
| 219 | few 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 Fiselier | 9e3e137 | 2016-07-19 23:07:03 +0000 | [diff] [blame] | 235 | Benchmarks |
| 236 | ========== |
| 237 | |
| 238 | Libc++ contains benchmark tests separately from the test of the test suite. |
| 239 | The benchmarks are written using the `Google Benchmark`_ library, a copy of which |
| 240 | is stored in the libc++ repository. |
| 241 | |
| 242 | For 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 | |
| 247 | Building Benchmarks |
| 248 | ------------------- |
| 249 | |
Eric Fiselier | 93f212c | 2016-08-29 19:50:49 +0000 | [diff] [blame] | 250 | The benchmark tests are not built by default. The benchmarks can be built using |
| 251 | the ``cxx-benchmarks`` target. |
Eric Fiselier | 9e3e137 | 2016-07-19 23:07:03 +0000 | [diff] [blame] | 252 | |
| 253 | An example build would look like: |
| 254 | |
| 255 | .. code-block:: bash |
| 256 | |
| 257 | $ cd build |
Eric Fiselier | 93f212c | 2016-08-29 19:50:49 +0000 | [diff] [blame] | 258 | $ cmake [options] <path to libcxx sources> |
| 259 | $ make cxx-benchmarks |
Eric Fiselier | 9e3e137 | 2016-07-19 23:07:03 +0000 | [diff] [blame] | 260 | |
| 261 | This will build all of the benchmarks under ``<libcxx-src>/benchmarks`` to be |
| 262 | built against the just-built libc++. The compiled tests are output into |
| 263 | ``build/benchmarks``. |
| 264 | |
| 265 | The benchmarks can also be built against the platforms native standard library |
| 266 | using the ``-DLIBCXX_BUILD_BENCHMARKS_NATIVE_STDLIB=ON`` CMake option. This |
| 267 | is useful for comparing the performance of libc++ to other standard libraries. |
| 268 | The compiled benchmarks are named ``<test>.libcxx.out`` if they test libc++ and |
| 269 | ``<test>.native.out`` otherwise. |
| 270 | |
| 271 | Also See: |
| 272 | |
| 273 | * :ref:`Building Libc++ <build instructions>` |
| 274 | * :ref:`CMake Options` |
| 275 | |
| 276 | Running Benchmarks |
| 277 | ------------------ |
| 278 | |
| 279 | The benchmarks must be run manually by the user. Currently there is no way |
| 280 | to run them as part of the build. |
| 281 | |
| 282 | For example: |
| 283 | |
| 284 | .. code-block:: bash |
| 285 | |
| 286 | $ cd build/benchmarks |
Eric Fiselier | 93f212c | 2016-08-29 19:50:49 +0000 | [diff] [blame] | 287 | $ make cxx-benchmarks |
Eric Fiselier | 9e3e137 | 2016-07-19 23:07:03 +0000 | [diff] [blame] | 288 | $ ./algorithms.libcxx.out # Runs all the benchmarks |
| 289 | $ ./algorithms.libcxx.out --benchmark_filter=BM_Sort.* # Only runs the sort benchmarks |
| 290 | |
| 291 | For more information about running benchmarks see `Google Benchmark`_. |