-
-
Notifications
You must be signed in to change notification settings - Fork 654
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
Python native extension module support #81
Comments
I would be willing to merge a change that makes it possible to build Python extensions using Cosmopolitan.
If all that doesn't discourage you so far, then contributions are welcome. |
@jart, why limit this to Python extensions? I'd like to be able to build Lua extensions as well and load them into redbean or Lua executable. I'm good with re-compiling Lua modules using cosmopolitan if needed. Also, Lua support should be fairly simple, as it only relies on being able to load the library and find Now that I think more about it, would this allow the same dynamic library to be loaded on all supported platforms? That would be very interesting... |
Because Python extensions have a .py file that gets run before the native code is loaded and we can put the monkey patching code in there, which turns APE blob into an SO/DLL/DYLIB. Without a script it's not possible to polyglot macho/pe/elf. |
We can do the same thing with Lua modules: load .lua file first, which will tweak the blob and then load it. |
@jart, is there a cosmopolitan-provided method to detect the system it's running on? Since the Lua code may need to patch the code differently depending on the platform, what's the best way to find it? Lua provides its own mechanism for this, but it's a compile-time one and since it's compiled on one platform, but runs on multiple ones, I don't think it's going to help here. |
We should fix the Lua provided one. The C functions are |
@jart, so something like the following in luaconf.h:
would need to be replaced with |
Just use forward slash. The system calls automatically change |
It's not (just) for the file IO conversion; Lua stores it in a config variable available from the interpreter (package.config), which is often used for detecting the host system (windows or not). |
Does Lua change its behavior based on that? I want the portability guff to be abstracted by the POSIX interfaces. How about we just add an API to redbean. What do you think of: static int LuaGetHostOs(lua_State *L) {
const char *s;
if (IsLinux()) {
s = "linux";
} else if (IsMetal()) {
s = "metal";
} else if (IsWindows()) {
s = "windows";
} else if (IsXnu()) {
s = "xnu";
} else if (IsOpenbsd()) {
s = "openbsd";
} else if (IsFreebsd()) {
s = "freebsd";
} else if (IsNetbsd()) {
s = "netbsd";
} else {
s = "wut";
}
lua_pushstring(L, s);
return 1;
} |
It does not, as far as I know.
Yes, this should work. Would it be better to have |
Also discussed in #137. As far as I understand, the approach discussed earlier in this ticket and in #137 are slightly different: here @jart is proposing to tweak the binary (by either Python or Lua code that is executed before the library is loaded), so that it can be used by the system loader and in #137 I'm proposing to implement the actual loader that would load ape-based dynamic libraries directly (possibly from memory in addition to the file). This would provide cross-platform support for dynamic libraries, but they would obviously need to be recompiled using the cosmopolitan libraries. |
I have been trying to find a nice way to get The current build process in Cosmopolitan results in
This works for simple CPython extensions: I added the The only issue is step 4. Since Python allows relative imports inside a package, splitting the C and Python parts of a library means I have to ensure that such imports are handled correctly. This usually means creating However, I think the entire process can be automated by correctly customizing the default build process in Python's It would be pretty cool if this was solved: we could create a Python APE, do |
First of all, huge kudos for the amazing work on cosmopolitan.
While I'm still studying how it works, I hope you can pardon me for asking a noob question here. I was wondering if it's possible to compile cross-platform shared libraries (e..g, .so+.dll for Windows/Linux) so you can simply use a single shared library file to load dynamically in other programs e.g., via LoadLibrary on Windows or dlopen on Linux?
The text was updated successfully, but these errors were encountered: