Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix config-settings handling in pip install #9115

Merged
merged 5 commits into from
May 10, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/python_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down
11 changes: 3 additions & 8 deletions python-package/packager/build_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand Down
7 changes: 4 additions & 3 deletions python-package/packager/nativelib.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,6 @@ 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()
Expand All @@ -146,6 +143,10 @@ def locate_or_build_libxgboost(
logger.info("Using system XGBoost: %s", str(libxgboost))
return libxgboost

libxgboost = locate_local_libxgboost(toplevel_dir, logger=logger)
if libxgboost is not None:
return libxgboost

if toplevel_dir.joinpath("cpp_src").exists():
# Source distribution; all C++ source files to be found in cpp_src/
cpp_src_dir = toplevel_dir.joinpath("cpp_src")
Expand Down
3 changes: 2 additions & 1 deletion python-package/packager/pep517.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Comment on lines +82 to +83
Copy link
Collaborator Author

@hcho3 hcho3 May 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I manually tested this change by:

  1. Create a new Conda environment.
  2. Build XGBoost from the source using CMake:
conda activate my_env
cmake .. -GNinja -DCMAKE_INSTALL_PREFIX="$CONDA_PREFIX" -DCMAKE_INSTALL_LIBDIR="lib"
  1. Install libxgboost into the Conda env:
ninja install
  1. Install the Python package with use_system_libxgboost flag:
pip install -v .  --config-settings use_system_libxgboost=True
  1. Inspect $CONDA_PREFIX to ensure that only one copy of libxgboost.so is installed in the Conda environment.
$ find . -name libxgboost.so
./lib/libxgboost.so


with cd(workspace):
wheel_name = hatchling.build.build_wheel(
Expand Down