Created
March 19, 2025 14:28
-
-
Save EncodeTheCode/42d3b226a6cafa2820957d8060577c72 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import sys | |
import subprocess | |
import shutil | |
import argparse | |
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__)) | |
SRC_DIR = os.path.join(PROJECT_ROOT, "src") | |
BUILD_DIR = os.path.join(PROJECT_ROOT, "build") | |
MODULES_DIR = os.path.join(BUILD_DIR, "modules") | |
os.makedirs(MODULES_DIR, exist_ok=True) | |
def get_python_include(): | |
"""Returns the Python include path dynamically.""" | |
return subprocess.check_output( | |
[sys.executable, "-c", "import sysconfig; print(sysconfig.get_path('include'))"], | |
text=True | |
).strip() | |
def compile_cpp_modules(arch): | |
"""Compiles C++ modules into shared libraries (DLL/SO) using GCC.""" | |
cpp_modules = ["module1"] | |
python_include = get_python_include() | |
arch_flag = "-m64" if arch == "64" else "-m32" | |
for module in cpp_modules: | |
cpp_file = os.path.join(SRC_DIR, "modules", f"{module}.cpp") | |
output_file = os.path.join(MODULES_DIR, f"{module}_{arch}.so") | |
if sys.platform == "win32": | |
output_file = os.path.join(MODULES_DIR, f"{module}_{arch}.dll") | |
compile_cmd = [ | |
"gcc", "-O3", "-Wall", "-shared", "-std=c++11", "-fPIC", | |
arch_flag, # Choose 32-bit or 64-bit | |
f"-I{python_include}", cpp_file, "-o", output_file | |
] | |
print(f"πΉ Compiling C++ module ({arch}-bit): {module}...") | |
try: | |
subprocess.run(compile_cmd, check=True) | |
print(f"β {module} ({arch}-bit) compiled successfully!") | |
except subprocess.CalledProcessError: | |
print(f"β Failed to compile {module} ({arch}-bit)") | |
sys.exit(1) | |
def compile_python_to_cpp(arch): | |
"""Compiles Python script into C++ using Transcrypt.""" | |
print(f"πΉ Transpiling Python script to C++ for {arch}-bit...") | |
transcrypt_cmd = [ | |
"transcrypt", | |
"-b", | |
"--nomin", | |
os.path.join(SRC_DIR, "main.py"), | |
] | |
try: | |
subprocess.run(transcrypt_cmd, check=True) | |
print(f"β Python ({arch}-bit) transpiled to C++ successfully!") | |
except subprocess.CalledProcessError: | |
print(f"β Python to C++ transpilation ({arch}-bit) failed!") | |
sys.exit(1) | |
def compile_executable(arch): | |
"""Compiles the transpiled C++ code into an executable.""" | |
print(f"πΉ Compiling C++ executable ({arch}-bit)...") | |
cpp_file = os.path.join(SRC_DIR, "__target__", "main.cpp") | |
output_file = os.path.join(BUILD_DIR, f"my_application_{arch}.exe" if sys.platform == "win32" else f"my_application_{arch}") | |
compile_cmd = [ | |
"gcc", "-O3", "-Wall", "-std=c++11", "-fPIC", | |
"-o", output_file, cpp_file, | |
"-m64" if arch == "64" else "-m32" | |
] | |
try: | |
subprocess.run(compile_cmd, check=True) | |
print(f"β C++ executable ({arch}-bit) compiled successfully!") | |
except subprocess.CalledProcessError: | |
print(f"β C++ executable ({arch}-bit) compilation failed!") | |
sys.exit(1) | |
def finalize_build(arch): | |
"""Finalizes the build and checks the output.""" | |
compiled_exe = os.path.join(BUILD_DIR, f"my_application_{arch}.exe" if sys.platform == "win32" else f"my_application_{arch}") | |
if os.path.exists(compiled_exe): | |
print(f"π Build completed successfully for {arch}-bit!") | |
print(f"πΉ Executable created: {compiled_exe}") | |
else: | |
print(f"β Error: Executable ({arch}-bit) not found!") | |
sys.exit(1) | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser(description="Python to C++ Compiler") | |
parser.add_argument("--arch", choices=["32", "64", "both"], default="64", help="Choose 32-bit, 64-bit, or both architectures") | |
args = parser.parse_args() | |
architectures = ["32", "64"] if args.arch == "both" else [args.arch] | |
for arch in architectures: | |
compile_cpp_modules(arch) | |
compile_python_to_cpp(arch) | |
compile_executable(arch) | |
finalize_build(arch) | |
print("β All requested builds completed successfully!") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment