| 1 |
# This file is licensed under the Apache License v2.0 with LLVM Exceptions. |
| 2 |
# See https://llvm.org/LICENSE.txt for license information. |
| 3 |
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 4 |
|
| 5 |
"""LLVM libc starlark rules for tests. |
| 6 |
|
| 7 |
libc functions are created though the libc_build_rules.bzl:libc_function. |
| 8 |
They come in two flavors: |
| 9 |
- the internal one that is scoped into the `LIBC_NAMESPACE` namespace. |
| 10 |
- the libc one that is the regular C function. |
| 11 |
|
| 12 |
When performing tests we make sure to always use the internal version. |
| 13 |
""" |
| 14 |
|
| 15 |
load("//libc:libc_build_rules.bzl", "libc_common_copts") |
| 16 |
load("//libc:libc_configure_options.bzl", "LIBC_CONFIGURE_OPTIONS") |
| 17 |
|
| 18 |
_FULL_BUILD_COPTS = [ |
| 19 |
"-nostdlib++", |
| 20 |
"-nostdlib", |
| 21 |
"-DLIBC_FULL_BUILD", |
| 22 |
"-DLIBC_COPT_USE_C_ASSERT", |
| 23 |
] |
| 24 |
|
| 25 |
def libc_test( |
| 26 |
name, |
| 27 |
copts = [], |
| 28 |
deps = [], |
| 29 |
local_defines = [], |
| 30 |
use_test_framework = True, |
| 31 |
full_build = False, |
| 32 |
**kwargs): |
| 33 |
"""Add target for a libc test. |
| 34 |
|
| 35 |
Args: |
| 36 |
name: Test target name |
| 37 |
copts: The list of options to add to the C++ compilation command. |
| 38 |
deps: The list of libc functions and libraries to be linked in. |
| 39 |
local_defines: The list of target local_defines if any. |
| 40 |
use_test_framework: Whether to use the libc unit test `main` function. |
| 41 |
full_build: Whether to compile with LIBC_FULL_BUILD and disallow |
| 42 |
use of system headers. This is useful for tests that include both |
| 43 |
LLVM libc headers and proxy headers to avoid conflicting definitions. |
| 44 |
**kwargs: Attributes relevant for a cc_test. |
| 45 |
""" |
| 46 |
deps = deps + [ |
| 47 |
"//libc:__support_macros_config", |
| 48 |
"//libc:__support_libc_errno", |
| 49 |
"//libc:errno", |
| 50 |
"//libc:func_aligned_alloc", |
| 51 |
"//libc:func_free", |
| 52 |
"//libc:func_malloc", |
| 53 |
"//libc:func_realloc", |
| 54 |
] |
| 55 |
if use_test_framework: |
| 56 |
deps = deps + ["//libc/test/UnitTest:LibcUnitTest"] |
| 57 |
|
| 58 |
if full_build: |
| 59 |
copts = copts + _FULL_BUILD_COPTS |
| 60 |
|
| 61 |
native.cc_test( |
| 62 |
name = name, |
| 63 |
local_defines = local_defines + LIBC_CONFIGURE_OPTIONS, |
| 64 |
deps = deps, |
| 65 |
copts = copts + libc_common_copts(), |
| 66 |
linkstatic = 1, |
| 67 |
**kwargs |
| 68 |
) |
| 69 |
|
| 70 |
def libc_test_library(name, copts = [], local_defines = [], **kwargs): |
| 71 |
"""Add target for library used in libc tests. |
| 72 |
|
| 73 |
Args: |
| 74 |
name: Library target name. |
| 75 |
copts: See cc_library.copts. |
| 76 |
local_defines: See cc_library.local_defines. |
| 77 |
**kwargs: Other attributes relevant to cc_library (e.g. "deps"). |
| 78 |
""" |
| 79 |
native.cc_library( |
| 80 |
name = name, |
| 81 |
testonly = True, |
| 82 |
copts = copts + libc_common_copts(), |
| 83 |
local_defines = local_defines + LIBC_CONFIGURE_OPTIONS, |
| 84 |
linkstatic = 1, |
| 85 |
**kwargs |
| 86 |
) |