Skip to content

A lightweight way to build and embed python to applications

License

Notifications You must be signed in to change notification settings

Denzy7/cpython-lite

Repository files navigation

cpython-lite

A lightweight and easy way to build and embed python to applications

building

Tested on an Arch Linux system using:

Tested on Windows 7 using MinGW from WinLibs. (The official installer is incredibly old and doesn't have libraries to build modern Python)

Download an official tarball from the Python website Version 3.9.*'s have been thoroughly tested and are the recommended tarball.

Now configure the source
If configuring for another system other than x86_x64 Linux (linux64), see the various platform-specific tips in configs directory

cmake -S . -B <out-of-tree-build-directory> -Dpyver=<python-version-using>
e.g.
cmake -S . -B <out-of-tree-build-directory> -Dpyver=3.9.4

This checks if valid configuration files are available for the system you want to build for. Check for valid config files in the configs directory

Patch posixmodule.c if you will be compiling on MinGW (Linux): patch -u Python-<pyver>/Modules/posixmodule.c -i config/posixmodule-<pyver>.patch

If on Windows, adjust Modules/posixmodule.c to include the few lines in config/posixmodule-<pyver>.patch

Windows users need to also specify -G "MinGW Makefiles". Please note the location where you extracted MinGW and add <LOCATION>/bin to your path as environment variable. <LOCATION>/bin should contain mingw32-make.exe

If configuration succeeds, build the source
cmake --build <out-of-tree-build-directory>
You can also pass extra options to speed up builds ( -j$(nproc) )

bootstrapping

Once a build is complete, you need to bootstrap a standard python library for python to use. It contains the basic python scripts python needs to start up.
You can find various bootstrapping packages in the bootstrap directory.

Use the win32 package for a portable solution. You can also use the basic versions but keep in mind it will not work well with embedding especially if using packages i.e. import <package>.<embedded_module>
You can also use a standard python install to bootstrap. However its much better to use a bootstrap package to keep the disk usage minimal

The main executables looks for a bootstrap directory next to it named bootstrap so ensure you extract a bootstrap package there or copy a Python library there.

porting

If you want to create a new platform config files, you will need a copy of the existing pyconfig.h. If porting a similar platform to those in configs directory, just change the defines such as SIZEOF_INT to match the said platform. You can also build the config tool and run it on the target machine to get a list of defines to change.
Its also possible for UNIX platforms to share config.c files. Be sure to use the config.c file for modules. Win32 needs the config.c here to initialize Windows specific modules.

The library CMakeLists.txt can offer some insights and tips for porting.

known issues

  • Causes SIGSEGV on Linux if you compile an extension and import it with py-lite (extension will work with your distro's python installation however)

About

A lightweight way to build and embed python to applications

Resources

License

Stars

Watchers

Forks

Packages

No packages published