From 0cd4382d7266bf04bbd28ada5a4c5f0271d6eacf Mon Sep 17 00:00:00 2001 From: Philip Hyunsu Cho Date: Tue, 9 May 2023 17:54:20 -0700 Subject: [PATCH] Fix config-settings handling in pip install (#9115) * Fix config_settings handling in pip install * Fix formatting * Fix flag use_system_libxgboost * Add setuptools to doc requirements.txt * Fix mypy --- .github/workflows/python_tests.yml | 2 +- doc/requirements.txt | 3 ++- python-package/packager/build_config.py | 11 +++-------- python-package/packager/nativelib.py | 15 ++++++++------- python-package/packager/pep517.py | 3 ++- 5 files changed, 16 insertions(+), 18 deletions(-) diff --git a/.github/workflows/python_tests.yml b/.github/workflows/python_tests.yml index 78a17d3f7..98dc1b468 100644 --- a/.github/workflows/python_tests.yml +++ b/.github/workflows/python_tests.yml @@ -66,7 +66,7 @@ jobs: cd python-package python --version python -m build --sdist - pip install -v ./dist/xgboost-*.tar.gz + pip install -v ./dist/xgboost-*.tar.gz --config-settings use_openmp=False cd .. python -c 'import xgboost' diff --git a/doc/requirements.txt b/doc/requirements.txt index 720ff91a6..667ef268f 100644 --- a/doc/requirements.txt +++ b/doc/requirements.txt @@ -11,4 +11,5 @@ myst-parser xgboost_ray sphinx-gallery pyspark -cloudpickle \ No newline at end of file +cloudpickle +setuptools diff --git a/python-package/packager/build_config.py b/python-package/packager/build_config.py index 290cf15db..26392a897 100644 --- a/python-package/packager/build_config.py +++ b/python-package/packager/build_config.py @@ -26,23 +26,18 @@ class BuildConfiguration: # pylint: disable=R0902 # Special option: See explanation below use_system_libxgboost: bool = False - def _set_config_setting( - self, config_settings: Dict[str, Any], field_name: str - ) -> None: - if field_name in config_settings: + def _set_config_setting(self, config_settings: Dict[str, Any]) -> None: + for field_name in config_settings: setattr( self, field_name, (config_settings[field_name].lower() in ["true", "1", "on"]), ) - else: - raise ValueError(f"Field {field_name} is not a valid config_settings") def update(self, config_settings: Optional[Dict[str, Any]]) -> None: """Parse config_settings from Pip (or other PEP 517 frontend)""" if config_settings is not None: - for field_name in [x.name for x in dataclasses.fields(self)]: - self._set_config_setting(config_settings, field_name) + self._set_config_setting(config_settings) def get_cmake_args(self) -> List[str]: """Convert build configuration to CMake args""" diff --git a/python-package/packager/nativelib.py b/python-package/packager/nativelib.py index f7f5b4e79..f1708d6c5 100644 --- a/python-package/packager/nativelib.py +++ b/python-package/packager/nativelib.py @@ -130,20 +130,21 @@ def locate_or_build_libxgboost( """Locate libxgboost; if not exist, build it""" logger = logging.getLogger("xgboost.packager.locate_or_build_libxgboost") - libxgboost = locate_local_libxgboost(toplevel_dir, logger=logger) - if libxgboost is not None: - return libxgboost if build_config.use_system_libxgboost: # Find libxgboost from system prefix sys_prefix = pathlib.Path(sys.prefix).absolute().resolve() - libxgboost = sys_prefix / "lib" / _lib_name() - if not libxgboost.exists(): + libxgboost_sys = sys_prefix / "lib" / _lib_name() + if not libxgboost_sys.exists(): raise RuntimeError( f"use_system_libxgboost was specified but {_lib_name()} is " - f"not found in {libxgboost.parent}" + f"not found in {libxgboost_sys.parent}" ) - logger.info("Using system XGBoost: %s", str(libxgboost)) + logger.info("Using system XGBoost: %s", str(libxgboost_sys)) + return libxgboost_sys + + libxgboost = locate_local_libxgboost(toplevel_dir, logger=logger) + if libxgboost is not None: return libxgboost if toplevel_dir.joinpath("cpp_src").exists(): diff --git a/python-package/packager/pep517.py b/python-package/packager/pep517.py index 56583e117..2c4f9e3e6 100644 --- a/python-package/packager/pep517.py +++ b/python-package/packager/pep517.py @@ -79,7 +79,8 @@ def build_wheel( libxgboost = locate_or_build_libxgboost( TOPLEVEL_DIR, build_dir=build_dir, build_config=build_config ) - copy_with_logging(libxgboost, lib_path, logger=logger) + if not build_config.use_system_libxgboost: + copy_with_logging(libxgboost, lib_path, logger=logger) with cd(workspace): wheel_name = hatchling.build.build_wheel(