-
-
Notifications
You must be signed in to change notification settings - Fork 644
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Compiling Python #141
Comments
This is really exciting. Could you rebase your python27 repo on top of https://github.com/python/cpython so provenance is clearer and I can git diff exactly what you did? Alternatively, some kind of minimal build script showing the steps that are needed would be super helpful. |
https://github.com/ahgamut/cpython/tree/cosmo_py27 Clone the repo and run There are some minor details in the commit messages regarding what I tried to compile, etc. |
Here's the Changing the build process for SQLite was as follows:
Adding
|
The The interpreter couldn't find the standard library because the paths were coded in as absolute paths at compile time. I changed that to use relative paths (i.e. Right now, running Question: does Cosmopolitan handle paths (forward slash on Linux, backslash on Windows) and environment variables (separated by |
Your syntax error in interactive mode might be similar to the errors I experienced in trying to get various shells to run under Windows. Specifically inside Windows console when you hit enter, it will send CRLF, but the interpreter is expecting only LF for end of line, so it interprets the CR as part of the statement. You can test this by hitting Ctrl-J instead of enter. If the syntax error goes away but your cursor ends up in a funny position, then that's the problem. For path conversion, this is done inside I haven't had much time to look at this project over the past few weeks, but if I do get back to it and find any Windows-specific quirks, I'll probably post over on #117 or open a PR. I expect any solutions will be similar for shells, Python interpreter etc. |
The problem is with CRLF: I just tested statements terminating with Ctrl-J on Windows, and those are accepted (I still have to press Enter to run the statement, but at least the statement runs before showing
Python needs to set
Both proceed similarly: check Right now, I've just changed the compile-time absolute path locations to relative paths, and it seems to work ok. Maybe after some reading I can customize |
It's really annoying that POSIX doesn't define a function to search The CRLF thing is a trickier problem. Something you can try is using mintty (easiest way is from Git Bash) as your "terminal" instead of Windows console. I'm not sure, but that might avoid the generation of a CRLF when hitting enter, which would at least be a temporary workaround. Solving the problem inside console is more challenging. Personally I don't see it as very useful that carriage return is parsed as a non-whitespace token, even on UNIX. It seems to me the cleanest solution would be for Python to ignore CR, or treat it the same as a trailing space. That's the approach I went with trying to get ash to work, but I'm not sure if it will have unintended consequences for files with binary data in them. |
Maybe we could "polyfill" this one for UNIX: https://docs.microsoft.com/en-us/windows/win32/api/processenv/nf-processenv-searchpatha It could perhaps be used to handle finding things in the APE's ZIP filesystem too. |
A quick list of the internal modules that can't be compiled yet (full list in the repo README):
|
The Python tokenizer now ignores CR when reading input. No more syntax errors when the APE runs in interactive mode on Windows! |
I have built the APE binary of Python 2.7, you can download it here. |
We could generalize the |
Maybe just skip looking through environment variables when starting up |
As a workaround for Cosmopolitan Python, you can build the APE version of the latest Wasm3 and run Rust Python WebAssembly interpreter in it: $ ./wasm3.com --stack-size 1000000 rustpython.wasm
Welcome to the magnificent Rust Python 0.1.1 interpreter 😱 🖖
>>>>> |
I think getting the I had a look at the
@jart do you (plan to) have an implementation of the above functions in cosmopolitan? I tried to add just |
Contributions are welcome on |
You can find the Python 2.7 APE binary in awesome-cosmo. |
Until now,
I added the standard library to the internal ZIP store, and added the location of the APE as the first entry in
|
Now all the functions related to the
(Edit Not yet there on windows because https://github.com/ahgamut/cpython/tree/cosmo_py36 also works. Python 3.6.14 has another 5 months before EOL though. |
@jart I was trying to
The Win32 API docs say that:
I changed the |
With the above It is also possible to serve dynamic pages: just download Flask and its pure-python dependencies as wheels, and unzip the wheels into the APE at Tested on Windows 10 and Debian Linux. I wrote a summary of the changes made for the Python APE here. |
@jart @pkulchenko I would like to know if it possible to use MbedTLS for the SSL support required in Python. Does MbedTLS have Python bindings? I don't think MbedTLS is a drop-in replacement for OpenSSL (like BoringSSL), is there is a list of equivalent functions somewhere?
The Compiling everything with
1 this is by ignoring failing libraries like |
@ahgamut, I did find the same library, as you already referenced, but I can't comment on the rest, as I haven't used it. |
@jart here's a quick summary of stuff to be examined further:
|
I'm still excited about porting Python and now have time available to help.
Python3000 can no longer be safely ignored. I'd recommend we just do that unless there's big blockers. Or both. It'll make people unhappy if we publish only Python2. Speaking of which, I've decided that I do want to start distributing "Actually Portable Python". I've mentioned before, language authors should ideally incorporate Cosmo into their official release processes. Until that happens, we can demonstrate the demand exists by distributing ourselves.
If you got OpenSSL to build then I'd say stick with that. I chose MbedTLS for redbean because I wanted something tinier and I wouldn't agree to the OpenSSL license. However Python is already huge and it appears OpenSSL finally fixed its license. So it looks good to me. I'd even support checking-in both OpenSSL and Python3 to third party. |
FYI: Anything less than Python 3.5 are EOL now: https://devguide.python.org/#status-of-python-branches |
Changed the name of the issue because we have 4 ports of Python to Cosmopolitan Libc:
|
@ahgamut That sounds amazing! Would it be possible for you to maybe provide a binary of one or two of them, just to make it easier for people to check it out and get excited? (On a mostly unrelated note: The Discord link in https://ahgamut.github.io/2021/07/13/ape-python/ seems to have expired) |
I support @ahgamut distributing Actually Portable Python binaries on his blog. We're already doing release binaries for Actually Portable Perl. Binary releases are hard to pull off gracefully and I think @G4Vi did a great job with that. @stefnotch if you want an Actually Portable Python binary to hold you over in the meantime, there's a link to a We do have a Discord and anyone reading is welcome to join: https://discord.gg/vFdkMdQN Please note this link expires in seven days. You can email [email protected] if you need another one. |
Actually Portable Python (CPython 3.11.4) binaries are available here: https://github.com/ahgamut/superconfigure/releases/tag/z0.0.3 |
They do seem to work on Windows, but you mgiht have to run them from the command line and not explorer. Also it erases the entire line every time I press backspace or plays the system sound for an inpalid keypress, I think it's called if there's nothing to delete. |
I was able to reproduce compiling python.com on my machine from the superconfigure repo. @ahgamut Could this issue be closed now? |
Very well, closing. we can re-open if there are any new major issues with building CPython. If anyone wants to try out a CPython3.11 Actually Portable Executable, you can download one from here: https://github.com/ahgamut/superconfigure/releases/tag/z0.0.24 |
I am trying to compile python.com using the cpython cosmo_py311 branch: https://github.com/ahgamut/cpython/tree/cosmo_py311 After following the instructions and running ./superconfigure i get the following error message: checking whether we are cross compiling... configure: error: in I also attach the config.log: Do you know what might be causing this issue? PS: gcc --version returns gcc (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0 |
@EirikJaccheri I would say the If you'd like to build CPython3.11 with Cosmopolitan Libc from source, I'd recommend trying out my |
Hi, The reason why i tried to use the cosmo_py311 is that there seemed to be support to include C libraries in python.com (specifically i would like to include numpy, clickhouse_connect, pandas, datetime, toml, sys and time) In the superconfigure repo i got the impression that one could only include pure python libraries. Am i wrong? Is it possible to add these libraries? |
the builds in superconfigure also provide C extensions (notably |
Now that Cosmopolitan supports dlopen, can Ctypes be made to work? Mostly curious. |
|
Hi again, I figured out that numpy is not a dependency of clickhouse_connect. To get clickhouse_connect to work i only need two libraries which use C-extensions: zstandard and lz4 (https://pypi.org/project/zstandard/#description and https://pypi.org/project/lz4/#files). @ahgamut Is it possible to build these packages using superconfigure? If so, how? Eirik |
ok, seems like it it can be done, with the following steps:
|
@ahgamut Do you have any plans to add a python single executable cross compiler? Something like |
I'm pretty happy building python via |
Interesting. Are you saying you can already make a single executable of the python app + the python runtime with If so do you mind pointing me to the piece of code that does it and I can try myself? |
The superconfigure build is the single executable I was referring to -- I add the packages/app I need into the CPython source tree. For example: https://github.com/ahgamut/superconfigure/releases/download/z0.0.27/datasette.zip contains a single-file python executable that can run the If I want to add more pure-python libraries to the above executable, I can do it using the mv datasette datasette.com
unzip -vl datasette.com
./datasette.com -m pip download tqdm # sample pure-python library
mkdir -p Lib/site-packages
unzip tqdm*.whl -d ./Lib/site-packages
zip -qr ./datasette.com Lib/site-packages
# now tqdm is part of the python executable
mv datasette.com datasette
rm -rf ./Lib/
./datasette -c 'import tqdm' related screenshot using above executable from superconfigure, following the shell scripts: So the |
Hi again, what i have done:
As you can see i commented out the./configure part of the script since there is no configure script when i untar lz4-1.9.4.tar.gz. When i source source vars/x86_64 and run the superconfigure script i get the following error message: Cleaning completed Can you point me to what i am doing wrong? @ahgamut Also: if i get the compress/lz3-1.9.4/superconfigure script to work, is the only remaining step to unzip the .whl file in Lib/site-packages and run .github/scripts/build/datasette? Thanks for your help, |
@EirikJaccheri I added a build script for Turns out we have to patch the Makefiles on
you mean you managed to build the |
I managed to install lz4 and now clickhouse_connect is working! It would be great if we could add these repositories to the repository. However i am not sure how to submit the pull request. The way i added the file was by manually adding files to python/cpy311-datasette/datasette/Modules/, python/cpy311-datasette/datasette/Modules/ and changing Modules/Setup. (as you can see inn my fork of datasette: https://github.com/EirikJaccheri/cpython/tree/datasette) How can i make it so that the packages are created from the python/cpy311-datasette/superconfigure script? Also, how do i clean the superconfigure repo? Thanks for all your help, |
Nice! can you post a screenshot of the working
I am not sure what
you can clean the superconfigure repo via |
That is indeed the correct library. Here is a screenshot of me importing clickhouse_connect: Great that you will include it in the next release! I work in a research institution and we are planing to use clickhouse connect to upload data from experiments to a centralized server. The ./datasette.com excecutable removes the barrier to entry of installing python with the correct packages, especially from windows:-) |
https://github.com/ahgamut/superconfigure/releases/download/z0.0.28/pypack1.zip |
https://github.com/ahgamut/python27https://github.com/ahgamut/cpython/tree/cosmo_py27
The
assert
macro needs to be changed incosmopolitan.h
to enable compilation (see #138).Afterwards, just clone the repo and run
superconfigure
.Python 2.7.18 compiled seamlessly once I figured out how
autoconf
worked, and what flags were being fed to the source files when runningmake
. I'm pretty sure we can compile any C-based extensions intopython.exe
-- they just need to compiled/linked with Cosmopolitan, with necessary glue code added to the Python source. For example, I was able to compile SQLite intopython.exe
to enable the internal_sqlite
module.The compiled APE is about 4.1MB with
MODE=tiny
(without any of the standard modules, the interpreter alone is around 1.6MB). Most of the modules in the stdlib compile without error. The_socket
module (required for Python's simple HTTP server) doesn't compile, as it requires the structs fromnetdb.h
.On Windows, the APE exits immediately because the intertpreter is unable to find the platform-specific files.
Module/getpath.c
andLib/site.py
in the Python source try to use absolute paths from the prefixes provided during compilation; Editing those files to search the right locations (possibly with somezipos
magic) ought to fix this.The text was updated successfully, but these errors were encountered: