sighingnow / libclang

(Unofficial) Release libclang (clang.cindex) on pypi.
https://pypi.org/project/libclang
Other
85 stars 23 forks source link

TranslationUnitLoadError: Error parsing translation unit. #54

Closed irfanmekic closed 1 year ago

irfanmekic commented 1 year ago

I am fairly new to using this package and followed the install installation instructions (pip install and changing the call variable in cindex.py file). After running the below code

import clang.cindex index = clang.cindex.Index.create() index.parse("test.cpp")

I get the TranslationUnitLoadError. Any guidance would be greatly appreciated.

I am using an M1 Mac, btw

sighingnow commented 1 year ago

Could you please provide your test.cpp so I can try to reproduce the issue?

Error message would be useful for debugging as well.

d-e-e-p commented 1 year ago

It seems included clang/native/libclang.dylib errors out with this message on recent mac M1 machines. Error goes away after replacing libclang.dylib under clang/native from:

https://github.com/llvm/llvm-project/releases/download/llvmorg-16.0.4/clang+llvm-16.0.4-arm64-apple-darwin22.0.tar.xz

Perhaps we need a github runner on macos-latest to build for current Darwin Kernel Version 22.4.0 ?

sighingnow commented 1 year ago

I have just rebuild version 16.0.4 wheels on cirrus-ci: https://cirrus-ci.com/task/4741189509840896

Not released yet. @d-e-e-p could you please help me to check if the latest cirrus-ci build works on your environment? The artifact can be downloaded from:

https://api.cirrus-ci.com/v1/artifact/task/4741189509840896/wheel/dist/libclang-16.0.4-py2.py3-none-macosx_11_0_arm64.whl

d-e-e-p commented 1 year ago

unfortunately I get the same error:

  File "/opt/homebrew/lib/python3.11/site-packages/clang/cindex.py", line 2897, in parse
    return TranslationUnit.from_source(path, args, unsaved_files, options,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/clang/cindex.py", line 3012, in from_source
    raise TranslationUnitLoadError("Error parsing translation unit.")
clang.cindex.TranslationUnitLoadError: Error parsing translation unit.

I noticed that:

> dyld_info  -platform /opt/homebrew/lib/python3.11/site-packages/clang/native/libclang.dylib
/opt/homebrew/lib/python3.11/site-packages/clang/native/libclang.dylib [arm64]:
    -platform:
        platform     minOS      sdk
           macOS     10.9      12.3

while the pre-built version from https://github.com/llvm/llvm-project/releases looks like:

> dyld_info  -platform clang+llvm-16.0.4-arm64-apple-darwin22.0/lib/libclang.dylib
clang+llvm-16.0.4-arm64-apple-darwin22.0/lib/libclang.dylib [arm64]:
    -platform:
        platform     minOS      sdk
           macOS     13.0      13.3

I'm also able to build from source and get a working version with:

#!/bin/bash -x
# from https://stackoverflow.com/questions/65293299/how-to-build-llvm-clang-clang-for-apple-m1
BUILD_SYSTEM=Ninja
INSTALLDIR=$HOME/.local

cmake ../llvm \
      -G$BUILD_SYSTEM -B build \
      -DCMAKE_OSX_ARCHITECTURES='arm64' \
      -DCMAKE_C_COMPILER=`which clang` \
      -DCMAKE_CXX_COMPILER=`which clang++` \
      -DCMAKE_BUILD_TYPE=Release \
      -DCMAKE_INSTALL_PREFIX=$INSTALLDIR \
      -DLLVM_LOCAL_RPATH=$INSTALLDIR/lib \
      -DLLVM_ENABLE_WERROR=FALSE \
      -DLLVM_TARGETS_TO_BUILD='AArch64' \
      -DLLVM_DEFAULT_TARGET_TRIPLE='16.0.4-arm64-apple-darwin22.4' \
      -DDEFAULT_SYSROOT="$(xcrun --show-sdk-path)" \
      -DLLVM_ENABLE_PROJECTS='clang'
irfanmekic commented 1 year ago

@sighingnow the full error message, along with a test file, are attached Archive.zip

d-e-e-p commented 1 year ago

see https://github.com/sighingnow/libclang/pull/56

d-e-e-p commented 1 year ago

would you prefer a pull request with build procedure on cirrus-ci over github runner?

blueloveTH commented 1 year ago

@d-e-e-p however, you binary on my machine does not work either.

blueloveTH commented 1 year ago

nevermind...seems it is due to the file does not exist..

sighingnow commented 1 year ago

Closing as new wheels has been published to https://pypi.org/project/libclang/16.0.6/#files