Skip to content

Install Pysvf from Scratch

bjjwwang edited this page Mar 30, 2025 · 4 revisions

Building SVF-Python from Scratch on macOS/Linux

1. Introduction

This document describes how to build SVF-Python from scratch on a Mac/Linux. If you can’t use a pre-built wheel due to environment, network, or other constraints, follow these steps to compile and install the package locally.

Note: These instructions assume macOS with the standard Apple Clang toolchain. Some details may vary based on your Xcode or Command Line Tools setup.

2. Prerequisites

  1. macOS Toolchain

    • Xcode or the Command Line Tools installed. You can verify by running:
      xcode-select --install
  2. Python 3.8 – 3.13

    • Make sure python3 is installed. If using [Homebrew](https://brew.sh/), you can install an up-to-date Python:
      brew install python
    • Alternatively, you can install multiple Python version via pyenv and setup your virtual env.
  3. Pip / Wheel / Setuptools

    • It’s often helpful to have the latest versions:
      python3 -m pip install --upgrade pybind11 pip setuptools wheel
  4. Git

    • Ensure you have Git installed (macOS usually has it by default).

3. Download the Source

Clone the SVF Python repository:

git clone https://github.com/SVF-tools/SVF
cd SVF

then build the SVF library: Release build

bash build.sh shared

Or Debug build

bash bash build.sh shared debug

Then we can see the libSvfLLVM.so and libSvfCore.so under Release-build/lib(or Debug-build/lib).

And we can see these environment variables:

SVF_DIR=/path/to/SVF
LLVM_DIR=/path/to/SVF/llvm-16.0.0.obj
Z3_DIR=/path/to/SVF/z3.obj

Then clone the SVF-Python repository:

git clone https://github.com/SVF-tools/SVF-Python.git
cd SVF-Python

4. Prepare for the Build

We should make sure these variables.

  • SVF_DIR points to the SVF directory, which should be given by the previous step.
  • LLVM_DIR points to the LLVM directory, which should be given by the previous step.
  • Z3_DIR points to the Z3 directory, which should be given by the previous step.
  • PYBIND11_DIR points to the pybind11 directory, which is python3 -m pybind11 --cmakedir.
  • CMAKE_BUILD_TYPE is the build type, which can be Release or Debug, default is Release. It should be the same as the build type of SVF.

Inside the SVF-Python directory:

  1. Clean Up (If Rebuilding)
    If you’ve attempted a previous build, remove cached directories:

    rm -rf build dist pysvf.egg-info 
  2. Adjust setup.py

    • Open the script in a text editor.
    • Update the Python binary path and version, if needed:
      version="0.0.0"
      you can set your version number.
    • Confirm it points to the correct python3 interpreter on your system.
  3. Run the Build Script

SVF_DIR=${SVF_DIR} LLVM_DIR=${LLVM_DIR} Z3_DIR=${Z3_DIR} bash build.sh

6. Installing the Wheel

Once the build is finished:

  1. Check the generated file name in dist/.
  2. Install it via pip:
    python3 -m pip install dist/YOUR_SVF_PYTHON.whl

If installation completes without errors, you should have pysvf available in your Python environment.

7. Quick Test

  1. Change to test_cases:
    cd test_cases
  2. Run the test script:
    python3 test.py
  3. If everything is set up correctly, you’ll see output related to ICFG (e.g., CallICFGNode, RetICFGNode, etc.) from the example bitcode.

Unlike the pip install pre-built wheels from Pypi, it has no dependency installation process. You should see a bunch of output from ICFGNode from the bitcodes.

.... [Other output
CallICFGNode45 {fun: main{ "ln": 21, "cl": 5, "fl": "src/ae_assert_tests/BASIC_array_2d_0-0.c" }}
   call void @svf_assert(i1 noundef zeroext %cmp), !dbg !47 CallICFGNode: { "ln": 21, "cl": 5, "fl": "src/ae_assert_tests/BASIC_array_2d_0-0.c" }

RetICFGNode46 {fun: main{ "ln": 21, "cl": 5, "fl": "src/ae_assert_tests/BASIC_array_2d_0-0.c" }}
   call void @svf_assert(i1 noundef zeroext %cmp), !dbg !47 RetICFGNode: { "ln": 21, "cl": 5, "fl": "src/ae_assert_tests/BASIC_array_2d_0-0.c" }

IntraICFGNode47 {fun: main{ "ln": 22, "cl": 5, "fl": "src/ae_assert_tests/BASIC_array_2d_0-0.c" }}
   ret i32 0, !dbg !48 { "ln": 22, "cl": 5, "fl": "src/ae_assert_tests/BASIC_array_2d_0-0.c" }

8. Troubleshooting

  • Missing or Incompatible CMake
    If the build fails, verify your CMake version (cmake --version).
  • Command Line Tools
    Make sure you installed Xcode or Command Line Tools; missing compilers often cause build issues.
  • Python Development Headers
    If you see errors about missing headers, ensure the correct Python dev headers are installed. (On Homebrew Python, this is usually included by default.)

9. Conclusion

With the above steps, you can build and install SVF-Python on macOS. Once installed, you can import pysvf in your scripts and start using SVF’s static analysis capabilities.

For more in-depth usage, refer to:

Clone this wiki locally