indygreg / python-build-standalone

Produce redistributable builds of Python
Mozilla Public License 2.0
1.99k stars 127 forks source link

Static python lib build? #73

Closed toastersocks closed 3 years ago

toastersocks commented 3 years ago

On macOS, is there a way to build a static python lib (lib/libpython3.9.a) instead of the dynamic python lib (lib/libpython3.9.dylib) this produces by default? I need a static lib for my use-case. When building python the "regular" way, if the shared option is not specified to the configure script, then a static lib is produced by default. I've looked through the docs, but I can't find anything about this. Is there any way to get this to create a static python lib? Thanks,

James

anki-code commented 3 years ago

This question is interesting for me too.

I have a building scripts to build portable xonsh shell - xonsh-portable-binary that based on Alpine (to have musl on board) and nuitka. And to compile statically linked portable xonsh I need statically linked libpython3.8.a.

For now I'm using python-cmake-buildsystem but it has no updates since Python 3.6. Here is how I did it.

After reading #57 I understand that there is a way to build the same with python-build-standalone but I'm not so experienced in building to do it.

@indygreg could you please share the way to build statically linked Python using python-build-standalone? (it will be super cool to have repeatable commands list for the building in docker for example)

Thanks!

indygreg commented 3 years ago

We don't currently include a static libpython in the macOS distributions. Although we probably could, as I believe CPython's build system produces it.

There is enough data in the distributions today to create your own static libpython, however. A .a file is simply a tar-like archive file containing the object files constituting the library. If you use the ar program to create a .a file from all the .o files in the python/build/ directory tree, you effectively have the same static library that the build system would produce.

indygreg commented 3 years ago

Oh, sorry - we do ship the static libpython: it's just in a path like python/install/lib/python3.9/config-3.9-darwin/libpython3.9.a instead of lib/libpython3.9.a. We could add a symlink to the distributions so the more obvious path exists.