Skip to content

[clang][python][test] Move python binding tests to lit framework #142948

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

Merged
merged 3 commits into from
Jun 25, 2025
Merged
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
1 change: 0 additions & 1 deletion clang/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,6 @@ if( CLANG_INCLUDE_TESTS )
clang_unit_site_config=${CMAKE_CURRENT_BINARY_DIR}/test/Unit/lit.site.cfg
)
add_subdirectory(test)
add_subdirectory(bindings/python/tests)

if(CLANG_BUILT_STANDALONE)
umbrella_lit_testsuite_end(check-all)
Expand Down
66 changes: 0 additions & 66 deletions clang/bindings/python/tests/CMakeLists.txt

This file was deleted.

11 changes: 11 additions & 0 deletions clang/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,17 @@ add_custom_target(clang-test)
add_dependencies(clang-test check-clang)
set_target_properties(clang-test PROPERTIES FOLDER "Clang/Tests")

# Allow running Clang Python binding tests separately from CI.
add_lit_testsuite(check-clang-python "Running the Clang Python tests"
${CMAKE_CURRENT_BINARY_DIR}
#LIT ${LLVM_LIT}
PARAMS ${CLANG_TEST_PARAMS}
DEPENDS ${CLANG_TEST_DEPS}
ARGS ${CLANG_TEST_EXTRA_ARGS} --filter=bindings.sh
# Avoid running tests twice.
EXCLUDE_FROM_CHECK_ALL
)

# FIXME: This logic can be removed once all buildbots have moved
# debuginfo-test from clang/test to llvm/projects or monorepo.
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/debuginfo-tests)
Expand Down
35 changes: 35 additions & 0 deletions clang/test/bindings/python/bindings.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/bin/sh

# UNSUPPORTED: !libclang-loadable

# Tests fail on Windows, and need someone knowledgeable to fix.
# It's not clear whether it's a test or a valid binding problem.
# XFAIL: target={{.*windows.*}}
Copy link
Collaborator

@dyung dyung Jun 25, 2025

Choose a reason for hiding this comment

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

Should this perhaps be system-windows instead?

This test failed on our Windows hosted, PS4 targeted buildbot.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I suspected something like that initially, but the problem with the llvm-clang-x86_64-sie-win buildbot is different: the build logs show

LLVM host triple: x86_64-pc-windows-msvc
LLVM default target triple: x86_64-sie-ps5

so this is the first instance of a cross-build. I found that lit already has a native feature that can be used here, which would resolve FIXME: Handle CMAKE_CROSSCOMPILING. in bindings.sh: just use

XFAIL: !native

I've never tried a cross-build of clang, so I'd really appreciate if someone with such a setup in place could try this.


# The Python FFI interface is broken on AIX: https://bugs.python.org/issue38628.
# XFAIL: target={{.*-aix.*}}

# AArch64 and Hexagon have known test failures that need to be addressed.
# SystemZ has broken Python/FFI interface:
# https://reviews.llvm.org/D52840#1265716
# XFAIL: target={{(aarch64|hexagon|s390x)-.*}}
# python SEGVs on Linux/sparc64 when loading libclang.so. Seems to be an FFI
# issue, too.
# XFAIL: target={{sparc.*-.*-linux.*}}

# Tests will fail if cross-compiling for a different target, as tests will try
# to use the host Python3_EXECUTABLE and make FFI calls to functions in target
# libraries.
#
# FIXME: Consider a solution that allows better control over these tests in
# a crosscompiling scenario. e.g. registering them with lit to allow them to
# be explicitly skipped via appropriate LIT_ARGS, or adding a mechanism to
# allow specifying a python interpreter compiled for the target that could
# be executed using qemu-user.
#
# FIXME: Handle CMAKE_CROSSCOMPILING.
# Again, might already be handled by libclang-loadable.

# RUN: env PYTHONPATH=%S/../../../bindings/python \
# RUN: CLANG_LIBRARY_PATH=`llvm-config --libdir` \
# RUN: %python -m unittest discover -s %S/tests
39 changes: 39 additions & 0 deletions clang/test/bindings/python/lit.local.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
def is_libclang_loadable():
# Do not try to run if libclang was built with sanitizers because
# the sanitizer library will likely be loaded too late to perform
# interception and will then fail.
# We could use LD_PRELOAD/DYLD_INSERT_LIBRARIES but this isn't
# portable so its easier just to not run the tests when building
# with ASan.
if config.llvm_use_sanitizer != "":
return False
try:
sys.path.append(os.path.join(config.clang_src_dir, "bindings/python"))
from clang.cindex import Config
conf = Config()
Config.set_library_path(config.clang_lib_dir)
conf.lib
return True
except Exception as e:
# Expected failure modes are considered benign when nothing can be
# done about them.
#
# Cannot load a 32-bit libclang.so into a 64-bit python.
if "wrong ELF class: ELFCLASS32" in str(e):
return False
# If libclang.so is missing, it must have been disabled intentionally,
# e.g. by building with LLVM_ENABLE_PIC=OFF.
elif "No such file or directory" in str(e):
return False
# Unexpected failure modes need to be investigated to either fix an
# underlying bug or accept the failure, so return True. This causes
# tests to run and FAIL, drawing developer attention.
else:
print("warning: unhandled failure in is_libclang_loadable: "
+ str(e), file=sys.stderr)
return True

if is_libclang_loadable():
config.available_features.add("libclang-loadable")

config.suffixes = ['.sh']
Loading