From 862ca6c294ca8bffad441ac6231d4f17a82edc93 Mon Sep 17 00:00:00 2001 From: Boris Bolliet Date: Sun, 29 Sep 2024 20:33:36 +0100 Subject: [PATCH] Update setup.py --- setup.py | 109 +++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 89 insertions(+), 20 deletions(-) diff --git a/setup.py b/setup.py index 434a4a2d..7e93de68 100644 --- a/setup.py +++ b/setup.py @@ -5,6 +5,9 @@ import numpy as np import site import subprocess as sbp +import platform +import shutil + # Get the site-packages path path_install = site.getusersitepackages() if os.path.exists(site.getusersitepackages()) else site.getsitepackages()[0] @@ -59,37 +62,103 @@ def get_gcc_path(): classy_sz_ext.cython_directives = {'language_level': "3" if sys.version_info.major >= 3 else "2"} +# # Custom build_ext class to handle building `libclass.a` +# class ClassyBuildExt(build_ext): +# def run(self): +# run_env = dict(CLASSDIR=path_install, **os.environ.copy()) +# print("Current working directory:", os.getcwd()) + +# # Skip chmod on Windows since it's unnecessary +# if not sys.platform.startswith('win'): +# print("Running chmod to make select_makefile.sh executable") +# sbp.run(["chmod", "+x", "select_makefile.sh"], cwd=os.path.join(os.getcwd(), "class-sz"), env=run_env, check=True) + +# # Run the script to select the correct Makefile +# print("Running the script to select the correct Makefile") +# if os.path.exists(os.path.join(os.getcwd(), "class-sz", "select_makefile.sh")): +# print("Running the script to select the correct Makefile") +# sbp.run(["./select_makefile.sh"], cwd=os.path.join(os.getcwd(), "class-sz"), env=run_env, check=True) +# else: +# raise FileNotFoundError("select_makefile.sh not found in class-sz directory") + +# sbp.run(["./select_makefile.sh"], cwd=os.path.join(os.getcwd(), "class-sz"), env=run_env, check=True) +# # sbp.run(["bash", "./select_makefile.sh"], cwd=os.path.join(os.getcwd(), "class-sz"), env=run_env, check=True) + + +# # Build the library +# print("Building the library") +# result = sbp.run("make libclass.a -j", shell=True, cwd=os.path.join(os.getcwd(), "class-sz"), env=run_env) +# if result.returncode != 0: +# raise RuntimeError("Building libclass.a failed") + +# build_ext.run(self) + + + # Custom build_ext class to handle building `libclass.a` class ClassyBuildExt(build_ext): def run(self): run_env = dict(CLASSDIR=path_install, **os.environ.copy()) print("Current working directory:", os.getcwd()) - # Skip chmod on Windows since it's unnecessary - if not sys.platform.startswith('win'): - print("Running chmod to make select_makefile.sh executable") - sbp.run(["chmod", "+x", "select_makefile.sh"], cwd=os.path.join(os.getcwd(), "class-sz"), env=run_env, check=True) - - # Run the script to select the correct Makefile - print("Running the script to select the correct Makefile") - if os.path.exists(os.path.join(os.getcwd(), "class-sz", "select_makefile.sh")): - print("Running the script to select the correct Makefile") - sbp.run(["./select_makefile.sh"], cwd=os.path.join(os.getcwd(), "class-sz"), env=run_env, check=True) + def select_makefile(): + system = platform.system() + machine = platform.machine() + + makefile_dir = os.path.join(os.getcwd(), "class-sz") # Ensure correct directory + print(f"System: {system}") + print(f"Machine: {machine}") + print(f"Makefile directory: {makefile_dir}") + + # Select the appropriate Makefile based on OS and architecture + if system == "Darwin" and machine == "arm64": + print("Using M1 Makefile") + shutil.copy(os.path.join(makefile_dir, "Makefile_m1"), os.path.join(makefile_dir, "Makefile")) + elif system == "Linux": + print("Using Linux Makefile") + shutil.copy(os.path.join(makefile_dir, "Makefile_linux"), os.path.join(makefile_dir, "Makefile")) + print("Environment Variables:") + print(f"C_INCLUDE_PATH: {os.environ.get('C_INCLUDE_PATH', '')}") + print(f"LD_LIBRARY_PATH: {os.environ.get('LD_LIBRARY_PATH', '')}") + print(f"LIBRARY_PATH: {os.environ.get('LIBRARY_PATH', '')}") + print(f"CFLAGS: {os.environ.get('CFLAGS', '')}") + print(f"LDFLAGS: {os.environ.get('LDFLAGS', '')}") + elif system == "Windows": + print("Using Windows Makefile") + shutil.copy(os.path.join(makefile_dir, "Makefile_windows"), os.path.join(makefile_dir, "Makefile")) + print("Environment Variables:") + print(f"C_INCLUDE_PATH: {os.environ.get('C_INCLUDE_PATH', '')}") + print(f"LIBRARY_PATH: {os.environ.get('LIBRARY_PATH', '')}") + print(f"CFLAGS: {os.environ.get('CFLAGS', '')}") + print(f"LDFLAGS: {os.environ.get('LDFLAGS', '')}") + else: + print(f"OS not supported yet: {system}. See README.md for pre-M1 mac support or get in touch.") + shutil.copy(os.path.join(makefile_dir, "Makefile_m1"), os.path.join(makefile_dir, "Makefile")) + + # Call the Python function to select the correct Makefile + select_makefile() + + # Build the library using `make` command + print("Building the library") + if platform.system() == "Windows": + # On Windows, ensure MinGW or MSYS2 is installed for `make` + # Modify the command if necessary to point to the correct `make` + make_command = "mingw32-make" if shutil.which("mingw32-make") else "make" else: - raise FileNotFoundError("select_makefile.sh not found in class-sz directory") + make_command = "make" - # sbp.run(["./select_makefile.sh"], cwd=os.path.join(os.getcwd(), "class-sz"), env=run_env, check=True) - sbp.run(["bash", "./select_makefile.sh"], cwd=os.path.join(os.getcwd(), "class-sz"), env=run_env, check=True) - - - # Build the library - print("Building the library") - result = sbp.run("make libclass.a -j", shell=True, cwd=os.path.join(os.getcwd(), "class-sz"), env=run_env) - if result.returncode != 0: - raise RuntimeError("Building libclass.a failed") + try: + result = sbp.run(f"{make_command} libclass.a -j", shell=True, cwd=os.path.join(os.getcwd(), "class-sz"), env=run_env) + if result.returncode != 0: + raise RuntimeError("Building libclass.a failed") + except FileNotFoundError as e: + print(f"Error: {e}") + raise RuntimeError(f"Make command not found. Ensure you have `make` installed on your system: {e}") + # Call the original build_ext run method build_ext.run(self) + print(pck_files) # Setup function setup(