JonathanSalwan / Triton

Triton is a dynamic binary analysis library. Build your own program analysis tools, automate your reverse engineering, perform software verification or just emulate code.
https://triton-library.github.io
Apache License 2.0
3.4k stars 525 forks source link

Add support for building Python Wheel packages #1145

Closed JonathanSalwan closed 1 year ago

JonathanSalwan commented 2 years ago

This refers to #1144.

Pending tasks

Any help on the following will be much appreciated! :

Building the .whl by default

$ python -m build --wheel
* Creating venv isolated environment...
* Installing packages in isolated environment... (setuptools >= 40.8.0, wheel)
* Getting dependencies for wheel...
running egg_info
creating triton_library.egg-info
[...]
* Installing packages in isolated environment... (wheel)
* Building wheel...
running bdist_wheel
running build
running build_ext
-- The C compiler identification is GNU 11.3.0
-- The CXX compiler identification is GNU 11.3.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
[...]
adding 'triton_library-1.0.0.dist-info/WHEEL'
adding 'triton_library-1.0.0.dist-info/top_level.txt'
adding 'triton_library-1.0.0.dist-info/RECORD'
removing build/bdist.linux-x86_64/wheel
Successfully built triton_library-1.0.0-cp39-cp39-linux_x86_64.whl

Building the .whl with a specific interface

$ LLVM_INTERFACE=On python -m build --wheel

By default, interfaces are defined as:

Installing the package with pip

$ pip install . --user
Processing /home/jonathan/Works/Tools/Triton
  Preparing metadata (setup.py) ... done
Building wheels for collected packages: triton-library
  Building wheel for triton-library (setup.py) ... done
  Created wheel for triton-library: filename=triton_library-1.0.0-cp39-cp39-linux_x86_64.whl size=1518902 sha256=aeb378b42f39a7bff36bc2037aca1fd3fd94d37227609ebd719d6f7afde95ccb
  Stored in directory: /tmp/pip-ephem-wheel-cache-xtz2872_/wheels/07/e0/9a/c02c3eac5862a6b34ec58c12793c45a3eb9827de1c9308c2e3
Successfully built triton-library
Installing collected packages: triton-library
Successfully installed triton-library-1.0.0
JonathanSalwan commented 2 years ago

@cnheitman, I think there were a bug around cmake_args on Linux. Commit 450a345fca636402a2c4b5149f78e8434a4e79fb fixes it (before that, cmake_args already defined were not taken into account). However, with this patch there is another issue when calling copy_file. The commit 404484112f6003ef7375b1d6287b431658424a17 fixes it but I do not know if it breaks something else. Is defining CMAKE_LIBRARY_OUTPUT_DIRECTORY important?

cnheitman commented 2 years ago

Hey @JonathanSalwan o/

Nice!

Indeed, there was a bug there '^^. I just built the .whl for both Ubuntu and Windows and it works fine. Replying to your question, no, CMAKE_LIBRARY_OUTPUT_DIRECTORY does not seem necessary, so let's remove it.

An update on the Bitwuzla support. I tried building it on Windows and I ran into some issue (bitwuzla/bitwuzla/issues/30). To summarize, they are updating the Windows build (code and doc) and it'll be ready in a week or two. I'll work on it again once it's ready. (Btw, I have already built Bitwuzla statically on Linux I'll add it to the python.yml when I have the Windows part working so we generate .whl supporting the same libraries on both OSs.)

JonathanSalwan commented 2 years ago

I've cleaned up CI files. There were duplicates of jobs over different files. Now there is only one linux.yml file that supports wide-integer as a matrix and enables LLVM + Bitwuzla. Thus, I've removed the files: llvm.yml, bitwuzla.yml and wide-integer.yml. I've also renamed cmake.yml to vcpkg.yml which is more explicit.

JonathanSalwan commented 2 years ago

Linux and Windows artifacts for different Python versions have been generated with success.

cnheitman commented 2 years ago

Awesome @JonathanSalwan :D I'll add support for Bitwuzla in Linux later today.

JonathanSalwan commented 2 years ago

\o/. I will try to add macOS tomorrow.

JonathanSalwan commented 2 years ago

Artifacts for macOS are ok

JonathanSalwan commented 2 years ago

LLVM static support for Linux and macOS is ok. For Windows we have to hack here and there.

n0thing2speak commented 1 year ago

m1 chip,macOS 12.4, python3.10.5 arm64 version,it can be compiled successfully, but the following error occurs when using it

Python 3.10.5 | packaged by conda-forge | (main, Jun 14 2022, 07:07:06) [Clang 13.0.1 ] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import triton
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
SystemError: initialization of triton did not return an extension module
cnheitman commented 1 year ago

How did you build it exactly? What interfaces did you turn on?

n0thing2speak commented 1 year ago

I think this might be some problems for the python installed in miniconda? I compiled and installed it successfully with the system's python, and I don't know why.

Here is the cmds:

git clone https://github.com/JonathanSalwan/Triton
cd Triton
mkdir build ; cd build
cmake .. -DCMAKE_INSTALL_PREFIX="$PREFIX" \
  -DCMAKE_C_COMPILER=gcc-12 \
  -DCMAKE_CXX_COMPILER=g++-12 \
  -DBOOST_ROOT=$(brew --prefix boost) \
  -DPYTHON_LIBRARY=/Users/$USER/mambaforge/lib/libpython3.10.dylib \
  -DPYTHON_INCLUDE_DIR=/Users/$USER/mambaforge/include/python3.10
make -j8 
cd ..
python -m build --wheel
cd dist 
pip install triton_library-1.0.0rc1-cp310-cp310-macosx_12_0_arm64.whl --user

This command will have the above problem in python installed by conda, but works well in python with Xcode version

cnheitman commented 1 year ago

First, you don't need to run cmake by your own for building the whl package, python -m build --wheel take care of that. So, you only need to type:

git clone https://github.com/JonathanSalwan/Triton
cd Triton
python -m build --wheel
cd dist 
pip install triton_library-1.0.0rc1-cp310-cp310-macosx_12_0_arm64.whl --user

I think the problem might be related to the python library that is being linked to triton. Can you send me the output of python -m build --wheel (at some point it runs cmake which prints information about which python lib is using).

n0thing2speak commented 1 year ago

In fact, when I don't run CMake, I get a bunch of errors. Sorry, I uninstalled the miniconda environment, and now the python installed from brew runs very well, do you need the arm64 wheel? I uploaded it belo triton_library-1.0.0rc1-cp39-cp39-macosx_12_0_arm64.whl.zip w

cnheitman commented 1 year ago

Hey! An update on this issue.

Btw, I'll do a PR with these changes once the CI on my fork finish running.

cnheitman commented 1 year ago

Hey o/ Another update.

JonathanSalwan commented 1 year ago

You rockz @cnheitman !

cnheitman commented 1 year ago

Last update on this issue.