Compiling Nuitka C generated code into WASM

221 Views Asked by At

I am trying to compile Python code into C using Nuitka and then use emcc to compile the generated C code into WASM. I have successfully generated C code from a simple HelloWorld.py file using the following command:

python -m nuitka --standalone --output-dir=output HelloWorld.py

This generates both a build and a dist folder in my output directory. The build folder contains several files, including:

__constants.bin
__constants.c
__constants.const
__constants.h
__constants.o
__helpers.c
__helpers.h
__helpers.o
__loader.c
__loader.o
module.__main__.c
module.__main__.const

and a static_src folder containing:

CompiledFunctionType.c
CompiledFunctionType.o
MainProgram.c
MainProgram.o

I am new to C and I am not sure how to compile all these scattered files into WASM using emcc. I have tried compiling one of the files with the following command:

emcc module.__main__.c -o module.__main__.wasm

but this resulted in several errors due to missing header files from Nuitka (e.g. nuitka/prelude.h and patchlevel.h). I added some paths for emcc to find these implementations, but eventually hit a wall with the following error:

/Users/gui/.pyenv/versions/3.10.0/include/python3.10/pyport.h:746:2: error: "LONG_BIT definition appears wrong for platform (bad gcc/glibc config?)."
  746 | #error "LONG_BIT definition appears wrong for platform (bad gcc/glibc config?)."

This indicates that there is a potential discrepancy between Python or its libraries that were compiled for a different architecture than the one I am currently using, e.g. 32 bit or 64 bit. I have verified that both my python version and compiler version are for 64bit so I couldn't figure out why I was getting this error. Looking to understand if there are any possible solutions for this issue?

Has anyone attempted to compile Nuitka generated C code into WASM using emcc? Is it possible to achieve this? Any recommendations on how to compile the C code into WASM would be greatly appreciated.

Thank you in advance.

Additional Information:

Python version: 3.10.0 
Nuitka version: 1.7.1
Nuitka compilation output: 
Nuitka-Options:INFO: Used command line options: --standalone --output-dir=output HelloWorld.py
Nuitka:INFO: Starting Python compilation with Nuitka '1.7.1' on Python '3.10' commercial grade 'not installed'.
Nuitka:INFO: Completed Python level compilation and optimization.               
Nuitka:INFO: Generating source code for C backend compiler.
Nuitka:INFO: Running data composer tool for optimal constant value handling.
Nuitka:INFO: Running C compilation via Scons.
Nuitka-Scons:INFO: Backend C compiler: /usr/bin/clang (clang).
Nuitka-Scons:INFO: Backend linking program with 6 files (no progress information available for this stage).
Nuitka-Scons:INFO: Compiled 6 C files using ccache.
Nuitka-Scons:INFO: Cached C files (using ccache) with result 'cache hit': 6
Nuitka:INFO: Keeping build directory 'output/HelloWorld.build'.                 
Nuitka:INFO: Created binary that runs on macOS 12.4 (arm64) or higher.
Nuitka:INFO: Successfully created 'output/HelloWorld.dist/HelloWorld.bin'.
0

There are 0 best solutions below