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

Open
wants to merge 2 commits 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
8 changes: 5 additions & 3 deletions .github/workflows/libclang-python-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ on:
- 'main'
paths:
- 'clang/bindings/python/**'
- 'clang/test/bindings/python/**'
- 'clang/tools/libclang/**'
- 'clang/CMakeList.txt'
- '.github/workflows/libclang-python-tests.yml'
- '.github/workflows/llvm-project-tests.yml'
pull_request:
paths:
- 'clang/bindings/python/**'
- 'clang/test/bindings/python/**'
- 'clang/tools/libclang/**'
- 'clang/CMakeList.txt'
- '.github/workflows/libclang-python-tests.yml'
- '.github/workflows/llvm-project-tests.yml'

Expand All @@ -33,7 +33,9 @@ jobs:
python-version: ["3.8", "3.13"]
uses: ./.github/workflows/llvm-project-tests.yml
with:
build_target: check-clang-python
build_target: libclang
run: |
llvm-lit -v tools/clang/test --filter=bindings.sh
Comment on lines -36 to +38
Copy link
Contributor

@DeinAlptraum DeinAlptraum Jun 19, 2025

Choose a reason for hiding this comment

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

Took me a while, the workflow probably doesn't run because there's an error here:
So far the workflow consisted of a single step, so there was no need to open a steps section, but since we have two steps this now becomes necessary. I.e. you'd need something like this:

    steps:
      - name: Bulid Libclang
        uses: ./.github/workflows/llvm-project-tests.yml
        with:
          build_target: libclang
          projects: clang
          # There is an issue running on "windows-2019".
          # See https://github.com/llvm/llvm-project/issues/76601#issuecomment-1873049082.
          os_list: '["ubuntu-24.04"]'
          python_version: ${{ matrix.python-version }}
      - name: Run tests
        run: llvm-lit -v tools/clang/test --filter=bindings.sh

(there's no need to adapt this now, because we still have other issues as described above)

projects: clang
# There is an issue running on "windows-2019".
# See https://github.com/llvm/llvm-project/issues/76601#issuecomment-1873049082.
Expand Down
1 change: 0 additions & 1 deletion clang/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,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.

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.*}}

# 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']