Skip to content

How to build Python wheels on Windows

MatejKastak edited this page Sep 19, 2022 · 2 revisions

Git clone the repositories

git clone https://www.github.com/avast/yari --recursive 

Install Visual Studio Build Tools

Download the bundle from https://visualstudio.microsoft.com/downloads/#other and install it.

Install Python

Install the Python version for the build.

Installers can be found on Python site https://www.python.org/downloads/ .

I installed "Windows installer (64-bit)" variant.

Repeat this step for other Python versions you want to build.

NOTE: I was not able to find Python executables in Program Files, but they seem to be installed in User\AppData\..., for now you only need one python version to be available.

Install nuget.exe

Nuget is a package manager for Windows. It will be used to get YARA dependencies like openssl and jansson.

Go to nuget website and download latest nuget.exe.

I created a directory C:\bin, added it to $PATH and copied nuget.exe here.

Install the YARA dependencies

Go to yari/yari-sys/yara and install dependencies using nuget:

nuget.exe restore .\windows\vs2017\yara.sln

Build libyara

From the Visual Studio Build Tools I pressed the "launch" button which spawned cmd.exe with correct env variables. Default command line is not able to find msbuild binary necessary for the build. Only the build of libyara itself must be executed from this command line. I built other things from git supplied bash shell.

To get the current WindowsTargetPlatformVersion you need to check environment in the command line spawned by build tools. I listed env variables using set and found string similar to 10.0.19041.0.

I found the current PlatformToolset online. It seems that the version corresponds to the version of the build tools. "Build Tools for Visual Studio 2019" should have v142 version.

On windows libyara.{a, so} is called libyara64.lib, this means we have to link against libyara64 in our build.rs.

Example:

msbuild /m /p:Platform=x64 /p:Configuration=Release /p:PlatformToolset="v142" /p:WindowsTargetPlatformVersion="10.0.19041.0" windows\vs2017\yara.sln

Build yari-sys

I was building everything step by step, not the whole package from the start. If you want you can skip to the part where we build Python wheel.

After the YARA lib is built you can build the yari-sys crate.

cd yari-sys
cargo build

Build yari-cli

Similarly to the previous step you can build the CLI application for YARI.

cd yari-cli
cargo build

cargo run
# And now you can test that everything works as expected, at least from the YARI
# shell

Build yari-py

The build happens in the yari-py/ directory. First we need to setup Python venv for the tooling.

python -m venv env

# source it based on your shell
. env/Scripts/activate

Install maturin to build the wheel:

pip install maturin

You can try the code using:

maturin develop

This will install the package into your currently active virtualenv. You can then start the Pyhton shell and test the yari module using for example the following code:

import yari

c = yari.Context()
res = c.eval('hash.sha256("test")')
print(res)

Finally, you can try to build release wheels, the -f flag will try to find other Python interpreters on you system and build wheels against them, so to build wheel for Python3.10 and Python3.9 you need to have both installed. Other flags are self explanatory.

maturin build -f --release --strip

After this you should try the wheels again, to make sure they work also.

Where to get the wheels

The final wheels are located in target/wheels directory.