From 902039b70e5b735536f933a2c52b5a448520bfd1 Mon Sep 17 00:00:00 2001 From: Talley Lambert Date: Sun, 15 Dec 2024 12:15:56 -0500 Subject: [PATCH] add back conda search --- ilpy/impl/solvers/SolverFactory.cpp | 10 +++----- setup.py | 38 ++++++++++++++++++++--------- 2 files changed, 31 insertions(+), 17 deletions(-) diff --git a/ilpy/impl/solvers/SolverFactory.cpp b/ilpy/impl/solvers/SolverFactory.cpp index e47db06..2f37cb2 100644 --- a/ilpy/impl/solvers/SolverFactory.cpp +++ b/ilpy/impl/solvers/SolverFactory.cpp @@ -21,15 +21,13 @@ #endif // Platform-specific library names +// These must match the names in setup.py #if defined(_WIN32) || defined(_WIN64) -#define GUROBI_LIB_NAME "ilpy_gurobi.dll" -#define SCIP_LIB_NAME "ilpy_scip.dll" -#elif defined(__APPLE__) +#define GUROBI_LIB_NAME "ilpybackend-gurobi.dll" +#define SCIP_LIB_NAME "ilpybackend-scip.dll" +#else #define GUROBI_LIB_NAME "ilpybackend-gurobi.so" #define SCIP_LIB_NAME "ilpybackend-scip.so" -#else -#define GUROBI_LIB_NAME "ilpy_gurobi.so" -#define SCIP_LIB_NAME "ilpy_scip.so" #endif // Load a library and return a handle diff --git a/setup.py b/setup.py index 857d434..5d212f8 100644 --- a/setup.py +++ b/setup.py @@ -13,18 +13,30 @@ define_macros = [("CYTHON_TRACE", CYTHON_TRACE)] -################ Main wrapper extension ################ - +include_dirs = ["ilpy/impl"] +library_dirs = [] if os.name == "nt": compile_args = ["/O2", "/std:c++17", "/wd4702"] else: compile_args = ["-O3", "-std=c++17", "-Wno-unreachable-code"] + +# 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" 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")) + + +################ Main wrapper extension ################ + + wrapper = Extension( "ilpy.wrapper", sources=["ilpy/wrapper.pyx"], extra_compile_args=compile_args, - include_dirs=["ilpy/impl"], + include_dirs=include_dirs, define_macros=define_macros, ) @@ -37,6 +49,7 @@ ################ Backend extensions ################ + BACKEND_SOURCES = [ "ilpy/impl/solvers/Solution.cpp", "ilpy/impl/solvers/Constraint.cpp", @@ -58,8 +71,9 @@ def _find_lib(lib: str) -> str | None: gurobi_backend = Extension( name="ilpy.ilpybackend-gurobi", sources=["ilpy/impl/solvers/GurobiBackend.cpp", *BACKEND_SOURCES], - include_dirs=["ilpy/impl"], + include_dirs=include_dirs, libraries=[gurobi_lib], + library_dirs=library_dirs, extra_compile_args=compile_args, define_macros=define_macros, ) @@ -67,12 +81,14 @@ def _find_lib(lib: str) -> str | None: else: print("Gurobi library NOT found, skipping Gurobi backend") + if scip_lib := _find_lib("scip"): scip_backend = Extension( name="ilpy.ilpybackend-scip", sources=["ilpy/impl/solvers/ScipBackend.cpp", *BACKEND_SOURCES], - include_dirs=["ilpy/impl"], + include_dirs=include_dirs, libraries=["scip"], + library_dirs=library_dirs, extra_compile_args=compile_args, define_macros=define_macros, ) @@ -82,9 +98,12 @@ def _find_lib(lib: str) -> str | None: ################ Custom build_ext command ################ + +# Custom build_ext command to remove platform-specific tags ("cpython-312-darwin") +# from the generated shared libraries. This makes it easier to discover them + + class CustomBuildExt(build_ext): # type: ignore - # Custom build_ext command to remove platform-specific tags ("cpython-312-darwin") - # from the generated shared libraries. This makes it easier to discover them def get_ext_filename(self, fullname: str) -> str: filename: str = super().get_ext_filename(fullname) if "ilpybackend-" in filename: @@ -94,7 +113,4 @@ def get_ext_filename(self, fullname: str) -> str: return filename -setup( - ext_modules=ext_modules, - cmdclass={"build_ext": CustomBuildExt}, -) +setup(ext_modules=ext_modules, cmdclass={"build_ext": CustomBuildExt})