Skip to content

[libc][bazel] Add bazel targets for libc/include/... tests. #141150

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ libc_test_library(
"//libc:llvm_libc_macros_stdfix_macros",
"//llvm:Support",
],
# Force linking in this library's `main()` to surface
# a duplicate symbol error if a test defines its own main.
alwayslink = True,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What would happen if you try to link in this library to a test which already has main() before this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'll fail to link with duplicate symbol: main. In the case that a test has it's own main(), I figured it's least surprising to be explicit about which main() a program is using rather than falling back to the unit test framework's main() if there's e.g. a typo somewhere in the test or the test's main() is namespaced or something.

Happy to revert if you think it's unnecessary or there's another reason to avoid it!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, both with and without this change the link would fail with duplicate symbol: main if the unit test which has its own main() would depend on //libc/test/UnitTest:LibcUnitTest. Could you clarify which behavior are you trying to change?

E.g. if a unit test accidentally has its int main() wrapped in a namespace, then both with and without this change the test will instead pick up the main() from the framework, won't it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, both with and without this change the link would fail with duplicate symbol: main if the unit test which has its own main() would depend on //libc/test/UnitTest:LibcUnitTest.

I could be misconfigured on my machine, but AFAICT this isn't true?

Here's some code examples: main...jtstogel:llvm-project:linker-opt-example. My understanding (which may be incorrect) is that the linker won't include the object file unless a symbol is used, so if the test has it's own main(), the unit test framework main will just be ignored.

E.g. if a unit test accidentally has its int main() wrapped in a namespace, then both with and without this change the test will instead pick up the main() from the framework, won't it?

Sorry that example wasn't well formed. I think a better argument for why files like these should set alwayslink=True is that the program's semantics falls down to link order. In the following example, what does the test do? I would expect it to immediately fail with duplicate symbol: main, but on my machine the test either fails or passes based on the ordering of [":main1", ":main2"].

cc_test(
  name = "example_test",
  srcs = ["example_test.cc"],
  deps = [":main1", ":main2"]
)

cc_library(name = "main1", srcs = ["main1.cc"])

cc_library(name = "main2", srcs = ["main2.cc"])

main1.cc

int main(void) { return 1; }

main2.cc

int main(void) { return 0; }

example_test.cc

// empty

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At the end of the day, this line of the change doesn't matter so much -- I only included it here since it seemed marginally better and non-controvertial. Happy to remove it if you feel it's worse for whatever reason.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the detailed explanation, I agree that this change is reasonable. Would you mind adding a comment above alwayslink = True to specify that we deliberately force linking in library to ensure the test case doesn't provide their own main()?

)

libc_test_library(
Expand Down
346 changes: 345 additions & 1 deletion utils/bazel/llvm-project-overlay/libc/test/include/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,363 @@

# Tests for LLVM libc public headers.

load("//libc/test:libc_test_rules.bzl", "libc_test")
load("//libc/test:libc_test_rules.bzl", "libc_test", "libc_test_library")

package(default_visibility = ["//visibility:public"])

licenses(["notice"])

libc_test(
name = "assert_test",
srcs = ["assert_test.cpp"],
deps = ["//libc:public_headers_deps"],
)

libc_test(
name = "complex_test",
srcs = ["complex_test.cpp"],
deps = [
"//libc:public_headers_deps",
"//libc/test/UnitTest:fp_test_helpers",
],
)

libc_test_library(
name = "fpclassify_test_fixture",
hdrs = ["FpClassifyTest.h"],
deps = [
"//libc:public_headers_deps",
"//libc/test/UnitTest:LibcUnitTest",
"//libc/test/UnitTest:fp_test_helpers",
],
)

libc_test(
name = "fpclassify_test",
srcs = ["fpclassify_test.cpp"],
deps = [
":fpclassify_test_fixture",
"//libc:public_headers_deps",
],
)

libc_test(
name = "fpclassifyf_test",
srcs = ["fpclassifyf_test.cpp"],
deps = [
":fpclassify_test_fixture",
"//libc:public_headers_deps",
],
)

libc_test(
name = "fpclassifyl_test",
srcs = ["fpclassifyl_test.cpp"],
deps = [
":fpclassify_test_fixture",
"//libc:public_headers_deps",
],
)

libc_test(
name = "fpclassify_c_test",
srcs = ["fpclassify_test.c"],
use_test_framework = False,
deps = ["//libc:public_headers_deps"],
)

libc_test_library(
name = "isfinite_test_fixture",
hdrs = ["IsFiniteTest.h"],
deps = [
"//libc:public_headers_deps",
"//libc/test/UnitTest:LibcUnitTest",
"//libc/test/UnitTest:fp_test_helpers",
],
)

libc_test(
name = "isfinite_test",
srcs = ["isfinite_test.cpp"],
deps = [
":isfinite_test_fixture",
"//libc:public_headers_deps",
],
)

libc_test(
name = "isfinitef_test",
srcs = ["isfinitef_test.cpp"],
deps = [
":isfinite_test_fixture",
"//libc:public_headers_deps",
],
)

libc_test(
name = "isfinitel_test",
srcs = ["isfinitel_test.cpp"],
deps = [
":isfinite_test_fixture",
"//libc:public_headers_deps",
],
)

libc_test(
name = "isfinite_c_test",
srcs = ["isfinite_test.c"],
use_test_framework = False,
deps = ["//libc:public_headers_deps"],
)

libc_test_library(
name = "isinf_test_fixture",
hdrs = ["IsInfTest.h"],
deps = [
"//libc:public_headers_deps",
"//libc/test/UnitTest:LibcUnitTest",
"//libc/test/UnitTest:fp_test_helpers",
],
)

libc_test(
name = "isinf_test",
srcs = ["isinf_test.cpp"],
deps = [
":isinf_test_fixture",
"//libc:public_headers_deps",
],
)

libc_test(
name = "isinff_test",
srcs = ["isinff_test.cpp"],
deps = [
":isinf_test_fixture",
"//libc:public_headers_deps",
],
)

libc_test(
name = "isinfl_test",
srcs = ["isinfl_test.cpp"],
deps = [
":isinf_test_fixture",
"//libc:public_headers_deps",
],
)

libc_test(
name = "isinf_c_test",
srcs = ["isinf_test.c"],
use_test_framework = False,
deps = ["//libc:public_headers_deps"],
)

libc_test_library(
name = "isnan_test_fixture",
hdrs = ["IsNanTest.h"],
deps = [
"//libc:public_headers_deps",
"//libc/test/UnitTest:LibcUnitTest",
"//libc/test/UnitTest:fp_test_helpers",
],
)

libc_test(
name = "isnan_test",
srcs = ["isnan_test.cpp"],
deps = [
":isnan_test_fixture",
"//libc:public_headers_deps",
],
)

libc_test(
name = "isnanf_test",
srcs = ["isnanf_test.cpp"],
deps = [
":isnan_test_fixture",
"//libc:public_headers_deps",
],
)

libc_test(
name = "isnanl_test",
srcs = ["isnanl_test.cpp"],
deps = [
":isnan_test_fixture",
"//libc:public_headers_deps",
],
)

libc_test(
name = "isnan_c_test",
srcs = ["isnan_test.c"],
use_test_framework = False,
deps = ["//libc:public_headers_deps"],
)

libc_test_library(
name = "isnormal_test_fixture",
hdrs = ["IsNormalTest.h"],
deps = [
"//libc:public_headers_deps",
"//libc/test/UnitTest:LibcUnitTest",
"//libc/test/UnitTest:fp_test_helpers",
],
)

libc_test(
name = "isnormal_test",
srcs = ["isnormal_test.cpp"],
deps = [
":isnormal_test_fixture",
"//libc:public_headers_deps",
],
)

libc_test(
name = "isnormalf_test",
srcs = ["isnormalf_test.cpp"],
deps = [
":isnormal_test_fixture",
"//libc:public_headers_deps",
],
)

libc_test(
name = "isnormall_test",
srcs = ["isnormall_test.cpp"],
deps = [
":isnormal_test_fixture",
"//libc:public_headers_deps",
],
)

libc_test(
name = "isnormal_c_test",
srcs = ["isnormal_test.c"],
use_test_framework = False,
deps = ["//libc:public_headers_deps"],
)

libc_test(
name = "issubnormal_c_test",
srcs = ["issubnormal_test.c"],
use_test_framework = False,
deps = ["//libc:public_headers_deps"],
)

libc_test_library(
name = "iszero_test_fixture",
hdrs = ["IsZeroTest.h"],
deps = [
"//libc:public_headers_deps",
"//libc/test/UnitTest:LibcUnitTest",
"//libc/test/UnitTest:fp_test_helpers",
],
)

libc_test(
name = "iszero_test",
srcs = ["iszero_test.cpp"],
deps = [
":iszero_test_fixture",
"//libc:public_headers_deps",
],
)

libc_test(
name = "iszerof_test",
srcs = ["iszerof_test.cpp"],
deps = [
":iszero_test_fixture",
"//libc:public_headers_deps",
],
)

libc_test(
name = "iszerol_test",
srcs = ["iszerol_test.cpp"],
deps = [
":iszero_test_fixture",
"//libc:public_headers_deps",
],
)

libc_test(
name = "iszero_c_test",
srcs = ["iszero_test.c"],
use_test_framework = False,
deps = ["//libc:public_headers_deps"],
)

libc_test_library(
name = "signbit_test_fixture",
hdrs = ["SignbitTest.h"],
deps = [
"//libc:public_headers_deps",
"//libc/test/UnitTest:LibcUnitTest",
"//libc/test/UnitTest:fp_test_helpers",
],
)

libc_test(
name = "signbit_test",
srcs = ["signbit_test.cpp"],
deps = [
":signbit_test_fixture",
"//libc:public_headers_deps",
],
)

libc_test(
name = "signbitf_test",
srcs = ["signbitf_test.cpp"],
deps = [
":signbit_test_fixture",
"//libc:public_headers_deps",
],
)

libc_test(
name = "signbitl_test",
srcs = ["signbitl_test.cpp"],
deps = [
":signbit_test_fixture",
"//libc:public_headers_deps",
],
)

libc_test(
name = "stdbit_test",
srcs = [
"stdbit_stub.h",
"stdbit_test.cpp",
],
deps = ["//libc:public_headers_deps"],
)

libc_test(
name = "signbit_c_test",
srcs = ["signbit_test.c"],
use_test_framework = False,
deps = ["//libc:public_headers_deps"],
)

libc_test(
name = "stdckdint_test",
srcs = ["stdckdint_test.cpp"],
deps = ["//libc:public_headers_deps"],
)

libc_test(
name = "sys_queue_test",
srcs = ["sys/queue_test.cpp"],
deps = [
"//libc:__support_char_vector",
"//libc:__support_cpp_string",
"//libc:public_headers_deps",
],
)
Loading
Loading