Skip to content

Conversation

@matusvalo
Copy link
Contributor

@matusvalo matusvalo commented Aug 16, 2022

This PR integrates functionality into cython command. New parameter --embed-modules is introduced which embeds multiple modules into one executable. Command

cython --embed --embed-modules=lcmath combinatorics.pyx

generates C file which can be compiled and staticaly linked with lcmath module to produce one single executable.

The PR is not migrating the functionality of -p switch of cython_freeze. Moreover, the scope of this PR is not adding documentation and removal of cython_freeze. It will be done in later PRs.

fixes #2849

@matusvalo
Copy link
Contributor Author

matusvalo commented Aug 16, 2022

PR published as draft for review of general approach and review of usage/ergonomics of CLI interface.

TODO:

  • CLI code to be polished - description is missing etc.
  • Unittests to be implemented.

@da-woods
Copy link
Contributor

In general I haven't personally been keen to push cython_freeze type functionality too far. Mainly because I think it has limited use without being able to bundle external non-Cython modules. And supporting that is a huge task that I don't think Cython should be taking on. (I actually think the whole --embed feature was probably a mistake because it gives the expectation that building a "single application" works when mostly it really doesn't)

So I don't know. This extra addition is fairly minor, but I'm not sure if it's a direction Cython really wants to go.

Ignoring that: what commands should I be using here? Is it cython lcmath.pyx first to build lcmath then your command with --embed-modules?

@matusvalo
Copy link
Contributor Author

matusvalo commented Aug 16, 2022

Commands (files are from Demos: https://github.com/cython/cython/tree/master/Demos/freeze):

cython --embed --embed-modules=lcmath combinatorics.pyx # creates combinatorics.c
cython lcmath.pyx # creates lcmath.c

gcc -c combinatorics.c ... -o combinatorics.o # compile combinatorics
gcc -c lcmath.c ... -o lcmath.o # compile lcmath
gcc lcmath.o combinatorics.o -o combinatorics #link them together statically

@matusvalo
Copy link
Contributor Author

matusvalo commented Aug 16, 2022

In general I haven't personally been keen to push cython_freeze type functionality too far. Mainly because I think it has limited use without being able to bundle external non-Cython modules. And supporting that is a huge task that I don't think Cython should be taking on.

Here is my humble point of view:

  • definitely bundling all dependencies is totally out of scope of cython
  • embedding can be interesting for someone who does not want distribute source code (EDIT: but this can be done by compiling all modules and having stupit main python script)
  • there is pretty popular question in stackoverflow regarding embed: https://stackoverflow.com/questions/22507592/making-an-executable-in-cython
  • Currently in cython there is half-baked support of embedding - there is no easy way to compile it (e.g. cythonize or setuptools), poorly documented etc... I think it should be decided to make it usable or remove it as out of scope for cython.

(I actually think the whole --embed feature was probably a mistake because it gives the expectation that building a "single application" works when mostly it really doesn't)

I personally think that this is the result of current state of documentation of this feature. This can be easily fixed by documenting --embed properly and adding one warning bar there.

@scoder
Copy link
Contributor

scoder commented Aug 16, 2022

I've seen people use --embed on the ML from time to time, so I'd not consider it unhelpful as it is.
I concur that it shouldn't cover more complex use cases, though.
I think we can leave the feature in. It doesn't hurt and seems to have a purpose. I also don't think we keep others from building something better by having the simple feature included.

@da-woods
Copy link
Contributor

To clarify, I definitely wasn't suggesting removing any existing features.

This can be easily fixed by documenting --embed properly and adding one warning bar there.

I did add a bit more details on the limitations to the docs a few months ago (76b22ac). So hopefully that's a little better

there is pretty popular question in stackoverflow regarding embed [...]

And the fact it's popular is exactly why I don't like it. Lots of people want to use Cython to create applications, but we can't actually do what they want (often).


So the question here is: is this PR a small enough extension to the existing feature that we can justify adding it. My current view is that I don't think it is (but I don't feel strongly).

@scoder scoder marked this pull request as ready for review October 7, 2025 13:16
@scoder scoder added this to the 3.2 milestone Oct 7, 2025
@scoder
Copy link
Contributor

scoder commented Oct 7, 2025

I think this is a tiny enough feature to just include it. Some users might find it helpful.

@scoder scoder merged commit 1ecb897 into cython:master Oct 15, 2025
79 checks passed
@scoder
Copy link
Contributor

scoder commented Oct 15, 2025

As mentioned, documentation and test are missing.

I don't think we need to remove cython_freeze.py right now. Just leave it where it is until we have a reason to remove it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Integrate "cython_freeze" into "--embed"

3 participants