简体   繁体   中英

Building and running llvm-py on Mac OS X

I was trying to build llvm-py on Mac OS X.

This is what I tried to do, I needed to download 11vm-2.7, and the README file has this comment: Make sure '--enable-pic' is passed to LLVM's 'configure'

  1. Download llvm 2.7 .
  2. Build llvm 2.7: Run ./configure --prefix=LLVM_DIRECTORY --enable-pic
  3. Download llvm-py 0.6 .
  4. Build llvm-py 0.6: Run python setup.py build --llvm-config=LLVM_DIRECTORY/bin/llvm-config

Everything compiles without errors, but when I tried to run test file, I got this error message.

ImportError: 'dlopen(/Library/Python/2.7/site-packages/llvm/_core.so, 2): Symbol not found: __ZTVN4llvm16ExtractValueInstE\\n Referenced from: /Library/Python/2.7/site-packages/llvm/_core.so\\n Expected in: flat namespace\\n in /Library/Python/2.7/site-packages/llvm/_core.so'

The message error seems to say that there is a missing function "llvmExtractValueInst" with flat namemspace issue. What's wrong with this?

In llvm 2.7, the Makefile.rules has this line

SharedLinkOptions=-Wl,-flat_namespace -Wl,-undefined -Wl,suppress \\ -dynamiclib

I tried to remove the flat_namespace , but I got compilation error.

ADDED

Following locojay's answer, I could build brew and llvmpy.

export REQUIRES_RTTI=1
brew install llvm --rtti
sudo pip install git+https://github.com/llvmpy/llvmpy

However, when I tried to execute the examples in the test directory, I still got different kind of error-

test> python example.py 
Traceback (most recent call last):
  File "example.py", line 4, in <module>
    from llvm import *
  File "/Library/Python/2.7/site-packages/llvm/__init__.py", line 11, in <module>
    from llvm import _core
ImportError: dlopen(/Library/Python/2.7/site-packages/llvm/_core.so, 2): Symbol not found: __ZN4llvm10DataLayout2IDE
  Referenced from: /Library/Python/2.7/site-packages/llvm/_core.so
  Expected in: flat namespace
 in /Library/Python/2.7/site-packages/llvm/_core.so 

This is the result when I run otool -L /Library/Python/2.7/site-packages/llvm/_core.so

/Library/Python/2.7/site-packages/llvm/_core.so:
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 159.1.0)
/usr/lib/libstdc++.6.dylib (compatibility version 7.0.0, current version 52.0.0)

have a look @ docs of llvmpy

For LLVM 3.2, make sure that environment variable REQUIRES_RTTI=1 is defined when running make. Otherwise, you may see "undefined symbol: _ZTIN4llvm24PassRegistrationListenerE". Please refer to http://llvm.org/docs/Packaging.html#c-features for details.

so for me this worked

export REQUIRES_RTTI=1
brew install llvm --rtti
pip install git+https://github.com/llvmpy/llvmpy

I guess one can always give anaconda a try in the worst case....

This is how I made it work.

  1. Install llvm with brew
    • export REQUIRES_RTTI=1
    • brew install llvm --enable-shared --with-clang --with-asan --rtti --enable-jit
    • You may skip --enable-jit or --with-clang
  2. Download the llvm-py
    • git clone https://github.com/llvmpy/llvmpy.git
  3. Build llvm-py
    • LLVM_CONFIG_PATH=/usr/local/opt/llvm/bin/llvm-config python setup.py install
    • You may need to check if you don't need to acquire root access for this command
  4. Test
    • python -c 'import llvm; llvm.test()'

It works under llvm 3.3 under OS X Mavericks.

test_scalar_type (llvm.tests.test_type_hash.TestTypeHash) ... ok
test_struct_type (llvm.tests.test_type_hash.TestTypeHash) ... ok
test_uses (llvm.tests.test_uses.TestUses) ... ok
test_volatile (llvm.tests.test_volatile.TestVolatile) ... ok
test_volatile_another (llvm.tests.test_volatile.TestVolatile) ... ok

----------------------------------------------------------------------
Ran 75 tests in 0.249s

OK (skipped=4)
-------------------------------run isolated tests-------------------------------
                    testing llvm.tests.test_intel_native_asm                    
.
----------------------------------------------------------------------
Ran 1 test in 0.008s

OK

I also tried with kaleidoscope , and it seems to work fine:

$ python kaleidoscope.py 
ready> 3+5
Evaluated to: 8.0
ready> 1-34-5
Evaluated to: -38.0
ready> 

With one modification in /Library/Python/2.7/site-packages/llvmpy/capsule.py line 114. I'm not sure if this is harmful or not, but it works with this change.

def release_ownership(old):
    logger.debug('Release %s', old)
    addr = getPointer(old)
    name = getName(old)
    if _addr2dtor.get((name, addr)) is None:
        clsname = getClassName(old)

        if not _pyclasses[clsname]._has_dtor():
            return
            # Guard duplicated release <-- raises an error 
        return
        raise Exception("Already released")
    _addr2dtor[(name, addr)] = None

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM