A tale of a dependency chain: Pillow and the crew

In this page I’d like to show what dependencies and unexpected issues I’ve found during Pillow Python package porting to win-arm64 investigation and how were these handled.

I think these issues and cross-dependencies will be quite common on the road of win-arm64 enablement, so we can all learn from these to make it more visible and easier to solve.

 

 

Pillow

Pillow is a Python package for image processing, more precisely it’s a fork of PIL (Python Imaging Library).

As Pillow is released by platform specific wheel files (https://pypi.org/project/Pillow/#files ) we’d need to build it from source for win-arm64 and check will it pass out-of-the-box or we’d need to submit changes anywhere to make it work.

Pillow’s build procedure relies on setuptools. As setuptools is updated for win-arm64, that’s good.
Pillow relies on external libraries.
Two of them are mandatory, the rest are optional for passing the tests, but preferred to get full functionality.

https://pillow.readthedocs.io/en/latest/installation.html#building-from-source

External library dependencies

https://pillow.readthedocs.io/en/stable/installation.html#external-libraries

  • libjpeg - required by default

  • zlib - required by default

  • lcms2

  • freetype

  • openjpg

  • libwebp

  • libpng

  • libimagequant

  • libtiff

  • fribidi

So for minimal support, we need to check libjpeg and zlib and for full support we can check all other libraries as well. However as the CI requires all external libraries, accepting win-arm64 as a new platform, still all of them needs to be enabled.

Warning: Any of these 3rd party libraries could depend on other 3rd party libraries as well!

Build from source on Windows

This section shows how Pillow is built from source on Windows when it’s already ported and working on x64 for example. Later on it will shown what’s not working for WoA out-of-the-box and how it’s ported.

Tutorial

python winbuild\build_prepare.py winbuild\build\build_dep_all.cmd winbuild\build\build_pillow.cmd install

Steps

build_prepare.py

It’s a custom Python script, implemeting several things. So if you’d need to update one of the 3rd party libraries and add or change any build step, the logic will be found here.

  1. Download external libraries

    • Example: libjpeg-turbo

      SF_MIRROR = "http://iweb.dl.sourceforge.net" "url": SF_MIRROR + "/project/libjpeg-turbo/2.1.2/libjpeg-turbo-2.1.2.tar.gz",
  2. Create build scripts for external libraries

    • Example: FreeType is built via Visual Studio project
      Generated example build script (on x64 as a reference): Pillow\winbuild\build\build_dep_freetype.cmd

      @echo ====================================================================== @echo ==== Building freetype (freetype-2.10.4) ==== @echo ====================================================================== cd /D C:\kg\stuff\WoA\test\python_packages\github\Pillow\winbuild\build\freetype-2.10.4 set INCLUDE=C:\kg\stuff\WoA\test\python_packages\github\Pillow\winbuild\build\inc set INCLIB=C:\kg\stuff\WoA\test\python_packages\github\Pillow\winbuild\build\lib set LIB=C:\kg\stuff\WoA\test\python_packages\github\Pillow\winbuild\build\lib path %PATH%;C:\kg\stuff\WoA\test\python_packages\github\Pillow\winbuild\build\bin call "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvarsall.bat" x86_amd64 @echo on rmdir /S /Q "objs" "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\MSBuild.exe" builds\windows\vc2010\freetype.sln /t:"Clean" /p:Configuration="Release Static" /p:Platform=x64 /m "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\MSBuild.exe" builds\windows\vc2010\freetype.sln /t:"Build" /p:Configuration="Release Static" /p:Platform=x64 /m xcopy /Y /E "include" "C:\kg\stuff\WoA\test\python_packages\github\Pillow\winbuild\build\inc" copy /Y /B "objs\x64\Release Static\freetype.lib" "C:\kg\stuff\WoA\test\python_packages\github\Pillow\winbuild\build\lib"
  3. Setup environment by vcvars

build_dep_all.cmd

Build all external libraries, by calling all the created scripts.
Dependencies are handled by the order of the calls.

This step can be replaced by individual build_dep_<library>.cmd calls, if any of the optional libraries are not supported, as this script exists in case of error.

Building libraries

The external libraries are built by the following methods, based on the host project’s approach:

Library

Project

Library

Project

lcms2

Visual Studio (2017)

freetype

Visual Studio (2010)

zlib

nmake

libwebp

nmake

libjpeg

cmake + nmake

libpng

cmake + nmake

libimagequant

cmake + nmake

libtiff

cmake + nmake

openjpg

cmake + nmake

fribidi

cmake + nmake

nmake

Basically it’s the Microsoft version of Make.
So any project which includes an nmake file, can be built with MSVC compiler via command line.

build_pillow.cmd

It’s a wrapper for calling setup.py, including external libraries to the environment.

Example on x64 machine:

Testing

Regression tests

Issue found on Windows

The following test case is failed on Windows locally:

It’s explicitly disabled on Linux and for Windows on CI:


It was reported, but basically it’s ignored:

Lesson learnt: Unfortunately, if a package is released for Windows, it doesn’t guarantee all tests are running and passing and/or maintained.

selftest

Port for win-arm64

The basic support required only the vcvars environment setup update for ARM64.

  • recognise ARM64 as a Windows platform by platform.machine

  • call vcvars by x86_arm64 msbuild_architecture

    • even though this architecture sounds strange, it makes sense as the MSVC tools are x86 hosted for ARM64

Tested with all external libraries disabled, replacing the build_pillow script by direct setup.py call:

External libraries

According to Pillow’s tutorial, only libjpeg-turbo and zlib are required external libraries by default, but in order to run regression tests, all libraries are required as the build system fails if any of these are failing.

Library

Status for win-arm64 out-of-the-box

Library

Status for win-arm64 out-of-the-box

lcms2

freetype

zlib

libwebp

libjpeg

libpng

(Pillow build is successful, but Neon optimization disabled)

libimagequant

libtiff

openjpg

fribidi

FreeType

 

FreeType is a widely used library to render fonts:

Host project:

Pillow downloads from (example)

Windows build

FreeType supports various platforms with various build systems, but for Windows it relies on Visual Studio 2010 project:

Test

The tests are only built with the Meson build system, so if we want to test at least locally on win-arm64, we should support Meson as well.

Issue found

One of the test cases wasn’t ported to Windows (not just for win-arm64).

Fix:

Port for win-arm64

Visual Studio build

VS2017 project should be updated to include ARM64 (as this was the first version to support ARM64).

libpng

libpng is the official PNG reference library.

Source:

Complicated

Marked as complicated, because there are multiple ways to include libpng and build on Windows and for Arm. Some of the combinations work for ARM64 out-of-the-box, some don’t.

Short version: For Pillow it’s working, for FreeType it’s not.

Host project’s build system’s on Windows:

  • Visual Studio 2010:

  • nmake script:

FreeType’s Meson build:

  • Custom build script, building with MSVC

Host project

build system

works on win-arm64 out-of-the-box

importing project

Explanation

Host project

build system

works on win-arm64 out-of-the-box

importing project

Explanation

libpng

VS 2010 solution

ARM64 as a new supported platform should be added

libpng

nmake

Pillow

Doesn’t include any Arm related code as it assumes Win == x86

Meson wrapdb

Meson

FreeType

Includes Arm Neon code, because __ARM_NEON__ is defined, but the preprocessing is failing, so it tries to build Armv7 assembly code.

Neon optimization

The Neon configuration makes it also complicated.

Neon optimization was implemented in libpng by:

  • assembly: up to armv7

    • PNG_ARM_NEON_IMPLEMENTATION=2

  • C intrinsics: starting with armv8

    • PNG_ARM_NEON_IMPLEMENTATION=1

Which version is required to be built is decided by preprocessor macros in the assembly, intrinsic C source and by pngpriv.h configuration header ( )

The decision depends on the compiler and the target platform.

Port for win-arm64

libpng

Pull request including all commits:

  • Visual Studio

    • Update to Visual Studio 2019

    • Fix C2220 warning errors for VS2019

    • Add ARM64 as a new platform

    • nmake

      • Create nmake file for win-arm64 to include neon optimizations

    • ARM Neon config fix to recognise win-arm64 as a valid ARM plaftorm

libpng Meson wrapdb
  • Assembly source file requires preprocessing. Gcc granted this by default, while MSVC doesnt, so for MSVC it should be done explicitly by C preprocessor

    • The assembly optimization file is preprocessed, which results empty assembly file then C intrinsic Neon optimization will be compiled, if the work-around is used below.

    • As libpng was inactive at the time I’ve created these patches (and written this summary), the ARM Neon config issue was fixed as a temporary work-around here

      • Even if libpng won’t explicitly support MSVC ARM64 define, we can still support ARM related source code for MSVC.

    • Integrate updated Meson config of libpng:

 

imagequant

Project homepage:

Source code:

This is another specilaized image library: “Palette quantization library that powers pngquant and other PNG optimizers.”

As it’s shown in the summary table up, it’s built by CMake + nmake.

The only problem for win-arm64 is that Intel specific feature, Streaming SIMD Extensions (SSE) is default enabled.

Port for win-arm64

Fix in cmake file: Do not enable SSE feature for ARM64 by default

little cms2 (lcms2)

Project homepage:

Source code:

It’s a small-footprint color management engine.

For Windows, it implements Visual Studio projects:

Port for win-arm64

Add ARM64 to Visual Studio 2019:

 

Integrate updated external libraries into Pillow

As soon as the host project accepted the win-arm64 port and released a new verson, it can be integrated into our consumer project.

FreeType

libimagequant

lcms2

libpng

The host project is still in review at the time this page is last updated, however it’s working for win-arm64 for Pillow, because it builds via nmake, which ignores the Arm related optimization source code. (details: ).

Build tools

 

Meson

Meson is another 3rd party project, which is needed only for building FreeType’s test now.
However, as it’s a generic build system, if we port it, it can be useful for other projects as well.

Port for win-arm64

Invoke arm specific vcvars.bat for arm:

Meson wrapdb

This is a collection of projects that use Meson as their build system, or have a meson port maintained by the Meson team.

It implements 2 functionalities:

  1. Resolve the dependency by downloading the external project

    • example:

  2. Build the project by Meson

    • example:

Debug dll issue

  • Meson creates debug build by default.

  • During Meson config, it creates a simple C program and tries to build and run it.

    • If running it is failing by the following errors, you reproduced the debug dll issue.

Work-arounds

1.Add other paths where the debug dlls are available

 

2.Use release build

with Meson:

Visual Studio CMake

Details:

ninja

Ninja is a widely used build system, probably doesn’t needs to be introduced. It is designed to have its input files generated by a higher-level build system, and it is designed to run builds as fast as possible.

Project homepage:

Source code:

Now it’s required for Meson higher-level build system.

Port for win-arm64

CMake

CMake is also a widely used open-source, cross-platform family of tools designed to build, test and package software.

Project homepage:

Source code:

Bonus

CMake is a bonus investigation in this context, because:

  • x86 version is released and can be used

  • none of the dependant projects require it

    • for Meson it’s optional

    • Ninja Python distribution requires it, but Ninja the stand-alone distribution doesn’t

However, now it’s not critical, still would be nice to understand what is missing for CMake for native win-arm64, if any.

Port for win-arm64

https://linaro.atlassian.net/wiki/spaces/WOAR/pages/28684353912/CMake

OpenSSL

OpenSSL is a software library for applications that secure communications over computer networks against eavesdropping or need to identify the party at the other end. It is widely used by Internet servers, including the majority of HTTPS websites.

For Windows binary releases are available for x86 and x64 also: https://sourceforge.net/projects/openssl/

Project homepage:

Port for win-arm64

Even though emulated x86 binary would work on win-arm64, for native applications (like CMake) a win-arm64 OpenSSL build is needed.

Fortunately the project is already ported for win-arm64, just a local build should be created:

Dependencies

Install x86 Perl:

  • x86 emulated version doesn’t break the native chain here

Steps

The default installation directory is “c:\Program Files\OpenSSL”. CMake is checking that path by default.