Skip to content

Commit

Permalink
simplify setup again
Browse files Browse the repository at this point in the history
  • Loading branch information
tlambert03 committed Dec 8, 2024
1 parent 2e3a16c commit 6c9bab0
Showing 1 changed file with 11 additions and 119 deletions.
130 changes: 11 additions & 119 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,96 +1,12 @@
import glob
import os
from ctypes import util
from itertools import product

from Cython.Build import cythonize
from setuptools import setup
from setuptools.extension import Extension

print("PATH:", os.environ["PATH"])
print("CONDA_PREFIX:", os.environ.get("CONDA_PREFIX"))


def debug_conda_env() -> None:
conda_prefix = os.environ.get("CONDA_PREFIX")
if not conda_prefix:
raise RuntimeError("No active Conda environment found. Ensure Conda is active.")
print()
# Check Gurobi DLL directory
if os.name == "nt":
bin_dir = os.path.join(conda_prefix, "Library", "bin")
else:
bin_dir = os.path.join(conda_prefix, "lib")
print(f"Inspecting bin directory: {bin_dir}")
if os.path.exists(bin_dir):
for file in glob.glob(os.path.join(bin_dir, "*gurobi*")):
print(f" Found: {file}")
for file in glob.glob(os.path.join(bin_dir, "*scip*")):
print(f" Found: {file}")
# print the whole damn directory
for file in glob.glob(os.path.join(bin_dir, "*")):
print(f" Found: {file}")
else:
print("Gurobi bin directory does not exist!")

print()
# Check SCIP include directory
if os.name == "nt":
inc_dir = os.path.join(conda_prefix, "Library", "include", "scip")
else:
inc_dir = os.path.join(conda_prefix, "include", "scip")
print(f"Inspecting include directory: {inc_dir}")
if os.path.exists(inc_dir):
for file in glob.glob(os.path.join(inc_dir, "*")):
print(f" Found: {file}")
else:
print("SCIP include directory does not exist!")
# print parent directory
print(f" Parent directory: {os.path.dirname(inc_dir)}")
for file in glob.glob(os.path.join(os.path.dirname(inc_dir), "*")):
print(f" Found: {file}")
print()


def assert_gurobi_and_scip() -> None:
# Get the Conda environment prefix
conda_prefix = os.environ.get("CONDA_PREFIX")
if not conda_prefix:
raise RuntimeError("CONDA_PREFIX is not set. Ensure Conda is active in CI.")

# Paths to check
gurobi_dll_path = os.path.join(conda_prefix, "Library", "bin", "gurobi*.dll")
scip_header_path = os.path.join(
conda_prefix, "Library", "include", "scip", "scipdefplugins.h"
)

# Verify Gurobi DLL
gurobi_found = any(os.path.exists(path) for path in glob.glob(gurobi_dll_path))
if not gurobi_found:
raise FileNotFoundError(
f"Gurobi DLL not found in {gurobi_dll_path}. Ensure Gurobi is installed."
)

# Verify SCIP header
if not os.path.exists(scip_header_path):
raise FileNotFoundError(
f"SCIP header 'scipdefplugins.h' not found in {scip_header_path}. "
"Ensure SCIP is installed."
)

print("All required files are present.")


debug_conda_env()
if os.name == "nt":
assert_gurobi_and_scip()

# enable test coverage tracing if CYTHON_TRACE is set to a non-zero value
CYTHON_TRACE = int(os.getenv("CYTHON_TRACE") in ("1", "True"))
if not (CONDA_PREFIX := os.environ.get("CONDA_PREFIX")):
raise RuntimeError(
"No active Conda environment found. Activate an environment before running."
)

libraries = ["libscip"] if os.name == "nt" else ["scip"]
include_dirs = ["ilpy/impl"]
Expand All @@ -103,48 +19,24 @@ def assert_gurobi_and_scip() -> None:
# include conda environment windows include/lib if it exists
# this will be done automatically by conda build, but is useful if someone
# tries to build this directly with pip install in a conda environment
if os.name == "nt":
include = os.path.join(CONDA_PREFIX, "Library", "include")
include_dirs.append(include)
library_dirs.append(os.path.join(CONDA_PREFIX, "Library", "lib"))
if not os.path.exists(include):
print(
"WARNING: Conda include directory not found. "
"Ensure you have the Conda environment activated."
)
if os.name == "nt" and "CONDA_PREFIX" in os.environ:
include_dirs.append(os.path.join(os.environ["CONDA_PREFIX"], "Library", "include"))
library_dirs.append(os.path.join(os.environ["CONDA_PREFIX"], "Library", "lib"))

# look for various gurobi versions, which are annoyingly
# suffixed with the version number, and wildcards don't work


def find_conda_gurobi() -> "tuple[str, str] | tuple[None, None]":
# Construct the potential library paths
library_paths = [
os.path.join(str(CONDA_PREFIX), "Library", "bin"), # Windows DLLs
os.path.join(str(CONDA_PREFIX), "lib"), # macOS/Linux
]

for lib_path in library_paths:
if os.path.exists(lib_path):
os.environ["PATH"] += os.pathsep + lib_path # Dynamically add to PATH

# Search for Gurobi in the active Conda environment
for prefix in ("lib", ""):
gurobi_lib = f"{prefix}gurobi110"
if found := util.find_library(gurobi_lib):
return gurobi_lib, found

return (None, None)


GUROBI_LIB, lib_path = find_conda_gurobi()
if GUROBI_LIB is not None:
print(f"FOUND GUROBI library {GUROBI_LIB!r}: {lib_path}")
libraries.append(GUROBI_LIB)
compile_args.append("/DHAVE_GUROBI" if os.name == "nt" else "-DHAVE_GUROBI")
for prefix in ("lib", ""):
gurobi_lib = f"{prefix}gurobi110" # only using gurobi 11 at the moment
if found := util.find_library(gurobi_lib):
print("FOUND GUROBI library: ", found)
libraries.append(gurobi_lib)
compile_args.append("/DHAVE_GUROBI" if os.name == 'nt' else "-DHAVE_GUROBI")
break
else:
print("WARNING: GUROBI library not found")


wrapper = Extension(
"ilpy.wrapper",
sources=["ilpy/wrapper.pyx"],
Expand Down

0 comments on commit 6c9bab0

Please sign in to comment.